blob: 140c17f75765ce29a50321577632a7fe2dd810fb [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 */
211
212#ifdef FEAT_CINDENT
213static int can_cindent; /* may do cindenting on this line */
214#endif
215
216static int old_indent = 0; /* for ^^D command in insert mode */
217
218#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000219static int revins_on; /* reverse insert mode on */
220static int revins_chars; /* how much to skip after edit */
221static int revins_legal; /* was the last char 'legal'? */
222static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#endif
224
225#if defined(FEAT_MBYTE) && defined(MACOS_CLASSIC)
226static short previous_script = smRoman;
227#endif
228
229static int ins_need_undo; /* call u_save() before inserting a
230 char. Set when edit() is called.
231 after that arrow_used is used. */
232
233static int did_add_space = FALSE; /* auto_format() added an extra space
234 under the cursor */
235
236/*
237 * edit(): Start inserting text.
238 *
239 * "cmdchar" can be:
240 * 'i' normal insert command
241 * 'a' normal append command
242 * 'R' replace command
243 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
244 * but still only one <CR> is inserted. The <Esc> is not used for redo.
245 * 'g' "gI" command.
246 * 'V' "gR" command for Virtual Replace mode.
247 * 'v' "gr" command for single character Virtual Replace mode.
248 *
249 * This function is not called recursively. For CTRL-O commands, it returns
250 * and lets the caller handle the Normal-mode command.
251 *
252 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
253 */
254 int
255edit(cmdchar, startln, count)
256 int cmdchar;
257 int startln; /* if set, insert at start of line */
258 long count;
259{
260 int c = 0;
261 char_u *ptr;
262 int lastc;
263 colnr_T mincol;
264 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 int i;
266 int did_backspace = TRUE; /* previous char was backspace */
267#ifdef FEAT_CINDENT
268 int line_is_white = FALSE; /* line is empty before insert */
269#endif
270 linenr_T old_topline = 0; /* topline before insertion */
271#ifdef FEAT_DIFF
272 int old_topfill = -1;
273#endif
274 int inserted_space = FALSE; /* just inserted a space */
275 int replaceState = REPLACE;
276 int did_restart_edit = restart_edit;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000277 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278
279 /* sleep before redrawing, needed for "CTRL-O :" that results in an
280 * error message */
281 check_for_delay(TRUE);
282
283#ifdef HAVE_SANDBOX
284 /* Don't allow inserting in the sandbox. */
285 if (sandbox != 0)
286 {
287 EMSG(_(e_sandbox));
288 return FALSE;
289 }
290#endif
291
292#ifdef FEAT_INS_EXPAND
293 ins_compl_clear(); /* clear stuff for CTRL-X mode */
294#endif
295
Bram Moolenaar843ee412004-06-30 16:16:41 +0000296#ifdef FEAT_AUTOCMD
297 /*
298 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
299 */
300 if (cmdchar != 'r' && cmdchar != 'v')
301 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000302# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000303 if (cmdchar == 'R')
304 ptr = (char_u *)"r";
305 else if (cmdchar == 'V')
306 ptr = (char_u *)"v";
307 else
308 ptr = (char_u *)"i";
309 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000310# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000311 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
312 }
313#endif
314
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#ifdef FEAT_MOUSE
316 /*
317 * When doing a paste with the middle mouse button, Insstart is set to
318 * where the paste started.
319 */
320 if (where_paste_started.lnum != 0)
321 Insstart = where_paste_started;
322 else
323#endif
324 {
325 Insstart = curwin->w_cursor;
326 if (startln)
327 Insstart.col = 0;
328 }
329 Insstart_textlen = linetabsize(ml_get_curline());
330 Insstart_blank_vcol = MAXCOL;
331 if (!did_ai)
332 ai_col = 0;
333
334 if (cmdchar != NUL && restart_edit == 0)
335 {
336 ResetRedobuff();
337 AppendNumberToRedobuff(count);
338#ifdef FEAT_VREPLACE
339 if (cmdchar == 'V' || cmdchar == 'v')
340 {
341 /* "gR" or "gr" command */
342 AppendCharToRedobuff('g');
343 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
344 }
345 else
346#endif
347 {
348 AppendCharToRedobuff(cmdchar);
349 if (cmdchar == 'g') /* "gI" command */
350 AppendCharToRedobuff('I');
351 else if (cmdchar == 'r') /* "r<CR>" command */
352 count = 1; /* insert only one <CR> */
353 }
354 }
355
356 if (cmdchar == 'R')
357 {
358#ifdef FEAT_FKMAP
359 if (p_fkmap && p_ri)
360 {
361 beep_flush();
362 EMSG(farsi_text_3); /* encoded in Farsi */
363 State = INSERT;
364 }
365 else
366#endif
367 State = REPLACE;
368 }
369#ifdef FEAT_VREPLACE
370 else if (cmdchar == 'V' || cmdchar == 'v')
371 {
372 State = VREPLACE;
373 replaceState = VREPLACE;
374 orig_line_count = curbuf->b_ml.ml_line_count;
375 vr_lines_changed = 1;
376 }
377#endif
378 else
379 State = INSERT;
380
381 stop_insert_mode = FALSE;
382
383 /*
384 * Need to recompute the cursor position, it might move when the cursor is
385 * on a TAB or special character.
386 */
387 curs_columns(TRUE);
388
389 /*
390 * Enable langmap or IME, indicated by 'iminsert'.
391 * Note that IME may enabled/disabled without us noticing here, thus the
392 * 'iminsert' value may not reflect what is actually used. It is updated
393 * when hitting <Esc>.
394 */
395 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
396 State |= LANGMAP;
397#ifdef USE_IM_CONTROL
398 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
399#endif
400
401#if defined(FEAT_MBYTE) && defined(MACOS_CLASSIC)
402 KeyScript(previous_script);
403#endif
404
405#ifdef FEAT_MOUSE
406 setmouse();
407#endif
408#ifdef FEAT_CMDL_INFO
409 clear_showcmd();
410#endif
411#ifdef FEAT_RIGHTLEFT
412 /* there is no reverse replace mode */
413 revins_on = (State == INSERT && p_ri);
414 if (revins_on)
415 undisplay_dollar();
416 revins_chars = 0;
417 revins_legal = 0;
418 revins_scol = -1;
419#endif
420
421 /*
422 * Handle restarting Insert mode.
423 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
424 * restart_edit non-zero, and something in the stuff buffer.
425 */
426 if (restart_edit != 0 && stuff_empty())
427 {
428#ifdef FEAT_MOUSE
429 /*
430 * After a paste we consider text typed to be part of the insert for
431 * the pasted text. You can backspace over the pasted text too.
432 */
433 if (where_paste_started.lnum)
434 arrow_used = FALSE;
435 else
436#endif
437 arrow_used = TRUE;
438 restart_edit = 0;
439
440 /*
441 * If the cursor was after the end-of-line before the CTRL-O and it is
442 * now at the end-of-line, put it after the end-of-line (this is not
443 * correct in very rare cases).
444 * Also do this if curswant is greater than the current virtual
445 * column. Eg after "^O$" or "^O80|".
446 */
447 validate_virtcol();
448 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000449 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 || curwin->w_curswant > curwin->w_virtcol)
451 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
452 {
453 if (ptr[1] == NUL)
454 ++curwin->w_cursor.col;
455#ifdef FEAT_MBYTE
456 else if (has_mbyte)
457 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000458 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 if (ptr[i] == NUL)
460 curwin->w_cursor.col += i;
461 }
462#endif
463 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000464 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 }
466 else
467 arrow_used = FALSE;
468
469 /* we are in insert mode now, don't need to start it anymore */
470 need_start_insertmode = FALSE;
471
472 /* Need to save the line for undo before inserting the first char. */
473 ins_need_undo = TRUE;
474
475#ifdef FEAT_MOUSE
476 where_paste_started.lnum = 0;
477#endif
478#ifdef FEAT_CINDENT
479 can_cindent = TRUE;
480#endif
481#ifdef FEAT_FOLDING
482 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
483 * restarting. */
484 if (!p_im && did_restart_edit == 0)
485 foldOpenCursor();
486#endif
487
488 /*
489 * If 'showmode' is set, show the current (insert/replace/..) mode.
490 * A warning message for changing a readonly file is given here, before
491 * actually changing anything. It's put after the mode, if any.
492 */
493 i = 0;
494 if (p_smd)
495 i = showmode();
496
497 if (!p_im && did_restart_edit == 0)
498 change_warning(i + 1);
499
500#ifdef CURSOR_SHAPE
501 ui_cursor_shape(); /* may show different cursor shape */
502#endif
503#ifdef FEAT_DIGRAPHS
504 do_digraph(-1); /* clear digraphs */
505#endif
506
507/*
508 * Get the current length of the redo buffer, those characters have to be
509 * skipped if we want to get to the inserted characters.
510 */
511 ptr = get_inserted();
512 if (ptr == NULL)
513 new_insert_skip = 0;
514 else
515 {
516 new_insert_skip = (int)STRLEN(ptr);
517 vim_free(ptr);
518 }
519
520 old_indent = 0;
521
522 /*
523 * Main loop in Insert mode: repeat until Insert mode is left.
524 */
525 for (;;)
526 {
527#ifdef FEAT_RIGHTLEFT
528 if (!revins_legal)
529 revins_scol = -1; /* reset on illegal motions */
530 else
531 revins_legal = 0;
532#endif
533 if (arrow_used) /* don't repeat insert when arrow key used */
534 count = 0;
535
536 if (stop_insert_mode)
537 {
538 /* ":stopinsert" used or 'insertmode' reset */
539 count = 0;
540 goto doESCkey;
541 }
542
543 /* set curwin->w_curswant for next K_DOWN or K_UP */
544 if (!arrow_used)
545 curwin->w_set_curswant = TRUE;
546
547 /* If there is no typeahead may check for timestamps (e.g., for when a
548 * menu invoked a shell command). */
549 if (stuff_empty())
550 {
551 did_check_timestamps = FALSE;
552 if (need_check_timestamps)
553 check_timestamps(FALSE);
554 }
555
556 /*
557 * When emsg() was called msg_scroll will have been set.
558 */
559 msg_scroll = FALSE;
560
561#ifdef FEAT_GUI
562 /* When 'mousefocus' is set a mouse movement may have taken us to
563 * another window. "need_mouse_correct" may then be set because of an
564 * autocommand. */
565 if (need_mouse_correct)
566 gui_mouse_correct();
567#endif
568
569#ifdef FEAT_FOLDING
570 /* Open fold at the cursor line, according to 'foldopen'. */
571 if (fdo_flags & FDO_INSERT)
572 foldOpenCursor();
573 /* Close folds where the cursor isn't, according to 'foldclose' */
574 if (!char_avail())
575 foldCheckClose();
576#endif
577
578 /*
579 * If we inserted a character at the last position of the last line in
580 * the window, scroll the window one line up. This avoids an extra
581 * redraw.
582 * This is detected when the cursor column is smaller after inserting
583 * something.
584 * Don't do this when the topline changed already, it has
585 * already been adjusted (by insertchar() calling open_line())).
586 */
587 if (curbuf->b_mod_set
588 && curwin->w_p_wrap
589 && !did_backspace
590 && curwin->w_topline == old_topline
591#ifdef FEAT_DIFF
592 && curwin->w_topfill == old_topfill
593#endif
594 )
595 {
596 mincol = curwin->w_wcol;
597 validate_cursor_col();
598
599 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
600 && curwin->w_wrow == W_WINROW(curwin)
601 + curwin->w_height - 1 - p_so
602 && (curwin->w_cursor.lnum != curwin->w_topline
603#ifdef FEAT_DIFF
604 || curwin->w_topfill > 0
605#endif
606 ))
607 {
608#ifdef FEAT_DIFF
609 if (curwin->w_topfill > 0)
610 --curwin->w_topfill;
611 else
612#endif
613#ifdef FEAT_FOLDING
614 if (hasFolding(curwin->w_topline, NULL, &old_topline))
615 set_topline(curwin, old_topline + 1);
616 else
617#endif
618 set_topline(curwin, curwin->w_topline + 1);
619 }
620 }
621
622 /* May need to adjust w_topline to show the cursor. */
623 update_topline();
624
625 did_backspace = FALSE;
626
627 validate_cursor(); /* may set must_redraw */
628
629 /*
630 * Redraw the display when no characters are waiting.
631 * Also shows mode, ruler and positions cursor.
632 */
633 ins_redraw();
634
635#ifdef FEAT_SCROLLBIND
636 if (curwin->w_p_scb)
637 do_check_scrollbind(TRUE);
638#endif
639
640 update_curswant();
641 old_topline = curwin->w_topline;
642#ifdef FEAT_DIFF
643 old_topfill = curwin->w_topfill;
644#endif
645
646#ifdef USE_ON_FLY_SCROLL
647 dont_scroll = FALSE; /* allow scrolling here */
648#endif
649
650 /*
651 * Get a character for Insert mode.
652 */
653 lastc = c; /* remember previous char for CTRL-D */
654 c = safe_vgetc();
655
656#ifdef FEAT_RIGHTLEFT
657 if (p_hkmap && KeyTyped)
658 c = hkmap(c); /* Hebrew mode mapping */
659#endif
660#ifdef FEAT_FKMAP
661 if (p_fkmap && KeyTyped)
662 c = fkmap(c); /* Farsi mode mapping */
663#endif
664
665#ifdef FEAT_INS_EXPAND
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000666 /* When the popup menu is visible cursor keys change the selection. */
667 if (c == K_UP && pum_visible())
668 c = Ctrl_P;
669 if (c == K_DOWN && pum_visible())
670 c = Ctrl_N;
671
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
673 * it does fix up the text when finishing completion. */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000674 if (c != K_IGNORE)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000675 {
676 if (ins_compl_prep(c))
677 continue;
678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#endif
680
Bram Moolenaar488c6512005-08-11 20:09:58 +0000681 /* CTRL-\ CTRL-N goes to Normal mode,
682 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
683 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 if (c == Ctrl_BSL)
685 {
686 /* may need to redraw when no more chars available now */
687 ins_redraw();
688 ++no_mapping;
689 ++allow_keys;
690 c = safe_vgetc();
691 --no_mapping;
692 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000693 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000695 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 vungetc(c);
697 c = Ctrl_BSL;
698 }
699 else if (c == Ctrl_G && p_im)
700 continue;
701 else
702 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000703 if (c == Ctrl_O)
704 {
705 ins_ctrl_o();
706 ins_at_eol = FALSE; /* cursor keeps its column */
707 nomove = TRUE;
708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 count = 0;
710 goto doESCkey;
711 }
712 }
713
714#ifdef FEAT_DIGRAPHS
715 c = do_digraph(c);
716#endif
717
718#ifdef FEAT_INS_EXPAND
719 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
720 goto docomplete;
721#endif
722 if (c == Ctrl_V || c == Ctrl_Q)
723 {
724 ins_ctrl_v();
725 c = Ctrl_V; /* pretend CTRL-V is last typed character */
726 continue;
727 }
728
729#ifdef FEAT_CINDENT
730 if (cindent_on()
731# ifdef FEAT_INS_EXPAND
732 && ctrl_x_mode == 0
733# endif
734 )
735 {
736 /* A key name preceded by a bang means this key is not to be
737 * inserted. Skip ahead to the re-indenting below.
738 * A key name preceded by a star means that indenting has to be
739 * done before inserting the key. */
740 line_is_white = inindent(0);
741 if (in_cinkeys(c, '!', line_is_white))
742 goto force_cindent;
743 if (can_cindent && in_cinkeys(c, '*', line_is_white)
744 && stop_arrow() == OK)
745 do_c_expr_indent();
746 }
747#endif
748
749#ifdef FEAT_RIGHTLEFT
750 if (curwin->w_p_rl)
751 switch (c)
752 {
753 case K_LEFT: c = K_RIGHT; break;
754 case K_S_LEFT: c = K_S_RIGHT; break;
755 case K_C_LEFT: c = K_C_RIGHT; break;
756 case K_RIGHT: c = K_LEFT; break;
757 case K_S_RIGHT: c = K_S_LEFT; break;
758 case K_C_RIGHT: c = K_C_LEFT; break;
759 }
760#endif
761
762#ifdef FEAT_VISUAL
763 /*
764 * If 'keymodel' contains "startsel", may start selection. If it
765 * does, a CTRL-O and c will be stuffed, we need to get these
766 * characters.
767 */
768 if (ins_start_select(c))
769 continue;
770#endif
771
772 /*
773 * The big switch to handle a character in insert mode.
774 */
775 switch (c)
776 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000777 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 if (echeck_abbr(ESC + ABBR_OFF))
779 break;
780 /*FALLTHROUGH*/
781
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000782 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783#ifdef FEAT_CMDWIN
784 if (c == Ctrl_C && cmdwin_type != 0)
785 {
786 /* Close the cmdline window. */
787 cmdwin_result = K_IGNORE;
788 got_int = FALSE; /* don't stop executing autocommands et al. */
789 goto doESCkey;
790 }
791#endif
792
793#ifdef UNIX
794do_intr:
795#endif
796 /* when 'insertmode' set, and not halfway a mapping, don't leave
797 * Insert mode */
798 if (goto_im())
799 {
800 if (got_int)
801 {
802 (void)vgetc(); /* flush all buffers */
803 got_int = FALSE;
804 }
805 else
806 vim_beep();
807 break;
808 }
809doESCkey:
810 /*
811 * This is the ONLY return from edit()!
812 */
813 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
814 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000815 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 o_lnum = curwin->w_cursor.lnum;
817
Bram Moolenaar488c6512005-08-11 20:09:58 +0000818 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000819 {
820#ifdef FEAT_AUTOCMD
821 if (cmdchar != 'r' && cmdchar != 'v')
822 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
823 FALSE, curbuf);
824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 continue;
828
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000829 case Ctrl_Z: /* suspend when 'insertmode' set */
830 if (!p_im)
831 goto normalchar; /* insert CTRL-Z as normal char */
832 stuffReadbuff((char_u *)":st\r");
833 c = Ctrl_O;
834 /*FALLTHROUGH*/
835
836 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000837#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000838 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000839 goto docomplete;
840#endif
841 if (echeck_abbr(Ctrl_O + ABBR_OFF))
842 break;
843 ins_ctrl_o();
844 count = 0;
845 goto doESCkey;
846
Bram Moolenaar572cb562005-08-05 21:35:02 +0000847 case K_INS: /* toggle insert/replace mode */
848 case K_KINS:
849 ins_insert(replaceState);
850 break;
851
852 case K_SELECT: /* end of Select mode mapping - ignore */
853 break;
854
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000855#ifdef FEAT_SNIFF
856 case K_SNIFF: /* Sniff command received */
857 stuffcharReadbuff(K_SNIFF);
858 goto doESCkey;
859#endif
860
861 case K_HELP: /* Help key works like <ESC> <Help> */
862 case K_F1:
863 case K_XF1:
864 stuffcharReadbuff(K_HELP);
865 if (p_im)
866 need_start_insertmode = TRUE;
867 goto doESCkey;
868
869#ifdef FEAT_NETBEANS_INTG
870 case K_F21: /* NetBeans command */
871 ++no_mapping; /* don't map the next key hits */
872 i = safe_vgetc();
873 --no_mapping;
874 netbeans_keycommand(i);
875 break;
876#endif
877
878 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 case NUL:
880 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000881 /* For ^@ the trailing ESC will end the insert, unless there is an
882 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
884 && c != Ctrl_A && !p_im)
885 goto doESCkey; /* quit insert mode */
886 inserted_space = FALSE;
887 break;
888
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000889 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 ins_reg();
891 auto_format(FALSE, TRUE);
892 inserted_space = FALSE;
893 break;
894
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000895 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 ins_ctrl_g();
897 break;
898
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000899 case Ctrl_HAT: /* switch input mode and/or langmap */
900 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 break;
902
903#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000904 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 if (!p_ari)
906 goto normalchar;
907 ins_ctrl_();
908 break;
909#endif
910
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000911 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
913 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
914 goto docomplete;
915#endif
916 /* FALLTHROUGH */
917
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000918 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919# ifdef FEAT_INS_EXPAND
920 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
921 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000922 if (has_compl_option(FALSE))
923 goto docomplete;
924 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
926# endif
927 ins_shift(c, lastc);
928 auto_format(FALSE, TRUE);
929 inserted_space = FALSE;
930 break;
931
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000932 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 case K_KDEL:
934 ins_del();
935 auto_format(FALSE, TRUE);
936 break;
937
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000938 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 case Ctrl_H:
940 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
941 auto_format(FALSE, TRUE);
942 break;
943
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000944 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
946 auto_format(FALSE, TRUE);
947 break;
948
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000949 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000950# ifdef FEAT_COMPL_FUNC
951 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000952 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000953 goto docomplete;
954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
956 auto_format(FALSE, TRUE);
957 inserted_space = FALSE;
958 break;
959
960#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000961 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 case K_LEFTMOUSE_NM:
963 case K_LEFTDRAG:
964 case K_LEFTRELEASE:
965 case K_LEFTRELEASE_NM:
966 case K_MIDDLEMOUSE:
967 case K_MIDDLEDRAG:
968 case K_MIDDLERELEASE:
969 case K_RIGHTMOUSE:
970 case K_RIGHTDRAG:
971 case K_RIGHTRELEASE:
972 case K_X1MOUSE:
973 case K_X1DRAG:
974 case K_X1RELEASE:
975 case K_X2MOUSE:
976 case K_X2DRAG:
977 case K_X2RELEASE:
978 ins_mouse(c);
979 break;
980
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000981 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 ins_mousescroll(FALSE);
983 break;
984
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000985 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 ins_mousescroll(TRUE);
987 break;
988#endif
989
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000990 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 break;
992
993#ifdef FEAT_GUI
994 case K_VER_SCROLLBAR:
995 ins_scroll();
996 break;
997
998 case K_HOR_SCROLLBAR:
999 ins_horscroll();
1000 break;
1001#endif
1002
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001003 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 case K_S_HOME:
1006 case K_C_HOME:
1007 ins_home(c);
1008 break;
1009
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001010 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 case K_S_END:
1013 case K_C_END:
1014 ins_end(c);
1015 break;
1016
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001017 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001018 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1019 ins_s_left();
1020 else
1021 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 break;
1023
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001024 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 case K_C_LEFT:
1026 ins_s_left();
1027 break;
1028
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001029 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001030 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1031 ins_s_right();
1032 else
1033 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 break;
1035
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001036 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 case K_C_RIGHT:
1038 ins_s_right();
1039 break;
1040
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001041 case K_UP: /* <Up> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001042 if (mod_mask & MOD_MASK_SHIFT)
1043 ins_pageup();
1044 else
1045 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 break;
1047
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001048 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 case K_PAGEUP:
1050 case K_KPAGEUP:
1051 ins_pageup();
1052 break;
1053
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001054 case K_DOWN: /* <Down> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001055 if (mod_mask & MOD_MASK_SHIFT)
1056 ins_pagedown();
1057 else
1058 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 break;
1060
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 case K_PAGEDOWN:
1063 case K_KPAGEDOWN:
1064 ins_pagedown();
1065 break;
1066
1067#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001068 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 ins_drop();
1070 break;
1071#endif
1072
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001073 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 c = TAB;
1075 /* FALLTHROUGH */
1076
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001077 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1079 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1080 goto docomplete;
1081#endif
1082 inserted_space = FALSE;
1083 if (ins_tab())
1084 goto normalchar; /* insert TAB as a normal char */
1085 auto_format(FALSE, TRUE);
1086 break;
1087
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001088 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 c = CAR;
1090 /* FALLTHROUGH */
1091 case CAR:
1092 case NL:
1093#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1094 /* In a quickfix window a <CR> jumps to the error under the
1095 * cursor. */
1096 if (bt_quickfix(curbuf) && c == CAR)
1097 {
1098 do_cmdline_cmd((char_u *)".cc");
1099 break;
1100 }
1101#endif
1102#ifdef FEAT_CMDWIN
1103 if (cmdwin_type != 0)
1104 {
1105 /* Execute the command in the cmdline window. */
1106 cmdwin_result = CAR;
1107 goto doESCkey;
1108 }
1109#endif
1110 if (ins_eol(c) && !p_im)
1111 goto doESCkey; /* out of memory */
1112 auto_format(FALSE, FALSE);
1113 inserted_space = FALSE;
1114 break;
1115
1116#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001117 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118# ifdef FEAT_INS_EXPAND
1119 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1120 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001121 if (has_compl_option(TRUE))
1122 goto docomplete;
1123 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 }
1125# endif
1126# ifdef FEAT_DIGRAPHS
1127 c = ins_digraph();
1128 if (c == NUL)
1129 break;
1130# endif
1131 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133
1134#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001135 case Ctrl_X: /* Enter CTRL-X mode */
1136 ins_ctrl_x();
1137 break;
1138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001139 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 if (ctrl_x_mode != CTRL_X_TAGS)
1141 goto normalchar;
1142 goto docomplete;
1143
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001144 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 if (ctrl_x_mode != CTRL_X_FILES)
1146 goto normalchar;
1147 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001148
1149 case 's': /* Spelling completion after ^X */
1150 case Ctrl_S:
1151 if (ctrl_x_mode != CTRL_X_SPELL)
1152 goto normalchar;
1153 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154#endif
1155
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001156 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157#ifdef FEAT_INS_EXPAND
1158 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1159#endif
1160 {
1161 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1162 if (p_im)
1163 {
1164 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1165 break;
1166 goto doESCkey;
1167 }
1168 goto normalchar;
1169 }
1170#ifdef FEAT_INS_EXPAND
1171 /* FALLTHROUGH */
1172
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001173 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 case Ctrl_N:
1175 /* if 'complete' is empty then plain ^P is no longer special,
1176 * but it is under other ^X modes */
1177 if (*curbuf->b_p_cpt == NUL
1178 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001179 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 goto normalchar;
1181
1182docomplete:
1183 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001184 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 break;
1186#endif /* FEAT_INS_EXPAND */
1187
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001188 case Ctrl_Y: /* copy from previous line or scroll down */
1189 case Ctrl_E: /* copy from next line or scroll up */
1190 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 break;
1192
1193 default:
1194#ifdef UNIX
1195 if (c == intr_char) /* special interrupt char */
1196 goto do_intr;
1197#endif
1198
1199 /*
1200 * Insert a nomal character.
1201 */
1202normalchar:
1203#ifdef FEAT_SMARTINDENT
1204 /* Try to perform smart-indenting. */
1205 ins_try_si(c);
1206#endif
1207
1208 if (c == ' ')
1209 {
1210 inserted_space = TRUE;
1211#ifdef FEAT_CINDENT
1212 if (inindent(0))
1213 can_cindent = FALSE;
1214#endif
1215 if (Insstart_blank_vcol == MAXCOL
1216 && curwin->w_cursor.lnum == Insstart.lnum)
1217 Insstart_blank_vcol = get_nolist_virtcol();
1218 }
1219
1220 if (vim_iswordc(c) || !echeck_abbr(
1221#ifdef FEAT_MBYTE
1222 /* Add ABBR_OFF for characters above 0x100, this is
1223 * what check_abbr() expects. */
1224 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1225#endif
1226 c))
1227 {
1228 insert_special(c, FALSE, FALSE);
1229#ifdef FEAT_RIGHTLEFT
1230 revins_legal++;
1231 revins_chars++;
1232#endif
1233 }
1234
1235 auto_format(FALSE, TRUE);
1236
1237#ifdef FEAT_FOLDING
1238 /* When inserting a character the cursor line must never be in a
1239 * closed fold. */
1240 foldOpenCursor();
1241#endif
1242 break;
1243 } /* end of switch (c) */
1244
1245 /* If the cursor was moved we didn't just insert a space */
1246 if (arrow_used)
1247 inserted_space = FALSE;
1248
1249#ifdef FEAT_CINDENT
1250 if (can_cindent && cindent_on()
1251# ifdef FEAT_INS_EXPAND
1252 && ctrl_x_mode == 0
1253# endif
1254 )
1255 {
1256force_cindent:
1257 /*
1258 * Indent now if a key was typed that is in 'cinkeys'.
1259 */
1260 if (in_cinkeys(c, ' ', line_is_white))
1261 {
1262 if (stop_arrow() == OK)
1263 /* re-indent the current line */
1264 do_c_expr_indent();
1265 }
1266 }
1267#endif /* FEAT_CINDENT */
1268
1269 } /* for (;;) */
1270 /* NOTREACHED */
1271}
1272
1273/*
1274 * Redraw for Insert mode.
1275 * This is postponed until getting the next character to make '$' in the 'cpo'
1276 * option work correctly.
1277 * Only redraw when there are no characters available. This speeds up
1278 * inserting sequences of characters (e.g., for CTRL-R).
1279 */
1280 static void
1281ins_redraw()
1282{
1283 if (!char_avail())
1284 {
1285 if (must_redraw)
1286 update_screen(0);
1287 else if (clear_cmdline || redraw_cmdline)
1288 showmode(); /* clear cmdline and show mode */
1289 showruler(FALSE);
1290 setcursor();
1291 emsg_on_display = FALSE; /* may remove error message now */
1292 }
1293}
1294
1295/*
1296 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1297 */
1298 static void
1299ins_ctrl_v()
1300{
1301 int c;
1302
1303 /* may need to redraw when no more chars available now */
1304 ins_redraw();
1305
1306 if (redrawing() && !char_avail())
1307 edit_putchar('^', TRUE);
1308 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1309
1310#ifdef FEAT_CMDL_INFO
1311 add_to_showcmd_c(Ctrl_V);
1312#endif
1313
1314 c = get_literal();
1315#ifdef FEAT_CMDL_INFO
1316 clear_showcmd();
1317#endif
1318 insert_special(c, FALSE, TRUE);
1319#ifdef FEAT_RIGHTLEFT
1320 revins_chars++;
1321 revins_legal++;
1322#endif
1323}
1324
1325/*
1326 * Put a character directly onto the screen. It's not stored in a buffer.
1327 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1328 */
1329static int pc_status;
1330#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1331#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1332#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1333#define PC_STATUS_SET 3 /* pc_bytes was filled */
1334#ifdef FEAT_MBYTE
1335static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1336#else
1337static char_u pc_bytes[2]; /* saved bytes */
1338#endif
1339static int pc_attr;
1340static int pc_row;
1341static int pc_col;
1342
1343 void
1344edit_putchar(c, highlight)
1345 int c;
1346 int highlight;
1347{
1348 int attr;
1349
1350 if (ScreenLines != NULL)
1351 {
1352 update_topline(); /* just in case w_topline isn't valid */
1353 validate_cursor();
1354 if (highlight)
1355 attr = hl_attr(HLF_8);
1356 else
1357 attr = 0;
1358 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1359 pc_col = W_WINCOL(curwin);
1360#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1361 pc_status = PC_STATUS_UNSET;
1362#endif
1363#ifdef FEAT_RIGHTLEFT
1364 if (curwin->w_p_rl)
1365 {
1366 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1367# ifdef FEAT_MBYTE
1368 if (has_mbyte)
1369 {
1370 int fix_col = mb_fix_col(pc_col, pc_row);
1371
1372 if (fix_col != pc_col)
1373 {
1374 screen_putchar(' ', pc_row, fix_col, attr);
1375 --curwin->w_wcol;
1376 pc_status = PC_STATUS_RIGHT;
1377 }
1378 }
1379# endif
1380 }
1381 else
1382#endif
1383 {
1384 pc_col += curwin->w_wcol;
1385#ifdef FEAT_MBYTE
1386 if (mb_lefthalve(pc_row, pc_col))
1387 pc_status = PC_STATUS_LEFT;
1388#endif
1389 }
1390
1391 /* save the character to be able to put it back */
1392#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1393 if (pc_status == PC_STATUS_UNSET)
1394#endif
1395 {
1396 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1397 pc_status = PC_STATUS_SET;
1398 }
1399 screen_putchar(c, pc_row, pc_col, attr);
1400 }
1401}
1402
1403/*
1404 * Undo the previous edit_putchar().
1405 */
1406 void
1407edit_unputchar()
1408{
1409 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1410 {
1411#if defined(FEAT_MBYTE)
1412 if (pc_status == PC_STATUS_RIGHT)
1413 ++curwin->w_wcol;
1414 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1415 redrawWinline(curwin->w_cursor.lnum, FALSE);
1416 else
1417#endif
1418 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1419 }
1420}
1421
1422/*
1423 * Called when p_dollar is set: display a '$' at the end of the changed text
1424 * Only works when cursor is in the line that changes.
1425 */
1426 void
1427display_dollar(col)
1428 colnr_T col;
1429{
1430 colnr_T save_col;
1431
1432 if (!redrawing())
1433 return;
1434
1435 cursor_off();
1436 save_col = curwin->w_cursor.col;
1437 curwin->w_cursor.col = col;
1438#ifdef FEAT_MBYTE
1439 if (has_mbyte)
1440 {
1441 char_u *p;
1442
1443 /* If on the last byte of a multi-byte move to the first byte. */
1444 p = ml_get_curline();
1445 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1446 }
1447#endif
1448 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1449 if (curwin->w_wcol < W_WIDTH(curwin))
1450 {
1451 edit_putchar('$', FALSE);
1452 dollar_vcol = curwin->w_virtcol;
1453 }
1454 curwin->w_cursor.col = save_col;
1455}
1456
1457/*
1458 * Call this function before moving the cursor from the normal insert position
1459 * in insert mode.
1460 */
1461 static void
1462undisplay_dollar()
1463{
1464 if (dollar_vcol)
1465 {
1466 dollar_vcol = 0;
1467 redrawWinline(curwin->w_cursor.lnum, FALSE);
1468 }
1469}
1470
1471/*
1472 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1473 * Keep the cursor on the same character.
1474 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1475 * type == INDENT_DEC decrease indent (for CTRL-D)
1476 * type == INDENT_SET set indent to "amount"
1477 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1478 */
1479 void
1480change_indent(type, amount, round, replaced)
1481 int type;
1482 int amount;
1483 int round;
1484 int replaced; /* replaced character, put on replace stack */
1485{
1486 int vcol;
1487 int last_vcol;
1488 int insstart_less; /* reduction for Insstart.col */
1489 int new_cursor_col;
1490 int i;
1491 char_u *ptr;
1492 int save_p_list;
1493 int start_col;
1494 colnr_T vc;
1495#ifdef FEAT_VREPLACE
1496 colnr_T orig_col = 0; /* init for GCC */
1497 char_u *new_line, *orig_line = NULL; /* init for GCC */
1498
1499 /* VREPLACE mode needs to know what the line was like before changing */
1500 if (State & VREPLACE_FLAG)
1501 {
1502 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1503 orig_col = curwin->w_cursor.col;
1504 }
1505#endif
1506
1507 /* for the following tricks we don't want list mode */
1508 save_p_list = curwin->w_p_list;
1509 curwin->w_p_list = FALSE;
1510 vc = getvcol_nolist(&curwin->w_cursor);
1511 vcol = vc;
1512
1513 /*
1514 * For Replace mode we need to fix the replace stack later, which is only
1515 * possible when the cursor is in the indent. Remember the number of
1516 * characters before the cursor if it's possible.
1517 */
1518 start_col = curwin->w_cursor.col;
1519
1520 /* determine offset from first non-blank */
1521 new_cursor_col = curwin->w_cursor.col;
1522 beginline(BL_WHITE);
1523 new_cursor_col -= curwin->w_cursor.col;
1524
1525 insstart_less = curwin->w_cursor.col;
1526
1527 /*
1528 * If the cursor is in the indent, compute how many screen columns the
1529 * cursor is to the left of the first non-blank.
1530 */
1531 if (new_cursor_col < 0)
1532 vcol = get_indent() - vcol;
1533
1534 if (new_cursor_col > 0) /* can't fix replace stack */
1535 start_col = -1;
1536
1537 /*
1538 * Set the new indent. The cursor will be put on the first non-blank.
1539 */
1540 if (type == INDENT_SET)
1541 (void)set_indent(amount, SIN_CHANGED);
1542 else
1543 {
1544#ifdef FEAT_VREPLACE
1545 int save_State = State;
1546
1547 /* Avoid being called recursively. */
1548 if (State & VREPLACE_FLAG)
1549 State = INSERT;
1550#endif
1551 shift_line(type == INDENT_DEC, round, 1);
1552#ifdef FEAT_VREPLACE
1553 State = save_State;
1554#endif
1555 }
1556 insstart_less -= curwin->w_cursor.col;
1557
1558 /*
1559 * Try to put cursor on same character.
1560 * If the cursor is at or after the first non-blank in the line,
1561 * compute the cursor column relative to the column of the first
1562 * non-blank character.
1563 * If we are not in insert mode, leave the cursor on the first non-blank.
1564 * If the cursor is before the first non-blank, position it relative
1565 * to the first non-blank, counted in screen columns.
1566 */
1567 if (new_cursor_col >= 0)
1568 {
1569 /*
1570 * When changing the indent while the cursor is touching it, reset
1571 * Insstart_col to 0.
1572 */
1573 if (new_cursor_col == 0)
1574 insstart_less = MAXCOL;
1575 new_cursor_col += curwin->w_cursor.col;
1576 }
1577 else if (!(State & INSERT))
1578 new_cursor_col = curwin->w_cursor.col;
1579 else
1580 {
1581 /*
1582 * Compute the screen column where the cursor should be.
1583 */
1584 vcol = get_indent() - vcol;
1585 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1586
1587 /*
1588 * Advance the cursor until we reach the right screen column.
1589 */
1590 vcol = last_vcol = 0;
1591 new_cursor_col = -1;
1592 ptr = ml_get_curline();
1593 while (vcol <= (int)curwin->w_virtcol)
1594 {
1595 last_vcol = vcol;
1596#ifdef FEAT_MBYTE
1597 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001598 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 else
1600#endif
1601 ++new_cursor_col;
1602 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1603 }
1604 vcol = last_vcol;
1605
1606 /*
1607 * May need to insert spaces to be able to position the cursor on
1608 * the right screen column.
1609 */
1610 if (vcol != (int)curwin->w_virtcol)
1611 {
1612 curwin->w_cursor.col = new_cursor_col;
1613 i = (int)curwin->w_virtcol - vcol;
1614 ptr = alloc(i + 1);
1615 if (ptr != NULL)
1616 {
1617 new_cursor_col += i;
1618 ptr[i] = NUL;
1619 while (--i >= 0)
1620 ptr[i] = ' ';
1621 ins_str(ptr);
1622 vim_free(ptr);
1623 }
1624 }
1625
1626 /*
1627 * When changing the indent while the cursor is in it, reset
1628 * Insstart_col to 0.
1629 */
1630 insstart_less = MAXCOL;
1631 }
1632
1633 curwin->w_p_list = save_p_list;
1634
1635 if (new_cursor_col <= 0)
1636 curwin->w_cursor.col = 0;
1637 else
1638 curwin->w_cursor.col = new_cursor_col;
1639 curwin->w_set_curswant = TRUE;
1640 changed_cline_bef_curs();
1641
1642 /*
1643 * May have to adjust the start of the insert.
1644 */
1645 if (State & INSERT)
1646 {
1647 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1648 {
1649 if ((int)Insstart.col <= insstart_less)
1650 Insstart.col = 0;
1651 else
1652 Insstart.col -= insstart_less;
1653 }
1654 if ((int)ai_col <= insstart_less)
1655 ai_col = 0;
1656 else
1657 ai_col -= insstart_less;
1658 }
1659
1660 /*
1661 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1662 * If the number of characters before the cursor decreased, need to pop a
1663 * few characters from the replace stack.
1664 * If the number of characters before the cursor increased, need to push a
1665 * few NULs onto the replace stack.
1666 */
1667 if (REPLACE_NORMAL(State) && start_col >= 0)
1668 {
1669 while (start_col > (int)curwin->w_cursor.col)
1670 {
1671 replace_join(0); /* remove a NUL from the replace stack */
1672 --start_col;
1673 }
1674 while (start_col < (int)curwin->w_cursor.col || replaced)
1675 {
1676 replace_push(NUL);
1677 if (replaced)
1678 {
1679 replace_push(replaced);
1680 replaced = NUL;
1681 }
1682 ++start_col;
1683 }
1684 }
1685
1686#ifdef FEAT_VREPLACE
1687 /*
1688 * For VREPLACE mode, we also have to fix the replace stack. In this case
1689 * it is always possible because we backspace over the whole line and then
1690 * put it back again the way we wanted it.
1691 */
1692 if (State & VREPLACE_FLAG)
1693 {
1694 /* If orig_line didn't allocate, just return. At least we did the job,
1695 * even if you can't backspace. */
1696 if (orig_line == NULL)
1697 return;
1698
1699 /* Save new line */
1700 new_line = vim_strsave(ml_get_curline());
1701 if (new_line == NULL)
1702 return;
1703
1704 /* We only put back the new line up to the cursor */
1705 new_line[curwin->w_cursor.col] = NUL;
1706
1707 /* Put back original line */
1708 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1709 curwin->w_cursor.col = orig_col;
1710
1711 /* Backspace from cursor to start of line */
1712 backspace_until_column(0);
1713
1714 /* Insert new stuff into line again */
1715 ins_bytes(new_line);
1716
1717 vim_free(new_line);
1718 }
1719#endif
1720}
1721
1722/*
1723 * Truncate the space at the end of a line. This is to be used only in an
1724 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1725 * modes.
1726 */
1727 void
1728truncate_spaces(line)
1729 char_u *line;
1730{
1731 int i;
1732
1733 /* find start of trailing white space */
1734 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1735 {
1736 if (State & REPLACE_FLAG)
1737 replace_join(0); /* remove a NUL from the replace stack */
1738 }
1739 line[i + 1] = NUL;
1740}
1741
1742#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1743 || defined(FEAT_COMMENTS) || defined(PROTO)
1744/*
1745 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1746 * modes correctly. May also be used when not in insert mode at all.
1747 */
1748 void
1749backspace_until_column(col)
1750 int col;
1751{
1752 while ((int)curwin->w_cursor.col > col)
1753 {
1754 curwin->w_cursor.col--;
1755 if (State & REPLACE_FLAG)
1756 replace_do_bs();
1757 else
1758 (void)del_char(FALSE);
1759 }
1760}
1761#endif
1762
1763#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1764/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001765 * CTRL-X pressed in Insert mode.
1766 */
1767 static void
1768ins_ctrl_x()
1769{
1770 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1771 * CTRL-V works like CTRL-N */
1772 if (ctrl_x_mode != CTRL_X_CMDLINE)
1773 {
1774 /* if the next ^X<> won't ADD nothing, then reset
1775 * compl_cont_status */
1776 if (compl_cont_status & CONT_N_ADDS)
1777 compl_cont_status = (compl_cont_status | CONT_INTRPT);
1778 else
1779 compl_cont_status = 0;
1780 /* We're not sure which CTRL-X mode it will be yet */
1781 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1782 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1783 edit_submode_pre = NULL;
1784 showmode();
1785 }
1786}
1787
1788/*
1789 * Return TRUE if the 'dict' or 'tsr' option can be used.
1790 */
1791 static int
1792has_compl_option(dict_opt)
1793 int dict_opt;
1794{
1795 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1796 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1797 {
1798 ctrl_x_mode = 0;
1799 edit_submode = NULL;
1800 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1801 : (char_u *)_("'thesaurus' option is empty"),
1802 hl_attr(HLF_E));
1803 if (emsg_silent == 0)
1804 {
1805 vim_beep();
1806 setcursor();
1807 out_flush();
1808 ui_delay(2000L, FALSE);
1809 }
1810 return FALSE;
1811 }
1812 return TRUE;
1813}
1814
1815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1817 * This depends on the current mode.
1818 */
1819 int
1820vim_is_ctrl_x_key(c)
1821 int c;
1822{
1823 /* Always allow ^R - let it's results then be checked */
1824 if (c == Ctrl_R)
1825 return TRUE;
1826
1827 switch (ctrl_x_mode)
1828 {
1829 case 0: /* Not in any CTRL-X mode */
1830 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1831 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001832 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1834 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1835 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001836 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1837 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 case CTRL_X_SCROLL:
1839 return (c == Ctrl_Y || c == Ctrl_E);
1840 case CTRL_X_WHOLE_LINE:
1841 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1842 case CTRL_X_FILES:
1843 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1844 case CTRL_X_DICTIONARY:
1845 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1846 case CTRL_X_THESAURUS:
1847 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1848 case CTRL_X_TAGS:
1849 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1850#ifdef FEAT_FIND_ID
1851 case CTRL_X_PATH_PATTERNS:
1852 return (c == Ctrl_P || c == Ctrl_N);
1853 case CTRL_X_PATH_DEFINES:
1854 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1855#endif
1856 case CTRL_X_CMDLINE:
1857 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1858 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001859#ifdef FEAT_COMPL_FUNC
1860 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001861 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001862 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001863 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001864#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001865 case CTRL_X_SPELL:
1866 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868 EMSG(_(e_internal));
1869 return FALSE;
1870}
1871
1872/*
1873 * This is like ins_compl_add(), but if ic and inf are set, then the
1874 * case of the originally typed text is used, and the case of the completed
1875 * text is infered, ie this tries to work out what case you probably wanted
1876 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001877 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 */
1879 int
Bram Moolenaar572cb562005-08-05 21:35:02 +00001880ins_compl_add_infercase(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 char_u *str;
1882 int len;
1883 char_u *fname;
1884 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001885 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886{
1887 int has_lower = FALSE;
1888 int was_letter = FALSE;
1889 int idx;
1890
1891 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
1892 {
1893 /* Infer case of completed part -- webb */
1894 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001895 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
1897 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001898 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001900 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 {
1902 has_lower = TRUE;
1903 if (isupper(IObuff[idx]))
1904 {
1905 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001906 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
1908 break;
1909 }
1910 }
1911 }
1912
1913 /*
1914 * Rule 2: No lower case, 2nd consecutive letter converted to
1915 * upper case.
1916 */
1917 if (!has_lower)
1918 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001919 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001921 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 && islower(IObuff[idx]))
1923 {
1924 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001925 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
1927 break;
1928 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001929 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931 }
1932
1933 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001934 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935
Bram Moolenaar572cb562005-08-05 21:35:02 +00001936 return ins_compl_add(IObuff, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00001938 return ins_compl_add(str, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939}
1940
1941/*
1942 * Add a match to the list of matches.
1943 * If the given string is already in the list of completions, then return
1944 * FAIL, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaar572cb562005-08-05 21:35:02 +00001945 * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb.
1946 *
1947 * New:
1948 * If the given string is already in the list of completions, then return
1949 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
1950 * maybe because alloc() returns NULL, then FAIL is returned -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00001952 int
1953ins_compl_add(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 char_u *str;
1955 int len;
1956 char_u *fname;
1957 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001958 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959{
Bram Moolenaar572cb562005-08-05 21:35:02 +00001960 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961
1962 ui_breakcheck();
1963 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00001964 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 if (len < 0)
1966 len = (int)STRLEN(str);
1967
1968 /*
1969 * If the same match is already present, don't add it.
1970 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001971 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001973 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 do
1975 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00001976 if ( !(match->cp_flags & ORIGINAL_TEXT)
1977 && STRNCMP(match->cp_str, str, (size_t)len) == 0
1978 && match->cp_str[len] == NUL)
1979 return NOTDONE;
1980 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001981 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 }
1983
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001984 /* Remove any popup menu before changing the list of matches. */
1985 ins_compl_del_pum();
1986
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 /*
1988 * Allocate a new match structure.
1989 * Copy the values to the new match structure.
1990 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00001991 match = (compl_T *)alloc((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00001993 return FAIL;
1994 match->cp_number = -1;
1995 if (flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00001997 match->cp_number = 0;
1998 match->cp_str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002000 else if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 {
2002 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002003 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 }
2005 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002006 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2008 * - NULL otherwise. --Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002009 if (fname && compl_curr_match && compl_curr_match->cp_fname
2010 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
2011 match->cp_fname = compl_curr_match->cp_fname;
2012 else if (fname && (match->cp_fname = vim_strsave(fname)) != NULL)
2013 flags |= FREE_FNAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002015 match->cp_fname = NULL;
2016 match->cp_flags = flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017
2018 /*
2019 * Link the new match structure in the list of matches.
2020 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002021 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002022 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 else if (dir == FORWARD)
2024 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002025 match->cp_next = compl_curr_match->cp_next;
2026 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
2028 else /* BACKWARD */
2029 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002030 match->cp_next = compl_curr_match;
2031 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002033 if (match->cp_next)
2034 match->cp_next->cp_prev = match;
2035 if (match->cp_prev)
2036 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002038 compl_first_match = match;
2039 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040
2041 return OK;
2042}
2043
2044/*
2045 * Add an array of matches to the list of matches.
2046 * Frees matches[].
2047 */
2048 static void
2049ins_compl_add_matches(num_matches, matches, dir)
2050 int num_matches;
2051 char_u **matches;
2052 int dir;
2053{
2054 int i;
2055 int add_r = OK;
2056 int ldir = dir;
2057
Bram Moolenaar572cb562005-08-05 21:35:02 +00002058 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 if ((add_r = ins_compl_add(matches[i], -1, NULL, ldir, 0)) == OK)
2060 /* if dir was BACKWARD then honor it just once */
2061 ldir = FORWARD;
2062 FreeWild(num_matches, matches);
2063}
2064
2065/* Make the completion list cyclic.
2066 * Return the number of matches (excluding the original).
2067 */
2068 static int
2069ins_compl_make_cyclic()
2070{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002071 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 int count = 0;
2073
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002074 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
2076 /*
2077 * Find the end of the list.
2078 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002079 match = compl_first_match;
2080 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002081 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002083 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 ++count;
2085 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002086 match->cp_next = compl_first_match;
2087 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
2089 return count;
2090}
2091
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002092static char_u **compl_match_array = NULL;
2093static int compl_match_arraysize;
2094
2095/*
2096 * Update the screen and when there is any scrolling remove the popup menu.
2097 */
2098 static void
2099ins_compl_upd_pum()
2100{
2101 int h;
2102
2103 if (compl_match_array != NULL)
2104 {
2105 h = curwin->w_cline_height;
2106 update_screen(0);
2107 if (h != curwin->w_cline_height)
2108 ins_compl_del_pum();
2109 }
2110}
2111
2112/*
2113 * Remove any popup menu.
2114 */
2115 static void
2116ins_compl_del_pum()
2117{
2118 if (compl_match_array != NULL)
2119 {
2120 pum_undisplay();
2121 vim_free(compl_match_array);
2122 compl_match_array = NULL;
2123 }
2124}
2125
2126/*
2127 * Return TRUE if the popup menu should be displayed.
2128 */
2129 static int
2130pum_wanted()
2131{
2132 compl_T *compl;
2133 int i;
2134
2135 /* 'completeopt' must contain "menu" */
2136 if (*p_cot == NUL)
2137 return FALSE;
2138
2139 /* The display looks bad on a B&W display. */
2140 if (t_colors < 8
2141#ifdef FEAT_GUI
2142 && !gui.in_use
2143#endif
2144 )
2145 return FALSE;
2146
2147 /* Don't display the popup menu if there are no matches or there is only
2148 * one (ignoring the original text). */
2149 compl = compl_first_match;
2150 i = 0;
2151 do
2152 {
2153 if (compl == NULL
2154 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2155 break;
2156 compl = compl->cp_next;
2157 } while (compl != compl_first_match);
2158
2159 return (i >= 2);
2160}
2161
2162/*
2163 * Show the popup menu for the list of matches.
2164 */
2165 static void
2166ins_compl_show_pum()
2167{
2168 compl_T *compl;
2169 int i;
2170 int cur = -1;
2171 colnr_T col;
2172
2173 if (!pum_wanted())
2174 return;
2175
2176 /* Update the screen before drawing the popup menu over it. */
2177 update_screen(0);
2178
2179 if (compl_match_array == NULL)
2180 {
2181 /* Need to build the popup menu list. */
2182 compl_match_arraysize = 0;
2183 compl = compl_first_match;
2184 do
2185 {
2186 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2187 ++compl_match_arraysize;
2188 compl = compl->cp_next;
2189 } while (compl != NULL && compl != compl_first_match);
2190 compl_match_array = (char_u **)alloc((unsigned)(sizeof(char_u **)
2191 * compl_match_arraysize));
2192 if (compl_match_array != NULL)
2193 {
2194 i = 0;
2195 compl = compl_first_match;
2196 do
2197 {
2198 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2199 {
2200 if (compl == compl_shown_match)
2201 cur = i;
2202 compl_match_array[i++] = compl->cp_str;
2203 }
2204 compl = compl->cp_next;
2205 } while (compl != NULL && compl != compl_first_match);
2206 }
2207 }
2208 else
2209 {
2210 /* popup menu already exists, only need to find the current item.*/
2211 i = 0;
2212 compl = compl_first_match;
2213 do
2214 {
2215 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2216 {
2217 if (compl == compl_shown_match)
2218 {
2219 cur = i;
2220 break;
2221 }
2222 ++i;
2223 }
2224 compl = compl->cp_next;
2225 } while (compl != NULL && compl != compl_first_match);
2226 }
2227
2228 if (compl_match_array != NULL)
2229 {
2230 /* Compute the screen column of the start of the completed text.
2231 * Use the cursor to get all wrapping and other settings right. */
2232 col = curwin->w_cursor.col;
2233 curwin->w_cursor.col = compl_col;
2234 validate_cursor_col();
2235 pum_display(compl_match_array, compl_match_arraysize, cur,
2236 curwin->w_cline_row + W_WINROW(curwin),
2237 curwin->w_cline_height,
2238 curwin->w_wcol + W_WINCOL(curwin));
2239 curwin->w_cursor.col = col;
2240 }
2241}
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243#define DICT_FIRST (1) /* use just first element in "dict" */
2244#define DICT_EXACT (2) /* "dict" is the exact name of a file */
2245/*
2246 * Add any identifiers that match the given pattern to the list of
2247 * completions.
2248 */
2249 static void
2250ins_compl_dictionaries(dict, pat, dir, flags, thesaurus)
2251 char_u *dict;
2252 char_u *pat;
2253 int dir;
2254 int flags;
2255 int thesaurus;
2256{
2257 char_u *ptr;
2258 char_u *buf;
2259 FILE *fp;
2260 regmatch_T regmatch;
2261 int add_r;
2262 char_u **files;
2263 int count;
2264 int i;
2265 int save_p_scs;
2266
2267 buf = alloc(LSIZE);
2268 /* If 'infercase' is set, don't use 'smartcase' here */
2269 save_p_scs = p_scs;
2270 if (curbuf->b_p_inf)
2271 p_scs = FALSE;
2272 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2273 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2274 regmatch.rm_ic = ignorecase(pat);
2275 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002276 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
2278 /* copy one dictionary file name into buf */
2279 if (flags == DICT_EXACT)
2280 {
2281 count = 1;
2282 files = &dict;
2283 }
2284 else
2285 {
2286 /* Expand wildcards in the dictionary name, but do not allow
2287 * backticks (for security, the 'dict' option may have been set in
2288 * a modeline). */
2289 copy_option_part(&dict, buf, LSIZE, ",");
2290 if (vim_strchr(buf, '`') != NULL
2291 || expand_wildcards(1, &buf, &count, &files,
2292 EW_FILE|EW_SILENT) != OK)
2293 count = 0;
2294 }
2295
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002296 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
2298 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2299 if (flags != DICT_EXACT)
2300 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002301 vim_snprintf((char *)IObuff, IOSIZE,
2302 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2304 }
2305
2306 if (fp != NULL)
2307 {
2308 /*
2309 * Read dictionary file line by line.
2310 * Check each line for a match.
2311 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002312 while (!got_int && !compl_interrupted
2313 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 ptr = buf;
2316 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2317 {
2318 ptr = regmatch.startp[0];
2319 ptr = find_word_end(ptr);
2320 add_r = ins_compl_add_infercase(regmatch.startp[0],
2321 (int)(ptr - regmatch.startp[0]),
2322 files[i], dir, 0);
2323 if (thesaurus)
2324 {
2325 char_u *wstart;
2326
2327 /*
2328 * Add the other matches on the line
2329 */
2330 while (!got_int)
2331 {
2332 /* Find start of the next word. Skip white
2333 * space and punctuation. */
2334 ptr = find_word_start(ptr);
2335 if (*ptr == NUL || *ptr == NL)
2336 break;
2337 wstart = ptr;
2338
2339 /* Find end of the word and add it. */
2340#ifdef FEAT_MBYTE
2341 if (has_mbyte)
2342 /* Japanese words may have characters in
2343 * different classes, only separate words
2344 * with single-byte non-word characters. */
2345 while (*ptr != NUL)
2346 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002347 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348
2349 if (l < 2 && !vim_iswordc(*ptr))
2350 break;
2351 ptr += l;
2352 }
2353 else
2354#endif
2355 ptr = find_word_end(ptr);
2356 add_r = ins_compl_add_infercase(wstart,
2357 (int)(ptr - wstart), files[i], dir, 0);
2358 }
2359 }
2360 if (add_r == OK)
2361 /* if dir was BACKWARD then honor it just once */
2362 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002363 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 break;
2365 /* avoid expensive call to vim_regexec() when at end
2366 * of line */
2367 if (*ptr == '\n' || got_int)
2368 break;
2369 }
2370 line_breakcheck();
Bram Moolenaar572cb562005-08-05 21:35:02 +00002371 ins_compl_check_keys(50);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
2373 fclose(fp);
2374 }
2375 }
2376 if (flags != DICT_EXACT)
2377 FreeWild(count, files);
2378 if (flags)
2379 break;
2380 }
2381 p_scs = save_p_scs;
2382 vim_free(regmatch.regprog);
2383 vim_free(buf);
2384}
2385
2386/*
2387 * Find the start of the next word.
2388 * Returns a pointer to the first char of the word. Also stops at a NUL.
2389 */
2390 char_u *
2391find_word_start(ptr)
2392 char_u *ptr;
2393{
2394#ifdef FEAT_MBYTE
2395 if (has_mbyte)
2396 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002397 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 else
2399#endif
2400 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2401 ++ptr;
2402 return ptr;
2403}
2404
2405/*
2406 * Find the end of the word. Assumes it starts inside a word.
2407 * Returns a pointer to just after the word.
2408 */
2409 char_u *
2410find_word_end(ptr)
2411 char_u *ptr;
2412{
2413#ifdef FEAT_MBYTE
2414 int start_class;
2415
2416 if (has_mbyte)
2417 {
2418 start_class = mb_get_class(ptr);
2419 if (start_class > 1)
2420 while (*ptr != NUL)
2421 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002422 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 if (mb_get_class(ptr) != start_class)
2424 break;
2425 }
2426 }
2427 else
2428#endif
2429 while (vim_iswordc(*ptr))
2430 ++ptr;
2431 return ptr;
2432}
2433
2434/*
2435 * Free the list of completions
2436 */
2437 static void
2438ins_compl_free()
2439{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002440 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002442 vim_free(compl_pattern);
2443 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002445 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002447
2448 ins_compl_del_pum();
2449 pum_clear();
2450
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002451 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 do
2453 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002454 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002455 compl_curr_match = compl_curr_match->cp_next;
2456 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002458 if (match->cp_flags & FREE_FNAME)
2459 vim_free(match->cp_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002461 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2462 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463}
2464
2465 static void
2466ins_compl_clear()
2467{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002468 compl_cont_status = 0;
2469 compl_started = FALSE;
2470 compl_matches = 0;
2471 vim_free(compl_pattern);
2472 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 save_sm = -1;
2474 edit_submode_extra = NULL;
2475}
2476
2477/*
2478 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002479 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002480 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002482 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483ins_compl_prep(c)
2484 int c;
2485{
2486 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 int temp;
2488 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002489 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
2491 /* Forget any previous 'special' messages if this is actually
2492 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2493 */
2494 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2495 edit_submode_extra = NULL;
2496
2497 /* Ignore end of Select mode mapping */
2498 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002499 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
2501 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2502 {
2503 /*
2504 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2505 * it will be yet. Now we decide.
2506 */
2507 switch (c)
2508 {
2509 case Ctrl_E:
2510 case Ctrl_Y:
2511 ctrl_x_mode = CTRL_X_SCROLL;
2512 if (!(State & REPLACE_FLAG))
2513 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2514 else
2515 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2516 edit_submode_pre = NULL;
2517 showmode();
2518 break;
2519 case Ctrl_L:
2520 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2521 break;
2522 case Ctrl_F:
2523 ctrl_x_mode = CTRL_X_FILES;
2524 break;
2525 case Ctrl_K:
2526 ctrl_x_mode = CTRL_X_DICTIONARY;
2527 break;
2528 case Ctrl_R:
2529 /* Simply allow ^R to happen without affecting ^X mode */
2530 break;
2531 case Ctrl_T:
2532 ctrl_x_mode = CTRL_X_THESAURUS;
2533 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002534#ifdef FEAT_COMPL_FUNC
2535 case Ctrl_U:
2536 ctrl_x_mode = CTRL_X_FUNCTION;
2537 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002538 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002539 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002540 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002541#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002542 case 's':
2543 case Ctrl_S:
2544 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002545#ifdef FEAT_SYN_HL
2546 spell_back_to_badword();
2547#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002548 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 case Ctrl_RSB:
2550 ctrl_x_mode = CTRL_X_TAGS;
2551 break;
2552#ifdef FEAT_FIND_ID
2553 case Ctrl_I:
2554 case K_S_TAB:
2555 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2556 break;
2557 case Ctrl_D:
2558 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2559 break;
2560#endif
2561 case Ctrl_V:
2562 case Ctrl_Q:
2563 ctrl_x_mode = CTRL_X_CMDLINE;
2564 break;
2565 case Ctrl_P:
2566 case Ctrl_N:
2567 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
2568 * just started ^X mode, or there were enough ^X's to cancel
2569 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2570 * do normal expansion when interrupting a different mode (say
2571 * ^X^F^X^P or ^P^X^X^P, see below)
2572 * nothing changes if interrupting mode 0, (eg, the flag
2573 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002574 if (!(compl_cont_status & CONT_INTRPT))
2575 compl_cont_status |= CONT_LOCAL;
2576 else if (compl_cont_mode != 0)
2577 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 /* FALLTHROUGH */
2579 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002580 /* If we have typed at least 2 ^X's... for modes != 0, we set
2581 * compl_cont_status = 0 (eg, as if we had just started ^X
2582 * mode).
2583 * For mode 0, we set "compl_cont_mode" to an impossible
2584 * value, in both cases ^X^X can be used to restart the same
2585 * mode (avoiding ADDING mode).
2586 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2587 * 'complete' and local ^P expansions respectively.
2588 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2589 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if (c == Ctrl_X)
2591 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002592 if (compl_cont_mode != 0)
2593 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002595 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
2597 ctrl_x_mode = 0;
2598 edit_submode = NULL;
2599 showmode();
2600 break;
2601 }
2602 }
2603 else if (ctrl_x_mode != 0)
2604 {
2605 /* We're already in CTRL-X mode, do we stay in it? */
2606 if (!vim_is_ctrl_x_key(c))
2607 {
2608 if (ctrl_x_mode == CTRL_X_SCROLL)
2609 ctrl_x_mode = 0;
2610 else
2611 ctrl_x_mode = CTRL_X_FINISHED;
2612 edit_submode = NULL;
2613 }
2614 showmode();
2615 }
2616
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002617 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 {
2619 /* Show error message from attempted keyword completion (probably
2620 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002621 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 showmode();
2623 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R)
2624 || ctrl_x_mode == CTRL_X_FINISHED)
2625 {
2626 /* Get here when we have finished typing a sequence of ^N and
2627 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002628 * memory that was used, and make sure we can redo the insert. */
2629 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002631 char_u *p;
2632
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 /*
2634 * If any of the original typed text has been changed,
2635 * eg when ignorecase is set, we must add back-spaces to
2636 * the redo buffer. We add as few as necessary to delete
2637 * just the part of the original text that has changed.
2638 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002639 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002640 p = compl_orig_text;
2641 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002643 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 ++ptr;
2645 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002646 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 AppendCharToRedobuff(K_BS);
2648 AppendToRedobuffLit(ptr);
2649 }
2650
2651#ifdef FEAT_CINDENT
2652 want_cindent = (can_cindent && cindent_on());
2653#endif
2654 /*
2655 * When completing whole lines: fix indent for 'cindent'.
2656 * Otherwise, break line if it's too long.
2657 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002658 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 {
2660#ifdef FEAT_CINDENT
2661 /* re-indent the current line */
2662 if (want_cindent)
2663 {
2664 do_c_expr_indent();
2665 want_cindent = FALSE; /* don't do it again */
2666 }
2667#endif
2668 }
2669 else
2670 {
2671 /* put the cursor on the last char, for 'tw' formatting */
2672 curwin->w_cursor.col--;
2673 if (stop_arrow() == OK)
2674 insertchar(NUL, 0, -1);
2675 curwin->w_cursor.col++;
2676 }
2677
2678 auto_format(FALSE, TRUE);
2679
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002680 /* if the popup menu is displayed hitting Enter means accepting
2681 * the selection without inserting anything. */
2682 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
2683 retval = TRUE;
2684
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002686 compl_started = FALSE;
2687 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 msg_clr_cmdline(); /* necessary for "noshowmode" */
2689 ctrl_x_mode = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002690 if (save_sm >= 0)
2691 p_sm = save_sm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 if (edit_submode != NULL)
2693 {
2694 edit_submode = NULL;
2695 showmode();
2696 }
2697
2698#ifdef FEAT_CINDENT
2699 /*
2700 * Indent now if a key was typed that is in 'cinkeys'.
2701 */
2702 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2703 do_c_expr_indent();
2704#endif
2705 }
2706 }
2707
2708 /* reset continue_* if we left expansion-mode, if we stay they'll be
2709 * (re)set properly in ins_complete() */
2710 if (!vim_is_ctrl_x_key(c))
2711 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002712 compl_cont_status = 0;
2713 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002715
2716 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717}
2718
2719/*
2720 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2721 * (depending on flag) starting from buf and looking for a non-scanned
2722 * buffer (other than curbuf). curbuf is special, if it is called with
2723 * buf=curbuf then it has to be the first call for a given flag/expansion.
2724 *
2725 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2726 */
2727 static buf_T *
2728ins_compl_next_buf(buf, flag)
2729 buf_T *buf;
2730 int flag;
2731{
2732#ifdef FEAT_WINDOWS
2733 static win_T *wp;
2734#endif
2735
2736 if (flag == 'w') /* just windows */
2737 {
2738#ifdef FEAT_WINDOWS
2739 if (buf == curbuf) /* first call for this flag/expansion */
2740 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002741 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 && wp->w_buffer->b_scanned)
2743 ;
2744 buf = wp->w_buffer;
2745#else
2746 buf = curbuf;
2747#endif
2748 }
2749 else
2750 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2751 * (unlisted buffers)
2752 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002753 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 && ((flag == 'U'
2755 ? buf->b_p_bl
2756 : (!buf->b_p_bl
2757 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2758 || buf->b_scanned
2759 || (buf->b_ml.ml_mfp == NULL
2760 && ctrl_x_mode == CTRL_X_WHOLE_LINE)))
2761 ;
2762 return buf;
2763}
2764
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002765#ifdef FEAT_COMPL_FUNC
Bram Moolenaare344bea2005-09-01 20:46:49 +00002766static int expand_by_function __ARGS((int type, char_u *base, char_u ***matches));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002767
2768/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002769 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00002770 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002771 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002772 */
2773 static int
Bram Moolenaare344bea2005-09-01 20:46:49 +00002774expand_by_function(type, base, matches)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002775 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002776 char_u *base;
2777 char_u ***matches;
2778{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002779 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002780 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002781 listitem_T *li;
2782 garray_T ga;
2783 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002784 char_u *funcname;
2785 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002786
Bram Moolenaare344bea2005-09-01 20:46:49 +00002787 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
2788 if (*funcname == NUL)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002789 return 0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002790
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002791 /* Call 'completefunc' to obtain the list of matches. */
2792 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00002793 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002794
Bram Moolenaare344bea2005-09-01 20:46:49 +00002795 pos = curwin->w_cursor;
2796 matchlist = call_func_retlist(funcname, 2, args, FALSE);
2797 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002798 if (matchlist == NULL)
2799 return 0;
2800
2801 /* Go through the List with matches and put them in an array. */
2802 ga_init2(&ga, (int)sizeof(char_u *), 8);
2803 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002804 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002805 p = get_tv_string_chk(&li->li_tv);
2806 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002807 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002808 if (ga_grow(&ga, 1) == FAIL)
2809 break;
2810 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p);
2811 ++ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002812 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002813 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002814
2815 list_unref(matchlist);
2816 *matches = (char_u **)ga.ga_data;
2817 return ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002818}
2819#endif /* FEAT_COMPL_FUNC */
2820
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002821/*
2822 * Get the next expansion(s), using "compl_pattern".
2823 * The search starts at position "ini" in curbuf and in the direction dir.
2824 * When "compl_started" is FALSE start at that position, otherwise
2825 * continue where we stopped searching before.
2826 * This may return before finding all the matches.
2827 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 */
2829 static int
2830ins_compl_get_exp(ini, dir)
2831 pos_T *ini;
2832 int dir;
2833{
2834 static pos_T first_match_pos;
2835 static pos_T last_match_pos;
2836 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002837 static int found_all = FALSE; /* Found all matches of a
2838 certain type. */
2839 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840
Bram Moolenaar572cb562005-08-05 21:35:02 +00002841 pos_T *pos;
2842 char_u **matches;
2843 int save_p_scs;
2844 int save_p_ws;
2845 int save_p_ic;
2846 int i;
2847 int num_matches;
2848 int len;
2849 int found_new_match;
2850 int type = ctrl_x_mode;
2851 char_u *ptr;
2852 char_u *dict = NULL;
2853 int dict_f = 0;
2854 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002856 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
2858 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
2859 ins_buf->b_scanned = 0;
2860 found_all = FALSE;
2861 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002862 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002863 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 last_match_pos = first_match_pos = *ini;
2865 }
2866
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002867 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 pos = (dir == FORWARD) ? &last_match_pos : &first_match_pos;
2869 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
2870 for (;;)
2871 {
2872 found_new_match = FAIL;
2873
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002874 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 * or if found_all says this entry is done. For ^X^L only use the
2876 * entries from 'complete' that look in loaded buffers. */
2877 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002878 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
2880 found_all = FALSE;
2881 while (*e_cpt == ',' || *e_cpt == ' ')
2882 e_cpt++;
2883 if (*e_cpt == '.' && !curbuf->b_scanned)
2884 {
2885 ins_buf = curbuf;
2886 first_match_pos = *ini;
2887 /* So that ^N can match word immediately after cursor */
2888 if (ctrl_x_mode == 0)
2889 dec(&first_match_pos);
2890 last_match_pos = first_match_pos;
2891 type = 0;
2892 }
2893 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2894 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2895 {
2896 /* Scan a buffer, but not the current one. */
2897 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
2898 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002899 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 first_match_pos.col = last_match_pos.col = 0;
2901 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2902 last_match_pos.lnum = 0;
2903 type = 0;
2904 }
2905 else /* unloaded buffer, scan like dictionary */
2906 {
2907 found_all = TRUE;
2908 if (ins_buf->b_fname == NULL)
2909 continue;
2910 type = CTRL_X_DICTIONARY;
2911 dict = ins_buf->b_fname;
2912 dict_f = DICT_EXACT;
2913 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00002914 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 ins_buf->b_fname == NULL
2916 ? buf_spname(ins_buf)
2917 : ins_buf->b_sfname == NULL
2918 ? (char *)ins_buf->b_fname
2919 : (char *)ins_buf->b_sfname);
2920 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2921 }
2922 else if (*e_cpt == NUL)
2923 break;
2924 else
2925 {
2926 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2927 type = -1;
2928 else if (*e_cpt == 'k' || *e_cpt == 's')
2929 {
2930 if (*e_cpt == 'k')
2931 type = CTRL_X_DICTIONARY;
2932 else
2933 type = CTRL_X_THESAURUS;
2934 if (*++e_cpt != ',' && *e_cpt != NUL)
2935 {
2936 dict = e_cpt;
2937 dict_f = DICT_FIRST;
2938 }
2939 }
2940#ifdef FEAT_FIND_ID
2941 else if (*e_cpt == 'i')
2942 type = CTRL_X_PATH_PATTERNS;
2943 else if (*e_cpt == 'd')
2944 type = CTRL_X_PATH_DEFINES;
2945#endif
2946 else if (*e_cpt == ']' || *e_cpt == 't')
2947 {
2948 type = CTRL_X_TAGS;
2949 sprintf((char*)IObuff, _("Scanning tags."));
2950 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2951 }
2952 else
2953 type = -1;
2954
2955 /* in any case e_cpt is advanced to the next entry */
2956 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2957
2958 found_all = TRUE;
2959 if (type == -1)
2960 continue;
2961 }
2962 }
2963
2964 switch (type)
2965 {
2966 case -1:
2967 break;
2968#ifdef FEAT_FIND_ID
2969 case CTRL_X_PATH_PATTERNS:
2970 case CTRL_X_PATH_DEFINES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002971 find_pattern_in_path(compl_pattern, dir,
2972 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002974 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
2976 (linenr_T)1, (linenr_T)MAXLNUM);
2977 break;
2978#endif
2979
2980 case CTRL_X_DICTIONARY:
2981 case CTRL_X_THESAURUS:
2982 ins_compl_dictionaries(
2983 dict ? dict
2984 : (type == CTRL_X_THESAURUS
2985 ? (*curbuf->b_p_tsr == NUL
2986 ? p_tsr
2987 : curbuf->b_p_tsr)
2988 : (*curbuf->b_p_dict == NUL
2989 ? p_dict
2990 : curbuf->b_p_dict)),
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002991 compl_pattern, dir,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
2993 dict = NULL;
2994 break;
2995
2996 case CTRL_X_TAGS:
2997 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
2998 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002999 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003002 * of matches is found when compl_pattern is empty */
3003 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3005 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3006 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3007 {
3008 ins_compl_add_matches(num_matches, matches, dir);
3009 }
3010 p_ic = save_p_ic;
3011 break;
3012
3013 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003014 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3016 {
3017
3018 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003019 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 ins_compl_add_matches(num_matches, matches, dir);
3021 }
3022 break;
3023
3024 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003025 if (expand_cmdline(&compl_xp, compl_pattern,
3026 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 &num_matches, &matches) == EXPAND_OK)
3028 ins_compl_add_matches(num_matches, matches, dir);
3029 break;
3030
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003031#ifdef FEAT_COMPL_FUNC
3032 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003033 case CTRL_X_OMNI:
3034 if (*compl_pattern == NUL)
3035 num_matches = 0;
3036 else
3037 num_matches = expand_by_function(type, compl_pattern, &matches);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003038 if (num_matches > 0)
3039 ins_compl_add_matches(num_matches, matches, dir);
3040 break;
3041#endif
3042
Bram Moolenaar488c6512005-08-11 20:09:58 +00003043 case CTRL_X_SPELL:
3044#ifdef FEAT_SYN_HL
3045 num_matches = expand_spelling(first_match_pos.lnum,
3046 first_match_pos.col, compl_pattern, &matches);
3047 if (num_matches > 0)
3048 ins_compl_add_matches(num_matches, matches, dir);
3049#endif
3050 break;
3051
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 default: /* normal ^P/^N and ^X^L */
3053 /*
3054 * If 'infercase' is set, don't use 'smartcase' here
3055 */
3056 save_p_scs = p_scs;
3057 if (ins_buf->b_p_inf)
3058 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 /* buffers other than curbuf are scanned from the beginning or the
3061 * end but never from the middle, thus setting nowrapscan in this
3062 * buffers is a good idea, on the other hand, we always set
3063 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3064 save_p_ws = p_ws;
3065 if (ins_buf != curbuf)
3066 p_ws = FALSE;
3067 else if (*e_cpt == '.')
3068 p_ws = TRUE;
3069 for (;;)
3070 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003071 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003073 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3074 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003076 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003078 dir, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 else
3080 found_new_match = searchit(NULL, ins_buf, pos, dir,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003081 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 RE_LAST);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003083 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003085 /* set compl_started even on fail */
3086 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 first_match_pos = *pos;
3088 last_match_pos = *pos;
3089 }
3090 else if (first_match_pos.lnum == last_match_pos.lnum
3091 && first_match_pos.col == last_match_pos.col)
3092 found_new_match = FAIL;
3093 if (found_new_match == FAIL)
3094 {
3095 if (ins_buf == curbuf)
3096 found_all = TRUE;
3097 break;
3098 }
3099
3100 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003101 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 && ini->lnum == pos->lnum
3103 && ini->col == pos->col)
3104 continue;
3105 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3106 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3107 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003108 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
3110 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3111 continue;
3112 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3113 if (!p_paste)
3114 ptr = skipwhite(ptr);
3115 }
3116 len = (int)STRLEN(ptr);
3117 }
3118 else
3119 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003120 char_u *tmp_ptr = ptr;
3121
3122 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003124 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 /* Skip if already inside a word. */
3126 if (vim_iswordp(tmp_ptr))
3127 continue;
3128 /* Find start of next word. */
3129 tmp_ptr = find_word_start(tmp_ptr);
3130 }
3131 /* Find end of this word. */
3132 tmp_ptr = find_word_end(tmp_ptr);
3133 len = (int)(tmp_ptr - ptr);
3134
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003135 if ((compl_cont_status & CONT_ADDING)
3136 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 {
3138 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3139 {
3140 /* Try next line, if any. the new word will be
3141 * "join" as if the normal command "J" was used.
3142 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003143 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 * works -- Acevedo */
3145 STRNCPY(IObuff, ptr, len);
3146 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3147 tmp_ptr = ptr = skipwhite(ptr);
3148 /* Find start of next word. */
3149 tmp_ptr = find_word_start(tmp_ptr);
3150 /* Find end of next word. */
3151 tmp_ptr = find_word_end(tmp_ptr);
3152 if (tmp_ptr > ptr)
3153 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003154 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003156 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 IObuff[len++] = ' ';
3158 /* IObuf =~ "\k.* ", thus len >= 2 */
3159 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003160 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 || (vim_strchr(p_cpo, CPO_JOINSP)
3162 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003163 && (IObuff[len - 2] == '?'
3164 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 IObuff[len++] = ' ';
3166 }
3167 /* copy as much as posible of the new word */
3168 if (tmp_ptr - ptr >= IOSIZE - len)
3169 tmp_ptr = ptr + IOSIZE - len - 1;
3170 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3171 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003172 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 }
3174 IObuff[len] = NUL;
3175 ptr = IObuff;
3176 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003177 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 continue;
3179 }
3180 }
3181 if (ins_compl_add_infercase(ptr, len,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003182 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar572cb562005-08-05 21:35:02 +00003183 dir, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
3185 found_new_match = OK;
3186 break;
3187 }
3188 }
3189 p_scs = save_p_scs;
3190 p_ws = save_p_ws;
3191 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003192
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003193 /* check if compl_curr_match has changed, (e.g. other type of
3194 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003195 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 found_new_match = OK;
3197
3198 /* break the loop for specialized modes (use 'complete' just for the
3199 * generic ctrl_x_mode == 0) or when we've found a new match */
3200 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003201 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003202 {
3203 if (got_int)
3204 break;
3205 if (pum_wanted() && type != -1)
3206 /* Fill the popup menu as soon as possible. */
3207 ins_compl_check_keys(0);
3208 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3209 || compl_interrupted)
3210 break;
3211 compl_started = TRUE;
3212 }
3213 else
3214 {
3215 /* Mark a buffer scanned when it has been scanned completely */
3216 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3217 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003219 compl_started = FALSE;
3220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003222 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223
3224 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3225 && *e_cpt == NUL) /* Got to end of 'complete' */
3226 found_new_match = FAIL;
3227
3228 i = -1; /* total of matches, unknown */
3229 if (found_new_match == FAIL
3230 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3231 i = ins_compl_make_cyclic();
3232
3233 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003234 * just been made cyclic then we have to move compl_curr_match to the next
3235 * or previous entry (if any) -- Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003236 compl_curr_match = dir == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003237 if (compl_curr_match == NULL)
3238 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 return i;
3240}
3241
3242/* Delete the old text being completed. */
3243 static void
3244ins_compl_delete()
3245{
3246 int i;
3247
3248 /*
3249 * In insert mode: Delete the typed part.
3250 * In replace mode: Put the old characters back, if any.
3251 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003252 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 backspace_until_column(i);
3254 changed_cline_bef_curs();
3255}
3256
3257/* Insert the new text being completed. */
3258 static void
3259ins_compl_insert()
3260{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003261 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262}
3263
3264/*
3265 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003266 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3267 * get more completions. If it is FALSE, then we just do nothing when there
3268 * are no more completions in a given direction. The latter case is used when
3269 * we are still in the middle of finding completions, to allow browsing
3270 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 * Return the total number of matches, or -1 if still unknown -- webb.
3272 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003273 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3274 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 *
3276 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003277 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3278 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 */
3280 static int
3281ins_compl_next(allow_get_expansion)
3282 int allow_get_expansion;
3283{
3284 int num_matches = -1;
3285 int i;
3286
3287 if (allow_get_expansion)
3288 {
3289 /* Delete old text to be replaced */
3290 ins_compl_delete();
3291 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003292 compl_pending = FALSE;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003293 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
3294 compl_shown_match = compl_shown_match->cp_next;
3295 else if (compl_shows_dir == BACKWARD && compl_shown_match->cp_prev != NULL)
3296 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 else
3298 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003299 compl_pending = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 if (allow_get_expansion)
3301 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003302 num_matches = ins_compl_get_exp(&compl_startpos, compl_direction);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003303 if (compl_pending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003305 if (compl_direction == compl_shows_dir)
3306 compl_shown_match = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
3308 }
3309 else
3310 return -1;
3311 }
3312
3313 /* Insert the text of the new completion */
3314 ins_compl_insert();
3315
3316 if (!allow_get_expansion)
3317 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003318 /* may undisplay the popup menu first */
3319 ins_compl_upd_pum();
3320
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 /* Display the current match. */
3322 update_screen(0);
3323
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003324 /* display the updated popup menu */
3325 ins_compl_show_pum();
3326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 /* Delete old text to be replaced, since we're still searching and
3328 * don't want to match ourselves! */
3329 ins_compl_delete();
3330 }
3331
3332 /*
3333 * Show the file name for the match (if any)
3334 * Truncate the file name to avoid a wait for return.
3335 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003336 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 {
3338 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003339 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 if (i <= 0)
3341 i = 0;
3342 else
3343 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003344 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 msg(IObuff);
3346 redraw_cmdline = FALSE; /* don't overwrite! */
3347 }
3348
3349 return num_matches;
3350}
3351
3352/*
3353 * Call this while finding completions, to check whether the user has hit a key
3354 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003355 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003357 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 */
3359 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003360ins_compl_check_keys(frequency)
3361 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363 static int count = 0;
3364
3365 int c;
3366
3367 /* Don't check when reading keys from a script. That would break the test
3368 * scripts */
3369 if (using_script())
3370 return;
3371
3372 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003373 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 return;
3375 count = 0;
3376
3377 ++no_mapping;
3378 c = vpeekc_any();
3379 --no_mapping;
3380 if (c != NUL)
3381 {
3382 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3383 {
3384 c = safe_vgetc(); /* Eat the character */
3385 if (c == Ctrl_P || c == Ctrl_L)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003386 compl_shows_dir = BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003388 compl_shows_dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 (void)ins_compl_next(FALSE);
3390 }
3391 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003392 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003394 if (compl_pending && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 (void)ins_compl_next(FALSE);
3396}
3397
3398/*
3399 * Do Insert mode completion.
3400 * Called when character "c" was typed, which has a meaning for completion.
3401 * Returns OK if completion was done, FAIL if something failed (out of mem).
3402 */
3403 static int
3404ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003405 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003407 char_u *line;
3408 int startcol = 0; /* column where searched text starts */
3409 colnr_T curs_col; /* cursor column */
3410 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
3412 if (c == Ctrl_P || c == Ctrl_L)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003413 compl_direction = BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003415 compl_direction = FORWARD;
3416 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 {
3418 /* First time we hit ^N or ^P (in a row, I mean) */
3419
3420 /* Turn off 'sm' so we don't show matches with ^X^L */
3421 save_sm = p_sm;
3422 p_sm = FALSE;
3423
3424 did_ai = FALSE;
3425#ifdef FEAT_SMARTINDENT
3426 did_si = FALSE;
3427 can_si = FALSE;
3428 can_si_back = FALSE;
3429#endif
3430 if (stop_arrow() == FAIL)
3431 return FAIL;
3432
3433 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003434 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
3436 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003437 * "compl_startpos" to the cursor as a pattern to add a new word
3438 * instead of expand the one before the cursor, in word-wise if
3439 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 * is not in the same line as the cursor then fix it (the line has
3441 * been split because it was longer than 'tw'). if SOL is set then
3442 * skip the previous pattern, a word at the beginning of the line has
3443 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003444 if ((compl_cont_status & CONT_INTRPT) && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
3446 /*
3447 * it is a continued search
3448 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003449 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3451 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3452 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003453 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003455 /* line (probably) wrapped, set compl_startpos to the
3456 * first non_blank in the line, if it is not a wordchar
3457 * include it to get a better pattern, but then we don't
3458 * want the "\\<" prefix, check it bellow */
3459 compl_col = (colnr_T)(skipwhite(line) - line);
3460 compl_startpos.col = compl_col;
3461 compl_startpos.lnum = curwin->w_cursor.lnum;
3462 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 }
3464 else
3465 {
3466 /* S_IPOS was set when we inserted a word that was at the
3467 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003468 * mode but first we need to redefine compl_startpos */
3469 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003471 compl_cont_status |= CONT_SOL;
3472 compl_startpos.col = (colnr_T)(skipwhite(
3473 line + compl_length
3474 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003476 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003478 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003479 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 * have enough space? just being paranoic */
3481#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003482 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003484 compl_cont_status &= ~CONT_SOL;
3485 compl_length = (IOSIZE - MIN_SPACE);
3486 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003488 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3489 if (compl_length < 1)
3490 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
3492 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003493 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003495 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
3497 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003498 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003500 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003502 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003504 compl_cont_status = 0;
3505 compl_cont_status |= CONT_N_ADDS;
3506 compl_startpos = curwin->w_cursor;
3507 startcol = (int)curs_col;
3508 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510
3511 /* Work out completion pattern and original text -- webb */
3512 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3513 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003514 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3516 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003517 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003519 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003521 compl_col += ++startcol;
3522 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
3524 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003525 compl_pattern = str_foldcase(line + compl_col,
3526 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003528 compl_pattern = vim_strnsave(line + compl_col,
3529 compl_length);
3530 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 return FAIL;
3532 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003533 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
3535 char_u *prefix = (char_u *)"\\<";
3536
3537 /* we need 3 extra chars, 1 for the NUL and
3538 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003539 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3540 compl_length) + 3);
3541 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003543 if (!vim_iswordp(line + compl_col)
3544 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 && (
3546#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003547 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003549 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550#endif
3551 )))
3552 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003553 STRCPY((char *)compl_pattern, prefix);
3554 (void)quote_meta(compl_pattern + STRLEN(prefix),
3555 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003557 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003559 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003561 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562#endif
3563 )
3564 {
3565 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003566 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3567 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003569 compl_col += curs_col;
3570 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572 else
3573 {
3574#ifdef FEAT_MBYTE
3575 /* Search the point of change class of multibyte character
3576 * or not a word single byte character backward. */
3577 if (has_mbyte)
3578 {
3579 int base_class;
3580 int head_off;
3581
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003582 startcol -= (*mb_head_off)(line, line + startcol);
3583 base_class = mb_get_class(line + startcol);
3584 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003586 head_off = (*mb_head_off)(line, line + startcol);
3587 if (base_class != mb_get_class(line + startcol
3588 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003590 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 }
3592 }
3593 else
3594#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003595 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003597 compl_col += ++startcol;
3598 compl_length = (int)curs_col - startcol;
3599 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
3601 /* Only match word with at least two chars -- webb
3602 * there's no need to call quote_meta,
3603 * alloc(7) is enough -- Acevedo
3604 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003605 compl_pattern = alloc(7);
3606 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003608 STRCPY((char *)compl_pattern, "\\<");
3609 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3610 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612 else
3613 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003614 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3615 compl_length) + 3);
3616 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003618 STRCPY((char *)compl_pattern, "\\<");
3619 (void)quote_meta(compl_pattern + 2, line + compl_col,
3620 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622 }
3623 }
3624 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3625 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003626 compl_col = skipwhite(line) - line;
3627 compl_length = (int)curs_col - (int)compl_col;
3628 if (compl_length < 0) /* cursor in indent: empty pattern */
3629 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003631 compl_pattern = str_foldcase(line + compl_col, compl_length,
3632 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003634 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3635 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 return FAIL;
3637 }
3638 else if (ctrl_x_mode == CTRL_X_FILES)
3639 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003640 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003642 compl_col += ++startcol;
3643 compl_length = (int)curs_col - startcol;
3644 compl_pattern = addstar(line + compl_col, compl_length,
3645 EXPAND_FILES);
3646 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 return FAIL;
3648 }
3649 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3650 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003651 compl_pattern = vim_strnsave(line, curs_col);
3652 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003654 set_cmd_context(&compl_xp, compl_pattern,
3655 (int)STRLEN(compl_pattern), curs_col);
3656 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3657 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003659 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
3660 compl_col = startcol;
3661 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003663 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003664 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00003665#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003666 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00003667 * Call user defined function 'completefunc' with "a:findstart"
3668 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003669 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00003670 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003671 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003672 char_u *funcname;
3673 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003674
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003675 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00003676 * string */
3677 funcname = ctrl_x_mode == CTRL_X_FUNCTION
3678 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3679 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003680 {
3681 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3682 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003683 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003684 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003685
3686 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003687 args[1] = NULL;
3688 pos = curwin->w_cursor;
3689 col = call_func_retnr(funcname, 2, args, FALSE);
3690 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003691
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003692 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003693 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003694 compl_col = col;
3695 if ((colnr_T)compl_col > curs_col)
3696 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003697
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003698 /* Setup variables for completion. Need to obtain "line" again,
3699 * it may have become invalid. */
3700 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003701 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003702 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3703 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003704#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003705 return FAIL;
3706 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00003707 else if (ctrl_x_mode == CTRL_X_SPELL)
3708 {
3709#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00003710 if (spell_bad_len > 0)
3711 compl_col = curs_col - spell_bad_len;
3712 else
3713 compl_col = spell_word_start(startcol);
3714 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00003715 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003716 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003717 compl_length = (int)curs_col - compl_col;
3718 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3719 if (compl_pattern == NULL)
3720#endif
3721 return FAIL;
3722 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003723 else
3724 {
3725 EMSG2(_(e_intern2), "ins_complete()");
3726 return FAIL;
3727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003729 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
3731 edit_submode_pre = (char_u *)_(" Adding");
3732 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3733 {
3734 /* Insert a new line, keep indentation but ignore 'comments' */
3735#ifdef FEAT_COMMENTS
3736 char_u *old = curbuf->b_p_com;
3737
3738 curbuf->b_p_com = (char_u *)"";
3739#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003740 compl_startpos.lnum = curwin->w_cursor.lnum;
3741 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 ins_eol('\r');
3743#ifdef FEAT_COMMENTS
3744 curbuf->b_p_com = old;
3745#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003746 compl_length = 0;
3747 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
3749 }
3750 else
3751 {
3752 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003753 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
3755
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003756 if (compl_cont_status & CONT_LOCAL)
3757 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 else
3759 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
3760
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003762 * "compl_orig_text" itself (not a copy) is added, it will be freed
3763 * when the list of matches is freed. */
3764 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
3765 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
3766 -1, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003768 vim_free(compl_pattern);
3769 compl_pattern = NULL;
3770 vim_free(compl_orig_text);
3771 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 return FAIL;
3773 }
3774
3775 /* showmode might reset the internal line pointers, so it must
3776 * be called before line = ml_get(), or when this address is no
3777 * longer needed. -- Acevedo.
3778 */
3779 edit_submode_extra = (char_u *)_("-- Searching...");
3780 edit_submode_highl = HLF_COUNT;
3781 showmode();
3782 edit_submode_extra = NULL;
3783 out_flush();
3784 }
3785
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003786 compl_shown_match = compl_curr_match;
3787 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788
3789 /*
3790 * Find next match.
3791 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003792 n = ins_compl_next(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003794 /* may undisplay the popup menu */
3795 ins_compl_upd_pum();
3796
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003797 if (n > 1) /* all matches have been found */
3798 compl_matches = n;
3799 compl_curr_match = compl_shown_match;
3800 compl_direction = compl_shows_dir;
3801 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802
3803 /* eat the ESC to avoid leaving insert mode */
3804 if (got_int && !global_busy)
3805 {
3806 (void)vgetc();
3807 got_int = FALSE;
3808 }
3809
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003810 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003811 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003813 edit_submode_extra = (compl_cont_status & CONT_ADDING)
3814 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
3816 edit_submode_highl = HLF_E;
3817 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
3818 * because we couldn't expand anything at first place, but if we used
3819 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
3820 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003821 if ( compl_length > 1
3822 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 || (ctrl_x_mode != 0
3824 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
3825 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003826 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828
Bram Moolenaar572cb562005-08-05 21:35:02 +00003829 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003830 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003832 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
3834 if (edit_submode_extra == NULL)
3835 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003836 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 {
3838 edit_submode_extra = (char_u *)_("Back at original");
3839 edit_submode_highl = HLF_W;
3840 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003841 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 {
3843 edit_submode_extra = (char_u *)_("Word from other line");
3844 edit_submode_highl = HLF_COUNT;
3845 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00003846 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 {
3848 edit_submode_extra = (char_u *)_("The only match");
3849 edit_submode_highl = HLF_COUNT;
3850 }
3851 else
3852 {
3853 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003854 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003856 int number = 0;
3857 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003859 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
3861 /* search backwards for the first valid (!= -1) number.
3862 * This should normally succeed already at the first loop
3863 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003864 for (match = compl_curr_match->cp_prev; match != NULL
3865 && match != compl_first_match;
3866 match = match->cp_prev)
3867 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003869 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 break;
3871 }
3872 if (match != NULL)
3873 /* go up and assign all numbers which are not assigned
3874 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003875 for (match = match->cp_next;
3876 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003877 match = match->cp_next)
3878 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 }
3880 else /* BACKWARD */
3881 {
3882 /* search forwards (upwards) for the first valid (!= -1)
3883 * number. This should normally succeed already at the
3884 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003885 for (match = compl_curr_match->cp_next; match != NULL
3886 && match != compl_first_match;
3887 match = match->cp_next)
3888 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003890 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 break;
3892 }
3893 if (match != NULL)
3894 /* go down and assign all numbers which are not
3895 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003896 for (match = match->cp_prev; match
3897 && match->cp_number == -1;
3898 match = match->cp_prev)
3899 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
3901 }
3902
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003903 /* The match should always have a sequence number now, this is
3904 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003905 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 {
3907 /* Space for 10 text chars. + 2x10-digit no.s */
3908 static char_u match_ref[31];
3909
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003910 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00003912 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003914 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00003915 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003916 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 edit_submode_extra = match_ref;
3918 edit_submode_highl = HLF_R;
3919 if (dollar_vcol)
3920 curs_columns(FALSE);
3921 }
3922 }
3923 }
3924
3925 /* Show a message about what (completion) mode we're in. */
3926 showmode();
3927 if (edit_submode_extra != NULL)
3928 {
3929 if (!p_smd)
3930 msg_attr(edit_submode_extra,
3931 edit_submode_highl < HLF_COUNT
3932 ? hl_attr(edit_submode_highl) : 0);
3933 }
3934 else
3935 msg_clr_cmdline(); /* necessary for "noshowmode" */
3936
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003937 ins_compl_show_pum();
3938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 return OK;
3940}
3941
3942/*
3943 * Looks in the first "len" chars. of "src" for search-metachars.
3944 * If dest is not NULL the chars. are copied there quoting (with
3945 * a backslash) the metachars, and dest would be NUL terminated.
3946 * Returns the length (needed) of dest
3947 */
3948 static int
3949quote_meta(dest, src, len)
3950 char_u *dest;
3951 char_u *src;
3952 int len;
3953{
3954 int m;
3955
3956 for (m = len; --len >= 0; src++)
3957 {
3958 switch (*src)
3959 {
3960 case '.':
3961 case '*':
3962 case '[':
3963 if (ctrl_x_mode == CTRL_X_DICTIONARY
3964 || ctrl_x_mode == CTRL_X_THESAURUS)
3965 break;
3966 case '~':
3967 if (!p_magic) /* quote these only if magic is set */
3968 break;
3969 case '\\':
3970 if (ctrl_x_mode == CTRL_X_DICTIONARY
3971 || ctrl_x_mode == CTRL_X_THESAURUS)
3972 break;
3973 case '^': /* currently it's not needed. */
3974 case '$':
3975 m++;
3976 if (dest != NULL)
3977 *dest++ = '\\';
3978 break;
3979 }
3980 if (dest != NULL)
3981 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003982# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 /* Copy remaining bytes of a multibyte character. */
3984 if (has_mbyte)
3985 {
3986 int i, mb_len;
3987
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003988 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 if (mb_len > 0 && len >= mb_len)
3990 for (i = 0; i < mb_len; ++i)
3991 {
3992 --len;
3993 ++src;
3994 if (dest != NULL)
3995 *dest++ = *src;
3996 }
3997 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00003998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000 if (dest != NULL)
4001 *dest = NUL;
4002
4003 return m;
4004}
4005#endif /* FEAT_INS_EXPAND */
4006
4007/*
4008 * Next character is interpreted literally.
4009 * A one, two or three digit decimal number is interpreted as its byte value.
4010 * If one or two digits are entered, the next character is given to vungetc().
4011 * For Unicode a character > 255 may be returned.
4012 */
4013 int
4014get_literal()
4015{
4016 int cc;
4017 int nc;
4018 int i;
4019 int hex = FALSE;
4020 int octal = FALSE;
4021#ifdef FEAT_MBYTE
4022 int unicode = 0;
4023#endif
4024
4025 if (got_int)
4026 return Ctrl_C;
4027
4028#ifdef FEAT_GUI
4029 /*
4030 * In GUI there is no point inserting the internal code for a special key.
4031 * It is more useful to insert the string "<KEY>" instead. This would
4032 * probably be useful in a text window too, but it would not be
4033 * vi-compatible (maybe there should be an option for it?) -- webb
4034 */
4035 if (gui.in_use)
4036 ++allow_keys;
4037#endif
4038#ifdef USE_ON_FLY_SCROLL
4039 dont_scroll = TRUE; /* disallow scrolling here */
4040#endif
4041 ++no_mapping; /* don't map the next key hits */
4042 cc = 0;
4043 i = 0;
4044 for (;;)
4045 {
4046 do
4047 nc = safe_vgetc();
4048 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4049 || nc == K_HOR_SCROLLBAR);
4050#ifdef FEAT_CMDL_INFO
4051 if (!(State & CMDLINE)
4052# ifdef FEAT_MBYTE
4053 && MB_BYTE2LEN_CHECK(nc) == 1
4054# endif
4055 )
4056 add_to_showcmd(nc);
4057#endif
4058 if (nc == 'x' || nc == 'X')
4059 hex = TRUE;
4060 else if (nc == 'o' || nc == 'O')
4061 octal = TRUE;
4062#ifdef FEAT_MBYTE
4063 else if (nc == 'u' || nc == 'U')
4064 unicode = nc;
4065#endif
4066 else
4067 {
4068 if (hex
4069#ifdef FEAT_MBYTE
4070 || unicode != 0
4071#endif
4072 )
4073 {
4074 if (!vim_isxdigit(nc))
4075 break;
4076 cc = cc * 16 + hex2nr(nc);
4077 }
4078 else if (octal)
4079 {
4080 if (nc < '0' || nc > '7')
4081 break;
4082 cc = cc * 8 + nc - '0';
4083 }
4084 else
4085 {
4086 if (!VIM_ISDIGIT(nc))
4087 break;
4088 cc = cc * 10 + nc - '0';
4089 }
4090
4091 ++i;
4092 }
4093
4094 if (cc > 255
4095#ifdef FEAT_MBYTE
4096 && unicode == 0
4097#endif
4098 )
4099 cc = 255; /* limit range to 0-255 */
4100 nc = 0;
4101
4102 if (hex) /* hex: up to two chars */
4103 {
4104 if (i >= 2)
4105 break;
4106 }
4107#ifdef FEAT_MBYTE
4108 else if (unicode) /* Unicode: up to four or eight chars */
4109 {
4110 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4111 break;
4112 }
4113#endif
4114 else if (i >= 3) /* decimal or octal: up to three chars */
4115 break;
4116 }
4117 if (i == 0) /* no number entered */
4118 {
4119 if (nc == K_ZERO) /* NUL is stored as NL */
4120 {
4121 cc = '\n';
4122 nc = 0;
4123 }
4124 else
4125 {
4126 cc = nc;
4127 nc = 0;
4128 }
4129 }
4130
4131 if (cc == 0) /* NUL is stored as NL */
4132 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004133#ifdef FEAT_MBYTE
4134 if (enc_dbcs && (cc & 0xff) == 0)
4135 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4136 second byte will cause trouble! */
4137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
4139 --no_mapping;
4140#ifdef FEAT_GUI
4141 if (gui.in_use)
4142 --allow_keys;
4143#endif
4144 if (nc)
4145 vungetc(nc);
4146 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4147 return cc;
4148}
4149
4150/*
4151 * Insert character, taking care of special keys and mod_mask
4152 */
4153 static void
4154insert_special(c, allow_modmask, ctrlv)
4155 int c;
4156 int allow_modmask;
4157 int ctrlv; /* c was typed after CTRL-V */
4158{
4159 char_u *p;
4160 int len;
4161
4162 /*
4163 * Special function key, translate into "<Key>". Up to the last '>' is
4164 * inserted with ins_str(), so as not to replace characters in replace
4165 * mode.
4166 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4167 * unless 'allow_modmask' is TRUE.
4168 */
4169#ifdef MACOS
4170 /* Command-key never produces a normal key */
4171 if (mod_mask & MOD_MASK_CMD)
4172 allow_modmask = TRUE;
4173#endif
4174 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4175 {
4176 p = get_special_key_name(c, mod_mask);
4177 len = (int)STRLEN(p);
4178 c = p[len - 1];
4179 if (len > 2)
4180 {
4181 if (stop_arrow() == FAIL)
4182 return;
4183 p[len - 1] = NUL;
4184 ins_str(p);
4185 AppendToRedobuffLit(p);
4186 ctrlv = FALSE;
4187 }
4188 }
4189 if (stop_arrow() == OK)
4190 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4191}
4192
4193/*
4194 * Special characters in this context are those that need processing other
4195 * than the simple insertion that can be performed here. This includes ESC
4196 * which terminates the insert, and CR/NL which need special processing to
4197 * open up a new line. This routine tries to optimize insertions performed by
4198 * the "redo", "undo" or "put" commands, so it needs to know when it should
4199 * stop and defer processing to the "normal" mechanism.
4200 * '0' and '^' are special, because they can be followed by CTRL-D.
4201 */
4202#ifdef EBCDIC
4203# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4204#else
4205# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4206#endif
4207
4208#ifdef FEAT_MBYTE
4209# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4210#else
4211# define WHITECHAR(cc) vim_iswhite(cc)
4212#endif
4213
4214 void
4215insertchar(c, flags, second_indent)
4216 int c; /* character to insert or NUL */
4217 int flags; /* INSCHAR_FORMAT, etc. */
4218 int second_indent; /* indent for second line if >= 0 */
4219{
4220 int haveto_redraw = FALSE;
4221 int textwidth;
4222#ifdef FEAT_COMMENTS
4223 colnr_T leader_len;
4224 char_u *p;
4225 int no_leader = FALSE;
4226 int do_comments = (flags & INSCHAR_DO_COM);
4227#endif
4228 int fo_white_par;
4229 int first_line = TRUE;
4230 int fo_ins_blank;
4231#ifdef FEAT_MBYTE
4232 int fo_multibyte;
4233#endif
4234 int save_char = NUL;
4235 int cc;
4236
4237 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4238 fo_ins_blank = has_format_option(FO_INS_BLANK);
4239#ifdef FEAT_MBYTE
4240 fo_multibyte = has_format_option(FO_MBYTE_BREAK);
4241#endif
4242 fo_white_par = has_format_option(FO_WHITE_PAR);
4243
4244 /*
4245 * Try to break the line in two or more pieces when:
4246 * - Always do this if we have been called to do formatting only.
4247 * - Always do this when 'formatoptions' has the 'a' flag and the line
4248 * ends in white space.
4249 * - Otherwise:
4250 * - Don't do this if inserting a blank
4251 * - Don't do this if an existing character is being replaced, unless
4252 * we're in VREPLACE mode.
4253 * - Do this if the cursor is not on the line where insert started
4254 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4255 * before the insert.
4256 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4257 * before 'textwidth'
4258 */
4259 if (textwidth
4260 && ((flags & INSCHAR_FORMAT)
4261 || (!vim_iswhite(c)
4262 && !((State & REPLACE_FLAG)
4263#ifdef FEAT_VREPLACE
4264 && !(State & VREPLACE_FLAG)
4265#endif
4266 && *ml_get_cursor() != NUL)
4267 && (curwin->w_cursor.lnum != Insstart.lnum
4268 || ((!has_format_option(FO_INS_LONG)
4269 || Insstart_textlen <= (colnr_T)textwidth)
4270 && (!fo_ins_blank
4271 || Insstart_blank_vcol <= (colnr_T)textwidth
4272 ))))))
4273 {
4274 /*
4275 * When 'ai' is off we don't want a space under the cursor to be
4276 * deleted. Replace it with an 'x' temporarily.
4277 */
4278 if (!curbuf->b_p_ai)
4279 {
4280 cc = gchar_cursor();
4281 if (vim_iswhite(cc))
4282 {
4283 save_char = cc;
4284 pchar_cursor('x');
4285 }
4286 }
4287
4288 /*
4289 * Repeat breaking lines, until the current line is not too long.
4290 */
4291 while (!got_int)
4292 {
4293 int startcol; /* Cursor column at entry */
4294 int wantcol; /* column at textwidth border */
4295 int foundcol; /* column for start of spaces */
4296 int end_foundcol = 0; /* column for start of word */
4297 colnr_T len;
4298 colnr_T virtcol;
4299#ifdef FEAT_VREPLACE
4300 int orig_col = 0;
4301 char_u *saved_text = NULL;
4302#endif
4303 colnr_T col;
4304
4305 virtcol = get_nolist_virtcol();
4306 if (virtcol < (colnr_T)textwidth)
4307 break;
4308
4309#ifdef FEAT_COMMENTS
4310 if (no_leader)
4311 do_comments = FALSE;
4312 else if (!(flags & INSCHAR_FORMAT)
4313 && has_format_option(FO_WRAP_COMS))
4314 do_comments = TRUE;
4315
4316 /* Don't break until after the comment leader */
4317 if (do_comments)
4318 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
4319 else
4320 leader_len = 0;
4321
4322 /* If the line doesn't start with a comment leader, then don't
4323 * start one in a following broken line. Avoids that a %word
4324 * moved to the start of the next line causes all following lines
4325 * to start with %. */
4326 if (leader_len == 0)
4327 no_leader = TRUE;
4328#endif
4329 if (!(flags & INSCHAR_FORMAT)
4330#ifdef FEAT_COMMENTS
4331 && leader_len == 0
4332#endif
4333 && !has_format_option(FO_WRAP))
4334
4335 {
4336 textwidth = 0;
4337 break;
4338 }
4339 if ((startcol = curwin->w_cursor.col) == 0)
4340 break;
4341
4342 /* find column of textwidth border */
4343 coladvance((colnr_T)textwidth);
4344 wantcol = curwin->w_cursor.col;
4345
4346 curwin->w_cursor.col = startcol - 1;
4347#ifdef FEAT_MBYTE
4348 /* Correct cursor for multi-byte character. */
4349 if (has_mbyte)
4350 mb_adjust_cursor();
4351#endif
4352 foundcol = 0;
4353
4354 /*
4355 * Find position to break at.
4356 * Stop at first entered white when 'formatoptions' has 'v'
4357 */
4358 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
4359 || curwin->w_cursor.lnum != Insstart.lnum
4360 || curwin->w_cursor.col >= Insstart.col)
4361 {
4362 cc = gchar_cursor();
4363 if (WHITECHAR(cc))
4364 {
4365 /* remember position of blank just before text */
4366 end_foundcol = curwin->w_cursor.col;
4367
4368 /* find start of sequence of blanks */
4369 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
4370 {
4371 dec_cursor();
4372 cc = gchar_cursor();
4373 }
4374 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
4375 break; /* only spaces in front of text */
4376#ifdef FEAT_COMMENTS
4377 /* Don't break until after the comment leader */
4378 if (curwin->w_cursor.col < leader_len)
4379 break;
4380#endif
4381 if (has_format_option(FO_ONE_LETTER))
4382 {
4383 /* do not break after one-letter words */
4384 if (curwin->w_cursor.col == 0)
4385 break; /* one-letter word at begin */
4386
4387 col = curwin->w_cursor.col;
4388 dec_cursor();
4389 cc = gchar_cursor();
4390
4391 if (WHITECHAR(cc))
4392 continue; /* one-letter, continue */
4393 curwin->w_cursor.col = col;
4394 }
4395#ifdef FEAT_MBYTE
4396 if (has_mbyte)
4397 foundcol = curwin->w_cursor.col
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004398 + (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 else
4400#endif
4401 foundcol = curwin->w_cursor.col + 1;
4402 if (curwin->w_cursor.col < (colnr_T)wantcol)
4403 break;
4404 }
4405#ifdef FEAT_MBYTE
4406 else if (cc >= 0x100 && fo_multibyte
4407 && curwin->w_cursor.col <= (colnr_T)wantcol)
4408 {
4409 /* Break after or before a multi-byte character. */
4410 foundcol = curwin->w_cursor.col;
4411 if (curwin->w_cursor.col < (colnr_T)wantcol)
4412 foundcol += (*mb_char2len)(cc);
4413 end_foundcol = foundcol;
4414 break;
4415 }
4416#endif
4417 if (curwin->w_cursor.col == 0)
4418 break;
4419 dec_cursor();
4420 }
4421
4422 if (foundcol == 0) /* no spaces, cannot break line */
4423 {
4424 curwin->w_cursor.col = startcol;
4425 break;
4426 }
4427
4428 /* Going to break the line, remove any "$" now. */
4429 undisplay_dollar();
4430
4431 /*
4432 * Offset between cursor position and line break is used by replace
4433 * stack functions. VREPLACE does not use this, and backspaces
4434 * over the text instead.
4435 */
4436#ifdef FEAT_VREPLACE
4437 if (State & VREPLACE_FLAG)
4438 orig_col = startcol; /* Will start backspacing from here */
4439 else
4440#endif
4441 replace_offset = startcol - end_foundcol - 1;
4442
4443 /*
4444 * adjust startcol for spaces that will be deleted and
4445 * characters that will remain on top line
4446 */
4447 curwin->w_cursor.col = foundcol;
4448 while (cc = gchar_cursor(), WHITECHAR(cc))
4449 inc_cursor();
4450 startcol -= curwin->w_cursor.col;
4451 if (startcol < 0)
4452 startcol = 0;
4453
4454#ifdef FEAT_VREPLACE
4455 if (State & VREPLACE_FLAG)
4456 {
4457 /*
4458 * In VREPLACE mode, we will backspace over the text to be
4459 * wrapped, so save a copy now to put on the next line.
4460 */
4461 saved_text = vim_strsave(ml_get_cursor());
4462 curwin->w_cursor.col = orig_col;
4463 if (saved_text == NULL)
4464 break; /* Can't do it, out of memory */
4465 saved_text[startcol] = NUL;
4466
4467 /* Backspace over characters that will move to the next line */
4468 if (!fo_white_par)
4469 backspace_until_column(foundcol);
4470 }
4471 else
4472#endif
4473 {
4474 /* put cursor after pos. to break line */
4475 if (!fo_white_par)
4476 curwin->w_cursor.col = foundcol;
4477 }
4478
4479 /*
4480 * Split the line just before the margin.
4481 * Only insert/delete lines, but don't really redraw the window.
4482 */
4483 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
4484 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
4485#ifdef FEAT_COMMENTS
4486 + (do_comments ? OPENLINE_DO_COM : 0)
4487#endif
4488 , old_indent);
4489 old_indent = 0;
4490
4491 replace_offset = 0;
4492 if (first_line)
4493 {
4494 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
4495 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
4496 if (second_indent >= 0)
4497 {
4498#ifdef FEAT_VREPLACE
4499 if (State & VREPLACE_FLAG)
4500 change_indent(INDENT_SET, second_indent, FALSE, NUL);
4501 else
4502#endif
4503 (void)set_indent(second_indent, SIN_CHANGED);
4504 }
4505 first_line = FALSE;
4506 }
4507
4508#ifdef FEAT_VREPLACE
4509 if (State & VREPLACE_FLAG)
4510 {
4511 /*
4512 * In VREPLACE mode we have backspaced over the text to be
4513 * moved, now we re-insert it into the new line.
4514 */
4515 ins_bytes(saved_text);
4516 vim_free(saved_text);
4517 }
4518 else
4519#endif
4520 {
4521 /*
4522 * Check if cursor is not past the NUL off the line, cindent
4523 * may have added or removed indent.
4524 */
4525 curwin->w_cursor.col += startcol;
4526 len = (colnr_T)STRLEN(ml_get_curline());
4527 if (curwin->w_cursor.col > len)
4528 curwin->w_cursor.col = len;
4529 }
4530
4531 haveto_redraw = TRUE;
4532#ifdef FEAT_CINDENT
4533 can_cindent = TRUE;
4534#endif
4535 /* moved the cursor, don't autoindent or cindent now */
4536 did_ai = FALSE;
4537#ifdef FEAT_SMARTINDENT
4538 did_si = FALSE;
4539 can_si = FALSE;
4540 can_si_back = FALSE;
4541#endif
4542 line_breakcheck();
4543 }
4544
4545 if (save_char) /* put back space after cursor */
4546 pchar_cursor(save_char);
4547
4548 if (c == NUL) /* formatting only */
4549 return;
4550 if (haveto_redraw)
4551 {
4552 update_topline();
4553 redraw_curbuf_later(VALID);
4554 }
4555 }
4556 if (c == NUL) /* only formatting was wanted */
4557 return;
4558
4559#ifdef FEAT_COMMENTS
4560 /* Check whether this character should end a comment. */
4561 if (did_ai && (int)c == end_comment_pending)
4562 {
4563 char_u *line;
4564 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4565 int middle_len, end_len;
4566 int i;
4567
4568 /*
4569 * Need to remove existing (middle) comment leader and insert end
4570 * comment leader. First, check what comment leader we can find.
4571 */
4572 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4573 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4574 {
4575 /* Skip middle-comment string */
4576 while (*p && p[-1] != ':') /* find end of middle flags */
4577 ++p;
4578 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4579 /* Don't count trailing white space for middle_len */
4580 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4581 --middle_len;
4582
4583 /* Find the end-comment string */
4584 while (*p && p[-1] != ':') /* find end of end flags */
4585 ++p;
4586 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4587
4588 /* Skip white space before the cursor */
4589 i = curwin->w_cursor.col;
4590 while (--i >= 0 && vim_iswhite(line[i]))
4591 ;
4592 i++;
4593
4594 /* Skip to before the middle leader */
4595 i -= middle_len;
4596
4597 /* Check some expected things before we go on */
4598 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4599 {
4600 /* Backspace over all the stuff we want to replace */
4601 backspace_until_column(i);
4602
4603 /*
4604 * Insert the end-comment string, except for the last
4605 * character, which will get inserted as normal later.
4606 */
4607 ins_bytes_len(lead_end, end_len - 1);
4608 }
4609 }
4610 }
4611 end_comment_pending = NUL;
4612#endif
4613
4614 did_ai = FALSE;
4615#ifdef FEAT_SMARTINDENT
4616 did_si = FALSE;
4617 can_si = FALSE;
4618 can_si_back = FALSE;
4619#endif
4620
4621 /*
4622 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4623 * This speeds up normal text input considerably.
4624 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4625 * need to re-indent at a ':', or any other character (but not what
4626 * 'paste' is set)..
4627 */
4628#ifdef USE_ON_FLY_SCROLL
4629 dont_scroll = FALSE; /* allow scrolling here */
4630#endif
4631
4632 if ( !ISSPECIAL(c)
4633#ifdef FEAT_MBYTE
4634 && (!has_mbyte || (*mb_char2len)(c) == 1)
4635#endif
4636 && vpeekc() != NUL
4637 && !(State & REPLACE_FLAG)
4638#ifdef FEAT_CINDENT
4639 && !cindent_on()
4640#endif
4641#ifdef FEAT_RIGHTLEFT
4642 && !p_ri
4643#endif
4644 )
4645 {
4646#define INPUT_BUFLEN 100
4647 char_u buf[INPUT_BUFLEN + 1];
4648 int i;
4649 colnr_T virtcol = 0;
4650
4651 buf[0] = c;
4652 i = 1;
4653 if (textwidth)
4654 virtcol = get_nolist_virtcol();
4655 /*
4656 * Stop the string when:
4657 * - no more chars available
4658 * - finding a special character (command key)
4659 * - buffer is full
4660 * - running into the 'textwidth' boundary
4661 * - need to check for abbreviation: A non-word char after a word-char
4662 */
4663 while ( (c = vpeekc()) != NUL
4664 && !ISSPECIAL(c)
4665#ifdef FEAT_MBYTE
4666 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4667#endif
4668 && i < INPUT_BUFLEN
4669 && (textwidth == 0
4670 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4671 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4672 {
4673#ifdef FEAT_RIGHTLEFT
4674 c = vgetc();
4675 if (p_hkmap && KeyTyped)
4676 c = hkmap(c); /* Hebrew mode mapping */
4677# ifdef FEAT_FKMAP
4678 if (p_fkmap && KeyTyped)
4679 c = fkmap(c); /* Farsi mode mapping */
4680# endif
4681 buf[i++] = c;
4682#else
4683 buf[i++] = vgetc();
4684#endif
4685 }
4686
4687#ifdef FEAT_DIGRAPHS
4688 do_digraph(-1); /* clear digraphs */
4689 do_digraph(buf[i-1]); /* may be the start of a digraph */
4690#endif
4691 buf[i] = NUL;
4692 ins_str(buf);
4693 if (flags & INSCHAR_CTRLV)
4694 {
4695 redo_literal(*buf);
4696 i = 1;
4697 }
4698 else
4699 i = 0;
4700 if (buf[i] != NUL)
4701 AppendToRedobuffLit(buf + i);
4702 }
4703 else
4704 {
4705#ifdef FEAT_MBYTE
4706 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
4707 {
4708 char_u buf[MB_MAXBYTES + 1];
4709
4710 (*mb_char2bytes)(c, buf);
4711 buf[cc] = NUL;
4712 ins_char_bytes(buf, cc);
4713 AppendCharToRedobuff(c);
4714 }
4715 else
4716#endif
4717 {
4718 ins_char(c);
4719 if (flags & INSCHAR_CTRLV)
4720 redo_literal(c);
4721 else
4722 AppendCharToRedobuff(c);
4723 }
4724 }
4725}
4726
4727/*
4728 * Called after inserting or deleting text: When 'formatoptions' includes the
4729 * 'a' flag format from the current line until the end of the paragraph.
4730 * Keep the cursor at the same position relative to the text.
4731 * The caller must have saved the cursor line for undo, following ones will be
4732 * saved here.
4733 */
4734 void
4735auto_format(trailblank, prev_line)
4736 int trailblank; /* when TRUE also format with trailing blank */
4737 int prev_line; /* may start in previous line */
4738{
4739 pos_T pos;
4740 colnr_T len;
4741 char_u *old;
4742 char_u *new, *pnew;
4743 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004744 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
4746 if (!has_format_option(FO_AUTO))
4747 return;
4748
4749 pos = curwin->w_cursor;
4750 old = ml_get_curline();
4751
4752 /* may remove added space */
4753 check_auto_format(FALSE);
4754
4755 /* Don't format in Insert mode when the cursor is on a trailing blank, the
4756 * user might insert normal text next. Also skip formatting when "1" is
4757 * in 'formatoptions' and there is a single character before the cursor.
4758 * Otherwise the line would be broken and when typing another non-white
4759 * next they are not joined back together. */
4760 wasatend = (pos.col == STRLEN(old));
4761 if (*old != NUL && !trailblank && wasatend)
4762 {
4763 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004764 cc = gchar_cursor();
4765 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
4766 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004768 cc = gchar_cursor();
4769 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
4771 curwin->w_cursor = pos;
4772 return;
4773 }
4774 curwin->w_cursor = pos;
4775 }
4776
4777#ifdef FEAT_COMMENTS
4778 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
4779 * comments. */
4780 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
4781 && get_leader_len(old, NULL, FALSE) == 0)
4782 return;
4783#endif
4784
4785 /*
4786 * May start formatting in a previous line, so that after "x" a word is
4787 * moved to the previous line if it fits there now. Only when this is not
4788 * the start of a paragraph.
4789 */
4790 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
4791 {
4792 --curwin->w_cursor.lnum;
4793 if (u_save_cursor() == FAIL)
4794 return;
4795 }
4796
4797 /*
4798 * Do the formatting and restore the cursor position. "saved_cursor" will
4799 * be adjusted for the text formatting.
4800 */
4801 saved_cursor = pos;
4802 format_lines((linenr_T)-1);
4803 curwin->w_cursor = saved_cursor;
4804 saved_cursor.lnum = 0;
4805
4806 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4807 {
4808 /* "cannot happen" */
4809 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4810 coladvance((colnr_T)MAXCOL);
4811 }
4812 else
4813 check_cursor_col();
4814
4815 /* Insert mode: If the cursor is now after the end of the line while it
4816 * previously wasn't, the line was broken. Because of the rule above we
4817 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
4818 * formatted. */
4819 if (!wasatend && has_format_option(FO_WHITE_PAR))
4820 {
4821 new = ml_get_curline();
4822 len = STRLEN(new);
4823 if (curwin->w_cursor.col == len)
4824 {
4825 pnew = vim_strnsave(new, len + 2);
4826 pnew[len] = ' ';
4827 pnew[len + 1] = NUL;
4828 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
4829 /* remove the space later */
4830 did_add_space = TRUE;
4831 }
4832 else
4833 /* may remove added space */
4834 check_auto_format(FALSE);
4835 }
4836
4837 check_cursor();
4838}
4839
4840/*
4841 * When an extra space was added to continue a paragraph for auto-formatting,
4842 * delete it now. The space must be under the cursor, just after the insert
4843 * position.
4844 */
4845 static void
4846check_auto_format(end_insert)
4847 int end_insert; /* TRUE when ending Insert mode */
4848{
4849 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004850 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851
4852 if (did_add_space)
4853 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004854 cc = gchar_cursor();
4855 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 /* Somehow the space was removed already. */
4857 did_add_space = FALSE;
4858 else
4859 {
4860 if (!end_insert)
4861 {
4862 inc_cursor();
4863 c = gchar_cursor();
4864 dec_cursor();
4865 }
4866 if (c != NUL)
4867 {
4868 /* The space is no longer at the end of the line, delete it. */
4869 del_char(FALSE);
4870 did_add_space = FALSE;
4871 }
4872 }
4873 }
4874}
4875
4876/*
4877 * Find out textwidth to be used for formatting:
4878 * if 'textwidth' option is set, use it
4879 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
4880 * if invalid value, use 0.
4881 * Set default to window width (maximum 79) for "gq" operator.
4882 */
4883 int
4884comp_textwidth(ff)
4885 int ff; /* force formatting (for "Q" command) */
4886{
4887 int textwidth;
4888
4889 textwidth = curbuf->b_p_tw;
4890 if (textwidth == 0 && curbuf->b_p_wm)
4891 {
4892 /* The width is the window width minus 'wrapmargin' minus all the
4893 * things that add to the margin. */
4894 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
4895#ifdef FEAT_CMDWIN
4896 if (cmdwin_type != 0)
4897 textwidth -= 1;
4898#endif
4899#ifdef FEAT_FOLDING
4900 textwidth -= curwin->w_p_fdc;
4901#endif
4902#ifdef FEAT_SIGNS
4903 if (curwin->w_buffer->b_signlist != NULL
4904# ifdef FEAT_NETBEANS_INTG
4905 || usingNetbeans
4906# endif
4907 )
4908 textwidth -= 1;
4909#endif
4910 if (curwin->w_p_nu)
4911 textwidth -= 8;
4912 }
4913 if (textwidth < 0)
4914 textwidth = 0;
4915 if (ff && textwidth == 0)
4916 {
4917 textwidth = W_WIDTH(curwin) - 1;
4918 if (textwidth > 79)
4919 textwidth = 79;
4920 }
4921 return textwidth;
4922}
4923
4924/*
4925 * Put a character in the redo buffer, for when just after a CTRL-V.
4926 */
4927 static void
4928redo_literal(c)
4929 int c;
4930{
4931 char_u buf[10];
4932
4933 /* Only digits need special treatment. Translate them into a string of
4934 * three digits. */
4935 if (VIM_ISDIGIT(c))
4936 {
4937 sprintf((char *)buf, "%03d", c);
4938 AppendToRedobuff(buf);
4939 }
4940 else
4941 AppendCharToRedobuff(c);
4942}
4943
4944/*
4945 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00004946 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 */
4948 static void
4949start_arrow(end_insert_pos)
4950 pos_T *end_insert_pos;
4951{
4952 if (!arrow_used) /* something has been inserted */
4953 {
4954 AppendToRedobuff(ESC_STR);
4955 stop_insert(end_insert_pos, FALSE);
4956 arrow_used = TRUE; /* this means we stopped the current insert */
4957 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004958#ifdef FEAT_SYN_HL
4959 check_spell_redraw();
4960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961}
4962
Bram Moolenaar217ad922005-03-20 22:37:15 +00004963#ifdef FEAT_SYN_HL
4964/*
4965 * If we skipped highlighting word at cursor, do it now.
4966 * It may be skipped again, thus reset spell_redraw_lnum first.
4967 */
4968 static void
4969check_spell_redraw()
4970{
4971 if (spell_redraw_lnum != 0)
4972 {
4973 linenr_T lnum = spell_redraw_lnum;
4974
4975 spell_redraw_lnum = 0;
4976 redrawWinline(lnum, FALSE);
4977 }
4978}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00004979
4980/*
4981 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
4982 * spelled word, if there is one.
4983 */
4984 static void
4985spell_back_to_badword()
4986{
4987 pos_T tpos = curwin->w_cursor;
4988
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00004989 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00004990 if (curwin->w_cursor.col != tpos.col)
4991 start_arrow(&tpos);
4992}
Bram Moolenaar217ad922005-03-20 22:37:15 +00004993#endif
4994
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995/*
4996 * stop_arrow() is called before a change is made in insert mode.
4997 * If an arrow key has been used, start a new insertion.
4998 * Returns FAIL if undo is impossible, shouldn't insert then.
4999 */
5000 int
5001stop_arrow()
5002{
5003 if (arrow_used)
5004 {
5005 if (u_save_cursor() == OK)
5006 {
5007 arrow_used = FALSE;
5008 ins_need_undo = FALSE;
5009 }
5010 Insstart = curwin->w_cursor; /* new insertion starts here */
5011 Insstart_textlen = linetabsize(ml_get_curline());
5012 ai_col = 0;
5013#ifdef FEAT_VREPLACE
5014 if (State & VREPLACE_FLAG)
5015 {
5016 orig_line_count = curbuf->b_ml.ml_line_count;
5017 vr_lines_changed = 1;
5018 }
5019#endif
5020 ResetRedobuff();
5021 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
5022 }
5023 else if (ins_need_undo)
5024 {
5025 if (u_save_cursor() == OK)
5026 ins_need_undo = FALSE;
5027 }
5028
5029#ifdef FEAT_FOLDING
5030 /* Always open fold at the cursor line when inserting something. */
5031 foldOpenCursor();
5032#endif
5033
5034 return (arrow_used || ins_need_undo ? FAIL : OK);
5035}
5036
5037/*
5038 * do a few things to stop inserting
5039 */
5040 static void
5041stop_insert(end_insert_pos, esc)
5042 pos_T *end_insert_pos; /* where insert ended */
5043 int esc; /* called by ins_esc() */
5044{
5045 int cc;
5046
5047 stop_redo_ins();
5048 replace_flush(); /* abandon replace stack */
5049
5050 /*
5051 * save the inserted text for later redo with ^@
5052 */
5053 vim_free(last_insert);
5054 last_insert = get_inserted();
5055 last_insert_skip = new_insert_skip;
5056
5057 if (!arrow_used)
5058 {
5059 /* Auto-format now. It may seem strange to do this when stopping an
5060 * insertion (or moving the cursor), but it's required when appending
5061 * a line and having it end in a space. But only do it when something
5062 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005063 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005065 pos_T tpos = curwin->w_cursor;
5066
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 /* When the cursor is at the end of the line after a space the
5068 * formatting will move it to the following word. Avoid that by
5069 * moving the cursor onto the space. */
5070 cc = 'x';
5071 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5072 {
5073 dec_cursor();
5074 cc = gchar_cursor();
5075 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005076 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 }
5078
5079 auto_format(TRUE, FALSE);
5080
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005081 if (vim_iswhite(cc))
5082 {
5083 if (gchar_cursor() != NUL)
5084 inc_cursor();
5085#ifdef FEAT_VIRTUALEDIT
5086 /* If the cursor is still at the same character, also keep
5087 * the "coladd". */
5088 if (gchar_cursor() == NUL
5089 && curwin->w_cursor.lnum == tpos.lnum
5090 && curwin->w_cursor.col == tpos.col)
5091 curwin->w_cursor.coladd = tpos.coladd;
5092#endif
5093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095
5096 /* If a space was inserted for auto-formatting, remove it now. */
5097 check_auto_format(TRUE);
5098
5099 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005100 * of the line, and put the cursor back.
5101 * Do this when ESC was used or moving the cursor up/down. */
5102 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5103 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005105 pos_T tpos = curwin->w_cursor;
5106
5107 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5109 --curwin->w_cursor.col;
5110 while (cc = gchar_cursor(), vim_iswhite(cc))
5111 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005112 if (curwin->w_cursor.lnum != tpos.lnum)
5113 curwin->w_cursor = tpos;
5114 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5116
5117#ifdef FEAT_VISUAL
5118 /* <C-S-Right> may have started Visual mode, adjust the position for
5119 * deleted characters. */
5120 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5121 {
5122 cc = STRLEN(ml_get_curline());
5123 if (VIsual.col > (colnr_T)cc)
5124 {
5125 VIsual.col = cc;
5126# ifdef FEAT_VIRTUALEDIT
5127 VIsual.coladd = 0;
5128# endif
5129 }
5130 }
5131#endif
5132 }
5133 }
5134 did_ai = FALSE;
5135#ifdef FEAT_SMARTINDENT
5136 did_si = FALSE;
5137 can_si = FALSE;
5138 can_si_back = FALSE;
5139#endif
5140
5141 /* set '[ and '] to the inserted text */
5142 curbuf->b_op_start = Insstart;
5143 curbuf->b_op_end = *end_insert_pos;
5144}
5145
5146/*
5147 * Set the last inserted text to a single character.
5148 * Used for the replace command.
5149 */
5150 void
5151set_last_insert(c)
5152 int c;
5153{
5154 char_u *s;
5155
5156 vim_free(last_insert);
5157#ifdef FEAT_MBYTE
5158 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5159#else
5160 last_insert = alloc(6);
5161#endif
5162 if (last_insert != NULL)
5163 {
5164 s = last_insert;
5165 /* Use the CTRL-V only when entering a special char */
5166 if (c < ' ' || c == DEL)
5167 *s++ = Ctrl_V;
5168 s = add_char2buf(c, s);
5169 *s++ = ESC;
5170 *s++ = NUL;
5171 last_insert_skip = 0;
5172 }
5173}
5174
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005175#if defined(EXITFREE) || defined(PROTO)
5176 void
5177free_last_insert()
5178{
5179 vim_free(last_insert);
5180 last_insert = NULL;
5181}
5182#endif
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184/*
5185 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5186 * and CSI. Handle multi-byte characters.
5187 * Returns a pointer to after the added bytes.
5188 */
5189 char_u *
5190add_char2buf(c, s)
5191 int c;
5192 char_u *s;
5193{
5194#ifdef FEAT_MBYTE
5195 char_u temp[MB_MAXBYTES];
5196 int i;
5197 int len;
5198
5199 len = (*mb_char2bytes)(c, temp);
5200 for (i = 0; i < len; ++i)
5201 {
5202 c = temp[i];
5203#endif
5204 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5205 if (c == K_SPECIAL)
5206 {
5207 *s++ = K_SPECIAL;
5208 *s++ = KS_SPECIAL;
5209 *s++ = KE_FILLER;
5210 }
5211#ifdef FEAT_GUI
5212 else if (c == CSI)
5213 {
5214 *s++ = CSI;
5215 *s++ = KS_EXTRA;
5216 *s++ = (int)KE_CSI;
5217 }
5218#endif
5219 else
5220 *s++ = c;
5221#ifdef FEAT_MBYTE
5222 }
5223#endif
5224 return s;
5225}
5226
5227/*
5228 * move cursor to start of line
5229 * if flags & BL_WHITE move to first non-white
5230 * if flags & BL_SOL move to first non-white if startofline is set,
5231 * otherwise keep "curswant" column
5232 * if flags & BL_FIX don't leave the cursor on a NUL.
5233 */
5234 void
5235beginline(flags)
5236 int flags;
5237{
5238 if ((flags & BL_SOL) && !p_sol)
5239 coladvance(curwin->w_curswant);
5240 else
5241 {
5242 curwin->w_cursor.col = 0;
5243#ifdef FEAT_VIRTUALEDIT
5244 curwin->w_cursor.coladd = 0;
5245#endif
5246
5247 if (flags & (BL_WHITE | BL_SOL))
5248 {
5249 char_u *ptr;
5250
5251 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5252 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5253 ++curwin->w_cursor.col;
5254 }
5255 curwin->w_set_curswant = TRUE;
5256 }
5257}
5258
5259/*
5260 * oneright oneleft cursor_down cursor_up
5261 *
5262 * Move one char {right,left,down,up}.
5263 * Doesn't move onto the NUL past the end of the line.
5264 * Return OK when successful, FAIL when we hit a line of file boundary.
5265 */
5266
5267 int
5268oneright()
5269{
5270 char_u *ptr;
5271#ifdef FEAT_MBYTE
5272 int l;
5273#endif
5274
5275#ifdef FEAT_VIRTUALEDIT
5276 if (virtual_active())
5277 {
5278 pos_T prevpos = curwin->w_cursor;
5279
5280 /* Adjust for multi-wide char (excluding TAB) */
5281 ptr = ml_get_cursor();
5282 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5283#ifdef FEAT_MBYTE
5284 (*mb_ptr2char)(ptr)
5285#else
5286 *ptr
5287#endif
5288 ))
5289 ? ptr2cells(ptr) : 1));
5290 curwin->w_set_curswant = TRUE;
5291 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5292 return (prevpos.col != curwin->w_cursor.col
5293 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5294 }
5295#endif
5296
5297 ptr = ml_get_cursor();
5298#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005299 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 {
5301 /* The character under the cursor is a multi-byte character, move
5302 * several bytes right, but don't end up on the NUL. */
5303 if (ptr[l] == NUL)
5304 return FAIL;
5305 curwin->w_cursor.col += l;
5306 }
5307 else
5308#endif
5309 {
5310 if (*ptr++ == NUL || *ptr == NUL)
5311 return FAIL;
5312 ++curwin->w_cursor.col;
5313 }
5314
5315 curwin->w_set_curswant = TRUE;
5316 return OK;
5317}
5318
5319 int
5320oneleft()
5321{
5322#ifdef FEAT_VIRTUALEDIT
5323 if (virtual_active())
5324 {
5325 int width;
5326 int v = getviscol();
5327
5328 if (v == 0)
5329 return FAIL;
5330
5331# ifdef FEAT_LINEBREAK
5332 /* We might get stuck on 'showbreak', skip over it. */
5333 width = 1;
5334 for (;;)
5335 {
5336 coladvance(v - width);
5337 /* getviscol() is slow, skip it when 'showbreak' is empty and
5338 * there are no multi-byte characters */
5339 if ((*p_sbr == NUL
5340# ifdef FEAT_MBYTE
5341 && !has_mbyte
5342# endif
5343 ) || getviscol() < v)
5344 break;
5345 ++width;
5346 }
5347# else
5348 coladvance(v - 1);
5349# endif
5350
5351 if (curwin->w_cursor.coladd == 1)
5352 {
5353 char_u *ptr;
5354
5355 /* Adjust for multi-wide char (not a TAB) */
5356 ptr = ml_get_cursor();
5357 if (*ptr != TAB && vim_isprintc(
5358# ifdef FEAT_MBYTE
5359 (*mb_ptr2char)(ptr)
5360# else
5361 *ptr
5362# endif
5363 ) && ptr2cells(ptr) > 1)
5364 curwin->w_cursor.coladd = 0;
5365 }
5366
5367 curwin->w_set_curswant = TRUE;
5368 return OK;
5369 }
5370#endif
5371
5372 if (curwin->w_cursor.col == 0)
5373 return FAIL;
5374
5375 curwin->w_set_curswant = TRUE;
5376 --curwin->w_cursor.col;
5377
5378#ifdef FEAT_MBYTE
5379 /* if the character on the left of the current cursor is a multi-byte
5380 * character, move to its first byte */
5381 if (has_mbyte)
5382 mb_adjust_cursor();
5383#endif
5384 return OK;
5385}
5386
5387 int
5388cursor_up(n, upd_topline)
5389 long n;
5390 int upd_topline; /* When TRUE: update topline */
5391{
5392 linenr_T lnum;
5393
5394 if (n > 0)
5395 {
5396 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005397 /* This fails if the cursor is already in the first line or the count
5398 * is larger than the line number and '-' is in 'cpoptions' */
5399 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 return FAIL;
5401 if (n >= lnum)
5402 lnum = 1;
5403 else
5404#ifdef FEAT_FOLDING
5405 if (hasAnyFolding(curwin))
5406 {
5407 /*
5408 * Count each sequence of folded lines as one logical line.
5409 */
5410 /* go to the the start of the current fold */
5411 (void)hasFolding(lnum, &lnum, NULL);
5412
5413 while (n--)
5414 {
5415 /* move up one line */
5416 --lnum;
5417 if (lnum <= 1)
5418 break;
5419 /* If we entered a fold, move to the beginning, unless in
5420 * Insert mode or when 'foldopen' contains "all": it will open
5421 * in a moment. */
5422 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
5423 (void)hasFolding(lnum, &lnum, NULL);
5424 }
5425 if (lnum < 1)
5426 lnum = 1;
5427 }
5428 else
5429#endif
5430 lnum -= n;
5431 curwin->w_cursor.lnum = lnum;
5432 }
5433
5434 /* try to advance to the column we want to be at */
5435 coladvance(curwin->w_curswant);
5436
5437 if (upd_topline)
5438 update_topline(); /* make sure curwin->w_topline is valid */
5439
5440 return OK;
5441}
5442
5443/*
5444 * Cursor down a number of logical lines.
5445 */
5446 int
5447cursor_down(n, upd_topline)
5448 long n;
5449 int upd_topline; /* When TRUE: update topline */
5450{
5451 linenr_T lnum;
5452
5453 if (n > 0)
5454 {
5455 lnum = curwin->w_cursor.lnum;
5456#ifdef FEAT_FOLDING
5457 /* Move to last line of fold, will fail if it's the end-of-file. */
5458 (void)hasFolding(lnum, NULL, &lnum);
5459#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00005460 /* This fails if the cursor is already in the last line or would move
5461 * beyound the last line and '-' is in 'cpoptions' */
5462 if (lnum >= curbuf->b_ml.ml_line_count
5463 || (lnum + n > curbuf->b_ml.ml_line_count
5464 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 return FAIL;
5466 if (lnum + n >= curbuf->b_ml.ml_line_count)
5467 lnum = curbuf->b_ml.ml_line_count;
5468 else
5469#ifdef FEAT_FOLDING
5470 if (hasAnyFolding(curwin))
5471 {
5472 linenr_T last;
5473
5474 /* count each sequence of folded lines as one logical line */
5475 while (n--)
5476 {
5477 if (hasFolding(lnum, NULL, &last))
5478 lnum = last + 1;
5479 else
5480 ++lnum;
5481 if (lnum >= curbuf->b_ml.ml_line_count)
5482 break;
5483 }
5484 if (lnum > curbuf->b_ml.ml_line_count)
5485 lnum = curbuf->b_ml.ml_line_count;
5486 }
5487 else
5488#endif
5489 lnum += n;
5490 curwin->w_cursor.lnum = lnum;
5491 }
5492
5493 /* try to advance to the column we want to be at */
5494 coladvance(curwin->w_curswant);
5495
5496 if (upd_topline)
5497 update_topline(); /* make sure curwin->w_topline is valid */
5498
5499 return OK;
5500}
5501
5502/*
5503 * Stuff the last inserted text in the read buffer.
5504 * Last_insert actually is a copy of the redo buffer, so we
5505 * first have to remove the command.
5506 */
5507 int
5508stuff_inserted(c, count, no_esc)
5509 int c; /* Command character to be inserted */
5510 long count; /* Repeat this many times */
5511 int no_esc; /* Don't add an ESC at the end */
5512{
5513 char_u *esc_ptr;
5514 char_u *ptr;
5515 char_u *last_ptr;
5516 char_u last = NUL;
5517
5518 ptr = get_last_insert();
5519 if (ptr == NULL)
5520 {
5521 EMSG(_(e_noinstext));
5522 return FAIL;
5523 }
5524
5525 /* may want to stuff the command character, to start Insert mode */
5526 if (c != NUL)
5527 stuffcharReadbuff(c);
5528 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
5529 *esc_ptr = NUL; /* remove the ESC */
5530
5531 /* when the last char is either "0" or "^" it will be quoted if no ESC
5532 * comes after it OR if it will inserted more than once and "ptr"
5533 * starts with ^D. -- Acevedo
5534 */
5535 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
5536 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
5537 && (no_esc || (*ptr == Ctrl_D && count > 1)))
5538 {
5539 last = *last_ptr;
5540 *last_ptr = NUL;
5541 }
5542
5543 do
5544 {
5545 stuffReadbuff(ptr);
5546 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
5547 if (last)
5548 stuffReadbuff((char_u *)(last == '0'
5549 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
5550 : IF_EB("\026^", CTRL_V_STR "^")));
5551 }
5552 while (--count > 0);
5553
5554 if (last)
5555 *last_ptr = last;
5556
5557 if (esc_ptr != NULL)
5558 *esc_ptr = ESC; /* put the ESC back */
5559
5560 /* may want to stuff a trailing ESC, to get out of Insert mode */
5561 if (!no_esc)
5562 stuffcharReadbuff(ESC);
5563
5564 return OK;
5565}
5566
5567 char_u *
5568get_last_insert()
5569{
5570 if (last_insert == NULL)
5571 return NULL;
5572 return last_insert + last_insert_skip;
5573}
5574
5575/*
5576 * Get last inserted string, and remove trailing <Esc>.
5577 * Returns pointer to allocated memory (must be freed) or NULL.
5578 */
5579 char_u *
5580get_last_insert_save()
5581{
5582 char_u *s;
5583 int len;
5584
5585 if (last_insert == NULL)
5586 return NULL;
5587 s = vim_strsave(last_insert + last_insert_skip);
5588 if (s != NULL)
5589 {
5590 len = (int)STRLEN(s);
5591 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
5592 s[len - 1] = NUL;
5593 }
5594 return s;
5595}
5596
5597/*
5598 * Check the word in front of the cursor for an abbreviation.
5599 * Called when the non-id character "c" has been entered.
5600 * When an abbreviation is recognized it is removed from the text and
5601 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
5602 */
5603 static int
5604echeck_abbr(c)
5605 int c;
5606{
5607 /* Don't check for abbreviation in paste mode, when disabled and just
5608 * after moving around with cursor keys. */
5609 if (p_paste || no_abbr || arrow_used)
5610 return FALSE;
5611
5612 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
5613 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
5614}
5615
5616/*
5617 * replace-stack functions
5618 *
5619 * When replacing characters, the replaced characters are remembered for each
5620 * new character. This is used to re-insert the old text when backspacing.
5621 *
5622 * There is a NUL headed list of characters for each character that is
5623 * currently in the file after the insertion point. When BS is used, one NUL
5624 * headed list is put back for the deleted character.
5625 *
5626 * For a newline, there are two NUL headed lists. One contains the characters
5627 * that the NL replaced. The extra one stores the characters after the cursor
5628 * that were deleted (always white space).
5629 *
5630 * Replace_offset is normally 0, in which case replace_push will add a new
5631 * character at the end of the stack. If replace_offset is not 0, that many
5632 * characters will be left on the stack above the newly inserted character.
5633 */
5634
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00005635static char_u *replace_stack = NULL;
5636static long replace_stack_nr = 0; /* next entry in replace stack */
5637static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638
5639 void
5640replace_push(c)
5641 int c; /* character that is replaced (NUL is none) */
5642{
5643 char_u *p;
5644
5645 if (replace_stack_nr < replace_offset) /* nothing to do */
5646 return;
5647 if (replace_stack_len <= replace_stack_nr)
5648 {
5649 replace_stack_len += 50;
5650 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
5651 if (p == NULL) /* out of memory */
5652 {
5653 replace_stack_len -= 50;
5654 return;
5655 }
5656 if (replace_stack != NULL)
5657 {
5658 mch_memmove(p, replace_stack,
5659 (size_t)(replace_stack_nr * sizeof(char_u)));
5660 vim_free(replace_stack);
5661 }
5662 replace_stack = p;
5663 }
5664 p = replace_stack + replace_stack_nr - replace_offset;
5665 if (replace_offset)
5666 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
5667 *p = c;
5668 ++replace_stack_nr;
5669}
5670
5671/*
5672 * call replace_push(c) with replace_offset set to the first NUL.
5673 */
5674 static void
5675replace_push_off(c)
5676 int c;
5677{
5678 char_u *p;
5679
5680 p = replace_stack + replace_stack_nr;
5681 for (replace_offset = 1; replace_offset < replace_stack_nr;
5682 ++replace_offset)
5683 if (*--p == NUL)
5684 break;
5685 replace_push(c);
5686 replace_offset = 0;
5687}
5688
5689/*
5690 * Pop one item from the replace stack.
5691 * return -1 if stack empty
5692 * return replaced character or NUL otherwise
5693 */
5694 static int
5695replace_pop()
5696{
5697 if (replace_stack_nr == 0)
5698 return -1;
5699 return (int)replace_stack[--replace_stack_nr];
5700}
5701
5702/*
5703 * Join the top two items on the replace stack. This removes to "off"'th NUL
5704 * encountered.
5705 */
5706 static void
5707replace_join(off)
5708 int off; /* offset for which NUL to remove */
5709{
5710 int i;
5711
5712 for (i = replace_stack_nr; --i >= 0; )
5713 if (replace_stack[i] == NUL && off-- <= 0)
5714 {
5715 --replace_stack_nr;
5716 mch_memmove(replace_stack + i, replace_stack + i + 1,
5717 (size_t)(replace_stack_nr - i));
5718 return;
5719 }
5720}
5721
5722/*
5723 * Pop bytes from the replace stack until a NUL is found, and insert them
5724 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
5725 */
5726 static void
5727replace_pop_ins()
5728{
5729 int cc;
5730 int oldState = State;
5731
5732 State = NORMAL; /* don't want REPLACE here */
5733 while ((cc = replace_pop()) > 0)
5734 {
5735#ifdef FEAT_MBYTE
5736 mb_replace_pop_ins(cc);
5737#else
5738 ins_char(cc);
5739#endif
5740 dec_cursor();
5741 }
5742 State = oldState;
5743}
5744
5745#ifdef FEAT_MBYTE
5746/*
5747 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
5748 * indicates a multi-byte char, pop the other bytes too.
5749 */
5750 static void
5751mb_replace_pop_ins(cc)
5752 int cc;
5753{
5754 int n;
5755 char_u buf[MB_MAXBYTES];
5756 int i;
5757 int c;
5758
5759 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
5760 {
5761 buf[0] = cc;
5762 for (i = 1; i < n; ++i)
5763 buf[i] = replace_pop();
5764 ins_bytes_len(buf, n);
5765 }
5766 else
5767 ins_char(cc);
5768
5769 if (enc_utf8)
5770 /* Handle composing chars. */
5771 for (;;)
5772 {
5773 c = replace_pop();
5774 if (c == -1) /* stack empty */
5775 break;
5776 if ((n = MB_BYTE2LEN(c)) == 1)
5777 {
5778 /* Not a multi-byte char, put it back. */
5779 replace_push(c);
5780 break;
5781 }
5782 else
5783 {
5784 buf[0] = c;
5785 for (i = 1; i < n; ++i)
5786 buf[i] = replace_pop();
5787 if (utf_iscomposing(utf_ptr2char(buf)))
5788 ins_bytes_len(buf, n);
5789 else
5790 {
5791 /* Not a composing char, put it back. */
5792 for (i = n - 1; i >= 0; --i)
5793 replace_push(buf[i]);
5794 break;
5795 }
5796 }
5797 }
5798}
5799#endif
5800
5801/*
5802 * make the replace stack empty
5803 * (called when exiting replace mode)
5804 */
5805 static void
5806replace_flush()
5807{
5808 vim_free(replace_stack);
5809 replace_stack = NULL;
5810 replace_stack_len = 0;
5811 replace_stack_nr = 0;
5812}
5813
5814/*
5815 * Handle doing a BS for one character.
5816 * cc < 0: replace stack empty, just move cursor
5817 * cc == 0: character was inserted, delete it
5818 * cc > 0: character was replaced, put cc (first byte of original char) back
5819 * and check for more characters to be put back
5820 */
5821 static void
5822replace_do_bs()
5823{
5824 int cc;
5825#ifdef FEAT_VREPLACE
5826 int orig_len = 0;
5827 int ins_len;
5828 int orig_vcols = 0;
5829 colnr_T start_vcol;
5830 char_u *p;
5831 int i;
5832 int vcol;
5833#endif
5834
5835 cc = replace_pop();
5836 if (cc > 0)
5837 {
5838#ifdef FEAT_VREPLACE
5839 if (State & VREPLACE_FLAG)
5840 {
5841 /* Get the number of screen cells used by the character we are
5842 * going to delete. */
5843 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
5844 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
5845 }
5846#endif
5847#ifdef FEAT_MBYTE
5848 if (has_mbyte)
5849 {
5850 del_char(FALSE);
5851# ifdef FEAT_VREPLACE
5852 if (State & VREPLACE_FLAG)
5853 orig_len = STRLEN(ml_get_cursor());
5854# endif
5855 replace_push(cc);
5856 }
5857 else
5858#endif
5859 {
5860 pchar_cursor(cc);
5861#ifdef FEAT_VREPLACE
5862 if (State & VREPLACE_FLAG)
5863 orig_len = STRLEN(ml_get_cursor()) - 1;
5864#endif
5865 }
5866 replace_pop_ins();
5867
5868#ifdef FEAT_VREPLACE
5869 if (State & VREPLACE_FLAG)
5870 {
5871 /* Get the number of screen cells used by the inserted characters */
5872 p = ml_get_cursor();
5873 ins_len = STRLEN(p) - orig_len;
5874 vcol = start_vcol;
5875 for (i = 0; i < ins_len; ++i)
5876 {
5877 vcol += chartabsize(p + i, vcol);
5878#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005879 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880#endif
5881 }
5882 vcol -= start_vcol;
5883
5884 /* Delete spaces that were inserted after the cursor to keep the
5885 * text aligned. */
5886 curwin->w_cursor.col += ins_len;
5887 while (vcol > orig_vcols && gchar_cursor() == ' ')
5888 {
5889 del_char(FALSE);
5890 ++orig_vcols;
5891 }
5892 curwin->w_cursor.col -= ins_len;
5893 }
5894#endif
5895
5896 /* mark the buffer as changed and prepare for displaying */
5897 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
5898 }
5899 else if (cc == 0)
5900 (void)del_char(FALSE);
5901}
5902
5903#ifdef FEAT_CINDENT
5904/*
5905 * Return TRUE if C-indenting is on.
5906 */
5907 static int
5908cindent_on()
5909{
5910 return (!p_paste && (curbuf->b_p_cin
5911# ifdef FEAT_EVAL
5912 || *curbuf->b_p_inde != NUL
5913# endif
5914 ));
5915}
5916#endif
5917
5918#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
5919/*
5920 * Re-indent the current line, based on the current contents of it and the
5921 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
5922 * confused what all the part that handles Control-T is doing that I'm not.
5923 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
5924 */
5925
5926 void
5927fixthisline(get_the_indent)
5928 int (*get_the_indent) __ARGS((void));
5929{
5930 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
5931 if (linewhite(curwin->w_cursor.lnum))
5932 did_ai = TRUE; /* delete the indent if the line stays empty */
5933}
5934
5935 void
5936fix_indent()
5937{
5938 if (p_paste)
5939 return;
5940# ifdef FEAT_LISP
5941 if (curbuf->b_p_lisp && curbuf->b_p_ai)
5942 fixthisline(get_lisp_indent);
5943# endif
5944# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
5945 else
5946# endif
5947# ifdef FEAT_CINDENT
5948 if (cindent_on())
5949 do_c_expr_indent();
5950# endif
5951}
5952
5953#endif
5954
5955#ifdef FEAT_CINDENT
5956/*
5957 * return TRUE if 'cinkeys' contains the key "keytyped",
5958 * when == '*': Only if key is preceded with '*' (indent before insert)
5959 * when == '!': Only if key is prededed with '!' (don't insert)
5960 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
5961 *
5962 * "keytyped" can have a few special values:
5963 * KEY_OPEN_FORW
5964 * KEY_OPEN_BACK
5965 * KEY_COMPLETE just finished completion.
5966 *
5967 * If line_is_empty is TRUE accept keys with '0' before them.
5968 */
5969 int
5970in_cinkeys(keytyped, when, line_is_empty)
5971 int keytyped;
5972 int when;
5973 int line_is_empty;
5974{
5975 char_u *look;
5976 int try_match;
5977 int try_match_word;
5978 char_u *p;
5979 char_u *line;
5980 int icase;
5981 int i;
5982
5983#ifdef FEAT_EVAL
5984 if (*curbuf->b_p_inde != NUL)
5985 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
5986 else
5987#endif
5988 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
5989 while (*look)
5990 {
5991 /*
5992 * Find out if we want to try a match with this key, depending on
5993 * 'when' and a '*' or '!' before the key.
5994 */
5995 switch (when)
5996 {
5997 case '*': try_match = (*look == '*'); break;
5998 case '!': try_match = (*look == '!'); break;
5999 default: try_match = (*look != '*'); break;
6000 }
6001 if (*look == '*' || *look == '!')
6002 ++look;
6003
6004 /*
6005 * If there is a '0', only accept a match if the line is empty.
6006 * But may still match when typing last char of a word.
6007 */
6008 if (*look == '0')
6009 {
6010 try_match_word = try_match;
6011 if (!line_is_empty)
6012 try_match = FALSE;
6013 ++look;
6014 }
6015 else
6016 try_match_word = FALSE;
6017
6018 /*
6019 * does it look like a control character?
6020 */
6021 if (*look == '^'
6022#ifdef EBCDIC
6023 && (Ctrl_chr(look[1]) != 0)
6024#else
6025 && look[1] >= '?' && look[1] <= '_'
6026#endif
6027 )
6028 {
6029 if (try_match && keytyped == Ctrl_chr(look[1]))
6030 return TRUE;
6031 look += 2;
6032 }
6033 /*
6034 * 'o' means "o" command, open forward.
6035 * 'O' means "O" command, open backward.
6036 */
6037 else if (*look == 'o')
6038 {
6039 if (try_match && keytyped == KEY_OPEN_FORW)
6040 return TRUE;
6041 ++look;
6042 }
6043 else if (*look == 'O')
6044 {
6045 if (try_match && keytyped == KEY_OPEN_BACK)
6046 return TRUE;
6047 ++look;
6048 }
6049
6050 /*
6051 * 'e' means to check for "else" at start of line and just before the
6052 * cursor.
6053 */
6054 else if (*look == 'e')
6055 {
6056 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6057 {
6058 p = ml_get_curline();
6059 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6060 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6061 return TRUE;
6062 }
6063 ++look;
6064 }
6065
6066 /*
6067 * ':' only causes an indent if it is at the end of a label or case
6068 * statement, or when it was before typing the ':' (to fix
6069 * class::method for C++).
6070 */
6071 else if (*look == ':')
6072 {
6073 if (try_match && keytyped == ':')
6074 {
6075 p = ml_get_curline();
6076 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6077 return TRUE;
6078 if (curwin->w_cursor.col > 2
6079 && p[curwin->w_cursor.col - 1] == ':'
6080 && p[curwin->w_cursor.col - 2] == ':')
6081 {
6082 p[curwin->w_cursor.col - 1] = ' ';
6083 i = (cin_iscase(p) || cin_isscopedecl(p)
6084 || cin_islabel(30));
6085 p = ml_get_curline();
6086 p[curwin->w_cursor.col - 1] = ':';
6087 if (i)
6088 return TRUE;
6089 }
6090 }
6091 ++look;
6092 }
6093
6094
6095 /*
6096 * Is it a key in <>, maybe?
6097 */
6098 else if (*look == '<')
6099 {
6100 if (try_match)
6101 {
6102 /*
6103 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6104 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6105 * >, *, : and ! keys if they really really want to.
6106 */
6107 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6108 && keytyped == look[1])
6109 return TRUE;
6110
6111 if (keytyped == get_special_key_code(look + 1))
6112 return TRUE;
6113 }
6114 while (*look && *look != '>')
6115 look++;
6116 while (*look == '>')
6117 look++;
6118 }
6119
6120 /*
6121 * Is it a word: "=word"?
6122 */
6123 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6124 {
6125 ++look;
6126 if (*look == '~')
6127 {
6128 icase = TRUE;
6129 ++look;
6130 }
6131 else
6132 icase = FALSE;
6133 p = vim_strchr(look, ',');
6134 if (p == NULL)
6135 p = look + STRLEN(look);
6136 if ((try_match || try_match_word)
6137 && curwin->w_cursor.col >= (colnr_T)(p - look))
6138 {
6139 int match = FALSE;
6140
6141#ifdef FEAT_INS_EXPAND
6142 if (keytyped == KEY_COMPLETE)
6143 {
6144 char_u *s;
6145
6146 /* Just completed a word, check if it starts with "look".
6147 * search back for the start of a word. */
6148 line = ml_get_curline();
6149# ifdef FEAT_MBYTE
6150 if (has_mbyte)
6151 {
6152 char_u *n;
6153
6154 for (s = line + curwin->w_cursor.col; s > line; s = n)
6155 {
6156 n = mb_prevptr(line, s);
6157 if (!vim_iswordp(n))
6158 break;
6159 }
6160 }
6161 else
6162# endif
6163 for (s = line + curwin->w_cursor.col; s > line; --s)
6164 if (!vim_iswordc(s[-1]))
6165 break;
6166 if (s + (p - look) <= line + curwin->w_cursor.col
6167 && (icase
6168 ? MB_STRNICMP(s, look, p - look)
6169 : STRNCMP(s, look, p - look)) == 0)
6170 match = TRUE;
6171 }
6172 else
6173#endif
6174 /* TODO: multi-byte */
6175 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6176 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6177 {
6178 line = ml_get_cursor();
6179 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6180 || !vim_iswordc(line[-(p - look) - 1]))
6181 && (icase
6182 ? MB_STRNICMP(line - (p - look), look, p - look)
6183 : STRNCMP(line - (p - look), look, p - look))
6184 == 0)
6185 match = TRUE;
6186 }
6187 if (match && try_match_word && !try_match)
6188 {
6189 /* "0=word": Check if there are only blanks before the
6190 * word. */
6191 line = ml_get_curline();
6192 if ((int)(skipwhite(line) - line) !=
6193 (int)(curwin->w_cursor.col - (p - look)))
6194 match = FALSE;
6195 }
6196 if (match)
6197 return TRUE;
6198 }
6199 look = p;
6200 }
6201
6202 /*
6203 * ok, it's a boring generic character.
6204 */
6205 else
6206 {
6207 if (try_match && *look == keytyped)
6208 return TRUE;
6209 ++look;
6210 }
6211
6212 /*
6213 * Skip over ", ".
6214 */
6215 look = skip_to_option_part(look);
6216 }
6217 return FALSE;
6218}
6219#endif /* FEAT_CINDENT */
6220
6221#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6222/*
6223 * Map Hebrew keyboard when in hkmap mode.
6224 */
6225 int
6226hkmap(c)
6227 int c;
6228{
6229 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6230 {
6231 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6232 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6233 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6234 static char_u map[26] =
6235 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6236 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6237 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6238 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6239 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6240 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6241 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6242 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6243 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6244
6245 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6246 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6247 /* '-1'='sofit' */
6248 else if (c == 'x')
6249 return 'X';
6250 else if (c == 'q')
6251 return '\''; /* {geresh}={'} */
6252 else if (c == 246)
6253 return ' '; /* \"o --> ' ' for a german keyboard */
6254 else if (c == 228)
6255 return ' '; /* \"a --> ' ' -- / -- */
6256 else if (c == 252)
6257 return ' '; /* \"u --> ' ' -- / -- */
6258#ifdef EBCDIC
6259 else if (islower(c))
6260#else
6261 /* NOTE: islower() does not do the right thing for us on Linux so we
6262 * do this the same was as 5.7 and previous, so it works correctly on
6263 * all systems. Specifically, the e.g. Delete and Arrow keys are
6264 * munged and won't work if e.g. searching for Hebrew text.
6265 */
6266 else if (c >= 'a' && c <= 'z')
6267#endif
6268 return (int)(map[CharOrdLow(c)] + p_aleph);
6269 else
6270 return c;
6271 }
6272 else
6273 {
6274 switch (c)
6275 {
6276 case '`': return ';';
6277 case '/': return '.';
6278 case '\'': return ',';
6279 case 'q': return '/';
6280 case 'w': return '\'';
6281
6282 /* Hebrew letters - set offset from 'a' */
6283 case ',': c = '{'; break;
6284 case '.': c = 'v'; break;
6285 case ';': c = 't'; break;
6286 default: {
6287 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6288
6289#ifdef EBCDIC
6290 /* see note about islower() above */
6291 if (!islower(c))
6292#else
6293 if (c < 'a' || c > 'z')
6294#endif
6295 return c;
6296 c = str[CharOrdLow(c)];
6297 break;
6298 }
6299 }
6300
6301 return (int)(CharOrdLow(c) + p_aleph);
6302 }
6303}
6304#endif
6305
6306 static void
6307ins_reg()
6308{
6309 int need_redraw = FALSE;
6310 int regname;
6311 int literally = 0;
6312
6313 /*
6314 * If we are going to wait for a character, show a '"'.
6315 */
6316 pc_status = PC_STATUS_UNSET;
6317 if (redrawing() && !char_avail())
6318 {
6319 /* may need to redraw when no more chars available now */
6320 ins_redraw();
6321
6322 edit_putchar('"', TRUE);
6323#ifdef FEAT_CMDL_INFO
6324 add_to_showcmd_c(Ctrl_R);
6325#endif
6326 }
6327
6328#ifdef USE_ON_FLY_SCROLL
6329 dont_scroll = TRUE; /* disallow scrolling here */
6330#endif
6331
6332 /*
6333 * Don't map the register name. This also prevents the mode message to be
6334 * deleted when ESC is hit.
6335 */
6336 ++no_mapping;
6337 regname = safe_vgetc();
6338#ifdef FEAT_LANGMAP
6339 LANGMAP_ADJUST(regname, TRUE);
6340#endif
6341 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6342 {
6343 /* Get a third key for literal register insertion */
6344 literally = regname;
6345#ifdef FEAT_CMDL_INFO
6346 add_to_showcmd_c(literally);
6347#endif
6348 regname = safe_vgetc();
6349#ifdef FEAT_LANGMAP
6350 LANGMAP_ADJUST(regname, TRUE);
6351#endif
6352 }
6353 --no_mapping;
6354
6355#ifdef FEAT_EVAL
6356 /*
6357 * Don't call u_sync() while getting the expression,
6358 * evaluating it or giving an error message for it!
6359 */
6360 ++no_u_sync;
6361 if (regname == '=')
6362 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006363# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006367# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 /* Restore the Input Method. */
6369 if (im_on)
6370 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006371# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006373 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6374 {
6375 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 else
6379 {
6380#endif
6381 if (literally == Ctrl_O || literally == Ctrl_P)
6382 {
6383 /* Append the command to the redo buffer. */
6384 AppendCharToRedobuff(Ctrl_R);
6385 AppendCharToRedobuff(literally);
6386 AppendCharToRedobuff(regname);
6387
6388 do_put(regname, BACKWARD, 1L,
6389 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6390 }
6391 else if (insert_reg(regname, literally) == FAIL)
6392 {
6393 vim_beep();
6394 need_redraw = TRUE; /* remove the '"' */
6395 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006396 else if (stop_insert_mode)
6397 /* When the '=' register was used and a function was invoked that
6398 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
6399 * insert anything, need to remove the '"' */
6400 need_redraw = TRUE;
6401
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402#ifdef FEAT_EVAL
6403 }
6404 --no_u_sync;
6405#endif
6406#ifdef FEAT_CMDL_INFO
6407 clear_showcmd();
6408#endif
6409
6410 /* If the inserted register is empty, we need to remove the '"' */
6411 if (need_redraw || stuff_empty())
6412 edit_unputchar();
6413}
6414
6415/*
6416 * CTRL-G commands in Insert mode.
6417 */
6418 static void
6419ins_ctrl_g()
6420{
6421 int c;
6422
6423#ifdef FEAT_INS_EXPAND
6424 /* Right after CTRL-X the cursor will be after the ruler. */
6425 setcursor();
6426#endif
6427
6428 /*
6429 * Don't map the second key. This also prevents the mode message to be
6430 * deleted when ESC is hit.
6431 */
6432 ++no_mapping;
6433 c = safe_vgetc();
6434 --no_mapping;
6435 switch (c)
6436 {
6437 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
6438 case K_UP:
6439 case Ctrl_K:
6440 case 'k': ins_up(TRUE);
6441 break;
6442
6443 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
6444 case K_DOWN:
6445 case Ctrl_J:
6446 case 'j': ins_down(TRUE);
6447 break;
6448
6449 /* CTRL-G u: start new undoable edit */
6450 case 'u': u_sync();
6451 ins_need_undo = TRUE;
6452 break;
6453
6454 /* Unknown CTRL-G command, reserved for future expansion. */
6455 default: vim_beep();
6456 }
6457}
6458
6459/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006460 * CTRL-^ in Insert mode.
6461 */
6462 static void
6463ins_ctrl_hat()
6464{
6465 if (map_to_exists_mode((char_u *)"", LANGMAP))
6466 {
6467 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
6468 if (State & LANGMAP)
6469 {
6470 curbuf->b_p_iminsert = B_IMODE_NONE;
6471 State &= ~LANGMAP;
6472 }
6473 else
6474 {
6475 curbuf->b_p_iminsert = B_IMODE_LMAP;
6476 State |= LANGMAP;
6477#ifdef USE_IM_CONTROL
6478 im_set_active(FALSE);
6479#endif
6480 }
6481 }
6482#ifdef USE_IM_CONTROL
6483 else
6484 {
6485 /* There are no ":lmap" mappings, toggle IM */
6486 if (im_get_status())
6487 {
6488 curbuf->b_p_iminsert = B_IMODE_NONE;
6489 im_set_active(FALSE);
6490 }
6491 else
6492 {
6493 curbuf->b_p_iminsert = B_IMODE_IM;
6494 State &= ~LANGMAP;
6495 im_set_active(TRUE);
6496 }
6497 }
6498#endif
6499 set_iminsert_global();
6500 showmode();
6501#ifdef FEAT_GUI
6502 /* may show different cursor shape or color */
6503 if (gui.in_use)
6504 gui_update_cursor(TRUE, FALSE);
6505#endif
6506#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6507 /* Show/unshow value of 'keymap' in status lines. */
6508 status_redraw_curbuf();
6509#endif
6510}
6511
6512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 * Handle ESC in insert mode.
6514 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
6515 * insert.
6516 */
6517 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00006518ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 long *count;
6520 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00006521 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522{
6523 int temp;
6524 static int disabled_redraw = FALSE;
6525
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006526#ifdef FEAT_SYN_HL
6527 check_spell_redraw();
6528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529#if defined(FEAT_HANGULIN)
6530# if defined(ESC_CHG_TO_ENG_MODE)
6531 hangul_input_state_set(0);
6532# endif
6533 if (composing_hangul)
6534 {
6535 push_raw_key(composing_hangul_buffer, 2);
6536 composing_hangul = 0;
6537 }
6538#endif
6539#if defined(FEAT_MBYTE) && defined(MACOS_CLASSIC)
6540 previous_script = GetScriptManagerVariable(smKeyScript);
6541 KeyScript(smKeyRoman); /* or smKeySysScript */
6542#endif
6543
6544 temp = curwin->w_cursor.col;
6545 if (disabled_redraw)
6546 {
6547 --RedrawingDisabled;
6548 disabled_redraw = FALSE;
6549 }
6550 if (!arrow_used)
6551 {
6552 /*
6553 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00006554 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
6555 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 */
6557 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00006558 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559
6560 /*
6561 * Repeating insert may take a long time. Check for
6562 * interrupt now and then.
6563 */
6564 if (*count > 0)
6565 {
6566 line_breakcheck();
6567 if (got_int)
6568 *count = 0;
6569 }
6570
6571 if (--*count > 0) /* repeat what was typed */
6572 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006573 /* Vi repeats the insert without replacing characters. */
6574 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
6575 State &= ~REPLACE_FLAG;
6576
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 (void)start_redo_ins();
6578 if (cmdchar == 'r' || cmdchar == 'v')
6579 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
6580 ++RedrawingDisabled;
6581 disabled_redraw = TRUE;
6582 return FALSE; /* repeat the insert */
6583 }
6584 stop_insert(&curwin->w_cursor, TRUE);
6585 undisplay_dollar();
6586 }
6587
6588 /* When an autoindent was removed, curswant stays after the
6589 * indent */
6590 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
6591 curwin->w_set_curswant = TRUE;
6592
6593 /* Remember the last Insert position in the '^ mark. */
6594 if (!cmdmod.keepjumps)
6595 curbuf->b_last_insert = curwin->w_cursor;
6596
6597 /*
6598 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00006599 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00006601 if (!nomove
6602 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603#ifdef FEAT_VIRTUALEDIT
6604 || curwin->w_cursor.coladd > 0
6605#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006606 )
6607 && (restart_edit == NUL
6608 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00006610 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006612 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613#ifdef FEAT_RIGHTLEFT
6614 && !revins_on
6615#endif
6616 )
6617 {
6618#ifdef FEAT_VIRTUALEDIT
6619 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
6620 {
6621 oneleft();
6622 if (restart_edit != NUL)
6623 ++curwin->w_cursor.coladd;
6624 }
6625 else
6626#endif
6627 {
6628 --curwin->w_cursor.col;
6629#ifdef FEAT_MBYTE
6630 /* Correct cursor for multi-byte character. */
6631 if (has_mbyte)
6632 mb_adjust_cursor();
6633#endif
6634 }
6635 }
6636
6637#ifdef USE_IM_CONTROL
6638 /* Disable IM to allow typing English directly for Normal mode commands.
6639 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
6640 * well). */
6641 if (!(State & LANGMAP))
6642 im_save_status(&curbuf->b_p_iminsert);
6643 im_set_active(FALSE);
6644#endif
6645
6646 State = NORMAL;
6647 /* need to position cursor again (e.g. when on a TAB ) */
6648 changed_cline_bef_curs();
6649
6650#ifdef FEAT_MOUSE
6651 setmouse();
6652#endif
6653#ifdef CURSOR_SHAPE
6654 ui_cursor_shape(); /* may show different cursor shape */
6655#endif
6656
6657 /*
6658 * When recording or for CTRL-O, need to display the new mode.
6659 * Otherwise remove the mode message.
6660 */
6661 if (Recording || restart_edit != NUL)
6662 showmode();
6663 else if (p_smd)
6664 MSG("");
6665
6666 return TRUE; /* exit Insert mode */
6667}
6668
6669#ifdef FEAT_RIGHTLEFT
6670/*
6671 * Toggle language: hkmap and revins_on.
6672 * Move to end of reverse inserted text.
6673 */
6674 static void
6675ins_ctrl_()
6676{
6677 if (revins_on && revins_chars && revins_scol >= 0)
6678 {
6679 while (gchar_cursor() != NUL && revins_chars--)
6680 ++curwin->w_cursor.col;
6681 }
6682 p_ri = !p_ri;
6683 revins_on = (State == INSERT && p_ri);
6684 if (revins_on)
6685 {
6686 revins_scol = curwin->w_cursor.col;
6687 revins_legal++;
6688 revins_chars = 0;
6689 undisplay_dollar();
6690 }
6691 else
6692 revins_scol = -1;
6693#ifdef FEAT_FKMAP
6694 if (p_altkeymap)
6695 {
6696 /*
6697 * to be consistent also for redo command, using '.'
6698 * set arrow_used to true and stop it - causing to redo
6699 * characters entered in one mode (normal/reverse insert).
6700 */
6701 arrow_used = TRUE;
6702 (void)stop_arrow();
6703 p_fkmap = curwin->w_p_rl ^ p_ri;
6704 if (p_fkmap && p_ri)
6705 State = INSERT;
6706 }
6707 else
6708#endif
6709 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
6710 showmode();
6711}
6712#endif
6713
6714#ifdef FEAT_VISUAL
6715/*
6716 * If 'keymodel' contains "startsel", may start selection.
6717 * Returns TRUE when a CTRL-O and other keys stuffed.
6718 */
6719 static int
6720ins_start_select(c)
6721 int c;
6722{
6723 if (km_startsel)
6724 switch (c)
6725 {
6726 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 case K_PAGEUP:
6729 case K_KPAGEUP:
6730 case K_PAGEDOWN:
6731 case K_KPAGEDOWN:
6732# ifdef MACOS
6733 case K_LEFT:
6734 case K_RIGHT:
6735 case K_UP:
6736 case K_DOWN:
6737 case K_END:
6738 case K_HOME:
6739# endif
6740 if (!(mod_mask & MOD_MASK_SHIFT))
6741 break;
6742 /* FALLTHROUGH */
6743 case K_S_LEFT:
6744 case K_S_RIGHT:
6745 case K_S_UP:
6746 case K_S_DOWN:
6747 case K_S_END:
6748 case K_S_HOME:
6749 /* Start selection right away, the cursor can move with
6750 * CTRL-O when beyond the end of the line. */
6751 start_selection();
6752
6753 /* Execute the key in (insert) Select mode. */
6754 stuffcharReadbuff(Ctrl_O);
6755 if (mod_mask)
6756 {
6757 char_u buf[4];
6758
6759 buf[0] = K_SPECIAL;
6760 buf[1] = KS_MODIFIER;
6761 buf[2] = mod_mask;
6762 buf[3] = NUL;
6763 stuffReadbuff(buf);
6764 }
6765 stuffcharReadbuff(c);
6766 return TRUE;
6767 }
6768 return FALSE;
6769}
6770#endif
6771
6772/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006773 * <Insert> key in Insert mode: toggle insert/remplace mode.
6774 */
6775 static void
6776ins_insert(replaceState)
6777 int replaceState;
6778{
6779#ifdef FEAT_FKMAP
6780 if (p_fkmap && p_ri)
6781 {
6782 beep_flush();
6783 EMSG(farsi_text_3); /* encoded in Farsi */
6784 return;
6785 }
6786#endif
6787
6788#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00006789# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006790 set_vim_var_string(VV_INSERTMODE,
6791 (char_u *)((State & REPLACE_FLAG) ? "i" :
6792 replaceState == VREPLACE ? "v" : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00006793# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006794 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
6795#endif
6796 if (State & REPLACE_FLAG)
6797 State = INSERT | (State & LANGMAP);
6798 else
6799 State = replaceState | (State & LANGMAP);
6800 AppendCharToRedobuff(K_INS);
6801 showmode();
6802#ifdef CURSOR_SHAPE
6803 ui_cursor_shape(); /* may show different cursor shape */
6804#endif
6805}
6806
6807/*
6808 * Pressed CTRL-O in Insert mode.
6809 */
6810 static void
6811ins_ctrl_o()
6812{
6813#ifdef FEAT_VREPLACE
6814 if (State & VREPLACE_FLAG)
6815 restart_edit = 'V';
6816 else
6817#endif
6818 if (State & REPLACE_FLAG)
6819 restart_edit = 'R';
6820 else
6821 restart_edit = 'I';
6822#ifdef FEAT_VIRTUALEDIT
6823 if (virtual_active())
6824 ins_at_eol = FALSE; /* cursor always keeps its column */
6825 else
6826#endif
6827 ins_at_eol = (gchar_cursor() == NUL);
6828}
6829
6830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 * If the cursor is on an indent, ^T/^D insert/delete one
6832 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
6833 * Always round the indent to 'shiftwith', this is compatible
6834 * with vi. But vi only supports ^T and ^D after an
6835 * autoindent, we support it everywhere.
6836 */
6837 static void
6838ins_shift(c, lastc)
6839 int c;
6840 int lastc;
6841{
6842 if (stop_arrow() == FAIL)
6843 return;
6844 AppendCharToRedobuff(c);
6845
6846 /*
6847 * 0^D and ^^D: remove all indent.
6848 */
6849 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
6850 {
6851 --curwin->w_cursor.col;
6852 (void)del_char(FALSE); /* delete the '^' or '0' */
6853 /* In Replace mode, restore the characters that '^' or '0' replaced. */
6854 if (State & REPLACE_FLAG)
6855 replace_pop_ins();
6856 if (lastc == '^')
6857 old_indent = get_indent(); /* remember curr. indent */
6858 change_indent(INDENT_SET, 0, TRUE, 0);
6859 }
6860 else
6861 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
6862
6863 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
6864 did_ai = FALSE;
6865#ifdef FEAT_SMARTINDENT
6866 did_si = FALSE;
6867 can_si = FALSE;
6868 can_si_back = FALSE;
6869#endif
6870#ifdef FEAT_CINDENT
6871 can_cindent = FALSE; /* no cindenting after ^D or ^T */
6872#endif
6873}
6874
6875 static void
6876ins_del()
6877{
6878 int temp;
6879
6880 if (stop_arrow() == FAIL)
6881 return;
6882 if (gchar_cursor() == NUL) /* delete newline */
6883 {
6884 temp = curwin->w_cursor.col;
6885 if (!can_bs(BS_EOL) /* only if "eol" included */
6886 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
6887 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
6888 || do_join(FALSE) == FAIL)
6889 vim_beep();
6890 else
6891 curwin->w_cursor.col = temp;
6892 }
6893 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
6894 vim_beep();
6895 did_ai = FALSE;
6896#ifdef FEAT_SMARTINDENT
6897 did_si = FALSE;
6898 can_si = FALSE;
6899 can_si_back = FALSE;
6900#endif
6901 AppendCharToRedobuff(K_DEL);
6902}
6903
6904/*
6905 * Handle Backspace, delete-word and delete-line in Insert mode.
6906 * Return TRUE when backspace was actually used.
6907 */
6908 static int
6909ins_bs(c, mode, inserted_space_p)
6910 int c;
6911 int mode;
6912 int *inserted_space_p;
6913{
6914 linenr_T lnum;
6915 int cc;
6916 int temp = 0; /* init for GCC */
6917 colnr_T mincol;
6918 int did_backspace = FALSE;
6919 int in_indent;
6920 int oldState;
6921#ifdef FEAT_MBYTE
6922 int p1, p2;
6923#endif
6924
6925 /*
6926 * can't delete anything in an empty file
6927 * can't backup past first character in buffer
6928 * can't backup past starting point unless 'backspace' > 1
6929 * can backup to a previous line if 'backspace' == 0
6930 */
6931 if ( bufempty()
6932 || (
6933#ifdef FEAT_RIGHTLEFT
6934 !revins_on &&
6935#endif
6936 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
6937 || (!can_bs(BS_START)
6938 && (arrow_used
6939 || (curwin->w_cursor.lnum == Insstart.lnum
6940 && curwin->w_cursor.col <= Insstart.col)))
6941 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
6942 && curwin->w_cursor.col <= ai_col)
6943 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
6944 {
6945 vim_beep();
6946 return FALSE;
6947 }
6948
6949 if (stop_arrow() == FAIL)
6950 return FALSE;
6951 in_indent = inindent(0);
6952#ifdef FEAT_CINDENT
6953 if (in_indent)
6954 can_cindent = FALSE;
6955#endif
6956#ifdef FEAT_COMMENTS
6957 end_comment_pending = NUL; /* After BS, don't auto-end comment */
6958#endif
6959#ifdef FEAT_RIGHTLEFT
6960 if (revins_on) /* put cursor after last inserted char */
6961 inc_cursor();
6962#endif
6963
6964#ifdef FEAT_VIRTUALEDIT
6965 /* Virtualedit:
6966 * BACKSPACE_CHAR eats a virtual space
6967 * BACKSPACE_WORD eats all coladd
6968 * BACKSPACE_LINE eats all coladd and keeps going
6969 */
6970 if (curwin->w_cursor.coladd > 0)
6971 {
6972 if (mode == BACKSPACE_CHAR)
6973 {
6974 --curwin->w_cursor.coladd;
6975 return TRUE;
6976 }
6977 if (mode == BACKSPACE_WORD)
6978 {
6979 curwin->w_cursor.coladd = 0;
6980 return TRUE;
6981 }
6982 curwin->w_cursor.coladd = 0;
6983 }
6984#endif
6985
6986 /*
6987 * delete newline!
6988 */
6989 if (curwin->w_cursor.col == 0)
6990 {
6991 lnum = Insstart.lnum;
6992 if (curwin->w_cursor.lnum == Insstart.lnum
6993#ifdef FEAT_RIGHTLEFT
6994 || revins_on
6995#endif
6996 )
6997 {
6998 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
6999 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7000 return FALSE;
7001 --Insstart.lnum;
7002 Insstart.col = MAXCOL;
7003 }
7004 /*
7005 * In replace mode:
7006 * cc < 0: NL was inserted, delete it
7007 * cc >= 0: NL was replaced, put original characters back
7008 */
7009 cc = -1;
7010 if (State & REPLACE_FLAG)
7011 cc = replace_pop(); /* returns -1 if NL was inserted */
7012 /*
7013 * In replace mode, in the line we started replacing, we only move the
7014 * cursor.
7015 */
7016 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7017 {
7018 dec_cursor();
7019 }
7020 else
7021 {
7022#ifdef FEAT_VREPLACE
7023 if (!(State & VREPLACE_FLAG)
7024 || curwin->w_cursor.lnum > orig_line_count)
7025#endif
7026 {
7027 temp = gchar_cursor(); /* remember current char */
7028 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007029
7030 /* When "aw" is in 'formatoptions' we must delete the space at
7031 * the end of the line, otherwise the line will be broken
7032 * again when auto-formatting. */
7033 if (has_format_option(FO_AUTO)
7034 && has_format_option(FO_WHITE_PAR))
7035 {
7036 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7037 TRUE);
7038 int len;
7039
7040 len = STRLEN(ptr);
7041 if (len > 0 && ptr[len - 1] == ' ')
7042 ptr[len - 1] = NUL;
7043 }
7044
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 (void)do_join(FALSE);
7046 if (temp == NUL && gchar_cursor() != NUL)
7047 inc_cursor();
7048 }
7049#ifdef FEAT_VREPLACE
7050 else
7051 dec_cursor();
7052#endif
7053
7054 /*
7055 * In REPLACE mode we have to put back the text that was replaced
7056 * by the NL. On the replace stack is first a NUL-terminated
7057 * sequence of characters that were deleted and then the
7058 * characters that NL replaced.
7059 */
7060 if (State & REPLACE_FLAG)
7061 {
7062 /*
7063 * Do the next ins_char() in NORMAL state, to
7064 * prevent ins_char() from replacing characters and
7065 * avoiding showmatch().
7066 */
7067 oldState = State;
7068 State = NORMAL;
7069 /*
7070 * restore characters (blanks) deleted after cursor
7071 */
7072 while (cc > 0)
7073 {
7074 temp = curwin->w_cursor.col;
7075#ifdef FEAT_MBYTE
7076 mb_replace_pop_ins(cc);
7077#else
7078 ins_char(cc);
7079#endif
7080 curwin->w_cursor.col = temp;
7081 cc = replace_pop();
7082 }
7083 /* restore the characters that NL replaced */
7084 replace_pop_ins();
7085 State = oldState;
7086 }
7087 }
7088 did_ai = FALSE;
7089 }
7090 else
7091 {
7092 /*
7093 * Delete character(s) before the cursor.
7094 */
7095#ifdef FEAT_RIGHTLEFT
7096 if (revins_on) /* put cursor on last inserted char */
7097 dec_cursor();
7098#endif
7099 mincol = 0;
7100 /* keep indent */
7101 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7102#ifdef FEAT_RIGHTLEFT
7103 && !revins_on
7104#endif
7105 )
7106 {
7107 temp = curwin->w_cursor.col;
7108 beginline(BL_WHITE);
7109 if (curwin->w_cursor.col < (colnr_T)temp)
7110 mincol = curwin->w_cursor.col;
7111 curwin->w_cursor.col = temp;
7112 }
7113
7114 /*
7115 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7116 */
7117 if ( mode == BACKSPACE_CHAR
7118 && ((p_sta && in_indent)
7119 || (curbuf->b_p_sts
7120 && (*(ml_get_cursor() - 1) == TAB
7121 || (*(ml_get_cursor() - 1) == ' '
7122 && (!*inserted_space_p
7123 || arrow_used))))))
7124 {
7125 int ts;
7126 colnr_T vcol;
7127 colnr_T want_vcol;
7128 int extra = 0;
7129
7130 *inserted_space_p = FALSE;
7131 if (p_sta)
7132 ts = curbuf->b_p_sw;
7133 else
7134 ts = curbuf->b_p_sts;
7135 /* Compute the virtual column where we want to be. Since
7136 * 'showbreak' may get in the way, need to get the last column of
7137 * the previous character. */
7138 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7139 dec_cursor();
7140 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7141 inc_cursor();
7142 want_vcol = (want_vcol / ts) * ts;
7143
7144 /* delete characters until we are at or before want_vcol */
7145 while (vcol > want_vcol
7146 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7147 {
7148 dec_cursor();
7149 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7150 if (State & REPLACE_FLAG)
7151 {
7152 /* Don't delete characters before the insert point when in
7153 * Replace mode */
7154 if (curwin->w_cursor.lnum != Insstart.lnum
7155 || curwin->w_cursor.col >= Insstart.col)
7156 {
7157#if 0 /* what was this for? It causes problems when sw != ts. */
7158 if (State == REPLACE && (int)vcol < want_vcol)
7159 {
7160 (void)del_char(FALSE);
7161 extra = 2; /* don't pop too much */
7162 }
7163 else
7164#endif
7165 replace_do_bs();
7166 }
7167 }
7168 else
7169 (void)del_char(FALSE);
7170 }
7171
7172 /* insert extra spaces until we are at want_vcol */
7173 while (vcol < want_vcol)
7174 {
7175 /* Remember the first char we inserted */
7176 if (curwin->w_cursor.lnum == Insstart.lnum
7177 && curwin->w_cursor.col < Insstart.col)
7178 Insstart.col = curwin->w_cursor.col;
7179
7180#ifdef FEAT_VREPLACE
7181 if (State & VREPLACE_FLAG)
7182 ins_char(' ');
7183 else
7184#endif
7185 {
7186 ins_str((char_u *)" ");
7187 if ((State & REPLACE_FLAG) && extra <= 1)
7188 {
7189 if (extra)
7190 replace_push_off(NUL);
7191 else
7192 replace_push(NUL);
7193 }
7194 if (extra == 2)
7195 extra = 1;
7196 }
7197 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7198 }
7199 }
7200
7201 /*
7202 * Delete upto starting point, start of line or previous word.
7203 */
7204 else do
7205 {
7206#ifdef FEAT_RIGHTLEFT
7207 if (!revins_on) /* put cursor on char to be deleted */
7208#endif
7209 dec_cursor();
7210
7211 /* start of word? */
7212 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7213 {
7214 mode = BACKSPACE_WORD_NOT_SPACE;
7215 temp = vim_iswordc(gchar_cursor());
7216 }
7217 /* end of word? */
7218 else if (mode == BACKSPACE_WORD_NOT_SPACE
7219 && (vim_isspace(cc = gchar_cursor())
7220 || vim_iswordc(cc) != temp))
7221 {
7222#ifdef FEAT_RIGHTLEFT
7223 if (!revins_on)
7224#endif
7225 inc_cursor();
7226#ifdef FEAT_RIGHTLEFT
7227 else if (State & REPLACE_FLAG)
7228 dec_cursor();
7229#endif
7230 break;
7231 }
7232 if (State & REPLACE_FLAG)
7233 replace_do_bs();
7234 else
7235 {
7236#ifdef FEAT_MBYTE
7237 if (enc_utf8 && p_deco)
7238 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7239#endif
7240 (void)del_char(FALSE);
7241#ifdef FEAT_MBYTE
7242 /*
7243 * If p1 or p2 is non-zero, there are combining characters we
7244 * need to take account of. Don't back up before the base
7245 * character.
7246 */
7247 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7248 inc_cursor();
7249#endif
7250#ifdef FEAT_RIGHTLEFT
7251 if (revins_chars)
7252 {
7253 revins_chars--;
7254 revins_legal++;
7255 }
7256 if (revins_on && gchar_cursor() == NUL)
7257 break;
7258#endif
7259 }
7260 /* Just a single backspace?: */
7261 if (mode == BACKSPACE_CHAR)
7262 break;
7263 } while (
7264#ifdef FEAT_RIGHTLEFT
7265 revins_on ||
7266#endif
7267 (curwin->w_cursor.col > mincol
7268 && (curwin->w_cursor.lnum != Insstart.lnum
7269 || curwin->w_cursor.col != Insstart.col)));
7270 did_backspace = TRUE;
7271 }
7272#ifdef FEAT_SMARTINDENT
7273 did_si = FALSE;
7274 can_si = FALSE;
7275 can_si_back = FALSE;
7276#endif
7277 if (curwin->w_cursor.col <= 1)
7278 did_ai = FALSE;
7279 /*
7280 * It's a little strange to put backspaces into the redo
7281 * buffer, but it makes auto-indent a lot easier to deal
7282 * with.
7283 */
7284 AppendCharToRedobuff(c);
7285
7286 /* If deleted before the insertion point, adjust it */
7287 if (curwin->w_cursor.lnum == Insstart.lnum
7288 && curwin->w_cursor.col < Insstart.col)
7289 Insstart.col = curwin->w_cursor.col;
7290
7291 /* vi behaviour: the cursor moves backward but the character that
7292 * was there remains visible
7293 * Vim behaviour: the cursor moves backward and the character that
7294 * was there is erased from the screen.
7295 * We can emulate the vi behaviour by pretending there is a dollar
7296 * displayed even when there isn't.
7297 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7298 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7299 dollar_vcol = curwin->w_virtcol;
7300
7301 return did_backspace;
7302}
7303
7304#ifdef FEAT_MOUSE
7305 static void
7306ins_mouse(c)
7307 int c;
7308{
7309 pos_T tpos;
7310
7311# ifdef FEAT_GUI
7312 /* When GUI is active, also move/paste when 'mouse' is empty */
7313 if (!gui.in_use)
7314# endif
7315 if (!mouse_has(MOUSE_INSERT))
7316 return;
7317
7318 undisplay_dollar();
7319 tpos = curwin->w_cursor;
7320 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7321 {
7322 start_arrow(&tpos);
7323# ifdef FEAT_CINDENT
7324 can_cindent = TRUE;
7325# endif
7326 }
7327
7328#ifdef FEAT_WINDOWS
7329 /* redraw status lines (in case another window became active) */
7330 redraw_statuslines();
7331#endif
7332}
7333
7334 static void
7335ins_mousescroll(up)
7336 int up;
7337{
7338 pos_T tpos;
7339# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7340 win_T *old_curwin;
7341# endif
7342
7343 tpos = curwin->w_cursor;
7344
7345# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7346 old_curwin = curwin;
7347
7348 /* Currently the mouse coordinates are only known in the GUI. */
7349 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7350 {
7351 int row, col;
7352
7353 row = mouse_row;
7354 col = mouse_col;
7355
7356 /* find the window at the pointer coordinates */
7357 curwin = mouse_find_win(&row, &col);
7358 curbuf = curwin->w_buffer;
7359 }
7360 if (curwin == old_curwin)
7361# endif
7362 undisplay_dollar();
7363
7364 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7365 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7366 else
7367 scroll_redraw(up, 3L);
7368
7369# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7370 curwin->w_redr_status = TRUE;
7371
7372 curwin = old_curwin;
7373 curbuf = curwin->w_buffer;
7374# endif
7375
7376 if (!equalpos(curwin->w_cursor, tpos))
7377 {
7378 start_arrow(&tpos);
7379# ifdef FEAT_CINDENT
7380 can_cindent = TRUE;
7381# endif
7382 }
7383}
7384#endif
7385
7386#ifdef FEAT_GUI
7387 void
7388ins_scroll()
7389{
7390 pos_T tpos;
7391
7392 undisplay_dollar();
7393 tpos = curwin->w_cursor;
7394 if (gui_do_scroll())
7395 {
7396 start_arrow(&tpos);
7397# ifdef FEAT_CINDENT
7398 can_cindent = TRUE;
7399# endif
7400 }
7401}
7402
7403 void
7404ins_horscroll()
7405{
7406 pos_T tpos;
7407
7408 undisplay_dollar();
7409 tpos = curwin->w_cursor;
7410 if (gui_do_horiz_scroll())
7411 {
7412 start_arrow(&tpos);
7413# ifdef FEAT_CINDENT
7414 can_cindent = TRUE;
7415# endif
7416 }
7417}
7418#endif
7419
7420 static void
7421ins_left()
7422{
7423 pos_T tpos;
7424
7425#ifdef FEAT_FOLDING
7426 if ((fdo_flags & FDO_HOR) && KeyTyped)
7427 foldOpenCursor();
7428#endif
7429 undisplay_dollar();
7430 tpos = curwin->w_cursor;
7431 if (oneleft() == OK)
7432 {
7433 start_arrow(&tpos);
7434#ifdef FEAT_RIGHTLEFT
7435 /* If exit reversed string, position is fixed */
7436 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
7437 revins_legal++;
7438 revins_chars++;
7439#endif
7440 }
7441
7442 /*
7443 * if 'whichwrap' set for cursor in insert mode may go to
7444 * previous line
7445 */
7446 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
7447 {
7448 start_arrow(&tpos);
7449 --(curwin->w_cursor.lnum);
7450 coladvance((colnr_T)MAXCOL);
7451 curwin->w_set_curswant = TRUE; /* so we stay at the end */
7452 }
7453 else
7454 vim_beep();
7455}
7456
7457 static void
7458ins_home(c)
7459 int c;
7460{
7461 pos_T tpos;
7462
7463#ifdef FEAT_FOLDING
7464 if ((fdo_flags & FDO_HOR) && KeyTyped)
7465 foldOpenCursor();
7466#endif
7467 undisplay_dollar();
7468 tpos = curwin->w_cursor;
7469 if (c == K_C_HOME)
7470 curwin->w_cursor.lnum = 1;
7471 curwin->w_cursor.col = 0;
7472#ifdef FEAT_VIRTUALEDIT
7473 curwin->w_cursor.coladd = 0;
7474#endif
7475 curwin->w_curswant = 0;
7476 start_arrow(&tpos);
7477}
7478
7479 static void
7480ins_end(c)
7481 int c;
7482{
7483 pos_T tpos;
7484
7485#ifdef FEAT_FOLDING
7486 if ((fdo_flags & FDO_HOR) && KeyTyped)
7487 foldOpenCursor();
7488#endif
7489 undisplay_dollar();
7490 tpos = curwin->w_cursor;
7491 if (c == K_C_END)
7492 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7493 coladvance((colnr_T)MAXCOL);
7494 curwin->w_curswant = MAXCOL;
7495
7496 start_arrow(&tpos);
7497}
7498
7499 static void
7500ins_s_left()
7501{
7502#ifdef FEAT_FOLDING
7503 if ((fdo_flags & FDO_HOR) && KeyTyped)
7504 foldOpenCursor();
7505#endif
7506 undisplay_dollar();
7507 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
7508 {
7509 start_arrow(&curwin->w_cursor);
7510 (void)bck_word(1L, FALSE, FALSE);
7511 curwin->w_set_curswant = TRUE;
7512 }
7513 else
7514 vim_beep();
7515}
7516
7517 static void
7518ins_right()
7519{
7520#ifdef FEAT_FOLDING
7521 if ((fdo_flags & FDO_HOR) && KeyTyped)
7522 foldOpenCursor();
7523#endif
7524 undisplay_dollar();
7525 if (gchar_cursor() != NUL || virtual_active()
7526 )
7527 {
7528 start_arrow(&curwin->w_cursor);
7529 curwin->w_set_curswant = TRUE;
7530#ifdef FEAT_VIRTUALEDIT
7531 if (virtual_active())
7532 oneright();
7533 else
7534#endif
7535 {
7536#ifdef FEAT_MBYTE
7537 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007538 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 else
7540#endif
7541 ++curwin->w_cursor.col;
7542 }
7543
7544#ifdef FEAT_RIGHTLEFT
7545 revins_legal++;
7546 if (revins_chars)
7547 revins_chars--;
7548#endif
7549 }
7550 /* if 'whichwrap' set for cursor in insert mode, may move the
7551 * cursor to the next line */
7552 else if (vim_strchr(p_ww, ']') != NULL
7553 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7554 {
7555 start_arrow(&curwin->w_cursor);
7556 curwin->w_set_curswant = TRUE;
7557 ++curwin->w_cursor.lnum;
7558 curwin->w_cursor.col = 0;
7559 }
7560 else
7561 vim_beep();
7562}
7563
7564 static void
7565ins_s_right()
7566{
7567#ifdef FEAT_FOLDING
7568 if ((fdo_flags & FDO_HOR) && KeyTyped)
7569 foldOpenCursor();
7570#endif
7571 undisplay_dollar();
7572 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
7573 || gchar_cursor() != NUL)
7574 {
7575 start_arrow(&curwin->w_cursor);
7576 (void)fwd_word(1L, FALSE, 0);
7577 curwin->w_set_curswant = TRUE;
7578 }
7579 else
7580 vim_beep();
7581}
7582
7583 static void
7584ins_up(startcol)
7585 int startcol; /* when TRUE move to Insstart.col */
7586{
7587 pos_T tpos;
7588 linenr_T old_topline = curwin->w_topline;
7589#ifdef FEAT_DIFF
7590 int old_topfill = curwin->w_topfill;
7591#endif
7592
7593 undisplay_dollar();
7594 tpos = curwin->w_cursor;
7595 if (cursor_up(1L, TRUE) == OK)
7596 {
7597 if (startcol)
7598 coladvance(getvcol_nolist(&Insstart));
7599 if (old_topline != curwin->w_topline
7600#ifdef FEAT_DIFF
7601 || old_topfill != curwin->w_topfill
7602#endif
7603 )
7604 redraw_later(VALID);
7605 start_arrow(&tpos);
7606#ifdef FEAT_CINDENT
7607 can_cindent = TRUE;
7608#endif
7609 }
7610 else
7611 vim_beep();
7612}
7613
7614 static void
7615ins_pageup()
7616{
7617 pos_T tpos;
7618
7619 undisplay_dollar();
7620 tpos = curwin->w_cursor;
7621 if (onepage(BACKWARD, 1L) == OK)
7622 {
7623 start_arrow(&tpos);
7624#ifdef FEAT_CINDENT
7625 can_cindent = TRUE;
7626#endif
7627 }
7628 else
7629 vim_beep();
7630}
7631
7632 static void
7633ins_down(startcol)
7634 int startcol; /* when TRUE move to Insstart.col */
7635{
7636 pos_T tpos;
7637 linenr_T old_topline = curwin->w_topline;
7638#ifdef FEAT_DIFF
7639 int old_topfill = curwin->w_topfill;
7640#endif
7641
7642 undisplay_dollar();
7643 tpos = curwin->w_cursor;
7644 if (cursor_down(1L, TRUE) == OK)
7645 {
7646 if (startcol)
7647 coladvance(getvcol_nolist(&Insstart));
7648 if (old_topline != curwin->w_topline
7649#ifdef FEAT_DIFF
7650 || old_topfill != curwin->w_topfill
7651#endif
7652 )
7653 redraw_later(VALID);
7654 start_arrow(&tpos);
7655#ifdef FEAT_CINDENT
7656 can_cindent = TRUE;
7657#endif
7658 }
7659 else
7660 vim_beep();
7661}
7662
7663 static void
7664ins_pagedown()
7665{
7666 pos_T tpos;
7667
7668 undisplay_dollar();
7669 tpos = curwin->w_cursor;
7670 if (onepage(FORWARD, 1L) == OK)
7671 {
7672 start_arrow(&tpos);
7673#ifdef FEAT_CINDENT
7674 can_cindent = TRUE;
7675#endif
7676 }
7677 else
7678 vim_beep();
7679}
7680
7681#ifdef FEAT_DND
7682 static void
7683ins_drop()
7684{
7685 do_put('~', BACKWARD, 1L, PUT_CURSEND);
7686}
7687#endif
7688
7689/*
7690 * Handle TAB in Insert or Replace mode.
7691 * Return TRUE when the TAB needs to be inserted like a normal character.
7692 */
7693 static int
7694ins_tab()
7695{
7696 int ind;
7697 int i;
7698 int temp;
7699
7700 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
7701 Insstart_blank_vcol = get_nolist_virtcol();
7702 if (echeck_abbr(TAB + ABBR_OFF))
7703 return FALSE;
7704
7705 ind = inindent(0);
7706#ifdef FEAT_CINDENT
7707 if (ind)
7708 can_cindent = FALSE;
7709#endif
7710
7711 /*
7712 * When nothing special, insert TAB like a normal character
7713 */
7714 if (!curbuf->b_p_et
7715 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
7716 && curbuf->b_p_sts == 0)
7717 return TRUE;
7718
7719 if (stop_arrow() == FAIL)
7720 return TRUE;
7721
7722 did_ai = FALSE;
7723#ifdef FEAT_SMARTINDENT
7724 did_si = FALSE;
7725 can_si = FALSE;
7726 can_si_back = FALSE;
7727#endif
7728 AppendToRedobuff((char_u *)"\t");
7729
7730 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
7731 temp = (int)curbuf->b_p_sw;
7732 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
7733 temp = (int)curbuf->b_p_sts;
7734 else /* otherwise use 'tabstop' */
7735 temp = (int)curbuf->b_p_ts;
7736 temp -= get_nolist_virtcol() % temp;
7737
7738 /*
7739 * Insert the first space with ins_char(). It will delete one char in
7740 * replace mode. Insert the rest with ins_str(); it will not delete any
7741 * chars. For VREPLACE mode, we use ins_char() for all characters.
7742 */
7743 ins_char(' ');
7744 while (--temp > 0)
7745 {
7746#ifdef FEAT_VREPLACE
7747 if (State & VREPLACE_FLAG)
7748 ins_char(' ');
7749 else
7750#endif
7751 {
7752 ins_str((char_u *)" ");
7753 if (State & REPLACE_FLAG) /* no char replaced */
7754 replace_push(NUL);
7755 }
7756 }
7757
7758 /*
7759 * When 'expandtab' not set: Replace spaces by TABs where possible.
7760 */
7761 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
7762 {
7763 char_u *ptr;
7764#ifdef FEAT_VREPLACE
7765 char_u *saved_line = NULL; /* init for GCC */
7766 pos_T pos;
7767#endif
7768 pos_T fpos;
7769 pos_T *cursor;
7770 colnr_T want_vcol, vcol;
7771 int change_col = -1;
7772 int save_list = curwin->w_p_list;
7773
7774 /*
7775 * Get the current line. For VREPLACE mode, don't make real changes
7776 * yet, just work on a copy of the line.
7777 */
7778#ifdef FEAT_VREPLACE
7779 if (State & VREPLACE_FLAG)
7780 {
7781 pos = curwin->w_cursor;
7782 cursor = &pos;
7783 saved_line = vim_strsave(ml_get_curline());
7784 if (saved_line == NULL)
7785 return FALSE;
7786 ptr = saved_line + pos.col;
7787 }
7788 else
7789#endif
7790 {
7791 ptr = ml_get_cursor();
7792 cursor = &curwin->w_cursor;
7793 }
7794
7795 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
7796 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
7797 curwin->w_p_list = FALSE;
7798
7799 /* Find first white before the cursor */
7800 fpos = curwin->w_cursor;
7801 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
7802 {
7803 --fpos.col;
7804 --ptr;
7805 }
7806
7807 /* In Replace mode, don't change characters before the insert point. */
7808 if ((State & REPLACE_FLAG)
7809 && fpos.lnum == Insstart.lnum
7810 && fpos.col < Insstart.col)
7811 {
7812 ptr += Insstart.col - fpos.col;
7813 fpos.col = Insstart.col;
7814 }
7815
7816 /* compute virtual column numbers of first white and cursor */
7817 getvcol(curwin, &fpos, &vcol, NULL, NULL);
7818 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
7819
7820 /* Use as many TABs as possible. Beware of 'showbreak' and
7821 * 'linebreak' adding extra virtual columns. */
7822 while (vim_iswhite(*ptr))
7823 {
7824 i = lbr_chartabsize((char_u *)"\t", vcol);
7825 if (vcol + i > want_vcol)
7826 break;
7827 if (*ptr != TAB)
7828 {
7829 *ptr = TAB;
7830 if (change_col < 0)
7831 {
7832 change_col = fpos.col; /* Column of first change */
7833 /* May have to adjust Insstart */
7834 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
7835 Insstart.col = fpos.col;
7836 }
7837 }
7838 ++fpos.col;
7839 ++ptr;
7840 vcol += i;
7841 }
7842
7843 if (change_col >= 0)
7844 {
7845 int repl_off = 0;
7846
7847 /* Skip over the spaces we need. */
7848 while (vcol < want_vcol && *ptr == ' ')
7849 {
7850 vcol += lbr_chartabsize(ptr, vcol);
7851 ++ptr;
7852 ++repl_off;
7853 }
7854 if (vcol > want_vcol)
7855 {
7856 /* Must have a char with 'showbreak' just before it. */
7857 --ptr;
7858 --repl_off;
7859 }
7860 fpos.col += repl_off;
7861
7862 /* Delete following spaces. */
7863 i = cursor->col - fpos.col;
7864 if (i > 0)
7865 {
7866 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
7867 /* correct replace stack. */
7868 if ((State & REPLACE_FLAG)
7869#ifdef FEAT_VREPLACE
7870 && !(State & VREPLACE_FLAG)
7871#endif
7872 )
7873 for (temp = i; --temp >= 0; )
7874 replace_join(repl_off);
7875 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00007876#ifdef FEAT_NETBEANS_INTG
7877 if (usingNetbeans)
7878 {
7879 netbeans_removed(curbuf, fpos.lnum, cursor->col,
7880 (long)(i + 1));
7881 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
7882 (char_u *)"\t", 1);
7883 }
7884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 cursor->col -= i;
7886
7887#ifdef FEAT_VREPLACE
7888 /*
7889 * In VREPLACE mode, we haven't changed anything yet. Do it now by
7890 * backspacing over the changed spacing and then inserting the new
7891 * spacing.
7892 */
7893 if (State & VREPLACE_FLAG)
7894 {
7895 /* Backspace from real cursor to change_col */
7896 backspace_until_column(change_col);
7897
7898 /* Insert each char in saved_line from changed_col to
7899 * ptr-cursor */
7900 ins_bytes_len(saved_line + change_col,
7901 cursor->col - change_col);
7902 }
7903#endif
7904 }
7905
7906#ifdef FEAT_VREPLACE
7907 if (State & VREPLACE_FLAG)
7908 vim_free(saved_line);
7909#endif
7910 curwin->w_p_list = save_list;
7911 }
7912
7913 return FALSE;
7914}
7915
7916/*
7917 * Handle CR or NL in insert mode.
7918 * Return TRUE when out of memory or can't undo.
7919 */
7920 static int
7921ins_eol(c)
7922 int c;
7923{
7924 int i;
7925
7926 if (echeck_abbr(c + ABBR_OFF))
7927 return FALSE;
7928 if (stop_arrow() == FAIL)
7929 return TRUE;
7930 undisplay_dollar();
7931
7932 /*
7933 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
7934 * character under the cursor. Only push a NUL on the replace stack,
7935 * nothing to put back when the NL is deleted.
7936 */
7937 if ((State & REPLACE_FLAG)
7938#ifdef FEAT_VREPLACE
7939 && !(State & VREPLACE_FLAG)
7940#endif
7941 )
7942 replace_push(NUL);
7943
7944 /*
7945 * In VREPLACE mode, a NL replaces the rest of the line, and starts
7946 * replacing the next line, so we push all of the characters left on the
7947 * line onto the replace stack. This is not done here though, it is done
7948 * in open_line().
7949 */
7950
7951#ifdef FEAT_RIGHTLEFT
7952# ifdef FEAT_FKMAP
7953 if (p_altkeymap && p_fkmap)
7954 fkmap(NL);
7955# endif
7956 /* NL in reverse insert will always start in the end of
7957 * current line. */
7958 if (revins_on)
7959 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7960#endif
7961
7962 AppendToRedobuff(NL_STR);
7963 i = open_line(FORWARD,
7964#ifdef FEAT_COMMENTS
7965 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
7966#endif
7967 0, old_indent);
7968 old_indent = 0;
7969#ifdef FEAT_CINDENT
7970 can_cindent = TRUE;
7971#endif
7972
7973 return (!i);
7974}
7975
7976#ifdef FEAT_DIGRAPHS
7977/*
7978 * Handle digraph in insert mode.
7979 * Returns character still to be inserted, or NUL when nothing remaining to be
7980 * done.
7981 */
7982 static int
7983ins_digraph()
7984{
7985 int c;
7986 int cc;
7987
7988 pc_status = PC_STATUS_UNSET;
7989 if (redrawing() && !char_avail())
7990 {
7991 /* may need to redraw when no more chars available now */
7992 ins_redraw();
7993
7994 edit_putchar('?', TRUE);
7995#ifdef FEAT_CMDL_INFO
7996 add_to_showcmd_c(Ctrl_K);
7997#endif
7998 }
7999
8000#ifdef USE_ON_FLY_SCROLL
8001 dont_scroll = TRUE; /* disallow scrolling here */
8002#endif
8003
8004 /* don't map the digraph chars. This also prevents the
8005 * mode message to be deleted when ESC is hit */
8006 ++no_mapping;
8007 ++allow_keys;
8008 c = safe_vgetc();
8009 --no_mapping;
8010 --allow_keys;
8011 if (IS_SPECIAL(c) || mod_mask) /* special key */
8012 {
8013#ifdef FEAT_CMDL_INFO
8014 clear_showcmd();
8015#endif
8016 insert_special(c, TRUE, FALSE);
8017 return NUL;
8018 }
8019 if (c != ESC)
8020 {
8021 if (redrawing() && !char_avail())
8022 {
8023 /* may need to redraw when no more chars available now */
8024 ins_redraw();
8025
8026 if (char2cells(c) == 1)
8027 {
8028 /* first remove the '?', otherwise it's restored when typing
8029 * an ESC next */
8030 edit_unputchar();
8031 ins_redraw();
8032 edit_putchar(c, TRUE);
8033 }
8034#ifdef FEAT_CMDL_INFO
8035 add_to_showcmd_c(c);
8036#endif
8037 }
8038 ++no_mapping;
8039 ++allow_keys;
8040 cc = safe_vgetc();
8041 --no_mapping;
8042 --allow_keys;
8043 if (cc != ESC)
8044 {
8045 AppendToRedobuff((char_u *)CTRL_V_STR);
8046 c = getdigraph(c, cc, TRUE);
8047#ifdef FEAT_CMDL_INFO
8048 clear_showcmd();
8049#endif
8050 return c;
8051 }
8052 }
8053 edit_unputchar();
8054#ifdef FEAT_CMDL_INFO
8055 clear_showcmd();
8056#endif
8057 return NUL;
8058}
8059#endif
8060
8061/*
8062 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8063 * Returns the char to be inserted, or NUL if none found.
8064 */
8065 static int
8066ins_copychar(lnum)
8067 linenr_T lnum;
8068{
8069 int c;
8070 int temp;
8071 char_u *ptr, *prev_ptr;
8072
8073 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8074 {
8075 vim_beep();
8076 return NUL;
8077 }
8078
8079 /* try to advance to the cursor column */
8080 temp = 0;
8081 ptr = ml_get(lnum);
8082 prev_ptr = ptr;
8083 validate_virtcol();
8084 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8085 {
8086 prev_ptr = ptr;
8087 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8088 }
8089 if ((colnr_T)temp > curwin->w_virtcol)
8090 ptr = prev_ptr;
8091
8092#ifdef FEAT_MBYTE
8093 c = (*mb_ptr2char)(ptr);
8094#else
8095 c = *ptr;
8096#endif
8097 if (c == NUL)
8098 vim_beep();
8099 return c;
8100}
8101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008102/*
8103 * CTRL-Y or CTRL-E typed in Insert mode.
8104 */
8105 static int
8106ins_ctrl_ey(tc)
8107 int tc;
8108{
8109 int c = tc;
8110
8111#ifdef FEAT_INS_EXPAND
8112 if (ctrl_x_mode == CTRL_X_SCROLL)
8113 {
8114 if (c == Ctrl_Y)
8115 scrolldown_clamp();
8116 else
8117 scrollup_clamp();
8118 redraw_later(VALID);
8119 }
8120 else
8121#endif
8122 {
8123 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8124 if (c != NUL)
8125 {
8126 long tw_save;
8127
8128 /* The character must be taken literally, insert like it
8129 * was typed after a CTRL-V, and pretend 'textwidth'
8130 * wasn't set. Digits, 'o' and 'x' are special after a
8131 * CTRL-V, don't use it for these. */
8132 if (c < 256 && !isalnum(c))
8133 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8134 tw_save = curbuf->b_p_tw;
8135 curbuf->b_p_tw = -1;
8136 insert_special(c, TRUE, FALSE);
8137 curbuf->b_p_tw = tw_save;
8138#ifdef FEAT_RIGHTLEFT
8139 revins_chars++;
8140 revins_legal++;
8141#endif
8142 c = Ctrl_V; /* pretend CTRL-V is last character */
8143 auto_format(FALSE, TRUE);
8144 }
8145 }
8146 return c;
8147}
8148
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149#ifdef FEAT_SMARTINDENT
8150/*
8151 * Try to do some very smart auto-indenting.
8152 * Used when inserting a "normal" character.
8153 */
8154 static void
8155ins_try_si(c)
8156 int c;
8157{
8158 pos_T *pos, old_pos;
8159 char_u *ptr;
8160 int i;
8161 int temp;
8162
8163 /*
8164 * do some very smart indenting when entering '{' or '}'
8165 */
8166 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8167 {
8168 /*
8169 * for '}' set indent equal to indent of line containing matching '{'
8170 */
8171 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8172 {
8173 old_pos = curwin->w_cursor;
8174 /*
8175 * If the matching '{' has a ')' immediately before it (ignoring
8176 * white-space), then line up with the start of the line
8177 * containing the matching '(' if there is one. This handles the
8178 * case where an "if (..\n..) {" statement continues over multiple
8179 * lines -- webb
8180 */
8181 ptr = ml_get(pos->lnum);
8182 i = pos->col;
8183 if (i > 0) /* skip blanks before '{' */
8184 while (--i > 0 && vim_iswhite(ptr[i]))
8185 ;
8186 curwin->w_cursor.lnum = pos->lnum;
8187 curwin->w_cursor.col = i;
8188 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8189 curwin->w_cursor = *pos;
8190 i = get_indent();
8191 curwin->w_cursor = old_pos;
8192#ifdef FEAT_VREPLACE
8193 if (State & VREPLACE_FLAG)
8194 change_indent(INDENT_SET, i, FALSE, NUL);
8195 else
8196#endif
8197 (void)set_indent(i, SIN_CHANGED);
8198 }
8199 else if (curwin->w_cursor.col > 0)
8200 {
8201 /*
8202 * when inserting '{' after "O" reduce indent, but not
8203 * more than indent of previous line
8204 */
8205 temp = TRUE;
8206 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8207 {
8208 old_pos = curwin->w_cursor;
8209 i = get_indent();
8210 while (curwin->w_cursor.lnum > 1)
8211 {
8212 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8213
8214 /* ignore empty lines and lines starting with '#'. */
8215 if (*ptr != '#' && *ptr != NUL)
8216 break;
8217 }
8218 if (get_indent() >= i)
8219 temp = FALSE;
8220 curwin->w_cursor = old_pos;
8221 }
8222 if (temp)
8223 shift_line(TRUE, FALSE, 1);
8224 }
8225 }
8226
8227 /*
8228 * set indent of '#' always to 0
8229 */
8230 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8231 {
8232 /* remember current indent for next line */
8233 old_indent = get_indent();
8234 (void)set_indent(0, SIN_CHANGED);
8235 }
8236
8237 /* Adjust ai_col, the char at this position can be deleted. */
8238 if (ai_col > curwin->w_cursor.col)
8239 ai_col = curwin->w_cursor.col;
8240}
8241#endif
8242
8243/*
8244 * Get the value that w_virtcol would have when 'list' is off.
8245 * Unless 'cpo' contains the 'L' flag.
8246 */
8247 static colnr_T
8248get_nolist_virtcol()
8249{
8250 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8251 return getvcol_nolist(&curwin->w_cursor);
8252 validate_virtcol();
8253 return curwin->w_virtcol;
8254}