blob: 0a770a7b4f7d67ef4a6f78328ac62ca2afc94e0c [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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225static int ins_need_undo; /* call u_save() before inserting a
226 char. Set when edit() is called.
227 after that arrow_used is used. */
228
229static int did_add_space = FALSE; /* auto_format() added an extra space
230 under the cursor */
231
232/*
233 * edit(): Start inserting text.
234 *
235 * "cmdchar" can be:
236 * 'i' normal insert command
237 * 'a' normal append command
238 * 'R' replace command
239 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
240 * but still only one <CR> is inserted. The <Esc> is not used for redo.
241 * 'g' "gI" command.
242 * 'V' "gR" command for Virtual Replace mode.
243 * 'v' "gr" command for single character Virtual Replace mode.
244 *
245 * This function is not called recursively. For CTRL-O commands, it returns
246 * and lets the caller handle the Normal-mode command.
247 *
248 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
249 */
250 int
251edit(cmdchar, startln, count)
252 int cmdchar;
253 int startln; /* if set, insert at start of line */
254 long count;
255{
256 int c = 0;
257 char_u *ptr;
258 int lastc;
259 colnr_T mincol;
260 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 int i;
262 int did_backspace = TRUE; /* previous char was backspace */
263#ifdef FEAT_CINDENT
264 int line_is_white = FALSE; /* line is empty before insert */
265#endif
266 linenr_T old_topline = 0; /* topline before insertion */
267#ifdef FEAT_DIFF
268 int old_topfill = -1;
269#endif
270 int inserted_space = FALSE; /* just inserted a space */
271 int replaceState = REPLACE;
272 int did_restart_edit = restart_edit;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000273 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275 /* sleep before redrawing, needed for "CTRL-O :" that results in an
276 * error message */
277 check_for_delay(TRUE);
278
279#ifdef HAVE_SANDBOX
280 /* Don't allow inserting in the sandbox. */
281 if (sandbox != 0)
282 {
283 EMSG(_(e_sandbox));
284 return FALSE;
285 }
286#endif
287
288#ifdef FEAT_INS_EXPAND
289 ins_compl_clear(); /* clear stuff for CTRL-X mode */
290#endif
291
Bram Moolenaar843ee412004-06-30 16:16:41 +0000292#ifdef FEAT_AUTOCMD
293 /*
294 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
295 */
296 if (cmdchar != 'r' && cmdchar != 'v')
297 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000298# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000299 if (cmdchar == 'R')
300 ptr = (char_u *)"r";
301 else if (cmdchar == 'V')
302 ptr = (char_u *)"v";
303 else
304 ptr = (char_u *)"i";
305 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000306# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000307 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
308 }
309#endif
310
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#ifdef FEAT_MOUSE
312 /*
313 * When doing a paste with the middle mouse button, Insstart is set to
314 * where the paste started.
315 */
316 if (where_paste_started.lnum != 0)
317 Insstart = where_paste_started;
318 else
319#endif
320 {
321 Insstart = curwin->w_cursor;
322 if (startln)
323 Insstart.col = 0;
324 }
325 Insstart_textlen = linetabsize(ml_get_curline());
326 Insstart_blank_vcol = MAXCOL;
327 if (!did_ai)
328 ai_col = 0;
329
330 if (cmdchar != NUL && restart_edit == 0)
331 {
332 ResetRedobuff();
333 AppendNumberToRedobuff(count);
334#ifdef FEAT_VREPLACE
335 if (cmdchar == 'V' || cmdchar == 'v')
336 {
337 /* "gR" or "gr" command */
338 AppendCharToRedobuff('g');
339 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
340 }
341 else
342#endif
343 {
344 AppendCharToRedobuff(cmdchar);
345 if (cmdchar == 'g') /* "gI" command */
346 AppendCharToRedobuff('I');
347 else if (cmdchar == 'r') /* "r<CR>" command */
348 count = 1; /* insert only one <CR> */
349 }
350 }
351
352 if (cmdchar == 'R')
353 {
354#ifdef FEAT_FKMAP
355 if (p_fkmap && p_ri)
356 {
357 beep_flush();
358 EMSG(farsi_text_3); /* encoded in Farsi */
359 State = INSERT;
360 }
361 else
362#endif
363 State = REPLACE;
364 }
365#ifdef FEAT_VREPLACE
366 else if (cmdchar == 'V' || cmdchar == 'v')
367 {
368 State = VREPLACE;
369 replaceState = VREPLACE;
370 orig_line_count = curbuf->b_ml.ml_line_count;
371 vr_lines_changed = 1;
372 }
373#endif
374 else
375 State = INSERT;
376
377 stop_insert_mode = FALSE;
378
379 /*
380 * Need to recompute the cursor position, it might move when the cursor is
381 * on a TAB or special character.
382 */
383 curs_columns(TRUE);
384
385 /*
386 * Enable langmap or IME, indicated by 'iminsert'.
387 * Note that IME may enabled/disabled without us noticing here, thus the
388 * 'iminsert' value may not reflect what is actually used. It is updated
389 * when hitting <Esc>.
390 */
391 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
392 State |= LANGMAP;
393#ifdef USE_IM_CONTROL
394 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
395#endif
396
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397#ifdef FEAT_MOUSE
398 setmouse();
399#endif
400#ifdef FEAT_CMDL_INFO
401 clear_showcmd();
402#endif
403#ifdef FEAT_RIGHTLEFT
404 /* there is no reverse replace mode */
405 revins_on = (State == INSERT && p_ri);
406 if (revins_on)
407 undisplay_dollar();
408 revins_chars = 0;
409 revins_legal = 0;
410 revins_scol = -1;
411#endif
412
413 /*
414 * Handle restarting Insert mode.
415 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
416 * restart_edit non-zero, and something in the stuff buffer.
417 */
418 if (restart_edit != 0 && stuff_empty())
419 {
420#ifdef FEAT_MOUSE
421 /*
422 * After a paste we consider text typed to be part of the insert for
423 * the pasted text. You can backspace over the pasted text too.
424 */
425 if (where_paste_started.lnum)
426 arrow_used = FALSE;
427 else
428#endif
429 arrow_used = TRUE;
430 restart_edit = 0;
431
432 /*
433 * If the cursor was after the end-of-line before the CTRL-O and it is
434 * now at the end-of-line, put it after the end-of-line (this is not
435 * correct in very rare cases).
436 * Also do this if curswant is greater than the current virtual
437 * column. Eg after "^O$" or "^O80|".
438 */
439 validate_virtcol();
440 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000441 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 || curwin->w_curswant > curwin->w_virtcol)
443 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
444 {
445 if (ptr[1] == NUL)
446 ++curwin->w_cursor.col;
447#ifdef FEAT_MBYTE
448 else if (has_mbyte)
449 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000450 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 if (ptr[i] == NUL)
452 curwin->w_cursor.col += i;
453 }
454#endif
455 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000456 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 }
458 else
459 arrow_used = FALSE;
460
461 /* we are in insert mode now, don't need to start it anymore */
462 need_start_insertmode = FALSE;
463
464 /* Need to save the line for undo before inserting the first char. */
465 ins_need_undo = TRUE;
466
467#ifdef FEAT_MOUSE
468 where_paste_started.lnum = 0;
469#endif
470#ifdef FEAT_CINDENT
471 can_cindent = TRUE;
472#endif
473#ifdef FEAT_FOLDING
474 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
475 * restarting. */
476 if (!p_im && did_restart_edit == 0)
477 foldOpenCursor();
478#endif
479
480 /*
481 * If 'showmode' is set, show the current (insert/replace/..) mode.
482 * A warning message for changing a readonly file is given here, before
483 * actually changing anything. It's put after the mode, if any.
484 */
485 i = 0;
486 if (p_smd)
487 i = showmode();
488
489 if (!p_im && did_restart_edit == 0)
490 change_warning(i + 1);
491
492#ifdef CURSOR_SHAPE
493 ui_cursor_shape(); /* may show different cursor shape */
494#endif
495#ifdef FEAT_DIGRAPHS
496 do_digraph(-1); /* clear digraphs */
497#endif
498
499/*
500 * Get the current length of the redo buffer, those characters have to be
501 * skipped if we want to get to the inserted characters.
502 */
503 ptr = get_inserted();
504 if (ptr == NULL)
505 new_insert_skip = 0;
506 else
507 {
508 new_insert_skip = (int)STRLEN(ptr);
509 vim_free(ptr);
510 }
511
512 old_indent = 0;
513
514 /*
515 * Main loop in Insert mode: repeat until Insert mode is left.
516 */
517 for (;;)
518 {
519#ifdef FEAT_RIGHTLEFT
520 if (!revins_legal)
521 revins_scol = -1; /* reset on illegal motions */
522 else
523 revins_legal = 0;
524#endif
525 if (arrow_used) /* don't repeat insert when arrow key used */
526 count = 0;
527
528 if (stop_insert_mode)
529 {
530 /* ":stopinsert" used or 'insertmode' reset */
531 count = 0;
532 goto doESCkey;
533 }
534
535 /* set curwin->w_curswant for next K_DOWN or K_UP */
536 if (!arrow_used)
537 curwin->w_set_curswant = TRUE;
538
539 /* If there is no typeahead may check for timestamps (e.g., for when a
540 * menu invoked a shell command). */
541 if (stuff_empty())
542 {
543 did_check_timestamps = FALSE;
544 if (need_check_timestamps)
545 check_timestamps(FALSE);
546 }
547
548 /*
549 * When emsg() was called msg_scroll will have been set.
550 */
551 msg_scroll = FALSE;
552
553#ifdef FEAT_GUI
554 /* When 'mousefocus' is set a mouse movement may have taken us to
555 * another window. "need_mouse_correct" may then be set because of an
556 * autocommand. */
557 if (need_mouse_correct)
558 gui_mouse_correct();
559#endif
560
561#ifdef FEAT_FOLDING
562 /* Open fold at the cursor line, according to 'foldopen'. */
563 if (fdo_flags & FDO_INSERT)
564 foldOpenCursor();
565 /* Close folds where the cursor isn't, according to 'foldclose' */
566 if (!char_avail())
567 foldCheckClose();
568#endif
569
570 /*
571 * If we inserted a character at the last position of the last line in
572 * the window, scroll the window one line up. This avoids an extra
573 * redraw.
574 * This is detected when the cursor column is smaller after inserting
575 * something.
576 * Don't do this when the topline changed already, it has
577 * already been adjusted (by insertchar() calling open_line())).
578 */
579 if (curbuf->b_mod_set
580 && curwin->w_p_wrap
581 && !did_backspace
582 && curwin->w_topline == old_topline
583#ifdef FEAT_DIFF
584 && curwin->w_topfill == old_topfill
585#endif
586 )
587 {
588 mincol = curwin->w_wcol;
589 validate_cursor_col();
590
591 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
592 && curwin->w_wrow == W_WINROW(curwin)
593 + curwin->w_height - 1 - p_so
594 && (curwin->w_cursor.lnum != curwin->w_topline
595#ifdef FEAT_DIFF
596 || curwin->w_topfill > 0
597#endif
598 ))
599 {
600#ifdef FEAT_DIFF
601 if (curwin->w_topfill > 0)
602 --curwin->w_topfill;
603 else
604#endif
605#ifdef FEAT_FOLDING
606 if (hasFolding(curwin->w_topline, NULL, &old_topline))
607 set_topline(curwin, old_topline + 1);
608 else
609#endif
610 set_topline(curwin, curwin->w_topline + 1);
611 }
612 }
613
614 /* May need to adjust w_topline to show the cursor. */
615 update_topline();
616
617 did_backspace = FALSE;
618
619 validate_cursor(); /* may set must_redraw */
620
621 /*
622 * Redraw the display when no characters are waiting.
623 * Also shows mode, ruler and positions cursor.
624 */
625 ins_redraw();
626
627#ifdef FEAT_SCROLLBIND
628 if (curwin->w_p_scb)
629 do_check_scrollbind(TRUE);
630#endif
631
632 update_curswant();
633 old_topline = curwin->w_topline;
634#ifdef FEAT_DIFF
635 old_topfill = curwin->w_topfill;
636#endif
637
638#ifdef USE_ON_FLY_SCROLL
639 dont_scroll = FALSE; /* allow scrolling here */
640#endif
641
642 /*
643 * Get a character for Insert mode.
644 */
645 lastc = c; /* remember previous char for CTRL-D */
646 c = safe_vgetc();
647
648#ifdef FEAT_RIGHTLEFT
649 if (p_hkmap && KeyTyped)
650 c = hkmap(c); /* Hebrew mode mapping */
651#endif
652#ifdef FEAT_FKMAP
653 if (p_fkmap && KeyTyped)
654 c = fkmap(c); /* Farsi mode mapping */
655#endif
656
657#ifdef FEAT_INS_EXPAND
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000658 /* When the popup menu is visible cursor keys change the selection. */
659 if (c == K_UP && pum_visible())
660 c = Ctrl_P;
661 if (c == K_DOWN && pum_visible())
662 c = Ctrl_N;
663
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
665 * it does fix up the text when finishing completion. */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000666 if (c != K_IGNORE)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000667 {
668 if (ins_compl_prep(c))
669 continue;
670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671#endif
672
Bram Moolenaar488c6512005-08-11 20:09:58 +0000673 /* CTRL-\ CTRL-N goes to Normal mode,
674 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
675 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 if (c == Ctrl_BSL)
677 {
678 /* may need to redraw when no more chars available now */
679 ins_redraw();
680 ++no_mapping;
681 ++allow_keys;
682 c = safe_vgetc();
683 --no_mapping;
684 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000685 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000687 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 vungetc(c);
689 c = Ctrl_BSL;
690 }
691 else if (c == Ctrl_G && p_im)
692 continue;
693 else
694 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000695 if (c == Ctrl_O)
696 {
697 ins_ctrl_o();
698 ins_at_eol = FALSE; /* cursor keeps its column */
699 nomove = TRUE;
700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 count = 0;
702 goto doESCkey;
703 }
704 }
705
706#ifdef FEAT_DIGRAPHS
707 c = do_digraph(c);
708#endif
709
710#ifdef FEAT_INS_EXPAND
711 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
712 goto docomplete;
713#endif
714 if (c == Ctrl_V || c == Ctrl_Q)
715 {
716 ins_ctrl_v();
717 c = Ctrl_V; /* pretend CTRL-V is last typed character */
718 continue;
719 }
720
721#ifdef FEAT_CINDENT
722 if (cindent_on()
723# ifdef FEAT_INS_EXPAND
724 && ctrl_x_mode == 0
725# endif
726 )
727 {
728 /* A key name preceded by a bang means this key is not to be
729 * inserted. Skip ahead to the re-indenting below.
730 * A key name preceded by a star means that indenting has to be
731 * done before inserting the key. */
732 line_is_white = inindent(0);
733 if (in_cinkeys(c, '!', line_is_white))
734 goto force_cindent;
735 if (can_cindent && in_cinkeys(c, '*', line_is_white)
736 && stop_arrow() == OK)
737 do_c_expr_indent();
738 }
739#endif
740
741#ifdef FEAT_RIGHTLEFT
742 if (curwin->w_p_rl)
743 switch (c)
744 {
745 case K_LEFT: c = K_RIGHT; break;
746 case K_S_LEFT: c = K_S_RIGHT; break;
747 case K_C_LEFT: c = K_C_RIGHT; break;
748 case K_RIGHT: c = K_LEFT; break;
749 case K_S_RIGHT: c = K_S_LEFT; break;
750 case K_C_RIGHT: c = K_C_LEFT; break;
751 }
752#endif
753
754#ifdef FEAT_VISUAL
755 /*
756 * If 'keymodel' contains "startsel", may start selection. If it
757 * does, a CTRL-O and c will be stuffed, we need to get these
758 * characters.
759 */
760 if (ins_start_select(c))
761 continue;
762#endif
763
764 /*
765 * The big switch to handle a character in insert mode.
766 */
767 switch (c)
768 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000769 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (echeck_abbr(ESC + ABBR_OFF))
771 break;
772 /*FALLTHROUGH*/
773
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000774 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#ifdef FEAT_CMDWIN
776 if (c == Ctrl_C && cmdwin_type != 0)
777 {
778 /* Close the cmdline window. */
779 cmdwin_result = K_IGNORE;
780 got_int = FALSE; /* don't stop executing autocommands et al. */
781 goto doESCkey;
782 }
783#endif
784
785#ifdef UNIX
786do_intr:
787#endif
788 /* when 'insertmode' set, and not halfway a mapping, don't leave
789 * Insert mode */
790 if (goto_im())
791 {
792 if (got_int)
793 {
794 (void)vgetc(); /* flush all buffers */
795 got_int = FALSE;
796 }
797 else
798 vim_beep();
799 break;
800 }
801doESCkey:
802 /*
803 * This is the ONLY return from edit()!
804 */
805 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
806 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000807 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 o_lnum = curwin->w_cursor.lnum;
809
Bram Moolenaar488c6512005-08-11 20:09:58 +0000810 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000811 {
812#ifdef FEAT_AUTOCMD
813 if (cmdchar != 'r' && cmdchar != 'v')
814 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
815 FALSE, curbuf);
816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 continue;
820
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000821 case Ctrl_Z: /* suspend when 'insertmode' set */
822 if (!p_im)
823 goto normalchar; /* insert CTRL-Z as normal char */
824 stuffReadbuff((char_u *)":st\r");
825 c = Ctrl_O;
826 /*FALLTHROUGH*/
827
828 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000829#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000830 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000831 goto docomplete;
832#endif
833 if (echeck_abbr(Ctrl_O + ABBR_OFF))
834 break;
835 ins_ctrl_o();
836 count = 0;
837 goto doESCkey;
838
Bram Moolenaar572cb562005-08-05 21:35:02 +0000839 case K_INS: /* toggle insert/replace mode */
840 case K_KINS:
841 ins_insert(replaceState);
842 break;
843
844 case K_SELECT: /* end of Select mode mapping - ignore */
845 break;
846
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000847#ifdef FEAT_SNIFF
848 case K_SNIFF: /* Sniff command received */
849 stuffcharReadbuff(K_SNIFF);
850 goto doESCkey;
851#endif
852
853 case K_HELP: /* Help key works like <ESC> <Help> */
854 case K_F1:
855 case K_XF1:
856 stuffcharReadbuff(K_HELP);
857 if (p_im)
858 need_start_insertmode = TRUE;
859 goto doESCkey;
860
861#ifdef FEAT_NETBEANS_INTG
862 case K_F21: /* NetBeans command */
863 ++no_mapping; /* don't map the next key hits */
864 i = safe_vgetc();
865 --no_mapping;
866 netbeans_keycommand(i);
867 break;
868#endif
869
870 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 case NUL:
872 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000873 /* For ^@ the trailing ESC will end the insert, unless there is an
874 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
876 && c != Ctrl_A && !p_im)
877 goto doESCkey; /* quit insert mode */
878 inserted_space = FALSE;
879 break;
880
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000881 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 ins_reg();
883 auto_format(FALSE, TRUE);
884 inserted_space = FALSE;
885 break;
886
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000887 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 ins_ctrl_g();
889 break;
890
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000891 case Ctrl_HAT: /* switch input mode and/or langmap */
892 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 break;
894
895#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000896 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (!p_ari)
898 goto normalchar;
899 ins_ctrl_();
900 break;
901#endif
902
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000903 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
905 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
906 goto docomplete;
907#endif
908 /* FALLTHROUGH */
909
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000910 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911# ifdef FEAT_INS_EXPAND
912 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
913 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000914 if (has_compl_option(FALSE))
915 goto docomplete;
916 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918# endif
919 ins_shift(c, lastc);
920 auto_format(FALSE, TRUE);
921 inserted_space = FALSE;
922 break;
923
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000924 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 case K_KDEL:
926 ins_del();
927 auto_format(FALSE, TRUE);
928 break;
929
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000930 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 case Ctrl_H:
932 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
933 auto_format(FALSE, TRUE);
934 break;
935
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000936 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
938 auto_format(FALSE, TRUE);
939 break;
940
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000941 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000942# ifdef FEAT_COMPL_FUNC
943 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000944 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000945 goto docomplete;
946# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
948 auto_format(FALSE, TRUE);
949 inserted_space = FALSE;
950 break;
951
952#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000953 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 case K_LEFTMOUSE_NM:
955 case K_LEFTDRAG:
956 case K_LEFTRELEASE:
957 case K_LEFTRELEASE_NM:
958 case K_MIDDLEMOUSE:
959 case K_MIDDLEDRAG:
960 case K_MIDDLERELEASE:
961 case K_RIGHTMOUSE:
962 case K_RIGHTDRAG:
963 case K_RIGHTRELEASE:
964 case K_X1MOUSE:
965 case K_X1DRAG:
966 case K_X1RELEASE:
967 case K_X2MOUSE:
968 case K_X2DRAG:
969 case K_X2RELEASE:
970 ins_mouse(c);
971 break;
972
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000973 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 ins_mousescroll(FALSE);
975 break;
976
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000977 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 ins_mousescroll(TRUE);
979 break;
980#endif
981
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000982 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 break;
984
985#ifdef FEAT_GUI
986 case K_VER_SCROLLBAR:
987 ins_scroll();
988 break;
989
990 case K_HOR_SCROLLBAR:
991 ins_horscroll();
992 break;
993#endif
994
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000995 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 case K_S_HOME:
998 case K_C_HOME:
999 ins_home(c);
1000 break;
1001
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001002 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 case K_S_END:
1005 case K_C_END:
1006 ins_end(c);
1007 break;
1008
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001009 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001010 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1011 ins_s_left();
1012 else
1013 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 break;
1015
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001016 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 case K_C_LEFT:
1018 ins_s_left();
1019 break;
1020
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001021 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001022 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1023 ins_s_right();
1024 else
1025 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 break;
1027
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001028 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 case K_C_RIGHT:
1030 ins_s_right();
1031 break;
1032
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001033 case K_UP: /* <Up> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001034 if (mod_mask & MOD_MASK_SHIFT)
1035 ins_pageup();
1036 else
1037 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 break;
1039
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001040 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 case K_PAGEUP:
1042 case K_KPAGEUP:
1043 ins_pageup();
1044 break;
1045
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001046 case K_DOWN: /* <Down> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001047 if (mod_mask & MOD_MASK_SHIFT)
1048 ins_pagedown();
1049 else
1050 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 break;
1052
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001053 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 case K_PAGEDOWN:
1055 case K_KPAGEDOWN:
1056 ins_pagedown();
1057 break;
1058
1059#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001060 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 ins_drop();
1062 break;
1063#endif
1064
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001065 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 c = TAB;
1067 /* FALLTHROUGH */
1068
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001069 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1071 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1072 goto docomplete;
1073#endif
1074 inserted_space = FALSE;
1075 if (ins_tab())
1076 goto normalchar; /* insert TAB as a normal char */
1077 auto_format(FALSE, TRUE);
1078 break;
1079
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001080 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 c = CAR;
1082 /* FALLTHROUGH */
1083 case CAR:
1084 case NL:
1085#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1086 /* In a quickfix window a <CR> jumps to the error under the
1087 * cursor. */
1088 if (bt_quickfix(curbuf) && c == CAR)
1089 {
1090 do_cmdline_cmd((char_u *)".cc");
1091 break;
1092 }
1093#endif
1094#ifdef FEAT_CMDWIN
1095 if (cmdwin_type != 0)
1096 {
1097 /* Execute the command in the cmdline window. */
1098 cmdwin_result = CAR;
1099 goto doESCkey;
1100 }
1101#endif
1102 if (ins_eol(c) && !p_im)
1103 goto doESCkey; /* out of memory */
1104 auto_format(FALSE, FALSE);
1105 inserted_space = FALSE;
1106 break;
1107
1108#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001109 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110# ifdef FEAT_INS_EXPAND
1111 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1112 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001113 if (has_compl_option(TRUE))
1114 goto docomplete;
1115 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 }
1117# endif
1118# ifdef FEAT_DIGRAPHS
1119 c = ins_digraph();
1120 if (c == NUL)
1121 break;
1122# endif
1123 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
1126#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001127 case Ctrl_X: /* Enter CTRL-X mode */
1128 ins_ctrl_x();
1129 break;
1130
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001131 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 if (ctrl_x_mode != CTRL_X_TAGS)
1133 goto normalchar;
1134 goto docomplete;
1135
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001136 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 if (ctrl_x_mode != CTRL_X_FILES)
1138 goto normalchar;
1139 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001140
1141 case 's': /* Spelling completion after ^X */
1142 case Ctrl_S:
1143 if (ctrl_x_mode != CTRL_X_SPELL)
1144 goto normalchar;
1145 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146#endif
1147
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001148 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149#ifdef FEAT_INS_EXPAND
1150 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1151#endif
1152 {
1153 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1154 if (p_im)
1155 {
1156 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1157 break;
1158 goto doESCkey;
1159 }
1160 goto normalchar;
1161 }
1162#ifdef FEAT_INS_EXPAND
1163 /* FALLTHROUGH */
1164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 case Ctrl_N:
1167 /* if 'complete' is empty then plain ^P is no longer special,
1168 * but it is under other ^X modes */
1169 if (*curbuf->b_p_cpt == NUL
1170 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001171 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 goto normalchar;
1173
1174docomplete:
1175 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001176 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 break;
1178#endif /* FEAT_INS_EXPAND */
1179
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001180 case Ctrl_Y: /* copy from previous line or scroll down */
1181 case Ctrl_E: /* copy from next line or scroll up */
1182 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 break;
1184
1185 default:
1186#ifdef UNIX
1187 if (c == intr_char) /* special interrupt char */
1188 goto do_intr;
1189#endif
1190
1191 /*
1192 * Insert a nomal character.
1193 */
1194normalchar:
1195#ifdef FEAT_SMARTINDENT
1196 /* Try to perform smart-indenting. */
1197 ins_try_si(c);
1198#endif
1199
1200 if (c == ' ')
1201 {
1202 inserted_space = TRUE;
1203#ifdef FEAT_CINDENT
1204 if (inindent(0))
1205 can_cindent = FALSE;
1206#endif
1207 if (Insstart_blank_vcol == MAXCOL
1208 && curwin->w_cursor.lnum == Insstart.lnum)
1209 Insstart_blank_vcol = get_nolist_virtcol();
1210 }
1211
1212 if (vim_iswordc(c) || !echeck_abbr(
1213#ifdef FEAT_MBYTE
1214 /* Add ABBR_OFF for characters above 0x100, this is
1215 * what check_abbr() expects. */
1216 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1217#endif
1218 c))
1219 {
1220 insert_special(c, FALSE, FALSE);
1221#ifdef FEAT_RIGHTLEFT
1222 revins_legal++;
1223 revins_chars++;
1224#endif
1225 }
1226
1227 auto_format(FALSE, TRUE);
1228
1229#ifdef FEAT_FOLDING
1230 /* When inserting a character the cursor line must never be in a
1231 * closed fold. */
1232 foldOpenCursor();
1233#endif
1234 break;
1235 } /* end of switch (c) */
1236
1237 /* If the cursor was moved we didn't just insert a space */
1238 if (arrow_used)
1239 inserted_space = FALSE;
1240
1241#ifdef FEAT_CINDENT
1242 if (can_cindent && cindent_on()
1243# ifdef FEAT_INS_EXPAND
1244 && ctrl_x_mode == 0
1245# endif
1246 )
1247 {
1248force_cindent:
1249 /*
1250 * Indent now if a key was typed that is in 'cinkeys'.
1251 */
1252 if (in_cinkeys(c, ' ', line_is_white))
1253 {
1254 if (stop_arrow() == OK)
1255 /* re-indent the current line */
1256 do_c_expr_indent();
1257 }
1258 }
1259#endif /* FEAT_CINDENT */
1260
1261 } /* for (;;) */
1262 /* NOTREACHED */
1263}
1264
1265/*
1266 * Redraw for Insert mode.
1267 * This is postponed until getting the next character to make '$' in the 'cpo'
1268 * option work correctly.
1269 * Only redraw when there are no characters available. This speeds up
1270 * inserting sequences of characters (e.g., for CTRL-R).
1271 */
1272 static void
1273ins_redraw()
1274{
1275 if (!char_avail())
1276 {
1277 if (must_redraw)
1278 update_screen(0);
1279 else if (clear_cmdline || redraw_cmdline)
1280 showmode(); /* clear cmdline and show mode */
1281 showruler(FALSE);
1282 setcursor();
1283 emsg_on_display = FALSE; /* may remove error message now */
1284 }
1285}
1286
1287/*
1288 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1289 */
1290 static void
1291ins_ctrl_v()
1292{
1293 int c;
1294
1295 /* may need to redraw when no more chars available now */
1296 ins_redraw();
1297
1298 if (redrawing() && !char_avail())
1299 edit_putchar('^', TRUE);
1300 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1301
1302#ifdef FEAT_CMDL_INFO
1303 add_to_showcmd_c(Ctrl_V);
1304#endif
1305
1306 c = get_literal();
1307#ifdef FEAT_CMDL_INFO
1308 clear_showcmd();
1309#endif
1310 insert_special(c, FALSE, TRUE);
1311#ifdef FEAT_RIGHTLEFT
1312 revins_chars++;
1313 revins_legal++;
1314#endif
1315}
1316
1317/*
1318 * Put a character directly onto the screen. It's not stored in a buffer.
1319 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1320 */
1321static int pc_status;
1322#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1323#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1324#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1325#define PC_STATUS_SET 3 /* pc_bytes was filled */
1326#ifdef FEAT_MBYTE
1327static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1328#else
1329static char_u pc_bytes[2]; /* saved bytes */
1330#endif
1331static int pc_attr;
1332static int pc_row;
1333static int pc_col;
1334
1335 void
1336edit_putchar(c, highlight)
1337 int c;
1338 int highlight;
1339{
1340 int attr;
1341
1342 if (ScreenLines != NULL)
1343 {
1344 update_topline(); /* just in case w_topline isn't valid */
1345 validate_cursor();
1346 if (highlight)
1347 attr = hl_attr(HLF_8);
1348 else
1349 attr = 0;
1350 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1351 pc_col = W_WINCOL(curwin);
1352#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1353 pc_status = PC_STATUS_UNSET;
1354#endif
1355#ifdef FEAT_RIGHTLEFT
1356 if (curwin->w_p_rl)
1357 {
1358 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1359# ifdef FEAT_MBYTE
1360 if (has_mbyte)
1361 {
1362 int fix_col = mb_fix_col(pc_col, pc_row);
1363
1364 if (fix_col != pc_col)
1365 {
1366 screen_putchar(' ', pc_row, fix_col, attr);
1367 --curwin->w_wcol;
1368 pc_status = PC_STATUS_RIGHT;
1369 }
1370 }
1371# endif
1372 }
1373 else
1374#endif
1375 {
1376 pc_col += curwin->w_wcol;
1377#ifdef FEAT_MBYTE
1378 if (mb_lefthalve(pc_row, pc_col))
1379 pc_status = PC_STATUS_LEFT;
1380#endif
1381 }
1382
1383 /* save the character to be able to put it back */
1384#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1385 if (pc_status == PC_STATUS_UNSET)
1386#endif
1387 {
1388 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1389 pc_status = PC_STATUS_SET;
1390 }
1391 screen_putchar(c, pc_row, pc_col, attr);
1392 }
1393}
1394
1395/*
1396 * Undo the previous edit_putchar().
1397 */
1398 void
1399edit_unputchar()
1400{
1401 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1402 {
1403#if defined(FEAT_MBYTE)
1404 if (pc_status == PC_STATUS_RIGHT)
1405 ++curwin->w_wcol;
1406 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1407 redrawWinline(curwin->w_cursor.lnum, FALSE);
1408 else
1409#endif
1410 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1411 }
1412}
1413
1414/*
1415 * Called when p_dollar is set: display a '$' at the end of the changed text
1416 * Only works when cursor is in the line that changes.
1417 */
1418 void
1419display_dollar(col)
1420 colnr_T col;
1421{
1422 colnr_T save_col;
1423
1424 if (!redrawing())
1425 return;
1426
1427 cursor_off();
1428 save_col = curwin->w_cursor.col;
1429 curwin->w_cursor.col = col;
1430#ifdef FEAT_MBYTE
1431 if (has_mbyte)
1432 {
1433 char_u *p;
1434
1435 /* If on the last byte of a multi-byte move to the first byte. */
1436 p = ml_get_curline();
1437 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1438 }
1439#endif
1440 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1441 if (curwin->w_wcol < W_WIDTH(curwin))
1442 {
1443 edit_putchar('$', FALSE);
1444 dollar_vcol = curwin->w_virtcol;
1445 }
1446 curwin->w_cursor.col = save_col;
1447}
1448
1449/*
1450 * Call this function before moving the cursor from the normal insert position
1451 * in insert mode.
1452 */
1453 static void
1454undisplay_dollar()
1455{
1456 if (dollar_vcol)
1457 {
1458 dollar_vcol = 0;
1459 redrawWinline(curwin->w_cursor.lnum, FALSE);
1460 }
1461}
1462
1463/*
1464 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1465 * Keep the cursor on the same character.
1466 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1467 * type == INDENT_DEC decrease indent (for CTRL-D)
1468 * type == INDENT_SET set indent to "amount"
1469 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1470 */
1471 void
1472change_indent(type, amount, round, replaced)
1473 int type;
1474 int amount;
1475 int round;
1476 int replaced; /* replaced character, put on replace stack */
1477{
1478 int vcol;
1479 int last_vcol;
1480 int insstart_less; /* reduction for Insstart.col */
1481 int new_cursor_col;
1482 int i;
1483 char_u *ptr;
1484 int save_p_list;
1485 int start_col;
1486 colnr_T vc;
1487#ifdef FEAT_VREPLACE
1488 colnr_T orig_col = 0; /* init for GCC */
1489 char_u *new_line, *orig_line = NULL; /* init for GCC */
1490
1491 /* VREPLACE mode needs to know what the line was like before changing */
1492 if (State & VREPLACE_FLAG)
1493 {
1494 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1495 orig_col = curwin->w_cursor.col;
1496 }
1497#endif
1498
1499 /* for the following tricks we don't want list mode */
1500 save_p_list = curwin->w_p_list;
1501 curwin->w_p_list = FALSE;
1502 vc = getvcol_nolist(&curwin->w_cursor);
1503 vcol = vc;
1504
1505 /*
1506 * For Replace mode we need to fix the replace stack later, which is only
1507 * possible when the cursor is in the indent. Remember the number of
1508 * characters before the cursor if it's possible.
1509 */
1510 start_col = curwin->w_cursor.col;
1511
1512 /* determine offset from first non-blank */
1513 new_cursor_col = curwin->w_cursor.col;
1514 beginline(BL_WHITE);
1515 new_cursor_col -= curwin->w_cursor.col;
1516
1517 insstart_less = curwin->w_cursor.col;
1518
1519 /*
1520 * If the cursor is in the indent, compute how many screen columns the
1521 * cursor is to the left of the first non-blank.
1522 */
1523 if (new_cursor_col < 0)
1524 vcol = get_indent() - vcol;
1525
1526 if (new_cursor_col > 0) /* can't fix replace stack */
1527 start_col = -1;
1528
1529 /*
1530 * Set the new indent. The cursor will be put on the first non-blank.
1531 */
1532 if (type == INDENT_SET)
1533 (void)set_indent(amount, SIN_CHANGED);
1534 else
1535 {
1536#ifdef FEAT_VREPLACE
1537 int save_State = State;
1538
1539 /* Avoid being called recursively. */
1540 if (State & VREPLACE_FLAG)
1541 State = INSERT;
1542#endif
1543 shift_line(type == INDENT_DEC, round, 1);
1544#ifdef FEAT_VREPLACE
1545 State = save_State;
1546#endif
1547 }
1548 insstart_less -= curwin->w_cursor.col;
1549
1550 /*
1551 * Try to put cursor on same character.
1552 * If the cursor is at or after the first non-blank in the line,
1553 * compute the cursor column relative to the column of the first
1554 * non-blank character.
1555 * If we are not in insert mode, leave the cursor on the first non-blank.
1556 * If the cursor is before the first non-blank, position it relative
1557 * to the first non-blank, counted in screen columns.
1558 */
1559 if (new_cursor_col >= 0)
1560 {
1561 /*
1562 * When changing the indent while the cursor is touching it, reset
1563 * Insstart_col to 0.
1564 */
1565 if (new_cursor_col == 0)
1566 insstart_less = MAXCOL;
1567 new_cursor_col += curwin->w_cursor.col;
1568 }
1569 else if (!(State & INSERT))
1570 new_cursor_col = curwin->w_cursor.col;
1571 else
1572 {
1573 /*
1574 * Compute the screen column where the cursor should be.
1575 */
1576 vcol = get_indent() - vcol;
1577 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1578
1579 /*
1580 * Advance the cursor until we reach the right screen column.
1581 */
1582 vcol = last_vcol = 0;
1583 new_cursor_col = -1;
1584 ptr = ml_get_curline();
1585 while (vcol <= (int)curwin->w_virtcol)
1586 {
1587 last_vcol = vcol;
1588#ifdef FEAT_MBYTE
1589 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001590 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 else
1592#endif
1593 ++new_cursor_col;
1594 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1595 }
1596 vcol = last_vcol;
1597
1598 /*
1599 * May need to insert spaces to be able to position the cursor on
1600 * the right screen column.
1601 */
1602 if (vcol != (int)curwin->w_virtcol)
1603 {
1604 curwin->w_cursor.col = new_cursor_col;
1605 i = (int)curwin->w_virtcol - vcol;
1606 ptr = alloc(i + 1);
1607 if (ptr != NULL)
1608 {
1609 new_cursor_col += i;
1610 ptr[i] = NUL;
1611 while (--i >= 0)
1612 ptr[i] = ' ';
1613 ins_str(ptr);
1614 vim_free(ptr);
1615 }
1616 }
1617
1618 /*
1619 * When changing the indent while the cursor is in it, reset
1620 * Insstart_col to 0.
1621 */
1622 insstart_less = MAXCOL;
1623 }
1624
1625 curwin->w_p_list = save_p_list;
1626
1627 if (new_cursor_col <= 0)
1628 curwin->w_cursor.col = 0;
1629 else
1630 curwin->w_cursor.col = new_cursor_col;
1631 curwin->w_set_curswant = TRUE;
1632 changed_cline_bef_curs();
1633
1634 /*
1635 * May have to adjust the start of the insert.
1636 */
1637 if (State & INSERT)
1638 {
1639 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1640 {
1641 if ((int)Insstart.col <= insstart_less)
1642 Insstart.col = 0;
1643 else
1644 Insstart.col -= insstart_less;
1645 }
1646 if ((int)ai_col <= insstart_less)
1647 ai_col = 0;
1648 else
1649 ai_col -= insstart_less;
1650 }
1651
1652 /*
1653 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1654 * If the number of characters before the cursor decreased, need to pop a
1655 * few characters from the replace stack.
1656 * If the number of characters before the cursor increased, need to push a
1657 * few NULs onto the replace stack.
1658 */
1659 if (REPLACE_NORMAL(State) && start_col >= 0)
1660 {
1661 while (start_col > (int)curwin->w_cursor.col)
1662 {
1663 replace_join(0); /* remove a NUL from the replace stack */
1664 --start_col;
1665 }
1666 while (start_col < (int)curwin->w_cursor.col || replaced)
1667 {
1668 replace_push(NUL);
1669 if (replaced)
1670 {
1671 replace_push(replaced);
1672 replaced = NUL;
1673 }
1674 ++start_col;
1675 }
1676 }
1677
1678#ifdef FEAT_VREPLACE
1679 /*
1680 * For VREPLACE mode, we also have to fix the replace stack. In this case
1681 * it is always possible because we backspace over the whole line and then
1682 * put it back again the way we wanted it.
1683 */
1684 if (State & VREPLACE_FLAG)
1685 {
1686 /* If orig_line didn't allocate, just return. At least we did the job,
1687 * even if you can't backspace. */
1688 if (orig_line == NULL)
1689 return;
1690
1691 /* Save new line */
1692 new_line = vim_strsave(ml_get_curline());
1693 if (new_line == NULL)
1694 return;
1695
1696 /* We only put back the new line up to the cursor */
1697 new_line[curwin->w_cursor.col] = NUL;
1698
1699 /* Put back original line */
1700 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1701 curwin->w_cursor.col = orig_col;
1702
1703 /* Backspace from cursor to start of line */
1704 backspace_until_column(0);
1705
1706 /* Insert new stuff into line again */
1707 ins_bytes(new_line);
1708
1709 vim_free(new_line);
1710 }
1711#endif
1712}
1713
1714/*
1715 * Truncate the space at the end of a line. This is to be used only in an
1716 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1717 * modes.
1718 */
1719 void
1720truncate_spaces(line)
1721 char_u *line;
1722{
1723 int i;
1724
1725 /* find start of trailing white space */
1726 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1727 {
1728 if (State & REPLACE_FLAG)
1729 replace_join(0); /* remove a NUL from the replace stack */
1730 }
1731 line[i + 1] = NUL;
1732}
1733
1734#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1735 || defined(FEAT_COMMENTS) || defined(PROTO)
1736/*
1737 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1738 * modes correctly. May also be used when not in insert mode at all.
1739 */
1740 void
1741backspace_until_column(col)
1742 int col;
1743{
1744 while ((int)curwin->w_cursor.col > col)
1745 {
1746 curwin->w_cursor.col--;
1747 if (State & REPLACE_FLAG)
1748 replace_do_bs();
1749 else
1750 (void)del_char(FALSE);
1751 }
1752}
1753#endif
1754
1755#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1756/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001757 * CTRL-X pressed in Insert mode.
1758 */
1759 static void
1760ins_ctrl_x()
1761{
1762 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1763 * CTRL-V works like CTRL-N */
1764 if (ctrl_x_mode != CTRL_X_CMDLINE)
1765 {
1766 /* if the next ^X<> won't ADD nothing, then reset
1767 * compl_cont_status */
1768 if (compl_cont_status & CONT_N_ADDS)
1769 compl_cont_status = (compl_cont_status | CONT_INTRPT);
1770 else
1771 compl_cont_status = 0;
1772 /* We're not sure which CTRL-X mode it will be yet */
1773 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1774 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1775 edit_submode_pre = NULL;
1776 showmode();
1777 }
1778}
1779
1780/*
1781 * Return TRUE if the 'dict' or 'tsr' option can be used.
1782 */
1783 static int
1784has_compl_option(dict_opt)
1785 int dict_opt;
1786{
1787 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1788 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1789 {
1790 ctrl_x_mode = 0;
1791 edit_submode = NULL;
1792 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1793 : (char_u *)_("'thesaurus' option is empty"),
1794 hl_attr(HLF_E));
1795 if (emsg_silent == 0)
1796 {
1797 vim_beep();
1798 setcursor();
1799 out_flush();
1800 ui_delay(2000L, FALSE);
1801 }
1802 return FALSE;
1803 }
1804 return TRUE;
1805}
1806
1807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1809 * This depends on the current mode.
1810 */
1811 int
1812vim_is_ctrl_x_key(c)
1813 int c;
1814{
1815 /* Always allow ^R - let it's results then be checked */
1816 if (c == Ctrl_R)
1817 return TRUE;
1818
1819 switch (ctrl_x_mode)
1820 {
1821 case 0: /* Not in any CTRL-X mode */
1822 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1823 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001824 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1826 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1827 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001828 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1829 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 case CTRL_X_SCROLL:
1831 return (c == Ctrl_Y || c == Ctrl_E);
1832 case CTRL_X_WHOLE_LINE:
1833 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1834 case CTRL_X_FILES:
1835 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1836 case CTRL_X_DICTIONARY:
1837 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1838 case CTRL_X_THESAURUS:
1839 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1840 case CTRL_X_TAGS:
1841 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1842#ifdef FEAT_FIND_ID
1843 case CTRL_X_PATH_PATTERNS:
1844 return (c == Ctrl_P || c == Ctrl_N);
1845 case CTRL_X_PATH_DEFINES:
1846 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1847#endif
1848 case CTRL_X_CMDLINE:
1849 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1850 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001851#ifdef FEAT_COMPL_FUNC
1852 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001853 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001854 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001855 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001856#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001857 case CTRL_X_SPELL:
1858 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 }
1860 EMSG(_(e_internal));
1861 return FALSE;
1862}
1863
1864/*
1865 * This is like ins_compl_add(), but if ic and inf are set, then the
1866 * case of the originally typed text is used, and the case of the completed
1867 * text is infered, ie this tries to work out what case you probably wanted
1868 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001869 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 */
1871 int
Bram Moolenaar572cb562005-08-05 21:35:02 +00001872ins_compl_add_infercase(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 char_u *str;
1874 int len;
1875 char_u *fname;
1876 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001877 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878{
1879 int has_lower = FALSE;
1880 int was_letter = FALSE;
1881 int idx;
1882
1883 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
1884 {
1885 /* Infer case of completed part -- webb */
1886 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001887 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888
1889 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001890 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001892 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 {
1894 has_lower = TRUE;
1895 if (isupper(IObuff[idx]))
1896 {
1897 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001898 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
1900 break;
1901 }
1902 }
1903 }
1904
1905 /*
1906 * Rule 2: No lower case, 2nd consecutive letter converted to
1907 * upper case.
1908 */
1909 if (!has_lower)
1910 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001911 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001913 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 && islower(IObuff[idx]))
1915 {
1916 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001917 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
1919 break;
1920 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001921 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 }
1923 }
1924
1925 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001926 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
Bram Moolenaar572cb562005-08-05 21:35:02 +00001928 return ins_compl_add(IObuff, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00001930 return ins_compl_add(str, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931}
1932
1933/*
1934 * Add a match to the list of matches.
1935 * If the given string is already in the list of completions, then return
1936 * FAIL, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaar572cb562005-08-05 21:35:02 +00001937 * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb.
1938 *
1939 * New:
1940 * If the given string is already in the list of completions, then return
1941 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
1942 * maybe because alloc() returns NULL, then FAIL is returned -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00001944 int
1945ins_compl_add(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 char_u *str;
1947 int len;
1948 char_u *fname;
1949 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001950 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951{
Bram Moolenaar572cb562005-08-05 21:35:02 +00001952 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953
1954 ui_breakcheck();
1955 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00001956 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 if (len < 0)
1958 len = (int)STRLEN(str);
1959
1960 /*
1961 * If the same match is already present, don't add it.
1962 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001963 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001965 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 do
1967 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00001968 if ( !(match->cp_flags & ORIGINAL_TEXT)
1969 && STRNCMP(match->cp_str, str, (size_t)len) == 0
1970 && match->cp_str[len] == NUL)
1971 return NOTDONE;
1972 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001973 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 }
1975
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001976 /* Remove any popup menu before changing the list of matches. */
1977 ins_compl_del_pum();
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 /*
1980 * Allocate a new match structure.
1981 * Copy the values to the new match structure.
1982 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00001983 match = (compl_T *)alloc((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00001985 return FAIL;
1986 match->cp_number = -1;
1987 if (flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00001989 match->cp_number = 0;
1990 match->cp_str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00001992 else if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
1994 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00001995 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 }
1997 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00001998 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2000 * - NULL otherwise. --Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002001 if (fname && compl_curr_match && compl_curr_match->cp_fname
2002 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
2003 match->cp_fname = compl_curr_match->cp_fname;
2004 else if (fname && (match->cp_fname = vim_strsave(fname)) != NULL)
2005 flags |= FREE_FNAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002007 match->cp_fname = NULL;
2008 match->cp_flags = flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
2010 /*
2011 * Link the new match structure in the list of matches.
2012 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002013 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002014 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 else if (dir == FORWARD)
2016 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002017 match->cp_next = compl_curr_match->cp_next;
2018 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
2020 else /* BACKWARD */
2021 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002022 match->cp_next = compl_curr_match;
2023 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002025 if (match->cp_next)
2026 match->cp_next->cp_prev = match;
2027 if (match->cp_prev)
2028 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002030 compl_first_match = match;
2031 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032
2033 return OK;
2034}
2035
2036/*
2037 * Add an array of matches to the list of matches.
2038 * Frees matches[].
2039 */
2040 static void
2041ins_compl_add_matches(num_matches, matches, dir)
2042 int num_matches;
2043 char_u **matches;
2044 int dir;
2045{
2046 int i;
2047 int add_r = OK;
2048 int ldir = dir;
2049
Bram Moolenaar572cb562005-08-05 21:35:02 +00002050 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 if ((add_r = ins_compl_add(matches[i], -1, NULL, ldir, 0)) == OK)
2052 /* if dir was BACKWARD then honor it just once */
2053 ldir = FORWARD;
2054 FreeWild(num_matches, matches);
2055}
2056
2057/* Make the completion list cyclic.
2058 * Return the number of matches (excluding the original).
2059 */
2060 static int
2061ins_compl_make_cyclic()
2062{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002063 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 int count = 0;
2065
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002066 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 {
2068 /*
2069 * Find the end of the list.
2070 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002071 match = compl_first_match;
2072 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002073 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002075 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 ++count;
2077 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002078 match->cp_next = compl_first_match;
2079 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
2081 return count;
2082}
2083
Bram Moolenaar9372a112005-12-06 19:59:18 +00002084/* "compl_match_array" points the currently displayed list of entries in the
2085 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002086static char_u **compl_match_array = NULL;
2087static int compl_match_arraysize;
2088
2089/*
2090 * Update the screen and when there is any scrolling remove the popup menu.
2091 */
2092 static void
2093ins_compl_upd_pum()
2094{
2095 int h;
2096
2097 if (compl_match_array != NULL)
2098 {
2099 h = curwin->w_cline_height;
2100 update_screen(0);
2101 if (h != curwin->w_cline_height)
2102 ins_compl_del_pum();
2103 }
2104}
2105
2106/*
2107 * Remove any popup menu.
2108 */
2109 static void
2110ins_compl_del_pum()
2111{
2112 if (compl_match_array != NULL)
2113 {
2114 pum_undisplay();
2115 vim_free(compl_match_array);
2116 compl_match_array = NULL;
2117 }
2118}
2119
2120/*
2121 * Return TRUE if the popup menu should be displayed.
2122 */
2123 static int
2124pum_wanted()
2125{
2126 compl_T *compl;
2127 int i;
2128
2129 /* 'completeopt' must contain "menu" */
2130 if (*p_cot == NUL)
2131 return FALSE;
2132
2133 /* The display looks bad on a B&W display. */
2134 if (t_colors < 8
2135#ifdef FEAT_GUI
2136 && !gui.in_use
2137#endif
2138 )
2139 return FALSE;
2140
2141 /* Don't display the popup menu if there are no matches or there is only
2142 * one (ignoring the original text). */
2143 compl = compl_first_match;
2144 i = 0;
2145 do
2146 {
2147 if (compl == NULL
2148 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2149 break;
2150 compl = compl->cp_next;
2151 } while (compl != compl_first_match);
2152
2153 return (i >= 2);
2154}
2155
2156/*
2157 * Show the popup menu for the list of matches.
2158 */
2159 static void
2160ins_compl_show_pum()
2161{
2162 compl_T *compl;
2163 int i;
2164 int cur = -1;
2165 colnr_T col;
2166
2167 if (!pum_wanted())
2168 return;
2169
2170 /* Update the screen before drawing the popup menu over it. */
2171 update_screen(0);
2172
2173 if (compl_match_array == NULL)
2174 {
2175 /* Need to build the popup menu list. */
2176 compl_match_arraysize = 0;
2177 compl = compl_first_match;
2178 do
2179 {
2180 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2181 ++compl_match_arraysize;
2182 compl = compl->cp_next;
2183 } while (compl != NULL && compl != compl_first_match);
2184 compl_match_array = (char_u **)alloc((unsigned)(sizeof(char_u **)
2185 * compl_match_arraysize));
2186 if (compl_match_array != NULL)
2187 {
2188 i = 0;
2189 compl = compl_first_match;
2190 do
2191 {
2192 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2193 {
2194 if (compl == compl_shown_match)
2195 cur = i;
2196 compl_match_array[i++] = compl->cp_str;
2197 }
2198 compl = compl->cp_next;
2199 } while (compl != NULL && compl != compl_first_match);
2200 }
2201 }
2202 else
2203 {
2204 /* popup menu already exists, only need to find the current item.*/
2205 i = 0;
2206 compl = compl_first_match;
2207 do
2208 {
2209 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2210 {
2211 if (compl == compl_shown_match)
2212 {
2213 cur = i;
2214 break;
2215 }
2216 ++i;
2217 }
2218 compl = compl->cp_next;
2219 } while (compl != NULL && compl != compl_first_match);
2220 }
2221
2222 if (compl_match_array != NULL)
2223 {
2224 /* Compute the screen column of the start of the completed text.
2225 * Use the cursor to get all wrapping and other settings right. */
2226 col = curwin->w_cursor.col;
2227 curwin->w_cursor.col = compl_col;
2228 validate_cursor_col();
2229 pum_display(compl_match_array, compl_match_arraysize, cur,
2230 curwin->w_cline_row + W_WINROW(curwin),
2231 curwin->w_cline_height,
2232 curwin->w_wcol + W_WINCOL(curwin));
2233 curwin->w_cursor.col = col;
2234 }
2235}
2236
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#define DICT_FIRST (1) /* use just first element in "dict" */
2238#define DICT_EXACT (2) /* "dict" is the exact name of a file */
2239/*
2240 * Add any identifiers that match the given pattern to the list of
2241 * completions.
2242 */
2243 static void
2244ins_compl_dictionaries(dict, pat, dir, flags, thesaurus)
2245 char_u *dict;
2246 char_u *pat;
2247 int dir;
2248 int flags;
2249 int thesaurus;
2250{
2251 char_u *ptr;
2252 char_u *buf;
2253 FILE *fp;
2254 regmatch_T regmatch;
2255 int add_r;
2256 char_u **files;
2257 int count;
2258 int i;
2259 int save_p_scs;
2260
2261 buf = alloc(LSIZE);
2262 /* If 'infercase' is set, don't use 'smartcase' here */
2263 save_p_scs = p_scs;
2264 if (curbuf->b_p_inf)
2265 p_scs = FALSE;
2266 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2267 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2268 regmatch.rm_ic = ignorecase(pat);
2269 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002270 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
2272 /* copy one dictionary file name into buf */
2273 if (flags == DICT_EXACT)
2274 {
2275 count = 1;
2276 files = &dict;
2277 }
2278 else
2279 {
2280 /* Expand wildcards in the dictionary name, but do not allow
2281 * backticks (for security, the 'dict' option may have been set in
2282 * a modeline). */
2283 copy_option_part(&dict, buf, LSIZE, ",");
2284 if (vim_strchr(buf, '`') != NULL
2285 || expand_wildcards(1, &buf, &count, &files,
2286 EW_FILE|EW_SILENT) != OK)
2287 count = 0;
2288 }
2289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002290 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2293 if (flags != DICT_EXACT)
2294 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002295 vim_snprintf((char *)IObuff, IOSIZE,
2296 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2298 }
2299
2300 if (fp != NULL)
2301 {
2302 /*
2303 * Read dictionary file line by line.
2304 * Check each line for a match.
2305 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002306 while (!got_int && !compl_interrupted
2307 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
2309 ptr = buf;
2310 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2311 {
2312 ptr = regmatch.startp[0];
2313 ptr = find_word_end(ptr);
2314 add_r = ins_compl_add_infercase(regmatch.startp[0],
2315 (int)(ptr - regmatch.startp[0]),
2316 files[i], dir, 0);
2317 if (thesaurus)
2318 {
2319 char_u *wstart;
2320
2321 /*
2322 * Add the other matches on the line
2323 */
2324 while (!got_int)
2325 {
2326 /* Find start of the next word. Skip white
2327 * space and punctuation. */
2328 ptr = find_word_start(ptr);
2329 if (*ptr == NUL || *ptr == NL)
2330 break;
2331 wstart = ptr;
2332
2333 /* Find end of the word and add it. */
2334#ifdef FEAT_MBYTE
2335 if (has_mbyte)
2336 /* Japanese words may have characters in
2337 * different classes, only separate words
2338 * with single-byte non-word characters. */
2339 while (*ptr != NUL)
2340 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002341 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
2343 if (l < 2 && !vim_iswordc(*ptr))
2344 break;
2345 ptr += l;
2346 }
2347 else
2348#endif
2349 ptr = find_word_end(ptr);
2350 add_r = ins_compl_add_infercase(wstart,
2351 (int)(ptr - wstart), files[i], dir, 0);
2352 }
2353 }
2354 if (add_r == OK)
2355 /* if dir was BACKWARD then honor it just once */
2356 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002357 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 break;
2359 /* avoid expensive call to vim_regexec() when at end
2360 * of line */
2361 if (*ptr == '\n' || got_int)
2362 break;
2363 }
2364 line_breakcheck();
Bram Moolenaar572cb562005-08-05 21:35:02 +00002365 ins_compl_check_keys(50);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 }
2367 fclose(fp);
2368 }
2369 }
2370 if (flags != DICT_EXACT)
2371 FreeWild(count, files);
2372 if (flags)
2373 break;
2374 }
2375 p_scs = save_p_scs;
2376 vim_free(regmatch.regprog);
2377 vim_free(buf);
2378}
2379
2380/*
2381 * Find the start of the next word.
2382 * Returns a pointer to the first char of the word. Also stops at a NUL.
2383 */
2384 char_u *
2385find_word_start(ptr)
2386 char_u *ptr;
2387{
2388#ifdef FEAT_MBYTE
2389 if (has_mbyte)
2390 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002391 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 else
2393#endif
2394 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2395 ++ptr;
2396 return ptr;
2397}
2398
2399/*
2400 * Find the end of the word. Assumes it starts inside a word.
2401 * Returns a pointer to just after the word.
2402 */
2403 char_u *
2404find_word_end(ptr)
2405 char_u *ptr;
2406{
2407#ifdef FEAT_MBYTE
2408 int start_class;
2409
2410 if (has_mbyte)
2411 {
2412 start_class = mb_get_class(ptr);
2413 if (start_class > 1)
2414 while (*ptr != NUL)
2415 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002416 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 if (mb_get_class(ptr) != start_class)
2418 break;
2419 }
2420 }
2421 else
2422#endif
2423 while (vim_iswordc(*ptr))
2424 ++ptr;
2425 return ptr;
2426}
2427
2428/*
2429 * Free the list of completions
2430 */
2431 static void
2432ins_compl_free()
2433{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002434 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002436 vim_free(compl_pattern);
2437 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002439 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002441
2442 ins_compl_del_pum();
2443 pum_clear();
2444
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002445 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 do
2447 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002448 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002449 compl_curr_match = compl_curr_match->cp_next;
2450 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002452 if (match->cp_flags & FREE_FNAME)
2453 vim_free(match->cp_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002455 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2456 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457}
2458
2459 static void
2460ins_compl_clear()
2461{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002462 compl_cont_status = 0;
2463 compl_started = FALSE;
2464 compl_matches = 0;
2465 vim_free(compl_pattern);
2466 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 save_sm = -1;
2468 edit_submode_extra = NULL;
2469}
2470
2471/*
2472 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002473 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002474 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002476 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477ins_compl_prep(c)
2478 int c;
2479{
2480 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 int temp;
2482 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002483 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485 /* Forget any previous 'special' messages if this is actually
2486 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2487 */
2488 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2489 edit_submode_extra = NULL;
2490
2491 /* Ignore end of Select mode mapping */
2492 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002493 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
2495 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2496 {
2497 /*
2498 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2499 * it will be yet. Now we decide.
2500 */
2501 switch (c)
2502 {
2503 case Ctrl_E:
2504 case Ctrl_Y:
2505 ctrl_x_mode = CTRL_X_SCROLL;
2506 if (!(State & REPLACE_FLAG))
2507 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2508 else
2509 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2510 edit_submode_pre = NULL;
2511 showmode();
2512 break;
2513 case Ctrl_L:
2514 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2515 break;
2516 case Ctrl_F:
2517 ctrl_x_mode = CTRL_X_FILES;
2518 break;
2519 case Ctrl_K:
2520 ctrl_x_mode = CTRL_X_DICTIONARY;
2521 break;
2522 case Ctrl_R:
2523 /* Simply allow ^R to happen without affecting ^X mode */
2524 break;
2525 case Ctrl_T:
2526 ctrl_x_mode = CTRL_X_THESAURUS;
2527 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002528#ifdef FEAT_COMPL_FUNC
2529 case Ctrl_U:
2530 ctrl_x_mode = CTRL_X_FUNCTION;
2531 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002532 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002533 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002534 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002535#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002536 case 's':
2537 case Ctrl_S:
2538 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002539#ifdef FEAT_SYN_HL
2540 spell_back_to_badword();
2541#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002542 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 case Ctrl_RSB:
2544 ctrl_x_mode = CTRL_X_TAGS;
2545 break;
2546#ifdef FEAT_FIND_ID
2547 case Ctrl_I:
2548 case K_S_TAB:
2549 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2550 break;
2551 case Ctrl_D:
2552 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2553 break;
2554#endif
2555 case Ctrl_V:
2556 case Ctrl_Q:
2557 ctrl_x_mode = CTRL_X_CMDLINE;
2558 break;
2559 case Ctrl_P:
2560 case Ctrl_N:
2561 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
2562 * just started ^X mode, or there were enough ^X's to cancel
2563 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2564 * do normal expansion when interrupting a different mode (say
2565 * ^X^F^X^P or ^P^X^X^P, see below)
2566 * nothing changes if interrupting mode 0, (eg, the flag
2567 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002568 if (!(compl_cont_status & CONT_INTRPT))
2569 compl_cont_status |= CONT_LOCAL;
2570 else if (compl_cont_mode != 0)
2571 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 /* FALLTHROUGH */
2573 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002574 /* If we have typed at least 2 ^X's... for modes != 0, we set
2575 * compl_cont_status = 0 (eg, as if we had just started ^X
2576 * mode).
2577 * For mode 0, we set "compl_cont_mode" to an impossible
2578 * value, in both cases ^X^X can be used to restart the same
2579 * mode (avoiding ADDING mode).
2580 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2581 * 'complete' and local ^P expansions respectively.
2582 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2583 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 if (c == Ctrl_X)
2585 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002586 if (compl_cont_mode != 0)
2587 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002589 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
2591 ctrl_x_mode = 0;
2592 edit_submode = NULL;
2593 showmode();
2594 break;
2595 }
2596 }
2597 else if (ctrl_x_mode != 0)
2598 {
2599 /* We're already in CTRL-X mode, do we stay in it? */
2600 if (!vim_is_ctrl_x_key(c))
2601 {
2602 if (ctrl_x_mode == CTRL_X_SCROLL)
2603 ctrl_x_mode = 0;
2604 else
2605 ctrl_x_mode = CTRL_X_FINISHED;
2606 edit_submode = NULL;
2607 }
2608 showmode();
2609 }
2610
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002611 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
2613 /* Show error message from attempted keyword completion (probably
2614 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002615 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 showmode();
2617 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R)
2618 || ctrl_x_mode == CTRL_X_FINISHED)
2619 {
2620 /* Get here when we have finished typing a sequence of ^N and
2621 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002622 * memory that was used, and make sure we can redo the insert. */
2623 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002625 char_u *p;
2626
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 /*
2628 * If any of the original typed text has been changed,
2629 * eg when ignorecase is set, we must add back-spaces to
2630 * the redo buffer. We add as few as necessary to delete
2631 * just the part of the original text that has changed.
2632 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002633 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002634 p = compl_orig_text;
2635 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002637 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 ++ptr;
2639 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002640 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 AppendCharToRedobuff(K_BS);
2642 AppendToRedobuffLit(ptr);
2643 }
2644
2645#ifdef FEAT_CINDENT
2646 want_cindent = (can_cindent && cindent_on());
2647#endif
2648 /*
2649 * When completing whole lines: fix indent for 'cindent'.
2650 * Otherwise, break line if it's too long.
2651 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002652 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 {
2654#ifdef FEAT_CINDENT
2655 /* re-indent the current line */
2656 if (want_cindent)
2657 {
2658 do_c_expr_indent();
2659 want_cindent = FALSE; /* don't do it again */
2660 }
2661#endif
2662 }
2663 else
2664 {
2665 /* put the cursor on the last char, for 'tw' formatting */
2666 curwin->w_cursor.col--;
2667 if (stop_arrow() == OK)
2668 insertchar(NUL, 0, -1);
2669 curwin->w_cursor.col++;
2670 }
2671
2672 auto_format(FALSE, TRUE);
2673
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002674 /* if the popup menu is displayed hitting Enter means accepting
2675 * the selection without inserting anything. */
2676 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
2677 retval = TRUE;
2678
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002680 compl_started = FALSE;
2681 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 msg_clr_cmdline(); /* necessary for "noshowmode" */
2683 ctrl_x_mode = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002684 if (save_sm >= 0)
2685 p_sm = save_sm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (edit_submode != NULL)
2687 {
2688 edit_submode = NULL;
2689 showmode();
2690 }
2691
2692#ifdef FEAT_CINDENT
2693 /*
2694 * Indent now if a key was typed that is in 'cinkeys'.
2695 */
2696 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2697 do_c_expr_indent();
2698#endif
2699 }
2700 }
2701
2702 /* reset continue_* if we left expansion-mode, if we stay they'll be
2703 * (re)set properly in ins_complete() */
2704 if (!vim_is_ctrl_x_key(c))
2705 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002706 compl_cont_status = 0;
2707 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002709
2710 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711}
2712
2713/*
2714 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2715 * (depending on flag) starting from buf and looking for a non-scanned
2716 * buffer (other than curbuf). curbuf is special, if it is called with
2717 * buf=curbuf then it has to be the first call for a given flag/expansion.
2718 *
2719 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2720 */
2721 static buf_T *
2722ins_compl_next_buf(buf, flag)
2723 buf_T *buf;
2724 int flag;
2725{
2726#ifdef FEAT_WINDOWS
2727 static win_T *wp;
2728#endif
2729
2730 if (flag == 'w') /* just windows */
2731 {
2732#ifdef FEAT_WINDOWS
2733 if (buf == curbuf) /* first call for this flag/expansion */
2734 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002735 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 && wp->w_buffer->b_scanned)
2737 ;
2738 buf = wp->w_buffer;
2739#else
2740 buf = curbuf;
2741#endif
2742 }
2743 else
2744 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2745 * (unlisted buffers)
2746 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002747 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 && ((flag == 'U'
2749 ? buf->b_p_bl
2750 : (!buf->b_p_bl
2751 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2752 || buf->b_scanned
2753 || (buf->b_ml.ml_mfp == NULL
2754 && ctrl_x_mode == CTRL_X_WHOLE_LINE)))
2755 ;
2756 return buf;
2757}
2758
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002759#ifdef FEAT_COMPL_FUNC
Bram Moolenaare344bea2005-09-01 20:46:49 +00002760static int expand_by_function __ARGS((int type, char_u *base, char_u ***matches));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002761
2762/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002763 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00002764 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002765 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002766 */
2767 static int
Bram Moolenaare344bea2005-09-01 20:46:49 +00002768expand_by_function(type, base, matches)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002769 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002770 char_u *base;
2771 char_u ***matches;
2772{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002773 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002774 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002775 listitem_T *li;
2776 garray_T ga;
2777 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002778 char_u *funcname;
2779 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002780
Bram Moolenaare344bea2005-09-01 20:46:49 +00002781 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
2782 if (*funcname == NUL)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002783 return 0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002784
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002785 /* Call 'completefunc' to obtain the list of matches. */
2786 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00002787 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002788
Bram Moolenaare344bea2005-09-01 20:46:49 +00002789 pos = curwin->w_cursor;
2790 matchlist = call_func_retlist(funcname, 2, args, FALSE);
2791 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002792 if (matchlist == NULL)
2793 return 0;
2794
2795 /* Go through the List with matches and put them in an array. */
2796 ga_init2(&ga, (int)sizeof(char_u *), 8);
2797 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002798 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002799 p = get_tv_string_chk(&li->li_tv);
2800 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002801 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002802 if (ga_grow(&ga, 1) == FAIL)
2803 break;
2804 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p);
2805 ++ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002806 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002807 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002808
2809 list_unref(matchlist);
2810 *matches = (char_u **)ga.ga_data;
2811 return ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002812}
2813#endif /* FEAT_COMPL_FUNC */
2814
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002815/*
2816 * Get the next expansion(s), using "compl_pattern".
2817 * The search starts at position "ini" in curbuf and in the direction dir.
2818 * When "compl_started" is FALSE start at that position, otherwise
2819 * continue where we stopped searching before.
2820 * This may return before finding all the matches.
2821 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 */
2823 static int
2824ins_compl_get_exp(ini, dir)
2825 pos_T *ini;
2826 int dir;
2827{
2828 static pos_T first_match_pos;
2829 static pos_T last_match_pos;
2830 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002831 static int found_all = FALSE; /* Found all matches of a
2832 certain type. */
2833 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
Bram Moolenaar572cb562005-08-05 21:35:02 +00002835 pos_T *pos;
2836 char_u **matches;
2837 int save_p_scs;
2838 int save_p_ws;
2839 int save_p_ic;
2840 int i;
2841 int num_matches;
2842 int len;
2843 int found_new_match;
2844 int type = ctrl_x_mode;
2845 char_u *ptr;
2846 char_u *dict = NULL;
2847 int dict_f = 0;
2848 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002850 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
2852 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
2853 ins_buf->b_scanned = 0;
2854 found_all = FALSE;
2855 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002856 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002857 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 last_match_pos = first_match_pos = *ini;
2859 }
2860
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002861 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 pos = (dir == FORWARD) ? &last_match_pos : &first_match_pos;
2863 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
2864 for (;;)
2865 {
2866 found_new_match = FAIL;
2867
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002868 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 * or if found_all says this entry is done. For ^X^L only use the
2870 * entries from 'complete' that look in loaded buffers. */
2871 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002872 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 {
2874 found_all = FALSE;
2875 while (*e_cpt == ',' || *e_cpt == ' ')
2876 e_cpt++;
2877 if (*e_cpt == '.' && !curbuf->b_scanned)
2878 {
2879 ins_buf = curbuf;
2880 first_match_pos = *ini;
2881 /* So that ^N can match word immediately after cursor */
2882 if (ctrl_x_mode == 0)
2883 dec(&first_match_pos);
2884 last_match_pos = first_match_pos;
2885 type = 0;
2886 }
2887 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2888 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2889 {
2890 /* Scan a buffer, but not the current one. */
2891 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
2892 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002893 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 first_match_pos.col = last_match_pos.col = 0;
2895 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2896 last_match_pos.lnum = 0;
2897 type = 0;
2898 }
2899 else /* unloaded buffer, scan like dictionary */
2900 {
2901 found_all = TRUE;
2902 if (ins_buf->b_fname == NULL)
2903 continue;
2904 type = CTRL_X_DICTIONARY;
2905 dict = ins_buf->b_fname;
2906 dict_f = DICT_EXACT;
2907 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00002908 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 ins_buf->b_fname == NULL
2910 ? buf_spname(ins_buf)
2911 : ins_buf->b_sfname == NULL
2912 ? (char *)ins_buf->b_fname
2913 : (char *)ins_buf->b_sfname);
2914 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2915 }
2916 else if (*e_cpt == NUL)
2917 break;
2918 else
2919 {
2920 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2921 type = -1;
2922 else if (*e_cpt == 'k' || *e_cpt == 's')
2923 {
2924 if (*e_cpt == 'k')
2925 type = CTRL_X_DICTIONARY;
2926 else
2927 type = CTRL_X_THESAURUS;
2928 if (*++e_cpt != ',' && *e_cpt != NUL)
2929 {
2930 dict = e_cpt;
2931 dict_f = DICT_FIRST;
2932 }
2933 }
2934#ifdef FEAT_FIND_ID
2935 else if (*e_cpt == 'i')
2936 type = CTRL_X_PATH_PATTERNS;
2937 else if (*e_cpt == 'd')
2938 type = CTRL_X_PATH_DEFINES;
2939#endif
2940 else if (*e_cpt == ']' || *e_cpt == 't')
2941 {
2942 type = CTRL_X_TAGS;
2943 sprintf((char*)IObuff, _("Scanning tags."));
2944 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2945 }
2946 else
2947 type = -1;
2948
2949 /* in any case e_cpt is advanced to the next entry */
2950 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2951
2952 found_all = TRUE;
2953 if (type == -1)
2954 continue;
2955 }
2956 }
2957
2958 switch (type)
2959 {
2960 case -1:
2961 break;
2962#ifdef FEAT_FIND_ID
2963 case CTRL_X_PATH_PATTERNS:
2964 case CTRL_X_PATH_DEFINES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002965 find_pattern_in_path(compl_pattern, dir,
2966 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002968 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
2970 (linenr_T)1, (linenr_T)MAXLNUM);
2971 break;
2972#endif
2973
2974 case CTRL_X_DICTIONARY:
2975 case CTRL_X_THESAURUS:
2976 ins_compl_dictionaries(
2977 dict ? dict
2978 : (type == CTRL_X_THESAURUS
2979 ? (*curbuf->b_p_tsr == NUL
2980 ? p_tsr
2981 : curbuf->b_p_tsr)
2982 : (*curbuf->b_p_dict == NUL
2983 ? p_dict
2984 : curbuf->b_p_dict)),
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002985 compl_pattern, dir,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
2987 dict = NULL;
2988 break;
2989
2990 case CTRL_X_TAGS:
2991 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
2992 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002993 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
2995 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002996 * of matches is found when compl_pattern is empty */
2997 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
2999 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3000 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3001 {
3002 ins_compl_add_matches(num_matches, matches, dir);
3003 }
3004 p_ic = save_p_ic;
3005 break;
3006
3007 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003008 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3010 {
3011
3012 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003013 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 ins_compl_add_matches(num_matches, matches, dir);
3015 }
3016 break;
3017
3018 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003019 if (expand_cmdline(&compl_xp, compl_pattern,
3020 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 &num_matches, &matches) == EXPAND_OK)
3022 ins_compl_add_matches(num_matches, matches, dir);
3023 break;
3024
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003025#ifdef FEAT_COMPL_FUNC
3026 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003027 case CTRL_X_OMNI:
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003028 num_matches = expand_by_function(type, compl_pattern, &matches);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003029 if (num_matches > 0)
3030 ins_compl_add_matches(num_matches, matches, dir);
3031 break;
3032#endif
3033
Bram Moolenaar488c6512005-08-11 20:09:58 +00003034 case CTRL_X_SPELL:
3035#ifdef FEAT_SYN_HL
3036 num_matches = expand_spelling(first_match_pos.lnum,
3037 first_match_pos.col, compl_pattern, &matches);
3038 if (num_matches > 0)
3039 ins_compl_add_matches(num_matches, matches, dir);
3040#endif
3041 break;
3042
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 default: /* normal ^P/^N and ^X^L */
3044 /*
3045 * If 'infercase' is set, don't use 'smartcase' here
3046 */
3047 save_p_scs = p_scs;
3048 if (ins_buf->b_p_inf)
3049 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003050
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 /* buffers other than curbuf are scanned from the beginning or the
3052 * end but never from the middle, thus setting nowrapscan in this
3053 * buffers is a good idea, on the other hand, we always set
3054 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3055 save_p_ws = p_ws;
3056 if (ins_buf != curbuf)
3057 p_ws = FALSE;
3058 else if (*e_cpt == '.')
3059 p_ws = TRUE;
3060 for (;;)
3061 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003062 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003064 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3065 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003067 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003069 dir, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 else
3071 found_new_match = searchit(NULL, ins_buf, pos, dir,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003072 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 RE_LAST);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003074 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003076 /* set compl_started even on fail */
3077 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 first_match_pos = *pos;
3079 last_match_pos = *pos;
3080 }
3081 else if (first_match_pos.lnum == last_match_pos.lnum
3082 && first_match_pos.col == last_match_pos.col)
3083 found_new_match = FAIL;
3084 if (found_new_match == FAIL)
3085 {
3086 if (ins_buf == curbuf)
3087 found_all = TRUE;
3088 break;
3089 }
3090
3091 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003092 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 && ini->lnum == pos->lnum
3094 && ini->col == pos->col)
3095 continue;
3096 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3097 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3098 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003099 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 {
3101 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3102 continue;
3103 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3104 if (!p_paste)
3105 ptr = skipwhite(ptr);
3106 }
3107 len = (int)STRLEN(ptr);
3108 }
3109 else
3110 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003111 char_u *tmp_ptr = ptr;
3112
3113 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003115 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 /* Skip if already inside a word. */
3117 if (vim_iswordp(tmp_ptr))
3118 continue;
3119 /* Find start of next word. */
3120 tmp_ptr = find_word_start(tmp_ptr);
3121 }
3122 /* Find end of this word. */
3123 tmp_ptr = find_word_end(tmp_ptr);
3124 len = (int)(tmp_ptr - ptr);
3125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003126 if ((compl_cont_status & CONT_ADDING)
3127 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 {
3129 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3130 {
3131 /* Try next line, if any. the new word will be
3132 * "join" as if the normal command "J" was used.
3133 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003134 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 * works -- Acevedo */
3136 STRNCPY(IObuff, ptr, len);
3137 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3138 tmp_ptr = ptr = skipwhite(ptr);
3139 /* Find start of next word. */
3140 tmp_ptr = find_word_start(tmp_ptr);
3141 /* Find end of next word. */
3142 tmp_ptr = find_word_end(tmp_ptr);
3143 if (tmp_ptr > ptr)
3144 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003145 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003147 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 IObuff[len++] = ' ';
3149 /* IObuf =~ "\k.* ", thus len >= 2 */
3150 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003151 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 || (vim_strchr(p_cpo, CPO_JOINSP)
3153 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003154 && (IObuff[len - 2] == '?'
3155 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 IObuff[len++] = ' ';
3157 }
3158 /* copy as much as posible of the new word */
3159 if (tmp_ptr - ptr >= IOSIZE - len)
3160 tmp_ptr = ptr + IOSIZE - len - 1;
3161 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3162 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003163 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 }
3165 IObuff[len] = NUL;
3166 ptr = IObuff;
3167 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003168 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 continue;
3170 }
3171 }
3172 if (ins_compl_add_infercase(ptr, len,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003173 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar572cb562005-08-05 21:35:02 +00003174 dir, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
3176 found_new_match = OK;
3177 break;
3178 }
3179 }
3180 p_scs = save_p_scs;
3181 p_ws = save_p_ws;
3182 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003183
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003184 /* check if compl_curr_match has changed, (e.g. other type of
3185 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003186 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 found_new_match = OK;
3188
3189 /* break the loop for specialized modes (use 'complete' just for the
3190 * generic ctrl_x_mode == 0) or when we've found a new match */
3191 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003192 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003193 {
3194 if (got_int)
3195 break;
3196 if (pum_wanted() && type != -1)
3197 /* Fill the popup menu as soon as possible. */
3198 ins_compl_check_keys(0);
3199 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3200 || compl_interrupted)
3201 break;
3202 compl_started = TRUE;
3203 }
3204 else
3205 {
3206 /* Mark a buffer scanned when it has been scanned completely */
3207 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3208 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003210 compl_started = FALSE;
3211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003213 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
3215 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3216 && *e_cpt == NUL) /* Got to end of 'complete' */
3217 found_new_match = FAIL;
3218
3219 i = -1; /* total of matches, unknown */
3220 if (found_new_match == FAIL
3221 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3222 i = ins_compl_make_cyclic();
3223
3224 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003225 * just been made cyclic then we have to move compl_curr_match to the next
3226 * or previous entry (if any) -- Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003227 compl_curr_match = dir == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003228 if (compl_curr_match == NULL)
3229 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 return i;
3231}
3232
3233/* Delete the old text being completed. */
3234 static void
3235ins_compl_delete()
3236{
3237 int i;
3238
3239 /*
3240 * In insert mode: Delete the typed part.
3241 * In replace mode: Put the old characters back, if any.
3242 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003243 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 backspace_until_column(i);
3245 changed_cline_bef_curs();
3246}
3247
3248/* Insert the new text being completed. */
3249 static void
3250ins_compl_insert()
3251{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003252 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253}
3254
3255/*
3256 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003257 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3258 * get more completions. If it is FALSE, then we just do nothing when there
3259 * are no more completions in a given direction. The latter case is used when
3260 * we are still in the middle of finding completions, to allow browsing
3261 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 * Return the total number of matches, or -1 if still unknown -- webb.
3263 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003264 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3265 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 *
3267 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003268 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3269 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 */
3271 static int
3272ins_compl_next(allow_get_expansion)
3273 int allow_get_expansion;
3274{
3275 int num_matches = -1;
3276 int i;
3277
3278 if (allow_get_expansion)
3279 {
3280 /* Delete old text to be replaced */
3281 ins_compl_delete();
3282 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003283 compl_pending = FALSE;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003284 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
3285 compl_shown_match = compl_shown_match->cp_next;
3286 else if (compl_shows_dir == BACKWARD && compl_shown_match->cp_prev != NULL)
3287 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 else
3289 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003290 compl_pending = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 if (allow_get_expansion)
3292 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003293 num_matches = ins_compl_get_exp(&compl_startpos, compl_direction);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003294 if (compl_pending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003296 if (compl_direction == compl_shows_dir)
3297 compl_shown_match = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
3299 }
3300 else
3301 return -1;
3302 }
3303
3304 /* Insert the text of the new completion */
3305 ins_compl_insert();
3306
3307 if (!allow_get_expansion)
3308 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003309 /* may undisplay the popup menu first */
3310 ins_compl_upd_pum();
3311
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 /* Display the current match. */
3313 update_screen(0);
3314
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003315 /* display the updated popup menu */
3316 ins_compl_show_pum();
3317
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 /* Delete old text to be replaced, since we're still searching and
3319 * don't want to match ourselves! */
3320 ins_compl_delete();
3321 }
3322
3323 /*
3324 * Show the file name for the match (if any)
3325 * Truncate the file name to avoid a wait for return.
3326 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003327 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 {
3329 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003330 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 if (i <= 0)
3332 i = 0;
3333 else
3334 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003335 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 msg(IObuff);
3337 redraw_cmdline = FALSE; /* don't overwrite! */
3338 }
3339
3340 return num_matches;
3341}
3342
3343/*
3344 * Call this while finding completions, to check whether the user has hit a key
3345 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003346 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003348 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 */
3350 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003351ins_compl_check_keys(frequency)
3352 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 static int count = 0;
3355
3356 int c;
3357
3358 /* Don't check when reading keys from a script. That would break the test
3359 * scripts */
3360 if (using_script())
3361 return;
3362
3363 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003364 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 return;
3366 count = 0;
3367
3368 ++no_mapping;
3369 c = vpeekc_any();
3370 --no_mapping;
3371 if (c != NUL)
3372 {
3373 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3374 {
3375 c = safe_vgetc(); /* Eat the character */
3376 if (c == Ctrl_P || c == Ctrl_L)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003377 compl_shows_dir = BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003379 compl_shows_dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 (void)ins_compl_next(FALSE);
3381 }
3382 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003383 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003385 if (compl_pending && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 (void)ins_compl_next(FALSE);
3387}
3388
3389/*
3390 * Do Insert mode completion.
3391 * Called when character "c" was typed, which has a meaning for completion.
3392 * Returns OK if completion was done, FAIL if something failed (out of mem).
3393 */
3394 static int
3395ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003396 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003398 char_u *line;
3399 int startcol = 0; /* column where searched text starts */
3400 colnr_T curs_col; /* cursor column */
3401 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
3403 if (c == Ctrl_P || c == Ctrl_L)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003404 compl_direction = BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003406 compl_direction = FORWARD;
3407 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 {
3409 /* First time we hit ^N or ^P (in a row, I mean) */
3410
3411 /* Turn off 'sm' so we don't show matches with ^X^L */
3412 save_sm = p_sm;
3413 p_sm = FALSE;
3414
3415 did_ai = FALSE;
3416#ifdef FEAT_SMARTINDENT
3417 did_si = FALSE;
3418 can_si = FALSE;
3419 can_si_back = FALSE;
3420#endif
3421 if (stop_arrow() == FAIL)
3422 return FAIL;
3423
3424 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003425 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
3427 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003428 * "compl_startpos" to the cursor as a pattern to add a new word
3429 * instead of expand the one before the cursor, in word-wise if
3430 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 * is not in the same line as the cursor then fix it (the line has
3432 * been split because it was longer than 'tw'). if SOL is set then
3433 * skip the previous pattern, a word at the beginning of the line has
3434 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003435 if ((compl_cont_status & CONT_INTRPT) && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 {
3437 /*
3438 * it is a continued search
3439 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003440 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3442 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3443 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003444 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003446 /* line (probably) wrapped, set compl_startpos to the
3447 * first non_blank in the line, if it is not a wordchar
3448 * include it to get a better pattern, but then we don't
3449 * want the "\\<" prefix, check it bellow */
3450 compl_col = (colnr_T)(skipwhite(line) - line);
3451 compl_startpos.col = compl_col;
3452 compl_startpos.lnum = curwin->w_cursor.lnum;
3453 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 }
3455 else
3456 {
3457 /* S_IPOS was set when we inserted a word that was at the
3458 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003459 * mode but first we need to redefine compl_startpos */
3460 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003462 compl_cont_status |= CONT_SOL;
3463 compl_startpos.col = (colnr_T)(skipwhite(
3464 line + compl_length
3465 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003467 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003469 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003470 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 * have enough space? just being paranoic */
3472#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003473 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003475 compl_cont_status &= ~CONT_SOL;
3476 compl_length = (IOSIZE - MIN_SPACE);
3477 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003479 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3480 if (compl_length < 1)
3481 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 }
3483 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003484 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003486 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003489 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003491 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003493 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003495 compl_cont_status = 0;
3496 compl_cont_status |= CONT_N_ADDS;
3497 compl_startpos = curwin->w_cursor;
3498 startcol = (int)curs_col;
3499 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
3501
3502 /* Work out completion pattern and original text -- webb */
3503 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3504 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003505 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3507 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003508 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003510 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003512 compl_col += ++startcol;
3513 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
3515 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003516 compl_pattern = str_foldcase(line + compl_col,
3517 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003519 compl_pattern = vim_strnsave(line + compl_col,
3520 compl_length);
3521 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 return FAIL;
3523 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003524 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 char_u *prefix = (char_u *)"\\<";
3527
3528 /* we need 3 extra chars, 1 for the NUL and
3529 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003530 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3531 compl_length) + 3);
3532 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003534 if (!vim_iswordp(line + compl_col)
3535 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 && (
3537#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003538 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003540 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541#endif
3542 )))
3543 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003544 STRCPY((char *)compl_pattern, prefix);
3545 (void)quote_meta(compl_pattern + STRLEN(prefix),
3546 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003548 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003550 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003552 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553#endif
3554 )
3555 {
3556 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003557 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3558 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003560 compl_col += curs_col;
3561 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
3563 else
3564 {
3565#ifdef FEAT_MBYTE
3566 /* Search the point of change class of multibyte character
3567 * or not a word single byte character backward. */
3568 if (has_mbyte)
3569 {
3570 int base_class;
3571 int head_off;
3572
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003573 startcol -= (*mb_head_off)(line, line + startcol);
3574 base_class = mb_get_class(line + startcol);
3575 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003577 head_off = (*mb_head_off)(line, line + startcol);
3578 if (base_class != mb_get_class(line + startcol
3579 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003581 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 }
3583 }
3584 else
3585#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003586 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003588 compl_col += ++startcol;
3589 compl_length = (int)curs_col - startcol;
3590 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
3592 /* Only match word with at least two chars -- webb
3593 * there's no need to call quote_meta,
3594 * alloc(7) is enough -- Acevedo
3595 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003596 compl_pattern = alloc(7);
3597 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003599 STRCPY((char *)compl_pattern, "\\<");
3600 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3601 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603 else
3604 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003605 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3606 compl_length) + 3);
3607 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003609 STRCPY((char *)compl_pattern, "\\<");
3610 (void)quote_meta(compl_pattern + 2, line + compl_col,
3611 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
3613 }
3614 }
3615 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3616 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003617 compl_col = skipwhite(line) - line;
3618 compl_length = (int)curs_col - (int)compl_col;
3619 if (compl_length < 0) /* cursor in indent: empty pattern */
3620 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003622 compl_pattern = str_foldcase(line + compl_col, compl_length,
3623 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003625 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3626 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 return FAIL;
3628 }
3629 else if (ctrl_x_mode == CTRL_X_FILES)
3630 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003631 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003633 compl_col += ++startcol;
3634 compl_length = (int)curs_col - startcol;
3635 compl_pattern = addstar(line + compl_col, compl_length,
3636 EXPAND_FILES);
3637 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 return FAIL;
3639 }
3640 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3641 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003642 compl_pattern = vim_strnsave(line, curs_col);
3643 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003645 set_cmd_context(&compl_xp, compl_pattern,
3646 (int)STRLEN(compl_pattern), curs_col);
3647 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3648 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003650 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
3651 compl_col = startcol;
3652 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003654 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003655 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00003656#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003657 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00003658 * Call user defined function 'completefunc' with "a:findstart"
3659 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003660 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00003661 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003662 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003663 char_u *funcname;
3664 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003665
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003666 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00003667 * string */
3668 funcname = ctrl_x_mode == CTRL_X_FUNCTION
3669 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3670 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003671 {
3672 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3673 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003674 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003675 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003676
3677 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003678 args[1] = NULL;
3679 pos = curwin->w_cursor;
3680 col = call_func_retnr(funcname, 2, args, FALSE);
3681 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003682
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003683 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003684 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003685 compl_col = col;
3686 if ((colnr_T)compl_col > curs_col)
3687 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003688
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003689 /* Setup variables for completion. Need to obtain "line" again,
3690 * it may have become invalid. */
3691 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003692 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003693 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3694 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003695#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003696 return FAIL;
3697 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00003698 else if (ctrl_x_mode == CTRL_X_SPELL)
3699 {
3700#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00003701 if (spell_bad_len > 0)
3702 compl_col = curs_col - spell_bad_len;
3703 else
3704 compl_col = spell_word_start(startcol);
3705 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00003706 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003707 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003708 compl_length = (int)curs_col - compl_col;
3709 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3710 if (compl_pattern == NULL)
3711#endif
3712 return FAIL;
3713 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003714 else
3715 {
3716 EMSG2(_(e_intern2), "ins_complete()");
3717 return FAIL;
3718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003720 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 {
3722 edit_submode_pre = (char_u *)_(" Adding");
3723 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3724 {
3725 /* Insert a new line, keep indentation but ignore 'comments' */
3726#ifdef FEAT_COMMENTS
3727 char_u *old = curbuf->b_p_com;
3728
3729 curbuf->b_p_com = (char_u *)"";
3730#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003731 compl_startpos.lnum = curwin->w_cursor.lnum;
3732 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 ins_eol('\r');
3734#ifdef FEAT_COMMENTS
3735 curbuf->b_p_com = old;
3736#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003737 compl_length = 0;
3738 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
3740 }
3741 else
3742 {
3743 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003744 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
3746
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003747 if (compl_cont_status & CONT_LOCAL)
3748 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 else
3750 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
3751
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003753 * "compl_orig_text" itself (not a copy) is added, it will be freed
3754 * when the list of matches is freed. */
3755 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
3756 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
3757 -1, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003759 vim_free(compl_pattern);
3760 compl_pattern = NULL;
3761 vim_free(compl_orig_text);
3762 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 return FAIL;
3764 }
3765
3766 /* showmode might reset the internal line pointers, so it must
3767 * be called before line = ml_get(), or when this address is no
3768 * longer needed. -- Acevedo.
3769 */
3770 edit_submode_extra = (char_u *)_("-- Searching...");
3771 edit_submode_highl = HLF_COUNT;
3772 showmode();
3773 edit_submode_extra = NULL;
3774 out_flush();
3775 }
3776
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003777 compl_shown_match = compl_curr_match;
3778 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779
3780 /*
3781 * Find next match.
3782 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003783 n = ins_compl_next(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003785 /* may undisplay the popup menu */
3786 ins_compl_upd_pum();
3787
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003788 if (n > 1) /* all matches have been found */
3789 compl_matches = n;
3790 compl_curr_match = compl_shown_match;
3791 compl_direction = compl_shows_dir;
3792 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793
3794 /* eat the ESC to avoid leaving insert mode */
3795 if (got_int && !global_busy)
3796 {
3797 (void)vgetc();
3798 got_int = FALSE;
3799 }
3800
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003801 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003802 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003804 edit_submode_extra = (compl_cont_status & CONT_ADDING)
3805 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
3807 edit_submode_highl = HLF_E;
3808 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
3809 * because we couldn't expand anything at first place, but if we used
3810 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
3811 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003812 if ( compl_length > 1
3813 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 || (ctrl_x_mode != 0
3815 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
3816 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003817 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819
Bram Moolenaar572cb562005-08-05 21:35:02 +00003820 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003821 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003823 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824
3825 if (edit_submode_extra == NULL)
3826 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003827 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 {
3829 edit_submode_extra = (char_u *)_("Back at original");
3830 edit_submode_highl = HLF_W;
3831 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003832 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 {
3834 edit_submode_extra = (char_u *)_("Word from other line");
3835 edit_submode_highl = HLF_COUNT;
3836 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00003837 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
3839 edit_submode_extra = (char_u *)_("The only match");
3840 edit_submode_highl = HLF_COUNT;
3841 }
3842 else
3843 {
3844 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003845 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003847 int number = 0;
3848 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003850 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 {
3852 /* search backwards for the first valid (!= -1) number.
3853 * This should normally succeed already at the first loop
3854 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003855 for (match = compl_curr_match->cp_prev; match != NULL
3856 && match != compl_first_match;
3857 match = match->cp_prev)
3858 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003860 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 break;
3862 }
3863 if (match != NULL)
3864 /* go up and assign all numbers which are not assigned
3865 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003866 for (match = match->cp_next;
3867 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003868 match = match->cp_next)
3869 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 }
3871 else /* BACKWARD */
3872 {
3873 /* search forwards (upwards) for the first valid (!= -1)
3874 * number. This should normally succeed already at the
3875 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003876 for (match = compl_curr_match->cp_next; match != NULL
3877 && match != compl_first_match;
3878 match = match->cp_next)
3879 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003881 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 break;
3883 }
3884 if (match != NULL)
3885 /* go down and assign all numbers which are not
3886 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003887 for (match = match->cp_prev; match
3888 && match->cp_number == -1;
3889 match = match->cp_prev)
3890 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 }
3892 }
3893
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003894 /* The match should always have a sequence number now, this is
3895 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003896 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 {
3898 /* Space for 10 text chars. + 2x10-digit no.s */
3899 static char_u match_ref[31];
3900
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003901 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00003903 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003905 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00003906 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003907 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 edit_submode_extra = match_ref;
3909 edit_submode_highl = HLF_R;
3910 if (dollar_vcol)
3911 curs_columns(FALSE);
3912 }
3913 }
3914 }
3915
3916 /* Show a message about what (completion) mode we're in. */
3917 showmode();
3918 if (edit_submode_extra != NULL)
3919 {
3920 if (!p_smd)
3921 msg_attr(edit_submode_extra,
3922 edit_submode_highl < HLF_COUNT
3923 ? hl_attr(edit_submode_highl) : 0);
3924 }
3925 else
3926 msg_clr_cmdline(); /* necessary for "noshowmode" */
3927
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003928 ins_compl_show_pum();
3929
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 return OK;
3931}
3932
3933/*
3934 * Looks in the first "len" chars. of "src" for search-metachars.
3935 * If dest is not NULL the chars. are copied there quoting (with
3936 * a backslash) the metachars, and dest would be NUL terminated.
3937 * Returns the length (needed) of dest
3938 */
3939 static int
3940quote_meta(dest, src, len)
3941 char_u *dest;
3942 char_u *src;
3943 int len;
3944{
3945 int m;
3946
3947 for (m = len; --len >= 0; src++)
3948 {
3949 switch (*src)
3950 {
3951 case '.':
3952 case '*':
3953 case '[':
3954 if (ctrl_x_mode == CTRL_X_DICTIONARY
3955 || ctrl_x_mode == CTRL_X_THESAURUS)
3956 break;
3957 case '~':
3958 if (!p_magic) /* quote these only if magic is set */
3959 break;
3960 case '\\':
3961 if (ctrl_x_mode == CTRL_X_DICTIONARY
3962 || ctrl_x_mode == CTRL_X_THESAURUS)
3963 break;
3964 case '^': /* currently it's not needed. */
3965 case '$':
3966 m++;
3967 if (dest != NULL)
3968 *dest++ = '\\';
3969 break;
3970 }
3971 if (dest != NULL)
3972 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003973# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 /* Copy remaining bytes of a multibyte character. */
3975 if (has_mbyte)
3976 {
3977 int i, mb_len;
3978
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003979 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 if (mb_len > 0 && len >= mb_len)
3981 for (i = 0; i < mb_len; ++i)
3982 {
3983 --len;
3984 ++src;
3985 if (dest != NULL)
3986 *dest++ = *src;
3987 }
3988 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00003989# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 }
3991 if (dest != NULL)
3992 *dest = NUL;
3993
3994 return m;
3995}
3996#endif /* FEAT_INS_EXPAND */
3997
3998/*
3999 * Next character is interpreted literally.
4000 * A one, two or three digit decimal number is interpreted as its byte value.
4001 * If one or two digits are entered, the next character is given to vungetc().
4002 * For Unicode a character > 255 may be returned.
4003 */
4004 int
4005get_literal()
4006{
4007 int cc;
4008 int nc;
4009 int i;
4010 int hex = FALSE;
4011 int octal = FALSE;
4012#ifdef FEAT_MBYTE
4013 int unicode = 0;
4014#endif
4015
4016 if (got_int)
4017 return Ctrl_C;
4018
4019#ifdef FEAT_GUI
4020 /*
4021 * In GUI there is no point inserting the internal code for a special key.
4022 * It is more useful to insert the string "<KEY>" instead. This would
4023 * probably be useful in a text window too, but it would not be
4024 * vi-compatible (maybe there should be an option for it?) -- webb
4025 */
4026 if (gui.in_use)
4027 ++allow_keys;
4028#endif
4029#ifdef USE_ON_FLY_SCROLL
4030 dont_scroll = TRUE; /* disallow scrolling here */
4031#endif
4032 ++no_mapping; /* don't map the next key hits */
4033 cc = 0;
4034 i = 0;
4035 for (;;)
4036 {
4037 do
4038 nc = safe_vgetc();
4039 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4040 || nc == K_HOR_SCROLLBAR);
4041#ifdef FEAT_CMDL_INFO
4042 if (!(State & CMDLINE)
4043# ifdef FEAT_MBYTE
4044 && MB_BYTE2LEN_CHECK(nc) == 1
4045# endif
4046 )
4047 add_to_showcmd(nc);
4048#endif
4049 if (nc == 'x' || nc == 'X')
4050 hex = TRUE;
4051 else if (nc == 'o' || nc == 'O')
4052 octal = TRUE;
4053#ifdef FEAT_MBYTE
4054 else if (nc == 'u' || nc == 'U')
4055 unicode = nc;
4056#endif
4057 else
4058 {
4059 if (hex
4060#ifdef FEAT_MBYTE
4061 || unicode != 0
4062#endif
4063 )
4064 {
4065 if (!vim_isxdigit(nc))
4066 break;
4067 cc = cc * 16 + hex2nr(nc);
4068 }
4069 else if (octal)
4070 {
4071 if (nc < '0' || nc > '7')
4072 break;
4073 cc = cc * 8 + nc - '0';
4074 }
4075 else
4076 {
4077 if (!VIM_ISDIGIT(nc))
4078 break;
4079 cc = cc * 10 + nc - '0';
4080 }
4081
4082 ++i;
4083 }
4084
4085 if (cc > 255
4086#ifdef FEAT_MBYTE
4087 && unicode == 0
4088#endif
4089 )
4090 cc = 255; /* limit range to 0-255 */
4091 nc = 0;
4092
4093 if (hex) /* hex: up to two chars */
4094 {
4095 if (i >= 2)
4096 break;
4097 }
4098#ifdef FEAT_MBYTE
4099 else if (unicode) /* Unicode: up to four or eight chars */
4100 {
4101 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4102 break;
4103 }
4104#endif
4105 else if (i >= 3) /* decimal or octal: up to three chars */
4106 break;
4107 }
4108 if (i == 0) /* no number entered */
4109 {
4110 if (nc == K_ZERO) /* NUL is stored as NL */
4111 {
4112 cc = '\n';
4113 nc = 0;
4114 }
4115 else
4116 {
4117 cc = nc;
4118 nc = 0;
4119 }
4120 }
4121
4122 if (cc == 0) /* NUL is stored as NL */
4123 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004124#ifdef FEAT_MBYTE
4125 if (enc_dbcs && (cc & 0xff) == 0)
4126 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4127 second byte will cause trouble! */
4128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129
4130 --no_mapping;
4131#ifdef FEAT_GUI
4132 if (gui.in_use)
4133 --allow_keys;
4134#endif
4135 if (nc)
4136 vungetc(nc);
4137 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4138 return cc;
4139}
4140
4141/*
4142 * Insert character, taking care of special keys and mod_mask
4143 */
4144 static void
4145insert_special(c, allow_modmask, ctrlv)
4146 int c;
4147 int allow_modmask;
4148 int ctrlv; /* c was typed after CTRL-V */
4149{
4150 char_u *p;
4151 int len;
4152
4153 /*
4154 * Special function key, translate into "<Key>". Up to the last '>' is
4155 * inserted with ins_str(), so as not to replace characters in replace
4156 * mode.
4157 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4158 * unless 'allow_modmask' is TRUE.
4159 */
4160#ifdef MACOS
4161 /* Command-key never produces a normal key */
4162 if (mod_mask & MOD_MASK_CMD)
4163 allow_modmask = TRUE;
4164#endif
4165 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4166 {
4167 p = get_special_key_name(c, mod_mask);
4168 len = (int)STRLEN(p);
4169 c = p[len - 1];
4170 if (len > 2)
4171 {
4172 if (stop_arrow() == FAIL)
4173 return;
4174 p[len - 1] = NUL;
4175 ins_str(p);
4176 AppendToRedobuffLit(p);
4177 ctrlv = FALSE;
4178 }
4179 }
4180 if (stop_arrow() == OK)
4181 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4182}
4183
4184/*
4185 * Special characters in this context are those that need processing other
4186 * than the simple insertion that can be performed here. This includes ESC
4187 * which terminates the insert, and CR/NL which need special processing to
4188 * open up a new line. This routine tries to optimize insertions performed by
4189 * the "redo", "undo" or "put" commands, so it needs to know when it should
4190 * stop and defer processing to the "normal" mechanism.
4191 * '0' and '^' are special, because they can be followed by CTRL-D.
4192 */
4193#ifdef EBCDIC
4194# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4195#else
4196# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4197#endif
4198
4199#ifdef FEAT_MBYTE
4200# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4201#else
4202# define WHITECHAR(cc) vim_iswhite(cc)
4203#endif
4204
4205 void
4206insertchar(c, flags, second_indent)
4207 int c; /* character to insert or NUL */
4208 int flags; /* INSCHAR_FORMAT, etc. */
4209 int second_indent; /* indent for second line if >= 0 */
4210{
4211 int haveto_redraw = FALSE;
4212 int textwidth;
4213#ifdef FEAT_COMMENTS
4214 colnr_T leader_len;
4215 char_u *p;
4216 int no_leader = FALSE;
4217 int do_comments = (flags & INSCHAR_DO_COM);
4218#endif
4219 int fo_white_par;
4220 int first_line = TRUE;
4221 int fo_ins_blank;
4222#ifdef FEAT_MBYTE
4223 int fo_multibyte;
4224#endif
4225 int save_char = NUL;
4226 int cc;
4227
4228 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4229 fo_ins_blank = has_format_option(FO_INS_BLANK);
4230#ifdef FEAT_MBYTE
4231 fo_multibyte = has_format_option(FO_MBYTE_BREAK);
4232#endif
4233 fo_white_par = has_format_option(FO_WHITE_PAR);
4234
4235 /*
4236 * Try to break the line in two or more pieces when:
4237 * - Always do this if we have been called to do formatting only.
4238 * - Always do this when 'formatoptions' has the 'a' flag and the line
4239 * ends in white space.
4240 * - Otherwise:
4241 * - Don't do this if inserting a blank
4242 * - Don't do this if an existing character is being replaced, unless
4243 * we're in VREPLACE mode.
4244 * - Do this if the cursor is not on the line where insert started
4245 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4246 * before the insert.
4247 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4248 * before 'textwidth'
4249 */
4250 if (textwidth
4251 && ((flags & INSCHAR_FORMAT)
4252 || (!vim_iswhite(c)
4253 && !((State & REPLACE_FLAG)
4254#ifdef FEAT_VREPLACE
4255 && !(State & VREPLACE_FLAG)
4256#endif
4257 && *ml_get_cursor() != NUL)
4258 && (curwin->w_cursor.lnum != Insstart.lnum
4259 || ((!has_format_option(FO_INS_LONG)
4260 || Insstart_textlen <= (colnr_T)textwidth)
4261 && (!fo_ins_blank
4262 || Insstart_blank_vcol <= (colnr_T)textwidth
4263 ))))))
4264 {
4265 /*
4266 * When 'ai' is off we don't want a space under the cursor to be
4267 * deleted. Replace it with an 'x' temporarily.
4268 */
4269 if (!curbuf->b_p_ai)
4270 {
4271 cc = gchar_cursor();
4272 if (vim_iswhite(cc))
4273 {
4274 save_char = cc;
4275 pchar_cursor('x');
4276 }
4277 }
4278
4279 /*
4280 * Repeat breaking lines, until the current line is not too long.
4281 */
4282 while (!got_int)
4283 {
4284 int startcol; /* Cursor column at entry */
4285 int wantcol; /* column at textwidth border */
4286 int foundcol; /* column for start of spaces */
4287 int end_foundcol = 0; /* column for start of word */
4288 colnr_T len;
4289 colnr_T virtcol;
4290#ifdef FEAT_VREPLACE
4291 int orig_col = 0;
4292 char_u *saved_text = NULL;
4293#endif
4294 colnr_T col;
4295
4296 virtcol = get_nolist_virtcol();
4297 if (virtcol < (colnr_T)textwidth)
4298 break;
4299
4300#ifdef FEAT_COMMENTS
4301 if (no_leader)
4302 do_comments = FALSE;
4303 else if (!(flags & INSCHAR_FORMAT)
4304 && has_format_option(FO_WRAP_COMS))
4305 do_comments = TRUE;
4306
4307 /* Don't break until after the comment leader */
4308 if (do_comments)
4309 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
4310 else
4311 leader_len = 0;
4312
4313 /* If the line doesn't start with a comment leader, then don't
4314 * start one in a following broken line. Avoids that a %word
4315 * moved to the start of the next line causes all following lines
4316 * to start with %. */
4317 if (leader_len == 0)
4318 no_leader = TRUE;
4319#endif
4320 if (!(flags & INSCHAR_FORMAT)
4321#ifdef FEAT_COMMENTS
4322 && leader_len == 0
4323#endif
4324 && !has_format_option(FO_WRAP))
4325
4326 {
4327 textwidth = 0;
4328 break;
4329 }
4330 if ((startcol = curwin->w_cursor.col) == 0)
4331 break;
4332
4333 /* find column of textwidth border */
4334 coladvance((colnr_T)textwidth);
4335 wantcol = curwin->w_cursor.col;
4336
4337 curwin->w_cursor.col = startcol - 1;
4338#ifdef FEAT_MBYTE
4339 /* Correct cursor for multi-byte character. */
4340 if (has_mbyte)
4341 mb_adjust_cursor();
4342#endif
4343 foundcol = 0;
4344
4345 /*
4346 * Find position to break at.
4347 * Stop at first entered white when 'formatoptions' has 'v'
4348 */
4349 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
4350 || curwin->w_cursor.lnum != Insstart.lnum
4351 || curwin->w_cursor.col >= Insstart.col)
4352 {
4353 cc = gchar_cursor();
4354 if (WHITECHAR(cc))
4355 {
4356 /* remember position of blank just before text */
4357 end_foundcol = curwin->w_cursor.col;
4358
4359 /* find start of sequence of blanks */
4360 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
4361 {
4362 dec_cursor();
4363 cc = gchar_cursor();
4364 }
4365 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
4366 break; /* only spaces in front of text */
4367#ifdef FEAT_COMMENTS
4368 /* Don't break until after the comment leader */
4369 if (curwin->w_cursor.col < leader_len)
4370 break;
4371#endif
4372 if (has_format_option(FO_ONE_LETTER))
4373 {
4374 /* do not break after one-letter words */
4375 if (curwin->w_cursor.col == 0)
4376 break; /* one-letter word at begin */
4377
4378 col = curwin->w_cursor.col;
4379 dec_cursor();
4380 cc = gchar_cursor();
4381
4382 if (WHITECHAR(cc))
4383 continue; /* one-letter, continue */
4384 curwin->w_cursor.col = col;
4385 }
4386#ifdef FEAT_MBYTE
4387 if (has_mbyte)
4388 foundcol = curwin->w_cursor.col
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004389 + (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 else
4391#endif
4392 foundcol = curwin->w_cursor.col + 1;
4393 if (curwin->w_cursor.col < (colnr_T)wantcol)
4394 break;
4395 }
4396#ifdef FEAT_MBYTE
4397 else if (cc >= 0x100 && fo_multibyte
4398 && curwin->w_cursor.col <= (colnr_T)wantcol)
4399 {
4400 /* Break after or before a multi-byte character. */
4401 foundcol = curwin->w_cursor.col;
4402 if (curwin->w_cursor.col < (colnr_T)wantcol)
4403 foundcol += (*mb_char2len)(cc);
4404 end_foundcol = foundcol;
4405 break;
4406 }
4407#endif
4408 if (curwin->w_cursor.col == 0)
4409 break;
4410 dec_cursor();
4411 }
4412
4413 if (foundcol == 0) /* no spaces, cannot break line */
4414 {
4415 curwin->w_cursor.col = startcol;
4416 break;
4417 }
4418
4419 /* Going to break the line, remove any "$" now. */
4420 undisplay_dollar();
4421
4422 /*
4423 * Offset between cursor position and line break is used by replace
4424 * stack functions. VREPLACE does not use this, and backspaces
4425 * over the text instead.
4426 */
4427#ifdef FEAT_VREPLACE
4428 if (State & VREPLACE_FLAG)
4429 orig_col = startcol; /* Will start backspacing from here */
4430 else
4431#endif
4432 replace_offset = startcol - end_foundcol - 1;
4433
4434 /*
4435 * adjust startcol for spaces that will be deleted and
4436 * characters that will remain on top line
4437 */
4438 curwin->w_cursor.col = foundcol;
4439 while (cc = gchar_cursor(), WHITECHAR(cc))
4440 inc_cursor();
4441 startcol -= curwin->w_cursor.col;
4442 if (startcol < 0)
4443 startcol = 0;
4444
4445#ifdef FEAT_VREPLACE
4446 if (State & VREPLACE_FLAG)
4447 {
4448 /*
4449 * In VREPLACE mode, we will backspace over the text to be
4450 * wrapped, so save a copy now to put on the next line.
4451 */
4452 saved_text = vim_strsave(ml_get_cursor());
4453 curwin->w_cursor.col = orig_col;
4454 if (saved_text == NULL)
4455 break; /* Can't do it, out of memory */
4456 saved_text[startcol] = NUL;
4457
4458 /* Backspace over characters that will move to the next line */
4459 if (!fo_white_par)
4460 backspace_until_column(foundcol);
4461 }
4462 else
4463#endif
4464 {
4465 /* put cursor after pos. to break line */
4466 if (!fo_white_par)
4467 curwin->w_cursor.col = foundcol;
4468 }
4469
4470 /*
4471 * Split the line just before the margin.
4472 * Only insert/delete lines, but don't really redraw the window.
4473 */
4474 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
4475 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
4476#ifdef FEAT_COMMENTS
4477 + (do_comments ? OPENLINE_DO_COM : 0)
4478#endif
4479 , old_indent);
4480 old_indent = 0;
4481
4482 replace_offset = 0;
4483 if (first_line)
4484 {
4485 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
4486 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
4487 if (second_indent >= 0)
4488 {
4489#ifdef FEAT_VREPLACE
4490 if (State & VREPLACE_FLAG)
4491 change_indent(INDENT_SET, second_indent, FALSE, NUL);
4492 else
4493#endif
4494 (void)set_indent(second_indent, SIN_CHANGED);
4495 }
4496 first_line = FALSE;
4497 }
4498
4499#ifdef FEAT_VREPLACE
4500 if (State & VREPLACE_FLAG)
4501 {
4502 /*
4503 * In VREPLACE mode we have backspaced over the text to be
4504 * moved, now we re-insert it into the new line.
4505 */
4506 ins_bytes(saved_text);
4507 vim_free(saved_text);
4508 }
4509 else
4510#endif
4511 {
4512 /*
4513 * Check if cursor is not past the NUL off the line, cindent
4514 * may have added or removed indent.
4515 */
4516 curwin->w_cursor.col += startcol;
4517 len = (colnr_T)STRLEN(ml_get_curline());
4518 if (curwin->w_cursor.col > len)
4519 curwin->w_cursor.col = len;
4520 }
4521
4522 haveto_redraw = TRUE;
4523#ifdef FEAT_CINDENT
4524 can_cindent = TRUE;
4525#endif
4526 /* moved the cursor, don't autoindent or cindent now */
4527 did_ai = FALSE;
4528#ifdef FEAT_SMARTINDENT
4529 did_si = FALSE;
4530 can_si = FALSE;
4531 can_si_back = FALSE;
4532#endif
4533 line_breakcheck();
4534 }
4535
4536 if (save_char) /* put back space after cursor */
4537 pchar_cursor(save_char);
4538
4539 if (c == NUL) /* formatting only */
4540 return;
4541 if (haveto_redraw)
4542 {
4543 update_topline();
4544 redraw_curbuf_later(VALID);
4545 }
4546 }
4547 if (c == NUL) /* only formatting was wanted */
4548 return;
4549
4550#ifdef FEAT_COMMENTS
4551 /* Check whether this character should end a comment. */
4552 if (did_ai && (int)c == end_comment_pending)
4553 {
4554 char_u *line;
4555 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4556 int middle_len, end_len;
4557 int i;
4558
4559 /*
4560 * Need to remove existing (middle) comment leader and insert end
4561 * comment leader. First, check what comment leader we can find.
4562 */
4563 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4564 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4565 {
4566 /* Skip middle-comment string */
4567 while (*p && p[-1] != ':') /* find end of middle flags */
4568 ++p;
4569 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4570 /* Don't count trailing white space for middle_len */
4571 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4572 --middle_len;
4573
4574 /* Find the end-comment string */
4575 while (*p && p[-1] != ':') /* find end of end flags */
4576 ++p;
4577 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4578
4579 /* Skip white space before the cursor */
4580 i = curwin->w_cursor.col;
4581 while (--i >= 0 && vim_iswhite(line[i]))
4582 ;
4583 i++;
4584
4585 /* Skip to before the middle leader */
4586 i -= middle_len;
4587
4588 /* Check some expected things before we go on */
4589 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4590 {
4591 /* Backspace over all the stuff we want to replace */
4592 backspace_until_column(i);
4593
4594 /*
4595 * Insert the end-comment string, except for the last
4596 * character, which will get inserted as normal later.
4597 */
4598 ins_bytes_len(lead_end, end_len - 1);
4599 }
4600 }
4601 }
4602 end_comment_pending = NUL;
4603#endif
4604
4605 did_ai = FALSE;
4606#ifdef FEAT_SMARTINDENT
4607 did_si = FALSE;
4608 can_si = FALSE;
4609 can_si_back = FALSE;
4610#endif
4611
4612 /*
4613 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4614 * This speeds up normal text input considerably.
4615 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4616 * need to re-indent at a ':', or any other character (but not what
4617 * 'paste' is set)..
4618 */
4619#ifdef USE_ON_FLY_SCROLL
4620 dont_scroll = FALSE; /* allow scrolling here */
4621#endif
4622
4623 if ( !ISSPECIAL(c)
4624#ifdef FEAT_MBYTE
4625 && (!has_mbyte || (*mb_char2len)(c) == 1)
4626#endif
4627 && vpeekc() != NUL
4628 && !(State & REPLACE_FLAG)
4629#ifdef FEAT_CINDENT
4630 && !cindent_on()
4631#endif
4632#ifdef FEAT_RIGHTLEFT
4633 && !p_ri
4634#endif
4635 )
4636 {
4637#define INPUT_BUFLEN 100
4638 char_u buf[INPUT_BUFLEN + 1];
4639 int i;
4640 colnr_T virtcol = 0;
4641
4642 buf[0] = c;
4643 i = 1;
4644 if (textwidth)
4645 virtcol = get_nolist_virtcol();
4646 /*
4647 * Stop the string when:
4648 * - no more chars available
4649 * - finding a special character (command key)
4650 * - buffer is full
4651 * - running into the 'textwidth' boundary
4652 * - need to check for abbreviation: A non-word char after a word-char
4653 */
4654 while ( (c = vpeekc()) != NUL
4655 && !ISSPECIAL(c)
4656#ifdef FEAT_MBYTE
4657 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4658#endif
4659 && i < INPUT_BUFLEN
4660 && (textwidth == 0
4661 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4662 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4663 {
4664#ifdef FEAT_RIGHTLEFT
4665 c = vgetc();
4666 if (p_hkmap && KeyTyped)
4667 c = hkmap(c); /* Hebrew mode mapping */
4668# ifdef FEAT_FKMAP
4669 if (p_fkmap && KeyTyped)
4670 c = fkmap(c); /* Farsi mode mapping */
4671# endif
4672 buf[i++] = c;
4673#else
4674 buf[i++] = vgetc();
4675#endif
4676 }
4677
4678#ifdef FEAT_DIGRAPHS
4679 do_digraph(-1); /* clear digraphs */
4680 do_digraph(buf[i-1]); /* may be the start of a digraph */
4681#endif
4682 buf[i] = NUL;
4683 ins_str(buf);
4684 if (flags & INSCHAR_CTRLV)
4685 {
4686 redo_literal(*buf);
4687 i = 1;
4688 }
4689 else
4690 i = 0;
4691 if (buf[i] != NUL)
4692 AppendToRedobuffLit(buf + i);
4693 }
4694 else
4695 {
4696#ifdef FEAT_MBYTE
4697 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
4698 {
4699 char_u buf[MB_MAXBYTES + 1];
4700
4701 (*mb_char2bytes)(c, buf);
4702 buf[cc] = NUL;
4703 ins_char_bytes(buf, cc);
4704 AppendCharToRedobuff(c);
4705 }
4706 else
4707#endif
4708 {
4709 ins_char(c);
4710 if (flags & INSCHAR_CTRLV)
4711 redo_literal(c);
4712 else
4713 AppendCharToRedobuff(c);
4714 }
4715 }
4716}
4717
4718/*
4719 * Called after inserting or deleting text: When 'formatoptions' includes the
4720 * 'a' flag format from the current line until the end of the paragraph.
4721 * Keep the cursor at the same position relative to the text.
4722 * The caller must have saved the cursor line for undo, following ones will be
4723 * saved here.
4724 */
4725 void
4726auto_format(trailblank, prev_line)
4727 int trailblank; /* when TRUE also format with trailing blank */
4728 int prev_line; /* may start in previous line */
4729{
4730 pos_T pos;
4731 colnr_T len;
4732 char_u *old;
4733 char_u *new, *pnew;
4734 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004735 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
4737 if (!has_format_option(FO_AUTO))
4738 return;
4739
4740 pos = curwin->w_cursor;
4741 old = ml_get_curline();
4742
4743 /* may remove added space */
4744 check_auto_format(FALSE);
4745
4746 /* Don't format in Insert mode when the cursor is on a trailing blank, the
4747 * user might insert normal text next. Also skip formatting when "1" is
4748 * in 'formatoptions' and there is a single character before the cursor.
4749 * Otherwise the line would be broken and when typing another non-white
4750 * next they are not joined back together. */
4751 wasatend = (pos.col == STRLEN(old));
4752 if (*old != NUL && !trailblank && wasatend)
4753 {
4754 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004755 cc = gchar_cursor();
4756 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
4757 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004759 cc = gchar_cursor();
4760 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 {
4762 curwin->w_cursor = pos;
4763 return;
4764 }
4765 curwin->w_cursor = pos;
4766 }
4767
4768#ifdef FEAT_COMMENTS
4769 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
4770 * comments. */
4771 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
4772 && get_leader_len(old, NULL, FALSE) == 0)
4773 return;
4774#endif
4775
4776 /*
4777 * May start formatting in a previous line, so that after "x" a word is
4778 * moved to the previous line if it fits there now. Only when this is not
4779 * the start of a paragraph.
4780 */
4781 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
4782 {
4783 --curwin->w_cursor.lnum;
4784 if (u_save_cursor() == FAIL)
4785 return;
4786 }
4787
4788 /*
4789 * Do the formatting and restore the cursor position. "saved_cursor" will
4790 * be adjusted for the text formatting.
4791 */
4792 saved_cursor = pos;
4793 format_lines((linenr_T)-1);
4794 curwin->w_cursor = saved_cursor;
4795 saved_cursor.lnum = 0;
4796
4797 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4798 {
4799 /* "cannot happen" */
4800 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4801 coladvance((colnr_T)MAXCOL);
4802 }
4803 else
4804 check_cursor_col();
4805
4806 /* Insert mode: If the cursor is now after the end of the line while it
4807 * previously wasn't, the line was broken. Because of the rule above we
4808 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
4809 * formatted. */
4810 if (!wasatend && has_format_option(FO_WHITE_PAR))
4811 {
4812 new = ml_get_curline();
4813 len = STRLEN(new);
4814 if (curwin->w_cursor.col == len)
4815 {
4816 pnew = vim_strnsave(new, len + 2);
4817 pnew[len] = ' ';
4818 pnew[len + 1] = NUL;
4819 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
4820 /* remove the space later */
4821 did_add_space = TRUE;
4822 }
4823 else
4824 /* may remove added space */
4825 check_auto_format(FALSE);
4826 }
4827
4828 check_cursor();
4829}
4830
4831/*
4832 * When an extra space was added to continue a paragraph for auto-formatting,
4833 * delete it now. The space must be under the cursor, just after the insert
4834 * position.
4835 */
4836 static void
4837check_auto_format(end_insert)
4838 int end_insert; /* TRUE when ending Insert mode */
4839{
4840 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004841 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842
4843 if (did_add_space)
4844 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004845 cc = gchar_cursor();
4846 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 /* Somehow the space was removed already. */
4848 did_add_space = FALSE;
4849 else
4850 {
4851 if (!end_insert)
4852 {
4853 inc_cursor();
4854 c = gchar_cursor();
4855 dec_cursor();
4856 }
4857 if (c != NUL)
4858 {
4859 /* The space is no longer at the end of the line, delete it. */
4860 del_char(FALSE);
4861 did_add_space = FALSE;
4862 }
4863 }
4864 }
4865}
4866
4867/*
4868 * Find out textwidth to be used for formatting:
4869 * if 'textwidth' option is set, use it
4870 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
4871 * if invalid value, use 0.
4872 * Set default to window width (maximum 79) for "gq" operator.
4873 */
4874 int
4875comp_textwidth(ff)
4876 int ff; /* force formatting (for "Q" command) */
4877{
4878 int textwidth;
4879
4880 textwidth = curbuf->b_p_tw;
4881 if (textwidth == 0 && curbuf->b_p_wm)
4882 {
4883 /* The width is the window width minus 'wrapmargin' minus all the
4884 * things that add to the margin. */
4885 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
4886#ifdef FEAT_CMDWIN
4887 if (cmdwin_type != 0)
4888 textwidth -= 1;
4889#endif
4890#ifdef FEAT_FOLDING
4891 textwidth -= curwin->w_p_fdc;
4892#endif
4893#ifdef FEAT_SIGNS
4894 if (curwin->w_buffer->b_signlist != NULL
4895# ifdef FEAT_NETBEANS_INTG
4896 || usingNetbeans
4897# endif
4898 )
4899 textwidth -= 1;
4900#endif
4901 if (curwin->w_p_nu)
4902 textwidth -= 8;
4903 }
4904 if (textwidth < 0)
4905 textwidth = 0;
4906 if (ff && textwidth == 0)
4907 {
4908 textwidth = W_WIDTH(curwin) - 1;
4909 if (textwidth > 79)
4910 textwidth = 79;
4911 }
4912 return textwidth;
4913}
4914
4915/*
4916 * Put a character in the redo buffer, for when just after a CTRL-V.
4917 */
4918 static void
4919redo_literal(c)
4920 int c;
4921{
4922 char_u buf[10];
4923
4924 /* Only digits need special treatment. Translate them into a string of
4925 * three digits. */
4926 if (VIM_ISDIGIT(c))
4927 {
4928 sprintf((char *)buf, "%03d", c);
4929 AppendToRedobuff(buf);
4930 }
4931 else
4932 AppendCharToRedobuff(c);
4933}
4934
4935/*
4936 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00004937 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 */
4939 static void
4940start_arrow(end_insert_pos)
4941 pos_T *end_insert_pos;
4942{
4943 if (!arrow_used) /* something has been inserted */
4944 {
4945 AppendToRedobuff(ESC_STR);
4946 stop_insert(end_insert_pos, FALSE);
4947 arrow_used = TRUE; /* this means we stopped the current insert */
4948 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004949#ifdef FEAT_SYN_HL
4950 check_spell_redraw();
4951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952}
4953
Bram Moolenaar217ad922005-03-20 22:37:15 +00004954#ifdef FEAT_SYN_HL
4955/*
4956 * If we skipped highlighting word at cursor, do it now.
4957 * It may be skipped again, thus reset spell_redraw_lnum first.
4958 */
4959 static void
4960check_spell_redraw()
4961{
4962 if (spell_redraw_lnum != 0)
4963 {
4964 linenr_T lnum = spell_redraw_lnum;
4965
4966 spell_redraw_lnum = 0;
4967 redrawWinline(lnum, FALSE);
4968 }
4969}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00004970
4971/*
4972 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
4973 * spelled word, if there is one.
4974 */
4975 static void
4976spell_back_to_badword()
4977{
4978 pos_T tpos = curwin->w_cursor;
4979
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00004980 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00004981 if (curwin->w_cursor.col != tpos.col)
4982 start_arrow(&tpos);
4983}
Bram Moolenaar217ad922005-03-20 22:37:15 +00004984#endif
4985
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986/*
4987 * stop_arrow() is called before a change is made in insert mode.
4988 * If an arrow key has been used, start a new insertion.
4989 * Returns FAIL if undo is impossible, shouldn't insert then.
4990 */
4991 int
4992stop_arrow()
4993{
4994 if (arrow_used)
4995 {
4996 if (u_save_cursor() == OK)
4997 {
4998 arrow_used = FALSE;
4999 ins_need_undo = FALSE;
5000 }
5001 Insstart = curwin->w_cursor; /* new insertion starts here */
5002 Insstart_textlen = linetabsize(ml_get_curline());
5003 ai_col = 0;
5004#ifdef FEAT_VREPLACE
5005 if (State & VREPLACE_FLAG)
5006 {
5007 orig_line_count = curbuf->b_ml.ml_line_count;
5008 vr_lines_changed = 1;
5009 }
5010#endif
5011 ResetRedobuff();
5012 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
5013 }
5014 else if (ins_need_undo)
5015 {
5016 if (u_save_cursor() == OK)
5017 ins_need_undo = FALSE;
5018 }
5019
5020#ifdef FEAT_FOLDING
5021 /* Always open fold at the cursor line when inserting something. */
5022 foldOpenCursor();
5023#endif
5024
5025 return (arrow_used || ins_need_undo ? FAIL : OK);
5026}
5027
5028/*
5029 * do a few things to stop inserting
5030 */
5031 static void
5032stop_insert(end_insert_pos, esc)
5033 pos_T *end_insert_pos; /* where insert ended */
5034 int esc; /* called by ins_esc() */
5035{
5036 int cc;
5037
5038 stop_redo_ins();
5039 replace_flush(); /* abandon replace stack */
5040
5041 /*
5042 * save the inserted text for later redo with ^@
5043 */
5044 vim_free(last_insert);
5045 last_insert = get_inserted();
5046 last_insert_skip = new_insert_skip;
5047
5048 if (!arrow_used)
5049 {
5050 /* Auto-format now. It may seem strange to do this when stopping an
5051 * insertion (or moving the cursor), but it's required when appending
5052 * a line and having it end in a space. But only do it when something
5053 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005054 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005056 pos_T tpos = curwin->w_cursor;
5057
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 /* When the cursor is at the end of the line after a space the
5059 * formatting will move it to the following word. Avoid that by
5060 * moving the cursor onto the space. */
5061 cc = 'x';
5062 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5063 {
5064 dec_cursor();
5065 cc = gchar_cursor();
5066 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005067 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069
5070 auto_format(TRUE, FALSE);
5071
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005072 if (vim_iswhite(cc))
5073 {
5074 if (gchar_cursor() != NUL)
5075 inc_cursor();
5076#ifdef FEAT_VIRTUALEDIT
5077 /* If the cursor is still at the same character, also keep
5078 * the "coladd". */
5079 if (gchar_cursor() == NUL
5080 && curwin->w_cursor.lnum == tpos.lnum
5081 && curwin->w_cursor.col == tpos.col)
5082 curwin->w_cursor.coladd = tpos.coladd;
5083#endif
5084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086
5087 /* If a space was inserted for auto-formatting, remove it now. */
5088 check_auto_format(TRUE);
5089
5090 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005091 * of the line, and put the cursor back.
5092 * Do this when ESC was used or moving the cursor up/down. */
5093 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5094 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005096 pos_T tpos = curwin->w_cursor;
5097
5098 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5100 --curwin->w_cursor.col;
5101 while (cc = gchar_cursor(), vim_iswhite(cc))
5102 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005103 if (curwin->w_cursor.lnum != tpos.lnum)
5104 curwin->w_cursor = tpos;
5105 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5107
5108#ifdef FEAT_VISUAL
5109 /* <C-S-Right> may have started Visual mode, adjust the position for
5110 * deleted characters. */
5111 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5112 {
5113 cc = STRLEN(ml_get_curline());
5114 if (VIsual.col > (colnr_T)cc)
5115 {
5116 VIsual.col = cc;
5117# ifdef FEAT_VIRTUALEDIT
5118 VIsual.coladd = 0;
5119# endif
5120 }
5121 }
5122#endif
5123 }
5124 }
5125 did_ai = FALSE;
5126#ifdef FEAT_SMARTINDENT
5127 did_si = FALSE;
5128 can_si = FALSE;
5129 can_si_back = FALSE;
5130#endif
5131
5132 /* set '[ and '] to the inserted text */
5133 curbuf->b_op_start = Insstart;
5134 curbuf->b_op_end = *end_insert_pos;
5135}
5136
5137/*
5138 * Set the last inserted text to a single character.
5139 * Used for the replace command.
5140 */
5141 void
5142set_last_insert(c)
5143 int c;
5144{
5145 char_u *s;
5146
5147 vim_free(last_insert);
5148#ifdef FEAT_MBYTE
5149 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5150#else
5151 last_insert = alloc(6);
5152#endif
5153 if (last_insert != NULL)
5154 {
5155 s = last_insert;
5156 /* Use the CTRL-V only when entering a special char */
5157 if (c < ' ' || c == DEL)
5158 *s++ = Ctrl_V;
5159 s = add_char2buf(c, s);
5160 *s++ = ESC;
5161 *s++ = NUL;
5162 last_insert_skip = 0;
5163 }
5164}
5165
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005166#if defined(EXITFREE) || defined(PROTO)
5167 void
5168free_last_insert()
5169{
5170 vim_free(last_insert);
5171 last_insert = NULL;
5172}
5173#endif
5174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175/*
5176 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5177 * and CSI. Handle multi-byte characters.
5178 * Returns a pointer to after the added bytes.
5179 */
5180 char_u *
5181add_char2buf(c, s)
5182 int c;
5183 char_u *s;
5184{
5185#ifdef FEAT_MBYTE
5186 char_u temp[MB_MAXBYTES];
5187 int i;
5188 int len;
5189
5190 len = (*mb_char2bytes)(c, temp);
5191 for (i = 0; i < len; ++i)
5192 {
5193 c = temp[i];
5194#endif
5195 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5196 if (c == K_SPECIAL)
5197 {
5198 *s++ = K_SPECIAL;
5199 *s++ = KS_SPECIAL;
5200 *s++ = KE_FILLER;
5201 }
5202#ifdef FEAT_GUI
5203 else if (c == CSI)
5204 {
5205 *s++ = CSI;
5206 *s++ = KS_EXTRA;
5207 *s++ = (int)KE_CSI;
5208 }
5209#endif
5210 else
5211 *s++ = c;
5212#ifdef FEAT_MBYTE
5213 }
5214#endif
5215 return s;
5216}
5217
5218/*
5219 * move cursor to start of line
5220 * if flags & BL_WHITE move to first non-white
5221 * if flags & BL_SOL move to first non-white if startofline is set,
5222 * otherwise keep "curswant" column
5223 * if flags & BL_FIX don't leave the cursor on a NUL.
5224 */
5225 void
5226beginline(flags)
5227 int flags;
5228{
5229 if ((flags & BL_SOL) && !p_sol)
5230 coladvance(curwin->w_curswant);
5231 else
5232 {
5233 curwin->w_cursor.col = 0;
5234#ifdef FEAT_VIRTUALEDIT
5235 curwin->w_cursor.coladd = 0;
5236#endif
5237
5238 if (flags & (BL_WHITE | BL_SOL))
5239 {
5240 char_u *ptr;
5241
5242 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5243 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5244 ++curwin->w_cursor.col;
5245 }
5246 curwin->w_set_curswant = TRUE;
5247 }
5248}
5249
5250/*
5251 * oneright oneleft cursor_down cursor_up
5252 *
5253 * Move one char {right,left,down,up}.
5254 * Doesn't move onto the NUL past the end of the line.
5255 * Return OK when successful, FAIL when we hit a line of file boundary.
5256 */
5257
5258 int
5259oneright()
5260{
5261 char_u *ptr;
5262#ifdef FEAT_MBYTE
5263 int l;
5264#endif
5265
5266#ifdef FEAT_VIRTUALEDIT
5267 if (virtual_active())
5268 {
5269 pos_T prevpos = curwin->w_cursor;
5270
5271 /* Adjust for multi-wide char (excluding TAB) */
5272 ptr = ml_get_cursor();
5273 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5274#ifdef FEAT_MBYTE
5275 (*mb_ptr2char)(ptr)
5276#else
5277 *ptr
5278#endif
5279 ))
5280 ? ptr2cells(ptr) : 1));
5281 curwin->w_set_curswant = TRUE;
5282 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5283 return (prevpos.col != curwin->w_cursor.col
5284 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5285 }
5286#endif
5287
5288 ptr = ml_get_cursor();
5289#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005290 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 {
5292 /* The character under the cursor is a multi-byte character, move
5293 * several bytes right, but don't end up on the NUL. */
5294 if (ptr[l] == NUL)
5295 return FAIL;
5296 curwin->w_cursor.col += l;
5297 }
5298 else
5299#endif
5300 {
5301 if (*ptr++ == NUL || *ptr == NUL)
5302 return FAIL;
5303 ++curwin->w_cursor.col;
5304 }
5305
5306 curwin->w_set_curswant = TRUE;
5307 return OK;
5308}
5309
5310 int
5311oneleft()
5312{
5313#ifdef FEAT_VIRTUALEDIT
5314 if (virtual_active())
5315 {
5316 int width;
5317 int v = getviscol();
5318
5319 if (v == 0)
5320 return FAIL;
5321
5322# ifdef FEAT_LINEBREAK
5323 /* We might get stuck on 'showbreak', skip over it. */
5324 width = 1;
5325 for (;;)
5326 {
5327 coladvance(v - width);
5328 /* getviscol() is slow, skip it when 'showbreak' is empty and
5329 * there are no multi-byte characters */
5330 if ((*p_sbr == NUL
5331# ifdef FEAT_MBYTE
5332 && !has_mbyte
5333# endif
5334 ) || getviscol() < v)
5335 break;
5336 ++width;
5337 }
5338# else
5339 coladvance(v - 1);
5340# endif
5341
5342 if (curwin->w_cursor.coladd == 1)
5343 {
5344 char_u *ptr;
5345
5346 /* Adjust for multi-wide char (not a TAB) */
5347 ptr = ml_get_cursor();
5348 if (*ptr != TAB && vim_isprintc(
5349# ifdef FEAT_MBYTE
5350 (*mb_ptr2char)(ptr)
5351# else
5352 *ptr
5353# endif
5354 ) && ptr2cells(ptr) > 1)
5355 curwin->w_cursor.coladd = 0;
5356 }
5357
5358 curwin->w_set_curswant = TRUE;
5359 return OK;
5360 }
5361#endif
5362
5363 if (curwin->w_cursor.col == 0)
5364 return FAIL;
5365
5366 curwin->w_set_curswant = TRUE;
5367 --curwin->w_cursor.col;
5368
5369#ifdef FEAT_MBYTE
5370 /* if the character on the left of the current cursor is a multi-byte
5371 * character, move to its first byte */
5372 if (has_mbyte)
5373 mb_adjust_cursor();
5374#endif
5375 return OK;
5376}
5377
5378 int
5379cursor_up(n, upd_topline)
5380 long n;
5381 int upd_topline; /* When TRUE: update topline */
5382{
5383 linenr_T lnum;
5384
5385 if (n > 0)
5386 {
5387 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005388 /* This fails if the cursor is already in the first line or the count
5389 * is larger than the line number and '-' is in 'cpoptions' */
5390 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 return FAIL;
5392 if (n >= lnum)
5393 lnum = 1;
5394 else
5395#ifdef FEAT_FOLDING
5396 if (hasAnyFolding(curwin))
5397 {
5398 /*
5399 * Count each sequence of folded lines as one logical line.
5400 */
5401 /* go to the the start of the current fold */
5402 (void)hasFolding(lnum, &lnum, NULL);
5403
5404 while (n--)
5405 {
5406 /* move up one line */
5407 --lnum;
5408 if (lnum <= 1)
5409 break;
5410 /* If we entered a fold, move to the beginning, unless in
5411 * Insert mode or when 'foldopen' contains "all": it will open
5412 * in a moment. */
5413 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
5414 (void)hasFolding(lnum, &lnum, NULL);
5415 }
5416 if (lnum < 1)
5417 lnum = 1;
5418 }
5419 else
5420#endif
5421 lnum -= n;
5422 curwin->w_cursor.lnum = lnum;
5423 }
5424
5425 /* try to advance to the column we want to be at */
5426 coladvance(curwin->w_curswant);
5427
5428 if (upd_topline)
5429 update_topline(); /* make sure curwin->w_topline is valid */
5430
5431 return OK;
5432}
5433
5434/*
5435 * Cursor down a number of logical lines.
5436 */
5437 int
5438cursor_down(n, upd_topline)
5439 long n;
5440 int upd_topline; /* When TRUE: update topline */
5441{
5442 linenr_T lnum;
5443
5444 if (n > 0)
5445 {
5446 lnum = curwin->w_cursor.lnum;
5447#ifdef FEAT_FOLDING
5448 /* Move to last line of fold, will fail if it's the end-of-file. */
5449 (void)hasFolding(lnum, NULL, &lnum);
5450#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00005451 /* This fails if the cursor is already in the last line or would move
5452 * beyound the last line and '-' is in 'cpoptions' */
5453 if (lnum >= curbuf->b_ml.ml_line_count
5454 || (lnum + n > curbuf->b_ml.ml_line_count
5455 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 return FAIL;
5457 if (lnum + n >= curbuf->b_ml.ml_line_count)
5458 lnum = curbuf->b_ml.ml_line_count;
5459 else
5460#ifdef FEAT_FOLDING
5461 if (hasAnyFolding(curwin))
5462 {
5463 linenr_T last;
5464
5465 /* count each sequence of folded lines as one logical line */
5466 while (n--)
5467 {
5468 if (hasFolding(lnum, NULL, &last))
5469 lnum = last + 1;
5470 else
5471 ++lnum;
5472 if (lnum >= curbuf->b_ml.ml_line_count)
5473 break;
5474 }
5475 if (lnum > curbuf->b_ml.ml_line_count)
5476 lnum = curbuf->b_ml.ml_line_count;
5477 }
5478 else
5479#endif
5480 lnum += n;
5481 curwin->w_cursor.lnum = lnum;
5482 }
5483
5484 /* try to advance to the column we want to be at */
5485 coladvance(curwin->w_curswant);
5486
5487 if (upd_topline)
5488 update_topline(); /* make sure curwin->w_topline is valid */
5489
5490 return OK;
5491}
5492
5493/*
5494 * Stuff the last inserted text in the read buffer.
5495 * Last_insert actually is a copy of the redo buffer, so we
5496 * first have to remove the command.
5497 */
5498 int
5499stuff_inserted(c, count, no_esc)
5500 int c; /* Command character to be inserted */
5501 long count; /* Repeat this many times */
5502 int no_esc; /* Don't add an ESC at the end */
5503{
5504 char_u *esc_ptr;
5505 char_u *ptr;
5506 char_u *last_ptr;
5507 char_u last = NUL;
5508
5509 ptr = get_last_insert();
5510 if (ptr == NULL)
5511 {
5512 EMSG(_(e_noinstext));
5513 return FAIL;
5514 }
5515
5516 /* may want to stuff the command character, to start Insert mode */
5517 if (c != NUL)
5518 stuffcharReadbuff(c);
5519 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
5520 *esc_ptr = NUL; /* remove the ESC */
5521
5522 /* when the last char is either "0" or "^" it will be quoted if no ESC
5523 * comes after it OR if it will inserted more than once and "ptr"
5524 * starts with ^D. -- Acevedo
5525 */
5526 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
5527 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
5528 && (no_esc || (*ptr == Ctrl_D && count > 1)))
5529 {
5530 last = *last_ptr;
5531 *last_ptr = NUL;
5532 }
5533
5534 do
5535 {
5536 stuffReadbuff(ptr);
5537 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
5538 if (last)
5539 stuffReadbuff((char_u *)(last == '0'
5540 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
5541 : IF_EB("\026^", CTRL_V_STR "^")));
5542 }
5543 while (--count > 0);
5544
5545 if (last)
5546 *last_ptr = last;
5547
5548 if (esc_ptr != NULL)
5549 *esc_ptr = ESC; /* put the ESC back */
5550
5551 /* may want to stuff a trailing ESC, to get out of Insert mode */
5552 if (!no_esc)
5553 stuffcharReadbuff(ESC);
5554
5555 return OK;
5556}
5557
5558 char_u *
5559get_last_insert()
5560{
5561 if (last_insert == NULL)
5562 return NULL;
5563 return last_insert + last_insert_skip;
5564}
5565
5566/*
5567 * Get last inserted string, and remove trailing <Esc>.
5568 * Returns pointer to allocated memory (must be freed) or NULL.
5569 */
5570 char_u *
5571get_last_insert_save()
5572{
5573 char_u *s;
5574 int len;
5575
5576 if (last_insert == NULL)
5577 return NULL;
5578 s = vim_strsave(last_insert + last_insert_skip);
5579 if (s != NULL)
5580 {
5581 len = (int)STRLEN(s);
5582 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
5583 s[len - 1] = NUL;
5584 }
5585 return s;
5586}
5587
5588/*
5589 * Check the word in front of the cursor for an abbreviation.
5590 * Called when the non-id character "c" has been entered.
5591 * When an abbreviation is recognized it is removed from the text and
5592 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
5593 */
5594 static int
5595echeck_abbr(c)
5596 int c;
5597{
5598 /* Don't check for abbreviation in paste mode, when disabled and just
5599 * after moving around with cursor keys. */
5600 if (p_paste || no_abbr || arrow_used)
5601 return FALSE;
5602
5603 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
5604 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
5605}
5606
5607/*
5608 * replace-stack functions
5609 *
5610 * When replacing characters, the replaced characters are remembered for each
5611 * new character. This is used to re-insert the old text when backspacing.
5612 *
5613 * There is a NUL headed list of characters for each character that is
5614 * currently in the file after the insertion point. When BS is used, one NUL
5615 * headed list is put back for the deleted character.
5616 *
5617 * For a newline, there are two NUL headed lists. One contains the characters
5618 * that the NL replaced. The extra one stores the characters after the cursor
5619 * that were deleted (always white space).
5620 *
5621 * Replace_offset is normally 0, in which case replace_push will add a new
5622 * character at the end of the stack. If replace_offset is not 0, that many
5623 * characters will be left on the stack above the newly inserted character.
5624 */
5625
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00005626static char_u *replace_stack = NULL;
5627static long replace_stack_nr = 0; /* next entry in replace stack */
5628static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629
5630 void
5631replace_push(c)
5632 int c; /* character that is replaced (NUL is none) */
5633{
5634 char_u *p;
5635
5636 if (replace_stack_nr < replace_offset) /* nothing to do */
5637 return;
5638 if (replace_stack_len <= replace_stack_nr)
5639 {
5640 replace_stack_len += 50;
5641 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
5642 if (p == NULL) /* out of memory */
5643 {
5644 replace_stack_len -= 50;
5645 return;
5646 }
5647 if (replace_stack != NULL)
5648 {
5649 mch_memmove(p, replace_stack,
5650 (size_t)(replace_stack_nr * sizeof(char_u)));
5651 vim_free(replace_stack);
5652 }
5653 replace_stack = p;
5654 }
5655 p = replace_stack + replace_stack_nr - replace_offset;
5656 if (replace_offset)
5657 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
5658 *p = c;
5659 ++replace_stack_nr;
5660}
5661
5662/*
5663 * call replace_push(c) with replace_offset set to the first NUL.
5664 */
5665 static void
5666replace_push_off(c)
5667 int c;
5668{
5669 char_u *p;
5670
5671 p = replace_stack + replace_stack_nr;
5672 for (replace_offset = 1; replace_offset < replace_stack_nr;
5673 ++replace_offset)
5674 if (*--p == NUL)
5675 break;
5676 replace_push(c);
5677 replace_offset = 0;
5678}
5679
5680/*
5681 * Pop one item from the replace stack.
5682 * return -1 if stack empty
5683 * return replaced character or NUL otherwise
5684 */
5685 static int
5686replace_pop()
5687{
5688 if (replace_stack_nr == 0)
5689 return -1;
5690 return (int)replace_stack[--replace_stack_nr];
5691}
5692
5693/*
5694 * Join the top two items on the replace stack. This removes to "off"'th NUL
5695 * encountered.
5696 */
5697 static void
5698replace_join(off)
5699 int off; /* offset for which NUL to remove */
5700{
5701 int i;
5702
5703 for (i = replace_stack_nr; --i >= 0; )
5704 if (replace_stack[i] == NUL && off-- <= 0)
5705 {
5706 --replace_stack_nr;
5707 mch_memmove(replace_stack + i, replace_stack + i + 1,
5708 (size_t)(replace_stack_nr - i));
5709 return;
5710 }
5711}
5712
5713/*
5714 * Pop bytes from the replace stack until a NUL is found, and insert them
5715 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
5716 */
5717 static void
5718replace_pop_ins()
5719{
5720 int cc;
5721 int oldState = State;
5722
5723 State = NORMAL; /* don't want REPLACE here */
5724 while ((cc = replace_pop()) > 0)
5725 {
5726#ifdef FEAT_MBYTE
5727 mb_replace_pop_ins(cc);
5728#else
5729 ins_char(cc);
5730#endif
5731 dec_cursor();
5732 }
5733 State = oldState;
5734}
5735
5736#ifdef FEAT_MBYTE
5737/*
5738 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
5739 * indicates a multi-byte char, pop the other bytes too.
5740 */
5741 static void
5742mb_replace_pop_ins(cc)
5743 int cc;
5744{
5745 int n;
5746 char_u buf[MB_MAXBYTES];
5747 int i;
5748 int c;
5749
5750 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
5751 {
5752 buf[0] = cc;
5753 for (i = 1; i < n; ++i)
5754 buf[i] = replace_pop();
5755 ins_bytes_len(buf, n);
5756 }
5757 else
5758 ins_char(cc);
5759
5760 if (enc_utf8)
5761 /* Handle composing chars. */
5762 for (;;)
5763 {
5764 c = replace_pop();
5765 if (c == -1) /* stack empty */
5766 break;
5767 if ((n = MB_BYTE2LEN(c)) == 1)
5768 {
5769 /* Not a multi-byte char, put it back. */
5770 replace_push(c);
5771 break;
5772 }
5773 else
5774 {
5775 buf[0] = c;
5776 for (i = 1; i < n; ++i)
5777 buf[i] = replace_pop();
5778 if (utf_iscomposing(utf_ptr2char(buf)))
5779 ins_bytes_len(buf, n);
5780 else
5781 {
5782 /* Not a composing char, put it back. */
5783 for (i = n - 1; i >= 0; --i)
5784 replace_push(buf[i]);
5785 break;
5786 }
5787 }
5788 }
5789}
5790#endif
5791
5792/*
5793 * make the replace stack empty
5794 * (called when exiting replace mode)
5795 */
5796 static void
5797replace_flush()
5798{
5799 vim_free(replace_stack);
5800 replace_stack = NULL;
5801 replace_stack_len = 0;
5802 replace_stack_nr = 0;
5803}
5804
5805/*
5806 * Handle doing a BS for one character.
5807 * cc < 0: replace stack empty, just move cursor
5808 * cc == 0: character was inserted, delete it
5809 * cc > 0: character was replaced, put cc (first byte of original char) back
5810 * and check for more characters to be put back
5811 */
5812 static void
5813replace_do_bs()
5814{
5815 int cc;
5816#ifdef FEAT_VREPLACE
5817 int orig_len = 0;
5818 int ins_len;
5819 int orig_vcols = 0;
5820 colnr_T start_vcol;
5821 char_u *p;
5822 int i;
5823 int vcol;
5824#endif
5825
5826 cc = replace_pop();
5827 if (cc > 0)
5828 {
5829#ifdef FEAT_VREPLACE
5830 if (State & VREPLACE_FLAG)
5831 {
5832 /* Get the number of screen cells used by the character we are
5833 * going to delete. */
5834 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
5835 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
5836 }
5837#endif
5838#ifdef FEAT_MBYTE
5839 if (has_mbyte)
5840 {
5841 del_char(FALSE);
5842# ifdef FEAT_VREPLACE
5843 if (State & VREPLACE_FLAG)
5844 orig_len = STRLEN(ml_get_cursor());
5845# endif
5846 replace_push(cc);
5847 }
5848 else
5849#endif
5850 {
5851 pchar_cursor(cc);
5852#ifdef FEAT_VREPLACE
5853 if (State & VREPLACE_FLAG)
5854 orig_len = STRLEN(ml_get_cursor()) - 1;
5855#endif
5856 }
5857 replace_pop_ins();
5858
5859#ifdef FEAT_VREPLACE
5860 if (State & VREPLACE_FLAG)
5861 {
5862 /* Get the number of screen cells used by the inserted characters */
5863 p = ml_get_cursor();
5864 ins_len = STRLEN(p) - orig_len;
5865 vcol = start_vcol;
5866 for (i = 0; i < ins_len; ++i)
5867 {
5868 vcol += chartabsize(p + i, vcol);
5869#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005870 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871#endif
5872 }
5873 vcol -= start_vcol;
5874
5875 /* Delete spaces that were inserted after the cursor to keep the
5876 * text aligned. */
5877 curwin->w_cursor.col += ins_len;
5878 while (vcol > orig_vcols && gchar_cursor() == ' ')
5879 {
5880 del_char(FALSE);
5881 ++orig_vcols;
5882 }
5883 curwin->w_cursor.col -= ins_len;
5884 }
5885#endif
5886
5887 /* mark the buffer as changed and prepare for displaying */
5888 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
5889 }
5890 else if (cc == 0)
5891 (void)del_char(FALSE);
5892}
5893
5894#ifdef FEAT_CINDENT
5895/*
5896 * Return TRUE if C-indenting is on.
5897 */
5898 static int
5899cindent_on()
5900{
5901 return (!p_paste && (curbuf->b_p_cin
5902# ifdef FEAT_EVAL
5903 || *curbuf->b_p_inde != NUL
5904# endif
5905 ));
5906}
5907#endif
5908
5909#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
5910/*
5911 * Re-indent the current line, based on the current contents of it and the
5912 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
5913 * confused what all the part that handles Control-T is doing that I'm not.
5914 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
5915 */
5916
5917 void
5918fixthisline(get_the_indent)
5919 int (*get_the_indent) __ARGS((void));
5920{
5921 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
5922 if (linewhite(curwin->w_cursor.lnum))
5923 did_ai = TRUE; /* delete the indent if the line stays empty */
5924}
5925
5926 void
5927fix_indent()
5928{
5929 if (p_paste)
5930 return;
5931# ifdef FEAT_LISP
5932 if (curbuf->b_p_lisp && curbuf->b_p_ai)
5933 fixthisline(get_lisp_indent);
5934# endif
5935# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
5936 else
5937# endif
5938# ifdef FEAT_CINDENT
5939 if (cindent_on())
5940 do_c_expr_indent();
5941# endif
5942}
5943
5944#endif
5945
5946#ifdef FEAT_CINDENT
5947/*
5948 * return TRUE if 'cinkeys' contains the key "keytyped",
5949 * when == '*': Only if key is preceded with '*' (indent before insert)
5950 * when == '!': Only if key is prededed with '!' (don't insert)
5951 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
5952 *
5953 * "keytyped" can have a few special values:
5954 * KEY_OPEN_FORW
5955 * KEY_OPEN_BACK
5956 * KEY_COMPLETE just finished completion.
5957 *
5958 * If line_is_empty is TRUE accept keys with '0' before them.
5959 */
5960 int
5961in_cinkeys(keytyped, when, line_is_empty)
5962 int keytyped;
5963 int when;
5964 int line_is_empty;
5965{
5966 char_u *look;
5967 int try_match;
5968 int try_match_word;
5969 char_u *p;
5970 char_u *line;
5971 int icase;
5972 int i;
5973
5974#ifdef FEAT_EVAL
5975 if (*curbuf->b_p_inde != NUL)
5976 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
5977 else
5978#endif
5979 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
5980 while (*look)
5981 {
5982 /*
5983 * Find out if we want to try a match with this key, depending on
5984 * 'when' and a '*' or '!' before the key.
5985 */
5986 switch (when)
5987 {
5988 case '*': try_match = (*look == '*'); break;
5989 case '!': try_match = (*look == '!'); break;
5990 default: try_match = (*look != '*'); break;
5991 }
5992 if (*look == '*' || *look == '!')
5993 ++look;
5994
5995 /*
5996 * If there is a '0', only accept a match if the line is empty.
5997 * But may still match when typing last char of a word.
5998 */
5999 if (*look == '0')
6000 {
6001 try_match_word = try_match;
6002 if (!line_is_empty)
6003 try_match = FALSE;
6004 ++look;
6005 }
6006 else
6007 try_match_word = FALSE;
6008
6009 /*
6010 * does it look like a control character?
6011 */
6012 if (*look == '^'
6013#ifdef EBCDIC
6014 && (Ctrl_chr(look[1]) != 0)
6015#else
6016 && look[1] >= '?' && look[1] <= '_'
6017#endif
6018 )
6019 {
6020 if (try_match && keytyped == Ctrl_chr(look[1]))
6021 return TRUE;
6022 look += 2;
6023 }
6024 /*
6025 * 'o' means "o" command, open forward.
6026 * 'O' means "O" command, open backward.
6027 */
6028 else if (*look == 'o')
6029 {
6030 if (try_match && keytyped == KEY_OPEN_FORW)
6031 return TRUE;
6032 ++look;
6033 }
6034 else if (*look == 'O')
6035 {
6036 if (try_match && keytyped == KEY_OPEN_BACK)
6037 return TRUE;
6038 ++look;
6039 }
6040
6041 /*
6042 * 'e' means to check for "else" at start of line and just before the
6043 * cursor.
6044 */
6045 else if (*look == 'e')
6046 {
6047 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6048 {
6049 p = ml_get_curline();
6050 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6051 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6052 return TRUE;
6053 }
6054 ++look;
6055 }
6056
6057 /*
6058 * ':' only causes an indent if it is at the end of a label or case
6059 * statement, or when it was before typing the ':' (to fix
6060 * class::method for C++).
6061 */
6062 else if (*look == ':')
6063 {
6064 if (try_match && keytyped == ':')
6065 {
6066 p = ml_get_curline();
6067 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6068 return TRUE;
6069 if (curwin->w_cursor.col > 2
6070 && p[curwin->w_cursor.col - 1] == ':'
6071 && p[curwin->w_cursor.col - 2] == ':')
6072 {
6073 p[curwin->w_cursor.col - 1] = ' ';
6074 i = (cin_iscase(p) || cin_isscopedecl(p)
6075 || cin_islabel(30));
6076 p = ml_get_curline();
6077 p[curwin->w_cursor.col - 1] = ':';
6078 if (i)
6079 return TRUE;
6080 }
6081 }
6082 ++look;
6083 }
6084
6085
6086 /*
6087 * Is it a key in <>, maybe?
6088 */
6089 else if (*look == '<')
6090 {
6091 if (try_match)
6092 {
6093 /*
6094 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6095 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6096 * >, *, : and ! keys if they really really want to.
6097 */
6098 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6099 && keytyped == look[1])
6100 return TRUE;
6101
6102 if (keytyped == get_special_key_code(look + 1))
6103 return TRUE;
6104 }
6105 while (*look && *look != '>')
6106 look++;
6107 while (*look == '>')
6108 look++;
6109 }
6110
6111 /*
6112 * Is it a word: "=word"?
6113 */
6114 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6115 {
6116 ++look;
6117 if (*look == '~')
6118 {
6119 icase = TRUE;
6120 ++look;
6121 }
6122 else
6123 icase = FALSE;
6124 p = vim_strchr(look, ',');
6125 if (p == NULL)
6126 p = look + STRLEN(look);
6127 if ((try_match || try_match_word)
6128 && curwin->w_cursor.col >= (colnr_T)(p - look))
6129 {
6130 int match = FALSE;
6131
6132#ifdef FEAT_INS_EXPAND
6133 if (keytyped == KEY_COMPLETE)
6134 {
6135 char_u *s;
6136
6137 /* Just completed a word, check if it starts with "look".
6138 * search back for the start of a word. */
6139 line = ml_get_curline();
6140# ifdef FEAT_MBYTE
6141 if (has_mbyte)
6142 {
6143 char_u *n;
6144
6145 for (s = line + curwin->w_cursor.col; s > line; s = n)
6146 {
6147 n = mb_prevptr(line, s);
6148 if (!vim_iswordp(n))
6149 break;
6150 }
6151 }
6152 else
6153# endif
6154 for (s = line + curwin->w_cursor.col; s > line; --s)
6155 if (!vim_iswordc(s[-1]))
6156 break;
6157 if (s + (p - look) <= line + curwin->w_cursor.col
6158 && (icase
6159 ? MB_STRNICMP(s, look, p - look)
6160 : STRNCMP(s, look, p - look)) == 0)
6161 match = TRUE;
6162 }
6163 else
6164#endif
6165 /* TODO: multi-byte */
6166 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6167 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6168 {
6169 line = ml_get_cursor();
6170 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6171 || !vim_iswordc(line[-(p - look) - 1]))
6172 && (icase
6173 ? MB_STRNICMP(line - (p - look), look, p - look)
6174 : STRNCMP(line - (p - look), look, p - look))
6175 == 0)
6176 match = TRUE;
6177 }
6178 if (match && try_match_word && !try_match)
6179 {
6180 /* "0=word": Check if there are only blanks before the
6181 * word. */
6182 line = ml_get_curline();
6183 if ((int)(skipwhite(line) - line) !=
6184 (int)(curwin->w_cursor.col - (p - look)))
6185 match = FALSE;
6186 }
6187 if (match)
6188 return TRUE;
6189 }
6190 look = p;
6191 }
6192
6193 /*
6194 * ok, it's a boring generic character.
6195 */
6196 else
6197 {
6198 if (try_match && *look == keytyped)
6199 return TRUE;
6200 ++look;
6201 }
6202
6203 /*
6204 * Skip over ", ".
6205 */
6206 look = skip_to_option_part(look);
6207 }
6208 return FALSE;
6209}
6210#endif /* FEAT_CINDENT */
6211
6212#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6213/*
6214 * Map Hebrew keyboard when in hkmap mode.
6215 */
6216 int
6217hkmap(c)
6218 int c;
6219{
6220 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6221 {
6222 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6223 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6224 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6225 static char_u map[26] =
6226 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6227 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6228 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6229 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6230 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6231 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6232 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6233 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6234 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6235
6236 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6237 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6238 /* '-1'='sofit' */
6239 else if (c == 'x')
6240 return 'X';
6241 else if (c == 'q')
6242 return '\''; /* {geresh}={'} */
6243 else if (c == 246)
6244 return ' '; /* \"o --> ' ' for a german keyboard */
6245 else if (c == 228)
6246 return ' '; /* \"a --> ' ' -- / -- */
6247 else if (c == 252)
6248 return ' '; /* \"u --> ' ' -- / -- */
6249#ifdef EBCDIC
6250 else if (islower(c))
6251#else
6252 /* NOTE: islower() does not do the right thing for us on Linux so we
6253 * do this the same was as 5.7 and previous, so it works correctly on
6254 * all systems. Specifically, the e.g. Delete and Arrow keys are
6255 * munged and won't work if e.g. searching for Hebrew text.
6256 */
6257 else if (c >= 'a' && c <= 'z')
6258#endif
6259 return (int)(map[CharOrdLow(c)] + p_aleph);
6260 else
6261 return c;
6262 }
6263 else
6264 {
6265 switch (c)
6266 {
6267 case '`': return ';';
6268 case '/': return '.';
6269 case '\'': return ',';
6270 case 'q': return '/';
6271 case 'w': return '\'';
6272
6273 /* Hebrew letters - set offset from 'a' */
6274 case ',': c = '{'; break;
6275 case '.': c = 'v'; break;
6276 case ';': c = 't'; break;
6277 default: {
6278 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6279
6280#ifdef EBCDIC
6281 /* see note about islower() above */
6282 if (!islower(c))
6283#else
6284 if (c < 'a' || c > 'z')
6285#endif
6286 return c;
6287 c = str[CharOrdLow(c)];
6288 break;
6289 }
6290 }
6291
6292 return (int)(CharOrdLow(c) + p_aleph);
6293 }
6294}
6295#endif
6296
6297 static void
6298ins_reg()
6299{
6300 int need_redraw = FALSE;
6301 int regname;
6302 int literally = 0;
6303
6304 /*
6305 * If we are going to wait for a character, show a '"'.
6306 */
6307 pc_status = PC_STATUS_UNSET;
6308 if (redrawing() && !char_avail())
6309 {
6310 /* may need to redraw when no more chars available now */
6311 ins_redraw();
6312
6313 edit_putchar('"', TRUE);
6314#ifdef FEAT_CMDL_INFO
6315 add_to_showcmd_c(Ctrl_R);
6316#endif
6317 }
6318
6319#ifdef USE_ON_FLY_SCROLL
6320 dont_scroll = TRUE; /* disallow scrolling here */
6321#endif
6322
6323 /*
6324 * Don't map the register name. This also prevents the mode message to be
6325 * deleted when ESC is hit.
6326 */
6327 ++no_mapping;
6328 regname = safe_vgetc();
6329#ifdef FEAT_LANGMAP
6330 LANGMAP_ADJUST(regname, TRUE);
6331#endif
6332 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6333 {
6334 /* Get a third key for literal register insertion */
6335 literally = regname;
6336#ifdef FEAT_CMDL_INFO
6337 add_to_showcmd_c(literally);
6338#endif
6339 regname = safe_vgetc();
6340#ifdef FEAT_LANGMAP
6341 LANGMAP_ADJUST(regname, TRUE);
6342#endif
6343 }
6344 --no_mapping;
6345
6346#ifdef FEAT_EVAL
6347 /*
6348 * Don't call u_sync() while getting the expression,
6349 * evaluating it or giving an error message for it!
6350 */
6351 ++no_u_sync;
6352 if (regname == '=')
6353 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006354# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006358# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 /* Restore the Input Method. */
6360 if (im_on)
6361 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006364 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6365 {
6366 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 else
6370 {
6371#endif
6372 if (literally == Ctrl_O || literally == Ctrl_P)
6373 {
6374 /* Append the command to the redo buffer. */
6375 AppendCharToRedobuff(Ctrl_R);
6376 AppendCharToRedobuff(literally);
6377 AppendCharToRedobuff(regname);
6378
6379 do_put(regname, BACKWARD, 1L,
6380 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6381 }
6382 else if (insert_reg(regname, literally) == FAIL)
6383 {
6384 vim_beep();
6385 need_redraw = TRUE; /* remove the '"' */
6386 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006387 else if (stop_insert_mode)
6388 /* When the '=' register was used and a function was invoked that
6389 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
6390 * insert anything, need to remove the '"' */
6391 need_redraw = TRUE;
6392
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393#ifdef FEAT_EVAL
6394 }
6395 --no_u_sync;
6396#endif
6397#ifdef FEAT_CMDL_INFO
6398 clear_showcmd();
6399#endif
6400
6401 /* If the inserted register is empty, we need to remove the '"' */
6402 if (need_redraw || stuff_empty())
6403 edit_unputchar();
6404}
6405
6406/*
6407 * CTRL-G commands in Insert mode.
6408 */
6409 static void
6410ins_ctrl_g()
6411{
6412 int c;
6413
6414#ifdef FEAT_INS_EXPAND
6415 /* Right after CTRL-X the cursor will be after the ruler. */
6416 setcursor();
6417#endif
6418
6419 /*
6420 * Don't map the second key. This also prevents the mode message to be
6421 * deleted when ESC is hit.
6422 */
6423 ++no_mapping;
6424 c = safe_vgetc();
6425 --no_mapping;
6426 switch (c)
6427 {
6428 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
6429 case K_UP:
6430 case Ctrl_K:
6431 case 'k': ins_up(TRUE);
6432 break;
6433
6434 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
6435 case K_DOWN:
6436 case Ctrl_J:
6437 case 'j': ins_down(TRUE);
6438 break;
6439
6440 /* CTRL-G u: start new undoable edit */
6441 case 'u': u_sync();
6442 ins_need_undo = TRUE;
6443 break;
6444
6445 /* Unknown CTRL-G command, reserved for future expansion. */
6446 default: vim_beep();
6447 }
6448}
6449
6450/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006451 * CTRL-^ in Insert mode.
6452 */
6453 static void
6454ins_ctrl_hat()
6455{
6456 if (map_to_exists_mode((char_u *)"", LANGMAP))
6457 {
6458 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
6459 if (State & LANGMAP)
6460 {
6461 curbuf->b_p_iminsert = B_IMODE_NONE;
6462 State &= ~LANGMAP;
6463 }
6464 else
6465 {
6466 curbuf->b_p_iminsert = B_IMODE_LMAP;
6467 State |= LANGMAP;
6468#ifdef USE_IM_CONTROL
6469 im_set_active(FALSE);
6470#endif
6471 }
6472 }
6473#ifdef USE_IM_CONTROL
6474 else
6475 {
6476 /* There are no ":lmap" mappings, toggle IM */
6477 if (im_get_status())
6478 {
6479 curbuf->b_p_iminsert = B_IMODE_NONE;
6480 im_set_active(FALSE);
6481 }
6482 else
6483 {
6484 curbuf->b_p_iminsert = B_IMODE_IM;
6485 State &= ~LANGMAP;
6486 im_set_active(TRUE);
6487 }
6488 }
6489#endif
6490 set_iminsert_global();
6491 showmode();
6492#ifdef FEAT_GUI
6493 /* may show different cursor shape or color */
6494 if (gui.in_use)
6495 gui_update_cursor(TRUE, FALSE);
6496#endif
6497#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6498 /* Show/unshow value of 'keymap' in status lines. */
6499 status_redraw_curbuf();
6500#endif
6501}
6502
6503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 * Handle ESC in insert mode.
6505 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
6506 * insert.
6507 */
6508 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00006509ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 long *count;
6511 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00006512 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
6514 int temp;
6515 static int disabled_redraw = FALSE;
6516
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006517#ifdef FEAT_SYN_HL
6518 check_spell_redraw();
6519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520#if defined(FEAT_HANGULIN)
6521# if defined(ESC_CHG_TO_ENG_MODE)
6522 hangul_input_state_set(0);
6523# endif
6524 if (composing_hangul)
6525 {
6526 push_raw_key(composing_hangul_buffer, 2);
6527 composing_hangul = 0;
6528 }
6529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
6531 temp = curwin->w_cursor.col;
6532 if (disabled_redraw)
6533 {
6534 --RedrawingDisabled;
6535 disabled_redraw = FALSE;
6536 }
6537 if (!arrow_used)
6538 {
6539 /*
6540 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00006541 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
6542 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 */
6544 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00006545 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546
6547 /*
6548 * Repeating insert may take a long time. Check for
6549 * interrupt now and then.
6550 */
6551 if (*count > 0)
6552 {
6553 line_breakcheck();
6554 if (got_int)
6555 *count = 0;
6556 }
6557
6558 if (--*count > 0) /* repeat what was typed */
6559 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006560 /* Vi repeats the insert without replacing characters. */
6561 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
6562 State &= ~REPLACE_FLAG;
6563
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 (void)start_redo_ins();
6565 if (cmdchar == 'r' || cmdchar == 'v')
6566 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
6567 ++RedrawingDisabled;
6568 disabled_redraw = TRUE;
6569 return FALSE; /* repeat the insert */
6570 }
6571 stop_insert(&curwin->w_cursor, TRUE);
6572 undisplay_dollar();
6573 }
6574
6575 /* When an autoindent was removed, curswant stays after the
6576 * indent */
6577 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
6578 curwin->w_set_curswant = TRUE;
6579
6580 /* Remember the last Insert position in the '^ mark. */
6581 if (!cmdmod.keepjumps)
6582 curbuf->b_last_insert = curwin->w_cursor;
6583
6584 /*
6585 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00006586 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00006588 if (!nomove
6589 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590#ifdef FEAT_VIRTUALEDIT
6591 || curwin->w_cursor.coladd > 0
6592#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006593 )
6594 && (restart_edit == NUL
6595 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00006597 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006599 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600#ifdef FEAT_RIGHTLEFT
6601 && !revins_on
6602#endif
6603 )
6604 {
6605#ifdef FEAT_VIRTUALEDIT
6606 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
6607 {
6608 oneleft();
6609 if (restart_edit != NUL)
6610 ++curwin->w_cursor.coladd;
6611 }
6612 else
6613#endif
6614 {
6615 --curwin->w_cursor.col;
6616#ifdef FEAT_MBYTE
6617 /* Correct cursor for multi-byte character. */
6618 if (has_mbyte)
6619 mb_adjust_cursor();
6620#endif
6621 }
6622 }
6623
6624#ifdef USE_IM_CONTROL
6625 /* Disable IM to allow typing English directly for Normal mode commands.
6626 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
6627 * well). */
6628 if (!(State & LANGMAP))
6629 im_save_status(&curbuf->b_p_iminsert);
6630 im_set_active(FALSE);
6631#endif
6632
6633 State = NORMAL;
6634 /* need to position cursor again (e.g. when on a TAB ) */
6635 changed_cline_bef_curs();
6636
6637#ifdef FEAT_MOUSE
6638 setmouse();
6639#endif
6640#ifdef CURSOR_SHAPE
6641 ui_cursor_shape(); /* may show different cursor shape */
6642#endif
6643
6644 /*
6645 * When recording or for CTRL-O, need to display the new mode.
6646 * Otherwise remove the mode message.
6647 */
6648 if (Recording || restart_edit != NUL)
6649 showmode();
6650 else if (p_smd)
6651 MSG("");
6652
6653 return TRUE; /* exit Insert mode */
6654}
6655
6656#ifdef FEAT_RIGHTLEFT
6657/*
6658 * Toggle language: hkmap and revins_on.
6659 * Move to end of reverse inserted text.
6660 */
6661 static void
6662ins_ctrl_()
6663{
6664 if (revins_on && revins_chars && revins_scol >= 0)
6665 {
6666 while (gchar_cursor() != NUL && revins_chars--)
6667 ++curwin->w_cursor.col;
6668 }
6669 p_ri = !p_ri;
6670 revins_on = (State == INSERT && p_ri);
6671 if (revins_on)
6672 {
6673 revins_scol = curwin->w_cursor.col;
6674 revins_legal++;
6675 revins_chars = 0;
6676 undisplay_dollar();
6677 }
6678 else
6679 revins_scol = -1;
6680#ifdef FEAT_FKMAP
6681 if (p_altkeymap)
6682 {
6683 /*
6684 * to be consistent also for redo command, using '.'
6685 * set arrow_used to true and stop it - causing to redo
6686 * characters entered in one mode (normal/reverse insert).
6687 */
6688 arrow_used = TRUE;
6689 (void)stop_arrow();
6690 p_fkmap = curwin->w_p_rl ^ p_ri;
6691 if (p_fkmap && p_ri)
6692 State = INSERT;
6693 }
6694 else
6695#endif
6696 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
6697 showmode();
6698}
6699#endif
6700
6701#ifdef FEAT_VISUAL
6702/*
6703 * If 'keymodel' contains "startsel", may start selection.
6704 * Returns TRUE when a CTRL-O and other keys stuffed.
6705 */
6706 static int
6707ins_start_select(c)
6708 int c;
6709{
6710 if (km_startsel)
6711 switch (c)
6712 {
6713 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 case K_PAGEUP:
6716 case K_KPAGEUP:
6717 case K_PAGEDOWN:
6718 case K_KPAGEDOWN:
6719# ifdef MACOS
6720 case K_LEFT:
6721 case K_RIGHT:
6722 case K_UP:
6723 case K_DOWN:
6724 case K_END:
6725 case K_HOME:
6726# endif
6727 if (!(mod_mask & MOD_MASK_SHIFT))
6728 break;
6729 /* FALLTHROUGH */
6730 case K_S_LEFT:
6731 case K_S_RIGHT:
6732 case K_S_UP:
6733 case K_S_DOWN:
6734 case K_S_END:
6735 case K_S_HOME:
6736 /* Start selection right away, the cursor can move with
6737 * CTRL-O when beyond the end of the line. */
6738 start_selection();
6739
6740 /* Execute the key in (insert) Select mode. */
6741 stuffcharReadbuff(Ctrl_O);
6742 if (mod_mask)
6743 {
6744 char_u buf[4];
6745
6746 buf[0] = K_SPECIAL;
6747 buf[1] = KS_MODIFIER;
6748 buf[2] = mod_mask;
6749 buf[3] = NUL;
6750 stuffReadbuff(buf);
6751 }
6752 stuffcharReadbuff(c);
6753 return TRUE;
6754 }
6755 return FALSE;
6756}
6757#endif
6758
6759/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006760 * <Insert> key in Insert mode: toggle insert/remplace mode.
6761 */
6762 static void
6763ins_insert(replaceState)
6764 int replaceState;
6765{
6766#ifdef FEAT_FKMAP
6767 if (p_fkmap && p_ri)
6768 {
6769 beep_flush();
6770 EMSG(farsi_text_3); /* encoded in Farsi */
6771 return;
6772 }
6773#endif
6774
6775#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00006776# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006777 set_vim_var_string(VV_INSERTMODE,
6778 (char_u *)((State & REPLACE_FLAG) ? "i" :
6779 replaceState == VREPLACE ? "v" : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00006780# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006781 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
6782#endif
6783 if (State & REPLACE_FLAG)
6784 State = INSERT | (State & LANGMAP);
6785 else
6786 State = replaceState | (State & LANGMAP);
6787 AppendCharToRedobuff(K_INS);
6788 showmode();
6789#ifdef CURSOR_SHAPE
6790 ui_cursor_shape(); /* may show different cursor shape */
6791#endif
6792}
6793
6794/*
6795 * Pressed CTRL-O in Insert mode.
6796 */
6797 static void
6798ins_ctrl_o()
6799{
6800#ifdef FEAT_VREPLACE
6801 if (State & VREPLACE_FLAG)
6802 restart_edit = 'V';
6803 else
6804#endif
6805 if (State & REPLACE_FLAG)
6806 restart_edit = 'R';
6807 else
6808 restart_edit = 'I';
6809#ifdef FEAT_VIRTUALEDIT
6810 if (virtual_active())
6811 ins_at_eol = FALSE; /* cursor always keeps its column */
6812 else
6813#endif
6814 ins_at_eol = (gchar_cursor() == NUL);
6815}
6816
6817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 * If the cursor is on an indent, ^T/^D insert/delete one
6819 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
6820 * Always round the indent to 'shiftwith', this is compatible
6821 * with vi. But vi only supports ^T and ^D after an
6822 * autoindent, we support it everywhere.
6823 */
6824 static void
6825ins_shift(c, lastc)
6826 int c;
6827 int lastc;
6828{
6829 if (stop_arrow() == FAIL)
6830 return;
6831 AppendCharToRedobuff(c);
6832
6833 /*
6834 * 0^D and ^^D: remove all indent.
6835 */
6836 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
6837 {
6838 --curwin->w_cursor.col;
6839 (void)del_char(FALSE); /* delete the '^' or '0' */
6840 /* In Replace mode, restore the characters that '^' or '0' replaced. */
6841 if (State & REPLACE_FLAG)
6842 replace_pop_ins();
6843 if (lastc == '^')
6844 old_indent = get_indent(); /* remember curr. indent */
6845 change_indent(INDENT_SET, 0, TRUE, 0);
6846 }
6847 else
6848 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
6849
6850 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
6851 did_ai = FALSE;
6852#ifdef FEAT_SMARTINDENT
6853 did_si = FALSE;
6854 can_si = FALSE;
6855 can_si_back = FALSE;
6856#endif
6857#ifdef FEAT_CINDENT
6858 can_cindent = FALSE; /* no cindenting after ^D or ^T */
6859#endif
6860}
6861
6862 static void
6863ins_del()
6864{
6865 int temp;
6866
6867 if (stop_arrow() == FAIL)
6868 return;
6869 if (gchar_cursor() == NUL) /* delete newline */
6870 {
6871 temp = curwin->w_cursor.col;
6872 if (!can_bs(BS_EOL) /* only if "eol" included */
6873 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
6874 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
6875 || do_join(FALSE) == FAIL)
6876 vim_beep();
6877 else
6878 curwin->w_cursor.col = temp;
6879 }
6880 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
6881 vim_beep();
6882 did_ai = FALSE;
6883#ifdef FEAT_SMARTINDENT
6884 did_si = FALSE;
6885 can_si = FALSE;
6886 can_si_back = FALSE;
6887#endif
6888 AppendCharToRedobuff(K_DEL);
6889}
6890
6891/*
6892 * Handle Backspace, delete-word and delete-line in Insert mode.
6893 * Return TRUE when backspace was actually used.
6894 */
6895 static int
6896ins_bs(c, mode, inserted_space_p)
6897 int c;
6898 int mode;
6899 int *inserted_space_p;
6900{
6901 linenr_T lnum;
6902 int cc;
6903 int temp = 0; /* init for GCC */
6904 colnr_T mincol;
6905 int did_backspace = FALSE;
6906 int in_indent;
6907 int oldState;
6908#ifdef FEAT_MBYTE
6909 int p1, p2;
6910#endif
6911
6912 /*
6913 * can't delete anything in an empty file
6914 * can't backup past first character in buffer
6915 * can't backup past starting point unless 'backspace' > 1
6916 * can backup to a previous line if 'backspace' == 0
6917 */
6918 if ( bufempty()
6919 || (
6920#ifdef FEAT_RIGHTLEFT
6921 !revins_on &&
6922#endif
6923 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
6924 || (!can_bs(BS_START)
6925 && (arrow_used
6926 || (curwin->w_cursor.lnum == Insstart.lnum
6927 && curwin->w_cursor.col <= Insstart.col)))
6928 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
6929 && curwin->w_cursor.col <= ai_col)
6930 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
6931 {
6932 vim_beep();
6933 return FALSE;
6934 }
6935
6936 if (stop_arrow() == FAIL)
6937 return FALSE;
6938 in_indent = inindent(0);
6939#ifdef FEAT_CINDENT
6940 if (in_indent)
6941 can_cindent = FALSE;
6942#endif
6943#ifdef FEAT_COMMENTS
6944 end_comment_pending = NUL; /* After BS, don't auto-end comment */
6945#endif
6946#ifdef FEAT_RIGHTLEFT
6947 if (revins_on) /* put cursor after last inserted char */
6948 inc_cursor();
6949#endif
6950
6951#ifdef FEAT_VIRTUALEDIT
6952 /* Virtualedit:
6953 * BACKSPACE_CHAR eats a virtual space
6954 * BACKSPACE_WORD eats all coladd
6955 * BACKSPACE_LINE eats all coladd and keeps going
6956 */
6957 if (curwin->w_cursor.coladd > 0)
6958 {
6959 if (mode == BACKSPACE_CHAR)
6960 {
6961 --curwin->w_cursor.coladd;
6962 return TRUE;
6963 }
6964 if (mode == BACKSPACE_WORD)
6965 {
6966 curwin->w_cursor.coladd = 0;
6967 return TRUE;
6968 }
6969 curwin->w_cursor.coladd = 0;
6970 }
6971#endif
6972
6973 /*
6974 * delete newline!
6975 */
6976 if (curwin->w_cursor.col == 0)
6977 {
6978 lnum = Insstart.lnum;
6979 if (curwin->w_cursor.lnum == Insstart.lnum
6980#ifdef FEAT_RIGHTLEFT
6981 || revins_on
6982#endif
6983 )
6984 {
6985 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
6986 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
6987 return FALSE;
6988 --Insstart.lnum;
6989 Insstart.col = MAXCOL;
6990 }
6991 /*
6992 * In replace mode:
6993 * cc < 0: NL was inserted, delete it
6994 * cc >= 0: NL was replaced, put original characters back
6995 */
6996 cc = -1;
6997 if (State & REPLACE_FLAG)
6998 cc = replace_pop(); /* returns -1 if NL was inserted */
6999 /*
7000 * In replace mode, in the line we started replacing, we only move the
7001 * cursor.
7002 */
7003 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7004 {
7005 dec_cursor();
7006 }
7007 else
7008 {
7009#ifdef FEAT_VREPLACE
7010 if (!(State & VREPLACE_FLAG)
7011 || curwin->w_cursor.lnum > orig_line_count)
7012#endif
7013 {
7014 temp = gchar_cursor(); /* remember current char */
7015 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007016
7017 /* When "aw" is in 'formatoptions' we must delete the space at
7018 * the end of the line, otherwise the line will be broken
7019 * again when auto-formatting. */
7020 if (has_format_option(FO_AUTO)
7021 && has_format_option(FO_WHITE_PAR))
7022 {
7023 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7024 TRUE);
7025 int len;
7026
7027 len = STRLEN(ptr);
7028 if (len > 0 && ptr[len - 1] == ' ')
7029 ptr[len - 1] = NUL;
7030 }
7031
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 (void)do_join(FALSE);
7033 if (temp == NUL && gchar_cursor() != NUL)
7034 inc_cursor();
7035 }
7036#ifdef FEAT_VREPLACE
7037 else
7038 dec_cursor();
7039#endif
7040
7041 /*
7042 * In REPLACE mode we have to put back the text that was replaced
7043 * by the NL. On the replace stack is first a NUL-terminated
7044 * sequence of characters that were deleted and then the
7045 * characters that NL replaced.
7046 */
7047 if (State & REPLACE_FLAG)
7048 {
7049 /*
7050 * Do the next ins_char() in NORMAL state, to
7051 * prevent ins_char() from replacing characters and
7052 * avoiding showmatch().
7053 */
7054 oldState = State;
7055 State = NORMAL;
7056 /*
7057 * restore characters (blanks) deleted after cursor
7058 */
7059 while (cc > 0)
7060 {
7061 temp = curwin->w_cursor.col;
7062#ifdef FEAT_MBYTE
7063 mb_replace_pop_ins(cc);
7064#else
7065 ins_char(cc);
7066#endif
7067 curwin->w_cursor.col = temp;
7068 cc = replace_pop();
7069 }
7070 /* restore the characters that NL replaced */
7071 replace_pop_ins();
7072 State = oldState;
7073 }
7074 }
7075 did_ai = FALSE;
7076 }
7077 else
7078 {
7079 /*
7080 * Delete character(s) before the cursor.
7081 */
7082#ifdef FEAT_RIGHTLEFT
7083 if (revins_on) /* put cursor on last inserted char */
7084 dec_cursor();
7085#endif
7086 mincol = 0;
7087 /* keep indent */
7088 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7089#ifdef FEAT_RIGHTLEFT
7090 && !revins_on
7091#endif
7092 )
7093 {
7094 temp = curwin->w_cursor.col;
7095 beginline(BL_WHITE);
7096 if (curwin->w_cursor.col < (colnr_T)temp)
7097 mincol = curwin->w_cursor.col;
7098 curwin->w_cursor.col = temp;
7099 }
7100
7101 /*
7102 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7103 */
7104 if ( mode == BACKSPACE_CHAR
7105 && ((p_sta && in_indent)
7106 || (curbuf->b_p_sts
7107 && (*(ml_get_cursor() - 1) == TAB
7108 || (*(ml_get_cursor() - 1) == ' '
7109 && (!*inserted_space_p
7110 || arrow_used))))))
7111 {
7112 int ts;
7113 colnr_T vcol;
7114 colnr_T want_vcol;
7115 int extra = 0;
7116
7117 *inserted_space_p = FALSE;
7118 if (p_sta)
7119 ts = curbuf->b_p_sw;
7120 else
7121 ts = curbuf->b_p_sts;
7122 /* Compute the virtual column where we want to be. Since
7123 * 'showbreak' may get in the way, need to get the last column of
7124 * the previous character. */
7125 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7126 dec_cursor();
7127 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7128 inc_cursor();
7129 want_vcol = (want_vcol / ts) * ts;
7130
7131 /* delete characters until we are at or before want_vcol */
7132 while (vcol > want_vcol
7133 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7134 {
7135 dec_cursor();
7136 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7137 if (State & REPLACE_FLAG)
7138 {
7139 /* Don't delete characters before the insert point when in
7140 * Replace mode */
7141 if (curwin->w_cursor.lnum != Insstart.lnum
7142 || curwin->w_cursor.col >= Insstart.col)
7143 {
7144#if 0 /* what was this for? It causes problems when sw != ts. */
7145 if (State == REPLACE && (int)vcol < want_vcol)
7146 {
7147 (void)del_char(FALSE);
7148 extra = 2; /* don't pop too much */
7149 }
7150 else
7151#endif
7152 replace_do_bs();
7153 }
7154 }
7155 else
7156 (void)del_char(FALSE);
7157 }
7158
7159 /* insert extra spaces until we are at want_vcol */
7160 while (vcol < want_vcol)
7161 {
7162 /* Remember the first char we inserted */
7163 if (curwin->w_cursor.lnum == Insstart.lnum
7164 && curwin->w_cursor.col < Insstart.col)
7165 Insstart.col = curwin->w_cursor.col;
7166
7167#ifdef FEAT_VREPLACE
7168 if (State & VREPLACE_FLAG)
7169 ins_char(' ');
7170 else
7171#endif
7172 {
7173 ins_str((char_u *)" ");
7174 if ((State & REPLACE_FLAG) && extra <= 1)
7175 {
7176 if (extra)
7177 replace_push_off(NUL);
7178 else
7179 replace_push(NUL);
7180 }
7181 if (extra == 2)
7182 extra = 1;
7183 }
7184 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7185 }
7186 }
7187
7188 /*
7189 * Delete upto starting point, start of line or previous word.
7190 */
7191 else do
7192 {
7193#ifdef FEAT_RIGHTLEFT
7194 if (!revins_on) /* put cursor on char to be deleted */
7195#endif
7196 dec_cursor();
7197
7198 /* start of word? */
7199 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7200 {
7201 mode = BACKSPACE_WORD_NOT_SPACE;
7202 temp = vim_iswordc(gchar_cursor());
7203 }
7204 /* end of word? */
7205 else if (mode == BACKSPACE_WORD_NOT_SPACE
7206 && (vim_isspace(cc = gchar_cursor())
7207 || vim_iswordc(cc) != temp))
7208 {
7209#ifdef FEAT_RIGHTLEFT
7210 if (!revins_on)
7211#endif
7212 inc_cursor();
7213#ifdef FEAT_RIGHTLEFT
7214 else if (State & REPLACE_FLAG)
7215 dec_cursor();
7216#endif
7217 break;
7218 }
7219 if (State & REPLACE_FLAG)
7220 replace_do_bs();
7221 else
7222 {
7223#ifdef FEAT_MBYTE
7224 if (enc_utf8 && p_deco)
7225 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7226#endif
7227 (void)del_char(FALSE);
7228#ifdef FEAT_MBYTE
7229 /*
7230 * If p1 or p2 is non-zero, there are combining characters we
7231 * need to take account of. Don't back up before the base
7232 * character.
7233 */
7234 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7235 inc_cursor();
7236#endif
7237#ifdef FEAT_RIGHTLEFT
7238 if (revins_chars)
7239 {
7240 revins_chars--;
7241 revins_legal++;
7242 }
7243 if (revins_on && gchar_cursor() == NUL)
7244 break;
7245#endif
7246 }
7247 /* Just a single backspace?: */
7248 if (mode == BACKSPACE_CHAR)
7249 break;
7250 } while (
7251#ifdef FEAT_RIGHTLEFT
7252 revins_on ||
7253#endif
7254 (curwin->w_cursor.col > mincol
7255 && (curwin->w_cursor.lnum != Insstart.lnum
7256 || curwin->w_cursor.col != Insstart.col)));
7257 did_backspace = TRUE;
7258 }
7259#ifdef FEAT_SMARTINDENT
7260 did_si = FALSE;
7261 can_si = FALSE;
7262 can_si_back = FALSE;
7263#endif
7264 if (curwin->w_cursor.col <= 1)
7265 did_ai = FALSE;
7266 /*
7267 * It's a little strange to put backspaces into the redo
7268 * buffer, but it makes auto-indent a lot easier to deal
7269 * with.
7270 */
7271 AppendCharToRedobuff(c);
7272
7273 /* If deleted before the insertion point, adjust it */
7274 if (curwin->w_cursor.lnum == Insstart.lnum
7275 && curwin->w_cursor.col < Insstart.col)
7276 Insstart.col = curwin->w_cursor.col;
7277
7278 /* vi behaviour: the cursor moves backward but the character that
7279 * was there remains visible
7280 * Vim behaviour: the cursor moves backward and the character that
7281 * was there is erased from the screen.
7282 * We can emulate the vi behaviour by pretending there is a dollar
7283 * displayed even when there isn't.
7284 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7285 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7286 dollar_vcol = curwin->w_virtcol;
7287
7288 return did_backspace;
7289}
7290
7291#ifdef FEAT_MOUSE
7292 static void
7293ins_mouse(c)
7294 int c;
7295{
7296 pos_T tpos;
7297
7298# ifdef FEAT_GUI
7299 /* When GUI is active, also move/paste when 'mouse' is empty */
7300 if (!gui.in_use)
7301# endif
7302 if (!mouse_has(MOUSE_INSERT))
7303 return;
7304
7305 undisplay_dollar();
7306 tpos = curwin->w_cursor;
7307 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7308 {
7309 start_arrow(&tpos);
7310# ifdef FEAT_CINDENT
7311 can_cindent = TRUE;
7312# endif
7313 }
7314
7315#ifdef FEAT_WINDOWS
7316 /* redraw status lines (in case another window became active) */
7317 redraw_statuslines();
7318#endif
7319}
7320
7321 static void
7322ins_mousescroll(up)
7323 int up;
7324{
7325 pos_T tpos;
7326# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7327 win_T *old_curwin;
7328# endif
7329
7330 tpos = curwin->w_cursor;
7331
7332# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7333 old_curwin = curwin;
7334
7335 /* Currently the mouse coordinates are only known in the GUI. */
7336 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7337 {
7338 int row, col;
7339
7340 row = mouse_row;
7341 col = mouse_col;
7342
7343 /* find the window at the pointer coordinates */
7344 curwin = mouse_find_win(&row, &col);
7345 curbuf = curwin->w_buffer;
7346 }
7347 if (curwin == old_curwin)
7348# endif
7349 undisplay_dollar();
7350
7351 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7352 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7353 else
7354 scroll_redraw(up, 3L);
7355
7356# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7357 curwin->w_redr_status = TRUE;
7358
7359 curwin = old_curwin;
7360 curbuf = curwin->w_buffer;
7361# endif
7362
7363 if (!equalpos(curwin->w_cursor, tpos))
7364 {
7365 start_arrow(&tpos);
7366# ifdef FEAT_CINDENT
7367 can_cindent = TRUE;
7368# endif
7369 }
7370}
7371#endif
7372
7373#ifdef FEAT_GUI
7374 void
7375ins_scroll()
7376{
7377 pos_T tpos;
7378
7379 undisplay_dollar();
7380 tpos = curwin->w_cursor;
7381 if (gui_do_scroll())
7382 {
7383 start_arrow(&tpos);
7384# ifdef FEAT_CINDENT
7385 can_cindent = TRUE;
7386# endif
7387 }
7388}
7389
7390 void
7391ins_horscroll()
7392{
7393 pos_T tpos;
7394
7395 undisplay_dollar();
7396 tpos = curwin->w_cursor;
7397 if (gui_do_horiz_scroll())
7398 {
7399 start_arrow(&tpos);
7400# ifdef FEAT_CINDENT
7401 can_cindent = TRUE;
7402# endif
7403 }
7404}
7405#endif
7406
7407 static void
7408ins_left()
7409{
7410 pos_T tpos;
7411
7412#ifdef FEAT_FOLDING
7413 if ((fdo_flags & FDO_HOR) && KeyTyped)
7414 foldOpenCursor();
7415#endif
7416 undisplay_dollar();
7417 tpos = curwin->w_cursor;
7418 if (oneleft() == OK)
7419 {
7420 start_arrow(&tpos);
7421#ifdef FEAT_RIGHTLEFT
7422 /* If exit reversed string, position is fixed */
7423 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
7424 revins_legal++;
7425 revins_chars++;
7426#endif
7427 }
7428
7429 /*
7430 * if 'whichwrap' set for cursor in insert mode may go to
7431 * previous line
7432 */
7433 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
7434 {
7435 start_arrow(&tpos);
7436 --(curwin->w_cursor.lnum);
7437 coladvance((colnr_T)MAXCOL);
7438 curwin->w_set_curswant = TRUE; /* so we stay at the end */
7439 }
7440 else
7441 vim_beep();
7442}
7443
7444 static void
7445ins_home(c)
7446 int c;
7447{
7448 pos_T tpos;
7449
7450#ifdef FEAT_FOLDING
7451 if ((fdo_flags & FDO_HOR) && KeyTyped)
7452 foldOpenCursor();
7453#endif
7454 undisplay_dollar();
7455 tpos = curwin->w_cursor;
7456 if (c == K_C_HOME)
7457 curwin->w_cursor.lnum = 1;
7458 curwin->w_cursor.col = 0;
7459#ifdef FEAT_VIRTUALEDIT
7460 curwin->w_cursor.coladd = 0;
7461#endif
7462 curwin->w_curswant = 0;
7463 start_arrow(&tpos);
7464}
7465
7466 static void
7467ins_end(c)
7468 int c;
7469{
7470 pos_T tpos;
7471
7472#ifdef FEAT_FOLDING
7473 if ((fdo_flags & FDO_HOR) && KeyTyped)
7474 foldOpenCursor();
7475#endif
7476 undisplay_dollar();
7477 tpos = curwin->w_cursor;
7478 if (c == K_C_END)
7479 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7480 coladvance((colnr_T)MAXCOL);
7481 curwin->w_curswant = MAXCOL;
7482
7483 start_arrow(&tpos);
7484}
7485
7486 static void
7487ins_s_left()
7488{
7489#ifdef FEAT_FOLDING
7490 if ((fdo_flags & FDO_HOR) && KeyTyped)
7491 foldOpenCursor();
7492#endif
7493 undisplay_dollar();
7494 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
7495 {
7496 start_arrow(&curwin->w_cursor);
7497 (void)bck_word(1L, FALSE, FALSE);
7498 curwin->w_set_curswant = TRUE;
7499 }
7500 else
7501 vim_beep();
7502}
7503
7504 static void
7505ins_right()
7506{
7507#ifdef FEAT_FOLDING
7508 if ((fdo_flags & FDO_HOR) && KeyTyped)
7509 foldOpenCursor();
7510#endif
7511 undisplay_dollar();
7512 if (gchar_cursor() != NUL || virtual_active()
7513 )
7514 {
7515 start_arrow(&curwin->w_cursor);
7516 curwin->w_set_curswant = TRUE;
7517#ifdef FEAT_VIRTUALEDIT
7518 if (virtual_active())
7519 oneright();
7520 else
7521#endif
7522 {
7523#ifdef FEAT_MBYTE
7524 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007525 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 else
7527#endif
7528 ++curwin->w_cursor.col;
7529 }
7530
7531#ifdef FEAT_RIGHTLEFT
7532 revins_legal++;
7533 if (revins_chars)
7534 revins_chars--;
7535#endif
7536 }
7537 /* if 'whichwrap' set for cursor in insert mode, may move the
7538 * cursor to the next line */
7539 else if (vim_strchr(p_ww, ']') != NULL
7540 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7541 {
7542 start_arrow(&curwin->w_cursor);
7543 curwin->w_set_curswant = TRUE;
7544 ++curwin->w_cursor.lnum;
7545 curwin->w_cursor.col = 0;
7546 }
7547 else
7548 vim_beep();
7549}
7550
7551 static void
7552ins_s_right()
7553{
7554#ifdef FEAT_FOLDING
7555 if ((fdo_flags & FDO_HOR) && KeyTyped)
7556 foldOpenCursor();
7557#endif
7558 undisplay_dollar();
7559 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
7560 || gchar_cursor() != NUL)
7561 {
7562 start_arrow(&curwin->w_cursor);
7563 (void)fwd_word(1L, FALSE, 0);
7564 curwin->w_set_curswant = TRUE;
7565 }
7566 else
7567 vim_beep();
7568}
7569
7570 static void
7571ins_up(startcol)
7572 int startcol; /* when TRUE move to Insstart.col */
7573{
7574 pos_T tpos;
7575 linenr_T old_topline = curwin->w_topline;
7576#ifdef FEAT_DIFF
7577 int old_topfill = curwin->w_topfill;
7578#endif
7579
7580 undisplay_dollar();
7581 tpos = curwin->w_cursor;
7582 if (cursor_up(1L, TRUE) == OK)
7583 {
7584 if (startcol)
7585 coladvance(getvcol_nolist(&Insstart));
7586 if (old_topline != curwin->w_topline
7587#ifdef FEAT_DIFF
7588 || old_topfill != curwin->w_topfill
7589#endif
7590 )
7591 redraw_later(VALID);
7592 start_arrow(&tpos);
7593#ifdef FEAT_CINDENT
7594 can_cindent = TRUE;
7595#endif
7596 }
7597 else
7598 vim_beep();
7599}
7600
7601 static void
7602ins_pageup()
7603{
7604 pos_T tpos;
7605
7606 undisplay_dollar();
7607 tpos = curwin->w_cursor;
7608 if (onepage(BACKWARD, 1L) == OK)
7609 {
7610 start_arrow(&tpos);
7611#ifdef FEAT_CINDENT
7612 can_cindent = TRUE;
7613#endif
7614 }
7615 else
7616 vim_beep();
7617}
7618
7619 static void
7620ins_down(startcol)
7621 int startcol; /* when TRUE move to Insstart.col */
7622{
7623 pos_T tpos;
7624 linenr_T old_topline = curwin->w_topline;
7625#ifdef FEAT_DIFF
7626 int old_topfill = curwin->w_topfill;
7627#endif
7628
7629 undisplay_dollar();
7630 tpos = curwin->w_cursor;
7631 if (cursor_down(1L, TRUE) == OK)
7632 {
7633 if (startcol)
7634 coladvance(getvcol_nolist(&Insstart));
7635 if (old_topline != curwin->w_topline
7636#ifdef FEAT_DIFF
7637 || old_topfill != curwin->w_topfill
7638#endif
7639 )
7640 redraw_later(VALID);
7641 start_arrow(&tpos);
7642#ifdef FEAT_CINDENT
7643 can_cindent = TRUE;
7644#endif
7645 }
7646 else
7647 vim_beep();
7648}
7649
7650 static void
7651ins_pagedown()
7652{
7653 pos_T tpos;
7654
7655 undisplay_dollar();
7656 tpos = curwin->w_cursor;
7657 if (onepage(FORWARD, 1L) == OK)
7658 {
7659 start_arrow(&tpos);
7660#ifdef FEAT_CINDENT
7661 can_cindent = TRUE;
7662#endif
7663 }
7664 else
7665 vim_beep();
7666}
7667
7668#ifdef FEAT_DND
7669 static void
7670ins_drop()
7671{
7672 do_put('~', BACKWARD, 1L, PUT_CURSEND);
7673}
7674#endif
7675
7676/*
7677 * Handle TAB in Insert or Replace mode.
7678 * Return TRUE when the TAB needs to be inserted like a normal character.
7679 */
7680 static int
7681ins_tab()
7682{
7683 int ind;
7684 int i;
7685 int temp;
7686
7687 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
7688 Insstart_blank_vcol = get_nolist_virtcol();
7689 if (echeck_abbr(TAB + ABBR_OFF))
7690 return FALSE;
7691
7692 ind = inindent(0);
7693#ifdef FEAT_CINDENT
7694 if (ind)
7695 can_cindent = FALSE;
7696#endif
7697
7698 /*
7699 * When nothing special, insert TAB like a normal character
7700 */
7701 if (!curbuf->b_p_et
7702 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
7703 && curbuf->b_p_sts == 0)
7704 return TRUE;
7705
7706 if (stop_arrow() == FAIL)
7707 return TRUE;
7708
7709 did_ai = FALSE;
7710#ifdef FEAT_SMARTINDENT
7711 did_si = FALSE;
7712 can_si = FALSE;
7713 can_si_back = FALSE;
7714#endif
7715 AppendToRedobuff((char_u *)"\t");
7716
7717 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
7718 temp = (int)curbuf->b_p_sw;
7719 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
7720 temp = (int)curbuf->b_p_sts;
7721 else /* otherwise use 'tabstop' */
7722 temp = (int)curbuf->b_p_ts;
7723 temp -= get_nolist_virtcol() % temp;
7724
7725 /*
7726 * Insert the first space with ins_char(). It will delete one char in
7727 * replace mode. Insert the rest with ins_str(); it will not delete any
7728 * chars. For VREPLACE mode, we use ins_char() for all characters.
7729 */
7730 ins_char(' ');
7731 while (--temp > 0)
7732 {
7733#ifdef FEAT_VREPLACE
7734 if (State & VREPLACE_FLAG)
7735 ins_char(' ');
7736 else
7737#endif
7738 {
7739 ins_str((char_u *)" ");
7740 if (State & REPLACE_FLAG) /* no char replaced */
7741 replace_push(NUL);
7742 }
7743 }
7744
7745 /*
7746 * When 'expandtab' not set: Replace spaces by TABs where possible.
7747 */
7748 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
7749 {
7750 char_u *ptr;
7751#ifdef FEAT_VREPLACE
7752 char_u *saved_line = NULL; /* init for GCC */
7753 pos_T pos;
7754#endif
7755 pos_T fpos;
7756 pos_T *cursor;
7757 colnr_T want_vcol, vcol;
7758 int change_col = -1;
7759 int save_list = curwin->w_p_list;
7760
7761 /*
7762 * Get the current line. For VREPLACE mode, don't make real changes
7763 * yet, just work on a copy of the line.
7764 */
7765#ifdef FEAT_VREPLACE
7766 if (State & VREPLACE_FLAG)
7767 {
7768 pos = curwin->w_cursor;
7769 cursor = &pos;
7770 saved_line = vim_strsave(ml_get_curline());
7771 if (saved_line == NULL)
7772 return FALSE;
7773 ptr = saved_line + pos.col;
7774 }
7775 else
7776#endif
7777 {
7778 ptr = ml_get_cursor();
7779 cursor = &curwin->w_cursor;
7780 }
7781
7782 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
7783 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
7784 curwin->w_p_list = FALSE;
7785
7786 /* Find first white before the cursor */
7787 fpos = curwin->w_cursor;
7788 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
7789 {
7790 --fpos.col;
7791 --ptr;
7792 }
7793
7794 /* In Replace mode, don't change characters before the insert point. */
7795 if ((State & REPLACE_FLAG)
7796 && fpos.lnum == Insstart.lnum
7797 && fpos.col < Insstart.col)
7798 {
7799 ptr += Insstart.col - fpos.col;
7800 fpos.col = Insstart.col;
7801 }
7802
7803 /* compute virtual column numbers of first white and cursor */
7804 getvcol(curwin, &fpos, &vcol, NULL, NULL);
7805 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
7806
7807 /* Use as many TABs as possible. Beware of 'showbreak' and
7808 * 'linebreak' adding extra virtual columns. */
7809 while (vim_iswhite(*ptr))
7810 {
7811 i = lbr_chartabsize((char_u *)"\t", vcol);
7812 if (vcol + i > want_vcol)
7813 break;
7814 if (*ptr != TAB)
7815 {
7816 *ptr = TAB;
7817 if (change_col < 0)
7818 {
7819 change_col = fpos.col; /* Column of first change */
7820 /* May have to adjust Insstart */
7821 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
7822 Insstart.col = fpos.col;
7823 }
7824 }
7825 ++fpos.col;
7826 ++ptr;
7827 vcol += i;
7828 }
7829
7830 if (change_col >= 0)
7831 {
7832 int repl_off = 0;
7833
7834 /* Skip over the spaces we need. */
7835 while (vcol < want_vcol && *ptr == ' ')
7836 {
7837 vcol += lbr_chartabsize(ptr, vcol);
7838 ++ptr;
7839 ++repl_off;
7840 }
7841 if (vcol > want_vcol)
7842 {
7843 /* Must have a char with 'showbreak' just before it. */
7844 --ptr;
7845 --repl_off;
7846 }
7847 fpos.col += repl_off;
7848
7849 /* Delete following spaces. */
7850 i = cursor->col - fpos.col;
7851 if (i > 0)
7852 {
7853 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
7854 /* correct replace stack. */
7855 if ((State & REPLACE_FLAG)
7856#ifdef FEAT_VREPLACE
7857 && !(State & VREPLACE_FLAG)
7858#endif
7859 )
7860 for (temp = i; --temp >= 0; )
7861 replace_join(repl_off);
7862 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00007863#ifdef FEAT_NETBEANS_INTG
7864 if (usingNetbeans)
7865 {
7866 netbeans_removed(curbuf, fpos.lnum, cursor->col,
7867 (long)(i + 1));
7868 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
7869 (char_u *)"\t", 1);
7870 }
7871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 cursor->col -= i;
7873
7874#ifdef FEAT_VREPLACE
7875 /*
7876 * In VREPLACE mode, we haven't changed anything yet. Do it now by
7877 * backspacing over the changed spacing and then inserting the new
7878 * spacing.
7879 */
7880 if (State & VREPLACE_FLAG)
7881 {
7882 /* Backspace from real cursor to change_col */
7883 backspace_until_column(change_col);
7884
7885 /* Insert each char in saved_line from changed_col to
7886 * ptr-cursor */
7887 ins_bytes_len(saved_line + change_col,
7888 cursor->col - change_col);
7889 }
7890#endif
7891 }
7892
7893#ifdef FEAT_VREPLACE
7894 if (State & VREPLACE_FLAG)
7895 vim_free(saved_line);
7896#endif
7897 curwin->w_p_list = save_list;
7898 }
7899
7900 return FALSE;
7901}
7902
7903/*
7904 * Handle CR or NL in insert mode.
7905 * Return TRUE when out of memory or can't undo.
7906 */
7907 static int
7908ins_eol(c)
7909 int c;
7910{
7911 int i;
7912
7913 if (echeck_abbr(c + ABBR_OFF))
7914 return FALSE;
7915 if (stop_arrow() == FAIL)
7916 return TRUE;
7917 undisplay_dollar();
7918
7919 /*
7920 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
7921 * character under the cursor. Only push a NUL on the replace stack,
7922 * nothing to put back when the NL is deleted.
7923 */
7924 if ((State & REPLACE_FLAG)
7925#ifdef FEAT_VREPLACE
7926 && !(State & VREPLACE_FLAG)
7927#endif
7928 )
7929 replace_push(NUL);
7930
7931 /*
7932 * In VREPLACE mode, a NL replaces the rest of the line, and starts
7933 * replacing the next line, so we push all of the characters left on the
7934 * line onto the replace stack. This is not done here though, it is done
7935 * in open_line().
7936 */
7937
7938#ifdef FEAT_RIGHTLEFT
7939# ifdef FEAT_FKMAP
7940 if (p_altkeymap && p_fkmap)
7941 fkmap(NL);
7942# endif
7943 /* NL in reverse insert will always start in the end of
7944 * current line. */
7945 if (revins_on)
7946 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7947#endif
7948
7949 AppendToRedobuff(NL_STR);
7950 i = open_line(FORWARD,
7951#ifdef FEAT_COMMENTS
7952 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
7953#endif
7954 0, old_indent);
7955 old_indent = 0;
7956#ifdef FEAT_CINDENT
7957 can_cindent = TRUE;
7958#endif
7959
7960 return (!i);
7961}
7962
7963#ifdef FEAT_DIGRAPHS
7964/*
7965 * Handle digraph in insert mode.
7966 * Returns character still to be inserted, or NUL when nothing remaining to be
7967 * done.
7968 */
7969 static int
7970ins_digraph()
7971{
7972 int c;
7973 int cc;
7974
7975 pc_status = PC_STATUS_UNSET;
7976 if (redrawing() && !char_avail())
7977 {
7978 /* may need to redraw when no more chars available now */
7979 ins_redraw();
7980
7981 edit_putchar('?', TRUE);
7982#ifdef FEAT_CMDL_INFO
7983 add_to_showcmd_c(Ctrl_K);
7984#endif
7985 }
7986
7987#ifdef USE_ON_FLY_SCROLL
7988 dont_scroll = TRUE; /* disallow scrolling here */
7989#endif
7990
7991 /* don't map the digraph chars. This also prevents the
7992 * mode message to be deleted when ESC is hit */
7993 ++no_mapping;
7994 ++allow_keys;
7995 c = safe_vgetc();
7996 --no_mapping;
7997 --allow_keys;
7998 if (IS_SPECIAL(c) || mod_mask) /* special key */
7999 {
8000#ifdef FEAT_CMDL_INFO
8001 clear_showcmd();
8002#endif
8003 insert_special(c, TRUE, FALSE);
8004 return NUL;
8005 }
8006 if (c != ESC)
8007 {
8008 if (redrawing() && !char_avail())
8009 {
8010 /* may need to redraw when no more chars available now */
8011 ins_redraw();
8012
8013 if (char2cells(c) == 1)
8014 {
8015 /* first remove the '?', otherwise it's restored when typing
8016 * an ESC next */
8017 edit_unputchar();
8018 ins_redraw();
8019 edit_putchar(c, TRUE);
8020 }
8021#ifdef FEAT_CMDL_INFO
8022 add_to_showcmd_c(c);
8023#endif
8024 }
8025 ++no_mapping;
8026 ++allow_keys;
8027 cc = safe_vgetc();
8028 --no_mapping;
8029 --allow_keys;
8030 if (cc != ESC)
8031 {
8032 AppendToRedobuff((char_u *)CTRL_V_STR);
8033 c = getdigraph(c, cc, TRUE);
8034#ifdef FEAT_CMDL_INFO
8035 clear_showcmd();
8036#endif
8037 return c;
8038 }
8039 }
8040 edit_unputchar();
8041#ifdef FEAT_CMDL_INFO
8042 clear_showcmd();
8043#endif
8044 return NUL;
8045}
8046#endif
8047
8048/*
8049 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8050 * Returns the char to be inserted, or NUL if none found.
8051 */
8052 static int
8053ins_copychar(lnum)
8054 linenr_T lnum;
8055{
8056 int c;
8057 int temp;
8058 char_u *ptr, *prev_ptr;
8059
8060 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8061 {
8062 vim_beep();
8063 return NUL;
8064 }
8065
8066 /* try to advance to the cursor column */
8067 temp = 0;
8068 ptr = ml_get(lnum);
8069 prev_ptr = ptr;
8070 validate_virtcol();
8071 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8072 {
8073 prev_ptr = ptr;
8074 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8075 }
8076 if ((colnr_T)temp > curwin->w_virtcol)
8077 ptr = prev_ptr;
8078
8079#ifdef FEAT_MBYTE
8080 c = (*mb_ptr2char)(ptr);
8081#else
8082 c = *ptr;
8083#endif
8084 if (c == NUL)
8085 vim_beep();
8086 return c;
8087}
8088
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008089/*
8090 * CTRL-Y or CTRL-E typed in Insert mode.
8091 */
8092 static int
8093ins_ctrl_ey(tc)
8094 int tc;
8095{
8096 int c = tc;
8097
8098#ifdef FEAT_INS_EXPAND
8099 if (ctrl_x_mode == CTRL_X_SCROLL)
8100 {
8101 if (c == Ctrl_Y)
8102 scrolldown_clamp();
8103 else
8104 scrollup_clamp();
8105 redraw_later(VALID);
8106 }
8107 else
8108#endif
8109 {
8110 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8111 if (c != NUL)
8112 {
8113 long tw_save;
8114
8115 /* The character must be taken literally, insert like it
8116 * was typed after a CTRL-V, and pretend 'textwidth'
8117 * wasn't set. Digits, 'o' and 'x' are special after a
8118 * CTRL-V, don't use it for these. */
8119 if (c < 256 && !isalnum(c))
8120 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8121 tw_save = curbuf->b_p_tw;
8122 curbuf->b_p_tw = -1;
8123 insert_special(c, TRUE, FALSE);
8124 curbuf->b_p_tw = tw_save;
8125#ifdef FEAT_RIGHTLEFT
8126 revins_chars++;
8127 revins_legal++;
8128#endif
8129 c = Ctrl_V; /* pretend CTRL-V is last character */
8130 auto_format(FALSE, TRUE);
8131 }
8132 }
8133 return c;
8134}
8135
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136#ifdef FEAT_SMARTINDENT
8137/*
8138 * Try to do some very smart auto-indenting.
8139 * Used when inserting a "normal" character.
8140 */
8141 static void
8142ins_try_si(c)
8143 int c;
8144{
8145 pos_T *pos, old_pos;
8146 char_u *ptr;
8147 int i;
8148 int temp;
8149
8150 /*
8151 * do some very smart indenting when entering '{' or '}'
8152 */
8153 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8154 {
8155 /*
8156 * for '}' set indent equal to indent of line containing matching '{'
8157 */
8158 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8159 {
8160 old_pos = curwin->w_cursor;
8161 /*
8162 * If the matching '{' has a ')' immediately before it (ignoring
8163 * white-space), then line up with the start of the line
8164 * containing the matching '(' if there is one. This handles the
8165 * case where an "if (..\n..) {" statement continues over multiple
8166 * lines -- webb
8167 */
8168 ptr = ml_get(pos->lnum);
8169 i = pos->col;
8170 if (i > 0) /* skip blanks before '{' */
8171 while (--i > 0 && vim_iswhite(ptr[i]))
8172 ;
8173 curwin->w_cursor.lnum = pos->lnum;
8174 curwin->w_cursor.col = i;
8175 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8176 curwin->w_cursor = *pos;
8177 i = get_indent();
8178 curwin->w_cursor = old_pos;
8179#ifdef FEAT_VREPLACE
8180 if (State & VREPLACE_FLAG)
8181 change_indent(INDENT_SET, i, FALSE, NUL);
8182 else
8183#endif
8184 (void)set_indent(i, SIN_CHANGED);
8185 }
8186 else if (curwin->w_cursor.col > 0)
8187 {
8188 /*
8189 * when inserting '{' after "O" reduce indent, but not
8190 * more than indent of previous line
8191 */
8192 temp = TRUE;
8193 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8194 {
8195 old_pos = curwin->w_cursor;
8196 i = get_indent();
8197 while (curwin->w_cursor.lnum > 1)
8198 {
8199 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8200
8201 /* ignore empty lines and lines starting with '#'. */
8202 if (*ptr != '#' && *ptr != NUL)
8203 break;
8204 }
8205 if (get_indent() >= i)
8206 temp = FALSE;
8207 curwin->w_cursor = old_pos;
8208 }
8209 if (temp)
8210 shift_line(TRUE, FALSE, 1);
8211 }
8212 }
8213
8214 /*
8215 * set indent of '#' always to 0
8216 */
8217 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8218 {
8219 /* remember current indent for next line */
8220 old_indent = get_indent();
8221 (void)set_indent(0, SIN_CHANGED);
8222 }
8223
8224 /* Adjust ai_col, the char at this position can be deleted. */
8225 if (ai_col > curwin->w_cursor.col)
8226 ai_col = curwin->w_cursor.col;
8227}
8228#endif
8229
8230/*
8231 * Get the value that w_virtcol would have when 'list' is off.
8232 * Unless 'cpo' contains the 'L' flag.
8233 */
8234 static colnr_T
8235get_nolist_virtcol()
8236{
8237 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8238 return getvcol_nolist(&curwin->w_cursor);
8239 validate_virtcol();
8240 return curwin->w_virtcol;
8241}