blob: ff09f09c838a046f96f2b14e8f13f272445ff636 [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 */
71 char_u *cp_fname; /* file containing the match */
72 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
73 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000074};
75
Bram Moolenaar572cb562005-08-05 21:35:02 +000076#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077#define FREE_FNAME (2)
78
79/*
80 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000081 * "compl_first_match" points to the start of the list.
82 * "compl_curr_match" points to the currently selected entry.
83 * "compl_shown_match" is different from compl_curr_match during
84 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000086static compl_T *compl_first_match = NULL;
87static compl_T *compl_curr_match = NULL;
88static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000089
Bram Moolenaar4be06f92005-07-29 22:36:03 +000090/* When the first completion is done "compl_started" is set. When it's
91 * FALSE the word to be completed must be located. */
92static int compl_started = FALSE;
93
Bram Moolenaar572cb562005-08-05 21:35:02 +000094static int compl_matches = 0;
95static char_u *compl_pattern = NULL;
96static int compl_direction = FORWARD;
97static int compl_shows_dir = FORWARD;
98static int compl_pending = FALSE;
99static pos_T compl_startpos;
100static colnr_T compl_col = 0; /* column where the text starts
101 * that is being completed */
102static int save_sm = -1;
103static char_u *compl_orig_text = NULL; /* text as it was before
104 * completion started */
105static int compl_cont_mode = 0;
106static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000108static void ins_ctrl_x __ARGS((void));
109static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int dir));
111static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000112static void ins_compl_upd_pum __ARGS((void));
113static void ins_compl_del_pum __ARGS((void));
114static int pum_wanted __ARGS((void));
115static void ins_compl_show_pum __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int dir, int flags, int thesaurus));
117static void ins_compl_free __ARGS((void));
118static void ins_compl_clear __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000119static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
121static int ins_compl_get_exp __ARGS((pos_T *ini, int dir));
122static void ins_compl_delete __ARGS((void));
123static void ins_compl_insert __ARGS((void));
124static int ins_compl_next __ARGS((int allow_get_expansion));
125static int ins_complete __ARGS((int c));
126static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
127#endif /* FEAT_INS_EXPAND */
128
129#define BACKSPACE_CHAR 1
130#define BACKSPACE_WORD 2
131#define BACKSPACE_WORD_NOT_SPACE 3
132#define BACKSPACE_LINE 4
133
134static void ins_redraw __ARGS((void));
135static void ins_ctrl_v __ARGS((void));
136static void undisplay_dollar __ARGS((void));
137static void insert_special __ARGS((int, int, int));
138static void check_auto_format __ARGS((int));
139static void redo_literal __ARGS((int c));
140static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000141#ifdef FEAT_SYN_HL
142static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000143static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000144static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
147static int echeck_abbr __ARGS((int));
148static void replace_push_off __ARGS((int c));
149static int replace_pop __ARGS((void));
150static void replace_join __ARGS((int off));
151static void replace_pop_ins __ARGS((void));
152#ifdef FEAT_MBYTE
153static void mb_replace_pop_ins __ARGS((int cc));
154#endif
155static void replace_flush __ARGS((void));
156static void replace_do_bs __ARGS((void));
157#ifdef FEAT_CINDENT
158static int cindent_on __ARGS((void));
159#endif
160static void ins_reg __ARGS((void));
161static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000162static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000163static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#ifdef FEAT_RIGHTLEFT
165static void ins_ctrl_ __ARGS((void));
166#endif
167#ifdef FEAT_VISUAL
168static int ins_start_select __ARGS((int c));
169#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000170static void ins_insert __ARGS((int replaceState));
171static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172static void ins_shift __ARGS((int c, int lastc));
173static void ins_del __ARGS((void));
174static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
175#ifdef FEAT_MOUSE
176static void ins_mouse __ARGS((int c));
177static void ins_mousescroll __ARGS((int up));
178#endif
179static void ins_left __ARGS((void));
180static void ins_home __ARGS((int c));
181static void ins_end __ARGS((int c));
182static void ins_s_left __ARGS((void));
183static void ins_right __ARGS((void));
184static void ins_s_right __ARGS((void));
185static void ins_up __ARGS((int startcol));
186static void ins_pageup __ARGS((void));
187static void ins_down __ARGS((int startcol));
188static void ins_pagedown __ARGS((void));
189#ifdef FEAT_DND
190static void ins_drop __ARGS((void));
191#endif
192static int ins_tab __ARGS((void));
193static int ins_eol __ARGS((int c));
194#ifdef FEAT_DIGRAPHS
195static int ins_digraph __ARGS((void));
196#endif
197static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000198static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199#ifdef FEAT_SMARTINDENT
200static void ins_try_si __ARGS((int c));
201#endif
202static colnr_T get_nolist_virtcol __ARGS((void));
203
204static colnr_T Insstart_textlen; /* length of line when insert started */
205static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
206
207static char_u *last_insert = NULL; /* the text of the previous insert,
208 K_SPECIAL and CSI are escaped */
209static int last_insert_skip; /* nr of chars in front of previous insert */
210static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000211static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
213#ifdef FEAT_CINDENT
214static int can_cindent; /* may do cindenting on this line */
215#endif
216
217static int old_indent = 0; /* for ^^D command in insert mode */
218
219#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000220static int revins_on; /* reverse insert mode on */
221static int revins_chars; /* how much to skip after edit */
222static int revins_legal; /* was the last char 'legal'? */
223static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#endif
225
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226static int ins_need_undo; /* call u_save() before inserting a
227 char. Set when edit() is called.
228 after that arrow_used is used. */
229
230static int did_add_space = FALSE; /* auto_format() added an extra space
231 under the cursor */
232
233/*
234 * edit(): Start inserting text.
235 *
236 * "cmdchar" can be:
237 * 'i' normal insert command
238 * 'a' normal append command
239 * 'R' replace command
240 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
241 * but still only one <CR> is inserted. The <Esc> is not used for redo.
242 * 'g' "gI" command.
243 * 'V' "gR" command for Virtual Replace mode.
244 * 'v' "gr" command for single character Virtual Replace mode.
245 *
246 * This function is not called recursively. For CTRL-O commands, it returns
247 * and lets the caller handle the Normal-mode command.
248 *
249 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
250 */
251 int
252edit(cmdchar, startln, count)
253 int cmdchar;
254 int startln; /* if set, insert at start of line */
255 long count;
256{
257 int c = 0;
258 char_u *ptr;
259 int lastc;
260 colnr_T mincol;
261 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 int i;
263 int did_backspace = TRUE; /* previous char was backspace */
264#ifdef FEAT_CINDENT
265 int line_is_white = FALSE; /* line is empty before insert */
266#endif
267 linenr_T old_topline = 0; /* topline before insertion */
268#ifdef FEAT_DIFF
269 int old_topfill = -1;
270#endif
271 int inserted_space = FALSE; /* just inserted a space */
272 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000273 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000275 /* Remember whether editing was restarted after CTRL-O. */
276 did_restart_edit = restart_edit;
277
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 /* sleep before redrawing, needed for "CTRL-O :" that results in an
279 * error message */
280 check_for_delay(TRUE);
281
282#ifdef HAVE_SANDBOX
283 /* Don't allow inserting in the sandbox. */
284 if (sandbox != 0)
285 {
286 EMSG(_(e_sandbox));
287 return FALSE;
288 }
289#endif
290
291#ifdef FEAT_INS_EXPAND
292 ins_compl_clear(); /* clear stuff for CTRL-X mode */
293#endif
294
Bram Moolenaar843ee412004-06-30 16:16:41 +0000295#ifdef FEAT_AUTOCMD
296 /*
297 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
298 */
299 if (cmdchar != 'r' && cmdchar != 'v')
300 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000301# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000302 if (cmdchar == 'R')
303 ptr = (char_u *)"r";
304 else if (cmdchar == 'V')
305 ptr = (char_u *)"v";
306 else
307 ptr = (char_u *)"i";
308 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000309# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000310 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
311 }
312#endif
313
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#ifdef FEAT_MOUSE
315 /*
316 * When doing a paste with the middle mouse button, Insstart is set to
317 * where the paste started.
318 */
319 if (where_paste_started.lnum != 0)
320 Insstart = where_paste_started;
321 else
322#endif
323 {
324 Insstart = curwin->w_cursor;
325 if (startln)
326 Insstart.col = 0;
327 }
328 Insstart_textlen = linetabsize(ml_get_curline());
329 Insstart_blank_vcol = MAXCOL;
330 if (!did_ai)
331 ai_col = 0;
332
333 if (cmdchar != NUL && restart_edit == 0)
334 {
335 ResetRedobuff();
336 AppendNumberToRedobuff(count);
337#ifdef FEAT_VREPLACE
338 if (cmdchar == 'V' || cmdchar == 'v')
339 {
340 /* "gR" or "gr" command */
341 AppendCharToRedobuff('g');
342 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
343 }
344 else
345#endif
346 {
347 AppendCharToRedobuff(cmdchar);
348 if (cmdchar == 'g') /* "gI" command */
349 AppendCharToRedobuff('I');
350 else if (cmdchar == 'r') /* "r<CR>" command */
351 count = 1; /* insert only one <CR> */
352 }
353 }
354
355 if (cmdchar == 'R')
356 {
357#ifdef FEAT_FKMAP
358 if (p_fkmap && p_ri)
359 {
360 beep_flush();
361 EMSG(farsi_text_3); /* encoded in Farsi */
362 State = INSERT;
363 }
364 else
365#endif
366 State = REPLACE;
367 }
368#ifdef FEAT_VREPLACE
369 else if (cmdchar == 'V' || cmdchar == 'v')
370 {
371 State = VREPLACE;
372 replaceState = VREPLACE;
373 orig_line_count = curbuf->b_ml.ml_line_count;
374 vr_lines_changed = 1;
375 }
376#endif
377 else
378 State = INSERT;
379
380 stop_insert_mode = FALSE;
381
382 /*
383 * Need to recompute the cursor position, it might move when the cursor is
384 * on a TAB or special character.
385 */
386 curs_columns(TRUE);
387
388 /*
389 * Enable langmap or IME, indicated by 'iminsert'.
390 * Note that IME may enabled/disabled without us noticing here, thus the
391 * 'iminsert' value may not reflect what is actually used. It is updated
392 * when hitting <Esc>.
393 */
394 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
395 State |= LANGMAP;
396#ifdef USE_IM_CONTROL
397 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
398#endif
399
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400#ifdef FEAT_MOUSE
401 setmouse();
402#endif
403#ifdef FEAT_CMDL_INFO
404 clear_showcmd();
405#endif
406#ifdef FEAT_RIGHTLEFT
407 /* there is no reverse replace mode */
408 revins_on = (State == INSERT && p_ri);
409 if (revins_on)
410 undisplay_dollar();
411 revins_chars = 0;
412 revins_legal = 0;
413 revins_scol = -1;
414#endif
415
416 /*
417 * Handle restarting Insert mode.
418 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
419 * restart_edit non-zero, and something in the stuff buffer.
420 */
421 if (restart_edit != 0 && stuff_empty())
422 {
423#ifdef FEAT_MOUSE
424 /*
425 * After a paste we consider text typed to be part of the insert for
426 * the pasted text. You can backspace over the pasted text too.
427 */
428 if (where_paste_started.lnum)
429 arrow_used = FALSE;
430 else
431#endif
432 arrow_used = TRUE;
433 restart_edit = 0;
434
435 /*
436 * If the cursor was after the end-of-line before the CTRL-O and it is
437 * now at the end-of-line, put it after the end-of-line (this is not
438 * correct in very rare cases).
439 * Also do this if curswant is greater than the current virtual
440 * column. Eg after "^O$" or "^O80|".
441 */
442 validate_virtcol();
443 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000444 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 || curwin->w_curswant > curwin->w_virtcol)
446 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
447 {
448 if (ptr[1] == NUL)
449 ++curwin->w_cursor.col;
450#ifdef FEAT_MBYTE
451 else if (has_mbyte)
452 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000453 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (ptr[i] == NUL)
455 curwin->w_cursor.col += i;
456 }
457#endif
458 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000459 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 }
461 else
462 arrow_used = FALSE;
463
464 /* we are in insert mode now, don't need to start it anymore */
465 need_start_insertmode = FALSE;
466
467 /* Need to save the line for undo before inserting the first char. */
468 ins_need_undo = TRUE;
469
470#ifdef FEAT_MOUSE
471 where_paste_started.lnum = 0;
472#endif
473#ifdef FEAT_CINDENT
474 can_cindent = TRUE;
475#endif
476#ifdef FEAT_FOLDING
477 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
478 * restarting. */
479 if (!p_im && did_restart_edit == 0)
480 foldOpenCursor();
481#endif
482
483 /*
484 * If 'showmode' is set, show the current (insert/replace/..) mode.
485 * A warning message for changing a readonly file is given here, before
486 * actually changing anything. It's put after the mode, if any.
487 */
488 i = 0;
489 if (p_smd)
490 i = showmode();
491
492 if (!p_im && did_restart_edit == 0)
493 change_warning(i + 1);
494
495#ifdef CURSOR_SHAPE
496 ui_cursor_shape(); /* may show different cursor shape */
497#endif
498#ifdef FEAT_DIGRAPHS
499 do_digraph(-1); /* clear digraphs */
500#endif
501
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000502 /*
503 * Get the current length of the redo buffer, those characters have to be
504 * skipped if we want to get to the inserted characters.
505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 ptr = get_inserted();
507 if (ptr == NULL)
508 new_insert_skip = 0;
509 else
510 {
511 new_insert_skip = (int)STRLEN(ptr);
512 vim_free(ptr);
513 }
514
515 old_indent = 0;
516
517 /*
518 * Main loop in Insert mode: repeat until Insert mode is left.
519 */
520 for (;;)
521 {
522#ifdef FEAT_RIGHTLEFT
523 if (!revins_legal)
524 revins_scol = -1; /* reset on illegal motions */
525 else
526 revins_legal = 0;
527#endif
528 if (arrow_used) /* don't repeat insert when arrow key used */
529 count = 0;
530
531 if (stop_insert_mode)
532 {
533 /* ":stopinsert" used or 'insertmode' reset */
534 count = 0;
535 goto doESCkey;
536 }
537
538 /* set curwin->w_curswant for next K_DOWN or K_UP */
539 if (!arrow_used)
540 curwin->w_set_curswant = TRUE;
541
542 /* If there is no typeahead may check for timestamps (e.g., for when a
543 * menu invoked a shell command). */
544 if (stuff_empty())
545 {
546 did_check_timestamps = FALSE;
547 if (need_check_timestamps)
548 check_timestamps(FALSE);
549 }
550
551 /*
552 * When emsg() was called msg_scroll will have been set.
553 */
554 msg_scroll = FALSE;
555
556#ifdef FEAT_GUI
557 /* When 'mousefocus' is set a mouse movement may have taken us to
558 * another window. "need_mouse_correct" may then be set because of an
559 * autocommand. */
560 if (need_mouse_correct)
561 gui_mouse_correct();
562#endif
563
564#ifdef FEAT_FOLDING
565 /* Open fold at the cursor line, according to 'foldopen'. */
566 if (fdo_flags & FDO_INSERT)
567 foldOpenCursor();
568 /* Close folds where the cursor isn't, according to 'foldclose' */
569 if (!char_avail())
570 foldCheckClose();
571#endif
572
573 /*
574 * If we inserted a character at the last position of the last line in
575 * the window, scroll the window one line up. This avoids an extra
576 * redraw.
577 * This is detected when the cursor column is smaller after inserting
578 * something.
579 * Don't do this when the topline changed already, it has
580 * already been adjusted (by insertchar() calling open_line())).
581 */
582 if (curbuf->b_mod_set
583 && curwin->w_p_wrap
584 && !did_backspace
585 && curwin->w_topline == old_topline
586#ifdef FEAT_DIFF
587 && curwin->w_topfill == old_topfill
588#endif
589 )
590 {
591 mincol = curwin->w_wcol;
592 validate_cursor_col();
593
594 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
595 && curwin->w_wrow == W_WINROW(curwin)
596 + curwin->w_height - 1 - p_so
597 && (curwin->w_cursor.lnum != curwin->w_topline
598#ifdef FEAT_DIFF
599 || curwin->w_topfill > 0
600#endif
601 ))
602 {
603#ifdef FEAT_DIFF
604 if (curwin->w_topfill > 0)
605 --curwin->w_topfill;
606 else
607#endif
608#ifdef FEAT_FOLDING
609 if (hasFolding(curwin->w_topline, NULL, &old_topline))
610 set_topline(curwin, old_topline + 1);
611 else
612#endif
613 set_topline(curwin, curwin->w_topline + 1);
614 }
615 }
616
617 /* May need to adjust w_topline to show the cursor. */
618 update_topline();
619
620 did_backspace = FALSE;
621
622 validate_cursor(); /* may set must_redraw */
623
624 /*
625 * Redraw the display when no characters are waiting.
626 * Also shows mode, ruler and positions cursor.
627 */
628 ins_redraw();
629
630#ifdef FEAT_SCROLLBIND
631 if (curwin->w_p_scb)
632 do_check_scrollbind(TRUE);
633#endif
634
635 update_curswant();
636 old_topline = curwin->w_topline;
637#ifdef FEAT_DIFF
638 old_topfill = curwin->w_topfill;
639#endif
640
641#ifdef USE_ON_FLY_SCROLL
642 dont_scroll = FALSE; /* allow scrolling here */
643#endif
644
645 /*
646 * Get a character for Insert mode.
647 */
648 lastc = c; /* remember previous char for CTRL-D */
649 c = safe_vgetc();
650
651#ifdef FEAT_RIGHTLEFT
652 if (p_hkmap && KeyTyped)
653 c = hkmap(c); /* Hebrew mode mapping */
654#endif
655#ifdef FEAT_FKMAP
656 if (p_fkmap && KeyTyped)
657 c = fkmap(c); /* Farsi mode mapping */
658#endif
659
660#ifdef FEAT_INS_EXPAND
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000661 /* When the popup menu is visible cursor keys change the selection. */
662 if (c == K_UP && pum_visible())
663 c = Ctrl_P;
664 if (c == K_DOWN && pum_visible())
665 c = Ctrl_N;
666
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
668 * it does fix up the text when finishing completion. */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000669 if (c != K_IGNORE)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000670 {
671 if (ins_compl_prep(c))
672 continue;
673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674#endif
675
Bram Moolenaar488c6512005-08-11 20:09:58 +0000676 /* CTRL-\ CTRL-N goes to Normal mode,
677 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
678 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 if (c == Ctrl_BSL)
680 {
681 /* may need to redraw when no more chars available now */
682 ins_redraw();
683 ++no_mapping;
684 ++allow_keys;
685 c = safe_vgetc();
686 --no_mapping;
687 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000688 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000690 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 vungetc(c);
692 c = Ctrl_BSL;
693 }
694 else if (c == Ctrl_G && p_im)
695 continue;
696 else
697 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000698 if (c == Ctrl_O)
699 {
700 ins_ctrl_o();
701 ins_at_eol = FALSE; /* cursor keeps its column */
702 nomove = TRUE;
703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 count = 0;
705 goto doESCkey;
706 }
707 }
708
709#ifdef FEAT_DIGRAPHS
710 c = do_digraph(c);
711#endif
712
713#ifdef FEAT_INS_EXPAND
714 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
715 goto docomplete;
716#endif
717 if (c == Ctrl_V || c == Ctrl_Q)
718 {
719 ins_ctrl_v();
720 c = Ctrl_V; /* pretend CTRL-V is last typed character */
721 continue;
722 }
723
724#ifdef FEAT_CINDENT
725 if (cindent_on()
726# ifdef FEAT_INS_EXPAND
727 && ctrl_x_mode == 0
728# endif
729 )
730 {
731 /* A key name preceded by a bang means this key is not to be
732 * inserted. Skip ahead to the re-indenting below.
733 * A key name preceded by a star means that indenting has to be
734 * done before inserting the key. */
735 line_is_white = inindent(0);
736 if (in_cinkeys(c, '!', line_is_white))
737 goto force_cindent;
738 if (can_cindent && in_cinkeys(c, '*', line_is_white)
739 && stop_arrow() == OK)
740 do_c_expr_indent();
741 }
742#endif
743
744#ifdef FEAT_RIGHTLEFT
745 if (curwin->w_p_rl)
746 switch (c)
747 {
748 case K_LEFT: c = K_RIGHT; break;
749 case K_S_LEFT: c = K_S_RIGHT; break;
750 case K_C_LEFT: c = K_C_RIGHT; break;
751 case K_RIGHT: c = K_LEFT; break;
752 case K_S_RIGHT: c = K_S_LEFT; break;
753 case K_C_RIGHT: c = K_C_LEFT; break;
754 }
755#endif
756
757#ifdef FEAT_VISUAL
758 /*
759 * If 'keymodel' contains "startsel", may start selection. If it
760 * does, a CTRL-O and c will be stuffed, we need to get these
761 * characters.
762 */
763 if (ins_start_select(c))
764 continue;
765#endif
766
767 /*
768 * The big switch to handle a character in insert mode.
769 */
770 switch (c)
771 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000772 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 if (echeck_abbr(ESC + ABBR_OFF))
774 break;
775 /*FALLTHROUGH*/
776
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000777 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778#ifdef FEAT_CMDWIN
779 if (c == Ctrl_C && cmdwin_type != 0)
780 {
781 /* Close the cmdline window. */
782 cmdwin_result = K_IGNORE;
783 got_int = FALSE; /* don't stop executing autocommands et al. */
784 goto doESCkey;
785 }
786#endif
787
788#ifdef UNIX
789do_intr:
790#endif
791 /* when 'insertmode' set, and not halfway a mapping, don't leave
792 * Insert mode */
793 if (goto_im())
794 {
795 if (got_int)
796 {
797 (void)vgetc(); /* flush all buffers */
798 got_int = FALSE;
799 }
800 else
801 vim_beep();
802 break;
803 }
804doESCkey:
805 /*
806 * This is the ONLY return from edit()!
807 */
808 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
809 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000810 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 o_lnum = curwin->w_cursor.lnum;
812
Bram Moolenaar488c6512005-08-11 20:09:58 +0000813 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000814 {
815#ifdef FEAT_AUTOCMD
816 if (cmdchar != 'r' && cmdchar != 'v')
817 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
818 FALSE, curbuf);
819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 continue;
823
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000824 case Ctrl_Z: /* suspend when 'insertmode' set */
825 if (!p_im)
826 goto normalchar; /* insert CTRL-Z as normal char */
827 stuffReadbuff((char_u *)":st\r");
828 c = Ctrl_O;
829 /*FALLTHROUGH*/
830
831 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000832#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000833 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000834 goto docomplete;
835#endif
836 if (echeck_abbr(Ctrl_O + ABBR_OFF))
837 break;
838 ins_ctrl_o();
839 count = 0;
840 goto doESCkey;
841
Bram Moolenaar572cb562005-08-05 21:35:02 +0000842 case K_INS: /* toggle insert/replace mode */
843 case K_KINS:
844 ins_insert(replaceState);
845 break;
846
847 case K_SELECT: /* end of Select mode mapping - ignore */
848 break;
849
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000850#ifdef FEAT_SNIFF
851 case K_SNIFF: /* Sniff command received */
852 stuffcharReadbuff(K_SNIFF);
853 goto doESCkey;
854#endif
855
856 case K_HELP: /* Help key works like <ESC> <Help> */
857 case K_F1:
858 case K_XF1:
859 stuffcharReadbuff(K_HELP);
860 if (p_im)
861 need_start_insertmode = TRUE;
862 goto doESCkey;
863
864#ifdef FEAT_NETBEANS_INTG
865 case K_F21: /* NetBeans command */
866 ++no_mapping; /* don't map the next key hits */
867 i = safe_vgetc();
868 --no_mapping;
869 netbeans_keycommand(i);
870 break;
871#endif
872
873 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 case NUL:
875 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000876 /* For ^@ the trailing ESC will end the insert, unless there is an
877 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
879 && c != Ctrl_A && !p_im)
880 goto doESCkey; /* quit insert mode */
881 inserted_space = FALSE;
882 break;
883
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000884 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 ins_reg();
886 auto_format(FALSE, TRUE);
887 inserted_space = FALSE;
888 break;
889
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000890 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 ins_ctrl_g();
892 break;
893
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000894 case Ctrl_HAT: /* switch input mode and/or langmap */
895 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 break;
897
898#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000899 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 if (!p_ari)
901 goto normalchar;
902 ins_ctrl_();
903 break;
904#endif
905
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000906 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
908 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
909 goto docomplete;
910#endif
911 /* FALLTHROUGH */
912
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000913 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914# ifdef FEAT_INS_EXPAND
915 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
916 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000917 if (has_compl_option(FALSE))
918 goto docomplete;
919 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
921# endif
922 ins_shift(c, lastc);
923 auto_format(FALSE, TRUE);
924 inserted_space = FALSE;
925 break;
926
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000927 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 case K_KDEL:
929 ins_del();
930 auto_format(FALSE, TRUE);
931 break;
932
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000933 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 case Ctrl_H:
935 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
936 auto_format(FALSE, TRUE);
937 break;
938
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000939 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
941 auto_format(FALSE, TRUE);
942 break;
943
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000944 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000945# ifdef FEAT_COMPL_FUNC
946 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000947 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000948 goto docomplete;
949# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
951 auto_format(FALSE, TRUE);
952 inserted_space = FALSE;
953 break;
954
955#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000956 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 case K_LEFTMOUSE_NM:
958 case K_LEFTDRAG:
959 case K_LEFTRELEASE:
960 case K_LEFTRELEASE_NM:
961 case K_MIDDLEMOUSE:
962 case K_MIDDLEDRAG:
963 case K_MIDDLERELEASE:
964 case K_RIGHTMOUSE:
965 case K_RIGHTDRAG:
966 case K_RIGHTRELEASE:
967 case K_X1MOUSE:
968 case K_X1DRAG:
969 case K_X1RELEASE:
970 case K_X2MOUSE:
971 case K_X2DRAG:
972 case K_X2RELEASE:
973 ins_mouse(c);
974 break;
975
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000976 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 ins_mousescroll(FALSE);
978 break;
979
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000980 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 ins_mousescroll(TRUE);
982 break;
983#endif
984
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000985 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 break;
987
988#ifdef FEAT_GUI
989 case K_VER_SCROLLBAR:
990 ins_scroll();
991 break;
992
993 case K_HOR_SCROLLBAR:
994 ins_horscroll();
995 break;
996#endif
997
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000998 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 case K_S_HOME:
1001 case K_C_HOME:
1002 ins_home(c);
1003 break;
1004
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001005 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 case K_S_END:
1008 case K_C_END:
1009 ins_end(c);
1010 break;
1011
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001012 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001013 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1014 ins_s_left();
1015 else
1016 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 break;
1018
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001019 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 case K_C_LEFT:
1021 ins_s_left();
1022 break;
1023
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001024 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001025 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1026 ins_s_right();
1027 else
1028 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 break;
1030
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001031 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 case K_C_RIGHT:
1033 ins_s_right();
1034 break;
1035
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001036 case K_UP: /* <Up> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001037 if (mod_mask & MOD_MASK_SHIFT)
1038 ins_pageup();
1039 else
1040 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 break;
1042
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001043 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 case K_PAGEUP:
1045 case K_KPAGEUP:
1046 ins_pageup();
1047 break;
1048
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 case K_DOWN: /* <Down> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001050 if (mod_mask & MOD_MASK_SHIFT)
1051 ins_pagedown();
1052 else
1053 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 break;
1055
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001056 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 case K_PAGEDOWN:
1058 case K_KPAGEDOWN:
1059 ins_pagedown();
1060 break;
1061
1062#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001063 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 ins_drop();
1065 break;
1066#endif
1067
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001068 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 c = TAB;
1070 /* FALLTHROUGH */
1071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1074 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1075 goto docomplete;
1076#endif
1077 inserted_space = FALSE;
1078 if (ins_tab())
1079 goto normalchar; /* insert TAB as a normal char */
1080 auto_format(FALSE, TRUE);
1081 break;
1082
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001083 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 c = CAR;
1085 /* FALLTHROUGH */
1086 case CAR:
1087 case NL:
1088#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1089 /* In a quickfix window a <CR> jumps to the error under the
1090 * cursor. */
1091 if (bt_quickfix(curbuf) && c == CAR)
1092 {
1093 do_cmdline_cmd((char_u *)".cc");
1094 break;
1095 }
1096#endif
1097#ifdef FEAT_CMDWIN
1098 if (cmdwin_type != 0)
1099 {
1100 /* Execute the command in the cmdline window. */
1101 cmdwin_result = CAR;
1102 goto doESCkey;
1103 }
1104#endif
1105 if (ins_eol(c) && !p_im)
1106 goto doESCkey; /* out of memory */
1107 auto_format(FALSE, FALSE);
1108 inserted_space = FALSE;
1109 break;
1110
1111#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113# ifdef FEAT_INS_EXPAND
1114 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1115 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001116 if (has_compl_option(TRUE))
1117 goto docomplete;
1118 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
1120# endif
1121# ifdef FEAT_DIGRAPHS
1122 c = ins_digraph();
1123 if (c == NUL)
1124 break;
1125# endif
1126 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
1129#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001130 case Ctrl_X: /* Enter CTRL-X mode */
1131 ins_ctrl_x();
1132 break;
1133
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001134 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 if (ctrl_x_mode != CTRL_X_TAGS)
1136 goto normalchar;
1137 goto docomplete;
1138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001139 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 if (ctrl_x_mode != CTRL_X_FILES)
1141 goto normalchar;
1142 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001143
1144 case 's': /* Spelling completion after ^X */
1145 case Ctrl_S:
1146 if (ctrl_x_mode != CTRL_X_SPELL)
1147 goto normalchar;
1148 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149#endif
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152#ifdef FEAT_INS_EXPAND
1153 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1154#endif
1155 {
1156 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1157 if (p_im)
1158 {
1159 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1160 break;
1161 goto doESCkey;
1162 }
1163 goto normalchar;
1164 }
1165#ifdef FEAT_INS_EXPAND
1166 /* FALLTHROUGH */
1167
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001168 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 case Ctrl_N:
1170 /* if 'complete' is empty then plain ^P is no longer special,
1171 * but it is under other ^X modes */
1172 if (*curbuf->b_p_cpt == NUL
1173 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 goto normalchar;
1176
1177docomplete:
1178 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001179 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 break;
1181#endif /* FEAT_INS_EXPAND */
1182
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001183 case Ctrl_Y: /* copy from previous line or scroll down */
1184 case Ctrl_E: /* copy from next line or scroll up */
1185 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 break;
1187
1188 default:
1189#ifdef UNIX
1190 if (c == intr_char) /* special interrupt char */
1191 goto do_intr;
1192#endif
1193
1194 /*
1195 * Insert a nomal character.
1196 */
1197normalchar:
1198#ifdef FEAT_SMARTINDENT
1199 /* Try to perform smart-indenting. */
1200 ins_try_si(c);
1201#endif
1202
1203 if (c == ' ')
1204 {
1205 inserted_space = TRUE;
1206#ifdef FEAT_CINDENT
1207 if (inindent(0))
1208 can_cindent = FALSE;
1209#endif
1210 if (Insstart_blank_vcol == MAXCOL
1211 && curwin->w_cursor.lnum == Insstart.lnum)
1212 Insstart_blank_vcol = get_nolist_virtcol();
1213 }
1214
1215 if (vim_iswordc(c) || !echeck_abbr(
1216#ifdef FEAT_MBYTE
1217 /* Add ABBR_OFF for characters above 0x100, this is
1218 * what check_abbr() expects. */
1219 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1220#endif
1221 c))
1222 {
1223 insert_special(c, FALSE, FALSE);
1224#ifdef FEAT_RIGHTLEFT
1225 revins_legal++;
1226 revins_chars++;
1227#endif
1228 }
1229
1230 auto_format(FALSE, TRUE);
1231
1232#ifdef FEAT_FOLDING
1233 /* When inserting a character the cursor line must never be in a
1234 * closed fold. */
1235 foldOpenCursor();
1236#endif
1237 break;
1238 } /* end of switch (c) */
1239
1240 /* If the cursor was moved we didn't just insert a space */
1241 if (arrow_used)
1242 inserted_space = FALSE;
1243
1244#ifdef FEAT_CINDENT
1245 if (can_cindent && cindent_on()
1246# ifdef FEAT_INS_EXPAND
1247 && ctrl_x_mode == 0
1248# endif
1249 )
1250 {
1251force_cindent:
1252 /*
1253 * Indent now if a key was typed that is in 'cinkeys'.
1254 */
1255 if (in_cinkeys(c, ' ', line_is_white))
1256 {
1257 if (stop_arrow() == OK)
1258 /* re-indent the current line */
1259 do_c_expr_indent();
1260 }
1261 }
1262#endif /* FEAT_CINDENT */
1263
1264 } /* for (;;) */
1265 /* NOTREACHED */
1266}
1267
1268/*
1269 * Redraw for Insert mode.
1270 * This is postponed until getting the next character to make '$' in the 'cpo'
1271 * option work correctly.
1272 * Only redraw when there are no characters available. This speeds up
1273 * inserting sequences of characters (e.g., for CTRL-R).
1274 */
1275 static void
1276ins_redraw()
1277{
1278 if (!char_avail())
1279 {
1280 if (must_redraw)
1281 update_screen(0);
1282 else if (clear_cmdline || redraw_cmdline)
1283 showmode(); /* clear cmdline and show mode */
1284 showruler(FALSE);
1285 setcursor();
1286 emsg_on_display = FALSE; /* may remove error message now */
1287 }
1288}
1289
1290/*
1291 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1292 */
1293 static void
1294ins_ctrl_v()
1295{
1296 int c;
1297
1298 /* may need to redraw when no more chars available now */
1299 ins_redraw();
1300
1301 if (redrawing() && !char_avail())
1302 edit_putchar('^', TRUE);
1303 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1304
1305#ifdef FEAT_CMDL_INFO
1306 add_to_showcmd_c(Ctrl_V);
1307#endif
1308
1309 c = get_literal();
1310#ifdef FEAT_CMDL_INFO
1311 clear_showcmd();
1312#endif
1313 insert_special(c, FALSE, TRUE);
1314#ifdef FEAT_RIGHTLEFT
1315 revins_chars++;
1316 revins_legal++;
1317#endif
1318}
1319
1320/*
1321 * Put a character directly onto the screen. It's not stored in a buffer.
1322 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1323 */
1324static int pc_status;
1325#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1326#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1327#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1328#define PC_STATUS_SET 3 /* pc_bytes was filled */
1329#ifdef FEAT_MBYTE
1330static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1331#else
1332static char_u pc_bytes[2]; /* saved bytes */
1333#endif
1334static int pc_attr;
1335static int pc_row;
1336static int pc_col;
1337
1338 void
1339edit_putchar(c, highlight)
1340 int c;
1341 int highlight;
1342{
1343 int attr;
1344
1345 if (ScreenLines != NULL)
1346 {
1347 update_topline(); /* just in case w_topline isn't valid */
1348 validate_cursor();
1349 if (highlight)
1350 attr = hl_attr(HLF_8);
1351 else
1352 attr = 0;
1353 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1354 pc_col = W_WINCOL(curwin);
1355#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1356 pc_status = PC_STATUS_UNSET;
1357#endif
1358#ifdef FEAT_RIGHTLEFT
1359 if (curwin->w_p_rl)
1360 {
1361 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1362# ifdef FEAT_MBYTE
1363 if (has_mbyte)
1364 {
1365 int fix_col = mb_fix_col(pc_col, pc_row);
1366
1367 if (fix_col != pc_col)
1368 {
1369 screen_putchar(' ', pc_row, fix_col, attr);
1370 --curwin->w_wcol;
1371 pc_status = PC_STATUS_RIGHT;
1372 }
1373 }
1374# endif
1375 }
1376 else
1377#endif
1378 {
1379 pc_col += curwin->w_wcol;
1380#ifdef FEAT_MBYTE
1381 if (mb_lefthalve(pc_row, pc_col))
1382 pc_status = PC_STATUS_LEFT;
1383#endif
1384 }
1385
1386 /* save the character to be able to put it back */
1387#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1388 if (pc_status == PC_STATUS_UNSET)
1389#endif
1390 {
1391 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1392 pc_status = PC_STATUS_SET;
1393 }
1394 screen_putchar(c, pc_row, pc_col, attr);
1395 }
1396}
1397
1398/*
1399 * Undo the previous edit_putchar().
1400 */
1401 void
1402edit_unputchar()
1403{
1404 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1405 {
1406#if defined(FEAT_MBYTE)
1407 if (pc_status == PC_STATUS_RIGHT)
1408 ++curwin->w_wcol;
1409 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1410 redrawWinline(curwin->w_cursor.lnum, FALSE);
1411 else
1412#endif
1413 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1414 }
1415}
1416
1417/*
1418 * Called when p_dollar is set: display a '$' at the end of the changed text
1419 * Only works when cursor is in the line that changes.
1420 */
1421 void
1422display_dollar(col)
1423 colnr_T col;
1424{
1425 colnr_T save_col;
1426
1427 if (!redrawing())
1428 return;
1429
1430 cursor_off();
1431 save_col = curwin->w_cursor.col;
1432 curwin->w_cursor.col = col;
1433#ifdef FEAT_MBYTE
1434 if (has_mbyte)
1435 {
1436 char_u *p;
1437
1438 /* If on the last byte of a multi-byte move to the first byte. */
1439 p = ml_get_curline();
1440 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1441 }
1442#endif
1443 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1444 if (curwin->w_wcol < W_WIDTH(curwin))
1445 {
1446 edit_putchar('$', FALSE);
1447 dollar_vcol = curwin->w_virtcol;
1448 }
1449 curwin->w_cursor.col = save_col;
1450}
1451
1452/*
1453 * Call this function before moving the cursor from the normal insert position
1454 * in insert mode.
1455 */
1456 static void
1457undisplay_dollar()
1458{
1459 if (dollar_vcol)
1460 {
1461 dollar_vcol = 0;
1462 redrawWinline(curwin->w_cursor.lnum, FALSE);
1463 }
1464}
1465
1466/*
1467 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1468 * Keep the cursor on the same character.
1469 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1470 * type == INDENT_DEC decrease indent (for CTRL-D)
1471 * type == INDENT_SET set indent to "amount"
1472 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1473 */
1474 void
1475change_indent(type, amount, round, replaced)
1476 int type;
1477 int amount;
1478 int round;
1479 int replaced; /* replaced character, put on replace stack */
1480{
1481 int vcol;
1482 int last_vcol;
1483 int insstart_less; /* reduction for Insstart.col */
1484 int new_cursor_col;
1485 int i;
1486 char_u *ptr;
1487 int save_p_list;
1488 int start_col;
1489 colnr_T vc;
1490#ifdef FEAT_VREPLACE
1491 colnr_T orig_col = 0; /* init for GCC */
1492 char_u *new_line, *orig_line = NULL; /* init for GCC */
1493
1494 /* VREPLACE mode needs to know what the line was like before changing */
1495 if (State & VREPLACE_FLAG)
1496 {
1497 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1498 orig_col = curwin->w_cursor.col;
1499 }
1500#endif
1501
1502 /* for the following tricks we don't want list mode */
1503 save_p_list = curwin->w_p_list;
1504 curwin->w_p_list = FALSE;
1505 vc = getvcol_nolist(&curwin->w_cursor);
1506 vcol = vc;
1507
1508 /*
1509 * For Replace mode we need to fix the replace stack later, which is only
1510 * possible when the cursor is in the indent. Remember the number of
1511 * characters before the cursor if it's possible.
1512 */
1513 start_col = curwin->w_cursor.col;
1514
1515 /* determine offset from first non-blank */
1516 new_cursor_col = curwin->w_cursor.col;
1517 beginline(BL_WHITE);
1518 new_cursor_col -= curwin->w_cursor.col;
1519
1520 insstart_less = curwin->w_cursor.col;
1521
1522 /*
1523 * If the cursor is in the indent, compute how many screen columns the
1524 * cursor is to the left of the first non-blank.
1525 */
1526 if (new_cursor_col < 0)
1527 vcol = get_indent() - vcol;
1528
1529 if (new_cursor_col > 0) /* can't fix replace stack */
1530 start_col = -1;
1531
1532 /*
1533 * Set the new indent. The cursor will be put on the first non-blank.
1534 */
1535 if (type == INDENT_SET)
1536 (void)set_indent(amount, SIN_CHANGED);
1537 else
1538 {
1539#ifdef FEAT_VREPLACE
1540 int save_State = State;
1541
1542 /* Avoid being called recursively. */
1543 if (State & VREPLACE_FLAG)
1544 State = INSERT;
1545#endif
1546 shift_line(type == INDENT_DEC, round, 1);
1547#ifdef FEAT_VREPLACE
1548 State = save_State;
1549#endif
1550 }
1551 insstart_less -= curwin->w_cursor.col;
1552
1553 /*
1554 * Try to put cursor on same character.
1555 * If the cursor is at or after the first non-blank in the line,
1556 * compute the cursor column relative to the column of the first
1557 * non-blank character.
1558 * If we are not in insert mode, leave the cursor on the first non-blank.
1559 * If the cursor is before the first non-blank, position it relative
1560 * to the first non-blank, counted in screen columns.
1561 */
1562 if (new_cursor_col >= 0)
1563 {
1564 /*
1565 * When changing the indent while the cursor is touching it, reset
1566 * Insstart_col to 0.
1567 */
1568 if (new_cursor_col == 0)
1569 insstart_less = MAXCOL;
1570 new_cursor_col += curwin->w_cursor.col;
1571 }
1572 else if (!(State & INSERT))
1573 new_cursor_col = curwin->w_cursor.col;
1574 else
1575 {
1576 /*
1577 * Compute the screen column where the cursor should be.
1578 */
1579 vcol = get_indent() - vcol;
1580 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1581
1582 /*
1583 * Advance the cursor until we reach the right screen column.
1584 */
1585 vcol = last_vcol = 0;
1586 new_cursor_col = -1;
1587 ptr = ml_get_curline();
1588 while (vcol <= (int)curwin->w_virtcol)
1589 {
1590 last_vcol = vcol;
1591#ifdef FEAT_MBYTE
1592 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001593 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 else
1595#endif
1596 ++new_cursor_col;
1597 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1598 }
1599 vcol = last_vcol;
1600
1601 /*
1602 * May need to insert spaces to be able to position the cursor on
1603 * the right screen column.
1604 */
1605 if (vcol != (int)curwin->w_virtcol)
1606 {
1607 curwin->w_cursor.col = new_cursor_col;
1608 i = (int)curwin->w_virtcol - vcol;
1609 ptr = alloc(i + 1);
1610 if (ptr != NULL)
1611 {
1612 new_cursor_col += i;
1613 ptr[i] = NUL;
1614 while (--i >= 0)
1615 ptr[i] = ' ';
1616 ins_str(ptr);
1617 vim_free(ptr);
1618 }
1619 }
1620
1621 /*
1622 * When changing the indent while the cursor is in it, reset
1623 * Insstart_col to 0.
1624 */
1625 insstart_less = MAXCOL;
1626 }
1627
1628 curwin->w_p_list = save_p_list;
1629
1630 if (new_cursor_col <= 0)
1631 curwin->w_cursor.col = 0;
1632 else
1633 curwin->w_cursor.col = new_cursor_col;
1634 curwin->w_set_curswant = TRUE;
1635 changed_cline_bef_curs();
1636
1637 /*
1638 * May have to adjust the start of the insert.
1639 */
1640 if (State & INSERT)
1641 {
1642 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1643 {
1644 if ((int)Insstart.col <= insstart_less)
1645 Insstart.col = 0;
1646 else
1647 Insstart.col -= insstart_less;
1648 }
1649 if ((int)ai_col <= insstart_less)
1650 ai_col = 0;
1651 else
1652 ai_col -= insstart_less;
1653 }
1654
1655 /*
1656 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1657 * If the number of characters before the cursor decreased, need to pop a
1658 * few characters from the replace stack.
1659 * If the number of characters before the cursor increased, need to push a
1660 * few NULs onto the replace stack.
1661 */
1662 if (REPLACE_NORMAL(State) && start_col >= 0)
1663 {
1664 while (start_col > (int)curwin->w_cursor.col)
1665 {
1666 replace_join(0); /* remove a NUL from the replace stack */
1667 --start_col;
1668 }
1669 while (start_col < (int)curwin->w_cursor.col || replaced)
1670 {
1671 replace_push(NUL);
1672 if (replaced)
1673 {
1674 replace_push(replaced);
1675 replaced = NUL;
1676 }
1677 ++start_col;
1678 }
1679 }
1680
1681#ifdef FEAT_VREPLACE
1682 /*
1683 * For VREPLACE mode, we also have to fix the replace stack. In this case
1684 * it is always possible because we backspace over the whole line and then
1685 * put it back again the way we wanted it.
1686 */
1687 if (State & VREPLACE_FLAG)
1688 {
1689 /* If orig_line didn't allocate, just return. At least we did the job,
1690 * even if you can't backspace. */
1691 if (orig_line == NULL)
1692 return;
1693
1694 /* Save new line */
1695 new_line = vim_strsave(ml_get_curline());
1696 if (new_line == NULL)
1697 return;
1698
1699 /* We only put back the new line up to the cursor */
1700 new_line[curwin->w_cursor.col] = NUL;
1701
1702 /* Put back original line */
1703 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1704 curwin->w_cursor.col = orig_col;
1705
1706 /* Backspace from cursor to start of line */
1707 backspace_until_column(0);
1708
1709 /* Insert new stuff into line again */
1710 ins_bytes(new_line);
1711
1712 vim_free(new_line);
1713 }
1714#endif
1715}
1716
1717/*
1718 * Truncate the space at the end of a line. This is to be used only in an
1719 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1720 * modes.
1721 */
1722 void
1723truncate_spaces(line)
1724 char_u *line;
1725{
1726 int i;
1727
1728 /* find start of trailing white space */
1729 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1730 {
1731 if (State & REPLACE_FLAG)
1732 replace_join(0); /* remove a NUL from the replace stack */
1733 }
1734 line[i + 1] = NUL;
1735}
1736
1737#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1738 || defined(FEAT_COMMENTS) || defined(PROTO)
1739/*
1740 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1741 * modes correctly. May also be used when not in insert mode at all.
1742 */
1743 void
1744backspace_until_column(col)
1745 int col;
1746{
1747 while ((int)curwin->w_cursor.col > col)
1748 {
1749 curwin->w_cursor.col--;
1750 if (State & REPLACE_FLAG)
1751 replace_do_bs();
1752 else
1753 (void)del_char(FALSE);
1754 }
1755}
1756#endif
1757
1758#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1759/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001760 * CTRL-X pressed in Insert mode.
1761 */
1762 static void
1763ins_ctrl_x()
1764{
1765 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1766 * CTRL-V works like CTRL-N */
1767 if (ctrl_x_mode != CTRL_X_CMDLINE)
1768 {
1769 /* if the next ^X<> won't ADD nothing, then reset
1770 * compl_cont_status */
1771 if (compl_cont_status & CONT_N_ADDS)
1772 compl_cont_status = (compl_cont_status | CONT_INTRPT);
1773 else
1774 compl_cont_status = 0;
1775 /* We're not sure which CTRL-X mode it will be yet */
1776 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1777 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1778 edit_submode_pre = NULL;
1779 showmode();
1780 }
1781}
1782
1783/*
1784 * Return TRUE if the 'dict' or 'tsr' option can be used.
1785 */
1786 static int
1787has_compl_option(dict_opt)
1788 int dict_opt;
1789{
1790 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1791 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1792 {
1793 ctrl_x_mode = 0;
1794 edit_submode = NULL;
1795 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1796 : (char_u *)_("'thesaurus' option is empty"),
1797 hl_attr(HLF_E));
1798 if (emsg_silent == 0)
1799 {
1800 vim_beep();
1801 setcursor();
1802 out_flush();
1803 ui_delay(2000L, FALSE);
1804 }
1805 return FALSE;
1806 }
1807 return TRUE;
1808}
1809
1810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1812 * This depends on the current mode.
1813 */
1814 int
1815vim_is_ctrl_x_key(c)
1816 int c;
1817{
1818 /* Always allow ^R - let it's results then be checked */
1819 if (c == Ctrl_R)
1820 return TRUE;
1821
1822 switch (ctrl_x_mode)
1823 {
1824 case 0: /* Not in any CTRL-X mode */
1825 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1826 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001827 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1829 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1830 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001831 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1832 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 case CTRL_X_SCROLL:
1834 return (c == Ctrl_Y || c == Ctrl_E);
1835 case CTRL_X_WHOLE_LINE:
1836 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1837 case CTRL_X_FILES:
1838 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1839 case CTRL_X_DICTIONARY:
1840 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1841 case CTRL_X_THESAURUS:
1842 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1843 case CTRL_X_TAGS:
1844 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1845#ifdef FEAT_FIND_ID
1846 case CTRL_X_PATH_PATTERNS:
1847 return (c == Ctrl_P || c == Ctrl_N);
1848 case CTRL_X_PATH_DEFINES:
1849 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1850#endif
1851 case CTRL_X_CMDLINE:
1852 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1853 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001854#ifdef FEAT_COMPL_FUNC
1855 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001856 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001857 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001858 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001859#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001860 case CTRL_X_SPELL:
1861 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 }
1863 EMSG(_(e_internal));
1864 return FALSE;
1865}
1866
1867/*
1868 * This is like ins_compl_add(), but if ic and inf are set, then the
1869 * case of the originally typed text is used, and the case of the completed
1870 * text is infered, ie this tries to work out what case you probably wanted
1871 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001872 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 */
1874 int
Bram Moolenaar572cb562005-08-05 21:35:02 +00001875ins_compl_add_infercase(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 char_u *str;
1877 int len;
1878 char_u *fname;
1879 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001880 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881{
1882 int has_lower = FALSE;
1883 int was_letter = FALSE;
1884 int idx;
1885
1886 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
1887 {
1888 /* Infer case of completed part -- webb */
1889 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001890 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891
1892 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001893 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001895 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 {
1897 has_lower = TRUE;
1898 if (isupper(IObuff[idx]))
1899 {
1900 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001901 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
1903 break;
1904 }
1905 }
1906 }
1907
1908 /*
1909 * Rule 2: No lower case, 2nd consecutive letter converted to
1910 * upper case.
1911 */
1912 if (!has_lower)
1913 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001914 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001916 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 && islower(IObuff[idx]))
1918 {
1919 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001920 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
1922 break;
1923 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001924 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926 }
1927
1928 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001929 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
Bram Moolenaar572cb562005-08-05 21:35:02 +00001931 return ins_compl_add(IObuff, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00001933 return ins_compl_add(str, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934}
1935
1936/*
1937 * Add a match to the list of matches.
1938 * If the given string is already in the list of completions, then return
1939 * FAIL, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaar572cb562005-08-05 21:35:02 +00001940 * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb.
1941 *
1942 * New:
1943 * If the given string is already in the list of completions, then return
1944 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
1945 * maybe because alloc() returns NULL, then FAIL is returned -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00001947 int
1948ins_compl_add(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 char_u *str;
1950 int len;
1951 char_u *fname;
1952 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001953 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954{
Bram Moolenaar572cb562005-08-05 21:35:02 +00001955 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956
1957 ui_breakcheck();
1958 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00001959 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 if (len < 0)
1961 len = (int)STRLEN(str);
1962
1963 /*
1964 * If the same match is already present, don't add it.
1965 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001966 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001968 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 do
1970 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00001971 if ( !(match->cp_flags & ORIGINAL_TEXT)
1972 && STRNCMP(match->cp_str, str, (size_t)len) == 0
1973 && match->cp_str[len] == NUL)
1974 return NOTDONE;
1975 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001976 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 }
1978
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001979 /* Remove any popup menu before changing the list of matches. */
1980 ins_compl_del_pum();
1981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 /*
1983 * Allocate a new match structure.
1984 * Copy the values to the new match structure.
1985 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00001986 match = (compl_T *)alloc((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00001988 return FAIL;
1989 match->cp_number = -1;
1990 if (flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00001992 match->cp_number = 0;
1993 match->cp_str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00001995 else if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 {
1997 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00001998 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002001 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2003 * - NULL otherwise. --Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002004 if (fname && compl_curr_match && compl_curr_match->cp_fname
2005 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
2006 match->cp_fname = compl_curr_match->cp_fname;
2007 else if (fname && (match->cp_fname = vim_strsave(fname)) != NULL)
2008 flags |= FREE_FNAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002010 match->cp_fname = NULL;
2011 match->cp_flags = flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
2013 /*
2014 * Link the new match structure in the list of matches.
2015 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002016 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002017 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 else if (dir == FORWARD)
2019 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002020 match->cp_next = compl_curr_match->cp_next;
2021 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 }
2023 else /* BACKWARD */
2024 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002025 match->cp_next = compl_curr_match;
2026 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002028 if (match->cp_next)
2029 match->cp_next->cp_prev = match;
2030 if (match->cp_prev)
2031 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002033 compl_first_match = match;
2034 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
2036 return OK;
2037}
2038
2039/*
2040 * Add an array of matches to the list of matches.
2041 * Frees matches[].
2042 */
2043 static void
2044ins_compl_add_matches(num_matches, matches, dir)
2045 int num_matches;
2046 char_u **matches;
2047 int dir;
2048{
2049 int i;
2050 int add_r = OK;
2051 int ldir = dir;
2052
Bram Moolenaar572cb562005-08-05 21:35:02 +00002053 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 if ((add_r = ins_compl_add(matches[i], -1, NULL, ldir, 0)) == OK)
2055 /* if dir was BACKWARD then honor it just once */
2056 ldir = FORWARD;
2057 FreeWild(num_matches, matches);
2058}
2059
2060/* Make the completion list cyclic.
2061 * Return the number of matches (excluding the original).
2062 */
2063 static int
2064ins_compl_make_cyclic()
2065{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002066 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 int count = 0;
2068
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002069 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
2071 /*
2072 * Find the end of the list.
2073 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002074 match = compl_first_match;
2075 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002076 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002078 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 ++count;
2080 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002081 match->cp_next = compl_first_match;
2082 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
2084 return count;
2085}
2086
Bram Moolenaar9372a112005-12-06 19:59:18 +00002087/* "compl_match_array" points the currently displayed list of entries in the
2088 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002089static char_u **compl_match_array = NULL;
2090static int compl_match_arraysize;
2091
2092/*
2093 * Update the screen and when there is any scrolling remove the popup menu.
2094 */
2095 static void
2096ins_compl_upd_pum()
2097{
2098 int h;
2099
2100 if (compl_match_array != NULL)
2101 {
2102 h = curwin->w_cline_height;
2103 update_screen(0);
2104 if (h != curwin->w_cline_height)
2105 ins_compl_del_pum();
2106 }
2107}
2108
2109/*
2110 * Remove any popup menu.
2111 */
2112 static void
2113ins_compl_del_pum()
2114{
2115 if (compl_match_array != NULL)
2116 {
2117 pum_undisplay();
2118 vim_free(compl_match_array);
2119 compl_match_array = NULL;
2120 }
2121}
2122
2123/*
2124 * Return TRUE if the popup menu should be displayed.
2125 */
2126 static int
2127pum_wanted()
2128{
2129 compl_T *compl;
2130 int i;
2131
2132 /* 'completeopt' must contain "menu" */
2133 if (*p_cot == NUL)
2134 return FALSE;
2135
2136 /* The display looks bad on a B&W display. */
2137 if (t_colors < 8
2138#ifdef FEAT_GUI
2139 && !gui.in_use
2140#endif
2141 )
2142 return FALSE;
2143
2144 /* Don't display the popup menu if there are no matches or there is only
2145 * one (ignoring the original text). */
2146 compl = compl_first_match;
2147 i = 0;
2148 do
2149 {
2150 if (compl == NULL
2151 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2152 break;
2153 compl = compl->cp_next;
2154 } while (compl != compl_first_match);
2155
2156 return (i >= 2);
2157}
2158
2159/*
2160 * Show the popup menu for the list of matches.
2161 */
2162 static void
2163ins_compl_show_pum()
2164{
2165 compl_T *compl;
2166 int i;
2167 int cur = -1;
2168 colnr_T col;
2169
2170 if (!pum_wanted())
2171 return;
2172
2173 /* Update the screen before drawing the popup menu over it. */
2174 update_screen(0);
2175
2176 if (compl_match_array == NULL)
2177 {
2178 /* Need to build the popup menu list. */
2179 compl_match_arraysize = 0;
2180 compl = compl_first_match;
2181 do
2182 {
2183 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2184 ++compl_match_arraysize;
2185 compl = compl->cp_next;
2186 } while (compl != NULL && compl != compl_first_match);
2187 compl_match_array = (char_u **)alloc((unsigned)(sizeof(char_u **)
2188 * compl_match_arraysize));
2189 if (compl_match_array != NULL)
2190 {
2191 i = 0;
2192 compl = compl_first_match;
2193 do
2194 {
2195 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2196 {
2197 if (compl == compl_shown_match)
2198 cur = i;
2199 compl_match_array[i++] = compl->cp_str;
2200 }
2201 compl = compl->cp_next;
2202 } while (compl != NULL && compl != compl_first_match);
2203 }
2204 }
2205 else
2206 {
2207 /* popup menu already exists, only need to find the current item.*/
2208 i = 0;
2209 compl = compl_first_match;
2210 do
2211 {
2212 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2213 {
2214 if (compl == compl_shown_match)
2215 {
2216 cur = i;
2217 break;
2218 }
2219 ++i;
2220 }
2221 compl = compl->cp_next;
2222 } while (compl != NULL && compl != compl_first_match);
2223 }
2224
2225 if (compl_match_array != NULL)
2226 {
2227 /* Compute the screen column of the start of the completed text.
2228 * Use the cursor to get all wrapping and other settings right. */
2229 col = curwin->w_cursor.col;
2230 curwin->w_cursor.col = compl_col;
2231 validate_cursor_col();
2232 pum_display(compl_match_array, compl_match_arraysize, cur,
2233 curwin->w_cline_row + W_WINROW(curwin),
2234 curwin->w_cline_height,
2235 curwin->w_wcol + W_WINCOL(curwin));
2236 curwin->w_cursor.col = col;
2237 }
2238}
2239
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240#define DICT_FIRST (1) /* use just first element in "dict" */
2241#define DICT_EXACT (2) /* "dict" is the exact name of a file */
2242/*
2243 * Add any identifiers that match the given pattern to the list of
2244 * completions.
2245 */
2246 static void
2247ins_compl_dictionaries(dict, pat, dir, flags, thesaurus)
2248 char_u *dict;
2249 char_u *pat;
2250 int dir;
2251 int flags;
2252 int thesaurus;
2253{
2254 char_u *ptr;
2255 char_u *buf;
2256 FILE *fp;
2257 regmatch_T regmatch;
2258 int add_r;
2259 char_u **files;
2260 int count;
2261 int i;
2262 int save_p_scs;
2263
2264 buf = alloc(LSIZE);
2265 /* If 'infercase' is set, don't use 'smartcase' here */
2266 save_p_scs = p_scs;
2267 if (curbuf->b_p_inf)
2268 p_scs = FALSE;
2269 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2270 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2271 regmatch.rm_ic = ignorecase(pat);
2272 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002273 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
2275 /* copy one dictionary file name into buf */
2276 if (flags == DICT_EXACT)
2277 {
2278 count = 1;
2279 files = &dict;
2280 }
2281 else
2282 {
2283 /* Expand wildcards in the dictionary name, but do not allow
2284 * backticks (for security, the 'dict' option may have been set in
2285 * a modeline). */
2286 copy_option_part(&dict, buf, LSIZE, ",");
2287 if (vim_strchr(buf, '`') != NULL
2288 || expand_wildcards(1, &buf, &count, &files,
2289 EW_FILE|EW_SILENT) != OK)
2290 count = 0;
2291 }
2292
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002293 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
2295 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2296 if (flags != DICT_EXACT)
2297 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002298 vim_snprintf((char *)IObuff, IOSIZE,
2299 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2301 }
2302
2303 if (fp != NULL)
2304 {
2305 /*
2306 * Read dictionary file line by line.
2307 * Check each line for a match.
2308 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002309 while (!got_int && !compl_interrupted
2310 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
2312 ptr = buf;
2313 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2314 {
2315 ptr = regmatch.startp[0];
2316 ptr = find_word_end(ptr);
2317 add_r = ins_compl_add_infercase(regmatch.startp[0],
2318 (int)(ptr - regmatch.startp[0]),
2319 files[i], dir, 0);
2320 if (thesaurus)
2321 {
2322 char_u *wstart;
2323
2324 /*
2325 * Add the other matches on the line
2326 */
2327 while (!got_int)
2328 {
2329 /* Find start of the next word. Skip white
2330 * space and punctuation. */
2331 ptr = find_word_start(ptr);
2332 if (*ptr == NUL || *ptr == NL)
2333 break;
2334 wstart = ptr;
2335
2336 /* Find end of the word and add it. */
2337#ifdef FEAT_MBYTE
2338 if (has_mbyte)
2339 /* Japanese words may have characters in
2340 * different classes, only separate words
2341 * with single-byte non-word characters. */
2342 while (*ptr != NUL)
2343 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002344 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
2346 if (l < 2 && !vim_iswordc(*ptr))
2347 break;
2348 ptr += l;
2349 }
2350 else
2351#endif
2352 ptr = find_word_end(ptr);
2353 add_r = ins_compl_add_infercase(wstart,
2354 (int)(ptr - wstart), files[i], dir, 0);
2355 }
2356 }
2357 if (add_r == OK)
2358 /* if dir was BACKWARD then honor it just once */
2359 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002360 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 break;
2362 /* avoid expensive call to vim_regexec() when at end
2363 * of line */
2364 if (*ptr == '\n' || got_int)
2365 break;
2366 }
2367 line_breakcheck();
Bram Moolenaar572cb562005-08-05 21:35:02 +00002368 ins_compl_check_keys(50);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370 fclose(fp);
2371 }
2372 }
2373 if (flags != DICT_EXACT)
2374 FreeWild(count, files);
2375 if (flags)
2376 break;
2377 }
2378 p_scs = save_p_scs;
2379 vim_free(regmatch.regprog);
2380 vim_free(buf);
2381}
2382
2383/*
2384 * Find the start of the next word.
2385 * Returns a pointer to the first char of the word. Also stops at a NUL.
2386 */
2387 char_u *
2388find_word_start(ptr)
2389 char_u *ptr;
2390{
2391#ifdef FEAT_MBYTE
2392 if (has_mbyte)
2393 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002394 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 else
2396#endif
2397 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2398 ++ptr;
2399 return ptr;
2400}
2401
2402/*
2403 * Find the end of the word. Assumes it starts inside a word.
2404 * Returns a pointer to just after the word.
2405 */
2406 char_u *
2407find_word_end(ptr)
2408 char_u *ptr;
2409{
2410#ifdef FEAT_MBYTE
2411 int start_class;
2412
2413 if (has_mbyte)
2414 {
2415 start_class = mb_get_class(ptr);
2416 if (start_class > 1)
2417 while (*ptr != NUL)
2418 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002419 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 if (mb_get_class(ptr) != start_class)
2421 break;
2422 }
2423 }
2424 else
2425#endif
2426 while (vim_iswordc(*ptr))
2427 ++ptr;
2428 return ptr;
2429}
2430
2431/*
2432 * Free the list of completions
2433 */
2434 static void
2435ins_compl_free()
2436{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002437 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002439 vim_free(compl_pattern);
2440 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002442 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002444
2445 ins_compl_del_pum();
2446 pum_clear();
2447
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002448 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 do
2450 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002451 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002452 compl_curr_match = compl_curr_match->cp_next;
2453 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002455 if (match->cp_flags & FREE_FNAME)
2456 vim_free(match->cp_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002458 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2459 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460}
2461
2462 static void
2463ins_compl_clear()
2464{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002465 compl_cont_status = 0;
2466 compl_started = FALSE;
2467 compl_matches = 0;
2468 vim_free(compl_pattern);
2469 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 save_sm = -1;
2471 edit_submode_extra = NULL;
2472}
2473
2474/*
2475 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002476 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002477 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002479 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480ins_compl_prep(c)
2481 int c;
2482{
2483 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 int temp;
2485 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002486 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
2488 /* Forget any previous 'special' messages if this is actually
2489 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2490 */
2491 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2492 edit_submode_extra = NULL;
2493
2494 /* Ignore end of Select mode mapping */
2495 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002496 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
2498 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2499 {
2500 /*
2501 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2502 * it will be yet. Now we decide.
2503 */
2504 switch (c)
2505 {
2506 case Ctrl_E:
2507 case Ctrl_Y:
2508 ctrl_x_mode = CTRL_X_SCROLL;
2509 if (!(State & REPLACE_FLAG))
2510 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2511 else
2512 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2513 edit_submode_pre = NULL;
2514 showmode();
2515 break;
2516 case Ctrl_L:
2517 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2518 break;
2519 case Ctrl_F:
2520 ctrl_x_mode = CTRL_X_FILES;
2521 break;
2522 case Ctrl_K:
2523 ctrl_x_mode = CTRL_X_DICTIONARY;
2524 break;
2525 case Ctrl_R:
2526 /* Simply allow ^R to happen without affecting ^X mode */
2527 break;
2528 case Ctrl_T:
2529 ctrl_x_mode = CTRL_X_THESAURUS;
2530 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002531#ifdef FEAT_COMPL_FUNC
2532 case Ctrl_U:
2533 ctrl_x_mode = CTRL_X_FUNCTION;
2534 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002535 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002536 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002537 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002538#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002539 case 's':
2540 case Ctrl_S:
2541 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002542#ifdef FEAT_SYN_HL
2543 spell_back_to_badword();
2544#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002545 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 case Ctrl_RSB:
2547 ctrl_x_mode = CTRL_X_TAGS;
2548 break;
2549#ifdef FEAT_FIND_ID
2550 case Ctrl_I:
2551 case K_S_TAB:
2552 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2553 break;
2554 case Ctrl_D:
2555 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2556 break;
2557#endif
2558 case Ctrl_V:
2559 case Ctrl_Q:
2560 ctrl_x_mode = CTRL_X_CMDLINE;
2561 break;
2562 case Ctrl_P:
2563 case Ctrl_N:
2564 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
2565 * just started ^X mode, or there were enough ^X's to cancel
2566 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2567 * do normal expansion when interrupting a different mode (say
2568 * ^X^F^X^P or ^P^X^X^P, see below)
2569 * nothing changes if interrupting mode 0, (eg, the flag
2570 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002571 if (!(compl_cont_status & CONT_INTRPT))
2572 compl_cont_status |= CONT_LOCAL;
2573 else if (compl_cont_mode != 0)
2574 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 /* FALLTHROUGH */
2576 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002577 /* If we have typed at least 2 ^X's... for modes != 0, we set
2578 * compl_cont_status = 0 (eg, as if we had just started ^X
2579 * mode).
2580 * For mode 0, we set "compl_cont_mode" to an impossible
2581 * value, in both cases ^X^X can be used to restart the same
2582 * mode (avoiding ADDING mode).
2583 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2584 * 'complete' and local ^P expansions respectively.
2585 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2586 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 if (c == Ctrl_X)
2588 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002589 if (compl_cont_mode != 0)
2590 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002592 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
2594 ctrl_x_mode = 0;
2595 edit_submode = NULL;
2596 showmode();
2597 break;
2598 }
2599 }
2600 else if (ctrl_x_mode != 0)
2601 {
2602 /* We're already in CTRL-X mode, do we stay in it? */
2603 if (!vim_is_ctrl_x_key(c))
2604 {
2605 if (ctrl_x_mode == CTRL_X_SCROLL)
2606 ctrl_x_mode = 0;
2607 else
2608 ctrl_x_mode = CTRL_X_FINISHED;
2609 edit_submode = NULL;
2610 }
2611 showmode();
2612 }
2613
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002614 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
2616 /* Show error message from attempted keyword completion (probably
2617 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002618 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 showmode();
2620 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R)
2621 || ctrl_x_mode == CTRL_X_FINISHED)
2622 {
2623 /* Get here when we have finished typing a sequence of ^N and
2624 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002625 * memory that was used, and make sure we can redo the insert. */
2626 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002628 char_u *p;
2629
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 /*
2631 * If any of the original typed text has been changed,
2632 * eg when ignorecase is set, we must add back-spaces to
2633 * the redo buffer. We add as few as necessary to delete
2634 * just the part of the original text that has changed.
2635 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002636 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002637 p = compl_orig_text;
2638 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002640 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 ++ptr;
2642 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002643 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 AppendCharToRedobuff(K_BS);
2645 AppendToRedobuffLit(ptr);
2646 }
2647
2648#ifdef FEAT_CINDENT
2649 want_cindent = (can_cindent && cindent_on());
2650#endif
2651 /*
2652 * When completing whole lines: fix indent for 'cindent'.
2653 * Otherwise, break line if it's too long.
2654 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002655 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 {
2657#ifdef FEAT_CINDENT
2658 /* re-indent the current line */
2659 if (want_cindent)
2660 {
2661 do_c_expr_indent();
2662 want_cindent = FALSE; /* don't do it again */
2663 }
2664#endif
2665 }
2666 else
2667 {
2668 /* put the cursor on the last char, for 'tw' formatting */
2669 curwin->w_cursor.col--;
2670 if (stop_arrow() == OK)
2671 insertchar(NUL, 0, -1);
2672 curwin->w_cursor.col++;
2673 }
2674
2675 auto_format(FALSE, TRUE);
2676
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002677 /* if the popup menu is displayed hitting Enter means accepting
2678 * the selection without inserting anything. */
2679 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
2680 retval = TRUE;
2681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002683 compl_started = FALSE;
2684 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 msg_clr_cmdline(); /* necessary for "noshowmode" */
2686 ctrl_x_mode = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002687 if (save_sm >= 0)
2688 p_sm = save_sm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 if (edit_submode != NULL)
2690 {
2691 edit_submode = NULL;
2692 showmode();
2693 }
2694
2695#ifdef FEAT_CINDENT
2696 /*
2697 * Indent now if a key was typed that is in 'cinkeys'.
2698 */
2699 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2700 do_c_expr_indent();
2701#endif
2702 }
2703 }
2704
2705 /* reset continue_* if we left expansion-mode, if we stay they'll be
2706 * (re)set properly in ins_complete() */
2707 if (!vim_is_ctrl_x_key(c))
2708 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002709 compl_cont_status = 0;
2710 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002712
2713 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714}
2715
2716/*
2717 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2718 * (depending on flag) starting from buf and looking for a non-scanned
2719 * buffer (other than curbuf). curbuf is special, if it is called with
2720 * buf=curbuf then it has to be the first call for a given flag/expansion.
2721 *
2722 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2723 */
2724 static buf_T *
2725ins_compl_next_buf(buf, flag)
2726 buf_T *buf;
2727 int flag;
2728{
2729#ifdef FEAT_WINDOWS
2730 static win_T *wp;
2731#endif
2732
2733 if (flag == 'w') /* just windows */
2734 {
2735#ifdef FEAT_WINDOWS
2736 if (buf == curbuf) /* first call for this flag/expansion */
2737 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002738 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 && wp->w_buffer->b_scanned)
2740 ;
2741 buf = wp->w_buffer;
2742#else
2743 buf = curbuf;
2744#endif
2745 }
2746 else
2747 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2748 * (unlisted buffers)
2749 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002750 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 && ((flag == 'U'
2752 ? buf->b_p_bl
2753 : (!buf->b_p_bl
2754 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2755 || buf->b_scanned
2756 || (buf->b_ml.ml_mfp == NULL
2757 && ctrl_x_mode == CTRL_X_WHOLE_LINE)))
2758 ;
2759 return buf;
2760}
2761
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002762#ifdef FEAT_COMPL_FUNC
Bram Moolenaare344bea2005-09-01 20:46:49 +00002763static int expand_by_function __ARGS((int type, char_u *base, char_u ***matches));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002764
2765/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002766 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00002767 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002768 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002769 */
2770 static int
Bram Moolenaare344bea2005-09-01 20:46:49 +00002771expand_by_function(type, base, matches)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002772 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002773 char_u *base;
2774 char_u ***matches;
2775{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002776 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002777 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002778 listitem_T *li;
2779 garray_T ga;
2780 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002781 char_u *funcname;
2782 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002783
Bram Moolenaare344bea2005-09-01 20:46:49 +00002784 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
2785 if (*funcname == NUL)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002786 return 0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002787
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002788 /* Call 'completefunc' to obtain the list of matches. */
2789 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00002790 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002791
Bram Moolenaare344bea2005-09-01 20:46:49 +00002792 pos = curwin->w_cursor;
2793 matchlist = call_func_retlist(funcname, 2, args, FALSE);
2794 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002795 if (matchlist == NULL)
2796 return 0;
2797
2798 /* Go through the List with matches and put them in an array. */
2799 ga_init2(&ga, (int)sizeof(char_u *), 8);
2800 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002801 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002802 p = get_tv_string_chk(&li->li_tv);
2803 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002804 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002805 if (ga_grow(&ga, 1) == FAIL)
2806 break;
2807 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p);
2808 ++ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002809 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002810 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002811
2812 list_unref(matchlist);
2813 *matches = (char_u **)ga.ga_data;
2814 return ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002815}
2816#endif /* FEAT_COMPL_FUNC */
2817
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002818/*
2819 * Get the next expansion(s), using "compl_pattern".
2820 * The search starts at position "ini" in curbuf and in the direction dir.
2821 * When "compl_started" is FALSE start at that position, otherwise
2822 * continue where we stopped searching before.
2823 * This may return before finding all the matches.
2824 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 */
2826 static int
2827ins_compl_get_exp(ini, dir)
2828 pos_T *ini;
2829 int dir;
2830{
2831 static pos_T first_match_pos;
2832 static pos_T last_match_pos;
2833 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002834 static int found_all = FALSE; /* Found all matches of a
2835 certain type. */
2836 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
Bram Moolenaar572cb562005-08-05 21:35:02 +00002838 pos_T *pos;
2839 char_u **matches;
2840 int save_p_scs;
2841 int save_p_ws;
2842 int save_p_ic;
2843 int i;
2844 int num_matches;
2845 int len;
2846 int found_new_match;
2847 int type = ctrl_x_mode;
2848 char_u *ptr;
2849 char_u *dict = NULL;
2850 int dict_f = 0;
2851 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002853 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 {
2855 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
2856 ins_buf->b_scanned = 0;
2857 found_all = FALSE;
2858 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002859 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002860 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 last_match_pos = first_match_pos = *ini;
2862 }
2863
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002864 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 pos = (dir == FORWARD) ? &last_match_pos : &first_match_pos;
2866 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
2867 for (;;)
2868 {
2869 found_new_match = FAIL;
2870
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002871 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 * or if found_all says this entry is done. For ^X^L only use the
2873 * entries from 'complete' that look in loaded buffers. */
2874 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002875 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 {
2877 found_all = FALSE;
2878 while (*e_cpt == ',' || *e_cpt == ' ')
2879 e_cpt++;
2880 if (*e_cpt == '.' && !curbuf->b_scanned)
2881 {
2882 ins_buf = curbuf;
2883 first_match_pos = *ini;
2884 /* So that ^N can match word immediately after cursor */
2885 if (ctrl_x_mode == 0)
2886 dec(&first_match_pos);
2887 last_match_pos = first_match_pos;
2888 type = 0;
2889 }
2890 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2891 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2892 {
2893 /* Scan a buffer, but not the current one. */
2894 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
2895 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002896 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 first_match_pos.col = last_match_pos.col = 0;
2898 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2899 last_match_pos.lnum = 0;
2900 type = 0;
2901 }
2902 else /* unloaded buffer, scan like dictionary */
2903 {
2904 found_all = TRUE;
2905 if (ins_buf->b_fname == NULL)
2906 continue;
2907 type = CTRL_X_DICTIONARY;
2908 dict = ins_buf->b_fname;
2909 dict_f = DICT_EXACT;
2910 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00002911 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 ins_buf->b_fname == NULL
2913 ? buf_spname(ins_buf)
2914 : ins_buf->b_sfname == NULL
2915 ? (char *)ins_buf->b_fname
2916 : (char *)ins_buf->b_sfname);
2917 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2918 }
2919 else if (*e_cpt == NUL)
2920 break;
2921 else
2922 {
2923 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2924 type = -1;
2925 else if (*e_cpt == 'k' || *e_cpt == 's')
2926 {
2927 if (*e_cpt == 'k')
2928 type = CTRL_X_DICTIONARY;
2929 else
2930 type = CTRL_X_THESAURUS;
2931 if (*++e_cpt != ',' && *e_cpt != NUL)
2932 {
2933 dict = e_cpt;
2934 dict_f = DICT_FIRST;
2935 }
2936 }
2937#ifdef FEAT_FIND_ID
2938 else if (*e_cpt == 'i')
2939 type = CTRL_X_PATH_PATTERNS;
2940 else if (*e_cpt == 'd')
2941 type = CTRL_X_PATH_DEFINES;
2942#endif
2943 else if (*e_cpt == ']' || *e_cpt == 't')
2944 {
2945 type = CTRL_X_TAGS;
2946 sprintf((char*)IObuff, _("Scanning tags."));
2947 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2948 }
2949 else
2950 type = -1;
2951
2952 /* in any case e_cpt is advanced to the next entry */
2953 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2954
2955 found_all = TRUE;
2956 if (type == -1)
2957 continue;
2958 }
2959 }
2960
2961 switch (type)
2962 {
2963 case -1:
2964 break;
2965#ifdef FEAT_FIND_ID
2966 case CTRL_X_PATH_PATTERNS:
2967 case CTRL_X_PATH_DEFINES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002968 find_pattern_in_path(compl_pattern, dir,
2969 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002971 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
2973 (linenr_T)1, (linenr_T)MAXLNUM);
2974 break;
2975#endif
2976
2977 case CTRL_X_DICTIONARY:
2978 case CTRL_X_THESAURUS:
2979 ins_compl_dictionaries(
2980 dict ? dict
2981 : (type == CTRL_X_THESAURUS
2982 ? (*curbuf->b_p_tsr == NUL
2983 ? p_tsr
2984 : curbuf->b_p_tsr)
2985 : (*curbuf->b_p_dict == NUL
2986 ? p_dict
2987 : curbuf->b_p_dict)),
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002988 compl_pattern, dir,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
2990 dict = NULL;
2991 break;
2992
2993 case CTRL_X_TAGS:
2994 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
2995 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002996 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
2998 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002999 * of matches is found when compl_pattern is empty */
3000 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3002 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3003 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3004 {
3005 ins_compl_add_matches(num_matches, matches, dir);
3006 }
3007 p_ic = save_p_ic;
3008 break;
3009
3010 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003011 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3013 {
3014
3015 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003016 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 ins_compl_add_matches(num_matches, matches, dir);
3018 }
3019 break;
3020
3021 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003022 if (expand_cmdline(&compl_xp, compl_pattern,
3023 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 &num_matches, &matches) == EXPAND_OK)
3025 ins_compl_add_matches(num_matches, matches, dir);
3026 break;
3027
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003028#ifdef FEAT_COMPL_FUNC
3029 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003030 case CTRL_X_OMNI:
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003031 num_matches = expand_by_function(type, compl_pattern, &matches);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003032 if (num_matches > 0)
3033 ins_compl_add_matches(num_matches, matches, dir);
3034 break;
3035#endif
3036
Bram Moolenaar488c6512005-08-11 20:09:58 +00003037 case CTRL_X_SPELL:
3038#ifdef FEAT_SYN_HL
3039 num_matches = expand_spelling(first_match_pos.lnum,
3040 first_match_pos.col, compl_pattern, &matches);
3041 if (num_matches > 0)
3042 ins_compl_add_matches(num_matches, matches, dir);
3043#endif
3044 break;
3045
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 default: /* normal ^P/^N and ^X^L */
3047 /*
3048 * If 'infercase' is set, don't use 'smartcase' here
3049 */
3050 save_p_scs = p_scs;
3051 if (ins_buf->b_p_inf)
3052 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003053
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 /* buffers other than curbuf are scanned from the beginning or the
3055 * end but never from the middle, thus setting nowrapscan in this
3056 * buffers is a good idea, on the other hand, we always set
3057 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3058 save_p_ws = p_ws;
3059 if (ins_buf != curbuf)
3060 p_ws = FALSE;
3061 else if (*e_cpt == '.')
3062 p_ws = TRUE;
3063 for (;;)
3064 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003065 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003067 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3068 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003070 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003072 dir, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 else
3074 found_new_match = searchit(NULL, ins_buf, pos, dir,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003075 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 RE_LAST);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003077 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003079 /* set compl_started even on fail */
3080 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 first_match_pos = *pos;
3082 last_match_pos = *pos;
3083 }
3084 else if (first_match_pos.lnum == last_match_pos.lnum
3085 && first_match_pos.col == last_match_pos.col)
3086 found_new_match = FAIL;
3087 if (found_new_match == FAIL)
3088 {
3089 if (ins_buf == curbuf)
3090 found_all = TRUE;
3091 break;
3092 }
3093
3094 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003095 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 && ini->lnum == pos->lnum
3097 && ini->col == pos->col)
3098 continue;
3099 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3100 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3101 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003102 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 {
3104 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3105 continue;
3106 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3107 if (!p_paste)
3108 ptr = skipwhite(ptr);
3109 }
3110 len = (int)STRLEN(ptr);
3111 }
3112 else
3113 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003114 char_u *tmp_ptr = ptr;
3115
3116 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003118 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 /* Skip if already inside a word. */
3120 if (vim_iswordp(tmp_ptr))
3121 continue;
3122 /* Find start of next word. */
3123 tmp_ptr = find_word_start(tmp_ptr);
3124 }
3125 /* Find end of this word. */
3126 tmp_ptr = find_word_end(tmp_ptr);
3127 len = (int)(tmp_ptr - ptr);
3128
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003129 if ((compl_cont_status & CONT_ADDING)
3130 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 {
3132 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3133 {
3134 /* Try next line, if any. the new word will be
3135 * "join" as if the normal command "J" was used.
3136 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003137 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 * works -- Acevedo */
3139 STRNCPY(IObuff, ptr, len);
3140 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3141 tmp_ptr = ptr = skipwhite(ptr);
3142 /* Find start of next word. */
3143 tmp_ptr = find_word_start(tmp_ptr);
3144 /* Find end of next word. */
3145 tmp_ptr = find_word_end(tmp_ptr);
3146 if (tmp_ptr > ptr)
3147 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003148 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003150 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 IObuff[len++] = ' ';
3152 /* IObuf =~ "\k.* ", thus len >= 2 */
3153 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003154 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 || (vim_strchr(p_cpo, CPO_JOINSP)
3156 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003157 && (IObuff[len - 2] == '?'
3158 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 IObuff[len++] = ' ';
3160 }
3161 /* copy as much as posible of the new word */
3162 if (tmp_ptr - ptr >= IOSIZE - len)
3163 tmp_ptr = ptr + IOSIZE - len - 1;
3164 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3165 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003166 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 }
3168 IObuff[len] = NUL;
3169 ptr = IObuff;
3170 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003171 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 continue;
3173 }
3174 }
3175 if (ins_compl_add_infercase(ptr, len,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003176 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar572cb562005-08-05 21:35:02 +00003177 dir, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 {
3179 found_new_match = OK;
3180 break;
3181 }
3182 }
3183 p_scs = save_p_scs;
3184 p_ws = save_p_ws;
3185 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003186
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003187 /* check if compl_curr_match has changed, (e.g. other type of
3188 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003189 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 found_new_match = OK;
3191
3192 /* break the loop for specialized modes (use 'complete' just for the
3193 * generic ctrl_x_mode == 0) or when we've found a new match */
3194 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003195 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003196 {
3197 if (got_int)
3198 break;
3199 if (pum_wanted() && type != -1)
3200 /* Fill the popup menu as soon as possible. */
3201 ins_compl_check_keys(0);
3202 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3203 || compl_interrupted)
3204 break;
3205 compl_started = TRUE;
3206 }
3207 else
3208 {
3209 /* Mark a buffer scanned when it has been scanned completely */
3210 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3211 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003213 compl_started = FALSE;
3214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003216 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
3218 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3219 && *e_cpt == NUL) /* Got to end of 'complete' */
3220 found_new_match = FAIL;
3221
3222 i = -1; /* total of matches, unknown */
3223 if (found_new_match == FAIL
3224 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3225 i = ins_compl_make_cyclic();
3226
3227 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003228 * just been made cyclic then we have to move compl_curr_match to the next
3229 * or previous entry (if any) -- Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003230 compl_curr_match = dir == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003231 if (compl_curr_match == NULL)
3232 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 return i;
3234}
3235
3236/* Delete the old text being completed. */
3237 static void
3238ins_compl_delete()
3239{
3240 int i;
3241
3242 /*
3243 * In insert mode: Delete the typed part.
3244 * In replace mode: Put the old characters back, if any.
3245 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003246 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 backspace_until_column(i);
3248 changed_cline_bef_curs();
3249}
3250
3251/* Insert the new text being completed. */
3252 static void
3253ins_compl_insert()
3254{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003255 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256}
3257
3258/*
3259 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003260 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3261 * get more completions. If it is FALSE, then we just do nothing when there
3262 * are no more completions in a given direction. The latter case is used when
3263 * we are still in the middle of finding completions, to allow browsing
3264 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 * Return the total number of matches, or -1 if still unknown -- webb.
3266 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003267 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3268 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 *
3270 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003271 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3272 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 */
3274 static int
3275ins_compl_next(allow_get_expansion)
3276 int allow_get_expansion;
3277{
3278 int num_matches = -1;
3279 int i;
3280
3281 if (allow_get_expansion)
3282 {
3283 /* Delete old text to be replaced */
3284 ins_compl_delete();
3285 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003286 compl_pending = FALSE;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003287 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
3288 compl_shown_match = compl_shown_match->cp_next;
3289 else if (compl_shows_dir == BACKWARD && compl_shown_match->cp_prev != NULL)
3290 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 else
3292 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003293 compl_pending = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 if (allow_get_expansion)
3295 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003296 num_matches = ins_compl_get_exp(&compl_startpos, compl_direction);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003297 if (compl_pending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003299 if (compl_direction == compl_shows_dir)
3300 compl_shown_match = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
3302 }
3303 else
3304 return -1;
3305 }
3306
3307 /* Insert the text of the new completion */
3308 ins_compl_insert();
3309
3310 if (!allow_get_expansion)
3311 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003312 /* may undisplay the popup menu first */
3313 ins_compl_upd_pum();
3314
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 /* Display the current match. */
3316 update_screen(0);
3317
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003318 /* display the updated popup menu */
3319 ins_compl_show_pum();
3320
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 /* Delete old text to be replaced, since we're still searching and
3322 * don't want to match ourselves! */
3323 ins_compl_delete();
3324 }
3325
3326 /*
3327 * Show the file name for the match (if any)
3328 * Truncate the file name to avoid a wait for return.
3329 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003330 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
3332 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003333 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 if (i <= 0)
3335 i = 0;
3336 else
3337 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003338 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 msg(IObuff);
3340 redraw_cmdline = FALSE; /* don't overwrite! */
3341 }
3342
3343 return num_matches;
3344}
3345
3346/*
3347 * Call this while finding completions, to check whether the user has hit a key
3348 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003349 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003351 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 */
3353 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003354ins_compl_check_keys(frequency)
3355 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
3357 static int count = 0;
3358
3359 int c;
3360
3361 /* Don't check when reading keys from a script. That would break the test
3362 * scripts */
3363 if (using_script())
3364 return;
3365
3366 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003367 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 return;
3369 count = 0;
3370
3371 ++no_mapping;
3372 c = vpeekc_any();
3373 --no_mapping;
3374 if (c != NUL)
3375 {
3376 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3377 {
3378 c = safe_vgetc(); /* Eat the character */
3379 if (c == Ctrl_P || c == Ctrl_L)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003380 compl_shows_dir = BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003382 compl_shows_dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 (void)ins_compl_next(FALSE);
3384 }
3385 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003386 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003388 if (compl_pending && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 (void)ins_compl_next(FALSE);
3390}
3391
3392/*
3393 * Do Insert mode completion.
3394 * Called when character "c" was typed, which has a meaning for completion.
3395 * Returns OK if completion was done, FAIL if something failed (out of mem).
3396 */
3397 static int
3398ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003399 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003401 char_u *line;
3402 int startcol = 0; /* column where searched text starts */
3403 colnr_T curs_col; /* cursor column */
3404 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
3406 if (c == Ctrl_P || c == Ctrl_L)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003407 compl_direction = BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003409 compl_direction = FORWARD;
3410 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 {
3412 /* First time we hit ^N or ^P (in a row, I mean) */
3413
3414 /* Turn off 'sm' so we don't show matches with ^X^L */
3415 save_sm = p_sm;
3416 p_sm = FALSE;
3417
3418 did_ai = FALSE;
3419#ifdef FEAT_SMARTINDENT
3420 did_si = FALSE;
3421 can_si = FALSE;
3422 can_si_back = FALSE;
3423#endif
3424 if (stop_arrow() == FAIL)
3425 return FAIL;
3426
3427 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003428 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003431 * "compl_startpos" to the cursor as a pattern to add a new word
3432 * instead of expand the one before the cursor, in word-wise if
3433 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 * is not in the same line as the cursor then fix it (the line has
3435 * been split because it was longer than 'tw'). if SOL is set then
3436 * skip the previous pattern, a word at the beginning of the line has
3437 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003438 if ((compl_cont_status & CONT_INTRPT) && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
3440 /*
3441 * it is a continued search
3442 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003443 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3445 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3446 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003447 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003449 /* line (probably) wrapped, set compl_startpos to the
3450 * first non_blank in the line, if it is not a wordchar
3451 * include it to get a better pattern, but then we don't
3452 * want the "\\<" prefix, check it bellow */
3453 compl_col = (colnr_T)(skipwhite(line) - line);
3454 compl_startpos.col = compl_col;
3455 compl_startpos.lnum = curwin->w_cursor.lnum;
3456 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 }
3458 else
3459 {
3460 /* S_IPOS was set when we inserted a word that was at the
3461 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003462 * mode but first we need to redefine compl_startpos */
3463 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003465 compl_cont_status |= CONT_SOL;
3466 compl_startpos.col = (colnr_T)(skipwhite(
3467 line + compl_length
3468 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003470 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003472 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003473 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 * have enough space? just being paranoic */
3475#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003476 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003478 compl_cont_status &= ~CONT_SOL;
3479 compl_length = (IOSIZE - MIN_SPACE);
3480 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003482 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3483 if (compl_length < 1)
3484 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 }
3486 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003487 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003489 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 }
3491 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003492 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003494 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003496 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003498 compl_cont_status = 0;
3499 compl_cont_status |= CONT_N_ADDS;
3500 compl_startpos = curwin->w_cursor;
3501 startcol = (int)curs_col;
3502 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
3504
3505 /* Work out completion pattern and original text -- webb */
3506 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3507 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003508 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3510 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003511 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003513 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003515 compl_col += ++startcol;
3516 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 }
3518 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003519 compl_pattern = str_foldcase(line + compl_col,
3520 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003522 compl_pattern = vim_strnsave(line + compl_col,
3523 compl_length);
3524 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 return FAIL;
3526 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003527 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
3529 char_u *prefix = (char_u *)"\\<";
3530
3531 /* we need 3 extra chars, 1 for the NUL and
3532 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003533 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3534 compl_length) + 3);
3535 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003537 if (!vim_iswordp(line + compl_col)
3538 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 && (
3540#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003541 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003543 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544#endif
3545 )))
3546 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003547 STRCPY((char *)compl_pattern, prefix);
3548 (void)quote_meta(compl_pattern + STRLEN(prefix),
3549 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003551 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003553 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003555 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556#endif
3557 )
3558 {
3559 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003560 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3561 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003563 compl_col += curs_col;
3564 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
3566 else
3567 {
3568#ifdef FEAT_MBYTE
3569 /* Search the point of change class of multibyte character
3570 * or not a word single byte character backward. */
3571 if (has_mbyte)
3572 {
3573 int base_class;
3574 int head_off;
3575
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003576 startcol -= (*mb_head_off)(line, line + startcol);
3577 base_class = mb_get_class(line + startcol);
3578 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003580 head_off = (*mb_head_off)(line, line + startcol);
3581 if (base_class != mb_get_class(line + startcol
3582 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003584 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
3586 }
3587 else
3588#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003589 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003591 compl_col += ++startcol;
3592 compl_length = (int)curs_col - startcol;
3593 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
3595 /* Only match word with at least two chars -- webb
3596 * there's no need to call quote_meta,
3597 * alloc(7) is enough -- Acevedo
3598 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003599 compl_pattern = alloc(7);
3600 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003602 STRCPY((char *)compl_pattern, "\\<");
3603 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3604 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 }
3606 else
3607 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003608 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3609 compl_length) + 3);
3610 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003612 STRCPY((char *)compl_pattern, "\\<");
3613 (void)quote_meta(compl_pattern + 2, line + compl_col,
3614 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
3616 }
3617 }
3618 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3619 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003620 compl_col = skipwhite(line) - line;
3621 compl_length = (int)curs_col - (int)compl_col;
3622 if (compl_length < 0) /* cursor in indent: empty pattern */
3623 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003625 compl_pattern = str_foldcase(line + compl_col, compl_length,
3626 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003628 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3629 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 return FAIL;
3631 }
3632 else if (ctrl_x_mode == CTRL_X_FILES)
3633 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003634 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003636 compl_col += ++startcol;
3637 compl_length = (int)curs_col - startcol;
3638 compl_pattern = addstar(line + compl_col, compl_length,
3639 EXPAND_FILES);
3640 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 return FAIL;
3642 }
3643 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3644 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003645 compl_pattern = vim_strnsave(line, curs_col);
3646 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003648 set_cmd_context(&compl_xp, compl_pattern,
3649 (int)STRLEN(compl_pattern), curs_col);
3650 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3651 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003653 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
3654 compl_col = startcol;
3655 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003657 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003658 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00003659#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003660 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00003661 * Call user defined function 'completefunc' with "a:findstart"
3662 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003663 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00003664 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003665 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003666 char_u *funcname;
3667 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003668
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003669 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00003670 * string */
3671 funcname = ctrl_x_mode == CTRL_X_FUNCTION
3672 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3673 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003674 {
3675 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3676 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003677 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003678 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003679
3680 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003681 args[1] = NULL;
3682 pos = curwin->w_cursor;
3683 col = call_func_retnr(funcname, 2, args, FALSE);
3684 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003685
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003686 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003687 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003688 compl_col = col;
3689 if ((colnr_T)compl_col > curs_col)
3690 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003691
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003692 /* Setup variables for completion. Need to obtain "line" again,
3693 * it may have become invalid. */
3694 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003695 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003696 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3697 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003698#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003699 return FAIL;
3700 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00003701 else if (ctrl_x_mode == CTRL_X_SPELL)
3702 {
3703#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00003704 if (spell_bad_len > 0)
3705 compl_col = curs_col - spell_bad_len;
3706 else
3707 compl_col = spell_word_start(startcol);
3708 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00003709 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003710 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003711 compl_length = (int)curs_col - compl_col;
3712 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3713 if (compl_pattern == NULL)
3714#endif
3715 return FAIL;
3716 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003717 else
3718 {
3719 EMSG2(_(e_intern2), "ins_complete()");
3720 return FAIL;
3721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003723 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
3725 edit_submode_pre = (char_u *)_(" Adding");
3726 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3727 {
3728 /* Insert a new line, keep indentation but ignore 'comments' */
3729#ifdef FEAT_COMMENTS
3730 char_u *old = curbuf->b_p_com;
3731
3732 curbuf->b_p_com = (char_u *)"";
3733#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003734 compl_startpos.lnum = curwin->w_cursor.lnum;
3735 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 ins_eol('\r');
3737#ifdef FEAT_COMMENTS
3738 curbuf->b_p_com = old;
3739#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003740 compl_length = 0;
3741 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
3743 }
3744 else
3745 {
3746 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003747 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
3749
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003750 if (compl_cont_status & CONT_LOCAL)
3751 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 else
3753 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
3754
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003756 * "compl_orig_text" itself (not a copy) is added, it will be freed
3757 * when the list of matches is freed. */
3758 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
3759 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
3760 -1, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003762 vim_free(compl_pattern);
3763 compl_pattern = NULL;
3764 vim_free(compl_orig_text);
3765 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 return FAIL;
3767 }
3768
3769 /* showmode might reset the internal line pointers, so it must
3770 * be called before line = ml_get(), or when this address is no
3771 * longer needed. -- Acevedo.
3772 */
3773 edit_submode_extra = (char_u *)_("-- Searching...");
3774 edit_submode_highl = HLF_COUNT;
3775 showmode();
3776 edit_submode_extra = NULL;
3777 out_flush();
3778 }
3779
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003780 compl_shown_match = compl_curr_match;
3781 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
3783 /*
3784 * Find next match.
3785 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003786 n = ins_compl_next(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003788 /* may undisplay the popup menu */
3789 ins_compl_upd_pum();
3790
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003791 if (n > 1) /* all matches have been found */
3792 compl_matches = n;
3793 compl_curr_match = compl_shown_match;
3794 compl_direction = compl_shows_dir;
3795 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796
3797 /* eat the ESC to avoid leaving insert mode */
3798 if (got_int && !global_busy)
3799 {
3800 (void)vgetc();
3801 got_int = FALSE;
3802 }
3803
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003804 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003805 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003807 edit_submode_extra = (compl_cont_status & CONT_ADDING)
3808 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
3810 edit_submode_highl = HLF_E;
3811 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
3812 * because we couldn't expand anything at first place, but if we used
3813 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
3814 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003815 if ( compl_length > 1
3816 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 || (ctrl_x_mode != 0
3818 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
3819 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003820 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822
Bram Moolenaar572cb562005-08-05 21:35:02 +00003823 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003824 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003826 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827
3828 if (edit_submode_extra == NULL)
3829 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003830 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 {
3832 edit_submode_extra = (char_u *)_("Back at original");
3833 edit_submode_highl = HLF_W;
3834 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003835 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 {
3837 edit_submode_extra = (char_u *)_("Word from other line");
3838 edit_submode_highl = HLF_COUNT;
3839 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00003840 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
3842 edit_submode_extra = (char_u *)_("The only match");
3843 edit_submode_highl = HLF_COUNT;
3844 }
3845 else
3846 {
3847 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003848 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003850 int number = 0;
3851 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003853 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
3855 /* search backwards for the first valid (!= -1) number.
3856 * This should normally succeed already at the first loop
3857 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003858 for (match = compl_curr_match->cp_prev; match != NULL
3859 && match != compl_first_match;
3860 match = match->cp_prev)
3861 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003863 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 break;
3865 }
3866 if (match != NULL)
3867 /* go up and assign all numbers which are not assigned
3868 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003869 for (match = match->cp_next;
3870 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003871 match = match->cp_next)
3872 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 }
3874 else /* BACKWARD */
3875 {
3876 /* search forwards (upwards) for the first valid (!= -1)
3877 * number. This should normally succeed already at the
3878 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003879 for (match = compl_curr_match->cp_next; match != NULL
3880 && match != compl_first_match;
3881 match = match->cp_next)
3882 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003884 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 break;
3886 }
3887 if (match != NULL)
3888 /* go down and assign all numbers which are not
3889 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003890 for (match = match->cp_prev; match
3891 && match->cp_number == -1;
3892 match = match->cp_prev)
3893 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
3895 }
3896
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003897 /* The match should always have a sequence number now, this is
3898 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003899 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 {
3901 /* Space for 10 text chars. + 2x10-digit no.s */
3902 static char_u match_ref[31];
3903
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003904 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00003906 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003908 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00003909 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003910 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 edit_submode_extra = match_ref;
3912 edit_submode_highl = HLF_R;
3913 if (dollar_vcol)
3914 curs_columns(FALSE);
3915 }
3916 }
3917 }
3918
3919 /* Show a message about what (completion) mode we're in. */
3920 showmode();
3921 if (edit_submode_extra != NULL)
3922 {
3923 if (!p_smd)
3924 msg_attr(edit_submode_extra,
3925 edit_submode_highl < HLF_COUNT
3926 ? hl_attr(edit_submode_highl) : 0);
3927 }
3928 else
3929 msg_clr_cmdline(); /* necessary for "noshowmode" */
3930
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003931 ins_compl_show_pum();
3932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 return OK;
3934}
3935
3936/*
3937 * Looks in the first "len" chars. of "src" for search-metachars.
3938 * If dest is not NULL the chars. are copied there quoting (with
3939 * a backslash) the metachars, and dest would be NUL terminated.
3940 * Returns the length (needed) of dest
3941 */
3942 static int
3943quote_meta(dest, src, len)
3944 char_u *dest;
3945 char_u *src;
3946 int len;
3947{
3948 int m;
3949
3950 for (m = len; --len >= 0; src++)
3951 {
3952 switch (*src)
3953 {
3954 case '.':
3955 case '*':
3956 case '[':
3957 if (ctrl_x_mode == CTRL_X_DICTIONARY
3958 || ctrl_x_mode == CTRL_X_THESAURUS)
3959 break;
3960 case '~':
3961 if (!p_magic) /* quote these only if magic is set */
3962 break;
3963 case '\\':
3964 if (ctrl_x_mode == CTRL_X_DICTIONARY
3965 || ctrl_x_mode == CTRL_X_THESAURUS)
3966 break;
3967 case '^': /* currently it's not needed. */
3968 case '$':
3969 m++;
3970 if (dest != NULL)
3971 *dest++ = '\\';
3972 break;
3973 }
3974 if (dest != NULL)
3975 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003976# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 /* Copy remaining bytes of a multibyte character. */
3978 if (has_mbyte)
3979 {
3980 int i, mb_len;
3981
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003982 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 if (mb_len > 0 && len >= mb_len)
3984 for (i = 0; i < mb_len; ++i)
3985 {
3986 --len;
3987 ++src;
3988 if (dest != NULL)
3989 *dest++ = *src;
3990 }
3991 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00003992# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994 if (dest != NULL)
3995 *dest = NUL;
3996
3997 return m;
3998}
3999#endif /* FEAT_INS_EXPAND */
4000
4001/*
4002 * Next character is interpreted literally.
4003 * A one, two or three digit decimal number is interpreted as its byte value.
4004 * If one or two digits are entered, the next character is given to vungetc().
4005 * For Unicode a character > 255 may be returned.
4006 */
4007 int
4008get_literal()
4009{
4010 int cc;
4011 int nc;
4012 int i;
4013 int hex = FALSE;
4014 int octal = FALSE;
4015#ifdef FEAT_MBYTE
4016 int unicode = 0;
4017#endif
4018
4019 if (got_int)
4020 return Ctrl_C;
4021
4022#ifdef FEAT_GUI
4023 /*
4024 * In GUI there is no point inserting the internal code for a special key.
4025 * It is more useful to insert the string "<KEY>" instead. This would
4026 * probably be useful in a text window too, but it would not be
4027 * vi-compatible (maybe there should be an option for it?) -- webb
4028 */
4029 if (gui.in_use)
4030 ++allow_keys;
4031#endif
4032#ifdef USE_ON_FLY_SCROLL
4033 dont_scroll = TRUE; /* disallow scrolling here */
4034#endif
4035 ++no_mapping; /* don't map the next key hits */
4036 cc = 0;
4037 i = 0;
4038 for (;;)
4039 {
4040 do
4041 nc = safe_vgetc();
4042 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4043 || nc == K_HOR_SCROLLBAR);
4044#ifdef FEAT_CMDL_INFO
4045 if (!(State & CMDLINE)
4046# ifdef FEAT_MBYTE
4047 && MB_BYTE2LEN_CHECK(nc) == 1
4048# endif
4049 )
4050 add_to_showcmd(nc);
4051#endif
4052 if (nc == 'x' || nc == 'X')
4053 hex = TRUE;
4054 else if (nc == 'o' || nc == 'O')
4055 octal = TRUE;
4056#ifdef FEAT_MBYTE
4057 else if (nc == 'u' || nc == 'U')
4058 unicode = nc;
4059#endif
4060 else
4061 {
4062 if (hex
4063#ifdef FEAT_MBYTE
4064 || unicode != 0
4065#endif
4066 )
4067 {
4068 if (!vim_isxdigit(nc))
4069 break;
4070 cc = cc * 16 + hex2nr(nc);
4071 }
4072 else if (octal)
4073 {
4074 if (nc < '0' || nc > '7')
4075 break;
4076 cc = cc * 8 + nc - '0';
4077 }
4078 else
4079 {
4080 if (!VIM_ISDIGIT(nc))
4081 break;
4082 cc = cc * 10 + nc - '0';
4083 }
4084
4085 ++i;
4086 }
4087
4088 if (cc > 255
4089#ifdef FEAT_MBYTE
4090 && unicode == 0
4091#endif
4092 )
4093 cc = 255; /* limit range to 0-255 */
4094 nc = 0;
4095
4096 if (hex) /* hex: up to two chars */
4097 {
4098 if (i >= 2)
4099 break;
4100 }
4101#ifdef FEAT_MBYTE
4102 else if (unicode) /* Unicode: up to four or eight chars */
4103 {
4104 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4105 break;
4106 }
4107#endif
4108 else if (i >= 3) /* decimal or octal: up to three chars */
4109 break;
4110 }
4111 if (i == 0) /* no number entered */
4112 {
4113 if (nc == K_ZERO) /* NUL is stored as NL */
4114 {
4115 cc = '\n';
4116 nc = 0;
4117 }
4118 else
4119 {
4120 cc = nc;
4121 nc = 0;
4122 }
4123 }
4124
4125 if (cc == 0) /* NUL is stored as NL */
4126 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004127#ifdef FEAT_MBYTE
4128 if (enc_dbcs && (cc & 0xff) == 0)
4129 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4130 second byte will cause trouble! */
4131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
4133 --no_mapping;
4134#ifdef FEAT_GUI
4135 if (gui.in_use)
4136 --allow_keys;
4137#endif
4138 if (nc)
4139 vungetc(nc);
4140 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4141 return cc;
4142}
4143
4144/*
4145 * Insert character, taking care of special keys and mod_mask
4146 */
4147 static void
4148insert_special(c, allow_modmask, ctrlv)
4149 int c;
4150 int allow_modmask;
4151 int ctrlv; /* c was typed after CTRL-V */
4152{
4153 char_u *p;
4154 int len;
4155
4156 /*
4157 * Special function key, translate into "<Key>". Up to the last '>' is
4158 * inserted with ins_str(), so as not to replace characters in replace
4159 * mode.
4160 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4161 * unless 'allow_modmask' is TRUE.
4162 */
4163#ifdef MACOS
4164 /* Command-key never produces a normal key */
4165 if (mod_mask & MOD_MASK_CMD)
4166 allow_modmask = TRUE;
4167#endif
4168 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4169 {
4170 p = get_special_key_name(c, mod_mask);
4171 len = (int)STRLEN(p);
4172 c = p[len - 1];
4173 if (len > 2)
4174 {
4175 if (stop_arrow() == FAIL)
4176 return;
4177 p[len - 1] = NUL;
4178 ins_str(p);
4179 AppendToRedobuffLit(p);
4180 ctrlv = FALSE;
4181 }
4182 }
4183 if (stop_arrow() == OK)
4184 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4185}
4186
4187/*
4188 * Special characters in this context are those that need processing other
4189 * than the simple insertion that can be performed here. This includes ESC
4190 * which terminates the insert, and CR/NL which need special processing to
4191 * open up a new line. This routine tries to optimize insertions performed by
4192 * the "redo", "undo" or "put" commands, so it needs to know when it should
4193 * stop and defer processing to the "normal" mechanism.
4194 * '0' and '^' are special, because they can be followed by CTRL-D.
4195 */
4196#ifdef EBCDIC
4197# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4198#else
4199# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4200#endif
4201
4202#ifdef FEAT_MBYTE
4203# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4204#else
4205# define WHITECHAR(cc) vim_iswhite(cc)
4206#endif
4207
4208 void
4209insertchar(c, flags, second_indent)
4210 int c; /* character to insert or NUL */
4211 int flags; /* INSCHAR_FORMAT, etc. */
4212 int second_indent; /* indent for second line if >= 0 */
4213{
4214 int haveto_redraw = FALSE;
4215 int textwidth;
4216#ifdef FEAT_COMMENTS
4217 colnr_T leader_len;
4218 char_u *p;
4219 int no_leader = FALSE;
4220 int do_comments = (flags & INSCHAR_DO_COM);
4221#endif
4222 int fo_white_par;
4223 int first_line = TRUE;
4224 int fo_ins_blank;
4225#ifdef FEAT_MBYTE
4226 int fo_multibyte;
4227#endif
4228 int save_char = NUL;
4229 int cc;
4230
4231 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4232 fo_ins_blank = has_format_option(FO_INS_BLANK);
4233#ifdef FEAT_MBYTE
4234 fo_multibyte = has_format_option(FO_MBYTE_BREAK);
4235#endif
4236 fo_white_par = has_format_option(FO_WHITE_PAR);
4237
4238 /*
4239 * Try to break the line in two or more pieces when:
4240 * - Always do this if we have been called to do formatting only.
4241 * - Always do this when 'formatoptions' has the 'a' flag and the line
4242 * ends in white space.
4243 * - Otherwise:
4244 * - Don't do this if inserting a blank
4245 * - Don't do this if an existing character is being replaced, unless
4246 * we're in VREPLACE mode.
4247 * - Do this if the cursor is not on the line where insert started
4248 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4249 * before the insert.
4250 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4251 * before 'textwidth'
4252 */
4253 if (textwidth
4254 && ((flags & INSCHAR_FORMAT)
4255 || (!vim_iswhite(c)
4256 && !((State & REPLACE_FLAG)
4257#ifdef FEAT_VREPLACE
4258 && !(State & VREPLACE_FLAG)
4259#endif
4260 && *ml_get_cursor() != NUL)
4261 && (curwin->w_cursor.lnum != Insstart.lnum
4262 || ((!has_format_option(FO_INS_LONG)
4263 || Insstart_textlen <= (colnr_T)textwidth)
4264 && (!fo_ins_blank
4265 || Insstart_blank_vcol <= (colnr_T)textwidth
4266 ))))))
4267 {
4268 /*
4269 * When 'ai' is off we don't want a space under the cursor to be
4270 * deleted. Replace it with an 'x' temporarily.
4271 */
4272 if (!curbuf->b_p_ai)
4273 {
4274 cc = gchar_cursor();
4275 if (vim_iswhite(cc))
4276 {
4277 save_char = cc;
4278 pchar_cursor('x');
4279 }
4280 }
4281
4282 /*
4283 * Repeat breaking lines, until the current line is not too long.
4284 */
4285 while (!got_int)
4286 {
4287 int startcol; /* Cursor column at entry */
4288 int wantcol; /* column at textwidth border */
4289 int foundcol; /* column for start of spaces */
4290 int end_foundcol = 0; /* column for start of word */
4291 colnr_T len;
4292 colnr_T virtcol;
4293#ifdef FEAT_VREPLACE
4294 int orig_col = 0;
4295 char_u *saved_text = NULL;
4296#endif
4297 colnr_T col;
4298
4299 virtcol = get_nolist_virtcol();
4300 if (virtcol < (colnr_T)textwidth)
4301 break;
4302
4303#ifdef FEAT_COMMENTS
4304 if (no_leader)
4305 do_comments = FALSE;
4306 else if (!(flags & INSCHAR_FORMAT)
4307 && has_format_option(FO_WRAP_COMS))
4308 do_comments = TRUE;
4309
4310 /* Don't break until after the comment leader */
4311 if (do_comments)
4312 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
4313 else
4314 leader_len = 0;
4315
4316 /* If the line doesn't start with a comment leader, then don't
4317 * start one in a following broken line. Avoids that a %word
4318 * moved to the start of the next line causes all following lines
4319 * to start with %. */
4320 if (leader_len == 0)
4321 no_leader = TRUE;
4322#endif
4323 if (!(flags & INSCHAR_FORMAT)
4324#ifdef FEAT_COMMENTS
4325 && leader_len == 0
4326#endif
4327 && !has_format_option(FO_WRAP))
4328
4329 {
4330 textwidth = 0;
4331 break;
4332 }
4333 if ((startcol = curwin->w_cursor.col) == 0)
4334 break;
4335
4336 /* find column of textwidth border */
4337 coladvance((colnr_T)textwidth);
4338 wantcol = curwin->w_cursor.col;
4339
4340 curwin->w_cursor.col = startcol - 1;
4341#ifdef FEAT_MBYTE
4342 /* Correct cursor for multi-byte character. */
4343 if (has_mbyte)
4344 mb_adjust_cursor();
4345#endif
4346 foundcol = 0;
4347
4348 /*
4349 * Find position to break at.
4350 * Stop at first entered white when 'formatoptions' has 'v'
4351 */
4352 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
4353 || curwin->w_cursor.lnum != Insstart.lnum
4354 || curwin->w_cursor.col >= Insstart.col)
4355 {
4356 cc = gchar_cursor();
4357 if (WHITECHAR(cc))
4358 {
4359 /* remember position of blank just before text */
4360 end_foundcol = curwin->w_cursor.col;
4361
4362 /* find start of sequence of blanks */
4363 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
4364 {
4365 dec_cursor();
4366 cc = gchar_cursor();
4367 }
4368 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
4369 break; /* only spaces in front of text */
4370#ifdef FEAT_COMMENTS
4371 /* Don't break until after the comment leader */
4372 if (curwin->w_cursor.col < leader_len)
4373 break;
4374#endif
4375 if (has_format_option(FO_ONE_LETTER))
4376 {
4377 /* do not break after one-letter words */
4378 if (curwin->w_cursor.col == 0)
4379 break; /* one-letter word at begin */
4380
4381 col = curwin->w_cursor.col;
4382 dec_cursor();
4383 cc = gchar_cursor();
4384
4385 if (WHITECHAR(cc))
4386 continue; /* one-letter, continue */
4387 curwin->w_cursor.col = col;
4388 }
4389#ifdef FEAT_MBYTE
4390 if (has_mbyte)
4391 foundcol = curwin->w_cursor.col
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004392 + (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 else
4394#endif
4395 foundcol = curwin->w_cursor.col + 1;
4396 if (curwin->w_cursor.col < (colnr_T)wantcol)
4397 break;
4398 }
4399#ifdef FEAT_MBYTE
4400 else if (cc >= 0x100 && fo_multibyte
4401 && curwin->w_cursor.col <= (colnr_T)wantcol)
4402 {
4403 /* Break after or before a multi-byte character. */
4404 foundcol = curwin->w_cursor.col;
4405 if (curwin->w_cursor.col < (colnr_T)wantcol)
4406 foundcol += (*mb_char2len)(cc);
4407 end_foundcol = foundcol;
4408 break;
4409 }
4410#endif
4411 if (curwin->w_cursor.col == 0)
4412 break;
4413 dec_cursor();
4414 }
4415
4416 if (foundcol == 0) /* no spaces, cannot break line */
4417 {
4418 curwin->w_cursor.col = startcol;
4419 break;
4420 }
4421
4422 /* Going to break the line, remove any "$" now. */
4423 undisplay_dollar();
4424
4425 /*
4426 * Offset between cursor position and line break is used by replace
4427 * stack functions. VREPLACE does not use this, and backspaces
4428 * over the text instead.
4429 */
4430#ifdef FEAT_VREPLACE
4431 if (State & VREPLACE_FLAG)
4432 orig_col = startcol; /* Will start backspacing from here */
4433 else
4434#endif
4435 replace_offset = startcol - end_foundcol - 1;
4436
4437 /*
4438 * adjust startcol for spaces that will be deleted and
4439 * characters that will remain on top line
4440 */
4441 curwin->w_cursor.col = foundcol;
4442 while (cc = gchar_cursor(), WHITECHAR(cc))
4443 inc_cursor();
4444 startcol -= curwin->w_cursor.col;
4445 if (startcol < 0)
4446 startcol = 0;
4447
4448#ifdef FEAT_VREPLACE
4449 if (State & VREPLACE_FLAG)
4450 {
4451 /*
4452 * In VREPLACE mode, we will backspace over the text to be
4453 * wrapped, so save a copy now to put on the next line.
4454 */
4455 saved_text = vim_strsave(ml_get_cursor());
4456 curwin->w_cursor.col = orig_col;
4457 if (saved_text == NULL)
4458 break; /* Can't do it, out of memory */
4459 saved_text[startcol] = NUL;
4460
4461 /* Backspace over characters that will move to the next line */
4462 if (!fo_white_par)
4463 backspace_until_column(foundcol);
4464 }
4465 else
4466#endif
4467 {
4468 /* put cursor after pos. to break line */
4469 if (!fo_white_par)
4470 curwin->w_cursor.col = foundcol;
4471 }
4472
4473 /*
4474 * Split the line just before the margin.
4475 * Only insert/delete lines, but don't really redraw the window.
4476 */
4477 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
4478 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
4479#ifdef FEAT_COMMENTS
4480 + (do_comments ? OPENLINE_DO_COM : 0)
4481#endif
4482 , old_indent);
4483 old_indent = 0;
4484
4485 replace_offset = 0;
4486 if (first_line)
4487 {
4488 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
4489 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
4490 if (second_indent >= 0)
4491 {
4492#ifdef FEAT_VREPLACE
4493 if (State & VREPLACE_FLAG)
4494 change_indent(INDENT_SET, second_indent, FALSE, NUL);
4495 else
4496#endif
4497 (void)set_indent(second_indent, SIN_CHANGED);
4498 }
4499 first_line = FALSE;
4500 }
4501
4502#ifdef FEAT_VREPLACE
4503 if (State & VREPLACE_FLAG)
4504 {
4505 /*
4506 * In VREPLACE mode we have backspaced over the text to be
4507 * moved, now we re-insert it into the new line.
4508 */
4509 ins_bytes(saved_text);
4510 vim_free(saved_text);
4511 }
4512 else
4513#endif
4514 {
4515 /*
4516 * Check if cursor is not past the NUL off the line, cindent
4517 * may have added or removed indent.
4518 */
4519 curwin->w_cursor.col += startcol;
4520 len = (colnr_T)STRLEN(ml_get_curline());
4521 if (curwin->w_cursor.col > len)
4522 curwin->w_cursor.col = len;
4523 }
4524
4525 haveto_redraw = TRUE;
4526#ifdef FEAT_CINDENT
4527 can_cindent = TRUE;
4528#endif
4529 /* moved the cursor, don't autoindent or cindent now */
4530 did_ai = FALSE;
4531#ifdef FEAT_SMARTINDENT
4532 did_si = FALSE;
4533 can_si = FALSE;
4534 can_si_back = FALSE;
4535#endif
4536 line_breakcheck();
4537 }
4538
4539 if (save_char) /* put back space after cursor */
4540 pchar_cursor(save_char);
4541
4542 if (c == NUL) /* formatting only */
4543 return;
4544 if (haveto_redraw)
4545 {
4546 update_topline();
4547 redraw_curbuf_later(VALID);
4548 }
4549 }
4550 if (c == NUL) /* only formatting was wanted */
4551 return;
4552
4553#ifdef FEAT_COMMENTS
4554 /* Check whether this character should end a comment. */
4555 if (did_ai && (int)c == end_comment_pending)
4556 {
4557 char_u *line;
4558 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4559 int middle_len, end_len;
4560 int i;
4561
4562 /*
4563 * Need to remove existing (middle) comment leader and insert end
4564 * comment leader. First, check what comment leader we can find.
4565 */
4566 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4567 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4568 {
4569 /* Skip middle-comment string */
4570 while (*p && p[-1] != ':') /* find end of middle flags */
4571 ++p;
4572 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4573 /* Don't count trailing white space for middle_len */
4574 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4575 --middle_len;
4576
4577 /* Find the end-comment string */
4578 while (*p && p[-1] != ':') /* find end of end flags */
4579 ++p;
4580 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4581
4582 /* Skip white space before the cursor */
4583 i = curwin->w_cursor.col;
4584 while (--i >= 0 && vim_iswhite(line[i]))
4585 ;
4586 i++;
4587
4588 /* Skip to before the middle leader */
4589 i -= middle_len;
4590
4591 /* Check some expected things before we go on */
4592 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4593 {
4594 /* Backspace over all the stuff we want to replace */
4595 backspace_until_column(i);
4596
4597 /*
4598 * Insert the end-comment string, except for the last
4599 * character, which will get inserted as normal later.
4600 */
4601 ins_bytes_len(lead_end, end_len - 1);
4602 }
4603 }
4604 }
4605 end_comment_pending = NUL;
4606#endif
4607
4608 did_ai = FALSE;
4609#ifdef FEAT_SMARTINDENT
4610 did_si = FALSE;
4611 can_si = FALSE;
4612 can_si_back = FALSE;
4613#endif
4614
4615 /*
4616 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4617 * This speeds up normal text input considerably.
4618 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4619 * need to re-indent at a ':', or any other character (but not what
4620 * 'paste' is set)..
4621 */
4622#ifdef USE_ON_FLY_SCROLL
4623 dont_scroll = FALSE; /* allow scrolling here */
4624#endif
4625
4626 if ( !ISSPECIAL(c)
4627#ifdef FEAT_MBYTE
4628 && (!has_mbyte || (*mb_char2len)(c) == 1)
4629#endif
4630 && vpeekc() != NUL
4631 && !(State & REPLACE_FLAG)
4632#ifdef FEAT_CINDENT
4633 && !cindent_on()
4634#endif
4635#ifdef FEAT_RIGHTLEFT
4636 && !p_ri
4637#endif
4638 )
4639 {
4640#define INPUT_BUFLEN 100
4641 char_u buf[INPUT_BUFLEN + 1];
4642 int i;
4643 colnr_T virtcol = 0;
4644
4645 buf[0] = c;
4646 i = 1;
4647 if (textwidth)
4648 virtcol = get_nolist_virtcol();
4649 /*
4650 * Stop the string when:
4651 * - no more chars available
4652 * - finding a special character (command key)
4653 * - buffer is full
4654 * - running into the 'textwidth' boundary
4655 * - need to check for abbreviation: A non-word char after a word-char
4656 */
4657 while ( (c = vpeekc()) != NUL
4658 && !ISSPECIAL(c)
4659#ifdef FEAT_MBYTE
4660 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4661#endif
4662 && i < INPUT_BUFLEN
4663 && (textwidth == 0
4664 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4665 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4666 {
4667#ifdef FEAT_RIGHTLEFT
4668 c = vgetc();
4669 if (p_hkmap && KeyTyped)
4670 c = hkmap(c); /* Hebrew mode mapping */
4671# ifdef FEAT_FKMAP
4672 if (p_fkmap && KeyTyped)
4673 c = fkmap(c); /* Farsi mode mapping */
4674# endif
4675 buf[i++] = c;
4676#else
4677 buf[i++] = vgetc();
4678#endif
4679 }
4680
4681#ifdef FEAT_DIGRAPHS
4682 do_digraph(-1); /* clear digraphs */
4683 do_digraph(buf[i-1]); /* may be the start of a digraph */
4684#endif
4685 buf[i] = NUL;
4686 ins_str(buf);
4687 if (flags & INSCHAR_CTRLV)
4688 {
4689 redo_literal(*buf);
4690 i = 1;
4691 }
4692 else
4693 i = 0;
4694 if (buf[i] != NUL)
4695 AppendToRedobuffLit(buf + i);
4696 }
4697 else
4698 {
4699#ifdef FEAT_MBYTE
4700 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
4701 {
4702 char_u buf[MB_MAXBYTES + 1];
4703
4704 (*mb_char2bytes)(c, buf);
4705 buf[cc] = NUL;
4706 ins_char_bytes(buf, cc);
4707 AppendCharToRedobuff(c);
4708 }
4709 else
4710#endif
4711 {
4712 ins_char(c);
4713 if (flags & INSCHAR_CTRLV)
4714 redo_literal(c);
4715 else
4716 AppendCharToRedobuff(c);
4717 }
4718 }
4719}
4720
4721/*
4722 * Called after inserting or deleting text: When 'formatoptions' includes the
4723 * 'a' flag format from the current line until the end of the paragraph.
4724 * Keep the cursor at the same position relative to the text.
4725 * The caller must have saved the cursor line for undo, following ones will be
4726 * saved here.
4727 */
4728 void
4729auto_format(trailblank, prev_line)
4730 int trailblank; /* when TRUE also format with trailing blank */
4731 int prev_line; /* may start in previous line */
4732{
4733 pos_T pos;
4734 colnr_T len;
4735 char_u *old;
4736 char_u *new, *pnew;
4737 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004738 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739
4740 if (!has_format_option(FO_AUTO))
4741 return;
4742
4743 pos = curwin->w_cursor;
4744 old = ml_get_curline();
4745
4746 /* may remove added space */
4747 check_auto_format(FALSE);
4748
4749 /* Don't format in Insert mode when the cursor is on a trailing blank, the
4750 * user might insert normal text next. Also skip formatting when "1" is
4751 * in 'formatoptions' and there is a single character before the cursor.
4752 * Otherwise the line would be broken and when typing another non-white
4753 * next they are not joined back together. */
4754 wasatend = (pos.col == STRLEN(old));
4755 if (*old != NUL && !trailblank && wasatend)
4756 {
4757 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004758 cc = gchar_cursor();
4759 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
4760 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004762 cc = gchar_cursor();
4763 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
4765 curwin->w_cursor = pos;
4766 return;
4767 }
4768 curwin->w_cursor = pos;
4769 }
4770
4771#ifdef FEAT_COMMENTS
4772 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
4773 * comments. */
4774 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
4775 && get_leader_len(old, NULL, FALSE) == 0)
4776 return;
4777#endif
4778
4779 /*
4780 * May start formatting in a previous line, so that after "x" a word is
4781 * moved to the previous line if it fits there now. Only when this is not
4782 * the start of a paragraph.
4783 */
4784 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
4785 {
4786 --curwin->w_cursor.lnum;
4787 if (u_save_cursor() == FAIL)
4788 return;
4789 }
4790
4791 /*
4792 * Do the formatting and restore the cursor position. "saved_cursor" will
4793 * be adjusted for the text formatting.
4794 */
4795 saved_cursor = pos;
4796 format_lines((linenr_T)-1);
4797 curwin->w_cursor = saved_cursor;
4798 saved_cursor.lnum = 0;
4799
4800 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4801 {
4802 /* "cannot happen" */
4803 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4804 coladvance((colnr_T)MAXCOL);
4805 }
4806 else
4807 check_cursor_col();
4808
4809 /* Insert mode: If the cursor is now after the end of the line while it
4810 * previously wasn't, the line was broken. Because of the rule above we
4811 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
4812 * formatted. */
4813 if (!wasatend && has_format_option(FO_WHITE_PAR))
4814 {
4815 new = ml_get_curline();
4816 len = STRLEN(new);
4817 if (curwin->w_cursor.col == len)
4818 {
4819 pnew = vim_strnsave(new, len + 2);
4820 pnew[len] = ' ';
4821 pnew[len + 1] = NUL;
4822 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
4823 /* remove the space later */
4824 did_add_space = TRUE;
4825 }
4826 else
4827 /* may remove added space */
4828 check_auto_format(FALSE);
4829 }
4830
4831 check_cursor();
4832}
4833
4834/*
4835 * When an extra space was added to continue a paragraph for auto-formatting,
4836 * delete it now. The space must be under the cursor, just after the insert
4837 * position.
4838 */
4839 static void
4840check_auto_format(end_insert)
4841 int end_insert; /* TRUE when ending Insert mode */
4842{
4843 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004844 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845
4846 if (did_add_space)
4847 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004848 cc = gchar_cursor();
4849 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 /* Somehow the space was removed already. */
4851 did_add_space = FALSE;
4852 else
4853 {
4854 if (!end_insert)
4855 {
4856 inc_cursor();
4857 c = gchar_cursor();
4858 dec_cursor();
4859 }
4860 if (c != NUL)
4861 {
4862 /* The space is no longer at the end of the line, delete it. */
4863 del_char(FALSE);
4864 did_add_space = FALSE;
4865 }
4866 }
4867 }
4868}
4869
4870/*
4871 * Find out textwidth to be used for formatting:
4872 * if 'textwidth' option is set, use it
4873 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
4874 * if invalid value, use 0.
4875 * Set default to window width (maximum 79) for "gq" operator.
4876 */
4877 int
4878comp_textwidth(ff)
4879 int ff; /* force formatting (for "Q" command) */
4880{
4881 int textwidth;
4882
4883 textwidth = curbuf->b_p_tw;
4884 if (textwidth == 0 && curbuf->b_p_wm)
4885 {
4886 /* The width is the window width minus 'wrapmargin' minus all the
4887 * things that add to the margin. */
4888 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
4889#ifdef FEAT_CMDWIN
4890 if (cmdwin_type != 0)
4891 textwidth -= 1;
4892#endif
4893#ifdef FEAT_FOLDING
4894 textwidth -= curwin->w_p_fdc;
4895#endif
4896#ifdef FEAT_SIGNS
4897 if (curwin->w_buffer->b_signlist != NULL
4898# ifdef FEAT_NETBEANS_INTG
4899 || usingNetbeans
4900# endif
4901 )
4902 textwidth -= 1;
4903#endif
4904 if (curwin->w_p_nu)
4905 textwidth -= 8;
4906 }
4907 if (textwidth < 0)
4908 textwidth = 0;
4909 if (ff && textwidth == 0)
4910 {
4911 textwidth = W_WIDTH(curwin) - 1;
4912 if (textwidth > 79)
4913 textwidth = 79;
4914 }
4915 return textwidth;
4916}
4917
4918/*
4919 * Put a character in the redo buffer, for when just after a CTRL-V.
4920 */
4921 static void
4922redo_literal(c)
4923 int c;
4924{
4925 char_u buf[10];
4926
4927 /* Only digits need special treatment. Translate them into a string of
4928 * three digits. */
4929 if (VIM_ISDIGIT(c))
4930 {
4931 sprintf((char *)buf, "%03d", c);
4932 AppendToRedobuff(buf);
4933 }
4934 else
4935 AppendCharToRedobuff(c);
4936}
4937
4938/*
4939 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00004940 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 */
4942 static void
4943start_arrow(end_insert_pos)
4944 pos_T *end_insert_pos;
4945{
4946 if (!arrow_used) /* something has been inserted */
4947 {
4948 AppendToRedobuff(ESC_STR);
4949 stop_insert(end_insert_pos, FALSE);
4950 arrow_used = TRUE; /* this means we stopped the current insert */
4951 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004952#ifdef FEAT_SYN_HL
4953 check_spell_redraw();
4954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955}
4956
Bram Moolenaar217ad922005-03-20 22:37:15 +00004957#ifdef FEAT_SYN_HL
4958/*
4959 * If we skipped highlighting word at cursor, do it now.
4960 * It may be skipped again, thus reset spell_redraw_lnum first.
4961 */
4962 static void
4963check_spell_redraw()
4964{
4965 if (spell_redraw_lnum != 0)
4966 {
4967 linenr_T lnum = spell_redraw_lnum;
4968
4969 spell_redraw_lnum = 0;
4970 redrawWinline(lnum, FALSE);
4971 }
4972}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00004973
4974/*
4975 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
4976 * spelled word, if there is one.
4977 */
4978 static void
4979spell_back_to_badword()
4980{
4981 pos_T tpos = curwin->w_cursor;
4982
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00004983 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00004984 if (curwin->w_cursor.col != tpos.col)
4985 start_arrow(&tpos);
4986}
Bram Moolenaar217ad922005-03-20 22:37:15 +00004987#endif
4988
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989/*
4990 * stop_arrow() is called before a change is made in insert mode.
4991 * If an arrow key has been used, start a new insertion.
4992 * Returns FAIL if undo is impossible, shouldn't insert then.
4993 */
4994 int
4995stop_arrow()
4996{
4997 if (arrow_used)
4998 {
4999 if (u_save_cursor() == OK)
5000 {
5001 arrow_used = FALSE;
5002 ins_need_undo = FALSE;
5003 }
5004 Insstart = curwin->w_cursor; /* new insertion starts here */
5005 Insstart_textlen = linetabsize(ml_get_curline());
5006 ai_col = 0;
5007#ifdef FEAT_VREPLACE
5008 if (State & VREPLACE_FLAG)
5009 {
5010 orig_line_count = curbuf->b_ml.ml_line_count;
5011 vr_lines_changed = 1;
5012 }
5013#endif
5014 ResetRedobuff();
5015 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
5016 }
5017 else if (ins_need_undo)
5018 {
5019 if (u_save_cursor() == OK)
5020 ins_need_undo = FALSE;
5021 }
5022
5023#ifdef FEAT_FOLDING
5024 /* Always open fold at the cursor line when inserting something. */
5025 foldOpenCursor();
5026#endif
5027
5028 return (arrow_used || ins_need_undo ? FAIL : OK);
5029}
5030
5031/*
5032 * do a few things to stop inserting
5033 */
5034 static void
5035stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005036 pos_T *end_insert_pos; /* where insert ended */
5037 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005039 int cc;
5040 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
5042 stop_redo_ins();
5043 replace_flush(); /* abandon replace stack */
5044
5045 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005046 * Save the inserted text for later redo with ^@ and CTRL-A.
5047 * Don't do it when "restart_edit" was set and nothing was inserted,
5048 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005050 ptr = get_inserted();
5051 if (did_restart_edit == 0 || (ptr != NULL && STRLEN(ptr) > new_insert_skip))
5052 {
5053 vim_free(last_insert);
5054 last_insert = ptr;
5055 last_insert_skip = new_insert_skip;
5056 }
5057 else
5058 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
5060 if (!arrow_used)
5061 {
5062 /* Auto-format now. It may seem strange to do this when stopping an
5063 * insertion (or moving the cursor), but it's required when appending
5064 * a line and having it end in a space. But only do it when something
5065 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005066 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005068 pos_T tpos = curwin->w_cursor;
5069
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 /* When the cursor is at the end of the line after a space the
5071 * formatting will move it to the following word. Avoid that by
5072 * moving the cursor onto the space. */
5073 cc = 'x';
5074 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5075 {
5076 dec_cursor();
5077 cc = gchar_cursor();
5078 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005079 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
5081
5082 auto_format(TRUE, FALSE);
5083
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005084 if (vim_iswhite(cc))
5085 {
5086 if (gchar_cursor() != NUL)
5087 inc_cursor();
5088#ifdef FEAT_VIRTUALEDIT
5089 /* If the cursor is still at the same character, also keep
5090 * the "coladd". */
5091 if (gchar_cursor() == NUL
5092 && curwin->w_cursor.lnum == tpos.lnum
5093 && curwin->w_cursor.col == tpos.col)
5094 curwin->w_cursor.coladd = tpos.coladd;
5095#endif
5096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098
5099 /* If a space was inserted for auto-formatting, remove it now. */
5100 check_auto_format(TRUE);
5101
5102 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005103 * of the line, and put the cursor back.
5104 * Do this when ESC was used or moving the cursor up/down. */
5105 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5106 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005108 pos_T tpos = curwin->w_cursor;
5109
5110 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5112 --curwin->w_cursor.col;
5113 while (cc = gchar_cursor(), vim_iswhite(cc))
5114 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005115 if (curwin->w_cursor.lnum != tpos.lnum)
5116 curwin->w_cursor = tpos;
5117 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5119
5120#ifdef FEAT_VISUAL
5121 /* <C-S-Right> may have started Visual mode, adjust the position for
5122 * deleted characters. */
5123 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5124 {
5125 cc = STRLEN(ml_get_curline());
5126 if (VIsual.col > (colnr_T)cc)
5127 {
5128 VIsual.col = cc;
5129# ifdef FEAT_VIRTUALEDIT
5130 VIsual.coladd = 0;
5131# endif
5132 }
5133 }
5134#endif
5135 }
5136 }
5137 did_ai = FALSE;
5138#ifdef FEAT_SMARTINDENT
5139 did_si = FALSE;
5140 can_si = FALSE;
5141 can_si_back = FALSE;
5142#endif
5143
5144 /* set '[ and '] to the inserted text */
5145 curbuf->b_op_start = Insstart;
5146 curbuf->b_op_end = *end_insert_pos;
5147}
5148
5149/*
5150 * Set the last inserted text to a single character.
5151 * Used for the replace command.
5152 */
5153 void
5154set_last_insert(c)
5155 int c;
5156{
5157 char_u *s;
5158
5159 vim_free(last_insert);
5160#ifdef FEAT_MBYTE
5161 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5162#else
5163 last_insert = alloc(6);
5164#endif
5165 if (last_insert != NULL)
5166 {
5167 s = last_insert;
5168 /* Use the CTRL-V only when entering a special char */
5169 if (c < ' ' || c == DEL)
5170 *s++ = Ctrl_V;
5171 s = add_char2buf(c, s);
5172 *s++ = ESC;
5173 *s++ = NUL;
5174 last_insert_skip = 0;
5175 }
5176}
5177
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005178#if defined(EXITFREE) || defined(PROTO)
5179 void
5180free_last_insert()
5181{
5182 vim_free(last_insert);
5183 last_insert = NULL;
5184}
5185#endif
5186
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187/*
5188 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5189 * and CSI. Handle multi-byte characters.
5190 * Returns a pointer to after the added bytes.
5191 */
5192 char_u *
5193add_char2buf(c, s)
5194 int c;
5195 char_u *s;
5196{
5197#ifdef FEAT_MBYTE
5198 char_u temp[MB_MAXBYTES];
5199 int i;
5200 int len;
5201
5202 len = (*mb_char2bytes)(c, temp);
5203 for (i = 0; i < len; ++i)
5204 {
5205 c = temp[i];
5206#endif
5207 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5208 if (c == K_SPECIAL)
5209 {
5210 *s++ = K_SPECIAL;
5211 *s++ = KS_SPECIAL;
5212 *s++ = KE_FILLER;
5213 }
5214#ifdef FEAT_GUI
5215 else if (c == CSI)
5216 {
5217 *s++ = CSI;
5218 *s++ = KS_EXTRA;
5219 *s++ = (int)KE_CSI;
5220 }
5221#endif
5222 else
5223 *s++ = c;
5224#ifdef FEAT_MBYTE
5225 }
5226#endif
5227 return s;
5228}
5229
5230/*
5231 * move cursor to start of line
5232 * if flags & BL_WHITE move to first non-white
5233 * if flags & BL_SOL move to first non-white if startofline is set,
5234 * otherwise keep "curswant" column
5235 * if flags & BL_FIX don't leave the cursor on a NUL.
5236 */
5237 void
5238beginline(flags)
5239 int flags;
5240{
5241 if ((flags & BL_SOL) && !p_sol)
5242 coladvance(curwin->w_curswant);
5243 else
5244 {
5245 curwin->w_cursor.col = 0;
5246#ifdef FEAT_VIRTUALEDIT
5247 curwin->w_cursor.coladd = 0;
5248#endif
5249
5250 if (flags & (BL_WHITE | BL_SOL))
5251 {
5252 char_u *ptr;
5253
5254 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5255 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5256 ++curwin->w_cursor.col;
5257 }
5258 curwin->w_set_curswant = TRUE;
5259 }
5260}
5261
5262/*
5263 * oneright oneleft cursor_down cursor_up
5264 *
5265 * Move one char {right,left,down,up}.
5266 * Doesn't move onto the NUL past the end of the line.
5267 * Return OK when successful, FAIL when we hit a line of file boundary.
5268 */
5269
5270 int
5271oneright()
5272{
5273 char_u *ptr;
5274#ifdef FEAT_MBYTE
5275 int l;
5276#endif
5277
5278#ifdef FEAT_VIRTUALEDIT
5279 if (virtual_active())
5280 {
5281 pos_T prevpos = curwin->w_cursor;
5282
5283 /* Adjust for multi-wide char (excluding TAB) */
5284 ptr = ml_get_cursor();
5285 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5286#ifdef FEAT_MBYTE
5287 (*mb_ptr2char)(ptr)
5288#else
5289 *ptr
5290#endif
5291 ))
5292 ? ptr2cells(ptr) : 1));
5293 curwin->w_set_curswant = TRUE;
5294 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5295 return (prevpos.col != curwin->w_cursor.col
5296 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5297 }
5298#endif
5299
5300 ptr = ml_get_cursor();
5301#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005302 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 {
5304 /* The character under the cursor is a multi-byte character, move
5305 * several bytes right, but don't end up on the NUL. */
5306 if (ptr[l] == NUL)
5307 return FAIL;
5308 curwin->w_cursor.col += l;
5309 }
5310 else
5311#endif
5312 {
5313 if (*ptr++ == NUL || *ptr == NUL)
5314 return FAIL;
5315 ++curwin->w_cursor.col;
5316 }
5317
5318 curwin->w_set_curswant = TRUE;
5319 return OK;
5320}
5321
5322 int
5323oneleft()
5324{
5325#ifdef FEAT_VIRTUALEDIT
5326 if (virtual_active())
5327 {
5328 int width;
5329 int v = getviscol();
5330
5331 if (v == 0)
5332 return FAIL;
5333
5334# ifdef FEAT_LINEBREAK
5335 /* We might get stuck on 'showbreak', skip over it. */
5336 width = 1;
5337 for (;;)
5338 {
5339 coladvance(v - width);
5340 /* getviscol() is slow, skip it when 'showbreak' is empty and
5341 * there are no multi-byte characters */
5342 if ((*p_sbr == NUL
5343# ifdef FEAT_MBYTE
5344 && !has_mbyte
5345# endif
5346 ) || getviscol() < v)
5347 break;
5348 ++width;
5349 }
5350# else
5351 coladvance(v - 1);
5352# endif
5353
5354 if (curwin->w_cursor.coladd == 1)
5355 {
5356 char_u *ptr;
5357
5358 /* Adjust for multi-wide char (not a TAB) */
5359 ptr = ml_get_cursor();
5360 if (*ptr != TAB && vim_isprintc(
5361# ifdef FEAT_MBYTE
5362 (*mb_ptr2char)(ptr)
5363# else
5364 *ptr
5365# endif
5366 ) && ptr2cells(ptr) > 1)
5367 curwin->w_cursor.coladd = 0;
5368 }
5369
5370 curwin->w_set_curswant = TRUE;
5371 return OK;
5372 }
5373#endif
5374
5375 if (curwin->w_cursor.col == 0)
5376 return FAIL;
5377
5378 curwin->w_set_curswant = TRUE;
5379 --curwin->w_cursor.col;
5380
5381#ifdef FEAT_MBYTE
5382 /* if the character on the left of the current cursor is a multi-byte
5383 * character, move to its first byte */
5384 if (has_mbyte)
5385 mb_adjust_cursor();
5386#endif
5387 return OK;
5388}
5389
5390 int
5391cursor_up(n, upd_topline)
5392 long n;
5393 int upd_topline; /* When TRUE: update topline */
5394{
5395 linenr_T lnum;
5396
5397 if (n > 0)
5398 {
5399 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005400 /* This fails if the cursor is already in the first line or the count
5401 * is larger than the line number and '-' is in 'cpoptions' */
5402 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 return FAIL;
5404 if (n >= lnum)
5405 lnum = 1;
5406 else
5407#ifdef FEAT_FOLDING
5408 if (hasAnyFolding(curwin))
5409 {
5410 /*
5411 * Count each sequence of folded lines as one logical line.
5412 */
5413 /* go to the the start of the current fold */
5414 (void)hasFolding(lnum, &lnum, NULL);
5415
5416 while (n--)
5417 {
5418 /* move up one line */
5419 --lnum;
5420 if (lnum <= 1)
5421 break;
5422 /* If we entered a fold, move to the beginning, unless in
5423 * Insert mode or when 'foldopen' contains "all": it will open
5424 * in a moment. */
5425 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
5426 (void)hasFolding(lnum, &lnum, NULL);
5427 }
5428 if (lnum < 1)
5429 lnum = 1;
5430 }
5431 else
5432#endif
5433 lnum -= n;
5434 curwin->w_cursor.lnum = lnum;
5435 }
5436
5437 /* try to advance to the column we want to be at */
5438 coladvance(curwin->w_curswant);
5439
5440 if (upd_topline)
5441 update_topline(); /* make sure curwin->w_topline is valid */
5442
5443 return OK;
5444}
5445
5446/*
5447 * Cursor down a number of logical lines.
5448 */
5449 int
5450cursor_down(n, upd_topline)
5451 long n;
5452 int upd_topline; /* When TRUE: update topline */
5453{
5454 linenr_T lnum;
5455
5456 if (n > 0)
5457 {
5458 lnum = curwin->w_cursor.lnum;
5459#ifdef FEAT_FOLDING
5460 /* Move to last line of fold, will fail if it's the end-of-file. */
5461 (void)hasFolding(lnum, NULL, &lnum);
5462#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00005463 /* This fails if the cursor is already in the last line or would move
5464 * beyound the last line and '-' is in 'cpoptions' */
5465 if (lnum >= curbuf->b_ml.ml_line_count
5466 || (lnum + n > curbuf->b_ml.ml_line_count
5467 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 return FAIL;
5469 if (lnum + n >= curbuf->b_ml.ml_line_count)
5470 lnum = curbuf->b_ml.ml_line_count;
5471 else
5472#ifdef FEAT_FOLDING
5473 if (hasAnyFolding(curwin))
5474 {
5475 linenr_T last;
5476
5477 /* count each sequence of folded lines as one logical line */
5478 while (n--)
5479 {
5480 if (hasFolding(lnum, NULL, &last))
5481 lnum = last + 1;
5482 else
5483 ++lnum;
5484 if (lnum >= curbuf->b_ml.ml_line_count)
5485 break;
5486 }
5487 if (lnum > curbuf->b_ml.ml_line_count)
5488 lnum = curbuf->b_ml.ml_line_count;
5489 }
5490 else
5491#endif
5492 lnum += n;
5493 curwin->w_cursor.lnum = lnum;
5494 }
5495
5496 /* try to advance to the column we want to be at */
5497 coladvance(curwin->w_curswant);
5498
5499 if (upd_topline)
5500 update_topline(); /* make sure curwin->w_topline is valid */
5501
5502 return OK;
5503}
5504
5505/*
5506 * Stuff the last inserted text in the read buffer.
5507 * Last_insert actually is a copy of the redo buffer, so we
5508 * first have to remove the command.
5509 */
5510 int
5511stuff_inserted(c, count, no_esc)
5512 int c; /* Command character to be inserted */
5513 long count; /* Repeat this many times */
5514 int no_esc; /* Don't add an ESC at the end */
5515{
5516 char_u *esc_ptr;
5517 char_u *ptr;
5518 char_u *last_ptr;
5519 char_u last = NUL;
5520
5521 ptr = get_last_insert();
5522 if (ptr == NULL)
5523 {
5524 EMSG(_(e_noinstext));
5525 return FAIL;
5526 }
5527
5528 /* may want to stuff the command character, to start Insert mode */
5529 if (c != NUL)
5530 stuffcharReadbuff(c);
5531 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
5532 *esc_ptr = NUL; /* remove the ESC */
5533
5534 /* when the last char is either "0" or "^" it will be quoted if no ESC
5535 * comes after it OR if it will inserted more than once and "ptr"
5536 * starts with ^D. -- Acevedo
5537 */
5538 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
5539 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
5540 && (no_esc || (*ptr == Ctrl_D && count > 1)))
5541 {
5542 last = *last_ptr;
5543 *last_ptr = NUL;
5544 }
5545
5546 do
5547 {
5548 stuffReadbuff(ptr);
5549 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
5550 if (last)
5551 stuffReadbuff((char_u *)(last == '0'
5552 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
5553 : IF_EB("\026^", CTRL_V_STR "^")));
5554 }
5555 while (--count > 0);
5556
5557 if (last)
5558 *last_ptr = last;
5559
5560 if (esc_ptr != NULL)
5561 *esc_ptr = ESC; /* put the ESC back */
5562
5563 /* may want to stuff a trailing ESC, to get out of Insert mode */
5564 if (!no_esc)
5565 stuffcharReadbuff(ESC);
5566
5567 return OK;
5568}
5569
5570 char_u *
5571get_last_insert()
5572{
5573 if (last_insert == NULL)
5574 return NULL;
5575 return last_insert + last_insert_skip;
5576}
5577
5578/*
5579 * Get last inserted string, and remove trailing <Esc>.
5580 * Returns pointer to allocated memory (must be freed) or NULL.
5581 */
5582 char_u *
5583get_last_insert_save()
5584{
5585 char_u *s;
5586 int len;
5587
5588 if (last_insert == NULL)
5589 return NULL;
5590 s = vim_strsave(last_insert + last_insert_skip);
5591 if (s != NULL)
5592 {
5593 len = (int)STRLEN(s);
5594 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
5595 s[len - 1] = NUL;
5596 }
5597 return s;
5598}
5599
5600/*
5601 * Check the word in front of the cursor for an abbreviation.
5602 * Called when the non-id character "c" has been entered.
5603 * When an abbreviation is recognized it is removed from the text and
5604 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
5605 */
5606 static int
5607echeck_abbr(c)
5608 int c;
5609{
5610 /* Don't check for abbreviation in paste mode, when disabled and just
5611 * after moving around with cursor keys. */
5612 if (p_paste || no_abbr || arrow_used)
5613 return FALSE;
5614
5615 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
5616 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
5617}
5618
5619/*
5620 * replace-stack functions
5621 *
5622 * When replacing characters, the replaced characters are remembered for each
5623 * new character. This is used to re-insert the old text when backspacing.
5624 *
5625 * There is a NUL headed list of characters for each character that is
5626 * currently in the file after the insertion point. When BS is used, one NUL
5627 * headed list is put back for the deleted character.
5628 *
5629 * For a newline, there are two NUL headed lists. One contains the characters
5630 * that the NL replaced. The extra one stores the characters after the cursor
5631 * that were deleted (always white space).
5632 *
5633 * Replace_offset is normally 0, in which case replace_push will add a new
5634 * character at the end of the stack. If replace_offset is not 0, that many
5635 * characters will be left on the stack above the newly inserted character.
5636 */
5637
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00005638static char_u *replace_stack = NULL;
5639static long replace_stack_nr = 0; /* next entry in replace stack */
5640static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
5642 void
5643replace_push(c)
5644 int c; /* character that is replaced (NUL is none) */
5645{
5646 char_u *p;
5647
5648 if (replace_stack_nr < replace_offset) /* nothing to do */
5649 return;
5650 if (replace_stack_len <= replace_stack_nr)
5651 {
5652 replace_stack_len += 50;
5653 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
5654 if (p == NULL) /* out of memory */
5655 {
5656 replace_stack_len -= 50;
5657 return;
5658 }
5659 if (replace_stack != NULL)
5660 {
5661 mch_memmove(p, replace_stack,
5662 (size_t)(replace_stack_nr * sizeof(char_u)));
5663 vim_free(replace_stack);
5664 }
5665 replace_stack = p;
5666 }
5667 p = replace_stack + replace_stack_nr - replace_offset;
5668 if (replace_offset)
5669 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
5670 *p = c;
5671 ++replace_stack_nr;
5672}
5673
5674/*
5675 * call replace_push(c) with replace_offset set to the first NUL.
5676 */
5677 static void
5678replace_push_off(c)
5679 int c;
5680{
5681 char_u *p;
5682
5683 p = replace_stack + replace_stack_nr;
5684 for (replace_offset = 1; replace_offset < replace_stack_nr;
5685 ++replace_offset)
5686 if (*--p == NUL)
5687 break;
5688 replace_push(c);
5689 replace_offset = 0;
5690}
5691
5692/*
5693 * Pop one item from the replace stack.
5694 * return -1 if stack empty
5695 * return replaced character or NUL otherwise
5696 */
5697 static int
5698replace_pop()
5699{
5700 if (replace_stack_nr == 0)
5701 return -1;
5702 return (int)replace_stack[--replace_stack_nr];
5703}
5704
5705/*
5706 * Join the top two items on the replace stack. This removes to "off"'th NUL
5707 * encountered.
5708 */
5709 static void
5710replace_join(off)
5711 int off; /* offset for which NUL to remove */
5712{
5713 int i;
5714
5715 for (i = replace_stack_nr; --i >= 0; )
5716 if (replace_stack[i] == NUL && off-- <= 0)
5717 {
5718 --replace_stack_nr;
5719 mch_memmove(replace_stack + i, replace_stack + i + 1,
5720 (size_t)(replace_stack_nr - i));
5721 return;
5722 }
5723}
5724
5725/*
5726 * Pop bytes from the replace stack until a NUL is found, and insert them
5727 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
5728 */
5729 static void
5730replace_pop_ins()
5731{
5732 int cc;
5733 int oldState = State;
5734
5735 State = NORMAL; /* don't want REPLACE here */
5736 while ((cc = replace_pop()) > 0)
5737 {
5738#ifdef FEAT_MBYTE
5739 mb_replace_pop_ins(cc);
5740#else
5741 ins_char(cc);
5742#endif
5743 dec_cursor();
5744 }
5745 State = oldState;
5746}
5747
5748#ifdef FEAT_MBYTE
5749/*
5750 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
5751 * indicates a multi-byte char, pop the other bytes too.
5752 */
5753 static void
5754mb_replace_pop_ins(cc)
5755 int cc;
5756{
5757 int n;
5758 char_u buf[MB_MAXBYTES];
5759 int i;
5760 int c;
5761
5762 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
5763 {
5764 buf[0] = cc;
5765 for (i = 1; i < n; ++i)
5766 buf[i] = replace_pop();
5767 ins_bytes_len(buf, n);
5768 }
5769 else
5770 ins_char(cc);
5771
5772 if (enc_utf8)
5773 /* Handle composing chars. */
5774 for (;;)
5775 {
5776 c = replace_pop();
5777 if (c == -1) /* stack empty */
5778 break;
5779 if ((n = MB_BYTE2LEN(c)) == 1)
5780 {
5781 /* Not a multi-byte char, put it back. */
5782 replace_push(c);
5783 break;
5784 }
5785 else
5786 {
5787 buf[0] = c;
5788 for (i = 1; i < n; ++i)
5789 buf[i] = replace_pop();
5790 if (utf_iscomposing(utf_ptr2char(buf)))
5791 ins_bytes_len(buf, n);
5792 else
5793 {
5794 /* Not a composing char, put it back. */
5795 for (i = n - 1; i >= 0; --i)
5796 replace_push(buf[i]);
5797 break;
5798 }
5799 }
5800 }
5801}
5802#endif
5803
5804/*
5805 * make the replace stack empty
5806 * (called when exiting replace mode)
5807 */
5808 static void
5809replace_flush()
5810{
5811 vim_free(replace_stack);
5812 replace_stack = NULL;
5813 replace_stack_len = 0;
5814 replace_stack_nr = 0;
5815}
5816
5817/*
5818 * Handle doing a BS for one character.
5819 * cc < 0: replace stack empty, just move cursor
5820 * cc == 0: character was inserted, delete it
5821 * cc > 0: character was replaced, put cc (first byte of original char) back
5822 * and check for more characters to be put back
5823 */
5824 static void
5825replace_do_bs()
5826{
5827 int cc;
5828#ifdef FEAT_VREPLACE
5829 int orig_len = 0;
5830 int ins_len;
5831 int orig_vcols = 0;
5832 colnr_T start_vcol;
5833 char_u *p;
5834 int i;
5835 int vcol;
5836#endif
5837
5838 cc = replace_pop();
5839 if (cc > 0)
5840 {
5841#ifdef FEAT_VREPLACE
5842 if (State & VREPLACE_FLAG)
5843 {
5844 /* Get the number of screen cells used by the character we are
5845 * going to delete. */
5846 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
5847 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
5848 }
5849#endif
5850#ifdef FEAT_MBYTE
5851 if (has_mbyte)
5852 {
5853 del_char(FALSE);
5854# ifdef FEAT_VREPLACE
5855 if (State & VREPLACE_FLAG)
5856 orig_len = STRLEN(ml_get_cursor());
5857# endif
5858 replace_push(cc);
5859 }
5860 else
5861#endif
5862 {
5863 pchar_cursor(cc);
5864#ifdef FEAT_VREPLACE
5865 if (State & VREPLACE_FLAG)
5866 orig_len = STRLEN(ml_get_cursor()) - 1;
5867#endif
5868 }
5869 replace_pop_ins();
5870
5871#ifdef FEAT_VREPLACE
5872 if (State & VREPLACE_FLAG)
5873 {
5874 /* Get the number of screen cells used by the inserted characters */
5875 p = ml_get_cursor();
5876 ins_len = STRLEN(p) - orig_len;
5877 vcol = start_vcol;
5878 for (i = 0; i < ins_len; ++i)
5879 {
5880 vcol += chartabsize(p + i, vcol);
5881#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005882 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883#endif
5884 }
5885 vcol -= start_vcol;
5886
5887 /* Delete spaces that were inserted after the cursor to keep the
5888 * text aligned. */
5889 curwin->w_cursor.col += ins_len;
5890 while (vcol > orig_vcols && gchar_cursor() == ' ')
5891 {
5892 del_char(FALSE);
5893 ++orig_vcols;
5894 }
5895 curwin->w_cursor.col -= ins_len;
5896 }
5897#endif
5898
5899 /* mark the buffer as changed and prepare for displaying */
5900 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
5901 }
5902 else if (cc == 0)
5903 (void)del_char(FALSE);
5904}
5905
5906#ifdef FEAT_CINDENT
5907/*
5908 * Return TRUE if C-indenting is on.
5909 */
5910 static int
5911cindent_on()
5912{
5913 return (!p_paste && (curbuf->b_p_cin
5914# ifdef FEAT_EVAL
5915 || *curbuf->b_p_inde != NUL
5916# endif
5917 ));
5918}
5919#endif
5920
5921#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
5922/*
5923 * Re-indent the current line, based on the current contents of it and the
5924 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
5925 * confused what all the part that handles Control-T is doing that I'm not.
5926 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
5927 */
5928
5929 void
5930fixthisline(get_the_indent)
5931 int (*get_the_indent) __ARGS((void));
5932{
5933 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
5934 if (linewhite(curwin->w_cursor.lnum))
5935 did_ai = TRUE; /* delete the indent if the line stays empty */
5936}
5937
5938 void
5939fix_indent()
5940{
5941 if (p_paste)
5942 return;
5943# ifdef FEAT_LISP
5944 if (curbuf->b_p_lisp && curbuf->b_p_ai)
5945 fixthisline(get_lisp_indent);
5946# endif
5947# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
5948 else
5949# endif
5950# ifdef FEAT_CINDENT
5951 if (cindent_on())
5952 do_c_expr_indent();
5953# endif
5954}
5955
5956#endif
5957
5958#ifdef FEAT_CINDENT
5959/*
5960 * return TRUE if 'cinkeys' contains the key "keytyped",
5961 * when == '*': Only if key is preceded with '*' (indent before insert)
5962 * when == '!': Only if key is prededed with '!' (don't insert)
5963 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
5964 *
5965 * "keytyped" can have a few special values:
5966 * KEY_OPEN_FORW
5967 * KEY_OPEN_BACK
5968 * KEY_COMPLETE just finished completion.
5969 *
5970 * If line_is_empty is TRUE accept keys with '0' before them.
5971 */
5972 int
5973in_cinkeys(keytyped, when, line_is_empty)
5974 int keytyped;
5975 int when;
5976 int line_is_empty;
5977{
5978 char_u *look;
5979 int try_match;
5980 int try_match_word;
5981 char_u *p;
5982 char_u *line;
5983 int icase;
5984 int i;
5985
5986#ifdef FEAT_EVAL
5987 if (*curbuf->b_p_inde != NUL)
5988 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
5989 else
5990#endif
5991 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
5992 while (*look)
5993 {
5994 /*
5995 * Find out if we want to try a match with this key, depending on
5996 * 'when' and a '*' or '!' before the key.
5997 */
5998 switch (when)
5999 {
6000 case '*': try_match = (*look == '*'); break;
6001 case '!': try_match = (*look == '!'); break;
6002 default: try_match = (*look != '*'); break;
6003 }
6004 if (*look == '*' || *look == '!')
6005 ++look;
6006
6007 /*
6008 * If there is a '0', only accept a match if the line is empty.
6009 * But may still match when typing last char of a word.
6010 */
6011 if (*look == '0')
6012 {
6013 try_match_word = try_match;
6014 if (!line_is_empty)
6015 try_match = FALSE;
6016 ++look;
6017 }
6018 else
6019 try_match_word = FALSE;
6020
6021 /*
6022 * does it look like a control character?
6023 */
6024 if (*look == '^'
6025#ifdef EBCDIC
6026 && (Ctrl_chr(look[1]) != 0)
6027#else
6028 && look[1] >= '?' && look[1] <= '_'
6029#endif
6030 )
6031 {
6032 if (try_match && keytyped == Ctrl_chr(look[1]))
6033 return TRUE;
6034 look += 2;
6035 }
6036 /*
6037 * 'o' means "o" command, open forward.
6038 * 'O' means "O" command, open backward.
6039 */
6040 else if (*look == 'o')
6041 {
6042 if (try_match && keytyped == KEY_OPEN_FORW)
6043 return TRUE;
6044 ++look;
6045 }
6046 else if (*look == 'O')
6047 {
6048 if (try_match && keytyped == KEY_OPEN_BACK)
6049 return TRUE;
6050 ++look;
6051 }
6052
6053 /*
6054 * 'e' means to check for "else" at start of line and just before the
6055 * cursor.
6056 */
6057 else if (*look == 'e')
6058 {
6059 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6060 {
6061 p = ml_get_curline();
6062 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6063 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6064 return TRUE;
6065 }
6066 ++look;
6067 }
6068
6069 /*
6070 * ':' only causes an indent if it is at the end of a label or case
6071 * statement, or when it was before typing the ':' (to fix
6072 * class::method for C++).
6073 */
6074 else if (*look == ':')
6075 {
6076 if (try_match && keytyped == ':')
6077 {
6078 p = ml_get_curline();
6079 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6080 return TRUE;
6081 if (curwin->w_cursor.col > 2
6082 && p[curwin->w_cursor.col - 1] == ':'
6083 && p[curwin->w_cursor.col - 2] == ':')
6084 {
6085 p[curwin->w_cursor.col - 1] = ' ';
6086 i = (cin_iscase(p) || cin_isscopedecl(p)
6087 || cin_islabel(30));
6088 p = ml_get_curline();
6089 p[curwin->w_cursor.col - 1] = ':';
6090 if (i)
6091 return TRUE;
6092 }
6093 }
6094 ++look;
6095 }
6096
6097
6098 /*
6099 * Is it a key in <>, maybe?
6100 */
6101 else if (*look == '<')
6102 {
6103 if (try_match)
6104 {
6105 /*
6106 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6107 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6108 * >, *, : and ! keys if they really really want to.
6109 */
6110 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6111 && keytyped == look[1])
6112 return TRUE;
6113
6114 if (keytyped == get_special_key_code(look + 1))
6115 return TRUE;
6116 }
6117 while (*look && *look != '>')
6118 look++;
6119 while (*look == '>')
6120 look++;
6121 }
6122
6123 /*
6124 * Is it a word: "=word"?
6125 */
6126 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6127 {
6128 ++look;
6129 if (*look == '~')
6130 {
6131 icase = TRUE;
6132 ++look;
6133 }
6134 else
6135 icase = FALSE;
6136 p = vim_strchr(look, ',');
6137 if (p == NULL)
6138 p = look + STRLEN(look);
6139 if ((try_match || try_match_word)
6140 && curwin->w_cursor.col >= (colnr_T)(p - look))
6141 {
6142 int match = FALSE;
6143
6144#ifdef FEAT_INS_EXPAND
6145 if (keytyped == KEY_COMPLETE)
6146 {
6147 char_u *s;
6148
6149 /* Just completed a word, check if it starts with "look".
6150 * search back for the start of a word. */
6151 line = ml_get_curline();
6152# ifdef FEAT_MBYTE
6153 if (has_mbyte)
6154 {
6155 char_u *n;
6156
6157 for (s = line + curwin->w_cursor.col; s > line; s = n)
6158 {
6159 n = mb_prevptr(line, s);
6160 if (!vim_iswordp(n))
6161 break;
6162 }
6163 }
6164 else
6165# endif
6166 for (s = line + curwin->w_cursor.col; s > line; --s)
6167 if (!vim_iswordc(s[-1]))
6168 break;
6169 if (s + (p - look) <= line + curwin->w_cursor.col
6170 && (icase
6171 ? MB_STRNICMP(s, look, p - look)
6172 : STRNCMP(s, look, p - look)) == 0)
6173 match = TRUE;
6174 }
6175 else
6176#endif
6177 /* TODO: multi-byte */
6178 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6179 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6180 {
6181 line = ml_get_cursor();
6182 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6183 || !vim_iswordc(line[-(p - look) - 1]))
6184 && (icase
6185 ? MB_STRNICMP(line - (p - look), look, p - look)
6186 : STRNCMP(line - (p - look), look, p - look))
6187 == 0)
6188 match = TRUE;
6189 }
6190 if (match && try_match_word && !try_match)
6191 {
6192 /* "0=word": Check if there are only blanks before the
6193 * word. */
6194 line = ml_get_curline();
6195 if ((int)(skipwhite(line) - line) !=
6196 (int)(curwin->w_cursor.col - (p - look)))
6197 match = FALSE;
6198 }
6199 if (match)
6200 return TRUE;
6201 }
6202 look = p;
6203 }
6204
6205 /*
6206 * ok, it's a boring generic character.
6207 */
6208 else
6209 {
6210 if (try_match && *look == keytyped)
6211 return TRUE;
6212 ++look;
6213 }
6214
6215 /*
6216 * Skip over ", ".
6217 */
6218 look = skip_to_option_part(look);
6219 }
6220 return FALSE;
6221}
6222#endif /* FEAT_CINDENT */
6223
6224#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6225/*
6226 * Map Hebrew keyboard when in hkmap mode.
6227 */
6228 int
6229hkmap(c)
6230 int c;
6231{
6232 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6233 {
6234 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6235 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6236 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6237 static char_u map[26] =
6238 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6239 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6240 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6241 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6242 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6243 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6244 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6245 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6246 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6247
6248 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6249 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6250 /* '-1'='sofit' */
6251 else if (c == 'x')
6252 return 'X';
6253 else if (c == 'q')
6254 return '\''; /* {geresh}={'} */
6255 else if (c == 246)
6256 return ' '; /* \"o --> ' ' for a german keyboard */
6257 else if (c == 228)
6258 return ' '; /* \"a --> ' ' -- / -- */
6259 else if (c == 252)
6260 return ' '; /* \"u --> ' ' -- / -- */
6261#ifdef EBCDIC
6262 else if (islower(c))
6263#else
6264 /* NOTE: islower() does not do the right thing for us on Linux so we
6265 * do this the same was as 5.7 and previous, so it works correctly on
6266 * all systems. Specifically, the e.g. Delete and Arrow keys are
6267 * munged and won't work if e.g. searching for Hebrew text.
6268 */
6269 else if (c >= 'a' && c <= 'z')
6270#endif
6271 return (int)(map[CharOrdLow(c)] + p_aleph);
6272 else
6273 return c;
6274 }
6275 else
6276 {
6277 switch (c)
6278 {
6279 case '`': return ';';
6280 case '/': return '.';
6281 case '\'': return ',';
6282 case 'q': return '/';
6283 case 'w': return '\'';
6284
6285 /* Hebrew letters - set offset from 'a' */
6286 case ',': c = '{'; break;
6287 case '.': c = 'v'; break;
6288 case ';': c = 't'; break;
6289 default: {
6290 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6291
6292#ifdef EBCDIC
6293 /* see note about islower() above */
6294 if (!islower(c))
6295#else
6296 if (c < 'a' || c > 'z')
6297#endif
6298 return c;
6299 c = str[CharOrdLow(c)];
6300 break;
6301 }
6302 }
6303
6304 return (int)(CharOrdLow(c) + p_aleph);
6305 }
6306}
6307#endif
6308
6309 static void
6310ins_reg()
6311{
6312 int need_redraw = FALSE;
6313 int regname;
6314 int literally = 0;
6315
6316 /*
6317 * If we are going to wait for a character, show a '"'.
6318 */
6319 pc_status = PC_STATUS_UNSET;
6320 if (redrawing() && !char_avail())
6321 {
6322 /* may need to redraw when no more chars available now */
6323 ins_redraw();
6324
6325 edit_putchar('"', TRUE);
6326#ifdef FEAT_CMDL_INFO
6327 add_to_showcmd_c(Ctrl_R);
6328#endif
6329 }
6330
6331#ifdef USE_ON_FLY_SCROLL
6332 dont_scroll = TRUE; /* disallow scrolling here */
6333#endif
6334
6335 /*
6336 * Don't map the register name. This also prevents the mode message to be
6337 * deleted when ESC is hit.
6338 */
6339 ++no_mapping;
6340 regname = safe_vgetc();
6341#ifdef FEAT_LANGMAP
6342 LANGMAP_ADJUST(regname, TRUE);
6343#endif
6344 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6345 {
6346 /* Get a third key for literal register insertion */
6347 literally = regname;
6348#ifdef FEAT_CMDL_INFO
6349 add_to_showcmd_c(literally);
6350#endif
6351 regname = safe_vgetc();
6352#ifdef FEAT_LANGMAP
6353 LANGMAP_ADJUST(regname, TRUE);
6354#endif
6355 }
6356 --no_mapping;
6357
6358#ifdef FEAT_EVAL
6359 /*
6360 * Don't call u_sync() while getting the expression,
6361 * evaluating it or giving an error message for it!
6362 */
6363 ++no_u_sync;
6364 if (regname == '=')
6365 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006366# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006368# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006370# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 /* Restore the Input Method. */
6372 if (im_on)
6373 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006376 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6377 {
6378 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 else
6382 {
6383#endif
6384 if (literally == Ctrl_O || literally == Ctrl_P)
6385 {
6386 /* Append the command to the redo buffer. */
6387 AppendCharToRedobuff(Ctrl_R);
6388 AppendCharToRedobuff(literally);
6389 AppendCharToRedobuff(regname);
6390
6391 do_put(regname, BACKWARD, 1L,
6392 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6393 }
6394 else if (insert_reg(regname, literally) == FAIL)
6395 {
6396 vim_beep();
6397 need_redraw = TRUE; /* remove the '"' */
6398 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006399 else if (stop_insert_mode)
6400 /* When the '=' register was used and a function was invoked that
6401 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
6402 * insert anything, need to remove the '"' */
6403 need_redraw = TRUE;
6404
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405#ifdef FEAT_EVAL
6406 }
6407 --no_u_sync;
6408#endif
6409#ifdef FEAT_CMDL_INFO
6410 clear_showcmd();
6411#endif
6412
6413 /* If the inserted register is empty, we need to remove the '"' */
6414 if (need_redraw || stuff_empty())
6415 edit_unputchar();
6416}
6417
6418/*
6419 * CTRL-G commands in Insert mode.
6420 */
6421 static void
6422ins_ctrl_g()
6423{
6424 int c;
6425
6426#ifdef FEAT_INS_EXPAND
6427 /* Right after CTRL-X the cursor will be after the ruler. */
6428 setcursor();
6429#endif
6430
6431 /*
6432 * Don't map the second key. This also prevents the mode message to be
6433 * deleted when ESC is hit.
6434 */
6435 ++no_mapping;
6436 c = safe_vgetc();
6437 --no_mapping;
6438 switch (c)
6439 {
6440 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
6441 case K_UP:
6442 case Ctrl_K:
6443 case 'k': ins_up(TRUE);
6444 break;
6445
6446 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
6447 case K_DOWN:
6448 case Ctrl_J:
6449 case 'j': ins_down(TRUE);
6450 break;
6451
6452 /* CTRL-G u: start new undoable edit */
6453 case 'u': u_sync();
6454 ins_need_undo = TRUE;
6455 break;
6456
6457 /* Unknown CTRL-G command, reserved for future expansion. */
6458 default: vim_beep();
6459 }
6460}
6461
6462/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006463 * CTRL-^ in Insert mode.
6464 */
6465 static void
6466ins_ctrl_hat()
6467{
6468 if (map_to_exists_mode((char_u *)"", LANGMAP))
6469 {
6470 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
6471 if (State & LANGMAP)
6472 {
6473 curbuf->b_p_iminsert = B_IMODE_NONE;
6474 State &= ~LANGMAP;
6475 }
6476 else
6477 {
6478 curbuf->b_p_iminsert = B_IMODE_LMAP;
6479 State |= LANGMAP;
6480#ifdef USE_IM_CONTROL
6481 im_set_active(FALSE);
6482#endif
6483 }
6484 }
6485#ifdef USE_IM_CONTROL
6486 else
6487 {
6488 /* There are no ":lmap" mappings, toggle IM */
6489 if (im_get_status())
6490 {
6491 curbuf->b_p_iminsert = B_IMODE_NONE;
6492 im_set_active(FALSE);
6493 }
6494 else
6495 {
6496 curbuf->b_p_iminsert = B_IMODE_IM;
6497 State &= ~LANGMAP;
6498 im_set_active(TRUE);
6499 }
6500 }
6501#endif
6502 set_iminsert_global();
6503 showmode();
6504#ifdef FEAT_GUI
6505 /* may show different cursor shape or color */
6506 if (gui.in_use)
6507 gui_update_cursor(TRUE, FALSE);
6508#endif
6509#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6510 /* Show/unshow value of 'keymap' in status lines. */
6511 status_redraw_curbuf();
6512#endif
6513}
6514
6515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 * Handle ESC in insert mode.
6517 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
6518 * insert.
6519 */
6520 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00006521ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 long *count;
6523 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00006524 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
6526 int temp;
6527 static int disabled_redraw = FALSE;
6528
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006529#ifdef FEAT_SYN_HL
6530 check_spell_redraw();
6531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532#if defined(FEAT_HANGULIN)
6533# if defined(ESC_CHG_TO_ENG_MODE)
6534 hangul_input_state_set(0);
6535# endif
6536 if (composing_hangul)
6537 {
6538 push_raw_key(composing_hangul_buffer, 2);
6539 composing_hangul = 0;
6540 }
6541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542
6543 temp = curwin->w_cursor.col;
6544 if (disabled_redraw)
6545 {
6546 --RedrawingDisabled;
6547 disabled_redraw = FALSE;
6548 }
6549 if (!arrow_used)
6550 {
6551 /*
6552 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00006553 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
6554 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 */
6556 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00006557 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558
6559 /*
6560 * Repeating insert may take a long time. Check for
6561 * interrupt now and then.
6562 */
6563 if (*count > 0)
6564 {
6565 line_breakcheck();
6566 if (got_int)
6567 *count = 0;
6568 }
6569
6570 if (--*count > 0) /* repeat what was typed */
6571 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006572 /* Vi repeats the insert without replacing characters. */
6573 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
6574 State &= ~REPLACE_FLAG;
6575
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 (void)start_redo_ins();
6577 if (cmdchar == 'r' || cmdchar == 'v')
6578 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
6579 ++RedrawingDisabled;
6580 disabled_redraw = TRUE;
6581 return FALSE; /* repeat the insert */
6582 }
6583 stop_insert(&curwin->w_cursor, TRUE);
6584 undisplay_dollar();
6585 }
6586
6587 /* When an autoindent was removed, curswant stays after the
6588 * indent */
6589 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
6590 curwin->w_set_curswant = TRUE;
6591
6592 /* Remember the last Insert position in the '^ mark. */
6593 if (!cmdmod.keepjumps)
6594 curbuf->b_last_insert = curwin->w_cursor;
6595
6596 /*
6597 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00006598 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00006600 if (!nomove
6601 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602#ifdef FEAT_VIRTUALEDIT
6603 || curwin->w_cursor.coladd > 0
6604#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006605 )
6606 && (restart_edit == NUL
6607 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00006609 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006611 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612#ifdef FEAT_RIGHTLEFT
6613 && !revins_on
6614#endif
6615 )
6616 {
6617#ifdef FEAT_VIRTUALEDIT
6618 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
6619 {
6620 oneleft();
6621 if (restart_edit != NUL)
6622 ++curwin->w_cursor.coladd;
6623 }
6624 else
6625#endif
6626 {
6627 --curwin->w_cursor.col;
6628#ifdef FEAT_MBYTE
6629 /* Correct cursor for multi-byte character. */
6630 if (has_mbyte)
6631 mb_adjust_cursor();
6632#endif
6633 }
6634 }
6635
6636#ifdef USE_IM_CONTROL
6637 /* Disable IM to allow typing English directly for Normal mode commands.
6638 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
6639 * well). */
6640 if (!(State & LANGMAP))
6641 im_save_status(&curbuf->b_p_iminsert);
6642 im_set_active(FALSE);
6643#endif
6644
6645 State = NORMAL;
6646 /* need to position cursor again (e.g. when on a TAB ) */
6647 changed_cline_bef_curs();
6648
6649#ifdef FEAT_MOUSE
6650 setmouse();
6651#endif
6652#ifdef CURSOR_SHAPE
6653 ui_cursor_shape(); /* may show different cursor shape */
6654#endif
6655
6656 /*
6657 * When recording or for CTRL-O, need to display the new mode.
6658 * Otherwise remove the mode message.
6659 */
6660 if (Recording || restart_edit != NUL)
6661 showmode();
6662 else if (p_smd)
6663 MSG("");
6664
6665 return TRUE; /* exit Insert mode */
6666}
6667
6668#ifdef FEAT_RIGHTLEFT
6669/*
6670 * Toggle language: hkmap and revins_on.
6671 * Move to end of reverse inserted text.
6672 */
6673 static void
6674ins_ctrl_()
6675{
6676 if (revins_on && revins_chars && revins_scol >= 0)
6677 {
6678 while (gchar_cursor() != NUL && revins_chars--)
6679 ++curwin->w_cursor.col;
6680 }
6681 p_ri = !p_ri;
6682 revins_on = (State == INSERT && p_ri);
6683 if (revins_on)
6684 {
6685 revins_scol = curwin->w_cursor.col;
6686 revins_legal++;
6687 revins_chars = 0;
6688 undisplay_dollar();
6689 }
6690 else
6691 revins_scol = -1;
6692#ifdef FEAT_FKMAP
6693 if (p_altkeymap)
6694 {
6695 /*
6696 * to be consistent also for redo command, using '.'
6697 * set arrow_used to true and stop it - causing to redo
6698 * characters entered in one mode (normal/reverse insert).
6699 */
6700 arrow_used = TRUE;
6701 (void)stop_arrow();
6702 p_fkmap = curwin->w_p_rl ^ p_ri;
6703 if (p_fkmap && p_ri)
6704 State = INSERT;
6705 }
6706 else
6707#endif
6708 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
6709 showmode();
6710}
6711#endif
6712
6713#ifdef FEAT_VISUAL
6714/*
6715 * If 'keymodel' contains "startsel", may start selection.
6716 * Returns TRUE when a CTRL-O and other keys stuffed.
6717 */
6718 static int
6719ins_start_select(c)
6720 int c;
6721{
6722 if (km_startsel)
6723 switch (c)
6724 {
6725 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 case K_PAGEUP:
6728 case K_KPAGEUP:
6729 case K_PAGEDOWN:
6730 case K_KPAGEDOWN:
6731# ifdef MACOS
6732 case K_LEFT:
6733 case K_RIGHT:
6734 case K_UP:
6735 case K_DOWN:
6736 case K_END:
6737 case K_HOME:
6738# endif
6739 if (!(mod_mask & MOD_MASK_SHIFT))
6740 break;
6741 /* FALLTHROUGH */
6742 case K_S_LEFT:
6743 case K_S_RIGHT:
6744 case K_S_UP:
6745 case K_S_DOWN:
6746 case K_S_END:
6747 case K_S_HOME:
6748 /* Start selection right away, the cursor can move with
6749 * CTRL-O when beyond the end of the line. */
6750 start_selection();
6751
6752 /* Execute the key in (insert) Select mode. */
6753 stuffcharReadbuff(Ctrl_O);
6754 if (mod_mask)
6755 {
6756 char_u buf[4];
6757
6758 buf[0] = K_SPECIAL;
6759 buf[1] = KS_MODIFIER;
6760 buf[2] = mod_mask;
6761 buf[3] = NUL;
6762 stuffReadbuff(buf);
6763 }
6764 stuffcharReadbuff(c);
6765 return TRUE;
6766 }
6767 return FALSE;
6768}
6769#endif
6770
6771/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006772 * <Insert> key in Insert mode: toggle insert/remplace mode.
6773 */
6774 static void
6775ins_insert(replaceState)
6776 int replaceState;
6777{
6778#ifdef FEAT_FKMAP
6779 if (p_fkmap && p_ri)
6780 {
6781 beep_flush();
6782 EMSG(farsi_text_3); /* encoded in Farsi */
6783 return;
6784 }
6785#endif
6786
6787#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00006788# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006789 set_vim_var_string(VV_INSERTMODE,
6790 (char_u *)((State & REPLACE_FLAG) ? "i" :
6791 replaceState == VREPLACE ? "v" : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00006792# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006793 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
6794#endif
6795 if (State & REPLACE_FLAG)
6796 State = INSERT | (State & LANGMAP);
6797 else
6798 State = replaceState | (State & LANGMAP);
6799 AppendCharToRedobuff(K_INS);
6800 showmode();
6801#ifdef CURSOR_SHAPE
6802 ui_cursor_shape(); /* may show different cursor shape */
6803#endif
6804}
6805
6806/*
6807 * Pressed CTRL-O in Insert mode.
6808 */
6809 static void
6810ins_ctrl_o()
6811{
6812#ifdef FEAT_VREPLACE
6813 if (State & VREPLACE_FLAG)
6814 restart_edit = 'V';
6815 else
6816#endif
6817 if (State & REPLACE_FLAG)
6818 restart_edit = 'R';
6819 else
6820 restart_edit = 'I';
6821#ifdef FEAT_VIRTUALEDIT
6822 if (virtual_active())
6823 ins_at_eol = FALSE; /* cursor always keeps its column */
6824 else
6825#endif
6826 ins_at_eol = (gchar_cursor() == NUL);
6827}
6828
6829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 * If the cursor is on an indent, ^T/^D insert/delete one
6831 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
6832 * Always round the indent to 'shiftwith', this is compatible
6833 * with vi. But vi only supports ^T and ^D after an
6834 * autoindent, we support it everywhere.
6835 */
6836 static void
6837ins_shift(c, lastc)
6838 int c;
6839 int lastc;
6840{
6841 if (stop_arrow() == FAIL)
6842 return;
6843 AppendCharToRedobuff(c);
6844
6845 /*
6846 * 0^D and ^^D: remove all indent.
6847 */
6848 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
6849 {
6850 --curwin->w_cursor.col;
6851 (void)del_char(FALSE); /* delete the '^' or '0' */
6852 /* In Replace mode, restore the characters that '^' or '0' replaced. */
6853 if (State & REPLACE_FLAG)
6854 replace_pop_ins();
6855 if (lastc == '^')
6856 old_indent = get_indent(); /* remember curr. indent */
6857 change_indent(INDENT_SET, 0, TRUE, 0);
6858 }
6859 else
6860 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
6861
6862 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
6863 did_ai = FALSE;
6864#ifdef FEAT_SMARTINDENT
6865 did_si = FALSE;
6866 can_si = FALSE;
6867 can_si_back = FALSE;
6868#endif
6869#ifdef FEAT_CINDENT
6870 can_cindent = FALSE; /* no cindenting after ^D or ^T */
6871#endif
6872}
6873
6874 static void
6875ins_del()
6876{
6877 int temp;
6878
6879 if (stop_arrow() == FAIL)
6880 return;
6881 if (gchar_cursor() == NUL) /* delete newline */
6882 {
6883 temp = curwin->w_cursor.col;
6884 if (!can_bs(BS_EOL) /* only if "eol" included */
6885 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
6886 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
6887 || do_join(FALSE) == FAIL)
6888 vim_beep();
6889 else
6890 curwin->w_cursor.col = temp;
6891 }
6892 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
6893 vim_beep();
6894 did_ai = FALSE;
6895#ifdef FEAT_SMARTINDENT
6896 did_si = FALSE;
6897 can_si = FALSE;
6898 can_si_back = FALSE;
6899#endif
6900 AppendCharToRedobuff(K_DEL);
6901}
6902
6903/*
6904 * Handle Backspace, delete-word and delete-line in Insert mode.
6905 * Return TRUE when backspace was actually used.
6906 */
6907 static int
6908ins_bs(c, mode, inserted_space_p)
6909 int c;
6910 int mode;
6911 int *inserted_space_p;
6912{
6913 linenr_T lnum;
6914 int cc;
6915 int temp = 0; /* init for GCC */
6916 colnr_T mincol;
6917 int did_backspace = FALSE;
6918 int in_indent;
6919 int oldState;
6920#ifdef FEAT_MBYTE
6921 int p1, p2;
6922#endif
6923
6924 /*
6925 * can't delete anything in an empty file
6926 * can't backup past first character in buffer
6927 * can't backup past starting point unless 'backspace' > 1
6928 * can backup to a previous line if 'backspace' == 0
6929 */
6930 if ( bufempty()
6931 || (
6932#ifdef FEAT_RIGHTLEFT
6933 !revins_on &&
6934#endif
6935 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
6936 || (!can_bs(BS_START)
6937 && (arrow_used
6938 || (curwin->w_cursor.lnum == Insstart.lnum
6939 && curwin->w_cursor.col <= Insstart.col)))
6940 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
6941 && curwin->w_cursor.col <= ai_col)
6942 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
6943 {
6944 vim_beep();
6945 return FALSE;
6946 }
6947
6948 if (stop_arrow() == FAIL)
6949 return FALSE;
6950 in_indent = inindent(0);
6951#ifdef FEAT_CINDENT
6952 if (in_indent)
6953 can_cindent = FALSE;
6954#endif
6955#ifdef FEAT_COMMENTS
6956 end_comment_pending = NUL; /* After BS, don't auto-end comment */
6957#endif
6958#ifdef FEAT_RIGHTLEFT
6959 if (revins_on) /* put cursor after last inserted char */
6960 inc_cursor();
6961#endif
6962
6963#ifdef FEAT_VIRTUALEDIT
6964 /* Virtualedit:
6965 * BACKSPACE_CHAR eats a virtual space
6966 * BACKSPACE_WORD eats all coladd
6967 * BACKSPACE_LINE eats all coladd and keeps going
6968 */
6969 if (curwin->w_cursor.coladd > 0)
6970 {
6971 if (mode == BACKSPACE_CHAR)
6972 {
6973 --curwin->w_cursor.coladd;
6974 return TRUE;
6975 }
6976 if (mode == BACKSPACE_WORD)
6977 {
6978 curwin->w_cursor.coladd = 0;
6979 return TRUE;
6980 }
6981 curwin->w_cursor.coladd = 0;
6982 }
6983#endif
6984
6985 /*
6986 * delete newline!
6987 */
6988 if (curwin->w_cursor.col == 0)
6989 {
6990 lnum = Insstart.lnum;
6991 if (curwin->w_cursor.lnum == Insstart.lnum
6992#ifdef FEAT_RIGHTLEFT
6993 || revins_on
6994#endif
6995 )
6996 {
6997 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
6998 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
6999 return FALSE;
7000 --Insstart.lnum;
7001 Insstart.col = MAXCOL;
7002 }
7003 /*
7004 * In replace mode:
7005 * cc < 0: NL was inserted, delete it
7006 * cc >= 0: NL was replaced, put original characters back
7007 */
7008 cc = -1;
7009 if (State & REPLACE_FLAG)
7010 cc = replace_pop(); /* returns -1 if NL was inserted */
7011 /*
7012 * In replace mode, in the line we started replacing, we only move the
7013 * cursor.
7014 */
7015 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7016 {
7017 dec_cursor();
7018 }
7019 else
7020 {
7021#ifdef FEAT_VREPLACE
7022 if (!(State & VREPLACE_FLAG)
7023 || curwin->w_cursor.lnum > orig_line_count)
7024#endif
7025 {
7026 temp = gchar_cursor(); /* remember current char */
7027 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007028
7029 /* When "aw" is in 'formatoptions' we must delete the space at
7030 * the end of the line, otherwise the line will be broken
7031 * again when auto-formatting. */
7032 if (has_format_option(FO_AUTO)
7033 && has_format_option(FO_WHITE_PAR))
7034 {
7035 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7036 TRUE);
7037 int len;
7038
7039 len = STRLEN(ptr);
7040 if (len > 0 && ptr[len - 1] == ' ')
7041 ptr[len - 1] = NUL;
7042 }
7043
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 (void)do_join(FALSE);
7045 if (temp == NUL && gchar_cursor() != NUL)
7046 inc_cursor();
7047 }
7048#ifdef FEAT_VREPLACE
7049 else
7050 dec_cursor();
7051#endif
7052
7053 /*
7054 * In REPLACE mode we have to put back the text that was replaced
7055 * by the NL. On the replace stack is first a NUL-terminated
7056 * sequence of characters that were deleted and then the
7057 * characters that NL replaced.
7058 */
7059 if (State & REPLACE_FLAG)
7060 {
7061 /*
7062 * Do the next ins_char() in NORMAL state, to
7063 * prevent ins_char() from replacing characters and
7064 * avoiding showmatch().
7065 */
7066 oldState = State;
7067 State = NORMAL;
7068 /*
7069 * restore characters (blanks) deleted after cursor
7070 */
7071 while (cc > 0)
7072 {
7073 temp = curwin->w_cursor.col;
7074#ifdef FEAT_MBYTE
7075 mb_replace_pop_ins(cc);
7076#else
7077 ins_char(cc);
7078#endif
7079 curwin->w_cursor.col = temp;
7080 cc = replace_pop();
7081 }
7082 /* restore the characters that NL replaced */
7083 replace_pop_ins();
7084 State = oldState;
7085 }
7086 }
7087 did_ai = FALSE;
7088 }
7089 else
7090 {
7091 /*
7092 * Delete character(s) before the cursor.
7093 */
7094#ifdef FEAT_RIGHTLEFT
7095 if (revins_on) /* put cursor on last inserted char */
7096 dec_cursor();
7097#endif
7098 mincol = 0;
7099 /* keep indent */
7100 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7101#ifdef FEAT_RIGHTLEFT
7102 && !revins_on
7103#endif
7104 )
7105 {
7106 temp = curwin->w_cursor.col;
7107 beginline(BL_WHITE);
7108 if (curwin->w_cursor.col < (colnr_T)temp)
7109 mincol = curwin->w_cursor.col;
7110 curwin->w_cursor.col = temp;
7111 }
7112
7113 /*
7114 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7115 */
7116 if ( mode == BACKSPACE_CHAR
7117 && ((p_sta && in_indent)
7118 || (curbuf->b_p_sts
7119 && (*(ml_get_cursor() - 1) == TAB
7120 || (*(ml_get_cursor() - 1) == ' '
7121 && (!*inserted_space_p
7122 || arrow_used))))))
7123 {
7124 int ts;
7125 colnr_T vcol;
7126 colnr_T want_vcol;
7127 int extra = 0;
7128
7129 *inserted_space_p = FALSE;
7130 if (p_sta)
7131 ts = curbuf->b_p_sw;
7132 else
7133 ts = curbuf->b_p_sts;
7134 /* Compute the virtual column where we want to be. Since
7135 * 'showbreak' may get in the way, need to get the last column of
7136 * the previous character. */
7137 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7138 dec_cursor();
7139 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7140 inc_cursor();
7141 want_vcol = (want_vcol / ts) * ts;
7142
7143 /* delete characters until we are at or before want_vcol */
7144 while (vcol > want_vcol
7145 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7146 {
7147 dec_cursor();
7148 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7149 if (State & REPLACE_FLAG)
7150 {
7151 /* Don't delete characters before the insert point when in
7152 * Replace mode */
7153 if (curwin->w_cursor.lnum != Insstart.lnum
7154 || curwin->w_cursor.col >= Insstart.col)
7155 {
7156#if 0 /* what was this for? It causes problems when sw != ts. */
7157 if (State == REPLACE && (int)vcol < want_vcol)
7158 {
7159 (void)del_char(FALSE);
7160 extra = 2; /* don't pop too much */
7161 }
7162 else
7163#endif
7164 replace_do_bs();
7165 }
7166 }
7167 else
7168 (void)del_char(FALSE);
7169 }
7170
7171 /* insert extra spaces until we are at want_vcol */
7172 while (vcol < want_vcol)
7173 {
7174 /* Remember the first char we inserted */
7175 if (curwin->w_cursor.lnum == Insstart.lnum
7176 && curwin->w_cursor.col < Insstart.col)
7177 Insstart.col = curwin->w_cursor.col;
7178
7179#ifdef FEAT_VREPLACE
7180 if (State & VREPLACE_FLAG)
7181 ins_char(' ');
7182 else
7183#endif
7184 {
7185 ins_str((char_u *)" ");
7186 if ((State & REPLACE_FLAG) && extra <= 1)
7187 {
7188 if (extra)
7189 replace_push_off(NUL);
7190 else
7191 replace_push(NUL);
7192 }
7193 if (extra == 2)
7194 extra = 1;
7195 }
7196 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7197 }
7198 }
7199
7200 /*
7201 * Delete upto starting point, start of line or previous word.
7202 */
7203 else do
7204 {
7205#ifdef FEAT_RIGHTLEFT
7206 if (!revins_on) /* put cursor on char to be deleted */
7207#endif
7208 dec_cursor();
7209
7210 /* start of word? */
7211 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7212 {
7213 mode = BACKSPACE_WORD_NOT_SPACE;
7214 temp = vim_iswordc(gchar_cursor());
7215 }
7216 /* end of word? */
7217 else if (mode == BACKSPACE_WORD_NOT_SPACE
7218 && (vim_isspace(cc = gchar_cursor())
7219 || vim_iswordc(cc) != temp))
7220 {
7221#ifdef FEAT_RIGHTLEFT
7222 if (!revins_on)
7223#endif
7224 inc_cursor();
7225#ifdef FEAT_RIGHTLEFT
7226 else if (State & REPLACE_FLAG)
7227 dec_cursor();
7228#endif
7229 break;
7230 }
7231 if (State & REPLACE_FLAG)
7232 replace_do_bs();
7233 else
7234 {
7235#ifdef FEAT_MBYTE
7236 if (enc_utf8 && p_deco)
7237 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7238#endif
7239 (void)del_char(FALSE);
7240#ifdef FEAT_MBYTE
7241 /*
7242 * If p1 or p2 is non-zero, there are combining characters we
7243 * need to take account of. Don't back up before the base
7244 * character.
7245 */
7246 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7247 inc_cursor();
7248#endif
7249#ifdef FEAT_RIGHTLEFT
7250 if (revins_chars)
7251 {
7252 revins_chars--;
7253 revins_legal++;
7254 }
7255 if (revins_on && gchar_cursor() == NUL)
7256 break;
7257#endif
7258 }
7259 /* Just a single backspace?: */
7260 if (mode == BACKSPACE_CHAR)
7261 break;
7262 } while (
7263#ifdef FEAT_RIGHTLEFT
7264 revins_on ||
7265#endif
7266 (curwin->w_cursor.col > mincol
7267 && (curwin->w_cursor.lnum != Insstart.lnum
7268 || curwin->w_cursor.col != Insstart.col)));
7269 did_backspace = TRUE;
7270 }
7271#ifdef FEAT_SMARTINDENT
7272 did_si = FALSE;
7273 can_si = FALSE;
7274 can_si_back = FALSE;
7275#endif
7276 if (curwin->w_cursor.col <= 1)
7277 did_ai = FALSE;
7278 /*
7279 * It's a little strange to put backspaces into the redo
7280 * buffer, but it makes auto-indent a lot easier to deal
7281 * with.
7282 */
7283 AppendCharToRedobuff(c);
7284
7285 /* If deleted before the insertion point, adjust it */
7286 if (curwin->w_cursor.lnum == Insstart.lnum
7287 && curwin->w_cursor.col < Insstart.col)
7288 Insstart.col = curwin->w_cursor.col;
7289
7290 /* vi behaviour: the cursor moves backward but the character that
7291 * was there remains visible
7292 * Vim behaviour: the cursor moves backward and the character that
7293 * was there is erased from the screen.
7294 * We can emulate the vi behaviour by pretending there is a dollar
7295 * displayed even when there isn't.
7296 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7297 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7298 dollar_vcol = curwin->w_virtcol;
7299
7300 return did_backspace;
7301}
7302
7303#ifdef FEAT_MOUSE
7304 static void
7305ins_mouse(c)
7306 int c;
7307{
7308 pos_T tpos;
7309
7310# ifdef FEAT_GUI
7311 /* When GUI is active, also move/paste when 'mouse' is empty */
7312 if (!gui.in_use)
7313# endif
7314 if (!mouse_has(MOUSE_INSERT))
7315 return;
7316
7317 undisplay_dollar();
7318 tpos = curwin->w_cursor;
7319 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7320 {
7321 start_arrow(&tpos);
7322# ifdef FEAT_CINDENT
7323 can_cindent = TRUE;
7324# endif
7325 }
7326
7327#ifdef FEAT_WINDOWS
7328 /* redraw status lines (in case another window became active) */
7329 redraw_statuslines();
7330#endif
7331}
7332
7333 static void
7334ins_mousescroll(up)
7335 int up;
7336{
7337 pos_T tpos;
7338# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7339 win_T *old_curwin;
7340# endif
7341
7342 tpos = curwin->w_cursor;
7343
7344# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7345 old_curwin = curwin;
7346
7347 /* Currently the mouse coordinates are only known in the GUI. */
7348 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7349 {
7350 int row, col;
7351
7352 row = mouse_row;
7353 col = mouse_col;
7354
7355 /* find the window at the pointer coordinates */
7356 curwin = mouse_find_win(&row, &col);
7357 curbuf = curwin->w_buffer;
7358 }
7359 if (curwin == old_curwin)
7360# endif
7361 undisplay_dollar();
7362
7363 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7364 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7365 else
7366 scroll_redraw(up, 3L);
7367
7368# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7369 curwin->w_redr_status = TRUE;
7370
7371 curwin = old_curwin;
7372 curbuf = curwin->w_buffer;
7373# endif
7374
7375 if (!equalpos(curwin->w_cursor, tpos))
7376 {
7377 start_arrow(&tpos);
7378# ifdef FEAT_CINDENT
7379 can_cindent = TRUE;
7380# endif
7381 }
7382}
7383#endif
7384
7385#ifdef FEAT_GUI
7386 void
7387ins_scroll()
7388{
7389 pos_T tpos;
7390
7391 undisplay_dollar();
7392 tpos = curwin->w_cursor;
7393 if (gui_do_scroll())
7394 {
7395 start_arrow(&tpos);
7396# ifdef FEAT_CINDENT
7397 can_cindent = TRUE;
7398# endif
7399 }
7400}
7401
7402 void
7403ins_horscroll()
7404{
7405 pos_T tpos;
7406
7407 undisplay_dollar();
7408 tpos = curwin->w_cursor;
7409 if (gui_do_horiz_scroll())
7410 {
7411 start_arrow(&tpos);
7412# ifdef FEAT_CINDENT
7413 can_cindent = TRUE;
7414# endif
7415 }
7416}
7417#endif
7418
7419 static void
7420ins_left()
7421{
7422 pos_T tpos;
7423
7424#ifdef FEAT_FOLDING
7425 if ((fdo_flags & FDO_HOR) && KeyTyped)
7426 foldOpenCursor();
7427#endif
7428 undisplay_dollar();
7429 tpos = curwin->w_cursor;
7430 if (oneleft() == OK)
7431 {
7432 start_arrow(&tpos);
7433#ifdef FEAT_RIGHTLEFT
7434 /* If exit reversed string, position is fixed */
7435 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
7436 revins_legal++;
7437 revins_chars++;
7438#endif
7439 }
7440
7441 /*
7442 * if 'whichwrap' set for cursor in insert mode may go to
7443 * previous line
7444 */
7445 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
7446 {
7447 start_arrow(&tpos);
7448 --(curwin->w_cursor.lnum);
7449 coladvance((colnr_T)MAXCOL);
7450 curwin->w_set_curswant = TRUE; /* so we stay at the end */
7451 }
7452 else
7453 vim_beep();
7454}
7455
7456 static void
7457ins_home(c)
7458 int c;
7459{
7460 pos_T tpos;
7461
7462#ifdef FEAT_FOLDING
7463 if ((fdo_flags & FDO_HOR) && KeyTyped)
7464 foldOpenCursor();
7465#endif
7466 undisplay_dollar();
7467 tpos = curwin->w_cursor;
7468 if (c == K_C_HOME)
7469 curwin->w_cursor.lnum = 1;
7470 curwin->w_cursor.col = 0;
7471#ifdef FEAT_VIRTUALEDIT
7472 curwin->w_cursor.coladd = 0;
7473#endif
7474 curwin->w_curswant = 0;
7475 start_arrow(&tpos);
7476}
7477
7478 static void
7479ins_end(c)
7480 int c;
7481{
7482 pos_T tpos;
7483
7484#ifdef FEAT_FOLDING
7485 if ((fdo_flags & FDO_HOR) && KeyTyped)
7486 foldOpenCursor();
7487#endif
7488 undisplay_dollar();
7489 tpos = curwin->w_cursor;
7490 if (c == K_C_END)
7491 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7492 coladvance((colnr_T)MAXCOL);
7493 curwin->w_curswant = MAXCOL;
7494
7495 start_arrow(&tpos);
7496}
7497
7498 static void
7499ins_s_left()
7500{
7501#ifdef FEAT_FOLDING
7502 if ((fdo_flags & FDO_HOR) && KeyTyped)
7503 foldOpenCursor();
7504#endif
7505 undisplay_dollar();
7506 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
7507 {
7508 start_arrow(&curwin->w_cursor);
7509 (void)bck_word(1L, FALSE, FALSE);
7510 curwin->w_set_curswant = TRUE;
7511 }
7512 else
7513 vim_beep();
7514}
7515
7516 static void
7517ins_right()
7518{
7519#ifdef FEAT_FOLDING
7520 if ((fdo_flags & FDO_HOR) && KeyTyped)
7521 foldOpenCursor();
7522#endif
7523 undisplay_dollar();
7524 if (gchar_cursor() != NUL || virtual_active()
7525 )
7526 {
7527 start_arrow(&curwin->w_cursor);
7528 curwin->w_set_curswant = TRUE;
7529#ifdef FEAT_VIRTUALEDIT
7530 if (virtual_active())
7531 oneright();
7532 else
7533#endif
7534 {
7535#ifdef FEAT_MBYTE
7536 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007537 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 else
7539#endif
7540 ++curwin->w_cursor.col;
7541 }
7542
7543#ifdef FEAT_RIGHTLEFT
7544 revins_legal++;
7545 if (revins_chars)
7546 revins_chars--;
7547#endif
7548 }
7549 /* if 'whichwrap' set for cursor in insert mode, may move the
7550 * cursor to the next line */
7551 else if (vim_strchr(p_ww, ']') != NULL
7552 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7553 {
7554 start_arrow(&curwin->w_cursor);
7555 curwin->w_set_curswant = TRUE;
7556 ++curwin->w_cursor.lnum;
7557 curwin->w_cursor.col = 0;
7558 }
7559 else
7560 vim_beep();
7561}
7562
7563 static void
7564ins_s_right()
7565{
7566#ifdef FEAT_FOLDING
7567 if ((fdo_flags & FDO_HOR) && KeyTyped)
7568 foldOpenCursor();
7569#endif
7570 undisplay_dollar();
7571 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
7572 || gchar_cursor() != NUL)
7573 {
7574 start_arrow(&curwin->w_cursor);
7575 (void)fwd_word(1L, FALSE, 0);
7576 curwin->w_set_curswant = TRUE;
7577 }
7578 else
7579 vim_beep();
7580}
7581
7582 static void
7583ins_up(startcol)
7584 int startcol; /* when TRUE move to Insstart.col */
7585{
7586 pos_T tpos;
7587 linenr_T old_topline = curwin->w_topline;
7588#ifdef FEAT_DIFF
7589 int old_topfill = curwin->w_topfill;
7590#endif
7591
7592 undisplay_dollar();
7593 tpos = curwin->w_cursor;
7594 if (cursor_up(1L, TRUE) == OK)
7595 {
7596 if (startcol)
7597 coladvance(getvcol_nolist(&Insstart));
7598 if (old_topline != curwin->w_topline
7599#ifdef FEAT_DIFF
7600 || old_topfill != curwin->w_topfill
7601#endif
7602 )
7603 redraw_later(VALID);
7604 start_arrow(&tpos);
7605#ifdef FEAT_CINDENT
7606 can_cindent = TRUE;
7607#endif
7608 }
7609 else
7610 vim_beep();
7611}
7612
7613 static void
7614ins_pageup()
7615{
7616 pos_T tpos;
7617
7618 undisplay_dollar();
7619 tpos = curwin->w_cursor;
7620 if (onepage(BACKWARD, 1L) == OK)
7621 {
7622 start_arrow(&tpos);
7623#ifdef FEAT_CINDENT
7624 can_cindent = TRUE;
7625#endif
7626 }
7627 else
7628 vim_beep();
7629}
7630
7631 static void
7632ins_down(startcol)
7633 int startcol; /* when TRUE move to Insstart.col */
7634{
7635 pos_T tpos;
7636 linenr_T old_topline = curwin->w_topline;
7637#ifdef FEAT_DIFF
7638 int old_topfill = curwin->w_topfill;
7639#endif
7640
7641 undisplay_dollar();
7642 tpos = curwin->w_cursor;
7643 if (cursor_down(1L, TRUE) == OK)
7644 {
7645 if (startcol)
7646 coladvance(getvcol_nolist(&Insstart));
7647 if (old_topline != curwin->w_topline
7648#ifdef FEAT_DIFF
7649 || old_topfill != curwin->w_topfill
7650#endif
7651 )
7652 redraw_later(VALID);
7653 start_arrow(&tpos);
7654#ifdef FEAT_CINDENT
7655 can_cindent = TRUE;
7656#endif
7657 }
7658 else
7659 vim_beep();
7660}
7661
7662 static void
7663ins_pagedown()
7664{
7665 pos_T tpos;
7666
7667 undisplay_dollar();
7668 tpos = curwin->w_cursor;
7669 if (onepage(FORWARD, 1L) == OK)
7670 {
7671 start_arrow(&tpos);
7672#ifdef FEAT_CINDENT
7673 can_cindent = TRUE;
7674#endif
7675 }
7676 else
7677 vim_beep();
7678}
7679
7680#ifdef FEAT_DND
7681 static void
7682ins_drop()
7683{
7684 do_put('~', BACKWARD, 1L, PUT_CURSEND);
7685}
7686#endif
7687
7688/*
7689 * Handle TAB in Insert or Replace mode.
7690 * Return TRUE when the TAB needs to be inserted like a normal character.
7691 */
7692 static int
7693ins_tab()
7694{
7695 int ind;
7696 int i;
7697 int temp;
7698
7699 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
7700 Insstart_blank_vcol = get_nolist_virtcol();
7701 if (echeck_abbr(TAB + ABBR_OFF))
7702 return FALSE;
7703
7704 ind = inindent(0);
7705#ifdef FEAT_CINDENT
7706 if (ind)
7707 can_cindent = FALSE;
7708#endif
7709
7710 /*
7711 * When nothing special, insert TAB like a normal character
7712 */
7713 if (!curbuf->b_p_et
7714 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
7715 && curbuf->b_p_sts == 0)
7716 return TRUE;
7717
7718 if (stop_arrow() == FAIL)
7719 return TRUE;
7720
7721 did_ai = FALSE;
7722#ifdef FEAT_SMARTINDENT
7723 did_si = FALSE;
7724 can_si = FALSE;
7725 can_si_back = FALSE;
7726#endif
7727 AppendToRedobuff((char_u *)"\t");
7728
7729 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
7730 temp = (int)curbuf->b_p_sw;
7731 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
7732 temp = (int)curbuf->b_p_sts;
7733 else /* otherwise use 'tabstop' */
7734 temp = (int)curbuf->b_p_ts;
7735 temp -= get_nolist_virtcol() % temp;
7736
7737 /*
7738 * Insert the first space with ins_char(). It will delete one char in
7739 * replace mode. Insert the rest with ins_str(); it will not delete any
7740 * chars. For VREPLACE mode, we use ins_char() for all characters.
7741 */
7742 ins_char(' ');
7743 while (--temp > 0)
7744 {
7745#ifdef FEAT_VREPLACE
7746 if (State & VREPLACE_FLAG)
7747 ins_char(' ');
7748 else
7749#endif
7750 {
7751 ins_str((char_u *)" ");
7752 if (State & REPLACE_FLAG) /* no char replaced */
7753 replace_push(NUL);
7754 }
7755 }
7756
7757 /*
7758 * When 'expandtab' not set: Replace spaces by TABs where possible.
7759 */
7760 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
7761 {
7762 char_u *ptr;
7763#ifdef FEAT_VREPLACE
7764 char_u *saved_line = NULL; /* init for GCC */
7765 pos_T pos;
7766#endif
7767 pos_T fpos;
7768 pos_T *cursor;
7769 colnr_T want_vcol, vcol;
7770 int change_col = -1;
7771 int save_list = curwin->w_p_list;
7772
7773 /*
7774 * Get the current line. For VREPLACE mode, don't make real changes
7775 * yet, just work on a copy of the line.
7776 */
7777#ifdef FEAT_VREPLACE
7778 if (State & VREPLACE_FLAG)
7779 {
7780 pos = curwin->w_cursor;
7781 cursor = &pos;
7782 saved_line = vim_strsave(ml_get_curline());
7783 if (saved_line == NULL)
7784 return FALSE;
7785 ptr = saved_line + pos.col;
7786 }
7787 else
7788#endif
7789 {
7790 ptr = ml_get_cursor();
7791 cursor = &curwin->w_cursor;
7792 }
7793
7794 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
7795 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
7796 curwin->w_p_list = FALSE;
7797
7798 /* Find first white before the cursor */
7799 fpos = curwin->w_cursor;
7800 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
7801 {
7802 --fpos.col;
7803 --ptr;
7804 }
7805
7806 /* In Replace mode, don't change characters before the insert point. */
7807 if ((State & REPLACE_FLAG)
7808 && fpos.lnum == Insstart.lnum
7809 && fpos.col < Insstart.col)
7810 {
7811 ptr += Insstart.col - fpos.col;
7812 fpos.col = Insstart.col;
7813 }
7814
7815 /* compute virtual column numbers of first white and cursor */
7816 getvcol(curwin, &fpos, &vcol, NULL, NULL);
7817 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
7818
7819 /* Use as many TABs as possible. Beware of 'showbreak' and
7820 * 'linebreak' adding extra virtual columns. */
7821 while (vim_iswhite(*ptr))
7822 {
7823 i = lbr_chartabsize((char_u *)"\t", vcol);
7824 if (vcol + i > want_vcol)
7825 break;
7826 if (*ptr != TAB)
7827 {
7828 *ptr = TAB;
7829 if (change_col < 0)
7830 {
7831 change_col = fpos.col; /* Column of first change */
7832 /* May have to adjust Insstart */
7833 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
7834 Insstart.col = fpos.col;
7835 }
7836 }
7837 ++fpos.col;
7838 ++ptr;
7839 vcol += i;
7840 }
7841
7842 if (change_col >= 0)
7843 {
7844 int repl_off = 0;
7845
7846 /* Skip over the spaces we need. */
7847 while (vcol < want_vcol && *ptr == ' ')
7848 {
7849 vcol += lbr_chartabsize(ptr, vcol);
7850 ++ptr;
7851 ++repl_off;
7852 }
7853 if (vcol > want_vcol)
7854 {
7855 /* Must have a char with 'showbreak' just before it. */
7856 --ptr;
7857 --repl_off;
7858 }
7859 fpos.col += repl_off;
7860
7861 /* Delete following spaces. */
7862 i = cursor->col - fpos.col;
7863 if (i > 0)
7864 {
7865 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
7866 /* correct replace stack. */
7867 if ((State & REPLACE_FLAG)
7868#ifdef FEAT_VREPLACE
7869 && !(State & VREPLACE_FLAG)
7870#endif
7871 )
7872 for (temp = i; --temp >= 0; )
7873 replace_join(repl_off);
7874 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00007875#ifdef FEAT_NETBEANS_INTG
7876 if (usingNetbeans)
7877 {
7878 netbeans_removed(curbuf, fpos.lnum, cursor->col,
7879 (long)(i + 1));
7880 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
7881 (char_u *)"\t", 1);
7882 }
7883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 cursor->col -= i;
7885
7886#ifdef FEAT_VREPLACE
7887 /*
7888 * In VREPLACE mode, we haven't changed anything yet. Do it now by
7889 * backspacing over the changed spacing and then inserting the new
7890 * spacing.
7891 */
7892 if (State & VREPLACE_FLAG)
7893 {
7894 /* Backspace from real cursor to change_col */
7895 backspace_until_column(change_col);
7896
7897 /* Insert each char in saved_line from changed_col to
7898 * ptr-cursor */
7899 ins_bytes_len(saved_line + change_col,
7900 cursor->col - change_col);
7901 }
7902#endif
7903 }
7904
7905#ifdef FEAT_VREPLACE
7906 if (State & VREPLACE_FLAG)
7907 vim_free(saved_line);
7908#endif
7909 curwin->w_p_list = save_list;
7910 }
7911
7912 return FALSE;
7913}
7914
7915/*
7916 * Handle CR or NL in insert mode.
7917 * Return TRUE when out of memory or can't undo.
7918 */
7919 static int
7920ins_eol(c)
7921 int c;
7922{
7923 int i;
7924
7925 if (echeck_abbr(c + ABBR_OFF))
7926 return FALSE;
7927 if (stop_arrow() == FAIL)
7928 return TRUE;
7929 undisplay_dollar();
7930
7931 /*
7932 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
7933 * character under the cursor. Only push a NUL on the replace stack,
7934 * nothing to put back when the NL is deleted.
7935 */
7936 if ((State & REPLACE_FLAG)
7937#ifdef FEAT_VREPLACE
7938 && !(State & VREPLACE_FLAG)
7939#endif
7940 )
7941 replace_push(NUL);
7942
7943 /*
7944 * In VREPLACE mode, a NL replaces the rest of the line, and starts
7945 * replacing the next line, so we push all of the characters left on the
7946 * line onto the replace stack. This is not done here though, it is done
7947 * in open_line().
7948 */
7949
7950#ifdef FEAT_RIGHTLEFT
7951# ifdef FEAT_FKMAP
7952 if (p_altkeymap && p_fkmap)
7953 fkmap(NL);
7954# endif
7955 /* NL in reverse insert will always start in the end of
7956 * current line. */
7957 if (revins_on)
7958 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7959#endif
7960
7961 AppendToRedobuff(NL_STR);
7962 i = open_line(FORWARD,
7963#ifdef FEAT_COMMENTS
7964 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
7965#endif
7966 0, old_indent);
7967 old_indent = 0;
7968#ifdef FEAT_CINDENT
7969 can_cindent = TRUE;
7970#endif
7971
7972 return (!i);
7973}
7974
7975#ifdef FEAT_DIGRAPHS
7976/*
7977 * Handle digraph in insert mode.
7978 * Returns character still to be inserted, or NUL when nothing remaining to be
7979 * done.
7980 */
7981 static int
7982ins_digraph()
7983{
7984 int c;
7985 int cc;
7986
7987 pc_status = PC_STATUS_UNSET;
7988 if (redrawing() && !char_avail())
7989 {
7990 /* may need to redraw when no more chars available now */
7991 ins_redraw();
7992
7993 edit_putchar('?', TRUE);
7994#ifdef FEAT_CMDL_INFO
7995 add_to_showcmd_c(Ctrl_K);
7996#endif
7997 }
7998
7999#ifdef USE_ON_FLY_SCROLL
8000 dont_scroll = TRUE; /* disallow scrolling here */
8001#endif
8002
8003 /* don't map the digraph chars. This also prevents the
8004 * mode message to be deleted when ESC is hit */
8005 ++no_mapping;
8006 ++allow_keys;
8007 c = safe_vgetc();
8008 --no_mapping;
8009 --allow_keys;
8010 if (IS_SPECIAL(c) || mod_mask) /* special key */
8011 {
8012#ifdef FEAT_CMDL_INFO
8013 clear_showcmd();
8014#endif
8015 insert_special(c, TRUE, FALSE);
8016 return NUL;
8017 }
8018 if (c != ESC)
8019 {
8020 if (redrawing() && !char_avail())
8021 {
8022 /* may need to redraw when no more chars available now */
8023 ins_redraw();
8024
8025 if (char2cells(c) == 1)
8026 {
8027 /* first remove the '?', otherwise it's restored when typing
8028 * an ESC next */
8029 edit_unputchar();
8030 ins_redraw();
8031 edit_putchar(c, TRUE);
8032 }
8033#ifdef FEAT_CMDL_INFO
8034 add_to_showcmd_c(c);
8035#endif
8036 }
8037 ++no_mapping;
8038 ++allow_keys;
8039 cc = safe_vgetc();
8040 --no_mapping;
8041 --allow_keys;
8042 if (cc != ESC)
8043 {
8044 AppendToRedobuff((char_u *)CTRL_V_STR);
8045 c = getdigraph(c, cc, TRUE);
8046#ifdef FEAT_CMDL_INFO
8047 clear_showcmd();
8048#endif
8049 return c;
8050 }
8051 }
8052 edit_unputchar();
8053#ifdef FEAT_CMDL_INFO
8054 clear_showcmd();
8055#endif
8056 return NUL;
8057}
8058#endif
8059
8060/*
8061 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8062 * Returns the char to be inserted, or NUL if none found.
8063 */
8064 static int
8065ins_copychar(lnum)
8066 linenr_T lnum;
8067{
8068 int c;
8069 int temp;
8070 char_u *ptr, *prev_ptr;
8071
8072 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8073 {
8074 vim_beep();
8075 return NUL;
8076 }
8077
8078 /* try to advance to the cursor column */
8079 temp = 0;
8080 ptr = ml_get(lnum);
8081 prev_ptr = ptr;
8082 validate_virtcol();
8083 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8084 {
8085 prev_ptr = ptr;
8086 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8087 }
8088 if ((colnr_T)temp > curwin->w_virtcol)
8089 ptr = prev_ptr;
8090
8091#ifdef FEAT_MBYTE
8092 c = (*mb_ptr2char)(ptr);
8093#else
8094 c = *ptr;
8095#endif
8096 if (c == NUL)
8097 vim_beep();
8098 return c;
8099}
8100
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008101/*
8102 * CTRL-Y or CTRL-E typed in Insert mode.
8103 */
8104 static int
8105ins_ctrl_ey(tc)
8106 int tc;
8107{
8108 int c = tc;
8109
8110#ifdef FEAT_INS_EXPAND
8111 if (ctrl_x_mode == CTRL_X_SCROLL)
8112 {
8113 if (c == Ctrl_Y)
8114 scrolldown_clamp();
8115 else
8116 scrollup_clamp();
8117 redraw_later(VALID);
8118 }
8119 else
8120#endif
8121 {
8122 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8123 if (c != NUL)
8124 {
8125 long tw_save;
8126
8127 /* The character must be taken literally, insert like it
8128 * was typed after a CTRL-V, and pretend 'textwidth'
8129 * wasn't set. Digits, 'o' and 'x' are special after a
8130 * CTRL-V, don't use it for these. */
8131 if (c < 256 && !isalnum(c))
8132 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8133 tw_save = curbuf->b_p_tw;
8134 curbuf->b_p_tw = -1;
8135 insert_special(c, TRUE, FALSE);
8136 curbuf->b_p_tw = tw_save;
8137#ifdef FEAT_RIGHTLEFT
8138 revins_chars++;
8139 revins_legal++;
8140#endif
8141 c = Ctrl_V; /* pretend CTRL-V is last character */
8142 auto_format(FALSE, TRUE);
8143 }
8144 }
8145 return c;
8146}
8147
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148#ifdef FEAT_SMARTINDENT
8149/*
8150 * Try to do some very smart auto-indenting.
8151 * Used when inserting a "normal" character.
8152 */
8153 static void
8154ins_try_si(c)
8155 int c;
8156{
8157 pos_T *pos, old_pos;
8158 char_u *ptr;
8159 int i;
8160 int temp;
8161
8162 /*
8163 * do some very smart indenting when entering '{' or '}'
8164 */
8165 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8166 {
8167 /*
8168 * for '}' set indent equal to indent of line containing matching '{'
8169 */
8170 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8171 {
8172 old_pos = curwin->w_cursor;
8173 /*
8174 * If the matching '{' has a ')' immediately before it (ignoring
8175 * white-space), then line up with the start of the line
8176 * containing the matching '(' if there is one. This handles the
8177 * case where an "if (..\n..) {" statement continues over multiple
8178 * lines -- webb
8179 */
8180 ptr = ml_get(pos->lnum);
8181 i = pos->col;
8182 if (i > 0) /* skip blanks before '{' */
8183 while (--i > 0 && vim_iswhite(ptr[i]))
8184 ;
8185 curwin->w_cursor.lnum = pos->lnum;
8186 curwin->w_cursor.col = i;
8187 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8188 curwin->w_cursor = *pos;
8189 i = get_indent();
8190 curwin->w_cursor = old_pos;
8191#ifdef FEAT_VREPLACE
8192 if (State & VREPLACE_FLAG)
8193 change_indent(INDENT_SET, i, FALSE, NUL);
8194 else
8195#endif
8196 (void)set_indent(i, SIN_CHANGED);
8197 }
8198 else if (curwin->w_cursor.col > 0)
8199 {
8200 /*
8201 * when inserting '{' after "O" reduce indent, but not
8202 * more than indent of previous line
8203 */
8204 temp = TRUE;
8205 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8206 {
8207 old_pos = curwin->w_cursor;
8208 i = get_indent();
8209 while (curwin->w_cursor.lnum > 1)
8210 {
8211 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8212
8213 /* ignore empty lines and lines starting with '#'. */
8214 if (*ptr != '#' && *ptr != NUL)
8215 break;
8216 }
8217 if (get_indent() >= i)
8218 temp = FALSE;
8219 curwin->w_cursor = old_pos;
8220 }
8221 if (temp)
8222 shift_line(TRUE, FALSE, 1);
8223 }
8224 }
8225
8226 /*
8227 * set indent of '#' always to 0
8228 */
8229 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8230 {
8231 /* remember current indent for next line */
8232 old_indent = get_indent();
8233 (void)set_indent(0, SIN_CHANGED);
8234 }
8235
8236 /* Adjust ai_col, the char at this position can be deleted. */
8237 if (ai_col > curwin->w_cursor.col)
8238 ai_col = curwin->w_cursor.col;
8239}
8240#endif
8241
8242/*
8243 * Get the value that w_virtcol would have when 'list' is off.
8244 * Unless 'cpo' contains the 'L' flag.
8245 */
8246 static colnr_T
8247get_nolist_virtcol()
8248{
8249 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8250 return getvcol_nolist(&curwin->w_cursor);
8251 validate_virtcol();
8252 return curwin->w_virtcol;
8253}