blob: c59d08fc54db53d4f01a7482dcbd2d269db23399 [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 Moolenaar572cb562005-08-05 21:35:02 +000065typedef struct Completion compl_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000066struct Completion
67{
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 Moolenaar8b6144b2006-02-08 09:20:24 +000071 char_u *cp_extra; /* extra menu text (allocated, can be NULL) */
72 char_u *cp_info; /* verbose info (can be NULL) */
73 char_u cp_kind; /* kind of match, single letter, or NUL */
74 char_u *cp_fname; /* file containing the match, allocated when
75 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000076 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
77 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078};
79
Bram Moolenaar572cb562005-08-05 21:35:02 +000080#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#define FREE_FNAME (2)
82
83/*
84 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000085 * "compl_first_match" points to the start of the list.
86 * "compl_curr_match" points to the currently selected entry.
87 * "compl_shown_match" is different from compl_curr_match during
88 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000090static compl_T *compl_first_match = NULL;
91static compl_T *compl_curr_match = NULL;
92static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
Bram Moolenaara6557602006-02-04 22:43:20 +000094/* When "compl_leader" is not NULL only matches that start with this string
95 * are used. */
96static char_u *compl_leader = NULL;
97
98static int compl_used_match; /* Selected one of the matches. When
99 FALSE the match was edited or using
100 the longest common string. */
101
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000102/* When the first completion is done "compl_started" is set. When it's
103 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000104static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000105
Bram Moolenaar572cb562005-08-05 21:35:02 +0000106static int compl_matches = 0;
107static char_u *compl_pattern = NULL;
108static int compl_direction = FORWARD;
109static int compl_shows_dir = FORWARD;
110static int compl_pending = FALSE;
111static pos_T compl_startpos;
112static colnr_T compl_col = 0; /* column where the text starts
113 * that is being completed */
114static int save_sm = -1;
115static char_u *compl_orig_text = NULL; /* text as it was before
116 * completion started */
117static int compl_cont_mode = 0;
118static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000120static void ins_ctrl_x __ARGS((void));
121static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000122static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000124static void ins_compl_upd_pum __ARGS((void));
125static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000126static int pum_wanted __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000127static int pum_two_or_more __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000128static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129static void ins_compl_free __ARGS((void));
130static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000131static int ins_compl_bs __ARGS((void));
132static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000133static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000134static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000136static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137static void ins_compl_delete __ARGS((void));
138static void ins_compl_insert __ARGS((void));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000139static int ins_compl_next __ARGS((int allow_get_expansion, int count));
140static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000141static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000142static int ins_compl_key2count __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static int ins_complete __ARGS((int c));
144static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
145#endif /* FEAT_INS_EXPAND */
146
147#define BACKSPACE_CHAR 1
148#define BACKSPACE_WORD 2
149#define BACKSPACE_WORD_NOT_SPACE 3
150#define BACKSPACE_LINE 4
151
152static void ins_redraw __ARGS((void));
153static void ins_ctrl_v __ARGS((void));
154static void undisplay_dollar __ARGS((void));
155static void insert_special __ARGS((int, int, int));
156static void check_auto_format __ARGS((int));
157static void redo_literal __ARGS((int c));
158static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000159#ifdef FEAT_SYN_HL
160static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000161static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000162static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
165static int echeck_abbr __ARGS((int));
166static void replace_push_off __ARGS((int c));
167static int replace_pop __ARGS((void));
168static void replace_join __ARGS((int off));
169static void replace_pop_ins __ARGS((void));
170#ifdef FEAT_MBYTE
171static void mb_replace_pop_ins __ARGS((int cc));
172#endif
173static void replace_flush __ARGS((void));
174static void replace_do_bs __ARGS((void));
175#ifdef FEAT_CINDENT
176static int cindent_on __ARGS((void));
177#endif
178static void ins_reg __ARGS((void));
179static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000180static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000181static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#ifdef FEAT_RIGHTLEFT
183static void ins_ctrl_ __ARGS((void));
184#endif
185#ifdef FEAT_VISUAL
186static int ins_start_select __ARGS((int c));
187#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000188static void ins_insert __ARGS((int replaceState));
189static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190static void ins_shift __ARGS((int c, int lastc));
191static void ins_del __ARGS((void));
192static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
193#ifdef FEAT_MOUSE
194static void ins_mouse __ARGS((int c));
195static void ins_mousescroll __ARGS((int up));
196#endif
197static void ins_left __ARGS((void));
198static void ins_home __ARGS((int c));
199static void ins_end __ARGS((int c));
200static void ins_s_left __ARGS((void));
201static void ins_right __ARGS((void));
202static void ins_s_right __ARGS((void));
203static void ins_up __ARGS((int startcol));
204static void ins_pageup __ARGS((void));
205static void ins_down __ARGS((int startcol));
206static void ins_pagedown __ARGS((void));
207#ifdef FEAT_DND
208static void ins_drop __ARGS((void));
209#endif
210static int ins_tab __ARGS((void));
211static int ins_eol __ARGS((int c));
212#ifdef FEAT_DIGRAPHS
213static int ins_digraph __ARGS((void));
214#endif
215static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000216static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217#ifdef FEAT_SMARTINDENT
218static void ins_try_si __ARGS((int c));
219#endif
220static colnr_T get_nolist_virtcol __ARGS((void));
221
222static colnr_T Insstart_textlen; /* length of line when insert started */
223static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
224
225static char_u *last_insert = NULL; /* the text of the previous insert,
226 K_SPECIAL and CSI are escaped */
227static int last_insert_skip; /* nr of chars in front of previous insert */
228static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000229static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
231#ifdef FEAT_CINDENT
232static int can_cindent; /* may do cindenting on this line */
233#endif
234
235static int old_indent = 0; /* for ^^D command in insert mode */
236
237#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000238static int revins_on; /* reverse insert mode on */
239static int revins_chars; /* how much to skip after edit */
240static int revins_legal; /* was the last char 'legal'? */
241static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242#endif
243
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244static int ins_need_undo; /* call u_save() before inserting a
245 char. Set when edit() is called.
246 after that arrow_used is used. */
247
248static int did_add_space = FALSE; /* auto_format() added an extra space
249 under the cursor */
250
251/*
252 * edit(): Start inserting text.
253 *
254 * "cmdchar" can be:
255 * 'i' normal insert command
256 * 'a' normal append command
257 * 'R' replace command
258 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
259 * but still only one <CR> is inserted. The <Esc> is not used for redo.
260 * 'g' "gI" command.
261 * 'V' "gR" command for Virtual Replace mode.
262 * 'v' "gr" command for single character Virtual Replace mode.
263 *
264 * This function is not called recursively. For CTRL-O commands, it returns
265 * and lets the caller handle the Normal-mode command.
266 *
267 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
268 */
269 int
270edit(cmdchar, startln, count)
271 int cmdchar;
272 int startln; /* if set, insert at start of line */
273 long count;
274{
275 int c = 0;
276 char_u *ptr;
277 int lastc;
278 colnr_T mincol;
279 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 int i;
281 int did_backspace = TRUE; /* previous char was backspace */
282#ifdef FEAT_CINDENT
283 int line_is_white = FALSE; /* line is empty before insert */
284#endif
285 linenr_T old_topline = 0; /* topline before insertion */
286#ifdef FEAT_DIFF
287 int old_topfill = -1;
288#endif
289 int inserted_space = FALSE; /* just inserted a space */
290 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000291 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000293 /* Remember whether editing was restarted after CTRL-O. */
294 did_restart_edit = restart_edit;
295
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 /* sleep before redrawing, needed for "CTRL-O :" that results in an
297 * error message */
298 check_for_delay(TRUE);
299
300#ifdef HAVE_SANDBOX
301 /* Don't allow inserting in the sandbox. */
302 if (sandbox != 0)
303 {
304 EMSG(_(e_sandbox));
305 return FALSE;
306 }
307#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000308 /* Don't allow changes in the buffer while editing the cmdline. The
309 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000310 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000311 {
312 EMSG(_(e_secure));
313 return FALSE;
314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315
316#ifdef FEAT_INS_EXPAND
317 ins_compl_clear(); /* clear stuff for CTRL-X mode */
318#endif
319
Bram Moolenaar843ee412004-06-30 16:16:41 +0000320#ifdef FEAT_AUTOCMD
321 /*
322 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
323 */
324 if (cmdchar != 'r' && cmdchar != 'v')
325 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000326# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000327 if (cmdchar == 'R')
328 ptr = (char_u *)"r";
329 else if (cmdchar == 'V')
330 ptr = (char_u *)"v";
331 else
332 ptr = (char_u *)"i";
333 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000334# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000335 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
336 }
337#endif
338
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339#ifdef FEAT_MOUSE
340 /*
341 * When doing a paste with the middle mouse button, Insstart is set to
342 * where the paste started.
343 */
344 if (where_paste_started.lnum != 0)
345 Insstart = where_paste_started;
346 else
347#endif
348 {
349 Insstart = curwin->w_cursor;
350 if (startln)
351 Insstart.col = 0;
352 }
353 Insstart_textlen = linetabsize(ml_get_curline());
354 Insstart_blank_vcol = MAXCOL;
355 if (!did_ai)
356 ai_col = 0;
357
358 if (cmdchar != NUL && restart_edit == 0)
359 {
360 ResetRedobuff();
361 AppendNumberToRedobuff(count);
362#ifdef FEAT_VREPLACE
363 if (cmdchar == 'V' || cmdchar == 'v')
364 {
365 /* "gR" or "gr" command */
366 AppendCharToRedobuff('g');
367 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
368 }
369 else
370#endif
371 {
372 AppendCharToRedobuff(cmdchar);
373 if (cmdchar == 'g') /* "gI" command */
374 AppendCharToRedobuff('I');
375 else if (cmdchar == 'r') /* "r<CR>" command */
376 count = 1; /* insert only one <CR> */
377 }
378 }
379
380 if (cmdchar == 'R')
381 {
382#ifdef FEAT_FKMAP
383 if (p_fkmap && p_ri)
384 {
385 beep_flush();
386 EMSG(farsi_text_3); /* encoded in Farsi */
387 State = INSERT;
388 }
389 else
390#endif
391 State = REPLACE;
392 }
393#ifdef FEAT_VREPLACE
394 else if (cmdchar == 'V' || cmdchar == 'v')
395 {
396 State = VREPLACE;
397 replaceState = VREPLACE;
398 orig_line_count = curbuf->b_ml.ml_line_count;
399 vr_lines_changed = 1;
400 }
401#endif
402 else
403 State = INSERT;
404
405 stop_insert_mode = FALSE;
406
407 /*
408 * Need to recompute the cursor position, it might move when the cursor is
409 * on a TAB or special character.
410 */
411 curs_columns(TRUE);
412
413 /*
414 * Enable langmap or IME, indicated by 'iminsert'.
415 * Note that IME may enabled/disabled without us noticing here, thus the
416 * 'iminsert' value may not reflect what is actually used. It is updated
417 * when hitting <Esc>.
418 */
419 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
420 State |= LANGMAP;
421#ifdef USE_IM_CONTROL
422 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
423#endif
424
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425#ifdef FEAT_MOUSE
426 setmouse();
427#endif
428#ifdef FEAT_CMDL_INFO
429 clear_showcmd();
430#endif
431#ifdef FEAT_RIGHTLEFT
432 /* there is no reverse replace mode */
433 revins_on = (State == INSERT && p_ri);
434 if (revins_on)
435 undisplay_dollar();
436 revins_chars = 0;
437 revins_legal = 0;
438 revins_scol = -1;
439#endif
440
441 /*
442 * Handle restarting Insert mode.
443 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
444 * restart_edit non-zero, and something in the stuff buffer.
445 */
446 if (restart_edit != 0 && stuff_empty())
447 {
448#ifdef FEAT_MOUSE
449 /*
450 * After a paste we consider text typed to be part of the insert for
451 * the pasted text. You can backspace over the pasted text too.
452 */
453 if (where_paste_started.lnum)
454 arrow_used = FALSE;
455 else
456#endif
457 arrow_used = TRUE;
458 restart_edit = 0;
459
460 /*
461 * If the cursor was after the end-of-line before the CTRL-O and it is
462 * now at the end-of-line, put it after the end-of-line (this is not
463 * correct in very rare cases).
464 * Also do this if curswant is greater than the current virtual
465 * column. Eg after "^O$" or "^O80|".
466 */
467 validate_virtcol();
468 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000469 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 || curwin->w_curswant > curwin->w_virtcol)
471 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
472 {
473 if (ptr[1] == NUL)
474 ++curwin->w_cursor.col;
475#ifdef FEAT_MBYTE
476 else if (has_mbyte)
477 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000478 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 if (ptr[i] == NUL)
480 curwin->w_cursor.col += i;
481 }
482#endif
483 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000484 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 }
486 else
487 arrow_used = FALSE;
488
489 /* we are in insert mode now, don't need to start it anymore */
490 need_start_insertmode = FALSE;
491
492 /* Need to save the line for undo before inserting the first char. */
493 ins_need_undo = TRUE;
494
495#ifdef FEAT_MOUSE
496 where_paste_started.lnum = 0;
497#endif
498#ifdef FEAT_CINDENT
499 can_cindent = TRUE;
500#endif
501#ifdef FEAT_FOLDING
502 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
503 * restarting. */
504 if (!p_im && did_restart_edit == 0)
505 foldOpenCursor();
506#endif
507
508 /*
509 * If 'showmode' is set, show the current (insert/replace/..) mode.
510 * A warning message for changing a readonly file is given here, before
511 * actually changing anything. It's put after the mode, if any.
512 */
513 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000514 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 i = showmode();
516
517 if (!p_im && did_restart_edit == 0)
518 change_warning(i + 1);
519
520#ifdef CURSOR_SHAPE
521 ui_cursor_shape(); /* may show different cursor shape */
522#endif
523#ifdef FEAT_DIGRAPHS
524 do_digraph(-1); /* clear digraphs */
525#endif
526
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000527 /*
528 * Get the current length of the redo buffer, those characters have to be
529 * skipped if we want to get to the inserted characters.
530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 ptr = get_inserted();
532 if (ptr == NULL)
533 new_insert_skip = 0;
534 else
535 {
536 new_insert_skip = (int)STRLEN(ptr);
537 vim_free(ptr);
538 }
539
540 old_indent = 0;
541
542 /*
543 * Main loop in Insert mode: repeat until Insert mode is left.
544 */
545 for (;;)
546 {
547#ifdef FEAT_RIGHTLEFT
548 if (!revins_legal)
549 revins_scol = -1; /* reset on illegal motions */
550 else
551 revins_legal = 0;
552#endif
553 if (arrow_used) /* don't repeat insert when arrow key used */
554 count = 0;
555
556 if (stop_insert_mode)
557 {
558 /* ":stopinsert" used or 'insertmode' reset */
559 count = 0;
560 goto doESCkey;
561 }
562
563 /* set curwin->w_curswant for next K_DOWN or K_UP */
564 if (!arrow_used)
565 curwin->w_set_curswant = TRUE;
566
567 /* If there is no typeahead may check for timestamps (e.g., for when a
568 * menu invoked a shell command). */
569 if (stuff_empty())
570 {
571 did_check_timestamps = FALSE;
572 if (need_check_timestamps)
573 check_timestamps(FALSE);
574 }
575
576 /*
577 * When emsg() was called msg_scroll will have been set.
578 */
579 msg_scroll = FALSE;
580
581#ifdef FEAT_GUI
582 /* When 'mousefocus' is set a mouse movement may have taken us to
583 * another window. "need_mouse_correct" may then be set because of an
584 * autocommand. */
585 if (need_mouse_correct)
586 gui_mouse_correct();
587#endif
588
589#ifdef FEAT_FOLDING
590 /* Open fold at the cursor line, according to 'foldopen'. */
591 if (fdo_flags & FDO_INSERT)
592 foldOpenCursor();
593 /* Close folds where the cursor isn't, according to 'foldclose' */
594 if (!char_avail())
595 foldCheckClose();
596#endif
597
598 /*
599 * If we inserted a character at the last position of the last line in
600 * the window, scroll the window one line up. This avoids an extra
601 * redraw.
602 * This is detected when the cursor column is smaller after inserting
603 * something.
604 * Don't do this when the topline changed already, it has
605 * already been adjusted (by insertchar() calling open_line())).
606 */
607 if (curbuf->b_mod_set
608 && curwin->w_p_wrap
609 && !did_backspace
610 && curwin->w_topline == old_topline
611#ifdef FEAT_DIFF
612 && curwin->w_topfill == old_topfill
613#endif
614 )
615 {
616 mincol = curwin->w_wcol;
617 validate_cursor_col();
618
619 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
620 && curwin->w_wrow == W_WINROW(curwin)
621 + curwin->w_height - 1 - p_so
622 && (curwin->w_cursor.lnum != curwin->w_topline
623#ifdef FEAT_DIFF
624 || curwin->w_topfill > 0
625#endif
626 ))
627 {
628#ifdef FEAT_DIFF
629 if (curwin->w_topfill > 0)
630 --curwin->w_topfill;
631 else
632#endif
633#ifdef FEAT_FOLDING
634 if (hasFolding(curwin->w_topline, NULL, &old_topline))
635 set_topline(curwin, old_topline + 1);
636 else
637#endif
638 set_topline(curwin, curwin->w_topline + 1);
639 }
640 }
641
642 /* May need to adjust w_topline to show the cursor. */
643 update_topline();
644
645 did_backspace = FALSE;
646
647 validate_cursor(); /* may set must_redraw */
648
649 /*
650 * Redraw the display when no characters are waiting.
651 * Also shows mode, ruler and positions cursor.
652 */
653 ins_redraw();
654
655#ifdef FEAT_SCROLLBIND
656 if (curwin->w_p_scb)
657 do_check_scrollbind(TRUE);
658#endif
659
660 update_curswant();
661 old_topline = curwin->w_topline;
662#ifdef FEAT_DIFF
663 old_topfill = curwin->w_topfill;
664#endif
665
666#ifdef USE_ON_FLY_SCROLL
667 dont_scroll = FALSE; /* allow scrolling here */
668#endif
669
670 /*
671 * Get a character for Insert mode.
672 */
673 lastc = c; /* remember previous char for CTRL-D */
674 c = safe_vgetc();
675
676#ifdef FEAT_RIGHTLEFT
677 if (p_hkmap && KeyTyped)
678 c = hkmap(c); /* Hebrew mode mapping */
679#endif
680#ifdef FEAT_FKMAP
681 if (p_fkmap && KeyTyped)
682 c = fkmap(c); /* Farsi mode mapping */
683#endif
684
685#ifdef FEAT_INS_EXPAND
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000686 /* When the popup menu is visible cursor keys change the selection. */
687 if (c == K_UP && pum_visible())
688 c = Ctrl_P;
689 if (c == K_DOWN && pum_visible())
690 c = Ctrl_N;
691
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000692 /*
693 * Special handling of keys while the popup menu is visible or wanted
694 * and the cursor is still in the completed word.
695 */
696 if (compl_started && pum_wanted() && curwin->w_cursor.col >= compl_col)
Bram Moolenaara6557602006-02-04 22:43:20 +0000697 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000698 /* BS: Delete one character from "compl_leader". */
699 if ((c == K_BS || c == Ctrl_H)
700 && curwin->w_cursor.col > compl_col && ins_compl_bs())
Bram Moolenaara6557602006-02-04 22:43:20 +0000701 continue;
702
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000703 /* When no match was selected or it was edited. */
704 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000705 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000706 /* CTRL-L: Add one character from the current match to
707 * "compl_leader". */
708 if (c == Ctrl_L)
709 {
710 ins_compl_addfrommatch();
711 continue;
712 }
713
714 /* A printable character: Add it to "compl_leader". */
715 if (vim_isprintc(c))
716 {
717 ins_compl_addleader(c);
718 continue;
719 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000720 }
721 }
722
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
724 * it does fix up the text when finishing completion. */
Bram Moolenaara6557602006-02-04 22:43:20 +0000725 if (c != K_IGNORE && ins_compl_prep(c))
726 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727#endif
728
Bram Moolenaar488c6512005-08-11 20:09:58 +0000729 /* CTRL-\ CTRL-N goes to Normal mode,
730 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
731 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 if (c == Ctrl_BSL)
733 {
734 /* may need to redraw when no more chars available now */
735 ins_redraw();
736 ++no_mapping;
737 ++allow_keys;
738 c = safe_vgetc();
739 --no_mapping;
740 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000741 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000743 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 vungetc(c);
745 c = Ctrl_BSL;
746 }
747 else if (c == Ctrl_G && p_im)
748 continue;
749 else
750 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000751 if (c == Ctrl_O)
752 {
753 ins_ctrl_o();
754 ins_at_eol = FALSE; /* cursor keeps its column */
755 nomove = TRUE;
756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 count = 0;
758 goto doESCkey;
759 }
760 }
761
762#ifdef FEAT_DIGRAPHS
763 c = do_digraph(c);
764#endif
765
766#ifdef FEAT_INS_EXPAND
767 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
768 goto docomplete;
769#endif
770 if (c == Ctrl_V || c == Ctrl_Q)
771 {
772 ins_ctrl_v();
773 c = Ctrl_V; /* pretend CTRL-V is last typed character */
774 continue;
775 }
776
777#ifdef FEAT_CINDENT
778 if (cindent_on()
779# ifdef FEAT_INS_EXPAND
780 && ctrl_x_mode == 0
781# endif
782 )
783 {
784 /* A key name preceded by a bang means this key is not to be
785 * inserted. Skip ahead to the re-indenting below.
786 * A key name preceded by a star means that indenting has to be
787 * done before inserting the key. */
788 line_is_white = inindent(0);
789 if (in_cinkeys(c, '!', line_is_white))
790 goto force_cindent;
791 if (can_cindent && in_cinkeys(c, '*', line_is_white)
792 && stop_arrow() == OK)
793 do_c_expr_indent();
794 }
795#endif
796
797#ifdef FEAT_RIGHTLEFT
798 if (curwin->w_p_rl)
799 switch (c)
800 {
801 case K_LEFT: c = K_RIGHT; break;
802 case K_S_LEFT: c = K_S_RIGHT; break;
803 case K_C_LEFT: c = K_C_RIGHT; break;
804 case K_RIGHT: c = K_LEFT; break;
805 case K_S_RIGHT: c = K_S_LEFT; break;
806 case K_C_RIGHT: c = K_C_LEFT; break;
807 }
808#endif
809
810#ifdef FEAT_VISUAL
811 /*
812 * If 'keymodel' contains "startsel", may start selection. If it
813 * does, a CTRL-O and c will be stuffed, we need to get these
814 * characters.
815 */
816 if (ins_start_select(c))
817 continue;
818#endif
819
820 /*
821 * The big switch to handle a character in insert mode.
822 */
823 switch (c)
824 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000825 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 if (echeck_abbr(ESC + ABBR_OFF))
827 break;
828 /*FALLTHROUGH*/
829
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000830 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831#ifdef FEAT_CMDWIN
832 if (c == Ctrl_C && cmdwin_type != 0)
833 {
834 /* Close the cmdline window. */
835 cmdwin_result = K_IGNORE;
836 got_int = FALSE; /* don't stop executing autocommands et al. */
837 goto doESCkey;
838 }
839#endif
840
841#ifdef UNIX
842do_intr:
843#endif
844 /* when 'insertmode' set, and not halfway a mapping, don't leave
845 * Insert mode */
846 if (goto_im())
847 {
848 if (got_int)
849 {
850 (void)vgetc(); /* flush all buffers */
851 got_int = FALSE;
852 }
853 else
854 vim_beep();
855 break;
856 }
857doESCkey:
858 /*
859 * This is the ONLY return from edit()!
860 */
861 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
862 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000863 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 o_lnum = curwin->w_cursor.lnum;
865
Bram Moolenaar488c6512005-08-11 20:09:58 +0000866 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000867 {
868#ifdef FEAT_AUTOCMD
869 if (cmdchar != 'r' && cmdchar != 'v')
870 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
871 FALSE, curbuf);
872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 continue;
876
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000877 case Ctrl_Z: /* suspend when 'insertmode' set */
878 if (!p_im)
879 goto normalchar; /* insert CTRL-Z as normal char */
880 stuffReadbuff((char_u *)":st\r");
881 c = Ctrl_O;
882 /*FALLTHROUGH*/
883
884 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000885#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000886 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000887 goto docomplete;
888#endif
889 if (echeck_abbr(Ctrl_O + ABBR_OFF))
890 break;
891 ins_ctrl_o();
892 count = 0;
893 goto doESCkey;
894
Bram Moolenaar572cb562005-08-05 21:35:02 +0000895 case K_INS: /* toggle insert/replace mode */
896 case K_KINS:
897 ins_insert(replaceState);
898 break;
899
900 case K_SELECT: /* end of Select mode mapping - ignore */
901 break;
902
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000903#ifdef FEAT_SNIFF
904 case K_SNIFF: /* Sniff command received */
905 stuffcharReadbuff(K_SNIFF);
906 goto doESCkey;
907#endif
908
909 case K_HELP: /* Help key works like <ESC> <Help> */
910 case K_F1:
911 case K_XF1:
912 stuffcharReadbuff(K_HELP);
913 if (p_im)
914 need_start_insertmode = TRUE;
915 goto doESCkey;
916
917#ifdef FEAT_NETBEANS_INTG
918 case K_F21: /* NetBeans command */
919 ++no_mapping; /* don't map the next key hits */
920 i = safe_vgetc();
921 --no_mapping;
922 netbeans_keycommand(i);
923 break;
924#endif
925
926 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 case NUL:
928 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000929 /* For ^@ the trailing ESC will end the insert, unless there is an
930 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
932 && c != Ctrl_A && !p_im)
933 goto doESCkey; /* quit insert mode */
934 inserted_space = FALSE;
935 break;
936
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000937 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 ins_reg();
939 auto_format(FALSE, TRUE);
940 inserted_space = FALSE;
941 break;
942
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000943 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 ins_ctrl_g();
945 break;
946
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000947 case Ctrl_HAT: /* switch input mode and/or langmap */
948 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 break;
950
951#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000952 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (!p_ari)
954 goto normalchar;
955 ins_ctrl_();
956 break;
957#endif
958
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000959 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
961 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
962 goto docomplete;
963#endif
964 /* FALLTHROUGH */
965
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000966 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967# ifdef FEAT_INS_EXPAND
968 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
969 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000970 if (has_compl_option(FALSE))
971 goto docomplete;
972 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 }
974# endif
975 ins_shift(c, lastc);
976 auto_format(FALSE, TRUE);
977 inserted_space = FALSE;
978 break;
979
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000980 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 case K_KDEL:
982 ins_del();
983 auto_format(FALSE, TRUE);
984 break;
985
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000986 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 case Ctrl_H:
988 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
989 auto_format(FALSE, TRUE);
990 break;
991
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000992 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
994 auto_format(FALSE, TRUE);
995 break;
996
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000997 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000998# ifdef FEAT_COMPL_FUNC
999 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001000 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001001 goto docomplete;
1002# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1004 auto_format(FALSE, TRUE);
1005 inserted_space = FALSE;
1006 break;
1007
1008#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001009 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 case K_LEFTMOUSE_NM:
1011 case K_LEFTDRAG:
1012 case K_LEFTRELEASE:
1013 case K_LEFTRELEASE_NM:
1014 case K_MIDDLEMOUSE:
1015 case K_MIDDLEDRAG:
1016 case K_MIDDLERELEASE:
1017 case K_RIGHTMOUSE:
1018 case K_RIGHTDRAG:
1019 case K_RIGHTRELEASE:
1020 case K_X1MOUSE:
1021 case K_X1DRAG:
1022 case K_X1RELEASE:
1023 case K_X2MOUSE:
1024 case K_X2DRAG:
1025 case K_X2RELEASE:
1026 ins_mouse(c);
1027 break;
1028
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001029 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 ins_mousescroll(FALSE);
1031 break;
1032
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001033 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 ins_mousescroll(TRUE);
1035 break;
1036#endif
1037
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001038 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 break;
1040
Bram Moolenaar4770d092006-01-12 23:22:24 +00001041#ifdef FEAT_GUI_W32
1042 /* On Win32 ignore <M-F4>, we get it when closing the window was
1043 * cancelled. */
1044 case K_F4:
1045 if (mod_mask != MOD_MASK_ALT)
1046 goto normalchar;
1047 break;
1048#endif
1049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050#ifdef FEAT_GUI
1051 case K_VER_SCROLLBAR:
1052 ins_scroll();
1053 break;
1054
1055 case K_HOR_SCROLLBAR:
1056 ins_horscroll();
1057 break;
1058#endif
1059
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001060 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 case K_S_HOME:
1063 case K_C_HOME:
1064 ins_home(c);
1065 break;
1066
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001067 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 case K_S_END:
1070 case K_C_END:
1071 ins_end(c);
1072 break;
1073
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001074 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001075 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1076 ins_s_left();
1077 else
1078 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 break;
1080
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001081 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 case K_C_LEFT:
1083 ins_s_left();
1084 break;
1085
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001086 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001087 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1088 ins_s_right();
1089 else
1090 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 break;
1092
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001093 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 case K_C_RIGHT:
1095 ins_s_right();
1096 break;
1097
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001098 case K_UP: /* <Up> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001099 if (mod_mask & MOD_MASK_SHIFT)
1100 ins_pageup();
1101 else
1102 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 break;
1104
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001105 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 case K_PAGEUP:
1107 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001108#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001109 if (pum_visible())
1110 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 ins_pageup();
1113 break;
1114
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001115 case K_DOWN: /* <Down> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001116 if (mod_mask & MOD_MASK_SHIFT)
1117 ins_pagedown();
1118 else
1119 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 break;
1121
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001122 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 case K_PAGEDOWN:
1124 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001125#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001126 if (pum_visible())
1127 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 ins_pagedown();
1130 break;
1131
1132#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001133 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 ins_drop();
1135 break;
1136#endif
1137
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001138 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 c = TAB;
1140 /* FALLTHROUGH */
1141
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001142 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1144 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1145 goto docomplete;
1146#endif
1147 inserted_space = FALSE;
1148 if (ins_tab())
1149 goto normalchar; /* insert TAB as a normal char */
1150 auto_format(FALSE, TRUE);
1151 break;
1152
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001153 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 c = CAR;
1155 /* FALLTHROUGH */
1156 case CAR:
1157 case NL:
1158#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1159 /* In a quickfix window a <CR> jumps to the error under the
1160 * cursor. */
1161 if (bt_quickfix(curbuf) && c == CAR)
1162 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001163 if (curwin->w_llist_ref == NULL) /* quickfix window */
1164 do_cmdline_cmd((char_u *)".cc");
1165 else /* location list window */
1166 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 break;
1168 }
1169#endif
1170#ifdef FEAT_CMDWIN
1171 if (cmdwin_type != 0)
1172 {
1173 /* Execute the command in the cmdline window. */
1174 cmdwin_result = CAR;
1175 goto doESCkey;
1176 }
1177#endif
1178 if (ins_eol(c) && !p_im)
1179 goto doESCkey; /* out of memory */
1180 auto_format(FALSE, FALSE);
1181 inserted_space = FALSE;
1182 break;
1183
1184#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001185 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186# ifdef FEAT_INS_EXPAND
1187 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1188 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001189 if (has_compl_option(TRUE))
1190 goto docomplete;
1191 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 }
1193# endif
1194# ifdef FEAT_DIGRAPHS
1195 c = ins_digraph();
1196 if (c == NUL)
1197 break;
1198# endif
1199 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
1202#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001203 case Ctrl_X: /* Enter CTRL-X mode */
1204 ins_ctrl_x();
1205 break;
1206
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001207 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 if (ctrl_x_mode != CTRL_X_TAGS)
1209 goto normalchar;
1210 goto docomplete;
1211
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001212 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (ctrl_x_mode != CTRL_X_FILES)
1214 goto normalchar;
1215 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001216
1217 case 's': /* Spelling completion after ^X */
1218 case Ctrl_S:
1219 if (ctrl_x_mode != CTRL_X_SPELL)
1220 goto normalchar;
1221 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222#endif
1223
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001224 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225#ifdef FEAT_INS_EXPAND
1226 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1227#endif
1228 {
1229 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1230 if (p_im)
1231 {
1232 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1233 break;
1234 goto doESCkey;
1235 }
1236 goto normalchar;
1237 }
1238#ifdef FEAT_INS_EXPAND
1239 /* FALLTHROUGH */
1240
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001241 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 case Ctrl_N:
1243 /* if 'complete' is empty then plain ^P is no longer special,
1244 * but it is under other ^X modes */
1245 if (*curbuf->b_p_cpt == NUL
1246 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001247 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 goto normalchar;
1249
1250docomplete:
1251 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001252 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 break;
1254#endif /* FEAT_INS_EXPAND */
1255
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001256 case Ctrl_Y: /* copy from previous line or scroll down */
1257 case Ctrl_E: /* copy from next line or scroll up */
1258 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 break;
1260
1261 default:
1262#ifdef UNIX
1263 if (c == intr_char) /* special interrupt char */
1264 goto do_intr;
1265#endif
1266
1267 /*
1268 * Insert a nomal character.
1269 */
1270normalchar:
1271#ifdef FEAT_SMARTINDENT
1272 /* Try to perform smart-indenting. */
1273 ins_try_si(c);
1274#endif
1275
1276 if (c == ' ')
1277 {
1278 inserted_space = TRUE;
1279#ifdef FEAT_CINDENT
1280 if (inindent(0))
1281 can_cindent = FALSE;
1282#endif
1283 if (Insstart_blank_vcol == MAXCOL
1284 && curwin->w_cursor.lnum == Insstart.lnum)
1285 Insstart_blank_vcol = get_nolist_virtcol();
1286 }
1287
1288 if (vim_iswordc(c) || !echeck_abbr(
1289#ifdef FEAT_MBYTE
1290 /* Add ABBR_OFF for characters above 0x100, this is
1291 * what check_abbr() expects. */
1292 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1293#endif
1294 c))
1295 {
1296 insert_special(c, FALSE, FALSE);
1297#ifdef FEAT_RIGHTLEFT
1298 revins_legal++;
1299 revins_chars++;
1300#endif
1301 }
1302
1303 auto_format(FALSE, TRUE);
1304
1305#ifdef FEAT_FOLDING
1306 /* When inserting a character the cursor line must never be in a
1307 * closed fold. */
1308 foldOpenCursor();
1309#endif
1310 break;
1311 } /* end of switch (c) */
1312
1313 /* If the cursor was moved we didn't just insert a space */
1314 if (arrow_used)
1315 inserted_space = FALSE;
1316
1317#ifdef FEAT_CINDENT
1318 if (can_cindent && cindent_on()
1319# ifdef FEAT_INS_EXPAND
1320 && ctrl_x_mode == 0
1321# endif
1322 )
1323 {
1324force_cindent:
1325 /*
1326 * Indent now if a key was typed that is in 'cinkeys'.
1327 */
1328 if (in_cinkeys(c, ' ', line_is_white))
1329 {
1330 if (stop_arrow() == OK)
1331 /* re-indent the current line */
1332 do_c_expr_indent();
1333 }
1334 }
1335#endif /* FEAT_CINDENT */
1336
1337 } /* for (;;) */
1338 /* NOTREACHED */
1339}
1340
1341/*
1342 * Redraw for Insert mode.
1343 * This is postponed until getting the next character to make '$' in the 'cpo'
1344 * option work correctly.
1345 * Only redraw when there are no characters available. This speeds up
1346 * inserting sequences of characters (e.g., for CTRL-R).
1347 */
1348 static void
1349ins_redraw()
1350{
1351 if (!char_avail())
1352 {
1353 if (must_redraw)
1354 update_screen(0);
1355 else if (clear_cmdline || redraw_cmdline)
1356 showmode(); /* clear cmdline and show mode */
1357 showruler(FALSE);
1358 setcursor();
1359 emsg_on_display = FALSE; /* may remove error message now */
1360 }
1361}
1362
1363/*
1364 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1365 */
1366 static void
1367ins_ctrl_v()
1368{
1369 int c;
1370
1371 /* may need to redraw when no more chars available now */
1372 ins_redraw();
1373
1374 if (redrawing() && !char_avail())
1375 edit_putchar('^', TRUE);
1376 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1377
1378#ifdef FEAT_CMDL_INFO
1379 add_to_showcmd_c(Ctrl_V);
1380#endif
1381
1382 c = get_literal();
1383#ifdef FEAT_CMDL_INFO
1384 clear_showcmd();
1385#endif
1386 insert_special(c, FALSE, TRUE);
1387#ifdef FEAT_RIGHTLEFT
1388 revins_chars++;
1389 revins_legal++;
1390#endif
1391}
1392
1393/*
1394 * Put a character directly onto the screen. It's not stored in a buffer.
1395 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1396 */
1397static int pc_status;
1398#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1399#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1400#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1401#define PC_STATUS_SET 3 /* pc_bytes was filled */
1402#ifdef FEAT_MBYTE
1403static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1404#else
1405static char_u pc_bytes[2]; /* saved bytes */
1406#endif
1407static int pc_attr;
1408static int pc_row;
1409static int pc_col;
1410
1411 void
1412edit_putchar(c, highlight)
1413 int c;
1414 int highlight;
1415{
1416 int attr;
1417
1418 if (ScreenLines != NULL)
1419 {
1420 update_topline(); /* just in case w_topline isn't valid */
1421 validate_cursor();
1422 if (highlight)
1423 attr = hl_attr(HLF_8);
1424 else
1425 attr = 0;
1426 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1427 pc_col = W_WINCOL(curwin);
1428#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1429 pc_status = PC_STATUS_UNSET;
1430#endif
1431#ifdef FEAT_RIGHTLEFT
1432 if (curwin->w_p_rl)
1433 {
1434 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1435# ifdef FEAT_MBYTE
1436 if (has_mbyte)
1437 {
1438 int fix_col = mb_fix_col(pc_col, pc_row);
1439
1440 if (fix_col != pc_col)
1441 {
1442 screen_putchar(' ', pc_row, fix_col, attr);
1443 --curwin->w_wcol;
1444 pc_status = PC_STATUS_RIGHT;
1445 }
1446 }
1447# endif
1448 }
1449 else
1450#endif
1451 {
1452 pc_col += curwin->w_wcol;
1453#ifdef FEAT_MBYTE
1454 if (mb_lefthalve(pc_row, pc_col))
1455 pc_status = PC_STATUS_LEFT;
1456#endif
1457 }
1458
1459 /* save the character to be able to put it back */
1460#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1461 if (pc_status == PC_STATUS_UNSET)
1462#endif
1463 {
1464 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1465 pc_status = PC_STATUS_SET;
1466 }
1467 screen_putchar(c, pc_row, pc_col, attr);
1468 }
1469}
1470
1471/*
1472 * Undo the previous edit_putchar().
1473 */
1474 void
1475edit_unputchar()
1476{
1477 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1478 {
1479#if defined(FEAT_MBYTE)
1480 if (pc_status == PC_STATUS_RIGHT)
1481 ++curwin->w_wcol;
1482 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1483 redrawWinline(curwin->w_cursor.lnum, FALSE);
1484 else
1485#endif
1486 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1487 }
1488}
1489
1490/*
1491 * Called when p_dollar is set: display a '$' at the end of the changed text
1492 * Only works when cursor is in the line that changes.
1493 */
1494 void
1495display_dollar(col)
1496 colnr_T col;
1497{
1498 colnr_T save_col;
1499
1500 if (!redrawing())
1501 return;
1502
1503 cursor_off();
1504 save_col = curwin->w_cursor.col;
1505 curwin->w_cursor.col = col;
1506#ifdef FEAT_MBYTE
1507 if (has_mbyte)
1508 {
1509 char_u *p;
1510
1511 /* If on the last byte of a multi-byte move to the first byte. */
1512 p = ml_get_curline();
1513 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1514 }
1515#endif
1516 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1517 if (curwin->w_wcol < W_WIDTH(curwin))
1518 {
1519 edit_putchar('$', FALSE);
1520 dollar_vcol = curwin->w_virtcol;
1521 }
1522 curwin->w_cursor.col = save_col;
1523}
1524
1525/*
1526 * Call this function before moving the cursor from the normal insert position
1527 * in insert mode.
1528 */
1529 static void
1530undisplay_dollar()
1531{
1532 if (dollar_vcol)
1533 {
1534 dollar_vcol = 0;
1535 redrawWinline(curwin->w_cursor.lnum, FALSE);
1536 }
1537}
1538
1539/*
1540 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1541 * Keep the cursor on the same character.
1542 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1543 * type == INDENT_DEC decrease indent (for CTRL-D)
1544 * type == INDENT_SET set indent to "amount"
1545 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1546 */
1547 void
1548change_indent(type, amount, round, replaced)
1549 int type;
1550 int amount;
1551 int round;
1552 int replaced; /* replaced character, put on replace stack */
1553{
1554 int vcol;
1555 int last_vcol;
1556 int insstart_less; /* reduction for Insstart.col */
1557 int new_cursor_col;
1558 int i;
1559 char_u *ptr;
1560 int save_p_list;
1561 int start_col;
1562 colnr_T vc;
1563#ifdef FEAT_VREPLACE
1564 colnr_T orig_col = 0; /* init for GCC */
1565 char_u *new_line, *orig_line = NULL; /* init for GCC */
1566
1567 /* VREPLACE mode needs to know what the line was like before changing */
1568 if (State & VREPLACE_FLAG)
1569 {
1570 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1571 orig_col = curwin->w_cursor.col;
1572 }
1573#endif
1574
1575 /* for the following tricks we don't want list mode */
1576 save_p_list = curwin->w_p_list;
1577 curwin->w_p_list = FALSE;
1578 vc = getvcol_nolist(&curwin->w_cursor);
1579 vcol = vc;
1580
1581 /*
1582 * For Replace mode we need to fix the replace stack later, which is only
1583 * possible when the cursor is in the indent. Remember the number of
1584 * characters before the cursor if it's possible.
1585 */
1586 start_col = curwin->w_cursor.col;
1587
1588 /* determine offset from first non-blank */
1589 new_cursor_col = curwin->w_cursor.col;
1590 beginline(BL_WHITE);
1591 new_cursor_col -= curwin->w_cursor.col;
1592
1593 insstart_less = curwin->w_cursor.col;
1594
1595 /*
1596 * If the cursor is in the indent, compute how many screen columns the
1597 * cursor is to the left of the first non-blank.
1598 */
1599 if (new_cursor_col < 0)
1600 vcol = get_indent() - vcol;
1601
1602 if (new_cursor_col > 0) /* can't fix replace stack */
1603 start_col = -1;
1604
1605 /*
1606 * Set the new indent. The cursor will be put on the first non-blank.
1607 */
1608 if (type == INDENT_SET)
1609 (void)set_indent(amount, SIN_CHANGED);
1610 else
1611 {
1612#ifdef FEAT_VREPLACE
1613 int save_State = State;
1614
1615 /* Avoid being called recursively. */
1616 if (State & VREPLACE_FLAG)
1617 State = INSERT;
1618#endif
1619 shift_line(type == INDENT_DEC, round, 1);
1620#ifdef FEAT_VREPLACE
1621 State = save_State;
1622#endif
1623 }
1624 insstart_less -= curwin->w_cursor.col;
1625
1626 /*
1627 * Try to put cursor on same character.
1628 * If the cursor is at or after the first non-blank in the line,
1629 * compute the cursor column relative to the column of the first
1630 * non-blank character.
1631 * If we are not in insert mode, leave the cursor on the first non-blank.
1632 * If the cursor is before the first non-blank, position it relative
1633 * to the first non-blank, counted in screen columns.
1634 */
1635 if (new_cursor_col >= 0)
1636 {
1637 /*
1638 * When changing the indent while the cursor is touching it, reset
1639 * Insstart_col to 0.
1640 */
1641 if (new_cursor_col == 0)
1642 insstart_less = MAXCOL;
1643 new_cursor_col += curwin->w_cursor.col;
1644 }
1645 else if (!(State & INSERT))
1646 new_cursor_col = curwin->w_cursor.col;
1647 else
1648 {
1649 /*
1650 * Compute the screen column where the cursor should be.
1651 */
1652 vcol = get_indent() - vcol;
1653 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1654
1655 /*
1656 * Advance the cursor until we reach the right screen column.
1657 */
1658 vcol = last_vcol = 0;
1659 new_cursor_col = -1;
1660 ptr = ml_get_curline();
1661 while (vcol <= (int)curwin->w_virtcol)
1662 {
1663 last_vcol = vcol;
1664#ifdef FEAT_MBYTE
1665 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001666 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 else
1668#endif
1669 ++new_cursor_col;
1670 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1671 }
1672 vcol = last_vcol;
1673
1674 /*
1675 * May need to insert spaces to be able to position the cursor on
1676 * the right screen column.
1677 */
1678 if (vcol != (int)curwin->w_virtcol)
1679 {
1680 curwin->w_cursor.col = new_cursor_col;
1681 i = (int)curwin->w_virtcol - vcol;
1682 ptr = alloc(i + 1);
1683 if (ptr != NULL)
1684 {
1685 new_cursor_col += i;
1686 ptr[i] = NUL;
1687 while (--i >= 0)
1688 ptr[i] = ' ';
1689 ins_str(ptr);
1690 vim_free(ptr);
1691 }
1692 }
1693
1694 /*
1695 * When changing the indent while the cursor is in it, reset
1696 * Insstart_col to 0.
1697 */
1698 insstart_less = MAXCOL;
1699 }
1700
1701 curwin->w_p_list = save_p_list;
1702
1703 if (new_cursor_col <= 0)
1704 curwin->w_cursor.col = 0;
1705 else
1706 curwin->w_cursor.col = new_cursor_col;
1707 curwin->w_set_curswant = TRUE;
1708 changed_cline_bef_curs();
1709
1710 /*
1711 * May have to adjust the start of the insert.
1712 */
1713 if (State & INSERT)
1714 {
1715 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1716 {
1717 if ((int)Insstart.col <= insstart_less)
1718 Insstart.col = 0;
1719 else
1720 Insstart.col -= insstart_less;
1721 }
1722 if ((int)ai_col <= insstart_less)
1723 ai_col = 0;
1724 else
1725 ai_col -= insstart_less;
1726 }
1727
1728 /*
1729 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1730 * If the number of characters before the cursor decreased, need to pop a
1731 * few characters from the replace stack.
1732 * If the number of characters before the cursor increased, need to push a
1733 * few NULs onto the replace stack.
1734 */
1735 if (REPLACE_NORMAL(State) && start_col >= 0)
1736 {
1737 while (start_col > (int)curwin->w_cursor.col)
1738 {
1739 replace_join(0); /* remove a NUL from the replace stack */
1740 --start_col;
1741 }
1742 while (start_col < (int)curwin->w_cursor.col || replaced)
1743 {
1744 replace_push(NUL);
1745 if (replaced)
1746 {
1747 replace_push(replaced);
1748 replaced = NUL;
1749 }
1750 ++start_col;
1751 }
1752 }
1753
1754#ifdef FEAT_VREPLACE
1755 /*
1756 * For VREPLACE mode, we also have to fix the replace stack. In this case
1757 * it is always possible because we backspace over the whole line and then
1758 * put it back again the way we wanted it.
1759 */
1760 if (State & VREPLACE_FLAG)
1761 {
1762 /* If orig_line didn't allocate, just return. At least we did the job,
1763 * even if you can't backspace. */
1764 if (orig_line == NULL)
1765 return;
1766
1767 /* Save new line */
1768 new_line = vim_strsave(ml_get_curline());
1769 if (new_line == NULL)
1770 return;
1771
1772 /* We only put back the new line up to the cursor */
1773 new_line[curwin->w_cursor.col] = NUL;
1774
1775 /* Put back original line */
1776 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1777 curwin->w_cursor.col = orig_col;
1778
1779 /* Backspace from cursor to start of line */
1780 backspace_until_column(0);
1781
1782 /* Insert new stuff into line again */
1783 ins_bytes(new_line);
1784
1785 vim_free(new_line);
1786 }
1787#endif
1788}
1789
1790/*
1791 * Truncate the space at the end of a line. This is to be used only in an
1792 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1793 * modes.
1794 */
1795 void
1796truncate_spaces(line)
1797 char_u *line;
1798{
1799 int i;
1800
1801 /* find start of trailing white space */
1802 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1803 {
1804 if (State & REPLACE_FLAG)
1805 replace_join(0); /* remove a NUL from the replace stack */
1806 }
1807 line[i + 1] = NUL;
1808}
1809
1810#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1811 || defined(FEAT_COMMENTS) || defined(PROTO)
1812/*
1813 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1814 * modes correctly. May also be used when not in insert mode at all.
1815 */
1816 void
1817backspace_until_column(col)
1818 int col;
1819{
1820 while ((int)curwin->w_cursor.col > col)
1821 {
1822 curwin->w_cursor.col--;
1823 if (State & REPLACE_FLAG)
1824 replace_do_bs();
1825 else
1826 (void)del_char(FALSE);
1827 }
1828}
1829#endif
1830
1831#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1832/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001833 * CTRL-X pressed in Insert mode.
1834 */
1835 static void
1836ins_ctrl_x()
1837{
1838 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1839 * CTRL-V works like CTRL-N */
1840 if (ctrl_x_mode != CTRL_X_CMDLINE)
1841 {
1842 /* if the next ^X<> won't ADD nothing, then reset
1843 * compl_cont_status */
1844 if (compl_cont_status & CONT_N_ADDS)
1845 compl_cont_status = (compl_cont_status | CONT_INTRPT);
1846 else
1847 compl_cont_status = 0;
1848 /* We're not sure which CTRL-X mode it will be yet */
1849 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1850 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1851 edit_submode_pre = NULL;
1852 showmode();
1853 }
1854}
1855
1856/*
1857 * Return TRUE if the 'dict' or 'tsr' option can be used.
1858 */
1859 static int
1860has_compl_option(dict_opt)
1861 int dict_opt;
1862{
1863 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1864 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1865 {
1866 ctrl_x_mode = 0;
1867 edit_submode = NULL;
1868 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1869 : (char_u *)_("'thesaurus' option is empty"),
1870 hl_attr(HLF_E));
1871 if (emsg_silent == 0)
1872 {
1873 vim_beep();
1874 setcursor();
1875 out_flush();
1876 ui_delay(2000L, FALSE);
1877 }
1878 return FALSE;
1879 }
1880 return TRUE;
1881}
1882
1883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1885 * This depends on the current mode.
1886 */
1887 int
1888vim_is_ctrl_x_key(c)
1889 int c;
1890{
1891 /* Always allow ^R - let it's results then be checked */
1892 if (c == Ctrl_R)
1893 return TRUE;
1894
Bram Moolenaare3226be2005-12-18 22:10:00 +00001895 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001896 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001897 return TRUE;
1898
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 switch (ctrl_x_mode)
1900 {
1901 case 0: /* Not in any CTRL-X mode */
1902 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1903 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001904 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1906 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1907 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001908 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1909 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 case CTRL_X_SCROLL:
1911 return (c == Ctrl_Y || c == Ctrl_E);
1912 case CTRL_X_WHOLE_LINE:
1913 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1914 case CTRL_X_FILES:
1915 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1916 case CTRL_X_DICTIONARY:
1917 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1918 case CTRL_X_THESAURUS:
1919 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1920 case CTRL_X_TAGS:
1921 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1922#ifdef FEAT_FIND_ID
1923 case CTRL_X_PATH_PATTERNS:
1924 return (c == Ctrl_P || c == Ctrl_N);
1925 case CTRL_X_PATH_DEFINES:
1926 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1927#endif
1928 case CTRL_X_CMDLINE:
1929 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1930 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001931#ifdef FEAT_COMPL_FUNC
1932 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001933 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001934 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001935 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001936#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001937 case CTRL_X_SPELL:
1938 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940 EMSG(_(e_internal));
1941 return FALSE;
1942}
1943
1944/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001945 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 * case of the originally typed text is used, and the case of the completed
1947 * text is infered, ie this tries to work out what case you probably wanted
1948 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001949 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 */
1951 int
Bram Moolenaar572cb562005-08-05 21:35:02 +00001952ins_compl_add_infercase(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 char_u *str;
1954 int len;
1955 char_u *fname;
1956 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001957 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
1959 int has_lower = FALSE;
1960 int was_letter = FALSE;
1961 int idx;
1962
1963 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
1964 {
1965 /* Infer case of completed part -- webb */
1966 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001967 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968
1969 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001970 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001972 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 {
1974 has_lower = TRUE;
1975 if (isupper(IObuff[idx]))
1976 {
1977 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001978 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
1980 break;
1981 }
1982 }
1983 }
1984
1985 /*
1986 * Rule 2: No lower case, 2nd consecutive letter converted to
1987 * upper case.
1988 */
1989 if (!has_lower)
1990 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001991 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001993 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 && islower(IObuff[idx]))
1995 {
1996 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001997 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
1999 break;
2000 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002001 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 }
2003 }
2004
2005 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002006 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002008 return ins_compl_add(IObuff, len, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002010 return ins_compl_add(str, len, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011}
2012
2013/*
2014 * Add a match to the list of matches.
2015 * If the given string is already in the list of completions, then return
2016 * FAIL, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaar572cb562005-08-05 21:35:02 +00002017 * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb.
2018 *
2019 * New:
2020 * If the given string is already in the list of completions, then return
2021 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2022 * maybe because alloc() returns NULL, then FAIL is returned -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002024 int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002025ins_compl_add(str, len, fname, extra, cdir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 char_u *str;
2027 int len;
2028 char_u *fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002029 char_u *extra; /* extra text for popup menu or NULL */
2030 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002031 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002033 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002034 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
2036 ui_breakcheck();
2037 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002038 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 if (len < 0)
2040 len = (int)STRLEN(str);
2041
2042 /*
2043 * If the same match is already present, don't add it.
2044 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002045 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002047 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 do
2049 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002050 if ( !(match->cp_flags & ORIGINAL_TEXT)
2051 && STRNCMP(match->cp_str, str, (size_t)len) == 0
2052 && match->cp_str[len] == NUL)
2053 return NOTDONE;
2054 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002055 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 }
2057
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002058 /* Remove any popup menu before changing the list of matches. */
2059 ins_compl_del_pum();
2060
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 /*
2062 * Allocate a new match structure.
2063 * Copy the values to the new match structure.
2064 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002065 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002067 return FAIL;
2068 match->cp_number = -1;
2069 if (flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002071 match->cp_number = 0;
2072 match->cp_str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002074 else if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
2076 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002077 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002081 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2083 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002084 if (fname != NULL
2085 && compl_curr_match
2086 && compl_curr_match->cp_fname != NULL
2087 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002088 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002089 else if (fname != NULL)
2090 {
2091 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002092 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002095 match->cp_fname = NULL;
2096 match->cp_flags = flags;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002097 if (extra != NULL)
2098 match->cp_extra = vim_strsave(extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099
2100 /*
2101 * Link the new match structure in the list of matches.
2102 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002103 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002104 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 else if (dir == FORWARD)
2106 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002107 match->cp_next = compl_curr_match->cp_next;
2108 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 }
2110 else /* BACKWARD */
2111 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002112 match->cp_next = compl_curr_match;
2113 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002115 if (match->cp_next)
2116 match->cp_next->cp_prev = match;
2117 if (match->cp_prev)
2118 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002120 compl_first_match = match;
2121 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
2123 return OK;
2124}
2125
2126/*
2127 * Add an array of matches to the list of matches.
2128 * Frees matches[].
2129 */
2130 static void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002131ins_compl_add_matches(num_matches, matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 int num_matches;
2133 char_u **matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134{
2135 int i;
2136 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002137 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
Bram Moolenaar572cb562005-08-05 21:35:02 +00002139 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002140 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, dir, 0)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002142 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 FreeWild(num_matches, matches);
2144}
2145
2146/* Make the completion list cyclic.
2147 * Return the number of matches (excluding the original).
2148 */
2149 static int
2150ins_compl_make_cyclic()
2151{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002152 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 int count = 0;
2154
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002155 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
2157 /*
2158 * Find the end of the list.
2159 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002160 match = compl_first_match;
2161 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002162 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002164 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 ++count;
2166 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002167 match->cp_next = compl_first_match;
2168 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
2170 return count;
2171}
2172
Bram Moolenaar9372a112005-12-06 19:59:18 +00002173/* "compl_match_array" points the currently displayed list of entries in the
2174 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002175static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002176static int compl_match_arraysize;
2177
2178/*
2179 * Update the screen and when there is any scrolling remove the popup menu.
2180 */
2181 static void
2182ins_compl_upd_pum()
2183{
2184 int h;
2185
2186 if (compl_match_array != NULL)
2187 {
2188 h = curwin->w_cline_height;
2189 update_screen(0);
2190 if (h != curwin->w_cline_height)
2191 ins_compl_del_pum();
2192 }
2193}
2194
2195/*
2196 * Remove any popup menu.
2197 */
2198 static void
2199ins_compl_del_pum()
2200{
2201 if (compl_match_array != NULL)
2202 {
2203 pum_undisplay();
2204 vim_free(compl_match_array);
2205 compl_match_array = NULL;
2206 }
2207}
2208
2209/*
2210 * Return TRUE if the popup menu should be displayed.
2211 */
2212 static int
2213pum_wanted()
2214{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002215 /* 'completeopt' must contain "menu" */
2216 if (*p_cot == NUL)
2217 return FALSE;
2218
2219 /* The display looks bad on a B&W display. */
2220 if (t_colors < 8
2221#ifdef FEAT_GUI
2222 && !gui.in_use
2223#endif
2224 )
2225 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002226 return TRUE;
2227}
2228
2229/*
2230 * Return TRUE if there are two or more matches to be shown in the popup menu.
2231 */
2232 static int
2233pum_two_or_more()
2234{
2235 compl_T *compl;
2236 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002237
2238 /* Don't display the popup menu if there are no matches or there is only
2239 * one (ignoring the original text). */
2240 compl = compl_first_match;
2241 i = 0;
2242 do
2243 {
2244 if (compl == NULL
2245 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2246 break;
2247 compl = compl->cp_next;
2248 } while (compl != compl_first_match);
2249
2250 return (i >= 2);
2251}
2252
2253/*
2254 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002255 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002256 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002257 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002258ins_compl_show_pum()
2259{
2260 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002261 compl_T *shown_compl = NULL;
2262 int did_find_shown_match = FALSE;
2263 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002264 int i;
2265 int cur = -1;
2266 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002267 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002268
Bram Moolenaara6557602006-02-04 22:43:20 +00002269 if (!pum_wanted() || !pum_two_or_more())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002270 return;
2271
2272 /* Update the screen before drawing the popup menu over it. */
2273 update_screen(0);
2274
2275 if (compl_match_array == NULL)
2276 {
2277 /* Need to build the popup menu list. */
2278 compl_match_arraysize = 0;
2279 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002280 if (compl_leader != NULL)
2281 lead_len = STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002282 do
2283 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002284 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2285 && (compl_leader == NULL
2286 || STRNCMP(compl->cp_str, compl_leader, lead_len) == 0))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002287 ++compl_match_arraysize;
2288 compl = compl->cp_next;
2289 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002290 if (compl_match_arraysize == 0)
2291 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002292 compl_match_array = (pumitem_T *)alloc_clear(
2293 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002294 * compl_match_arraysize));
2295 if (compl_match_array != NULL)
2296 {
2297 i = 0;
2298 compl = compl_first_match;
2299 do
2300 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002301 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2302 && (compl_leader == NULL
2303 || STRNCMP(compl->cp_str, compl_leader,
2304 lead_len) == 0))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002305 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002306 if (!shown_match_ok)
2307 {
2308 if (compl == compl_shown_match || did_find_shown_match)
2309 {
2310 /* This item is the shown match or this is the
2311 * first displayed item after the shown match. */
2312 compl_shown_match = compl;
2313 did_find_shown_match = TRUE;
2314 shown_match_ok = TRUE;
2315 }
2316 else
2317 /* Remember this displayed match for when the
2318 * shown match is just below it. */
2319 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002320 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002321 }
2322 compl_match_array[i].pum_text = compl->cp_str;
2323 if (compl->cp_extra != NULL)
2324 compl_match_array[i++].pum_extra = compl->cp_extra;
2325 else
2326 compl_match_array[i++].pum_extra = compl->cp_fname;
2327 }
2328
2329 if (compl == compl_shown_match)
2330 {
2331 did_find_shown_match = TRUE;
2332 if (!shown_match_ok && shown_compl != NULL)
2333 {
2334 /* The shown match isn't displayed, set it to the
2335 * previously displayed match. */
2336 compl_shown_match = shown_compl;
2337 shown_match_ok = TRUE;
2338 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002339 }
2340 compl = compl->cp_next;
2341 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002342
2343 if (!shown_match_ok) /* no displayed match at all */
2344 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002345 }
2346 }
2347 else
2348 {
2349 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002350 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002351 if (compl_match_array[i].pum_text == compl_shown_match->cp_str)
Bram Moolenaara6557602006-02-04 22:43:20 +00002352 break;
2353 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002354 }
2355
2356 if (compl_match_array != NULL)
2357 {
2358 /* Compute the screen column of the start of the completed text.
2359 * Use the cursor to get all wrapping and other settings right. */
2360 col = curwin->w_cursor.col;
2361 curwin->w_cursor.col = compl_col;
2362 validate_cursor_col();
2363 pum_display(compl_match_array, compl_match_arraysize, cur,
2364 curwin->w_cline_row + W_WINROW(curwin),
2365 curwin->w_cline_height,
Bram Moolenaar280f1262006-01-30 00:14:18 +00002366 curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002367 curwin->w_cursor.col = col;
2368 }
2369}
2370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371#define DICT_FIRST (1) /* use just first element in "dict" */
2372#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002373
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374/*
2375 * Add any identifiers that match the given pattern to the list of
2376 * completions.
2377 */
2378 static void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002379ins_compl_dictionaries(dict, pat, flags, thesaurus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 char_u *dict;
2381 char_u *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 int flags;
2383 int thesaurus;
2384{
2385 char_u *ptr;
2386 char_u *buf;
2387 FILE *fp;
2388 regmatch_T regmatch;
2389 int add_r;
2390 char_u **files;
2391 int count;
2392 int i;
2393 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002394 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395
2396 buf = alloc(LSIZE);
2397 /* If 'infercase' is set, don't use 'smartcase' here */
2398 save_p_scs = p_scs;
2399 if (curbuf->b_p_inf)
2400 p_scs = FALSE;
2401 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2402 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2403 regmatch.rm_ic = ignorecase(pat);
2404 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002405 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
2407 /* copy one dictionary file name into buf */
2408 if (flags == DICT_EXACT)
2409 {
2410 count = 1;
2411 files = &dict;
2412 }
2413 else
2414 {
2415 /* Expand wildcards in the dictionary name, but do not allow
2416 * backticks (for security, the 'dict' option may have been set in
2417 * a modeline). */
2418 copy_option_part(&dict, buf, LSIZE, ",");
2419 if (vim_strchr(buf, '`') != NULL
2420 || expand_wildcards(1, &buf, &count, &files,
2421 EW_FILE|EW_SILENT) != OK)
2422 count = 0;
2423 }
2424
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002425 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
2427 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2428 if (flags != DICT_EXACT)
2429 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002430 vim_snprintf((char *)IObuff, IOSIZE,
2431 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2433 }
2434
2435 if (fp != NULL)
2436 {
2437 /*
2438 * Read dictionary file line by line.
2439 * Check each line for a match.
2440 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002441 while (!got_int && !compl_interrupted
2442 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
2444 ptr = buf;
2445 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2446 {
2447 ptr = regmatch.startp[0];
2448 ptr = find_word_end(ptr);
2449 add_r = ins_compl_add_infercase(regmatch.startp[0],
2450 (int)(ptr - regmatch.startp[0]),
2451 files[i], dir, 0);
2452 if (thesaurus)
2453 {
2454 char_u *wstart;
2455
2456 /*
2457 * Add the other matches on the line
2458 */
2459 while (!got_int)
2460 {
2461 /* Find start of the next word. Skip white
2462 * space and punctuation. */
2463 ptr = find_word_start(ptr);
2464 if (*ptr == NUL || *ptr == NL)
2465 break;
2466 wstart = ptr;
2467
2468 /* Find end of the word and add it. */
2469#ifdef FEAT_MBYTE
2470 if (has_mbyte)
2471 /* Japanese words may have characters in
2472 * different classes, only separate words
2473 * with single-byte non-word characters. */
2474 while (*ptr != NUL)
2475 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002476 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
2478 if (l < 2 && !vim_iswordc(*ptr))
2479 break;
2480 ptr += l;
2481 }
2482 else
2483#endif
2484 ptr = find_word_end(ptr);
2485 add_r = ins_compl_add_infercase(wstart,
2486 (int)(ptr - wstart), files[i], dir, 0);
2487 }
2488 }
2489 if (add_r == OK)
2490 /* if dir was BACKWARD then honor it just once */
2491 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002492 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 break;
2494 /* avoid expensive call to vim_regexec() when at end
2495 * of line */
2496 if (*ptr == '\n' || got_int)
2497 break;
2498 }
2499 line_breakcheck();
Bram Moolenaar572cb562005-08-05 21:35:02 +00002500 ins_compl_check_keys(50);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 }
2502 fclose(fp);
2503 }
2504 }
2505 if (flags != DICT_EXACT)
2506 FreeWild(count, files);
2507 if (flags)
2508 break;
2509 }
2510 p_scs = save_p_scs;
2511 vim_free(regmatch.regprog);
2512 vim_free(buf);
2513}
2514
2515/*
2516 * Find the start of the next word.
2517 * Returns a pointer to the first char of the word. Also stops at a NUL.
2518 */
2519 char_u *
2520find_word_start(ptr)
2521 char_u *ptr;
2522{
2523#ifdef FEAT_MBYTE
2524 if (has_mbyte)
2525 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002526 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 else
2528#endif
2529 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2530 ++ptr;
2531 return ptr;
2532}
2533
2534/*
2535 * Find the end of the word. Assumes it starts inside a word.
2536 * Returns a pointer to just after the word.
2537 */
2538 char_u *
2539find_word_end(ptr)
2540 char_u *ptr;
2541{
2542#ifdef FEAT_MBYTE
2543 int start_class;
2544
2545 if (has_mbyte)
2546 {
2547 start_class = mb_get_class(ptr);
2548 if (start_class > 1)
2549 while (*ptr != NUL)
2550 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002551 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 if (mb_get_class(ptr) != start_class)
2553 break;
2554 }
2555 }
2556 else
2557#endif
2558 while (vim_iswordc(*ptr))
2559 ++ptr;
2560 return ptr;
2561}
2562
2563/*
2564 * Free the list of completions
2565 */
2566 static void
2567ins_compl_free()
2568{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002569 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002571 vim_free(compl_pattern);
2572 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002573 vim_free(compl_leader);
2574 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002576 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002578
2579 ins_compl_del_pum();
2580 pum_clear();
2581
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002582 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 do
2584 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002585 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002586 compl_curr_match = compl_curr_match->cp_next;
2587 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002589 if (match->cp_flags & FREE_FNAME)
2590 vim_free(match->cp_fname);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002591 vim_free(match->cp_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002593 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2594 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595}
2596
2597 static void
2598ins_compl_clear()
2599{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002600 compl_cont_status = 0;
2601 compl_started = FALSE;
2602 compl_matches = 0;
2603 vim_free(compl_pattern);
2604 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002605 vim_free(compl_leader);
2606 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 save_sm = -1;
2608 edit_submode_extra = NULL;
2609}
2610
2611/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002612 * Delete one character before the cursor and show the subset of the matches
2613 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002614 * Returns TRUE if the work is done and another char to be got from the user.
2615 */
2616 static int
2617ins_compl_bs()
2618{
2619 char_u *line;
2620 char_u *p;
2621
2622 if (curwin->w_cursor.col <= compl_col + compl_length)
2623 {
2624 /* Deleted more than what was used to find matches, need to look for
2625 * matches all over again. */
2626 ins_compl_free();
2627 compl_started = FALSE;
2628 compl_matches = 0;
2629 }
2630
2631 line = ml_get_curline();
2632 p = line + curwin->w_cursor.col;
2633 mb_ptr_back(line, p);
2634
2635 vim_free(compl_leader);
2636 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2637 if (compl_leader != NULL)
2638 {
2639 ins_compl_del_pum();
2640 ins_compl_delete();
2641 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2642
2643 if (!compl_started)
2644 {
2645 /* Matches were cleared, need to search for them now. */
2646 if (ins_complete(Ctrl_N) == FAIL)
2647 compl_cont_status = 0;
2648 else
2649 {
2650 /* Remove the completed word again. */
2651 ins_compl_delete();
2652 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2653 }
2654 }
2655
2656 /* Show the popup menu with a different set of matches. */
2657 ins_compl_show_pum();
2658 compl_used_match = FALSE;
2659
2660 return TRUE;
2661 }
2662 return FALSE;
2663}
2664
2665/*
2666 * Append one character to the match leader. May reduce the number of
2667 * matches.
2668 */
2669 static void
2670ins_compl_addleader(c)
2671 int c;
2672{
2673#ifdef FEAT_MBYTE
2674 int cc;
2675
2676 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2677 {
2678 char_u buf[MB_MAXBYTES + 1];
2679
2680 (*mb_char2bytes)(c, buf);
2681 buf[cc] = NUL;
2682 ins_char_bytes(buf, cc);
2683 }
2684 else
2685#endif
2686 ins_char(c);
2687
2688 vim_free(compl_leader);
2689 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
2690 curwin->w_cursor.col - compl_col);
2691 if (compl_leader != NULL)
2692 {
2693 /* Show the popup menu with a different set of matches. */
2694 ins_compl_del_pum();
2695 ins_compl_show_pum();
2696 compl_used_match = FALSE;
2697 }
2698}
2699
2700/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002701 * Append one character to the match leader. May reduce the number of
2702 * matches.
2703 */
2704 static void
2705ins_compl_addfrommatch()
2706{
2707 char_u *p;
2708 int len = curwin->w_cursor.col - compl_col;
2709 int c;
2710
2711 p = compl_shown_match->cp_str;
2712 if (STRLEN(p) <= len) /* the match is too short */
2713 return;
2714 p += len;
2715#ifdef FEAT_MBYTE
2716 c = mb_ptr2char(p);
2717#else
2718 c = *p;
2719#endif
2720 ins_compl_addleader(c);
2721}
2722
2723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002725 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002726 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002728 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729ins_compl_prep(c)
2730 int c;
2731{
2732 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 int temp;
2734 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002735 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
2737 /* Forget any previous 'special' messages if this is actually
2738 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2739 */
2740 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2741 edit_submode_extra = NULL;
2742
2743 /* Ignore end of Select mode mapping */
2744 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002745 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2748 {
2749 /*
2750 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2751 * it will be yet. Now we decide.
2752 */
2753 switch (c)
2754 {
2755 case Ctrl_E:
2756 case Ctrl_Y:
2757 ctrl_x_mode = CTRL_X_SCROLL;
2758 if (!(State & REPLACE_FLAG))
2759 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2760 else
2761 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2762 edit_submode_pre = NULL;
2763 showmode();
2764 break;
2765 case Ctrl_L:
2766 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2767 break;
2768 case Ctrl_F:
2769 ctrl_x_mode = CTRL_X_FILES;
2770 break;
2771 case Ctrl_K:
2772 ctrl_x_mode = CTRL_X_DICTIONARY;
2773 break;
2774 case Ctrl_R:
2775 /* Simply allow ^R to happen without affecting ^X mode */
2776 break;
2777 case Ctrl_T:
2778 ctrl_x_mode = CTRL_X_THESAURUS;
2779 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002780#ifdef FEAT_COMPL_FUNC
2781 case Ctrl_U:
2782 ctrl_x_mode = CTRL_X_FUNCTION;
2783 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002784 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002785 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002786 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002787#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002788 case 's':
2789 case Ctrl_S:
2790 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002791#ifdef FEAT_SYN_HL
2792 spell_back_to_badword();
2793#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002794 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 case Ctrl_RSB:
2796 ctrl_x_mode = CTRL_X_TAGS;
2797 break;
2798#ifdef FEAT_FIND_ID
2799 case Ctrl_I:
2800 case K_S_TAB:
2801 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2802 break;
2803 case Ctrl_D:
2804 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2805 break;
2806#endif
2807 case Ctrl_V:
2808 case Ctrl_Q:
2809 ctrl_x_mode = CTRL_X_CMDLINE;
2810 break;
2811 case Ctrl_P:
2812 case Ctrl_N:
2813 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
2814 * just started ^X mode, or there were enough ^X's to cancel
2815 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2816 * do normal expansion when interrupting a different mode (say
2817 * ^X^F^X^P or ^P^X^X^P, see below)
2818 * nothing changes if interrupting mode 0, (eg, the flag
2819 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002820 if (!(compl_cont_status & CONT_INTRPT))
2821 compl_cont_status |= CONT_LOCAL;
2822 else if (compl_cont_mode != 0)
2823 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 /* FALLTHROUGH */
2825 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002826 /* If we have typed at least 2 ^X's... for modes != 0, we set
2827 * compl_cont_status = 0 (eg, as if we had just started ^X
2828 * mode).
2829 * For mode 0, we set "compl_cont_mode" to an impossible
2830 * value, in both cases ^X^X can be used to restart the same
2831 * mode (avoiding ADDING mode).
2832 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2833 * 'complete' and local ^P expansions respectively.
2834 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2835 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 if (c == Ctrl_X)
2837 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002838 if (compl_cont_mode != 0)
2839 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002841 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 }
2843 ctrl_x_mode = 0;
2844 edit_submode = NULL;
2845 showmode();
2846 break;
2847 }
2848 }
2849 else if (ctrl_x_mode != 0)
2850 {
2851 /* We're already in CTRL-X mode, do we stay in it? */
2852 if (!vim_is_ctrl_x_key(c))
2853 {
2854 if (ctrl_x_mode == CTRL_X_SCROLL)
2855 ctrl_x_mode = 0;
2856 else
2857 ctrl_x_mode = CTRL_X_FINISHED;
2858 edit_submode = NULL;
2859 }
2860 showmode();
2861 }
2862
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002863 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 {
2865 /* Show error message from attempted keyword completion (probably
2866 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002867 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002869 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
2870 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 || ctrl_x_mode == CTRL_X_FINISHED)
2872 {
2873 /* Get here when we have finished typing a sequence of ^N and
2874 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002875 * memory that was used, and make sure we can redo the insert. */
2876 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002878 char_u *p;
2879
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 /*
2881 * If any of the original typed text has been changed,
2882 * eg when ignorecase is set, we must add back-spaces to
2883 * the redo buffer. We add as few as necessary to delete
2884 * just the part of the original text that has changed.
2885 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002886 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002887 p = compl_orig_text;
2888 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002890 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 ++ptr;
2892 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002893 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002895 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
2897
2898#ifdef FEAT_CINDENT
2899 want_cindent = (can_cindent && cindent_on());
2900#endif
2901 /*
2902 * When completing whole lines: fix indent for 'cindent'.
2903 * Otherwise, break line if it's too long.
2904 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002905 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 {
2907#ifdef FEAT_CINDENT
2908 /* re-indent the current line */
2909 if (want_cindent)
2910 {
2911 do_c_expr_indent();
2912 want_cindent = FALSE; /* don't do it again */
2913 }
2914#endif
2915 }
2916 else
2917 {
2918 /* put the cursor on the last char, for 'tw' formatting */
2919 curwin->w_cursor.col--;
2920 if (stop_arrow() == OK)
2921 insertchar(NUL, 0, -1);
2922 curwin->w_cursor.col++;
2923 }
2924
2925 auto_format(FALSE, TRUE);
2926
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002927 /* if the popup menu is displayed hitting Enter means accepting
2928 * the selection without inserting anything. */
2929 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
2930 retval = TRUE;
2931
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002933 compl_started = FALSE;
2934 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 msg_clr_cmdline(); /* necessary for "noshowmode" */
2936 ctrl_x_mode = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002937 if (save_sm >= 0)
2938 p_sm = save_sm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 if (edit_submode != NULL)
2940 {
2941 edit_submode = NULL;
2942 showmode();
2943 }
2944
2945#ifdef FEAT_CINDENT
2946 /*
2947 * Indent now if a key was typed that is in 'cinkeys'.
2948 */
2949 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2950 do_c_expr_indent();
2951#endif
2952 }
2953 }
2954
2955 /* reset continue_* if we left expansion-mode, if we stay they'll be
2956 * (re)set properly in ins_complete() */
2957 if (!vim_is_ctrl_x_key(c))
2958 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002959 compl_cont_status = 0;
2960 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002962
2963 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964}
2965
2966/*
2967 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2968 * (depending on flag) starting from buf and looking for a non-scanned
2969 * buffer (other than curbuf). curbuf is special, if it is called with
2970 * buf=curbuf then it has to be the first call for a given flag/expansion.
2971 *
2972 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2973 */
2974 static buf_T *
2975ins_compl_next_buf(buf, flag)
2976 buf_T *buf;
2977 int flag;
2978{
2979#ifdef FEAT_WINDOWS
2980 static win_T *wp;
2981#endif
2982
2983 if (flag == 'w') /* just windows */
2984 {
2985#ifdef FEAT_WINDOWS
2986 if (buf == curbuf) /* first call for this flag/expansion */
2987 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002988 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 && wp->w_buffer->b_scanned)
2990 ;
2991 buf = wp->w_buffer;
2992#else
2993 buf = curbuf;
2994#endif
2995 }
2996 else
2997 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2998 * (unlisted buffers)
2999 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003000 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 && ((flag == 'U'
3002 ? buf->b_p_bl
3003 : (!buf->b_p_bl
3004 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
3005 || buf->b_scanned
3006 || (buf->b_ml.ml_mfp == NULL
3007 && ctrl_x_mode == CTRL_X_WHOLE_LINE)))
3008 ;
3009 return buf;
3010}
3011
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003012#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003013static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003014
3015/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003016 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003017 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003018 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003019 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003020 static void
3021expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003022 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003023 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003024{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003025 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003026 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003027 listitem_T *li;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003028 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003029 char_u *funcname;
3030 pos_T pos;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003031 int dir = compl_direction;
3032 char_u *x;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003033
Bram Moolenaare344bea2005-09-01 20:46:49 +00003034 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3035 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003036 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003037
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003038 /* Call 'completefunc' to obtain the list of matches. */
3039 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003040 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003041
Bram Moolenaare344bea2005-09-01 20:46:49 +00003042 pos = curwin->w_cursor;
3043 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3044 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003045 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003046 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003047
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003048 /* Go through the List with matches and add each of them. */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003049 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003050 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003051 if (li->li_tv.v_type == VAR_DICT && li->li_tv.vval.v_dict != NULL)
3052 {
3053 p = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"word", FALSE);
3054 x = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"menu", FALSE);
3055 }
3056 else
3057 {
3058 p = get_tv_string_chk(&li->li_tv);
3059 x = NULL;
3060 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003061 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003062 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003063 if (ins_compl_add(p, -1, NULL, x, dir, 0) == OK)
3064 /* if dir was BACKWARD then honor it just once */
3065 dir = FORWARD;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003066 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003067 else if (did_emsg)
3068 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003069 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003070
3071 list_unref(matchlist);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003072}
3073#endif /* FEAT_COMPL_FUNC */
3074
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003075/*
3076 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003077 * The search starts at position "ini" in curbuf and in the direction
3078 * compl_direction.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003079 * When "compl_started" is FALSE start at that position, otherwise
3080 * continue where we stopped searching before.
3081 * This may return before finding all the matches.
3082 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 */
3084 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003085ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087{
3088 static pos_T first_match_pos;
3089 static pos_T last_match_pos;
3090 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003091 static int found_all = FALSE; /* Found all matches of a
3092 certain type. */
3093 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094
Bram Moolenaar572cb562005-08-05 21:35:02 +00003095 pos_T *pos;
3096 char_u **matches;
3097 int save_p_scs;
3098 int save_p_ws;
3099 int save_p_ic;
3100 int i;
3101 int num_matches;
3102 int len;
3103 int found_new_match;
3104 int type = ctrl_x_mode;
3105 char_u *ptr;
3106 char_u *dict = NULL;
3107 int dict_f = 0;
3108 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003110 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
3112 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3113 ins_buf->b_scanned = 0;
3114 found_all = FALSE;
3115 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003116 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003117 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 last_match_pos = first_match_pos = *ini;
3119 }
3120
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003121 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003122 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3124 for (;;)
3125 {
3126 found_new_match = FAIL;
3127
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003128 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 * or if found_all says this entry is done. For ^X^L only use the
3130 * entries from 'complete' that look in loaded buffers. */
3131 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003132 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 {
3134 found_all = FALSE;
3135 while (*e_cpt == ',' || *e_cpt == ' ')
3136 e_cpt++;
3137 if (*e_cpt == '.' && !curbuf->b_scanned)
3138 {
3139 ins_buf = curbuf;
3140 first_match_pos = *ini;
3141 /* So that ^N can match word immediately after cursor */
3142 if (ctrl_x_mode == 0)
3143 dec(&first_match_pos);
3144 last_match_pos = first_match_pos;
3145 type = 0;
3146 }
3147 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3148 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3149 {
3150 /* Scan a buffer, but not the current one. */
3151 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3152 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003153 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 first_match_pos.col = last_match_pos.col = 0;
3155 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3156 last_match_pos.lnum = 0;
3157 type = 0;
3158 }
3159 else /* unloaded buffer, scan like dictionary */
3160 {
3161 found_all = TRUE;
3162 if (ins_buf->b_fname == NULL)
3163 continue;
3164 type = CTRL_X_DICTIONARY;
3165 dict = ins_buf->b_fname;
3166 dict_f = DICT_EXACT;
3167 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003168 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 ins_buf->b_fname == NULL
3170 ? buf_spname(ins_buf)
3171 : ins_buf->b_sfname == NULL
3172 ? (char *)ins_buf->b_fname
3173 : (char *)ins_buf->b_sfname);
3174 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3175 }
3176 else if (*e_cpt == NUL)
3177 break;
3178 else
3179 {
3180 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3181 type = -1;
3182 else if (*e_cpt == 'k' || *e_cpt == 's')
3183 {
3184 if (*e_cpt == 'k')
3185 type = CTRL_X_DICTIONARY;
3186 else
3187 type = CTRL_X_THESAURUS;
3188 if (*++e_cpt != ',' && *e_cpt != NUL)
3189 {
3190 dict = e_cpt;
3191 dict_f = DICT_FIRST;
3192 }
3193 }
3194#ifdef FEAT_FIND_ID
3195 else if (*e_cpt == 'i')
3196 type = CTRL_X_PATH_PATTERNS;
3197 else if (*e_cpt == 'd')
3198 type = CTRL_X_PATH_DEFINES;
3199#endif
3200 else if (*e_cpt == ']' || *e_cpt == 't')
3201 {
3202 type = CTRL_X_TAGS;
3203 sprintf((char*)IObuff, _("Scanning tags."));
3204 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3205 }
3206 else
3207 type = -1;
3208
3209 /* in any case e_cpt is advanced to the next entry */
3210 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3211
3212 found_all = TRUE;
3213 if (type == -1)
3214 continue;
3215 }
3216 }
3217
3218 switch (type)
3219 {
3220 case -1:
3221 break;
3222#ifdef FEAT_FIND_ID
3223 case CTRL_X_PATH_PATTERNS:
3224 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003225 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003226 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003228 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3230 (linenr_T)1, (linenr_T)MAXLNUM);
3231 break;
3232#endif
3233
3234 case CTRL_X_DICTIONARY:
3235 case CTRL_X_THESAURUS:
3236 ins_compl_dictionaries(
3237 dict ? dict
3238 : (type == CTRL_X_THESAURUS
3239 ? (*curbuf->b_p_tsr == NUL
3240 ? p_tsr
3241 : curbuf->b_p_tsr)
3242 : (*curbuf->b_p_dict == NUL
3243 ? p_dict
3244 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003245 compl_pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
3247 dict = NULL;
3248 break;
3249
3250 case CTRL_X_TAGS:
3251 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3252 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003253 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
3255 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003256 * of matches is found when compl_pattern is empty */
3257 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3259 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3260 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3261 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003262 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 }
3264 p_ic = save_p_ic;
3265 break;
3266
3267 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003268 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3270 {
3271
3272 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003273 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003274 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
3276 break;
3277
3278 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003279 if (expand_cmdline(&compl_xp, compl_pattern,
3280 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003282 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 break;
3284
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003285#ifdef FEAT_COMPL_FUNC
3286 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003287 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003288 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003289 break;
3290#endif
3291
Bram Moolenaar488c6512005-08-11 20:09:58 +00003292 case CTRL_X_SPELL:
3293#ifdef FEAT_SYN_HL
3294 num_matches = expand_spelling(first_match_pos.lnum,
3295 first_match_pos.col, compl_pattern, &matches);
3296 if (num_matches > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003297 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003298#endif
3299 break;
3300
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 default: /* normal ^P/^N and ^X^L */
3302 /*
3303 * If 'infercase' is set, don't use 'smartcase' here
3304 */
3305 save_p_scs = p_scs;
3306 if (ins_buf->b_p_inf)
3307 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003308
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 /* buffers other than curbuf are scanned from the beginning or the
3310 * end but never from the middle, thus setting nowrapscan in this
3311 * buffers is a good idea, on the other hand, we always set
3312 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3313 save_p_ws = p_ws;
3314 if (ins_buf != curbuf)
3315 p_ws = FALSE;
3316 else if (*e_cpt == '.')
3317 p_ws = TRUE;
3318 for (;;)
3319 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003320 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003322 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3323 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003325 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003327 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003329 found_new_match = searchit(NULL, ins_buf, pos,
3330 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003331 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 RE_LAST);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003333 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003335 /* set compl_started even on fail */
3336 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 first_match_pos = *pos;
3338 last_match_pos = *pos;
3339 }
3340 else if (first_match_pos.lnum == last_match_pos.lnum
3341 && first_match_pos.col == last_match_pos.col)
3342 found_new_match = FAIL;
3343 if (found_new_match == FAIL)
3344 {
3345 if (ins_buf == curbuf)
3346 found_all = TRUE;
3347 break;
3348 }
3349
3350 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003351 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 && ini->lnum == pos->lnum
3353 && ini->col == pos->col)
3354 continue;
3355 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3356 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3357 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003358 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
3360 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3361 continue;
3362 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3363 if (!p_paste)
3364 ptr = skipwhite(ptr);
3365 }
3366 len = (int)STRLEN(ptr);
3367 }
3368 else
3369 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003370 char_u *tmp_ptr = ptr;
3371
3372 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003374 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 /* Skip if already inside a word. */
3376 if (vim_iswordp(tmp_ptr))
3377 continue;
3378 /* Find start of next word. */
3379 tmp_ptr = find_word_start(tmp_ptr);
3380 }
3381 /* Find end of this word. */
3382 tmp_ptr = find_word_end(tmp_ptr);
3383 len = (int)(tmp_ptr - ptr);
3384
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003385 if ((compl_cont_status & CONT_ADDING)
3386 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 {
3388 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3389 {
3390 /* Try next line, if any. the new word will be
3391 * "join" as if the normal command "J" was used.
3392 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003393 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 * works -- Acevedo */
3395 STRNCPY(IObuff, ptr, len);
3396 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3397 tmp_ptr = ptr = skipwhite(ptr);
3398 /* Find start of next word. */
3399 tmp_ptr = find_word_start(tmp_ptr);
3400 /* Find end of next word. */
3401 tmp_ptr = find_word_end(tmp_ptr);
3402 if (tmp_ptr > ptr)
3403 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003404 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003406 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 IObuff[len++] = ' ';
3408 /* IObuf =~ "\k.* ", thus len >= 2 */
3409 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003410 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 || (vim_strchr(p_cpo, CPO_JOINSP)
3412 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003413 && (IObuff[len - 2] == '?'
3414 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 IObuff[len++] = ' ';
3416 }
3417 /* copy as much as posible of the new word */
3418 if (tmp_ptr - ptr >= IOSIZE - len)
3419 tmp_ptr = ptr + IOSIZE - len - 1;
3420 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3421 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003422 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 }
3424 IObuff[len] = NUL;
3425 ptr = IObuff;
3426 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003427 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 continue;
3429 }
3430 }
3431 if (ins_compl_add_infercase(ptr, len,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003432 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003433 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 {
3435 found_new_match = OK;
3436 break;
3437 }
3438 }
3439 p_scs = save_p_scs;
3440 p_ws = save_p_ws;
3441 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003442
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003443 /* check if compl_curr_match has changed, (e.g. other type of
3444 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003445 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 found_new_match = OK;
3447
3448 /* break the loop for specialized modes (use 'complete' just for the
3449 * generic ctrl_x_mode == 0) or when we've found a new match */
3450 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003451 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003452 {
3453 if (got_int)
3454 break;
3455 if (pum_wanted() && type != -1)
3456 /* Fill the popup menu as soon as possible. */
3457 ins_compl_check_keys(0);
3458 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3459 || compl_interrupted)
3460 break;
3461 compl_started = TRUE;
3462 }
3463 else
3464 {
3465 /* Mark a buffer scanned when it has been scanned completely */
3466 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3467 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003469 compl_started = FALSE;
3470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003472 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473
3474 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3475 && *e_cpt == NUL) /* Got to end of 'complete' */
3476 found_new_match = FAIL;
3477
3478 i = -1; /* total of matches, unknown */
3479 if (found_new_match == FAIL
3480 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3481 i = ins_compl_make_cyclic();
3482
3483 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003484 * just been made cyclic then we have to move compl_curr_match to the next
3485 * or previous entry (if any) -- Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003486 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003487 if (compl_curr_match == NULL)
3488 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 return i;
3490}
3491
3492/* Delete the old text being completed. */
3493 static void
3494ins_compl_delete()
3495{
3496 int i;
3497
3498 /*
3499 * In insert mode: Delete the typed part.
3500 * In replace mode: Put the old characters back, if any.
3501 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003502 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 backspace_until_column(i);
3504 changed_cline_bef_curs();
3505}
3506
3507/* Insert the new text being completed. */
3508 static void
3509ins_compl_insert()
3510{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003511 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003512 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513}
3514
3515/*
3516 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003517 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3518 * get more completions. If it is FALSE, then we just do nothing when there
3519 * are no more completions in a given direction. The latter case is used when
3520 * we are still in the middle of finding completions, to allow browsing
3521 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 * Return the total number of matches, or -1 if still unknown -- webb.
3523 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003524 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3525 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 *
3527 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003528 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3529 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 */
3531 static int
Bram Moolenaare3226be2005-12-18 22:10:00 +00003532ins_compl_next(allow_get_expansion, count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003534 int count; /* repeat completion this many times; should
3535 be at least 1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536{
3537 int num_matches = -1;
3538 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003539 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003540 compl_T *found_compl = NULL;
3541 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
3543 if (allow_get_expansion)
3544 {
3545 /* Delete old text to be replaced */
3546 ins_compl_delete();
3547 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003548 compl_pending = FALSE;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003549
3550 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3551 * around. */
3552 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003554 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003556 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003557 found_end = (compl_first_match != NULL
3558 && (compl_shown_match->cp_next == compl_first_match
3559 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003560 }
3561 else if (compl_shows_dir == BACKWARD
3562 && compl_shown_match->cp_prev != NULL)
3563 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003564 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003565 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003566 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
3568 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003569 {
3570 compl_pending = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003571 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003572 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003573
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003574 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara6557602006-02-04 22:43:20 +00003575 if (compl_pending && compl_direction == compl_shows_dir)
3576 compl_shown_match = compl_curr_match;
3577 found_end = FALSE;
3578 }
3579 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
3580 && compl_leader != NULL
3581 && STRNCMP(compl_shown_match->cp_str,
3582 compl_leader, STRLEN(compl_leader)) != 0)
3583 ++todo;
3584 else
3585 /* Remember a matching item. */
3586 found_compl = compl_shown_match;
3587
3588 /* Stop at the end of the list when we found a usable match. */
3589 if (found_end)
3590 {
3591 if (found_compl != NULL)
3592 {
3593 compl_shown_match = found_compl;
3594 break;
3595 }
3596 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
3599
3600 /* Insert the text of the new completion */
3601 ins_compl_insert();
3602
3603 if (!allow_get_expansion)
3604 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003605 /* may undisplay the popup menu first */
3606 ins_compl_upd_pum();
3607
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003608 /* display the updated popup menu */
3609 ins_compl_show_pum();
3610
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 /* Delete old text to be replaced, since we're still searching and
3612 * don't want to match ourselves! */
3613 ins_compl_delete();
3614 }
3615
3616 /*
3617 * Show the file name for the match (if any)
3618 * Truncate the file name to avoid a wait for return.
3619 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003620 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
3622 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003623 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 if (i <= 0)
3625 i = 0;
3626 else
3627 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003628 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 msg(IObuff);
3630 redraw_cmdline = FALSE; /* don't overwrite! */
3631 }
3632
3633 return num_matches;
3634}
3635
3636/*
3637 * Call this while finding completions, to check whether the user has hit a key
3638 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003639 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003641 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 */
3643 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003644ins_compl_check_keys(frequency)
3645 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
3647 static int count = 0;
3648
3649 int c;
3650
3651 /* Don't check when reading keys from a script. That would break the test
3652 * scripts */
3653 if (using_script())
3654 return;
3655
3656 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003657 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 return;
3659 count = 0;
3660
3661 ++no_mapping;
3662 c = vpeekc_any();
3663 --no_mapping;
3664 if (c != NUL)
3665 {
3666 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3667 {
3668 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003669 compl_shows_dir = ins_compl_key2dir(c);
3670 (void)ins_compl_next(FALSE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
3672 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003673 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003675 if (compl_pending && !got_int)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003676 (void)ins_compl_next(FALSE, 1);
3677}
3678
3679/*
3680 * Decide the direction of Insert mode complete from the key typed.
3681 * Returns BACKWARD or FORWARD.
3682 */
3683 static int
3684ins_compl_key2dir(c)
3685 int c;
3686{
3687 if (c == Ctrl_P || c == Ctrl_L || (pum_visible()
3688 && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP)))
3689 return BACKWARD;
3690 return FORWARD;
3691}
3692
3693/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003694 * Return TRUE for keys that are used for completion only when the popup menu
3695 * is visible.
3696 */
3697 static int
3698ins_compl_pum_key(c)
3699 int c;
3700{
3701 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
3702 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN);
3703}
3704
3705/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00003706 * Decide the number of completions to move forward.
3707 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3708 */
3709 static int
3710ins_compl_key2count(c)
3711 int c;
3712{
3713 int h;
3714
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003715 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00003716 {
3717 h = pum_get_height();
3718 if (h > 3)
3719 h -= 2; /* keep some context */
3720 return h;
3721 }
3722 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723}
3724
3725/*
3726 * Do Insert mode completion.
3727 * Called when character "c" was typed, which has a meaning for completion.
3728 * Returns OK if completion was done, FAIL if something failed (out of mem).
3729 */
3730 static int
3731ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003732 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003734 char_u *line;
3735 int startcol = 0; /* column where searched text starts */
3736 colnr_T curs_col; /* cursor column */
3737 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
Bram Moolenaare3226be2005-12-18 22:10:00 +00003739 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003740 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
3742 /* First time we hit ^N or ^P (in a row, I mean) */
3743
3744 /* Turn off 'sm' so we don't show matches with ^X^L */
3745 save_sm = p_sm;
3746 p_sm = FALSE;
3747
3748 did_ai = FALSE;
3749#ifdef FEAT_SMARTINDENT
3750 did_si = FALSE;
3751 can_si = FALSE;
3752 can_si_back = FALSE;
3753#endif
3754 if (stop_arrow() == FAIL)
3755 return FAIL;
3756
3757 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003758 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759
3760 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003761 * "compl_startpos" to the cursor as a pattern to add a new word
3762 * instead of expand the one before the cursor, in word-wise if
3763 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 * is not in the same line as the cursor then fix it (the line has
3765 * been split because it was longer than 'tw'). if SOL is set then
3766 * skip the previous pattern, a word at the beginning of the line has
3767 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003768 if ((compl_cont_status & CONT_INTRPT) && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 {
3770 /*
3771 * it is a continued search
3772 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003773 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3775 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3776 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003777 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003779 /* line (probably) wrapped, set compl_startpos to the
3780 * first non_blank in the line, if it is not a wordchar
3781 * include it to get a better pattern, but then we don't
3782 * want the "\\<" prefix, check it bellow */
3783 compl_col = (colnr_T)(skipwhite(line) - line);
3784 compl_startpos.col = compl_col;
3785 compl_startpos.lnum = curwin->w_cursor.lnum;
3786 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 }
3788 else
3789 {
3790 /* S_IPOS was set when we inserted a word that was at the
3791 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003792 * mode but first we need to redefine compl_startpos */
3793 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003795 compl_cont_status |= CONT_SOL;
3796 compl_startpos.col = (colnr_T)(skipwhite(
3797 line + compl_length
3798 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003800 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003802 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003803 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 * have enough space? just being paranoic */
3805#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003806 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003808 compl_cont_status &= ~CONT_SOL;
3809 compl_length = (IOSIZE - MIN_SPACE);
3810 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003812 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3813 if (compl_length < 1)
3814 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003817 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003819 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 }
3821 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003822 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003824 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003826 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003828 compl_cont_status = 0;
3829 compl_cont_status |= CONT_N_ADDS;
3830 compl_startpos = curwin->w_cursor;
3831 startcol = (int)curs_col;
3832 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834
3835 /* Work out completion pattern and original text -- webb */
3836 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3837 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003838 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3840 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003841 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003843 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003845 compl_col += ++startcol;
3846 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
3848 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003849 compl_pattern = str_foldcase(line + compl_col,
3850 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003852 compl_pattern = vim_strnsave(line + compl_col,
3853 compl_length);
3854 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 return FAIL;
3856 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003857 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 {
3859 char_u *prefix = (char_u *)"\\<";
3860
3861 /* we need 3 extra chars, 1 for the NUL and
3862 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003863 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3864 compl_length) + 3);
3865 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003867 if (!vim_iswordp(line + compl_col)
3868 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 && (
3870#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003871 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003873 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874#endif
3875 )))
3876 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003877 STRCPY((char *)compl_pattern, prefix);
3878 (void)quote_meta(compl_pattern + STRLEN(prefix),
3879 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003881 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003883 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003885 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886#endif
3887 )
3888 {
3889 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003890 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3891 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003893 compl_col += curs_col;
3894 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 }
3896 else
3897 {
3898#ifdef FEAT_MBYTE
3899 /* Search the point of change class of multibyte character
3900 * or not a word single byte character backward. */
3901 if (has_mbyte)
3902 {
3903 int base_class;
3904 int head_off;
3905
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003906 startcol -= (*mb_head_off)(line, line + startcol);
3907 base_class = mb_get_class(line + startcol);
3908 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003910 head_off = (*mb_head_off)(line, line + startcol);
3911 if (base_class != mb_get_class(line + startcol
3912 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003914 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916 }
3917 else
3918#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003919 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 compl_col += ++startcol;
3922 compl_length = (int)curs_col - startcol;
3923 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
3925 /* Only match word with at least two chars -- webb
3926 * there's no need to call quote_meta,
3927 * alloc(7) is enough -- Acevedo
3928 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003929 compl_pattern = alloc(7);
3930 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003932 STRCPY((char *)compl_pattern, "\\<");
3933 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3934 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
3936 else
3937 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003938 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3939 compl_length) + 3);
3940 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003942 STRCPY((char *)compl_pattern, "\\<");
3943 (void)quote_meta(compl_pattern + 2, line + compl_col,
3944 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
3946 }
3947 }
3948 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3949 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003950 compl_col = skipwhite(line) - line;
3951 compl_length = (int)curs_col - (int)compl_col;
3952 if (compl_length < 0) /* cursor in indent: empty pattern */
3953 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003955 compl_pattern = str_foldcase(line + compl_col, compl_length,
3956 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003958 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3959 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 return FAIL;
3961 }
3962 else if (ctrl_x_mode == CTRL_X_FILES)
3963 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003964 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003966 compl_col += ++startcol;
3967 compl_length = (int)curs_col - startcol;
3968 compl_pattern = addstar(line + compl_col, compl_length,
3969 EXPAND_FILES);
3970 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 return FAIL;
3972 }
3973 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3974 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003975 compl_pattern = vim_strnsave(line, curs_col);
3976 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003978 set_cmd_context(&compl_xp, compl_pattern,
3979 (int)STRLEN(compl_pattern), curs_col);
3980 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3981 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003983 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
3984 compl_col = startcol;
3985 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003987 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003988 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00003989#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003990 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00003991 * Call user defined function 'completefunc' with "a:findstart"
3992 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003993 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00003994 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003995 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003996 char_u *funcname;
3997 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003998
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003999 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004000 * string */
4001 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4002 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4003 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004004 {
4005 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4006 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004007 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004008 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004009
4010 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004011 args[1] = NULL;
4012 pos = curwin->w_cursor;
4013 col = call_func_retnr(funcname, 2, args, FALSE);
4014 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004015
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004016 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004017 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004018 compl_col = col;
4019 if ((colnr_T)compl_col > curs_col)
4020 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004021
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004022 /* Setup variables for completion. Need to obtain "line" again,
4023 * it may have become invalid. */
4024 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004025 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004026 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4027 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004028#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004029 return FAIL;
4030 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004031 else if (ctrl_x_mode == CTRL_X_SPELL)
4032 {
4033#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004034 if (spell_bad_len > 0)
4035 compl_col = curs_col - spell_bad_len;
4036 else
4037 compl_col = spell_word_start(startcol);
4038 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004039 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004040 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004041 compl_length = (int)curs_col - compl_col;
4042 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4043 if (compl_pattern == NULL)
4044#endif
4045 return FAIL;
4046 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004047 else
4048 {
4049 EMSG2(_(e_intern2), "ins_complete()");
4050 return FAIL;
4051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004053 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
4055 edit_submode_pre = (char_u *)_(" Adding");
4056 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4057 {
4058 /* Insert a new line, keep indentation but ignore 'comments' */
4059#ifdef FEAT_COMMENTS
4060 char_u *old = curbuf->b_p_com;
4061
4062 curbuf->b_p_com = (char_u *)"";
4063#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004064 compl_startpos.lnum = curwin->w_cursor.lnum;
4065 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 ins_eol('\r');
4067#ifdef FEAT_COMMENTS
4068 curbuf->b_p_com = old;
4069#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004070 compl_length = 0;
4071 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 }
4073 }
4074 else
4075 {
4076 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004077 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004080 if (compl_cont_status & CONT_LOCAL)
4081 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 else
4083 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004086 * "compl_orig_text" itself (not a copy) is added, it will be freed
4087 * when the list of matches is freed. */
4088 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4089 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004090 -1, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004092 vim_free(compl_pattern);
4093 compl_pattern = NULL;
4094 vim_free(compl_orig_text);
4095 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 return FAIL;
4097 }
4098
4099 /* showmode might reset the internal line pointers, so it must
4100 * be called before line = ml_get(), or when this address is no
4101 * longer needed. -- Acevedo.
4102 */
4103 edit_submode_extra = (char_u *)_("-- Searching...");
4104 edit_submode_highl = HLF_COUNT;
4105 showmode();
4106 edit_submode_extra = NULL;
4107 out_flush();
4108 }
4109
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004110 compl_shown_match = compl_curr_match;
4111 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
4113 /*
4114 * Find next match.
4115 */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004116 n = ins_compl_next(TRUE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004118 /* may undisplay the popup menu */
4119 ins_compl_upd_pum();
4120
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004121 if (n > 1) /* all matches have been found */
4122 compl_matches = n;
4123 compl_curr_match = compl_shown_match;
4124 compl_direction = compl_shows_dir;
4125 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
4127 /* eat the ESC to avoid leaving insert mode */
4128 if (got_int && !global_busy)
4129 {
4130 (void)vgetc();
4131 got_int = FALSE;
4132 }
4133
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004134 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004135 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004137 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4138 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4140 edit_submode_highl = HLF_E;
4141 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4142 * because we couldn't expand anything at first place, but if we used
4143 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4144 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004145 if ( compl_length > 1
4146 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 || (ctrl_x_mode != 0
4148 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4149 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004150 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152
Bram Moolenaar572cb562005-08-05 21:35:02 +00004153 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004154 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004156 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157
4158 if (edit_submode_extra == NULL)
4159 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004160 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 {
4162 edit_submode_extra = (char_u *)_("Back at original");
4163 edit_submode_highl = HLF_W;
4164 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004165 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 {
4167 edit_submode_extra = (char_u *)_("Word from other line");
4168 edit_submode_highl = HLF_COUNT;
4169 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004170 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 {
4172 edit_submode_extra = (char_u *)_("The only match");
4173 edit_submode_highl = HLF_COUNT;
4174 }
4175 else
4176 {
4177 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004178 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004180 int number = 0;
4181 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004183 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
4185 /* search backwards for the first valid (!= -1) number.
4186 * This should normally succeed already at the first loop
4187 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004188 for (match = compl_curr_match->cp_prev; match != NULL
4189 && match != compl_first_match;
4190 match = match->cp_prev)
4191 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004193 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 break;
4195 }
4196 if (match != NULL)
4197 /* go up and assign all numbers which are not assigned
4198 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004199 for (match = match->cp_next;
4200 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004201 match = match->cp_next)
4202 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204 else /* BACKWARD */
4205 {
4206 /* search forwards (upwards) for the first valid (!= -1)
4207 * number. This should normally succeed already at the
4208 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004209 for (match = compl_curr_match->cp_next; match != NULL
4210 && match != compl_first_match;
4211 match = match->cp_next)
4212 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004214 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 break;
4216 }
4217 if (match != NULL)
4218 /* go down and assign all numbers which are not
4219 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004220 for (match = match->cp_prev; match
4221 && match->cp_number == -1;
4222 match = match->cp_prev)
4223 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225 }
4226
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004227 /* The match should always have a sequence number now, this is
4228 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004229 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 {
4231 /* Space for 10 text chars. + 2x10-digit no.s */
4232 static char_u match_ref[31];
4233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004234 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004236 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004238 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004239 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004240 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 edit_submode_extra = match_ref;
4242 edit_submode_highl = HLF_R;
4243 if (dollar_vcol)
4244 curs_columns(FALSE);
4245 }
4246 }
4247 }
4248
4249 /* Show a message about what (completion) mode we're in. */
4250 showmode();
4251 if (edit_submode_extra != NULL)
4252 {
4253 if (!p_smd)
4254 msg_attr(edit_submode_extra,
4255 edit_submode_highl < HLF_COUNT
4256 ? hl_attr(edit_submode_highl) : 0);
4257 }
4258 else
4259 msg_clr_cmdline(); /* necessary for "noshowmode" */
4260
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004261 ins_compl_show_pum();
4262
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 return OK;
4264}
4265
4266/*
4267 * Looks in the first "len" chars. of "src" for search-metachars.
4268 * If dest is not NULL the chars. are copied there quoting (with
4269 * a backslash) the metachars, and dest would be NUL terminated.
4270 * Returns the length (needed) of dest
4271 */
4272 static int
4273quote_meta(dest, src, len)
4274 char_u *dest;
4275 char_u *src;
4276 int len;
4277{
4278 int m;
4279
4280 for (m = len; --len >= 0; src++)
4281 {
4282 switch (*src)
4283 {
4284 case '.':
4285 case '*':
4286 case '[':
4287 if (ctrl_x_mode == CTRL_X_DICTIONARY
4288 || ctrl_x_mode == CTRL_X_THESAURUS)
4289 break;
4290 case '~':
4291 if (!p_magic) /* quote these only if magic is set */
4292 break;
4293 case '\\':
4294 if (ctrl_x_mode == CTRL_X_DICTIONARY
4295 || ctrl_x_mode == CTRL_X_THESAURUS)
4296 break;
4297 case '^': /* currently it's not needed. */
4298 case '$':
4299 m++;
4300 if (dest != NULL)
4301 *dest++ = '\\';
4302 break;
4303 }
4304 if (dest != NULL)
4305 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004306# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 /* Copy remaining bytes of a multibyte character. */
4308 if (has_mbyte)
4309 {
4310 int i, mb_len;
4311
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004312 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 if (mb_len > 0 && len >= mb_len)
4314 for (i = 0; i < mb_len; ++i)
4315 {
4316 --len;
4317 ++src;
4318 if (dest != NULL)
4319 *dest++ = *src;
4320 }
4321 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324 if (dest != NULL)
4325 *dest = NUL;
4326
4327 return m;
4328}
4329#endif /* FEAT_INS_EXPAND */
4330
4331/*
4332 * Next character is interpreted literally.
4333 * A one, two or three digit decimal number is interpreted as its byte value.
4334 * If one or two digits are entered, the next character is given to vungetc().
4335 * For Unicode a character > 255 may be returned.
4336 */
4337 int
4338get_literal()
4339{
4340 int cc;
4341 int nc;
4342 int i;
4343 int hex = FALSE;
4344 int octal = FALSE;
4345#ifdef FEAT_MBYTE
4346 int unicode = 0;
4347#endif
4348
4349 if (got_int)
4350 return Ctrl_C;
4351
4352#ifdef FEAT_GUI
4353 /*
4354 * In GUI there is no point inserting the internal code for a special key.
4355 * It is more useful to insert the string "<KEY>" instead. This would
4356 * probably be useful in a text window too, but it would not be
4357 * vi-compatible (maybe there should be an option for it?) -- webb
4358 */
4359 if (gui.in_use)
4360 ++allow_keys;
4361#endif
4362#ifdef USE_ON_FLY_SCROLL
4363 dont_scroll = TRUE; /* disallow scrolling here */
4364#endif
4365 ++no_mapping; /* don't map the next key hits */
4366 cc = 0;
4367 i = 0;
4368 for (;;)
4369 {
4370 do
4371 nc = safe_vgetc();
4372 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4373 || nc == K_HOR_SCROLLBAR);
4374#ifdef FEAT_CMDL_INFO
4375 if (!(State & CMDLINE)
4376# ifdef FEAT_MBYTE
4377 && MB_BYTE2LEN_CHECK(nc) == 1
4378# endif
4379 )
4380 add_to_showcmd(nc);
4381#endif
4382 if (nc == 'x' || nc == 'X')
4383 hex = TRUE;
4384 else if (nc == 'o' || nc == 'O')
4385 octal = TRUE;
4386#ifdef FEAT_MBYTE
4387 else if (nc == 'u' || nc == 'U')
4388 unicode = nc;
4389#endif
4390 else
4391 {
4392 if (hex
4393#ifdef FEAT_MBYTE
4394 || unicode != 0
4395#endif
4396 )
4397 {
4398 if (!vim_isxdigit(nc))
4399 break;
4400 cc = cc * 16 + hex2nr(nc);
4401 }
4402 else if (octal)
4403 {
4404 if (nc < '0' || nc > '7')
4405 break;
4406 cc = cc * 8 + nc - '0';
4407 }
4408 else
4409 {
4410 if (!VIM_ISDIGIT(nc))
4411 break;
4412 cc = cc * 10 + nc - '0';
4413 }
4414
4415 ++i;
4416 }
4417
4418 if (cc > 255
4419#ifdef FEAT_MBYTE
4420 && unicode == 0
4421#endif
4422 )
4423 cc = 255; /* limit range to 0-255 */
4424 nc = 0;
4425
4426 if (hex) /* hex: up to two chars */
4427 {
4428 if (i >= 2)
4429 break;
4430 }
4431#ifdef FEAT_MBYTE
4432 else if (unicode) /* Unicode: up to four or eight chars */
4433 {
4434 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4435 break;
4436 }
4437#endif
4438 else if (i >= 3) /* decimal or octal: up to three chars */
4439 break;
4440 }
4441 if (i == 0) /* no number entered */
4442 {
4443 if (nc == K_ZERO) /* NUL is stored as NL */
4444 {
4445 cc = '\n';
4446 nc = 0;
4447 }
4448 else
4449 {
4450 cc = nc;
4451 nc = 0;
4452 }
4453 }
4454
4455 if (cc == 0) /* NUL is stored as NL */
4456 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004457#ifdef FEAT_MBYTE
4458 if (enc_dbcs && (cc & 0xff) == 0)
4459 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4460 second byte will cause trouble! */
4461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
4463 --no_mapping;
4464#ifdef FEAT_GUI
4465 if (gui.in_use)
4466 --allow_keys;
4467#endif
4468 if (nc)
4469 vungetc(nc);
4470 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4471 return cc;
4472}
4473
4474/*
4475 * Insert character, taking care of special keys and mod_mask
4476 */
4477 static void
4478insert_special(c, allow_modmask, ctrlv)
4479 int c;
4480 int allow_modmask;
4481 int ctrlv; /* c was typed after CTRL-V */
4482{
4483 char_u *p;
4484 int len;
4485
4486 /*
4487 * Special function key, translate into "<Key>". Up to the last '>' is
4488 * inserted with ins_str(), so as not to replace characters in replace
4489 * mode.
4490 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4491 * unless 'allow_modmask' is TRUE.
4492 */
4493#ifdef MACOS
4494 /* Command-key never produces a normal key */
4495 if (mod_mask & MOD_MASK_CMD)
4496 allow_modmask = TRUE;
4497#endif
4498 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4499 {
4500 p = get_special_key_name(c, mod_mask);
4501 len = (int)STRLEN(p);
4502 c = p[len - 1];
4503 if (len > 2)
4504 {
4505 if (stop_arrow() == FAIL)
4506 return;
4507 p[len - 1] = NUL;
4508 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004509 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 ctrlv = FALSE;
4511 }
4512 }
4513 if (stop_arrow() == OK)
4514 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4515}
4516
4517/*
4518 * Special characters in this context are those that need processing other
4519 * than the simple insertion that can be performed here. This includes ESC
4520 * which terminates the insert, and CR/NL which need special processing to
4521 * open up a new line. This routine tries to optimize insertions performed by
4522 * the "redo", "undo" or "put" commands, so it needs to know when it should
4523 * stop and defer processing to the "normal" mechanism.
4524 * '0' and '^' are special, because they can be followed by CTRL-D.
4525 */
4526#ifdef EBCDIC
4527# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4528#else
4529# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4530#endif
4531
4532#ifdef FEAT_MBYTE
4533# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4534#else
4535# define WHITECHAR(cc) vim_iswhite(cc)
4536#endif
4537
4538 void
4539insertchar(c, flags, second_indent)
4540 int c; /* character to insert or NUL */
4541 int flags; /* INSCHAR_FORMAT, etc. */
4542 int second_indent; /* indent for second line if >= 0 */
4543{
4544 int haveto_redraw = FALSE;
4545 int textwidth;
4546#ifdef FEAT_COMMENTS
4547 colnr_T leader_len;
4548 char_u *p;
4549 int no_leader = FALSE;
4550 int do_comments = (flags & INSCHAR_DO_COM);
4551#endif
4552 int fo_white_par;
4553 int first_line = TRUE;
4554 int fo_ins_blank;
4555#ifdef FEAT_MBYTE
4556 int fo_multibyte;
4557#endif
4558 int save_char = NUL;
4559 int cc;
4560
4561 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4562 fo_ins_blank = has_format_option(FO_INS_BLANK);
4563#ifdef FEAT_MBYTE
4564 fo_multibyte = has_format_option(FO_MBYTE_BREAK);
4565#endif
4566 fo_white_par = has_format_option(FO_WHITE_PAR);
4567
4568 /*
4569 * Try to break the line in two or more pieces when:
4570 * - Always do this if we have been called to do formatting only.
4571 * - Always do this when 'formatoptions' has the 'a' flag and the line
4572 * ends in white space.
4573 * - Otherwise:
4574 * - Don't do this if inserting a blank
4575 * - Don't do this if an existing character is being replaced, unless
4576 * we're in VREPLACE mode.
4577 * - Do this if the cursor is not on the line where insert started
4578 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4579 * before the insert.
4580 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4581 * before 'textwidth'
4582 */
4583 if (textwidth
4584 && ((flags & INSCHAR_FORMAT)
4585 || (!vim_iswhite(c)
4586 && !((State & REPLACE_FLAG)
4587#ifdef FEAT_VREPLACE
4588 && !(State & VREPLACE_FLAG)
4589#endif
4590 && *ml_get_cursor() != NUL)
4591 && (curwin->w_cursor.lnum != Insstart.lnum
4592 || ((!has_format_option(FO_INS_LONG)
4593 || Insstart_textlen <= (colnr_T)textwidth)
4594 && (!fo_ins_blank
4595 || Insstart_blank_vcol <= (colnr_T)textwidth
4596 ))))))
4597 {
4598 /*
4599 * When 'ai' is off we don't want a space under the cursor to be
4600 * deleted. Replace it with an 'x' temporarily.
4601 */
4602 if (!curbuf->b_p_ai)
4603 {
4604 cc = gchar_cursor();
4605 if (vim_iswhite(cc))
4606 {
4607 save_char = cc;
4608 pchar_cursor('x');
4609 }
4610 }
4611
4612 /*
4613 * Repeat breaking lines, until the current line is not too long.
4614 */
4615 while (!got_int)
4616 {
4617 int startcol; /* Cursor column at entry */
4618 int wantcol; /* column at textwidth border */
4619 int foundcol; /* column for start of spaces */
4620 int end_foundcol = 0; /* column for start of word */
4621 colnr_T len;
4622 colnr_T virtcol;
4623#ifdef FEAT_VREPLACE
4624 int orig_col = 0;
4625 char_u *saved_text = NULL;
4626#endif
4627 colnr_T col;
4628
4629 virtcol = get_nolist_virtcol();
4630 if (virtcol < (colnr_T)textwidth)
4631 break;
4632
4633#ifdef FEAT_COMMENTS
4634 if (no_leader)
4635 do_comments = FALSE;
4636 else if (!(flags & INSCHAR_FORMAT)
4637 && has_format_option(FO_WRAP_COMS))
4638 do_comments = TRUE;
4639
4640 /* Don't break until after the comment leader */
4641 if (do_comments)
4642 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
4643 else
4644 leader_len = 0;
4645
4646 /* If the line doesn't start with a comment leader, then don't
4647 * start one in a following broken line. Avoids that a %word
4648 * moved to the start of the next line causes all following lines
4649 * to start with %. */
4650 if (leader_len == 0)
4651 no_leader = TRUE;
4652#endif
4653 if (!(flags & INSCHAR_FORMAT)
4654#ifdef FEAT_COMMENTS
4655 && leader_len == 0
4656#endif
4657 && !has_format_option(FO_WRAP))
4658
4659 {
4660 textwidth = 0;
4661 break;
4662 }
4663 if ((startcol = curwin->w_cursor.col) == 0)
4664 break;
4665
4666 /* find column of textwidth border */
4667 coladvance((colnr_T)textwidth);
4668 wantcol = curwin->w_cursor.col;
4669
4670 curwin->w_cursor.col = startcol - 1;
4671#ifdef FEAT_MBYTE
4672 /* Correct cursor for multi-byte character. */
4673 if (has_mbyte)
4674 mb_adjust_cursor();
4675#endif
4676 foundcol = 0;
4677
4678 /*
4679 * Find position to break at.
4680 * Stop at first entered white when 'formatoptions' has 'v'
4681 */
4682 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
4683 || curwin->w_cursor.lnum != Insstart.lnum
4684 || curwin->w_cursor.col >= Insstart.col)
4685 {
4686 cc = gchar_cursor();
4687 if (WHITECHAR(cc))
4688 {
4689 /* remember position of blank just before text */
4690 end_foundcol = curwin->w_cursor.col;
4691
4692 /* find start of sequence of blanks */
4693 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
4694 {
4695 dec_cursor();
4696 cc = gchar_cursor();
4697 }
4698 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
4699 break; /* only spaces in front of text */
4700#ifdef FEAT_COMMENTS
4701 /* Don't break until after the comment leader */
4702 if (curwin->w_cursor.col < leader_len)
4703 break;
4704#endif
4705 if (has_format_option(FO_ONE_LETTER))
4706 {
4707 /* do not break after one-letter words */
4708 if (curwin->w_cursor.col == 0)
4709 break; /* one-letter word at begin */
4710
4711 col = curwin->w_cursor.col;
4712 dec_cursor();
4713 cc = gchar_cursor();
4714
4715 if (WHITECHAR(cc))
4716 continue; /* one-letter, continue */
4717 curwin->w_cursor.col = col;
4718 }
4719#ifdef FEAT_MBYTE
4720 if (has_mbyte)
4721 foundcol = curwin->w_cursor.col
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004722 + (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 else
4724#endif
4725 foundcol = curwin->w_cursor.col + 1;
4726 if (curwin->w_cursor.col < (colnr_T)wantcol)
4727 break;
4728 }
4729#ifdef FEAT_MBYTE
4730 else if (cc >= 0x100 && fo_multibyte
4731 && curwin->w_cursor.col <= (colnr_T)wantcol)
4732 {
4733 /* Break after or before a multi-byte character. */
4734 foundcol = curwin->w_cursor.col;
4735 if (curwin->w_cursor.col < (colnr_T)wantcol)
4736 foundcol += (*mb_char2len)(cc);
4737 end_foundcol = foundcol;
4738 break;
4739 }
4740#endif
4741 if (curwin->w_cursor.col == 0)
4742 break;
4743 dec_cursor();
4744 }
4745
4746 if (foundcol == 0) /* no spaces, cannot break line */
4747 {
4748 curwin->w_cursor.col = startcol;
4749 break;
4750 }
4751
4752 /* Going to break the line, remove any "$" now. */
4753 undisplay_dollar();
4754
4755 /*
4756 * Offset between cursor position and line break is used by replace
4757 * stack functions. VREPLACE does not use this, and backspaces
4758 * over the text instead.
4759 */
4760#ifdef FEAT_VREPLACE
4761 if (State & VREPLACE_FLAG)
4762 orig_col = startcol; /* Will start backspacing from here */
4763 else
4764#endif
4765 replace_offset = startcol - end_foundcol - 1;
4766
4767 /*
4768 * adjust startcol for spaces that will be deleted and
4769 * characters that will remain on top line
4770 */
4771 curwin->w_cursor.col = foundcol;
4772 while (cc = gchar_cursor(), WHITECHAR(cc))
4773 inc_cursor();
4774 startcol -= curwin->w_cursor.col;
4775 if (startcol < 0)
4776 startcol = 0;
4777
4778#ifdef FEAT_VREPLACE
4779 if (State & VREPLACE_FLAG)
4780 {
4781 /*
4782 * In VREPLACE mode, we will backspace over the text to be
4783 * wrapped, so save a copy now to put on the next line.
4784 */
4785 saved_text = vim_strsave(ml_get_cursor());
4786 curwin->w_cursor.col = orig_col;
4787 if (saved_text == NULL)
4788 break; /* Can't do it, out of memory */
4789 saved_text[startcol] = NUL;
4790
4791 /* Backspace over characters that will move to the next line */
4792 if (!fo_white_par)
4793 backspace_until_column(foundcol);
4794 }
4795 else
4796#endif
4797 {
4798 /* put cursor after pos. to break line */
4799 if (!fo_white_par)
4800 curwin->w_cursor.col = foundcol;
4801 }
4802
4803 /*
4804 * Split the line just before the margin.
4805 * Only insert/delete lines, but don't really redraw the window.
4806 */
4807 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
4808 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
4809#ifdef FEAT_COMMENTS
4810 + (do_comments ? OPENLINE_DO_COM : 0)
4811#endif
4812 , old_indent);
4813 old_indent = 0;
4814
4815 replace_offset = 0;
4816 if (first_line)
4817 {
4818 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
4819 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
4820 if (second_indent >= 0)
4821 {
4822#ifdef FEAT_VREPLACE
4823 if (State & VREPLACE_FLAG)
4824 change_indent(INDENT_SET, second_indent, FALSE, NUL);
4825 else
4826#endif
4827 (void)set_indent(second_indent, SIN_CHANGED);
4828 }
4829 first_line = FALSE;
4830 }
4831
4832#ifdef FEAT_VREPLACE
4833 if (State & VREPLACE_FLAG)
4834 {
4835 /*
4836 * In VREPLACE mode we have backspaced over the text to be
4837 * moved, now we re-insert it into the new line.
4838 */
4839 ins_bytes(saved_text);
4840 vim_free(saved_text);
4841 }
4842 else
4843#endif
4844 {
4845 /*
4846 * Check if cursor is not past the NUL off the line, cindent
4847 * may have added or removed indent.
4848 */
4849 curwin->w_cursor.col += startcol;
4850 len = (colnr_T)STRLEN(ml_get_curline());
4851 if (curwin->w_cursor.col > len)
4852 curwin->w_cursor.col = len;
4853 }
4854
4855 haveto_redraw = TRUE;
4856#ifdef FEAT_CINDENT
4857 can_cindent = TRUE;
4858#endif
4859 /* moved the cursor, don't autoindent or cindent now */
4860 did_ai = FALSE;
4861#ifdef FEAT_SMARTINDENT
4862 did_si = FALSE;
4863 can_si = FALSE;
4864 can_si_back = FALSE;
4865#endif
4866 line_breakcheck();
4867 }
4868
4869 if (save_char) /* put back space after cursor */
4870 pchar_cursor(save_char);
4871
4872 if (c == NUL) /* formatting only */
4873 return;
4874 if (haveto_redraw)
4875 {
4876 update_topline();
4877 redraw_curbuf_later(VALID);
4878 }
4879 }
4880 if (c == NUL) /* only formatting was wanted */
4881 return;
4882
4883#ifdef FEAT_COMMENTS
4884 /* Check whether this character should end a comment. */
4885 if (did_ai && (int)c == end_comment_pending)
4886 {
4887 char_u *line;
4888 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4889 int middle_len, end_len;
4890 int i;
4891
4892 /*
4893 * Need to remove existing (middle) comment leader and insert end
4894 * comment leader. First, check what comment leader we can find.
4895 */
4896 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4897 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4898 {
4899 /* Skip middle-comment string */
4900 while (*p && p[-1] != ':') /* find end of middle flags */
4901 ++p;
4902 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4903 /* Don't count trailing white space for middle_len */
4904 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4905 --middle_len;
4906
4907 /* Find the end-comment string */
4908 while (*p && p[-1] != ':') /* find end of end flags */
4909 ++p;
4910 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4911
4912 /* Skip white space before the cursor */
4913 i = curwin->w_cursor.col;
4914 while (--i >= 0 && vim_iswhite(line[i]))
4915 ;
4916 i++;
4917
4918 /* Skip to before the middle leader */
4919 i -= middle_len;
4920
4921 /* Check some expected things before we go on */
4922 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4923 {
4924 /* Backspace over all the stuff we want to replace */
4925 backspace_until_column(i);
4926
4927 /*
4928 * Insert the end-comment string, except for the last
4929 * character, which will get inserted as normal later.
4930 */
4931 ins_bytes_len(lead_end, end_len - 1);
4932 }
4933 }
4934 }
4935 end_comment_pending = NUL;
4936#endif
4937
4938 did_ai = FALSE;
4939#ifdef FEAT_SMARTINDENT
4940 did_si = FALSE;
4941 can_si = FALSE;
4942 can_si_back = FALSE;
4943#endif
4944
4945 /*
4946 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4947 * This speeds up normal text input considerably.
4948 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4949 * need to re-indent at a ':', or any other character (but not what
4950 * 'paste' is set)..
4951 */
4952#ifdef USE_ON_FLY_SCROLL
4953 dont_scroll = FALSE; /* allow scrolling here */
4954#endif
4955
4956 if ( !ISSPECIAL(c)
4957#ifdef FEAT_MBYTE
4958 && (!has_mbyte || (*mb_char2len)(c) == 1)
4959#endif
4960 && vpeekc() != NUL
4961 && !(State & REPLACE_FLAG)
4962#ifdef FEAT_CINDENT
4963 && !cindent_on()
4964#endif
4965#ifdef FEAT_RIGHTLEFT
4966 && !p_ri
4967#endif
4968 )
4969 {
4970#define INPUT_BUFLEN 100
4971 char_u buf[INPUT_BUFLEN + 1];
4972 int i;
4973 colnr_T virtcol = 0;
4974
4975 buf[0] = c;
4976 i = 1;
4977 if (textwidth)
4978 virtcol = get_nolist_virtcol();
4979 /*
4980 * Stop the string when:
4981 * - no more chars available
4982 * - finding a special character (command key)
4983 * - buffer is full
4984 * - running into the 'textwidth' boundary
4985 * - need to check for abbreviation: A non-word char after a word-char
4986 */
4987 while ( (c = vpeekc()) != NUL
4988 && !ISSPECIAL(c)
4989#ifdef FEAT_MBYTE
4990 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4991#endif
4992 && i < INPUT_BUFLEN
4993 && (textwidth == 0
4994 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4995 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4996 {
4997#ifdef FEAT_RIGHTLEFT
4998 c = vgetc();
4999 if (p_hkmap && KeyTyped)
5000 c = hkmap(c); /* Hebrew mode mapping */
5001# ifdef FEAT_FKMAP
5002 if (p_fkmap && KeyTyped)
5003 c = fkmap(c); /* Farsi mode mapping */
5004# endif
5005 buf[i++] = c;
5006#else
5007 buf[i++] = vgetc();
5008#endif
5009 }
5010
5011#ifdef FEAT_DIGRAPHS
5012 do_digraph(-1); /* clear digraphs */
5013 do_digraph(buf[i-1]); /* may be the start of a digraph */
5014#endif
5015 buf[i] = NUL;
5016 ins_str(buf);
5017 if (flags & INSCHAR_CTRLV)
5018 {
5019 redo_literal(*buf);
5020 i = 1;
5021 }
5022 else
5023 i = 0;
5024 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005025 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
5027 else
5028 {
5029#ifdef FEAT_MBYTE
5030 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5031 {
5032 char_u buf[MB_MAXBYTES + 1];
5033
5034 (*mb_char2bytes)(c, buf);
5035 buf[cc] = NUL;
5036 ins_char_bytes(buf, cc);
5037 AppendCharToRedobuff(c);
5038 }
5039 else
5040#endif
5041 {
5042 ins_char(c);
5043 if (flags & INSCHAR_CTRLV)
5044 redo_literal(c);
5045 else
5046 AppendCharToRedobuff(c);
5047 }
5048 }
5049}
5050
5051/*
5052 * Called after inserting or deleting text: When 'formatoptions' includes the
5053 * 'a' flag format from the current line until the end of the paragraph.
5054 * Keep the cursor at the same position relative to the text.
5055 * The caller must have saved the cursor line for undo, following ones will be
5056 * saved here.
5057 */
5058 void
5059auto_format(trailblank, prev_line)
5060 int trailblank; /* when TRUE also format with trailing blank */
5061 int prev_line; /* may start in previous line */
5062{
5063 pos_T pos;
5064 colnr_T len;
5065 char_u *old;
5066 char_u *new, *pnew;
5067 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005068 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070 if (!has_format_option(FO_AUTO))
5071 return;
5072
5073 pos = curwin->w_cursor;
5074 old = ml_get_curline();
5075
5076 /* may remove added space */
5077 check_auto_format(FALSE);
5078
5079 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5080 * user might insert normal text next. Also skip formatting when "1" is
5081 * in 'formatoptions' and there is a single character before the cursor.
5082 * Otherwise the line would be broken and when typing another non-white
5083 * next they are not joined back together. */
5084 wasatend = (pos.col == STRLEN(old));
5085 if (*old != NUL && !trailblank && wasatend)
5086 {
5087 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005088 cc = gchar_cursor();
5089 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5090 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005092 cc = gchar_cursor();
5093 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
5095 curwin->w_cursor = pos;
5096 return;
5097 }
5098 curwin->w_cursor = pos;
5099 }
5100
5101#ifdef FEAT_COMMENTS
5102 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5103 * comments. */
5104 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5105 && get_leader_len(old, NULL, FALSE) == 0)
5106 return;
5107#endif
5108
5109 /*
5110 * May start formatting in a previous line, so that after "x" a word is
5111 * moved to the previous line if it fits there now. Only when this is not
5112 * the start of a paragraph.
5113 */
5114 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5115 {
5116 --curwin->w_cursor.lnum;
5117 if (u_save_cursor() == FAIL)
5118 return;
5119 }
5120
5121 /*
5122 * Do the formatting and restore the cursor position. "saved_cursor" will
5123 * be adjusted for the text formatting.
5124 */
5125 saved_cursor = pos;
5126 format_lines((linenr_T)-1);
5127 curwin->w_cursor = saved_cursor;
5128 saved_cursor.lnum = 0;
5129
5130 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5131 {
5132 /* "cannot happen" */
5133 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5134 coladvance((colnr_T)MAXCOL);
5135 }
5136 else
5137 check_cursor_col();
5138
5139 /* Insert mode: If the cursor is now after the end of the line while it
5140 * previously wasn't, the line was broken. Because of the rule above we
5141 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5142 * formatted. */
5143 if (!wasatend && has_format_option(FO_WHITE_PAR))
5144 {
5145 new = ml_get_curline();
5146 len = STRLEN(new);
5147 if (curwin->w_cursor.col == len)
5148 {
5149 pnew = vim_strnsave(new, len + 2);
5150 pnew[len] = ' ';
5151 pnew[len + 1] = NUL;
5152 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5153 /* remove the space later */
5154 did_add_space = TRUE;
5155 }
5156 else
5157 /* may remove added space */
5158 check_auto_format(FALSE);
5159 }
5160
5161 check_cursor();
5162}
5163
5164/*
5165 * When an extra space was added to continue a paragraph for auto-formatting,
5166 * delete it now. The space must be under the cursor, just after the insert
5167 * position.
5168 */
5169 static void
5170check_auto_format(end_insert)
5171 int end_insert; /* TRUE when ending Insert mode */
5172{
5173 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005174 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175
5176 if (did_add_space)
5177 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005178 cc = gchar_cursor();
5179 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 /* Somehow the space was removed already. */
5181 did_add_space = FALSE;
5182 else
5183 {
5184 if (!end_insert)
5185 {
5186 inc_cursor();
5187 c = gchar_cursor();
5188 dec_cursor();
5189 }
5190 if (c != NUL)
5191 {
5192 /* The space is no longer at the end of the line, delete it. */
5193 del_char(FALSE);
5194 did_add_space = FALSE;
5195 }
5196 }
5197 }
5198}
5199
5200/*
5201 * Find out textwidth to be used for formatting:
5202 * if 'textwidth' option is set, use it
5203 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5204 * if invalid value, use 0.
5205 * Set default to window width (maximum 79) for "gq" operator.
5206 */
5207 int
5208comp_textwidth(ff)
5209 int ff; /* force formatting (for "Q" command) */
5210{
5211 int textwidth;
5212
5213 textwidth = curbuf->b_p_tw;
5214 if (textwidth == 0 && curbuf->b_p_wm)
5215 {
5216 /* The width is the window width minus 'wrapmargin' minus all the
5217 * things that add to the margin. */
5218 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5219#ifdef FEAT_CMDWIN
5220 if (cmdwin_type != 0)
5221 textwidth -= 1;
5222#endif
5223#ifdef FEAT_FOLDING
5224 textwidth -= curwin->w_p_fdc;
5225#endif
5226#ifdef FEAT_SIGNS
5227 if (curwin->w_buffer->b_signlist != NULL
5228# ifdef FEAT_NETBEANS_INTG
5229 || usingNetbeans
5230# endif
5231 )
5232 textwidth -= 1;
5233#endif
5234 if (curwin->w_p_nu)
5235 textwidth -= 8;
5236 }
5237 if (textwidth < 0)
5238 textwidth = 0;
5239 if (ff && textwidth == 0)
5240 {
5241 textwidth = W_WIDTH(curwin) - 1;
5242 if (textwidth > 79)
5243 textwidth = 79;
5244 }
5245 return textwidth;
5246}
5247
5248/*
5249 * Put a character in the redo buffer, for when just after a CTRL-V.
5250 */
5251 static void
5252redo_literal(c)
5253 int c;
5254{
5255 char_u buf[10];
5256
5257 /* Only digits need special treatment. Translate them into a string of
5258 * three digits. */
5259 if (VIM_ISDIGIT(c))
5260 {
5261 sprintf((char *)buf, "%03d", c);
5262 AppendToRedobuff(buf);
5263 }
5264 else
5265 AppendCharToRedobuff(c);
5266}
5267
5268/*
5269 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005270 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 */
5272 static void
5273start_arrow(end_insert_pos)
5274 pos_T *end_insert_pos;
5275{
5276 if (!arrow_used) /* something has been inserted */
5277 {
5278 AppendToRedobuff(ESC_STR);
5279 stop_insert(end_insert_pos, FALSE);
5280 arrow_used = TRUE; /* this means we stopped the current insert */
5281 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005282#ifdef FEAT_SYN_HL
5283 check_spell_redraw();
5284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285}
5286
Bram Moolenaar217ad922005-03-20 22:37:15 +00005287#ifdef FEAT_SYN_HL
5288/*
5289 * If we skipped highlighting word at cursor, do it now.
5290 * It may be skipped again, thus reset spell_redraw_lnum first.
5291 */
5292 static void
5293check_spell_redraw()
5294{
5295 if (spell_redraw_lnum != 0)
5296 {
5297 linenr_T lnum = spell_redraw_lnum;
5298
5299 spell_redraw_lnum = 0;
5300 redrawWinline(lnum, FALSE);
5301 }
5302}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005303
5304/*
5305 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5306 * spelled word, if there is one.
5307 */
5308 static void
5309spell_back_to_badword()
5310{
5311 pos_T tpos = curwin->w_cursor;
5312
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005313 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005314 if (curwin->w_cursor.col != tpos.col)
5315 start_arrow(&tpos);
5316}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005317#endif
5318
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319/*
5320 * stop_arrow() is called before a change is made in insert mode.
5321 * If an arrow key has been used, start a new insertion.
5322 * Returns FAIL if undo is impossible, shouldn't insert then.
5323 */
5324 int
5325stop_arrow()
5326{
5327 if (arrow_used)
5328 {
5329 if (u_save_cursor() == OK)
5330 {
5331 arrow_used = FALSE;
5332 ins_need_undo = FALSE;
5333 }
5334 Insstart = curwin->w_cursor; /* new insertion starts here */
5335 Insstart_textlen = linetabsize(ml_get_curline());
5336 ai_col = 0;
5337#ifdef FEAT_VREPLACE
5338 if (State & VREPLACE_FLAG)
5339 {
5340 orig_line_count = curbuf->b_ml.ml_line_count;
5341 vr_lines_changed = 1;
5342 }
5343#endif
5344 ResetRedobuff();
5345 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005346 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
5348 else if (ins_need_undo)
5349 {
5350 if (u_save_cursor() == OK)
5351 ins_need_undo = FALSE;
5352 }
5353
5354#ifdef FEAT_FOLDING
5355 /* Always open fold at the cursor line when inserting something. */
5356 foldOpenCursor();
5357#endif
5358
5359 return (arrow_used || ins_need_undo ? FAIL : OK);
5360}
5361
5362/*
5363 * do a few things to stop inserting
5364 */
5365 static void
5366stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005367 pos_T *end_insert_pos; /* where insert ended */
5368 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005370 int cc;
5371 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
5373 stop_redo_ins();
5374 replace_flush(); /* abandon replace stack */
5375
5376 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005377 * Save the inserted text for later redo with ^@ and CTRL-A.
5378 * Don't do it when "restart_edit" was set and nothing was inserted,
5379 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005381 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005382 if (did_restart_edit == 0 || (ptr != NULL
5383 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005384 {
5385 vim_free(last_insert);
5386 last_insert = ptr;
5387 last_insert_skip = new_insert_skip;
5388 }
5389 else
5390 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
5392 if (!arrow_used)
5393 {
5394 /* Auto-format now. It may seem strange to do this when stopping an
5395 * insertion (or moving the cursor), but it's required when appending
5396 * a line and having it end in a space. But only do it when something
5397 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005398 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005400 pos_T tpos = curwin->w_cursor;
5401
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 /* When the cursor is at the end of the line after a space the
5403 * formatting will move it to the following word. Avoid that by
5404 * moving the cursor onto the space. */
5405 cc = 'x';
5406 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5407 {
5408 dec_cursor();
5409 cc = gchar_cursor();
5410 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005411 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 }
5413
5414 auto_format(TRUE, FALSE);
5415
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005416 if (vim_iswhite(cc))
5417 {
5418 if (gchar_cursor() != NUL)
5419 inc_cursor();
5420#ifdef FEAT_VIRTUALEDIT
5421 /* If the cursor is still at the same character, also keep
5422 * the "coladd". */
5423 if (gchar_cursor() == NUL
5424 && curwin->w_cursor.lnum == tpos.lnum
5425 && curwin->w_cursor.col == tpos.col)
5426 curwin->w_cursor.coladd = tpos.coladd;
5427#endif
5428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 }
5430
5431 /* If a space was inserted for auto-formatting, remove it now. */
5432 check_auto_format(TRUE);
5433
5434 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005435 * of the line, and put the cursor back.
5436 * Do this when ESC was used or moving the cursor up/down. */
5437 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5438 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005440 pos_T tpos = curwin->w_cursor;
5441
5442 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5444 --curwin->w_cursor.col;
5445 while (cc = gchar_cursor(), vim_iswhite(cc))
5446 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005447 if (curwin->w_cursor.lnum != tpos.lnum)
5448 curwin->w_cursor = tpos;
5449 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5451
5452#ifdef FEAT_VISUAL
5453 /* <C-S-Right> may have started Visual mode, adjust the position for
5454 * deleted characters. */
5455 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5456 {
5457 cc = STRLEN(ml_get_curline());
5458 if (VIsual.col > (colnr_T)cc)
5459 {
5460 VIsual.col = cc;
5461# ifdef FEAT_VIRTUALEDIT
5462 VIsual.coladd = 0;
5463# endif
5464 }
5465 }
5466#endif
5467 }
5468 }
5469 did_ai = FALSE;
5470#ifdef FEAT_SMARTINDENT
5471 did_si = FALSE;
5472 can_si = FALSE;
5473 can_si_back = FALSE;
5474#endif
5475
5476 /* set '[ and '] to the inserted text */
5477 curbuf->b_op_start = Insstart;
5478 curbuf->b_op_end = *end_insert_pos;
5479}
5480
5481/*
5482 * Set the last inserted text to a single character.
5483 * Used for the replace command.
5484 */
5485 void
5486set_last_insert(c)
5487 int c;
5488{
5489 char_u *s;
5490
5491 vim_free(last_insert);
5492#ifdef FEAT_MBYTE
5493 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5494#else
5495 last_insert = alloc(6);
5496#endif
5497 if (last_insert != NULL)
5498 {
5499 s = last_insert;
5500 /* Use the CTRL-V only when entering a special char */
5501 if (c < ' ' || c == DEL)
5502 *s++ = Ctrl_V;
5503 s = add_char2buf(c, s);
5504 *s++ = ESC;
5505 *s++ = NUL;
5506 last_insert_skip = 0;
5507 }
5508}
5509
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005510#if defined(EXITFREE) || defined(PROTO)
5511 void
5512free_last_insert()
5513{
5514 vim_free(last_insert);
5515 last_insert = NULL;
5516}
5517#endif
5518
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519/*
5520 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5521 * and CSI. Handle multi-byte characters.
5522 * Returns a pointer to after the added bytes.
5523 */
5524 char_u *
5525add_char2buf(c, s)
5526 int c;
5527 char_u *s;
5528{
5529#ifdef FEAT_MBYTE
5530 char_u temp[MB_MAXBYTES];
5531 int i;
5532 int len;
5533
5534 len = (*mb_char2bytes)(c, temp);
5535 for (i = 0; i < len; ++i)
5536 {
5537 c = temp[i];
5538#endif
5539 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5540 if (c == K_SPECIAL)
5541 {
5542 *s++ = K_SPECIAL;
5543 *s++ = KS_SPECIAL;
5544 *s++ = KE_FILLER;
5545 }
5546#ifdef FEAT_GUI
5547 else if (c == CSI)
5548 {
5549 *s++ = CSI;
5550 *s++ = KS_EXTRA;
5551 *s++ = (int)KE_CSI;
5552 }
5553#endif
5554 else
5555 *s++ = c;
5556#ifdef FEAT_MBYTE
5557 }
5558#endif
5559 return s;
5560}
5561
5562/*
5563 * move cursor to start of line
5564 * if flags & BL_WHITE move to first non-white
5565 * if flags & BL_SOL move to first non-white if startofline is set,
5566 * otherwise keep "curswant" column
5567 * if flags & BL_FIX don't leave the cursor on a NUL.
5568 */
5569 void
5570beginline(flags)
5571 int flags;
5572{
5573 if ((flags & BL_SOL) && !p_sol)
5574 coladvance(curwin->w_curswant);
5575 else
5576 {
5577 curwin->w_cursor.col = 0;
5578#ifdef FEAT_VIRTUALEDIT
5579 curwin->w_cursor.coladd = 0;
5580#endif
5581
5582 if (flags & (BL_WHITE | BL_SOL))
5583 {
5584 char_u *ptr;
5585
5586 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5587 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5588 ++curwin->w_cursor.col;
5589 }
5590 curwin->w_set_curswant = TRUE;
5591 }
5592}
5593
5594/*
5595 * oneright oneleft cursor_down cursor_up
5596 *
5597 * Move one char {right,left,down,up}.
5598 * Doesn't move onto the NUL past the end of the line.
5599 * Return OK when successful, FAIL when we hit a line of file boundary.
5600 */
5601
5602 int
5603oneright()
5604{
5605 char_u *ptr;
5606#ifdef FEAT_MBYTE
5607 int l;
5608#endif
5609
5610#ifdef FEAT_VIRTUALEDIT
5611 if (virtual_active())
5612 {
5613 pos_T prevpos = curwin->w_cursor;
5614
5615 /* Adjust for multi-wide char (excluding TAB) */
5616 ptr = ml_get_cursor();
5617 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5618#ifdef FEAT_MBYTE
5619 (*mb_ptr2char)(ptr)
5620#else
5621 *ptr
5622#endif
5623 ))
5624 ? ptr2cells(ptr) : 1));
5625 curwin->w_set_curswant = TRUE;
5626 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5627 return (prevpos.col != curwin->w_cursor.col
5628 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5629 }
5630#endif
5631
5632 ptr = ml_get_cursor();
5633#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005634 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 {
5636 /* The character under the cursor is a multi-byte character, move
5637 * several bytes right, but don't end up on the NUL. */
5638 if (ptr[l] == NUL)
5639 return FAIL;
5640 curwin->w_cursor.col += l;
5641 }
5642 else
5643#endif
5644 {
5645 if (*ptr++ == NUL || *ptr == NUL)
5646 return FAIL;
5647 ++curwin->w_cursor.col;
5648 }
5649
5650 curwin->w_set_curswant = TRUE;
5651 return OK;
5652}
5653
5654 int
5655oneleft()
5656{
5657#ifdef FEAT_VIRTUALEDIT
5658 if (virtual_active())
5659 {
5660 int width;
5661 int v = getviscol();
5662
5663 if (v == 0)
5664 return FAIL;
5665
5666# ifdef FEAT_LINEBREAK
5667 /* We might get stuck on 'showbreak', skip over it. */
5668 width = 1;
5669 for (;;)
5670 {
5671 coladvance(v - width);
5672 /* getviscol() is slow, skip it when 'showbreak' is empty and
5673 * there are no multi-byte characters */
5674 if ((*p_sbr == NUL
5675# ifdef FEAT_MBYTE
5676 && !has_mbyte
5677# endif
5678 ) || getviscol() < v)
5679 break;
5680 ++width;
5681 }
5682# else
5683 coladvance(v - 1);
5684# endif
5685
5686 if (curwin->w_cursor.coladd == 1)
5687 {
5688 char_u *ptr;
5689
5690 /* Adjust for multi-wide char (not a TAB) */
5691 ptr = ml_get_cursor();
5692 if (*ptr != TAB && vim_isprintc(
5693# ifdef FEAT_MBYTE
5694 (*mb_ptr2char)(ptr)
5695# else
5696 *ptr
5697# endif
5698 ) && ptr2cells(ptr) > 1)
5699 curwin->w_cursor.coladd = 0;
5700 }
5701
5702 curwin->w_set_curswant = TRUE;
5703 return OK;
5704 }
5705#endif
5706
5707 if (curwin->w_cursor.col == 0)
5708 return FAIL;
5709
5710 curwin->w_set_curswant = TRUE;
5711 --curwin->w_cursor.col;
5712
5713#ifdef FEAT_MBYTE
5714 /* if the character on the left of the current cursor is a multi-byte
5715 * character, move to its first byte */
5716 if (has_mbyte)
5717 mb_adjust_cursor();
5718#endif
5719 return OK;
5720}
5721
5722 int
5723cursor_up(n, upd_topline)
5724 long n;
5725 int upd_topline; /* When TRUE: update topline */
5726{
5727 linenr_T lnum;
5728
5729 if (n > 0)
5730 {
5731 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005732 /* This fails if the cursor is already in the first line or the count
5733 * is larger than the line number and '-' is in 'cpoptions' */
5734 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 return FAIL;
5736 if (n >= lnum)
5737 lnum = 1;
5738 else
5739#ifdef FEAT_FOLDING
5740 if (hasAnyFolding(curwin))
5741 {
5742 /*
5743 * Count each sequence of folded lines as one logical line.
5744 */
5745 /* go to the the start of the current fold */
5746 (void)hasFolding(lnum, &lnum, NULL);
5747
5748 while (n--)
5749 {
5750 /* move up one line */
5751 --lnum;
5752 if (lnum <= 1)
5753 break;
5754 /* If we entered a fold, move to the beginning, unless in
5755 * Insert mode or when 'foldopen' contains "all": it will open
5756 * in a moment. */
5757 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
5758 (void)hasFolding(lnum, &lnum, NULL);
5759 }
5760 if (lnum < 1)
5761 lnum = 1;
5762 }
5763 else
5764#endif
5765 lnum -= n;
5766 curwin->w_cursor.lnum = lnum;
5767 }
5768
5769 /* try to advance to the column we want to be at */
5770 coladvance(curwin->w_curswant);
5771
5772 if (upd_topline)
5773 update_topline(); /* make sure curwin->w_topline is valid */
5774
5775 return OK;
5776}
5777
5778/*
5779 * Cursor down a number of logical lines.
5780 */
5781 int
5782cursor_down(n, upd_topline)
5783 long n;
5784 int upd_topline; /* When TRUE: update topline */
5785{
5786 linenr_T lnum;
5787
5788 if (n > 0)
5789 {
5790 lnum = curwin->w_cursor.lnum;
5791#ifdef FEAT_FOLDING
5792 /* Move to last line of fold, will fail if it's the end-of-file. */
5793 (void)hasFolding(lnum, NULL, &lnum);
5794#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00005795 /* This fails if the cursor is already in the last line or would move
5796 * beyound the last line and '-' is in 'cpoptions' */
5797 if (lnum >= curbuf->b_ml.ml_line_count
5798 || (lnum + n > curbuf->b_ml.ml_line_count
5799 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 return FAIL;
5801 if (lnum + n >= curbuf->b_ml.ml_line_count)
5802 lnum = curbuf->b_ml.ml_line_count;
5803 else
5804#ifdef FEAT_FOLDING
5805 if (hasAnyFolding(curwin))
5806 {
5807 linenr_T last;
5808
5809 /* count each sequence of folded lines as one logical line */
5810 while (n--)
5811 {
5812 if (hasFolding(lnum, NULL, &last))
5813 lnum = last + 1;
5814 else
5815 ++lnum;
5816 if (lnum >= curbuf->b_ml.ml_line_count)
5817 break;
5818 }
5819 if (lnum > curbuf->b_ml.ml_line_count)
5820 lnum = curbuf->b_ml.ml_line_count;
5821 }
5822 else
5823#endif
5824 lnum += n;
5825 curwin->w_cursor.lnum = lnum;
5826 }
5827
5828 /* try to advance to the column we want to be at */
5829 coladvance(curwin->w_curswant);
5830
5831 if (upd_topline)
5832 update_topline(); /* make sure curwin->w_topline is valid */
5833
5834 return OK;
5835}
5836
5837/*
5838 * Stuff the last inserted text in the read buffer.
5839 * Last_insert actually is a copy of the redo buffer, so we
5840 * first have to remove the command.
5841 */
5842 int
5843stuff_inserted(c, count, no_esc)
5844 int c; /* Command character to be inserted */
5845 long count; /* Repeat this many times */
5846 int no_esc; /* Don't add an ESC at the end */
5847{
5848 char_u *esc_ptr;
5849 char_u *ptr;
5850 char_u *last_ptr;
5851 char_u last = NUL;
5852
5853 ptr = get_last_insert();
5854 if (ptr == NULL)
5855 {
5856 EMSG(_(e_noinstext));
5857 return FAIL;
5858 }
5859
5860 /* may want to stuff the command character, to start Insert mode */
5861 if (c != NUL)
5862 stuffcharReadbuff(c);
5863 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
5864 *esc_ptr = NUL; /* remove the ESC */
5865
5866 /* when the last char is either "0" or "^" it will be quoted if no ESC
5867 * comes after it OR if it will inserted more than once and "ptr"
5868 * starts with ^D. -- Acevedo
5869 */
5870 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
5871 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
5872 && (no_esc || (*ptr == Ctrl_D && count > 1)))
5873 {
5874 last = *last_ptr;
5875 *last_ptr = NUL;
5876 }
5877
5878 do
5879 {
5880 stuffReadbuff(ptr);
5881 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
5882 if (last)
5883 stuffReadbuff((char_u *)(last == '0'
5884 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
5885 : IF_EB("\026^", CTRL_V_STR "^")));
5886 }
5887 while (--count > 0);
5888
5889 if (last)
5890 *last_ptr = last;
5891
5892 if (esc_ptr != NULL)
5893 *esc_ptr = ESC; /* put the ESC back */
5894
5895 /* may want to stuff a trailing ESC, to get out of Insert mode */
5896 if (!no_esc)
5897 stuffcharReadbuff(ESC);
5898
5899 return OK;
5900}
5901
5902 char_u *
5903get_last_insert()
5904{
5905 if (last_insert == NULL)
5906 return NULL;
5907 return last_insert + last_insert_skip;
5908}
5909
5910/*
5911 * Get last inserted string, and remove trailing <Esc>.
5912 * Returns pointer to allocated memory (must be freed) or NULL.
5913 */
5914 char_u *
5915get_last_insert_save()
5916{
5917 char_u *s;
5918 int len;
5919
5920 if (last_insert == NULL)
5921 return NULL;
5922 s = vim_strsave(last_insert + last_insert_skip);
5923 if (s != NULL)
5924 {
5925 len = (int)STRLEN(s);
5926 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
5927 s[len - 1] = NUL;
5928 }
5929 return s;
5930}
5931
5932/*
5933 * Check the word in front of the cursor for an abbreviation.
5934 * Called when the non-id character "c" has been entered.
5935 * When an abbreviation is recognized it is removed from the text and
5936 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
5937 */
5938 static int
5939echeck_abbr(c)
5940 int c;
5941{
5942 /* Don't check for abbreviation in paste mode, when disabled and just
5943 * after moving around with cursor keys. */
5944 if (p_paste || no_abbr || arrow_used)
5945 return FALSE;
5946
5947 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
5948 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
5949}
5950
5951/*
5952 * replace-stack functions
5953 *
5954 * When replacing characters, the replaced characters are remembered for each
5955 * new character. This is used to re-insert the old text when backspacing.
5956 *
5957 * There is a NUL headed list of characters for each character that is
5958 * currently in the file after the insertion point. When BS is used, one NUL
5959 * headed list is put back for the deleted character.
5960 *
5961 * For a newline, there are two NUL headed lists. One contains the characters
5962 * that the NL replaced. The extra one stores the characters after the cursor
5963 * that were deleted (always white space).
5964 *
5965 * Replace_offset is normally 0, in which case replace_push will add a new
5966 * character at the end of the stack. If replace_offset is not 0, that many
5967 * characters will be left on the stack above the newly inserted character.
5968 */
5969
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00005970static char_u *replace_stack = NULL;
5971static long replace_stack_nr = 0; /* next entry in replace stack */
5972static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973
5974 void
5975replace_push(c)
5976 int c; /* character that is replaced (NUL is none) */
5977{
5978 char_u *p;
5979
5980 if (replace_stack_nr < replace_offset) /* nothing to do */
5981 return;
5982 if (replace_stack_len <= replace_stack_nr)
5983 {
5984 replace_stack_len += 50;
5985 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
5986 if (p == NULL) /* out of memory */
5987 {
5988 replace_stack_len -= 50;
5989 return;
5990 }
5991 if (replace_stack != NULL)
5992 {
5993 mch_memmove(p, replace_stack,
5994 (size_t)(replace_stack_nr * sizeof(char_u)));
5995 vim_free(replace_stack);
5996 }
5997 replace_stack = p;
5998 }
5999 p = replace_stack + replace_stack_nr - replace_offset;
6000 if (replace_offset)
6001 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6002 *p = c;
6003 ++replace_stack_nr;
6004}
6005
6006/*
6007 * call replace_push(c) with replace_offset set to the first NUL.
6008 */
6009 static void
6010replace_push_off(c)
6011 int c;
6012{
6013 char_u *p;
6014
6015 p = replace_stack + replace_stack_nr;
6016 for (replace_offset = 1; replace_offset < replace_stack_nr;
6017 ++replace_offset)
6018 if (*--p == NUL)
6019 break;
6020 replace_push(c);
6021 replace_offset = 0;
6022}
6023
6024/*
6025 * Pop one item from the replace stack.
6026 * return -1 if stack empty
6027 * return replaced character or NUL otherwise
6028 */
6029 static int
6030replace_pop()
6031{
6032 if (replace_stack_nr == 0)
6033 return -1;
6034 return (int)replace_stack[--replace_stack_nr];
6035}
6036
6037/*
6038 * Join the top two items on the replace stack. This removes to "off"'th NUL
6039 * encountered.
6040 */
6041 static void
6042replace_join(off)
6043 int off; /* offset for which NUL to remove */
6044{
6045 int i;
6046
6047 for (i = replace_stack_nr; --i >= 0; )
6048 if (replace_stack[i] == NUL && off-- <= 0)
6049 {
6050 --replace_stack_nr;
6051 mch_memmove(replace_stack + i, replace_stack + i + 1,
6052 (size_t)(replace_stack_nr - i));
6053 return;
6054 }
6055}
6056
6057/*
6058 * Pop bytes from the replace stack until a NUL is found, and insert them
6059 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6060 */
6061 static void
6062replace_pop_ins()
6063{
6064 int cc;
6065 int oldState = State;
6066
6067 State = NORMAL; /* don't want REPLACE here */
6068 while ((cc = replace_pop()) > 0)
6069 {
6070#ifdef FEAT_MBYTE
6071 mb_replace_pop_ins(cc);
6072#else
6073 ins_char(cc);
6074#endif
6075 dec_cursor();
6076 }
6077 State = oldState;
6078}
6079
6080#ifdef FEAT_MBYTE
6081/*
6082 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6083 * indicates a multi-byte char, pop the other bytes too.
6084 */
6085 static void
6086mb_replace_pop_ins(cc)
6087 int cc;
6088{
6089 int n;
6090 char_u buf[MB_MAXBYTES];
6091 int i;
6092 int c;
6093
6094 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6095 {
6096 buf[0] = cc;
6097 for (i = 1; i < n; ++i)
6098 buf[i] = replace_pop();
6099 ins_bytes_len(buf, n);
6100 }
6101 else
6102 ins_char(cc);
6103
6104 if (enc_utf8)
6105 /* Handle composing chars. */
6106 for (;;)
6107 {
6108 c = replace_pop();
6109 if (c == -1) /* stack empty */
6110 break;
6111 if ((n = MB_BYTE2LEN(c)) == 1)
6112 {
6113 /* Not a multi-byte char, put it back. */
6114 replace_push(c);
6115 break;
6116 }
6117 else
6118 {
6119 buf[0] = c;
6120 for (i = 1; i < n; ++i)
6121 buf[i] = replace_pop();
6122 if (utf_iscomposing(utf_ptr2char(buf)))
6123 ins_bytes_len(buf, n);
6124 else
6125 {
6126 /* Not a composing char, put it back. */
6127 for (i = n - 1; i >= 0; --i)
6128 replace_push(buf[i]);
6129 break;
6130 }
6131 }
6132 }
6133}
6134#endif
6135
6136/*
6137 * make the replace stack empty
6138 * (called when exiting replace mode)
6139 */
6140 static void
6141replace_flush()
6142{
6143 vim_free(replace_stack);
6144 replace_stack = NULL;
6145 replace_stack_len = 0;
6146 replace_stack_nr = 0;
6147}
6148
6149/*
6150 * Handle doing a BS for one character.
6151 * cc < 0: replace stack empty, just move cursor
6152 * cc == 0: character was inserted, delete it
6153 * cc > 0: character was replaced, put cc (first byte of original char) back
6154 * and check for more characters to be put back
6155 */
6156 static void
6157replace_do_bs()
6158{
6159 int cc;
6160#ifdef FEAT_VREPLACE
6161 int orig_len = 0;
6162 int ins_len;
6163 int orig_vcols = 0;
6164 colnr_T start_vcol;
6165 char_u *p;
6166 int i;
6167 int vcol;
6168#endif
6169
6170 cc = replace_pop();
6171 if (cc > 0)
6172 {
6173#ifdef FEAT_VREPLACE
6174 if (State & VREPLACE_FLAG)
6175 {
6176 /* Get the number of screen cells used by the character we are
6177 * going to delete. */
6178 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6179 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6180 }
6181#endif
6182#ifdef FEAT_MBYTE
6183 if (has_mbyte)
6184 {
6185 del_char(FALSE);
6186# ifdef FEAT_VREPLACE
6187 if (State & VREPLACE_FLAG)
6188 orig_len = STRLEN(ml_get_cursor());
6189# endif
6190 replace_push(cc);
6191 }
6192 else
6193#endif
6194 {
6195 pchar_cursor(cc);
6196#ifdef FEAT_VREPLACE
6197 if (State & VREPLACE_FLAG)
6198 orig_len = STRLEN(ml_get_cursor()) - 1;
6199#endif
6200 }
6201 replace_pop_ins();
6202
6203#ifdef FEAT_VREPLACE
6204 if (State & VREPLACE_FLAG)
6205 {
6206 /* Get the number of screen cells used by the inserted characters */
6207 p = ml_get_cursor();
6208 ins_len = STRLEN(p) - orig_len;
6209 vcol = start_vcol;
6210 for (i = 0; i < ins_len; ++i)
6211 {
6212 vcol += chartabsize(p + i, vcol);
6213#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006214 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215#endif
6216 }
6217 vcol -= start_vcol;
6218
6219 /* Delete spaces that were inserted after the cursor to keep the
6220 * text aligned. */
6221 curwin->w_cursor.col += ins_len;
6222 while (vcol > orig_vcols && gchar_cursor() == ' ')
6223 {
6224 del_char(FALSE);
6225 ++orig_vcols;
6226 }
6227 curwin->w_cursor.col -= ins_len;
6228 }
6229#endif
6230
6231 /* mark the buffer as changed and prepare for displaying */
6232 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6233 }
6234 else if (cc == 0)
6235 (void)del_char(FALSE);
6236}
6237
6238#ifdef FEAT_CINDENT
6239/*
6240 * Return TRUE if C-indenting is on.
6241 */
6242 static int
6243cindent_on()
6244{
6245 return (!p_paste && (curbuf->b_p_cin
6246# ifdef FEAT_EVAL
6247 || *curbuf->b_p_inde != NUL
6248# endif
6249 ));
6250}
6251#endif
6252
6253#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6254/*
6255 * Re-indent the current line, based on the current contents of it and the
6256 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6257 * confused what all the part that handles Control-T is doing that I'm not.
6258 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6259 */
6260
6261 void
6262fixthisline(get_the_indent)
6263 int (*get_the_indent) __ARGS((void));
6264{
6265 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6266 if (linewhite(curwin->w_cursor.lnum))
6267 did_ai = TRUE; /* delete the indent if the line stays empty */
6268}
6269
6270 void
6271fix_indent()
6272{
6273 if (p_paste)
6274 return;
6275# ifdef FEAT_LISP
6276 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6277 fixthisline(get_lisp_indent);
6278# endif
6279# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6280 else
6281# endif
6282# ifdef FEAT_CINDENT
6283 if (cindent_on())
6284 do_c_expr_indent();
6285# endif
6286}
6287
6288#endif
6289
6290#ifdef FEAT_CINDENT
6291/*
6292 * return TRUE if 'cinkeys' contains the key "keytyped",
6293 * when == '*': Only if key is preceded with '*' (indent before insert)
6294 * when == '!': Only if key is prededed with '!' (don't insert)
6295 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6296 *
6297 * "keytyped" can have a few special values:
6298 * KEY_OPEN_FORW
6299 * KEY_OPEN_BACK
6300 * KEY_COMPLETE just finished completion.
6301 *
6302 * If line_is_empty is TRUE accept keys with '0' before them.
6303 */
6304 int
6305in_cinkeys(keytyped, when, line_is_empty)
6306 int keytyped;
6307 int when;
6308 int line_is_empty;
6309{
6310 char_u *look;
6311 int try_match;
6312 int try_match_word;
6313 char_u *p;
6314 char_u *line;
6315 int icase;
6316 int i;
6317
6318#ifdef FEAT_EVAL
6319 if (*curbuf->b_p_inde != NUL)
6320 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6321 else
6322#endif
6323 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6324 while (*look)
6325 {
6326 /*
6327 * Find out if we want to try a match with this key, depending on
6328 * 'when' and a '*' or '!' before the key.
6329 */
6330 switch (when)
6331 {
6332 case '*': try_match = (*look == '*'); break;
6333 case '!': try_match = (*look == '!'); break;
6334 default: try_match = (*look != '*'); break;
6335 }
6336 if (*look == '*' || *look == '!')
6337 ++look;
6338
6339 /*
6340 * If there is a '0', only accept a match if the line is empty.
6341 * But may still match when typing last char of a word.
6342 */
6343 if (*look == '0')
6344 {
6345 try_match_word = try_match;
6346 if (!line_is_empty)
6347 try_match = FALSE;
6348 ++look;
6349 }
6350 else
6351 try_match_word = FALSE;
6352
6353 /*
6354 * does it look like a control character?
6355 */
6356 if (*look == '^'
6357#ifdef EBCDIC
6358 && (Ctrl_chr(look[1]) != 0)
6359#else
6360 && look[1] >= '?' && look[1] <= '_'
6361#endif
6362 )
6363 {
6364 if (try_match && keytyped == Ctrl_chr(look[1]))
6365 return TRUE;
6366 look += 2;
6367 }
6368 /*
6369 * 'o' means "o" command, open forward.
6370 * 'O' means "O" command, open backward.
6371 */
6372 else if (*look == 'o')
6373 {
6374 if (try_match && keytyped == KEY_OPEN_FORW)
6375 return TRUE;
6376 ++look;
6377 }
6378 else if (*look == 'O')
6379 {
6380 if (try_match && keytyped == KEY_OPEN_BACK)
6381 return TRUE;
6382 ++look;
6383 }
6384
6385 /*
6386 * 'e' means to check for "else" at start of line and just before the
6387 * cursor.
6388 */
6389 else if (*look == 'e')
6390 {
6391 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6392 {
6393 p = ml_get_curline();
6394 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6395 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6396 return TRUE;
6397 }
6398 ++look;
6399 }
6400
6401 /*
6402 * ':' only causes an indent if it is at the end of a label or case
6403 * statement, or when it was before typing the ':' (to fix
6404 * class::method for C++).
6405 */
6406 else if (*look == ':')
6407 {
6408 if (try_match && keytyped == ':')
6409 {
6410 p = ml_get_curline();
6411 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6412 return TRUE;
6413 if (curwin->w_cursor.col > 2
6414 && p[curwin->w_cursor.col - 1] == ':'
6415 && p[curwin->w_cursor.col - 2] == ':')
6416 {
6417 p[curwin->w_cursor.col - 1] = ' ';
6418 i = (cin_iscase(p) || cin_isscopedecl(p)
6419 || cin_islabel(30));
6420 p = ml_get_curline();
6421 p[curwin->w_cursor.col - 1] = ':';
6422 if (i)
6423 return TRUE;
6424 }
6425 }
6426 ++look;
6427 }
6428
6429
6430 /*
6431 * Is it a key in <>, maybe?
6432 */
6433 else if (*look == '<')
6434 {
6435 if (try_match)
6436 {
6437 /*
6438 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6439 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6440 * >, *, : and ! keys if they really really want to.
6441 */
6442 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6443 && keytyped == look[1])
6444 return TRUE;
6445
6446 if (keytyped == get_special_key_code(look + 1))
6447 return TRUE;
6448 }
6449 while (*look && *look != '>')
6450 look++;
6451 while (*look == '>')
6452 look++;
6453 }
6454
6455 /*
6456 * Is it a word: "=word"?
6457 */
6458 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6459 {
6460 ++look;
6461 if (*look == '~')
6462 {
6463 icase = TRUE;
6464 ++look;
6465 }
6466 else
6467 icase = FALSE;
6468 p = vim_strchr(look, ',');
6469 if (p == NULL)
6470 p = look + STRLEN(look);
6471 if ((try_match || try_match_word)
6472 && curwin->w_cursor.col >= (colnr_T)(p - look))
6473 {
6474 int match = FALSE;
6475
6476#ifdef FEAT_INS_EXPAND
6477 if (keytyped == KEY_COMPLETE)
6478 {
6479 char_u *s;
6480
6481 /* Just completed a word, check if it starts with "look".
6482 * search back for the start of a word. */
6483 line = ml_get_curline();
6484# ifdef FEAT_MBYTE
6485 if (has_mbyte)
6486 {
6487 char_u *n;
6488
6489 for (s = line + curwin->w_cursor.col; s > line; s = n)
6490 {
6491 n = mb_prevptr(line, s);
6492 if (!vim_iswordp(n))
6493 break;
6494 }
6495 }
6496 else
6497# endif
6498 for (s = line + curwin->w_cursor.col; s > line; --s)
6499 if (!vim_iswordc(s[-1]))
6500 break;
6501 if (s + (p - look) <= line + curwin->w_cursor.col
6502 && (icase
6503 ? MB_STRNICMP(s, look, p - look)
6504 : STRNCMP(s, look, p - look)) == 0)
6505 match = TRUE;
6506 }
6507 else
6508#endif
6509 /* TODO: multi-byte */
6510 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6511 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6512 {
6513 line = ml_get_cursor();
6514 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6515 || !vim_iswordc(line[-(p - look) - 1]))
6516 && (icase
6517 ? MB_STRNICMP(line - (p - look), look, p - look)
6518 : STRNCMP(line - (p - look), look, p - look))
6519 == 0)
6520 match = TRUE;
6521 }
6522 if (match && try_match_word && !try_match)
6523 {
6524 /* "0=word": Check if there are only blanks before the
6525 * word. */
6526 line = ml_get_curline();
6527 if ((int)(skipwhite(line) - line) !=
6528 (int)(curwin->w_cursor.col - (p - look)))
6529 match = FALSE;
6530 }
6531 if (match)
6532 return TRUE;
6533 }
6534 look = p;
6535 }
6536
6537 /*
6538 * ok, it's a boring generic character.
6539 */
6540 else
6541 {
6542 if (try_match && *look == keytyped)
6543 return TRUE;
6544 ++look;
6545 }
6546
6547 /*
6548 * Skip over ", ".
6549 */
6550 look = skip_to_option_part(look);
6551 }
6552 return FALSE;
6553}
6554#endif /* FEAT_CINDENT */
6555
6556#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6557/*
6558 * Map Hebrew keyboard when in hkmap mode.
6559 */
6560 int
6561hkmap(c)
6562 int c;
6563{
6564 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6565 {
6566 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6567 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6568 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6569 static char_u map[26] =
6570 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6571 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6572 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6573 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6574 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6575 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6576 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6577 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6578 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6579
6580 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6581 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6582 /* '-1'='sofit' */
6583 else if (c == 'x')
6584 return 'X';
6585 else if (c == 'q')
6586 return '\''; /* {geresh}={'} */
6587 else if (c == 246)
6588 return ' '; /* \"o --> ' ' for a german keyboard */
6589 else if (c == 228)
6590 return ' '; /* \"a --> ' ' -- / -- */
6591 else if (c == 252)
6592 return ' '; /* \"u --> ' ' -- / -- */
6593#ifdef EBCDIC
6594 else if (islower(c))
6595#else
6596 /* NOTE: islower() does not do the right thing for us on Linux so we
6597 * do this the same was as 5.7 and previous, so it works correctly on
6598 * all systems. Specifically, the e.g. Delete and Arrow keys are
6599 * munged and won't work if e.g. searching for Hebrew text.
6600 */
6601 else if (c >= 'a' && c <= 'z')
6602#endif
6603 return (int)(map[CharOrdLow(c)] + p_aleph);
6604 else
6605 return c;
6606 }
6607 else
6608 {
6609 switch (c)
6610 {
6611 case '`': return ';';
6612 case '/': return '.';
6613 case '\'': return ',';
6614 case 'q': return '/';
6615 case 'w': return '\'';
6616
6617 /* Hebrew letters - set offset from 'a' */
6618 case ',': c = '{'; break;
6619 case '.': c = 'v'; break;
6620 case ';': c = 't'; break;
6621 default: {
6622 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6623
6624#ifdef EBCDIC
6625 /* see note about islower() above */
6626 if (!islower(c))
6627#else
6628 if (c < 'a' || c > 'z')
6629#endif
6630 return c;
6631 c = str[CharOrdLow(c)];
6632 break;
6633 }
6634 }
6635
6636 return (int)(CharOrdLow(c) + p_aleph);
6637 }
6638}
6639#endif
6640
6641 static void
6642ins_reg()
6643{
6644 int need_redraw = FALSE;
6645 int regname;
6646 int literally = 0;
6647
6648 /*
6649 * If we are going to wait for a character, show a '"'.
6650 */
6651 pc_status = PC_STATUS_UNSET;
6652 if (redrawing() && !char_avail())
6653 {
6654 /* may need to redraw when no more chars available now */
6655 ins_redraw();
6656
6657 edit_putchar('"', TRUE);
6658#ifdef FEAT_CMDL_INFO
6659 add_to_showcmd_c(Ctrl_R);
6660#endif
6661 }
6662
6663#ifdef USE_ON_FLY_SCROLL
6664 dont_scroll = TRUE; /* disallow scrolling here */
6665#endif
6666
6667 /*
6668 * Don't map the register name. This also prevents the mode message to be
6669 * deleted when ESC is hit.
6670 */
6671 ++no_mapping;
6672 regname = safe_vgetc();
6673#ifdef FEAT_LANGMAP
6674 LANGMAP_ADJUST(regname, TRUE);
6675#endif
6676 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6677 {
6678 /* Get a third key for literal register insertion */
6679 literally = regname;
6680#ifdef FEAT_CMDL_INFO
6681 add_to_showcmd_c(literally);
6682#endif
6683 regname = safe_vgetc();
6684#ifdef FEAT_LANGMAP
6685 LANGMAP_ADJUST(regname, TRUE);
6686#endif
6687 }
6688 --no_mapping;
6689
6690#ifdef FEAT_EVAL
6691 /*
6692 * Don't call u_sync() while getting the expression,
6693 * evaluating it or giving an error message for it!
6694 */
6695 ++no_u_sync;
6696 if (regname == '=')
6697 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006698# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006700# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006702# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 /* Restore the Input Method. */
6704 if (im_on)
6705 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006706# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006708 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6709 {
6710 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 else
6714 {
6715#endif
6716 if (literally == Ctrl_O || literally == Ctrl_P)
6717 {
6718 /* Append the command to the redo buffer. */
6719 AppendCharToRedobuff(Ctrl_R);
6720 AppendCharToRedobuff(literally);
6721 AppendCharToRedobuff(regname);
6722
6723 do_put(regname, BACKWARD, 1L,
6724 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6725 }
6726 else if (insert_reg(regname, literally) == FAIL)
6727 {
6728 vim_beep();
6729 need_redraw = TRUE; /* remove the '"' */
6730 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006731 else if (stop_insert_mode)
6732 /* When the '=' register was used and a function was invoked that
6733 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
6734 * insert anything, need to remove the '"' */
6735 need_redraw = TRUE;
6736
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737#ifdef FEAT_EVAL
6738 }
6739 --no_u_sync;
6740#endif
6741#ifdef FEAT_CMDL_INFO
6742 clear_showcmd();
6743#endif
6744
6745 /* If the inserted register is empty, we need to remove the '"' */
6746 if (need_redraw || stuff_empty())
6747 edit_unputchar();
6748}
6749
6750/*
6751 * CTRL-G commands in Insert mode.
6752 */
6753 static void
6754ins_ctrl_g()
6755{
6756 int c;
6757
6758#ifdef FEAT_INS_EXPAND
6759 /* Right after CTRL-X the cursor will be after the ruler. */
6760 setcursor();
6761#endif
6762
6763 /*
6764 * Don't map the second key. This also prevents the mode message to be
6765 * deleted when ESC is hit.
6766 */
6767 ++no_mapping;
6768 c = safe_vgetc();
6769 --no_mapping;
6770 switch (c)
6771 {
6772 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
6773 case K_UP:
6774 case Ctrl_K:
6775 case 'k': ins_up(TRUE);
6776 break;
6777
6778 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
6779 case K_DOWN:
6780 case Ctrl_J:
6781 case 'j': ins_down(TRUE);
6782 break;
6783
6784 /* CTRL-G u: start new undoable edit */
6785 case 'u': u_sync();
6786 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006787
6788 /* Need to reset Insstart, esp. because a BS that joins
6789 * aline to the previous one must save for undo. */
6790 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 break;
6792
6793 /* Unknown CTRL-G command, reserved for future expansion. */
6794 default: vim_beep();
6795 }
6796}
6797
6798/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006799 * CTRL-^ in Insert mode.
6800 */
6801 static void
6802ins_ctrl_hat()
6803{
6804 if (map_to_exists_mode((char_u *)"", LANGMAP))
6805 {
6806 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
6807 if (State & LANGMAP)
6808 {
6809 curbuf->b_p_iminsert = B_IMODE_NONE;
6810 State &= ~LANGMAP;
6811 }
6812 else
6813 {
6814 curbuf->b_p_iminsert = B_IMODE_LMAP;
6815 State |= LANGMAP;
6816#ifdef USE_IM_CONTROL
6817 im_set_active(FALSE);
6818#endif
6819 }
6820 }
6821#ifdef USE_IM_CONTROL
6822 else
6823 {
6824 /* There are no ":lmap" mappings, toggle IM */
6825 if (im_get_status())
6826 {
6827 curbuf->b_p_iminsert = B_IMODE_NONE;
6828 im_set_active(FALSE);
6829 }
6830 else
6831 {
6832 curbuf->b_p_iminsert = B_IMODE_IM;
6833 State &= ~LANGMAP;
6834 im_set_active(TRUE);
6835 }
6836 }
6837#endif
6838 set_iminsert_global();
6839 showmode();
6840#ifdef FEAT_GUI
6841 /* may show different cursor shape or color */
6842 if (gui.in_use)
6843 gui_update_cursor(TRUE, FALSE);
6844#endif
6845#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6846 /* Show/unshow value of 'keymap' in status lines. */
6847 status_redraw_curbuf();
6848#endif
6849}
6850
6851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 * Handle ESC in insert mode.
6853 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
6854 * insert.
6855 */
6856 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00006857ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 long *count;
6859 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00006860 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861{
6862 int temp;
6863 static int disabled_redraw = FALSE;
6864
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006865#ifdef FEAT_SYN_HL
6866 check_spell_redraw();
6867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868#if defined(FEAT_HANGULIN)
6869# if defined(ESC_CHG_TO_ENG_MODE)
6870 hangul_input_state_set(0);
6871# endif
6872 if (composing_hangul)
6873 {
6874 push_raw_key(composing_hangul_buffer, 2);
6875 composing_hangul = 0;
6876 }
6877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878
6879 temp = curwin->w_cursor.col;
6880 if (disabled_redraw)
6881 {
6882 --RedrawingDisabled;
6883 disabled_redraw = FALSE;
6884 }
6885 if (!arrow_used)
6886 {
6887 /*
6888 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00006889 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
6890 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 */
6892 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00006893 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894
6895 /*
6896 * Repeating insert may take a long time. Check for
6897 * interrupt now and then.
6898 */
6899 if (*count > 0)
6900 {
6901 line_breakcheck();
6902 if (got_int)
6903 *count = 0;
6904 }
6905
6906 if (--*count > 0) /* repeat what was typed */
6907 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006908 /* Vi repeats the insert without replacing characters. */
6909 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
6910 State &= ~REPLACE_FLAG;
6911
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 (void)start_redo_ins();
6913 if (cmdchar == 'r' || cmdchar == 'v')
6914 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
6915 ++RedrawingDisabled;
6916 disabled_redraw = TRUE;
6917 return FALSE; /* repeat the insert */
6918 }
6919 stop_insert(&curwin->w_cursor, TRUE);
6920 undisplay_dollar();
6921 }
6922
6923 /* When an autoindent was removed, curswant stays after the
6924 * indent */
6925 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
6926 curwin->w_set_curswant = TRUE;
6927
6928 /* Remember the last Insert position in the '^ mark. */
6929 if (!cmdmod.keepjumps)
6930 curbuf->b_last_insert = curwin->w_cursor;
6931
6932 /*
6933 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00006934 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00006936 if (!nomove
6937 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938#ifdef FEAT_VIRTUALEDIT
6939 || curwin->w_cursor.coladd > 0
6940#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006941 )
6942 && (restart_edit == NUL
6943 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00006945 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006947 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948#ifdef FEAT_RIGHTLEFT
6949 && !revins_on
6950#endif
6951 )
6952 {
6953#ifdef FEAT_VIRTUALEDIT
6954 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
6955 {
6956 oneleft();
6957 if (restart_edit != NUL)
6958 ++curwin->w_cursor.coladd;
6959 }
6960 else
6961#endif
6962 {
6963 --curwin->w_cursor.col;
6964#ifdef FEAT_MBYTE
6965 /* Correct cursor for multi-byte character. */
6966 if (has_mbyte)
6967 mb_adjust_cursor();
6968#endif
6969 }
6970 }
6971
6972#ifdef USE_IM_CONTROL
6973 /* Disable IM to allow typing English directly for Normal mode commands.
6974 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
6975 * well). */
6976 if (!(State & LANGMAP))
6977 im_save_status(&curbuf->b_p_iminsert);
6978 im_set_active(FALSE);
6979#endif
6980
6981 State = NORMAL;
6982 /* need to position cursor again (e.g. when on a TAB ) */
6983 changed_cline_bef_curs();
6984
6985#ifdef FEAT_MOUSE
6986 setmouse();
6987#endif
6988#ifdef CURSOR_SHAPE
6989 ui_cursor_shape(); /* may show different cursor shape */
6990#endif
6991
6992 /*
6993 * When recording or for CTRL-O, need to display the new mode.
6994 * Otherwise remove the mode message.
6995 */
6996 if (Recording || restart_edit != NUL)
6997 showmode();
6998 else if (p_smd)
6999 MSG("");
7000
7001 return TRUE; /* exit Insert mode */
7002}
7003
7004#ifdef FEAT_RIGHTLEFT
7005/*
7006 * Toggle language: hkmap and revins_on.
7007 * Move to end of reverse inserted text.
7008 */
7009 static void
7010ins_ctrl_()
7011{
7012 if (revins_on && revins_chars && revins_scol >= 0)
7013 {
7014 while (gchar_cursor() != NUL && revins_chars--)
7015 ++curwin->w_cursor.col;
7016 }
7017 p_ri = !p_ri;
7018 revins_on = (State == INSERT && p_ri);
7019 if (revins_on)
7020 {
7021 revins_scol = curwin->w_cursor.col;
7022 revins_legal++;
7023 revins_chars = 0;
7024 undisplay_dollar();
7025 }
7026 else
7027 revins_scol = -1;
7028#ifdef FEAT_FKMAP
7029 if (p_altkeymap)
7030 {
7031 /*
7032 * to be consistent also for redo command, using '.'
7033 * set arrow_used to true and stop it - causing to redo
7034 * characters entered in one mode (normal/reverse insert).
7035 */
7036 arrow_used = TRUE;
7037 (void)stop_arrow();
7038 p_fkmap = curwin->w_p_rl ^ p_ri;
7039 if (p_fkmap && p_ri)
7040 State = INSERT;
7041 }
7042 else
7043#endif
7044 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7045 showmode();
7046}
7047#endif
7048
7049#ifdef FEAT_VISUAL
7050/*
7051 * If 'keymodel' contains "startsel", may start selection.
7052 * Returns TRUE when a CTRL-O and other keys stuffed.
7053 */
7054 static int
7055ins_start_select(c)
7056 int c;
7057{
7058 if (km_startsel)
7059 switch (c)
7060 {
7061 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 case K_PAGEUP:
7064 case K_KPAGEUP:
7065 case K_PAGEDOWN:
7066 case K_KPAGEDOWN:
7067# ifdef MACOS
7068 case K_LEFT:
7069 case K_RIGHT:
7070 case K_UP:
7071 case K_DOWN:
7072 case K_END:
7073 case K_HOME:
7074# endif
7075 if (!(mod_mask & MOD_MASK_SHIFT))
7076 break;
7077 /* FALLTHROUGH */
7078 case K_S_LEFT:
7079 case K_S_RIGHT:
7080 case K_S_UP:
7081 case K_S_DOWN:
7082 case K_S_END:
7083 case K_S_HOME:
7084 /* Start selection right away, the cursor can move with
7085 * CTRL-O when beyond the end of the line. */
7086 start_selection();
7087
7088 /* Execute the key in (insert) Select mode. */
7089 stuffcharReadbuff(Ctrl_O);
7090 if (mod_mask)
7091 {
7092 char_u buf[4];
7093
7094 buf[0] = K_SPECIAL;
7095 buf[1] = KS_MODIFIER;
7096 buf[2] = mod_mask;
7097 buf[3] = NUL;
7098 stuffReadbuff(buf);
7099 }
7100 stuffcharReadbuff(c);
7101 return TRUE;
7102 }
7103 return FALSE;
7104}
7105#endif
7106
7107/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007108 * <Insert> key in Insert mode: toggle insert/remplace mode.
7109 */
7110 static void
7111ins_insert(replaceState)
7112 int replaceState;
7113{
7114#ifdef FEAT_FKMAP
7115 if (p_fkmap && p_ri)
7116 {
7117 beep_flush();
7118 EMSG(farsi_text_3); /* encoded in Farsi */
7119 return;
7120 }
7121#endif
7122
7123#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007124# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007125 set_vim_var_string(VV_INSERTMODE,
7126 (char_u *)((State & REPLACE_FLAG) ? "i" :
7127 replaceState == VREPLACE ? "v" : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007128# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007129 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7130#endif
7131 if (State & REPLACE_FLAG)
7132 State = INSERT | (State & LANGMAP);
7133 else
7134 State = replaceState | (State & LANGMAP);
7135 AppendCharToRedobuff(K_INS);
7136 showmode();
7137#ifdef CURSOR_SHAPE
7138 ui_cursor_shape(); /* may show different cursor shape */
7139#endif
7140}
7141
7142/*
7143 * Pressed CTRL-O in Insert mode.
7144 */
7145 static void
7146ins_ctrl_o()
7147{
7148#ifdef FEAT_VREPLACE
7149 if (State & VREPLACE_FLAG)
7150 restart_edit = 'V';
7151 else
7152#endif
7153 if (State & REPLACE_FLAG)
7154 restart_edit = 'R';
7155 else
7156 restart_edit = 'I';
7157#ifdef FEAT_VIRTUALEDIT
7158 if (virtual_active())
7159 ins_at_eol = FALSE; /* cursor always keeps its column */
7160 else
7161#endif
7162 ins_at_eol = (gchar_cursor() == NUL);
7163}
7164
7165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 * If the cursor is on an indent, ^T/^D insert/delete one
7167 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7168 * Always round the indent to 'shiftwith', this is compatible
7169 * with vi. But vi only supports ^T and ^D after an
7170 * autoindent, we support it everywhere.
7171 */
7172 static void
7173ins_shift(c, lastc)
7174 int c;
7175 int lastc;
7176{
7177 if (stop_arrow() == FAIL)
7178 return;
7179 AppendCharToRedobuff(c);
7180
7181 /*
7182 * 0^D and ^^D: remove all indent.
7183 */
7184 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7185 {
7186 --curwin->w_cursor.col;
7187 (void)del_char(FALSE); /* delete the '^' or '0' */
7188 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7189 if (State & REPLACE_FLAG)
7190 replace_pop_ins();
7191 if (lastc == '^')
7192 old_indent = get_indent(); /* remember curr. indent */
7193 change_indent(INDENT_SET, 0, TRUE, 0);
7194 }
7195 else
7196 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7197
7198 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7199 did_ai = FALSE;
7200#ifdef FEAT_SMARTINDENT
7201 did_si = FALSE;
7202 can_si = FALSE;
7203 can_si_back = FALSE;
7204#endif
7205#ifdef FEAT_CINDENT
7206 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7207#endif
7208}
7209
7210 static void
7211ins_del()
7212{
7213 int temp;
7214
7215 if (stop_arrow() == FAIL)
7216 return;
7217 if (gchar_cursor() == NUL) /* delete newline */
7218 {
7219 temp = curwin->w_cursor.col;
7220 if (!can_bs(BS_EOL) /* only if "eol" included */
7221 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7222 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7223 || do_join(FALSE) == FAIL)
7224 vim_beep();
7225 else
7226 curwin->w_cursor.col = temp;
7227 }
7228 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7229 vim_beep();
7230 did_ai = FALSE;
7231#ifdef FEAT_SMARTINDENT
7232 did_si = FALSE;
7233 can_si = FALSE;
7234 can_si_back = FALSE;
7235#endif
7236 AppendCharToRedobuff(K_DEL);
7237}
7238
7239/*
7240 * Handle Backspace, delete-word and delete-line in Insert mode.
7241 * Return TRUE when backspace was actually used.
7242 */
7243 static int
7244ins_bs(c, mode, inserted_space_p)
7245 int c;
7246 int mode;
7247 int *inserted_space_p;
7248{
7249 linenr_T lnum;
7250 int cc;
7251 int temp = 0; /* init for GCC */
7252 colnr_T mincol;
7253 int did_backspace = FALSE;
7254 int in_indent;
7255 int oldState;
7256#ifdef FEAT_MBYTE
7257 int p1, p2;
7258#endif
7259
7260 /*
7261 * can't delete anything in an empty file
7262 * can't backup past first character in buffer
7263 * can't backup past starting point unless 'backspace' > 1
7264 * can backup to a previous line if 'backspace' == 0
7265 */
7266 if ( bufempty()
7267 || (
7268#ifdef FEAT_RIGHTLEFT
7269 !revins_on &&
7270#endif
7271 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7272 || (!can_bs(BS_START)
7273 && (arrow_used
7274 || (curwin->w_cursor.lnum == Insstart.lnum
7275 && curwin->w_cursor.col <= Insstart.col)))
7276 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7277 && curwin->w_cursor.col <= ai_col)
7278 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7279 {
7280 vim_beep();
7281 return FALSE;
7282 }
7283
7284 if (stop_arrow() == FAIL)
7285 return FALSE;
7286 in_indent = inindent(0);
7287#ifdef FEAT_CINDENT
7288 if (in_indent)
7289 can_cindent = FALSE;
7290#endif
7291#ifdef FEAT_COMMENTS
7292 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7293#endif
7294#ifdef FEAT_RIGHTLEFT
7295 if (revins_on) /* put cursor after last inserted char */
7296 inc_cursor();
7297#endif
7298
7299#ifdef FEAT_VIRTUALEDIT
7300 /* Virtualedit:
7301 * BACKSPACE_CHAR eats a virtual space
7302 * BACKSPACE_WORD eats all coladd
7303 * BACKSPACE_LINE eats all coladd and keeps going
7304 */
7305 if (curwin->w_cursor.coladd > 0)
7306 {
7307 if (mode == BACKSPACE_CHAR)
7308 {
7309 --curwin->w_cursor.coladd;
7310 return TRUE;
7311 }
7312 if (mode == BACKSPACE_WORD)
7313 {
7314 curwin->w_cursor.coladd = 0;
7315 return TRUE;
7316 }
7317 curwin->w_cursor.coladd = 0;
7318 }
7319#endif
7320
7321 /*
7322 * delete newline!
7323 */
7324 if (curwin->w_cursor.col == 0)
7325 {
7326 lnum = Insstart.lnum;
7327 if (curwin->w_cursor.lnum == Insstart.lnum
7328#ifdef FEAT_RIGHTLEFT
7329 || revins_on
7330#endif
7331 )
7332 {
7333 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7334 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7335 return FALSE;
7336 --Insstart.lnum;
7337 Insstart.col = MAXCOL;
7338 }
7339 /*
7340 * In replace mode:
7341 * cc < 0: NL was inserted, delete it
7342 * cc >= 0: NL was replaced, put original characters back
7343 */
7344 cc = -1;
7345 if (State & REPLACE_FLAG)
7346 cc = replace_pop(); /* returns -1 if NL was inserted */
7347 /*
7348 * In replace mode, in the line we started replacing, we only move the
7349 * cursor.
7350 */
7351 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7352 {
7353 dec_cursor();
7354 }
7355 else
7356 {
7357#ifdef FEAT_VREPLACE
7358 if (!(State & VREPLACE_FLAG)
7359 || curwin->w_cursor.lnum > orig_line_count)
7360#endif
7361 {
7362 temp = gchar_cursor(); /* remember current char */
7363 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007364
7365 /* When "aw" is in 'formatoptions' we must delete the space at
7366 * the end of the line, otherwise the line will be broken
7367 * again when auto-formatting. */
7368 if (has_format_option(FO_AUTO)
7369 && has_format_option(FO_WHITE_PAR))
7370 {
7371 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7372 TRUE);
7373 int len;
7374
7375 len = STRLEN(ptr);
7376 if (len > 0 && ptr[len - 1] == ' ')
7377 ptr[len - 1] = NUL;
7378 }
7379
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 (void)do_join(FALSE);
7381 if (temp == NUL && gchar_cursor() != NUL)
7382 inc_cursor();
7383 }
7384#ifdef FEAT_VREPLACE
7385 else
7386 dec_cursor();
7387#endif
7388
7389 /*
7390 * In REPLACE mode we have to put back the text that was replaced
7391 * by the NL. On the replace stack is first a NUL-terminated
7392 * sequence of characters that were deleted and then the
7393 * characters that NL replaced.
7394 */
7395 if (State & REPLACE_FLAG)
7396 {
7397 /*
7398 * Do the next ins_char() in NORMAL state, to
7399 * prevent ins_char() from replacing characters and
7400 * avoiding showmatch().
7401 */
7402 oldState = State;
7403 State = NORMAL;
7404 /*
7405 * restore characters (blanks) deleted after cursor
7406 */
7407 while (cc > 0)
7408 {
7409 temp = curwin->w_cursor.col;
7410#ifdef FEAT_MBYTE
7411 mb_replace_pop_ins(cc);
7412#else
7413 ins_char(cc);
7414#endif
7415 curwin->w_cursor.col = temp;
7416 cc = replace_pop();
7417 }
7418 /* restore the characters that NL replaced */
7419 replace_pop_ins();
7420 State = oldState;
7421 }
7422 }
7423 did_ai = FALSE;
7424 }
7425 else
7426 {
7427 /*
7428 * Delete character(s) before the cursor.
7429 */
7430#ifdef FEAT_RIGHTLEFT
7431 if (revins_on) /* put cursor on last inserted char */
7432 dec_cursor();
7433#endif
7434 mincol = 0;
7435 /* keep indent */
7436 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7437#ifdef FEAT_RIGHTLEFT
7438 && !revins_on
7439#endif
7440 )
7441 {
7442 temp = curwin->w_cursor.col;
7443 beginline(BL_WHITE);
7444 if (curwin->w_cursor.col < (colnr_T)temp)
7445 mincol = curwin->w_cursor.col;
7446 curwin->w_cursor.col = temp;
7447 }
7448
7449 /*
7450 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7451 */
7452 if ( mode == BACKSPACE_CHAR
7453 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007454 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 && (*(ml_get_cursor() - 1) == TAB
7456 || (*(ml_get_cursor() - 1) == ' '
7457 && (!*inserted_space_p
7458 || arrow_used))))))
7459 {
7460 int ts;
7461 colnr_T vcol;
7462 colnr_T want_vcol;
7463 int extra = 0;
7464
7465 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007466 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 ts = curbuf->b_p_sw;
7468 else
7469 ts = curbuf->b_p_sts;
7470 /* Compute the virtual column where we want to be. Since
7471 * 'showbreak' may get in the way, need to get the last column of
7472 * the previous character. */
7473 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7474 dec_cursor();
7475 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7476 inc_cursor();
7477 want_vcol = (want_vcol / ts) * ts;
7478
7479 /* delete characters until we are at or before want_vcol */
7480 while (vcol > want_vcol
7481 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7482 {
7483 dec_cursor();
7484 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7485 if (State & REPLACE_FLAG)
7486 {
7487 /* Don't delete characters before the insert point when in
7488 * Replace mode */
7489 if (curwin->w_cursor.lnum != Insstart.lnum
7490 || curwin->w_cursor.col >= Insstart.col)
7491 {
7492#if 0 /* what was this for? It causes problems when sw != ts. */
7493 if (State == REPLACE && (int)vcol < want_vcol)
7494 {
7495 (void)del_char(FALSE);
7496 extra = 2; /* don't pop too much */
7497 }
7498 else
7499#endif
7500 replace_do_bs();
7501 }
7502 }
7503 else
7504 (void)del_char(FALSE);
7505 }
7506
7507 /* insert extra spaces until we are at want_vcol */
7508 while (vcol < want_vcol)
7509 {
7510 /* Remember the first char we inserted */
7511 if (curwin->w_cursor.lnum == Insstart.lnum
7512 && curwin->w_cursor.col < Insstart.col)
7513 Insstart.col = curwin->w_cursor.col;
7514
7515#ifdef FEAT_VREPLACE
7516 if (State & VREPLACE_FLAG)
7517 ins_char(' ');
7518 else
7519#endif
7520 {
7521 ins_str((char_u *)" ");
7522 if ((State & REPLACE_FLAG) && extra <= 1)
7523 {
7524 if (extra)
7525 replace_push_off(NUL);
7526 else
7527 replace_push(NUL);
7528 }
7529 if (extra == 2)
7530 extra = 1;
7531 }
7532 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7533 }
7534 }
7535
7536 /*
7537 * Delete upto starting point, start of line or previous word.
7538 */
7539 else do
7540 {
7541#ifdef FEAT_RIGHTLEFT
7542 if (!revins_on) /* put cursor on char to be deleted */
7543#endif
7544 dec_cursor();
7545
7546 /* start of word? */
7547 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7548 {
7549 mode = BACKSPACE_WORD_NOT_SPACE;
7550 temp = vim_iswordc(gchar_cursor());
7551 }
7552 /* end of word? */
7553 else if (mode == BACKSPACE_WORD_NOT_SPACE
7554 && (vim_isspace(cc = gchar_cursor())
7555 || vim_iswordc(cc) != temp))
7556 {
7557#ifdef FEAT_RIGHTLEFT
7558 if (!revins_on)
7559#endif
7560 inc_cursor();
7561#ifdef FEAT_RIGHTLEFT
7562 else if (State & REPLACE_FLAG)
7563 dec_cursor();
7564#endif
7565 break;
7566 }
7567 if (State & REPLACE_FLAG)
7568 replace_do_bs();
7569 else
7570 {
7571#ifdef FEAT_MBYTE
7572 if (enc_utf8 && p_deco)
7573 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7574#endif
7575 (void)del_char(FALSE);
7576#ifdef FEAT_MBYTE
7577 /*
7578 * If p1 or p2 is non-zero, there are combining characters we
7579 * need to take account of. Don't back up before the base
7580 * character.
7581 */
7582 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7583 inc_cursor();
7584#endif
7585#ifdef FEAT_RIGHTLEFT
7586 if (revins_chars)
7587 {
7588 revins_chars--;
7589 revins_legal++;
7590 }
7591 if (revins_on && gchar_cursor() == NUL)
7592 break;
7593#endif
7594 }
7595 /* Just a single backspace?: */
7596 if (mode == BACKSPACE_CHAR)
7597 break;
7598 } while (
7599#ifdef FEAT_RIGHTLEFT
7600 revins_on ||
7601#endif
7602 (curwin->w_cursor.col > mincol
7603 && (curwin->w_cursor.lnum != Insstart.lnum
7604 || curwin->w_cursor.col != Insstart.col)));
7605 did_backspace = TRUE;
7606 }
7607#ifdef FEAT_SMARTINDENT
7608 did_si = FALSE;
7609 can_si = FALSE;
7610 can_si_back = FALSE;
7611#endif
7612 if (curwin->w_cursor.col <= 1)
7613 did_ai = FALSE;
7614 /*
7615 * It's a little strange to put backspaces into the redo
7616 * buffer, but it makes auto-indent a lot easier to deal
7617 * with.
7618 */
7619 AppendCharToRedobuff(c);
7620
7621 /* If deleted before the insertion point, adjust it */
7622 if (curwin->w_cursor.lnum == Insstart.lnum
7623 && curwin->w_cursor.col < Insstart.col)
7624 Insstart.col = curwin->w_cursor.col;
7625
7626 /* vi behaviour: the cursor moves backward but the character that
7627 * was there remains visible
7628 * Vim behaviour: the cursor moves backward and the character that
7629 * was there is erased from the screen.
7630 * We can emulate the vi behaviour by pretending there is a dollar
7631 * displayed even when there isn't.
7632 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7633 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7634 dollar_vcol = curwin->w_virtcol;
7635
7636 return did_backspace;
7637}
7638
7639#ifdef FEAT_MOUSE
7640 static void
7641ins_mouse(c)
7642 int c;
7643{
7644 pos_T tpos;
7645
7646# ifdef FEAT_GUI
7647 /* When GUI is active, also move/paste when 'mouse' is empty */
7648 if (!gui.in_use)
7649# endif
7650 if (!mouse_has(MOUSE_INSERT))
7651 return;
7652
7653 undisplay_dollar();
7654 tpos = curwin->w_cursor;
7655 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7656 {
7657 start_arrow(&tpos);
7658# ifdef FEAT_CINDENT
7659 can_cindent = TRUE;
7660# endif
7661 }
7662
7663#ifdef FEAT_WINDOWS
7664 /* redraw status lines (in case another window became active) */
7665 redraw_statuslines();
7666#endif
7667}
7668
7669 static void
7670ins_mousescroll(up)
7671 int up;
7672{
7673 pos_T tpos;
7674# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7675 win_T *old_curwin;
7676# endif
7677
7678 tpos = curwin->w_cursor;
7679
7680# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7681 old_curwin = curwin;
7682
7683 /* Currently the mouse coordinates are only known in the GUI. */
7684 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7685 {
7686 int row, col;
7687
7688 row = mouse_row;
7689 col = mouse_col;
7690
7691 /* find the window at the pointer coordinates */
7692 curwin = mouse_find_win(&row, &col);
7693 curbuf = curwin->w_buffer;
7694 }
7695 if (curwin == old_curwin)
7696# endif
7697 undisplay_dollar();
7698
7699 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7700 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7701 else
7702 scroll_redraw(up, 3L);
7703
7704# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7705 curwin->w_redr_status = TRUE;
7706
7707 curwin = old_curwin;
7708 curbuf = curwin->w_buffer;
7709# endif
7710
7711 if (!equalpos(curwin->w_cursor, tpos))
7712 {
7713 start_arrow(&tpos);
7714# ifdef FEAT_CINDENT
7715 can_cindent = TRUE;
7716# endif
7717 }
7718}
7719#endif
7720
7721#ifdef FEAT_GUI
7722 void
7723ins_scroll()
7724{
7725 pos_T tpos;
7726
7727 undisplay_dollar();
7728 tpos = curwin->w_cursor;
7729 if (gui_do_scroll())
7730 {
7731 start_arrow(&tpos);
7732# ifdef FEAT_CINDENT
7733 can_cindent = TRUE;
7734# endif
7735 }
7736}
7737
7738 void
7739ins_horscroll()
7740{
7741 pos_T tpos;
7742
7743 undisplay_dollar();
7744 tpos = curwin->w_cursor;
7745 if (gui_do_horiz_scroll())
7746 {
7747 start_arrow(&tpos);
7748# ifdef FEAT_CINDENT
7749 can_cindent = TRUE;
7750# endif
7751 }
7752}
7753#endif
7754
7755 static void
7756ins_left()
7757{
7758 pos_T tpos;
7759
7760#ifdef FEAT_FOLDING
7761 if ((fdo_flags & FDO_HOR) && KeyTyped)
7762 foldOpenCursor();
7763#endif
7764 undisplay_dollar();
7765 tpos = curwin->w_cursor;
7766 if (oneleft() == OK)
7767 {
7768 start_arrow(&tpos);
7769#ifdef FEAT_RIGHTLEFT
7770 /* If exit reversed string, position is fixed */
7771 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
7772 revins_legal++;
7773 revins_chars++;
7774#endif
7775 }
7776
7777 /*
7778 * if 'whichwrap' set for cursor in insert mode may go to
7779 * previous line
7780 */
7781 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
7782 {
7783 start_arrow(&tpos);
7784 --(curwin->w_cursor.lnum);
7785 coladvance((colnr_T)MAXCOL);
7786 curwin->w_set_curswant = TRUE; /* so we stay at the end */
7787 }
7788 else
7789 vim_beep();
7790}
7791
7792 static void
7793ins_home(c)
7794 int c;
7795{
7796 pos_T tpos;
7797
7798#ifdef FEAT_FOLDING
7799 if ((fdo_flags & FDO_HOR) && KeyTyped)
7800 foldOpenCursor();
7801#endif
7802 undisplay_dollar();
7803 tpos = curwin->w_cursor;
7804 if (c == K_C_HOME)
7805 curwin->w_cursor.lnum = 1;
7806 curwin->w_cursor.col = 0;
7807#ifdef FEAT_VIRTUALEDIT
7808 curwin->w_cursor.coladd = 0;
7809#endif
7810 curwin->w_curswant = 0;
7811 start_arrow(&tpos);
7812}
7813
7814 static void
7815ins_end(c)
7816 int c;
7817{
7818 pos_T tpos;
7819
7820#ifdef FEAT_FOLDING
7821 if ((fdo_flags & FDO_HOR) && KeyTyped)
7822 foldOpenCursor();
7823#endif
7824 undisplay_dollar();
7825 tpos = curwin->w_cursor;
7826 if (c == K_C_END)
7827 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7828 coladvance((colnr_T)MAXCOL);
7829 curwin->w_curswant = MAXCOL;
7830
7831 start_arrow(&tpos);
7832}
7833
7834 static void
7835ins_s_left()
7836{
7837#ifdef FEAT_FOLDING
7838 if ((fdo_flags & FDO_HOR) && KeyTyped)
7839 foldOpenCursor();
7840#endif
7841 undisplay_dollar();
7842 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
7843 {
7844 start_arrow(&curwin->w_cursor);
7845 (void)bck_word(1L, FALSE, FALSE);
7846 curwin->w_set_curswant = TRUE;
7847 }
7848 else
7849 vim_beep();
7850}
7851
7852 static void
7853ins_right()
7854{
7855#ifdef FEAT_FOLDING
7856 if ((fdo_flags & FDO_HOR) && KeyTyped)
7857 foldOpenCursor();
7858#endif
7859 undisplay_dollar();
7860 if (gchar_cursor() != NUL || virtual_active()
7861 )
7862 {
7863 start_arrow(&curwin->w_cursor);
7864 curwin->w_set_curswant = TRUE;
7865#ifdef FEAT_VIRTUALEDIT
7866 if (virtual_active())
7867 oneright();
7868 else
7869#endif
7870 {
7871#ifdef FEAT_MBYTE
7872 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007873 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 else
7875#endif
7876 ++curwin->w_cursor.col;
7877 }
7878
7879#ifdef FEAT_RIGHTLEFT
7880 revins_legal++;
7881 if (revins_chars)
7882 revins_chars--;
7883#endif
7884 }
7885 /* if 'whichwrap' set for cursor in insert mode, may move the
7886 * cursor to the next line */
7887 else if (vim_strchr(p_ww, ']') != NULL
7888 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7889 {
7890 start_arrow(&curwin->w_cursor);
7891 curwin->w_set_curswant = TRUE;
7892 ++curwin->w_cursor.lnum;
7893 curwin->w_cursor.col = 0;
7894 }
7895 else
7896 vim_beep();
7897}
7898
7899 static void
7900ins_s_right()
7901{
7902#ifdef FEAT_FOLDING
7903 if ((fdo_flags & FDO_HOR) && KeyTyped)
7904 foldOpenCursor();
7905#endif
7906 undisplay_dollar();
7907 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
7908 || gchar_cursor() != NUL)
7909 {
7910 start_arrow(&curwin->w_cursor);
7911 (void)fwd_word(1L, FALSE, 0);
7912 curwin->w_set_curswant = TRUE;
7913 }
7914 else
7915 vim_beep();
7916}
7917
7918 static void
7919ins_up(startcol)
7920 int startcol; /* when TRUE move to Insstart.col */
7921{
7922 pos_T tpos;
7923 linenr_T old_topline = curwin->w_topline;
7924#ifdef FEAT_DIFF
7925 int old_topfill = curwin->w_topfill;
7926#endif
7927
7928 undisplay_dollar();
7929 tpos = curwin->w_cursor;
7930 if (cursor_up(1L, TRUE) == OK)
7931 {
7932 if (startcol)
7933 coladvance(getvcol_nolist(&Insstart));
7934 if (old_topline != curwin->w_topline
7935#ifdef FEAT_DIFF
7936 || old_topfill != curwin->w_topfill
7937#endif
7938 )
7939 redraw_later(VALID);
7940 start_arrow(&tpos);
7941#ifdef FEAT_CINDENT
7942 can_cindent = TRUE;
7943#endif
7944 }
7945 else
7946 vim_beep();
7947}
7948
7949 static void
7950ins_pageup()
7951{
7952 pos_T tpos;
7953
7954 undisplay_dollar();
7955 tpos = curwin->w_cursor;
7956 if (onepage(BACKWARD, 1L) == OK)
7957 {
7958 start_arrow(&tpos);
7959#ifdef FEAT_CINDENT
7960 can_cindent = TRUE;
7961#endif
7962 }
7963 else
7964 vim_beep();
7965}
7966
7967 static void
7968ins_down(startcol)
7969 int startcol; /* when TRUE move to Insstart.col */
7970{
7971 pos_T tpos;
7972 linenr_T old_topline = curwin->w_topline;
7973#ifdef FEAT_DIFF
7974 int old_topfill = curwin->w_topfill;
7975#endif
7976
7977 undisplay_dollar();
7978 tpos = curwin->w_cursor;
7979 if (cursor_down(1L, TRUE) == OK)
7980 {
7981 if (startcol)
7982 coladvance(getvcol_nolist(&Insstart));
7983 if (old_topline != curwin->w_topline
7984#ifdef FEAT_DIFF
7985 || old_topfill != curwin->w_topfill
7986#endif
7987 )
7988 redraw_later(VALID);
7989 start_arrow(&tpos);
7990#ifdef FEAT_CINDENT
7991 can_cindent = TRUE;
7992#endif
7993 }
7994 else
7995 vim_beep();
7996}
7997
7998 static void
7999ins_pagedown()
8000{
8001 pos_T tpos;
8002
8003 undisplay_dollar();
8004 tpos = curwin->w_cursor;
8005 if (onepage(FORWARD, 1L) == OK)
8006 {
8007 start_arrow(&tpos);
8008#ifdef FEAT_CINDENT
8009 can_cindent = TRUE;
8010#endif
8011 }
8012 else
8013 vim_beep();
8014}
8015
8016#ifdef FEAT_DND
8017 static void
8018ins_drop()
8019{
8020 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8021}
8022#endif
8023
8024/*
8025 * Handle TAB in Insert or Replace mode.
8026 * Return TRUE when the TAB needs to be inserted like a normal character.
8027 */
8028 static int
8029ins_tab()
8030{
8031 int ind;
8032 int i;
8033 int temp;
8034
8035 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8036 Insstart_blank_vcol = get_nolist_virtcol();
8037 if (echeck_abbr(TAB + ABBR_OFF))
8038 return FALSE;
8039
8040 ind = inindent(0);
8041#ifdef FEAT_CINDENT
8042 if (ind)
8043 can_cindent = FALSE;
8044#endif
8045
8046 /*
8047 * When nothing special, insert TAB like a normal character
8048 */
8049 if (!curbuf->b_p_et
8050 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8051 && curbuf->b_p_sts == 0)
8052 return TRUE;
8053
8054 if (stop_arrow() == FAIL)
8055 return TRUE;
8056
8057 did_ai = FALSE;
8058#ifdef FEAT_SMARTINDENT
8059 did_si = FALSE;
8060 can_si = FALSE;
8061 can_si_back = FALSE;
8062#endif
8063 AppendToRedobuff((char_u *)"\t");
8064
8065 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8066 temp = (int)curbuf->b_p_sw;
8067 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8068 temp = (int)curbuf->b_p_sts;
8069 else /* otherwise use 'tabstop' */
8070 temp = (int)curbuf->b_p_ts;
8071 temp -= get_nolist_virtcol() % temp;
8072
8073 /*
8074 * Insert the first space with ins_char(). It will delete one char in
8075 * replace mode. Insert the rest with ins_str(); it will not delete any
8076 * chars. For VREPLACE mode, we use ins_char() for all characters.
8077 */
8078 ins_char(' ');
8079 while (--temp > 0)
8080 {
8081#ifdef FEAT_VREPLACE
8082 if (State & VREPLACE_FLAG)
8083 ins_char(' ');
8084 else
8085#endif
8086 {
8087 ins_str((char_u *)" ");
8088 if (State & REPLACE_FLAG) /* no char replaced */
8089 replace_push(NUL);
8090 }
8091 }
8092
8093 /*
8094 * When 'expandtab' not set: Replace spaces by TABs where possible.
8095 */
8096 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8097 {
8098 char_u *ptr;
8099#ifdef FEAT_VREPLACE
8100 char_u *saved_line = NULL; /* init for GCC */
8101 pos_T pos;
8102#endif
8103 pos_T fpos;
8104 pos_T *cursor;
8105 colnr_T want_vcol, vcol;
8106 int change_col = -1;
8107 int save_list = curwin->w_p_list;
8108
8109 /*
8110 * Get the current line. For VREPLACE mode, don't make real changes
8111 * yet, just work on a copy of the line.
8112 */
8113#ifdef FEAT_VREPLACE
8114 if (State & VREPLACE_FLAG)
8115 {
8116 pos = curwin->w_cursor;
8117 cursor = &pos;
8118 saved_line = vim_strsave(ml_get_curline());
8119 if (saved_line == NULL)
8120 return FALSE;
8121 ptr = saved_line + pos.col;
8122 }
8123 else
8124#endif
8125 {
8126 ptr = ml_get_cursor();
8127 cursor = &curwin->w_cursor;
8128 }
8129
8130 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8131 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8132 curwin->w_p_list = FALSE;
8133
8134 /* Find first white before the cursor */
8135 fpos = curwin->w_cursor;
8136 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8137 {
8138 --fpos.col;
8139 --ptr;
8140 }
8141
8142 /* In Replace mode, don't change characters before the insert point. */
8143 if ((State & REPLACE_FLAG)
8144 && fpos.lnum == Insstart.lnum
8145 && fpos.col < Insstart.col)
8146 {
8147 ptr += Insstart.col - fpos.col;
8148 fpos.col = Insstart.col;
8149 }
8150
8151 /* compute virtual column numbers of first white and cursor */
8152 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8153 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8154
8155 /* Use as many TABs as possible. Beware of 'showbreak' and
8156 * 'linebreak' adding extra virtual columns. */
8157 while (vim_iswhite(*ptr))
8158 {
8159 i = lbr_chartabsize((char_u *)"\t", vcol);
8160 if (vcol + i > want_vcol)
8161 break;
8162 if (*ptr != TAB)
8163 {
8164 *ptr = TAB;
8165 if (change_col < 0)
8166 {
8167 change_col = fpos.col; /* Column of first change */
8168 /* May have to adjust Insstart */
8169 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8170 Insstart.col = fpos.col;
8171 }
8172 }
8173 ++fpos.col;
8174 ++ptr;
8175 vcol += i;
8176 }
8177
8178 if (change_col >= 0)
8179 {
8180 int repl_off = 0;
8181
8182 /* Skip over the spaces we need. */
8183 while (vcol < want_vcol && *ptr == ' ')
8184 {
8185 vcol += lbr_chartabsize(ptr, vcol);
8186 ++ptr;
8187 ++repl_off;
8188 }
8189 if (vcol > want_vcol)
8190 {
8191 /* Must have a char with 'showbreak' just before it. */
8192 --ptr;
8193 --repl_off;
8194 }
8195 fpos.col += repl_off;
8196
8197 /* Delete following spaces. */
8198 i = cursor->col - fpos.col;
8199 if (i > 0)
8200 {
8201 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8202 /* correct replace stack. */
8203 if ((State & REPLACE_FLAG)
8204#ifdef FEAT_VREPLACE
8205 && !(State & VREPLACE_FLAG)
8206#endif
8207 )
8208 for (temp = i; --temp >= 0; )
8209 replace_join(repl_off);
8210 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008211#ifdef FEAT_NETBEANS_INTG
8212 if (usingNetbeans)
8213 {
8214 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8215 (long)(i + 1));
8216 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8217 (char_u *)"\t", 1);
8218 }
8219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 cursor->col -= i;
8221
8222#ifdef FEAT_VREPLACE
8223 /*
8224 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8225 * backspacing over the changed spacing and then inserting the new
8226 * spacing.
8227 */
8228 if (State & VREPLACE_FLAG)
8229 {
8230 /* Backspace from real cursor to change_col */
8231 backspace_until_column(change_col);
8232
8233 /* Insert each char in saved_line from changed_col to
8234 * ptr-cursor */
8235 ins_bytes_len(saved_line + change_col,
8236 cursor->col - change_col);
8237 }
8238#endif
8239 }
8240
8241#ifdef FEAT_VREPLACE
8242 if (State & VREPLACE_FLAG)
8243 vim_free(saved_line);
8244#endif
8245 curwin->w_p_list = save_list;
8246 }
8247
8248 return FALSE;
8249}
8250
8251/*
8252 * Handle CR or NL in insert mode.
8253 * Return TRUE when out of memory or can't undo.
8254 */
8255 static int
8256ins_eol(c)
8257 int c;
8258{
8259 int i;
8260
8261 if (echeck_abbr(c + ABBR_OFF))
8262 return FALSE;
8263 if (stop_arrow() == FAIL)
8264 return TRUE;
8265 undisplay_dollar();
8266
8267 /*
8268 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8269 * character under the cursor. Only push a NUL on the replace stack,
8270 * nothing to put back when the NL is deleted.
8271 */
8272 if ((State & REPLACE_FLAG)
8273#ifdef FEAT_VREPLACE
8274 && !(State & VREPLACE_FLAG)
8275#endif
8276 )
8277 replace_push(NUL);
8278
8279 /*
8280 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8281 * replacing the next line, so we push all of the characters left on the
8282 * line onto the replace stack. This is not done here though, it is done
8283 * in open_line().
8284 */
8285
8286#ifdef FEAT_RIGHTLEFT
8287# ifdef FEAT_FKMAP
8288 if (p_altkeymap && p_fkmap)
8289 fkmap(NL);
8290# endif
8291 /* NL in reverse insert will always start in the end of
8292 * current line. */
8293 if (revins_on)
8294 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8295#endif
8296
8297 AppendToRedobuff(NL_STR);
8298 i = open_line(FORWARD,
8299#ifdef FEAT_COMMENTS
8300 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8301#endif
8302 0, old_indent);
8303 old_indent = 0;
8304#ifdef FEAT_CINDENT
8305 can_cindent = TRUE;
8306#endif
8307
8308 return (!i);
8309}
8310
8311#ifdef FEAT_DIGRAPHS
8312/*
8313 * Handle digraph in insert mode.
8314 * Returns character still to be inserted, or NUL when nothing remaining to be
8315 * done.
8316 */
8317 static int
8318ins_digraph()
8319{
8320 int c;
8321 int cc;
8322
8323 pc_status = PC_STATUS_UNSET;
8324 if (redrawing() && !char_avail())
8325 {
8326 /* may need to redraw when no more chars available now */
8327 ins_redraw();
8328
8329 edit_putchar('?', TRUE);
8330#ifdef FEAT_CMDL_INFO
8331 add_to_showcmd_c(Ctrl_K);
8332#endif
8333 }
8334
8335#ifdef USE_ON_FLY_SCROLL
8336 dont_scroll = TRUE; /* disallow scrolling here */
8337#endif
8338
8339 /* don't map the digraph chars. This also prevents the
8340 * mode message to be deleted when ESC is hit */
8341 ++no_mapping;
8342 ++allow_keys;
8343 c = safe_vgetc();
8344 --no_mapping;
8345 --allow_keys;
8346 if (IS_SPECIAL(c) || mod_mask) /* special key */
8347 {
8348#ifdef FEAT_CMDL_INFO
8349 clear_showcmd();
8350#endif
8351 insert_special(c, TRUE, FALSE);
8352 return NUL;
8353 }
8354 if (c != ESC)
8355 {
8356 if (redrawing() && !char_avail())
8357 {
8358 /* may need to redraw when no more chars available now */
8359 ins_redraw();
8360
8361 if (char2cells(c) == 1)
8362 {
8363 /* first remove the '?', otherwise it's restored when typing
8364 * an ESC next */
8365 edit_unputchar();
8366 ins_redraw();
8367 edit_putchar(c, TRUE);
8368 }
8369#ifdef FEAT_CMDL_INFO
8370 add_to_showcmd_c(c);
8371#endif
8372 }
8373 ++no_mapping;
8374 ++allow_keys;
8375 cc = safe_vgetc();
8376 --no_mapping;
8377 --allow_keys;
8378 if (cc != ESC)
8379 {
8380 AppendToRedobuff((char_u *)CTRL_V_STR);
8381 c = getdigraph(c, cc, TRUE);
8382#ifdef FEAT_CMDL_INFO
8383 clear_showcmd();
8384#endif
8385 return c;
8386 }
8387 }
8388 edit_unputchar();
8389#ifdef FEAT_CMDL_INFO
8390 clear_showcmd();
8391#endif
8392 return NUL;
8393}
8394#endif
8395
8396/*
8397 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8398 * Returns the char to be inserted, or NUL if none found.
8399 */
8400 static int
8401ins_copychar(lnum)
8402 linenr_T lnum;
8403{
8404 int c;
8405 int temp;
8406 char_u *ptr, *prev_ptr;
8407
8408 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8409 {
8410 vim_beep();
8411 return NUL;
8412 }
8413
8414 /* try to advance to the cursor column */
8415 temp = 0;
8416 ptr = ml_get(lnum);
8417 prev_ptr = ptr;
8418 validate_virtcol();
8419 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8420 {
8421 prev_ptr = ptr;
8422 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8423 }
8424 if ((colnr_T)temp > curwin->w_virtcol)
8425 ptr = prev_ptr;
8426
8427#ifdef FEAT_MBYTE
8428 c = (*mb_ptr2char)(ptr);
8429#else
8430 c = *ptr;
8431#endif
8432 if (c == NUL)
8433 vim_beep();
8434 return c;
8435}
8436
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008437/*
8438 * CTRL-Y or CTRL-E typed in Insert mode.
8439 */
8440 static int
8441ins_ctrl_ey(tc)
8442 int tc;
8443{
8444 int c = tc;
8445
8446#ifdef FEAT_INS_EXPAND
8447 if (ctrl_x_mode == CTRL_X_SCROLL)
8448 {
8449 if (c == Ctrl_Y)
8450 scrolldown_clamp();
8451 else
8452 scrollup_clamp();
8453 redraw_later(VALID);
8454 }
8455 else
8456#endif
8457 {
8458 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8459 if (c != NUL)
8460 {
8461 long tw_save;
8462
8463 /* The character must be taken literally, insert like it
8464 * was typed after a CTRL-V, and pretend 'textwidth'
8465 * wasn't set. Digits, 'o' and 'x' are special after a
8466 * CTRL-V, don't use it for these. */
8467 if (c < 256 && !isalnum(c))
8468 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8469 tw_save = curbuf->b_p_tw;
8470 curbuf->b_p_tw = -1;
8471 insert_special(c, TRUE, FALSE);
8472 curbuf->b_p_tw = tw_save;
8473#ifdef FEAT_RIGHTLEFT
8474 revins_chars++;
8475 revins_legal++;
8476#endif
8477 c = Ctrl_V; /* pretend CTRL-V is last character */
8478 auto_format(FALSE, TRUE);
8479 }
8480 }
8481 return c;
8482}
8483
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484#ifdef FEAT_SMARTINDENT
8485/*
8486 * Try to do some very smart auto-indenting.
8487 * Used when inserting a "normal" character.
8488 */
8489 static void
8490ins_try_si(c)
8491 int c;
8492{
8493 pos_T *pos, old_pos;
8494 char_u *ptr;
8495 int i;
8496 int temp;
8497
8498 /*
8499 * do some very smart indenting when entering '{' or '}'
8500 */
8501 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8502 {
8503 /*
8504 * for '}' set indent equal to indent of line containing matching '{'
8505 */
8506 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8507 {
8508 old_pos = curwin->w_cursor;
8509 /*
8510 * If the matching '{' has a ')' immediately before it (ignoring
8511 * white-space), then line up with the start of the line
8512 * containing the matching '(' if there is one. This handles the
8513 * case where an "if (..\n..) {" statement continues over multiple
8514 * lines -- webb
8515 */
8516 ptr = ml_get(pos->lnum);
8517 i = pos->col;
8518 if (i > 0) /* skip blanks before '{' */
8519 while (--i > 0 && vim_iswhite(ptr[i]))
8520 ;
8521 curwin->w_cursor.lnum = pos->lnum;
8522 curwin->w_cursor.col = i;
8523 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8524 curwin->w_cursor = *pos;
8525 i = get_indent();
8526 curwin->w_cursor = old_pos;
8527#ifdef FEAT_VREPLACE
8528 if (State & VREPLACE_FLAG)
8529 change_indent(INDENT_SET, i, FALSE, NUL);
8530 else
8531#endif
8532 (void)set_indent(i, SIN_CHANGED);
8533 }
8534 else if (curwin->w_cursor.col > 0)
8535 {
8536 /*
8537 * when inserting '{' after "O" reduce indent, but not
8538 * more than indent of previous line
8539 */
8540 temp = TRUE;
8541 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8542 {
8543 old_pos = curwin->w_cursor;
8544 i = get_indent();
8545 while (curwin->w_cursor.lnum > 1)
8546 {
8547 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8548
8549 /* ignore empty lines and lines starting with '#'. */
8550 if (*ptr != '#' && *ptr != NUL)
8551 break;
8552 }
8553 if (get_indent() >= i)
8554 temp = FALSE;
8555 curwin->w_cursor = old_pos;
8556 }
8557 if (temp)
8558 shift_line(TRUE, FALSE, 1);
8559 }
8560 }
8561
8562 /*
8563 * set indent of '#' always to 0
8564 */
8565 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8566 {
8567 /* remember current indent for next line */
8568 old_indent = get_indent();
8569 (void)set_indent(0, SIN_CHANGED);
8570 }
8571
8572 /* Adjust ai_col, the char at this position can be deleted. */
8573 if (ai_col > curwin->w_cursor.col)
8574 ai_col = curwin->w_cursor.col;
8575}
8576#endif
8577
8578/*
8579 * Get the value that w_virtcol would have when 'list' is off.
8580 * Unless 'cpo' contains the 'L' flag.
8581 */
8582 static colnr_T
8583get_nolist_virtcol()
8584{
8585 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8586 return getvcol_nolist(&curwin->w_cursor);
8587 validate_virtcol();
8588 return curwin->w_virtcol;
8589}