blob: c037d1f9610e0ca748ea935f8460362509ad5327 [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));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000124static int ins_compl_next __ARGS((int allow_get_expansion, int count));
125static int ins_compl_key2dir __ARGS((int c));
126static int ins_compl_key2count __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127static int ins_complete __ARGS((int c));
128static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
129#endif /* FEAT_INS_EXPAND */
130
131#define BACKSPACE_CHAR 1
132#define BACKSPACE_WORD 2
133#define BACKSPACE_WORD_NOT_SPACE 3
134#define BACKSPACE_LINE 4
135
136static void ins_redraw __ARGS((void));
137static void ins_ctrl_v __ARGS((void));
138static void undisplay_dollar __ARGS((void));
139static void insert_special __ARGS((int, int, int));
140static void check_auto_format __ARGS((int));
141static void redo_literal __ARGS((int c));
142static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000143#ifdef FEAT_SYN_HL
144static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000145static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000146static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
149static int echeck_abbr __ARGS((int));
150static void replace_push_off __ARGS((int c));
151static int replace_pop __ARGS((void));
152static void replace_join __ARGS((int off));
153static void replace_pop_ins __ARGS((void));
154#ifdef FEAT_MBYTE
155static void mb_replace_pop_ins __ARGS((int cc));
156#endif
157static void replace_flush __ARGS((void));
158static void replace_do_bs __ARGS((void));
159#ifdef FEAT_CINDENT
160static int cindent_on __ARGS((void));
161#endif
162static void ins_reg __ARGS((void));
163static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000164static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000165static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#ifdef FEAT_RIGHTLEFT
167static void ins_ctrl_ __ARGS((void));
168#endif
169#ifdef FEAT_VISUAL
170static int ins_start_select __ARGS((int c));
171#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000172static void ins_insert __ARGS((int replaceState));
173static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174static void ins_shift __ARGS((int c, int lastc));
175static void ins_del __ARGS((void));
176static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
177#ifdef FEAT_MOUSE
178static void ins_mouse __ARGS((int c));
179static void ins_mousescroll __ARGS((int up));
180#endif
181static void ins_left __ARGS((void));
182static void ins_home __ARGS((int c));
183static void ins_end __ARGS((int c));
184static void ins_s_left __ARGS((void));
185static void ins_right __ARGS((void));
186static void ins_s_right __ARGS((void));
187static void ins_up __ARGS((int startcol));
188static void ins_pageup __ARGS((void));
189static void ins_down __ARGS((int startcol));
190static void ins_pagedown __ARGS((void));
191#ifdef FEAT_DND
192static void ins_drop __ARGS((void));
193#endif
194static int ins_tab __ARGS((void));
195static int ins_eol __ARGS((int c));
196#ifdef FEAT_DIGRAPHS
197static int ins_digraph __ARGS((void));
198#endif
199static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000200static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifdef FEAT_SMARTINDENT
202static void ins_try_si __ARGS((int c));
203#endif
204static colnr_T get_nolist_virtcol __ARGS((void));
205
206static colnr_T Insstart_textlen; /* length of line when insert started */
207static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
208
209static char_u *last_insert = NULL; /* the text of the previous insert,
210 K_SPECIAL and CSI are escaped */
211static int last_insert_skip; /* nr of chars in front of previous insert */
212static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000213static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
215#ifdef FEAT_CINDENT
216static int can_cindent; /* may do cindenting on this line */
217#endif
218
219static int old_indent = 0; /* for ^^D command in insert mode */
220
221#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000222static int revins_on; /* reverse insert mode on */
223static int revins_chars; /* how much to skip after edit */
224static int revins_legal; /* was the last char 'legal'? */
225static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
227
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228static int ins_need_undo; /* call u_save() before inserting a
229 char. Set when edit() is called.
230 after that arrow_used is used. */
231
232static int did_add_space = FALSE; /* auto_format() added an extra space
233 under the cursor */
234
235/*
236 * edit(): Start inserting text.
237 *
238 * "cmdchar" can be:
239 * 'i' normal insert command
240 * 'a' normal append command
241 * 'R' replace command
242 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
243 * but still only one <CR> is inserted. The <Esc> is not used for redo.
244 * 'g' "gI" command.
245 * 'V' "gR" command for Virtual Replace mode.
246 * 'v' "gr" command for single character Virtual Replace mode.
247 *
248 * This function is not called recursively. For CTRL-O commands, it returns
249 * and lets the caller handle the Normal-mode command.
250 *
251 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
252 */
253 int
254edit(cmdchar, startln, count)
255 int cmdchar;
256 int startln; /* if set, insert at start of line */
257 long count;
258{
259 int c = 0;
260 char_u *ptr;
261 int lastc;
262 colnr_T mincol;
263 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 int i;
265 int did_backspace = TRUE; /* previous char was backspace */
266#ifdef FEAT_CINDENT
267 int line_is_white = FALSE; /* line is empty before insert */
268#endif
269 linenr_T old_topline = 0; /* topline before insertion */
270#ifdef FEAT_DIFF
271 int old_topfill = -1;
272#endif
273 int inserted_space = FALSE; /* just inserted a space */
274 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000275 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000277 /* Remember whether editing was restarted after CTRL-O. */
278 did_restart_edit = restart_edit;
279
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 /* sleep before redrawing, needed for "CTRL-O :" that results in an
281 * error message */
282 check_for_delay(TRUE);
283
284#ifdef HAVE_SANDBOX
285 /* Don't allow inserting in the sandbox. */
286 if (sandbox != 0)
287 {
288 EMSG(_(e_sandbox));
289 return FALSE;
290 }
291#endif
292
293#ifdef FEAT_INS_EXPAND
294 ins_compl_clear(); /* clear stuff for CTRL-X mode */
295#endif
296
Bram Moolenaar843ee412004-06-30 16:16:41 +0000297#ifdef FEAT_AUTOCMD
298 /*
299 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
300 */
301 if (cmdchar != 'r' && cmdchar != 'v')
302 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000303# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000304 if (cmdchar == 'R')
305 ptr = (char_u *)"r";
306 else if (cmdchar == 'V')
307 ptr = (char_u *)"v";
308 else
309 ptr = (char_u *)"i";
310 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000311# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000312 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
313 }
314#endif
315
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316#ifdef FEAT_MOUSE
317 /*
318 * When doing a paste with the middle mouse button, Insstart is set to
319 * where the paste started.
320 */
321 if (where_paste_started.lnum != 0)
322 Insstart = where_paste_started;
323 else
324#endif
325 {
326 Insstart = curwin->w_cursor;
327 if (startln)
328 Insstart.col = 0;
329 }
330 Insstart_textlen = linetabsize(ml_get_curline());
331 Insstart_blank_vcol = MAXCOL;
332 if (!did_ai)
333 ai_col = 0;
334
335 if (cmdchar != NUL && restart_edit == 0)
336 {
337 ResetRedobuff();
338 AppendNumberToRedobuff(count);
339#ifdef FEAT_VREPLACE
340 if (cmdchar == 'V' || cmdchar == 'v')
341 {
342 /* "gR" or "gr" command */
343 AppendCharToRedobuff('g');
344 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
345 }
346 else
347#endif
348 {
349 AppendCharToRedobuff(cmdchar);
350 if (cmdchar == 'g') /* "gI" command */
351 AppendCharToRedobuff('I');
352 else if (cmdchar == 'r') /* "r<CR>" command */
353 count = 1; /* insert only one <CR> */
354 }
355 }
356
357 if (cmdchar == 'R')
358 {
359#ifdef FEAT_FKMAP
360 if (p_fkmap && p_ri)
361 {
362 beep_flush();
363 EMSG(farsi_text_3); /* encoded in Farsi */
364 State = INSERT;
365 }
366 else
367#endif
368 State = REPLACE;
369 }
370#ifdef FEAT_VREPLACE
371 else if (cmdchar == 'V' || cmdchar == 'v')
372 {
373 State = VREPLACE;
374 replaceState = VREPLACE;
375 orig_line_count = curbuf->b_ml.ml_line_count;
376 vr_lines_changed = 1;
377 }
378#endif
379 else
380 State = INSERT;
381
382 stop_insert_mode = FALSE;
383
384 /*
385 * Need to recompute the cursor position, it might move when the cursor is
386 * on a TAB or special character.
387 */
388 curs_columns(TRUE);
389
390 /*
391 * Enable langmap or IME, indicated by 'iminsert'.
392 * Note that IME may enabled/disabled without us noticing here, thus the
393 * 'iminsert' value may not reflect what is actually used. It is updated
394 * when hitting <Esc>.
395 */
396 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
397 State |= LANGMAP;
398#ifdef USE_IM_CONTROL
399 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
400#endif
401
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#ifdef FEAT_MOUSE
403 setmouse();
404#endif
405#ifdef FEAT_CMDL_INFO
406 clear_showcmd();
407#endif
408#ifdef FEAT_RIGHTLEFT
409 /* there is no reverse replace mode */
410 revins_on = (State == INSERT && p_ri);
411 if (revins_on)
412 undisplay_dollar();
413 revins_chars = 0;
414 revins_legal = 0;
415 revins_scol = -1;
416#endif
417
418 /*
419 * Handle restarting Insert mode.
420 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
421 * restart_edit non-zero, and something in the stuff buffer.
422 */
423 if (restart_edit != 0 && stuff_empty())
424 {
425#ifdef FEAT_MOUSE
426 /*
427 * After a paste we consider text typed to be part of the insert for
428 * the pasted text. You can backspace over the pasted text too.
429 */
430 if (where_paste_started.lnum)
431 arrow_used = FALSE;
432 else
433#endif
434 arrow_used = TRUE;
435 restart_edit = 0;
436
437 /*
438 * If the cursor was after the end-of-line before the CTRL-O and it is
439 * now at the end-of-line, put it after the end-of-line (this is not
440 * correct in very rare cases).
441 * Also do this if curswant is greater than the current virtual
442 * column. Eg after "^O$" or "^O80|".
443 */
444 validate_virtcol();
445 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000446 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 || curwin->w_curswant > curwin->w_virtcol)
448 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
449 {
450 if (ptr[1] == NUL)
451 ++curwin->w_cursor.col;
452#ifdef FEAT_MBYTE
453 else if (has_mbyte)
454 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000455 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 if (ptr[i] == NUL)
457 curwin->w_cursor.col += i;
458 }
459#endif
460 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000461 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 }
463 else
464 arrow_used = FALSE;
465
466 /* we are in insert mode now, don't need to start it anymore */
467 need_start_insertmode = FALSE;
468
469 /* Need to save the line for undo before inserting the first char. */
470 ins_need_undo = TRUE;
471
472#ifdef FEAT_MOUSE
473 where_paste_started.lnum = 0;
474#endif
475#ifdef FEAT_CINDENT
476 can_cindent = TRUE;
477#endif
478#ifdef FEAT_FOLDING
479 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
480 * restarting. */
481 if (!p_im && did_restart_edit == 0)
482 foldOpenCursor();
483#endif
484
485 /*
486 * If 'showmode' is set, show the current (insert/replace/..) mode.
487 * A warning message for changing a readonly file is given here, before
488 * actually changing anything. It's put after the mode, if any.
489 */
490 i = 0;
491 if (p_smd)
492 i = showmode();
493
494 if (!p_im && did_restart_edit == 0)
495 change_warning(i + 1);
496
497#ifdef CURSOR_SHAPE
498 ui_cursor_shape(); /* may show different cursor shape */
499#endif
500#ifdef FEAT_DIGRAPHS
501 do_digraph(-1); /* clear digraphs */
502#endif
503
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000504 /*
505 * Get the current length of the redo buffer, those characters have to be
506 * skipped if we want to get to the inserted characters.
507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 ptr = get_inserted();
509 if (ptr == NULL)
510 new_insert_skip = 0;
511 else
512 {
513 new_insert_skip = (int)STRLEN(ptr);
514 vim_free(ptr);
515 }
516
517 old_indent = 0;
518
519 /*
520 * Main loop in Insert mode: repeat until Insert mode is left.
521 */
522 for (;;)
523 {
524#ifdef FEAT_RIGHTLEFT
525 if (!revins_legal)
526 revins_scol = -1; /* reset on illegal motions */
527 else
528 revins_legal = 0;
529#endif
530 if (arrow_used) /* don't repeat insert when arrow key used */
531 count = 0;
532
533 if (stop_insert_mode)
534 {
535 /* ":stopinsert" used or 'insertmode' reset */
536 count = 0;
537 goto doESCkey;
538 }
539
540 /* set curwin->w_curswant for next K_DOWN or K_UP */
541 if (!arrow_used)
542 curwin->w_set_curswant = TRUE;
543
544 /* If there is no typeahead may check for timestamps (e.g., for when a
545 * menu invoked a shell command). */
546 if (stuff_empty())
547 {
548 did_check_timestamps = FALSE;
549 if (need_check_timestamps)
550 check_timestamps(FALSE);
551 }
552
553 /*
554 * When emsg() was called msg_scroll will have been set.
555 */
556 msg_scroll = FALSE;
557
558#ifdef FEAT_GUI
559 /* When 'mousefocus' is set a mouse movement may have taken us to
560 * another window. "need_mouse_correct" may then be set because of an
561 * autocommand. */
562 if (need_mouse_correct)
563 gui_mouse_correct();
564#endif
565
566#ifdef FEAT_FOLDING
567 /* Open fold at the cursor line, according to 'foldopen'. */
568 if (fdo_flags & FDO_INSERT)
569 foldOpenCursor();
570 /* Close folds where the cursor isn't, according to 'foldclose' */
571 if (!char_avail())
572 foldCheckClose();
573#endif
574
575 /*
576 * If we inserted a character at the last position of the last line in
577 * the window, scroll the window one line up. This avoids an extra
578 * redraw.
579 * This is detected when the cursor column is smaller after inserting
580 * something.
581 * Don't do this when the topline changed already, it has
582 * already been adjusted (by insertchar() calling open_line())).
583 */
584 if (curbuf->b_mod_set
585 && curwin->w_p_wrap
586 && !did_backspace
587 && curwin->w_topline == old_topline
588#ifdef FEAT_DIFF
589 && curwin->w_topfill == old_topfill
590#endif
591 )
592 {
593 mincol = curwin->w_wcol;
594 validate_cursor_col();
595
596 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
597 && curwin->w_wrow == W_WINROW(curwin)
598 + curwin->w_height - 1 - p_so
599 && (curwin->w_cursor.lnum != curwin->w_topline
600#ifdef FEAT_DIFF
601 || curwin->w_topfill > 0
602#endif
603 ))
604 {
605#ifdef FEAT_DIFF
606 if (curwin->w_topfill > 0)
607 --curwin->w_topfill;
608 else
609#endif
610#ifdef FEAT_FOLDING
611 if (hasFolding(curwin->w_topline, NULL, &old_topline))
612 set_topline(curwin, old_topline + 1);
613 else
614#endif
615 set_topline(curwin, curwin->w_topline + 1);
616 }
617 }
618
619 /* May need to adjust w_topline to show the cursor. */
620 update_topline();
621
622 did_backspace = FALSE;
623
624 validate_cursor(); /* may set must_redraw */
625
626 /*
627 * Redraw the display when no characters are waiting.
628 * Also shows mode, ruler and positions cursor.
629 */
630 ins_redraw();
631
632#ifdef FEAT_SCROLLBIND
633 if (curwin->w_p_scb)
634 do_check_scrollbind(TRUE);
635#endif
636
637 update_curswant();
638 old_topline = curwin->w_topline;
639#ifdef FEAT_DIFF
640 old_topfill = curwin->w_topfill;
641#endif
642
643#ifdef USE_ON_FLY_SCROLL
644 dont_scroll = FALSE; /* allow scrolling here */
645#endif
646
647 /*
648 * Get a character for Insert mode.
649 */
650 lastc = c; /* remember previous char for CTRL-D */
651 c = safe_vgetc();
652
653#ifdef FEAT_RIGHTLEFT
654 if (p_hkmap && KeyTyped)
655 c = hkmap(c); /* Hebrew mode mapping */
656#endif
657#ifdef FEAT_FKMAP
658 if (p_fkmap && KeyTyped)
659 c = fkmap(c); /* Farsi mode mapping */
660#endif
661
662#ifdef FEAT_INS_EXPAND
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000663 /* When the popup menu is visible cursor keys change the selection. */
664 if (c == K_UP && pum_visible())
665 c = Ctrl_P;
666 if (c == K_DOWN && pum_visible())
667 c = Ctrl_N;
668
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
670 * it does fix up the text when finishing completion. */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000671 if (c != K_IGNORE)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000672 {
673 if (ins_compl_prep(c))
674 continue;
675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676#endif
677
Bram Moolenaar488c6512005-08-11 20:09:58 +0000678 /* CTRL-\ CTRL-N goes to Normal mode,
679 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
680 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 if (c == Ctrl_BSL)
682 {
683 /* may need to redraw when no more chars available now */
684 ins_redraw();
685 ++no_mapping;
686 ++allow_keys;
687 c = safe_vgetc();
688 --no_mapping;
689 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000690 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000692 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 vungetc(c);
694 c = Ctrl_BSL;
695 }
696 else if (c == Ctrl_G && p_im)
697 continue;
698 else
699 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000700 if (c == Ctrl_O)
701 {
702 ins_ctrl_o();
703 ins_at_eol = FALSE; /* cursor keeps its column */
704 nomove = TRUE;
705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 count = 0;
707 goto doESCkey;
708 }
709 }
710
711#ifdef FEAT_DIGRAPHS
712 c = do_digraph(c);
713#endif
714
715#ifdef FEAT_INS_EXPAND
716 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
717 goto docomplete;
718#endif
719 if (c == Ctrl_V || c == Ctrl_Q)
720 {
721 ins_ctrl_v();
722 c = Ctrl_V; /* pretend CTRL-V is last typed character */
723 continue;
724 }
725
726#ifdef FEAT_CINDENT
727 if (cindent_on()
728# ifdef FEAT_INS_EXPAND
729 && ctrl_x_mode == 0
730# endif
731 )
732 {
733 /* A key name preceded by a bang means this key is not to be
734 * inserted. Skip ahead to the re-indenting below.
735 * A key name preceded by a star means that indenting has to be
736 * done before inserting the key. */
737 line_is_white = inindent(0);
738 if (in_cinkeys(c, '!', line_is_white))
739 goto force_cindent;
740 if (can_cindent && in_cinkeys(c, '*', line_is_white)
741 && stop_arrow() == OK)
742 do_c_expr_indent();
743 }
744#endif
745
746#ifdef FEAT_RIGHTLEFT
747 if (curwin->w_p_rl)
748 switch (c)
749 {
750 case K_LEFT: c = K_RIGHT; break;
751 case K_S_LEFT: c = K_S_RIGHT; break;
752 case K_C_LEFT: c = K_C_RIGHT; break;
753 case K_RIGHT: c = K_LEFT; break;
754 case K_S_RIGHT: c = K_S_LEFT; break;
755 case K_C_RIGHT: c = K_C_LEFT; break;
756 }
757#endif
758
759#ifdef FEAT_VISUAL
760 /*
761 * If 'keymodel' contains "startsel", may start selection. If it
762 * does, a CTRL-O and c will be stuffed, we need to get these
763 * characters.
764 */
765 if (ins_start_select(c))
766 continue;
767#endif
768
769 /*
770 * The big switch to handle a character in insert mode.
771 */
772 switch (c)
773 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000774 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (echeck_abbr(ESC + ABBR_OFF))
776 break;
777 /*FALLTHROUGH*/
778
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000779 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#ifdef FEAT_CMDWIN
781 if (c == Ctrl_C && cmdwin_type != 0)
782 {
783 /* Close the cmdline window. */
784 cmdwin_result = K_IGNORE;
785 got_int = FALSE; /* don't stop executing autocommands et al. */
786 goto doESCkey;
787 }
788#endif
789
790#ifdef UNIX
791do_intr:
792#endif
793 /* when 'insertmode' set, and not halfway a mapping, don't leave
794 * Insert mode */
795 if (goto_im())
796 {
797 if (got_int)
798 {
799 (void)vgetc(); /* flush all buffers */
800 got_int = FALSE;
801 }
802 else
803 vim_beep();
804 break;
805 }
806doESCkey:
807 /*
808 * This is the ONLY return from edit()!
809 */
810 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
811 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000812 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 o_lnum = curwin->w_cursor.lnum;
814
Bram Moolenaar488c6512005-08-11 20:09:58 +0000815 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000816 {
817#ifdef FEAT_AUTOCMD
818 if (cmdchar != 'r' && cmdchar != 'v')
819 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
820 FALSE, curbuf);
821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 continue;
825
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000826 case Ctrl_Z: /* suspend when 'insertmode' set */
827 if (!p_im)
828 goto normalchar; /* insert CTRL-Z as normal char */
829 stuffReadbuff((char_u *)":st\r");
830 c = Ctrl_O;
831 /*FALLTHROUGH*/
832
833 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000834#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000835 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000836 goto docomplete;
837#endif
838 if (echeck_abbr(Ctrl_O + ABBR_OFF))
839 break;
840 ins_ctrl_o();
841 count = 0;
842 goto doESCkey;
843
Bram Moolenaar572cb562005-08-05 21:35:02 +0000844 case K_INS: /* toggle insert/replace mode */
845 case K_KINS:
846 ins_insert(replaceState);
847 break;
848
849 case K_SELECT: /* end of Select mode mapping - ignore */
850 break;
851
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000852#ifdef FEAT_SNIFF
853 case K_SNIFF: /* Sniff command received */
854 stuffcharReadbuff(K_SNIFF);
855 goto doESCkey;
856#endif
857
858 case K_HELP: /* Help key works like <ESC> <Help> */
859 case K_F1:
860 case K_XF1:
861 stuffcharReadbuff(K_HELP);
862 if (p_im)
863 need_start_insertmode = TRUE;
864 goto doESCkey;
865
866#ifdef FEAT_NETBEANS_INTG
867 case K_F21: /* NetBeans command */
868 ++no_mapping; /* don't map the next key hits */
869 i = safe_vgetc();
870 --no_mapping;
871 netbeans_keycommand(i);
872 break;
873#endif
874
875 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 case NUL:
877 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000878 /* For ^@ the trailing ESC will end the insert, unless there is an
879 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
881 && c != Ctrl_A && !p_im)
882 goto doESCkey; /* quit insert mode */
883 inserted_space = FALSE;
884 break;
885
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000886 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 ins_reg();
888 auto_format(FALSE, TRUE);
889 inserted_space = FALSE;
890 break;
891
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000892 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 ins_ctrl_g();
894 break;
895
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000896 case Ctrl_HAT: /* switch input mode and/or langmap */
897 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 break;
899
900#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000901 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (!p_ari)
903 goto normalchar;
904 ins_ctrl_();
905 break;
906#endif
907
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000908 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
910 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
911 goto docomplete;
912#endif
913 /* FALLTHROUGH */
914
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000915 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916# ifdef FEAT_INS_EXPAND
917 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
918 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000919 if (has_compl_option(FALSE))
920 goto docomplete;
921 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 }
923# endif
924 ins_shift(c, lastc);
925 auto_format(FALSE, TRUE);
926 inserted_space = FALSE;
927 break;
928
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000929 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 case K_KDEL:
931 ins_del();
932 auto_format(FALSE, TRUE);
933 break;
934
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000935 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 case Ctrl_H:
937 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
938 auto_format(FALSE, TRUE);
939 break;
940
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000941 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
943 auto_format(FALSE, TRUE);
944 break;
945
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000946 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000947# ifdef FEAT_COMPL_FUNC
948 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000949 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000950 goto docomplete;
951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
953 auto_format(FALSE, TRUE);
954 inserted_space = FALSE;
955 break;
956
957#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000958 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 case K_LEFTMOUSE_NM:
960 case K_LEFTDRAG:
961 case K_LEFTRELEASE:
962 case K_LEFTRELEASE_NM:
963 case K_MIDDLEMOUSE:
964 case K_MIDDLEDRAG:
965 case K_MIDDLERELEASE:
966 case K_RIGHTMOUSE:
967 case K_RIGHTDRAG:
968 case K_RIGHTRELEASE:
969 case K_X1MOUSE:
970 case K_X1DRAG:
971 case K_X1RELEASE:
972 case K_X2MOUSE:
973 case K_X2DRAG:
974 case K_X2RELEASE:
975 ins_mouse(c);
976 break;
977
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000978 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 ins_mousescroll(FALSE);
980 break;
981
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000982 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 ins_mousescroll(TRUE);
984 break;
985#endif
986
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000987 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 break;
989
Bram Moolenaar4770d092006-01-12 23:22:24 +0000990#ifdef FEAT_GUI_W32
991 /* On Win32 ignore <M-F4>, we get it when closing the window was
992 * cancelled. */
993 case K_F4:
994 if (mod_mask != MOD_MASK_ALT)
995 goto normalchar;
996 break;
997#endif
998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999#ifdef FEAT_GUI
1000 case K_VER_SCROLLBAR:
1001 ins_scroll();
1002 break;
1003
1004 case K_HOR_SCROLLBAR:
1005 ins_horscroll();
1006 break;
1007#endif
1008
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001009 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 case K_S_HOME:
1012 case K_C_HOME:
1013 ins_home(c);
1014 break;
1015
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001016 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 case K_S_END:
1019 case K_C_END:
1020 ins_end(c);
1021 break;
1022
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001023 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001024 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1025 ins_s_left();
1026 else
1027 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 break;
1029
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001030 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 case K_C_LEFT:
1032 ins_s_left();
1033 break;
1034
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001035 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001036 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1037 ins_s_right();
1038 else
1039 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 break;
1041
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001042 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 case K_C_RIGHT:
1044 ins_s_right();
1045 break;
1046
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001047 case K_UP: /* <Up> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001048 if (mod_mask & MOD_MASK_SHIFT)
1049 ins_pageup();
1050 else
1051 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 break;
1053
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001054 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 case K_PAGEUP:
1056 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001057#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001058 if (pum_visible())
1059 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 ins_pageup();
1062 break;
1063
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001064 case K_DOWN: /* <Down> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001065 if (mod_mask & MOD_MASK_SHIFT)
1066 ins_pagedown();
1067 else
1068 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 break;
1070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001071 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 case K_PAGEDOWN:
1073 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001074#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001075 if (pum_visible())
1076 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 ins_pagedown();
1079 break;
1080
1081#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001082 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 ins_drop();
1084 break;
1085#endif
1086
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001087 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 c = TAB;
1089 /* FALLTHROUGH */
1090
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001091 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1093 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1094 goto docomplete;
1095#endif
1096 inserted_space = FALSE;
1097 if (ins_tab())
1098 goto normalchar; /* insert TAB as a normal char */
1099 auto_format(FALSE, TRUE);
1100 break;
1101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 c = CAR;
1104 /* FALLTHROUGH */
1105 case CAR:
1106 case NL:
1107#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1108 /* In a quickfix window a <CR> jumps to the error under the
1109 * cursor. */
1110 if (bt_quickfix(curbuf) && c == CAR)
1111 {
1112 do_cmdline_cmd((char_u *)".cc");
1113 break;
1114 }
1115#endif
1116#ifdef FEAT_CMDWIN
1117 if (cmdwin_type != 0)
1118 {
1119 /* Execute the command in the cmdline window. */
1120 cmdwin_result = CAR;
1121 goto doESCkey;
1122 }
1123#endif
1124 if (ins_eol(c) && !p_im)
1125 goto doESCkey; /* out of memory */
1126 auto_format(FALSE, FALSE);
1127 inserted_space = FALSE;
1128 break;
1129
1130#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001131 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132# ifdef FEAT_INS_EXPAND
1133 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1134 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001135 if (has_compl_option(TRUE))
1136 goto docomplete;
1137 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139# endif
1140# ifdef FEAT_DIGRAPHS
1141 c = ins_digraph();
1142 if (c == NUL)
1143 break;
1144# endif
1145 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
1148#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001149 case Ctrl_X: /* Enter CTRL-X mode */
1150 ins_ctrl_x();
1151 break;
1152
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001153 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 if (ctrl_x_mode != CTRL_X_TAGS)
1155 goto normalchar;
1156 goto docomplete;
1157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (ctrl_x_mode != CTRL_X_FILES)
1160 goto normalchar;
1161 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001162
1163 case 's': /* Spelling completion after ^X */
1164 case Ctrl_S:
1165 if (ctrl_x_mode != CTRL_X_SPELL)
1166 goto normalchar;
1167 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168#endif
1169
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001170 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#ifdef FEAT_INS_EXPAND
1172 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1173#endif
1174 {
1175 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1176 if (p_im)
1177 {
1178 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1179 break;
1180 goto doESCkey;
1181 }
1182 goto normalchar;
1183 }
1184#ifdef FEAT_INS_EXPAND
1185 /* FALLTHROUGH */
1186
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001187 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 case Ctrl_N:
1189 /* if 'complete' is empty then plain ^P is no longer special,
1190 * but it is under other ^X modes */
1191 if (*curbuf->b_p_cpt == NUL
1192 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001193 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 goto normalchar;
1195
1196docomplete:
1197 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001198 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 break;
1200#endif /* FEAT_INS_EXPAND */
1201
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001202 case Ctrl_Y: /* copy from previous line or scroll down */
1203 case Ctrl_E: /* copy from next line or scroll up */
1204 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 break;
1206
1207 default:
1208#ifdef UNIX
1209 if (c == intr_char) /* special interrupt char */
1210 goto do_intr;
1211#endif
1212
1213 /*
1214 * Insert a nomal character.
1215 */
1216normalchar:
1217#ifdef FEAT_SMARTINDENT
1218 /* Try to perform smart-indenting. */
1219 ins_try_si(c);
1220#endif
1221
1222 if (c == ' ')
1223 {
1224 inserted_space = TRUE;
1225#ifdef FEAT_CINDENT
1226 if (inindent(0))
1227 can_cindent = FALSE;
1228#endif
1229 if (Insstart_blank_vcol == MAXCOL
1230 && curwin->w_cursor.lnum == Insstart.lnum)
1231 Insstart_blank_vcol = get_nolist_virtcol();
1232 }
1233
1234 if (vim_iswordc(c) || !echeck_abbr(
1235#ifdef FEAT_MBYTE
1236 /* Add ABBR_OFF for characters above 0x100, this is
1237 * what check_abbr() expects. */
1238 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1239#endif
1240 c))
1241 {
1242 insert_special(c, FALSE, FALSE);
1243#ifdef FEAT_RIGHTLEFT
1244 revins_legal++;
1245 revins_chars++;
1246#endif
1247 }
1248
1249 auto_format(FALSE, TRUE);
1250
1251#ifdef FEAT_FOLDING
1252 /* When inserting a character the cursor line must never be in a
1253 * closed fold. */
1254 foldOpenCursor();
1255#endif
1256 break;
1257 } /* end of switch (c) */
1258
1259 /* If the cursor was moved we didn't just insert a space */
1260 if (arrow_used)
1261 inserted_space = FALSE;
1262
1263#ifdef FEAT_CINDENT
1264 if (can_cindent && cindent_on()
1265# ifdef FEAT_INS_EXPAND
1266 && ctrl_x_mode == 0
1267# endif
1268 )
1269 {
1270force_cindent:
1271 /*
1272 * Indent now if a key was typed that is in 'cinkeys'.
1273 */
1274 if (in_cinkeys(c, ' ', line_is_white))
1275 {
1276 if (stop_arrow() == OK)
1277 /* re-indent the current line */
1278 do_c_expr_indent();
1279 }
1280 }
1281#endif /* FEAT_CINDENT */
1282
1283 } /* for (;;) */
1284 /* NOTREACHED */
1285}
1286
1287/*
1288 * Redraw for Insert mode.
1289 * This is postponed until getting the next character to make '$' in the 'cpo'
1290 * option work correctly.
1291 * Only redraw when there are no characters available. This speeds up
1292 * inserting sequences of characters (e.g., for CTRL-R).
1293 */
1294 static void
1295ins_redraw()
1296{
1297 if (!char_avail())
1298 {
1299 if (must_redraw)
1300 update_screen(0);
1301 else if (clear_cmdline || redraw_cmdline)
1302 showmode(); /* clear cmdline and show mode */
1303 showruler(FALSE);
1304 setcursor();
1305 emsg_on_display = FALSE; /* may remove error message now */
1306 }
1307}
1308
1309/*
1310 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1311 */
1312 static void
1313ins_ctrl_v()
1314{
1315 int c;
1316
1317 /* may need to redraw when no more chars available now */
1318 ins_redraw();
1319
1320 if (redrawing() && !char_avail())
1321 edit_putchar('^', TRUE);
1322 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1323
1324#ifdef FEAT_CMDL_INFO
1325 add_to_showcmd_c(Ctrl_V);
1326#endif
1327
1328 c = get_literal();
1329#ifdef FEAT_CMDL_INFO
1330 clear_showcmd();
1331#endif
1332 insert_special(c, FALSE, TRUE);
1333#ifdef FEAT_RIGHTLEFT
1334 revins_chars++;
1335 revins_legal++;
1336#endif
1337}
1338
1339/*
1340 * Put a character directly onto the screen. It's not stored in a buffer.
1341 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1342 */
1343static int pc_status;
1344#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1345#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1346#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1347#define PC_STATUS_SET 3 /* pc_bytes was filled */
1348#ifdef FEAT_MBYTE
1349static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1350#else
1351static char_u pc_bytes[2]; /* saved bytes */
1352#endif
1353static int pc_attr;
1354static int pc_row;
1355static int pc_col;
1356
1357 void
1358edit_putchar(c, highlight)
1359 int c;
1360 int highlight;
1361{
1362 int attr;
1363
1364 if (ScreenLines != NULL)
1365 {
1366 update_topline(); /* just in case w_topline isn't valid */
1367 validate_cursor();
1368 if (highlight)
1369 attr = hl_attr(HLF_8);
1370 else
1371 attr = 0;
1372 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1373 pc_col = W_WINCOL(curwin);
1374#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1375 pc_status = PC_STATUS_UNSET;
1376#endif
1377#ifdef FEAT_RIGHTLEFT
1378 if (curwin->w_p_rl)
1379 {
1380 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1381# ifdef FEAT_MBYTE
1382 if (has_mbyte)
1383 {
1384 int fix_col = mb_fix_col(pc_col, pc_row);
1385
1386 if (fix_col != pc_col)
1387 {
1388 screen_putchar(' ', pc_row, fix_col, attr);
1389 --curwin->w_wcol;
1390 pc_status = PC_STATUS_RIGHT;
1391 }
1392 }
1393# endif
1394 }
1395 else
1396#endif
1397 {
1398 pc_col += curwin->w_wcol;
1399#ifdef FEAT_MBYTE
1400 if (mb_lefthalve(pc_row, pc_col))
1401 pc_status = PC_STATUS_LEFT;
1402#endif
1403 }
1404
1405 /* save the character to be able to put it back */
1406#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1407 if (pc_status == PC_STATUS_UNSET)
1408#endif
1409 {
1410 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1411 pc_status = PC_STATUS_SET;
1412 }
1413 screen_putchar(c, pc_row, pc_col, attr);
1414 }
1415}
1416
1417/*
1418 * Undo the previous edit_putchar().
1419 */
1420 void
1421edit_unputchar()
1422{
1423 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1424 {
1425#if defined(FEAT_MBYTE)
1426 if (pc_status == PC_STATUS_RIGHT)
1427 ++curwin->w_wcol;
1428 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1429 redrawWinline(curwin->w_cursor.lnum, FALSE);
1430 else
1431#endif
1432 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1433 }
1434}
1435
1436/*
1437 * Called when p_dollar is set: display a '$' at the end of the changed text
1438 * Only works when cursor is in the line that changes.
1439 */
1440 void
1441display_dollar(col)
1442 colnr_T col;
1443{
1444 colnr_T save_col;
1445
1446 if (!redrawing())
1447 return;
1448
1449 cursor_off();
1450 save_col = curwin->w_cursor.col;
1451 curwin->w_cursor.col = col;
1452#ifdef FEAT_MBYTE
1453 if (has_mbyte)
1454 {
1455 char_u *p;
1456
1457 /* If on the last byte of a multi-byte move to the first byte. */
1458 p = ml_get_curline();
1459 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1460 }
1461#endif
1462 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1463 if (curwin->w_wcol < W_WIDTH(curwin))
1464 {
1465 edit_putchar('$', FALSE);
1466 dollar_vcol = curwin->w_virtcol;
1467 }
1468 curwin->w_cursor.col = save_col;
1469}
1470
1471/*
1472 * Call this function before moving the cursor from the normal insert position
1473 * in insert mode.
1474 */
1475 static void
1476undisplay_dollar()
1477{
1478 if (dollar_vcol)
1479 {
1480 dollar_vcol = 0;
1481 redrawWinline(curwin->w_cursor.lnum, FALSE);
1482 }
1483}
1484
1485/*
1486 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1487 * Keep the cursor on the same character.
1488 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1489 * type == INDENT_DEC decrease indent (for CTRL-D)
1490 * type == INDENT_SET set indent to "amount"
1491 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1492 */
1493 void
1494change_indent(type, amount, round, replaced)
1495 int type;
1496 int amount;
1497 int round;
1498 int replaced; /* replaced character, put on replace stack */
1499{
1500 int vcol;
1501 int last_vcol;
1502 int insstart_less; /* reduction for Insstart.col */
1503 int new_cursor_col;
1504 int i;
1505 char_u *ptr;
1506 int save_p_list;
1507 int start_col;
1508 colnr_T vc;
1509#ifdef FEAT_VREPLACE
1510 colnr_T orig_col = 0; /* init for GCC */
1511 char_u *new_line, *orig_line = NULL; /* init for GCC */
1512
1513 /* VREPLACE mode needs to know what the line was like before changing */
1514 if (State & VREPLACE_FLAG)
1515 {
1516 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1517 orig_col = curwin->w_cursor.col;
1518 }
1519#endif
1520
1521 /* for the following tricks we don't want list mode */
1522 save_p_list = curwin->w_p_list;
1523 curwin->w_p_list = FALSE;
1524 vc = getvcol_nolist(&curwin->w_cursor);
1525 vcol = vc;
1526
1527 /*
1528 * For Replace mode we need to fix the replace stack later, which is only
1529 * possible when the cursor is in the indent. Remember the number of
1530 * characters before the cursor if it's possible.
1531 */
1532 start_col = curwin->w_cursor.col;
1533
1534 /* determine offset from first non-blank */
1535 new_cursor_col = curwin->w_cursor.col;
1536 beginline(BL_WHITE);
1537 new_cursor_col -= curwin->w_cursor.col;
1538
1539 insstart_less = curwin->w_cursor.col;
1540
1541 /*
1542 * If the cursor is in the indent, compute how many screen columns the
1543 * cursor is to the left of the first non-blank.
1544 */
1545 if (new_cursor_col < 0)
1546 vcol = get_indent() - vcol;
1547
1548 if (new_cursor_col > 0) /* can't fix replace stack */
1549 start_col = -1;
1550
1551 /*
1552 * Set the new indent. The cursor will be put on the first non-blank.
1553 */
1554 if (type == INDENT_SET)
1555 (void)set_indent(amount, SIN_CHANGED);
1556 else
1557 {
1558#ifdef FEAT_VREPLACE
1559 int save_State = State;
1560
1561 /* Avoid being called recursively. */
1562 if (State & VREPLACE_FLAG)
1563 State = INSERT;
1564#endif
1565 shift_line(type == INDENT_DEC, round, 1);
1566#ifdef FEAT_VREPLACE
1567 State = save_State;
1568#endif
1569 }
1570 insstart_less -= curwin->w_cursor.col;
1571
1572 /*
1573 * Try to put cursor on same character.
1574 * If the cursor is at or after the first non-blank in the line,
1575 * compute the cursor column relative to the column of the first
1576 * non-blank character.
1577 * If we are not in insert mode, leave the cursor on the first non-blank.
1578 * If the cursor is before the first non-blank, position it relative
1579 * to the first non-blank, counted in screen columns.
1580 */
1581 if (new_cursor_col >= 0)
1582 {
1583 /*
1584 * When changing the indent while the cursor is touching it, reset
1585 * Insstart_col to 0.
1586 */
1587 if (new_cursor_col == 0)
1588 insstart_less = MAXCOL;
1589 new_cursor_col += curwin->w_cursor.col;
1590 }
1591 else if (!(State & INSERT))
1592 new_cursor_col = curwin->w_cursor.col;
1593 else
1594 {
1595 /*
1596 * Compute the screen column where the cursor should be.
1597 */
1598 vcol = get_indent() - vcol;
1599 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1600
1601 /*
1602 * Advance the cursor until we reach the right screen column.
1603 */
1604 vcol = last_vcol = 0;
1605 new_cursor_col = -1;
1606 ptr = ml_get_curline();
1607 while (vcol <= (int)curwin->w_virtcol)
1608 {
1609 last_vcol = vcol;
1610#ifdef FEAT_MBYTE
1611 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001612 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 else
1614#endif
1615 ++new_cursor_col;
1616 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1617 }
1618 vcol = last_vcol;
1619
1620 /*
1621 * May need to insert spaces to be able to position the cursor on
1622 * the right screen column.
1623 */
1624 if (vcol != (int)curwin->w_virtcol)
1625 {
1626 curwin->w_cursor.col = new_cursor_col;
1627 i = (int)curwin->w_virtcol - vcol;
1628 ptr = alloc(i + 1);
1629 if (ptr != NULL)
1630 {
1631 new_cursor_col += i;
1632 ptr[i] = NUL;
1633 while (--i >= 0)
1634 ptr[i] = ' ';
1635 ins_str(ptr);
1636 vim_free(ptr);
1637 }
1638 }
1639
1640 /*
1641 * When changing the indent while the cursor is in it, reset
1642 * Insstart_col to 0.
1643 */
1644 insstart_less = MAXCOL;
1645 }
1646
1647 curwin->w_p_list = save_p_list;
1648
1649 if (new_cursor_col <= 0)
1650 curwin->w_cursor.col = 0;
1651 else
1652 curwin->w_cursor.col = new_cursor_col;
1653 curwin->w_set_curswant = TRUE;
1654 changed_cline_bef_curs();
1655
1656 /*
1657 * May have to adjust the start of the insert.
1658 */
1659 if (State & INSERT)
1660 {
1661 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1662 {
1663 if ((int)Insstart.col <= insstart_less)
1664 Insstart.col = 0;
1665 else
1666 Insstart.col -= insstart_less;
1667 }
1668 if ((int)ai_col <= insstart_less)
1669 ai_col = 0;
1670 else
1671 ai_col -= insstart_less;
1672 }
1673
1674 /*
1675 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1676 * If the number of characters before the cursor decreased, need to pop a
1677 * few characters from the replace stack.
1678 * If the number of characters before the cursor increased, need to push a
1679 * few NULs onto the replace stack.
1680 */
1681 if (REPLACE_NORMAL(State) && start_col >= 0)
1682 {
1683 while (start_col > (int)curwin->w_cursor.col)
1684 {
1685 replace_join(0); /* remove a NUL from the replace stack */
1686 --start_col;
1687 }
1688 while (start_col < (int)curwin->w_cursor.col || replaced)
1689 {
1690 replace_push(NUL);
1691 if (replaced)
1692 {
1693 replace_push(replaced);
1694 replaced = NUL;
1695 }
1696 ++start_col;
1697 }
1698 }
1699
1700#ifdef FEAT_VREPLACE
1701 /*
1702 * For VREPLACE mode, we also have to fix the replace stack. In this case
1703 * it is always possible because we backspace over the whole line and then
1704 * put it back again the way we wanted it.
1705 */
1706 if (State & VREPLACE_FLAG)
1707 {
1708 /* If orig_line didn't allocate, just return. At least we did the job,
1709 * even if you can't backspace. */
1710 if (orig_line == NULL)
1711 return;
1712
1713 /* Save new line */
1714 new_line = vim_strsave(ml_get_curline());
1715 if (new_line == NULL)
1716 return;
1717
1718 /* We only put back the new line up to the cursor */
1719 new_line[curwin->w_cursor.col] = NUL;
1720
1721 /* Put back original line */
1722 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1723 curwin->w_cursor.col = orig_col;
1724
1725 /* Backspace from cursor to start of line */
1726 backspace_until_column(0);
1727
1728 /* Insert new stuff into line again */
1729 ins_bytes(new_line);
1730
1731 vim_free(new_line);
1732 }
1733#endif
1734}
1735
1736/*
1737 * Truncate the space at the end of a line. This is to be used only in an
1738 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1739 * modes.
1740 */
1741 void
1742truncate_spaces(line)
1743 char_u *line;
1744{
1745 int i;
1746
1747 /* find start of trailing white space */
1748 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1749 {
1750 if (State & REPLACE_FLAG)
1751 replace_join(0); /* remove a NUL from the replace stack */
1752 }
1753 line[i + 1] = NUL;
1754}
1755
1756#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1757 || defined(FEAT_COMMENTS) || defined(PROTO)
1758/*
1759 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1760 * modes correctly. May also be used when not in insert mode at all.
1761 */
1762 void
1763backspace_until_column(col)
1764 int col;
1765{
1766 while ((int)curwin->w_cursor.col > col)
1767 {
1768 curwin->w_cursor.col--;
1769 if (State & REPLACE_FLAG)
1770 replace_do_bs();
1771 else
1772 (void)del_char(FALSE);
1773 }
1774}
1775#endif
1776
1777#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1778/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001779 * CTRL-X pressed in Insert mode.
1780 */
1781 static void
1782ins_ctrl_x()
1783{
1784 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1785 * CTRL-V works like CTRL-N */
1786 if (ctrl_x_mode != CTRL_X_CMDLINE)
1787 {
1788 /* if the next ^X<> won't ADD nothing, then reset
1789 * compl_cont_status */
1790 if (compl_cont_status & CONT_N_ADDS)
1791 compl_cont_status = (compl_cont_status | CONT_INTRPT);
1792 else
1793 compl_cont_status = 0;
1794 /* We're not sure which CTRL-X mode it will be yet */
1795 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1796 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1797 edit_submode_pre = NULL;
1798 showmode();
1799 }
1800}
1801
1802/*
1803 * Return TRUE if the 'dict' or 'tsr' option can be used.
1804 */
1805 static int
1806has_compl_option(dict_opt)
1807 int dict_opt;
1808{
1809 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1810 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1811 {
1812 ctrl_x_mode = 0;
1813 edit_submode = NULL;
1814 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1815 : (char_u *)_("'thesaurus' option is empty"),
1816 hl_attr(HLF_E));
1817 if (emsg_silent == 0)
1818 {
1819 vim_beep();
1820 setcursor();
1821 out_flush();
1822 ui_delay(2000L, FALSE);
1823 }
1824 return FALSE;
1825 }
1826 return TRUE;
1827}
1828
1829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1831 * This depends on the current mode.
1832 */
1833 int
1834vim_is_ctrl_x_key(c)
1835 int c;
1836{
1837 /* Always allow ^R - let it's results then be checked */
1838 if (c == Ctrl_R)
1839 return TRUE;
1840
Bram Moolenaare3226be2005-12-18 22:10:00 +00001841 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
1842 if (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
1843 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN))
1844 return TRUE;
1845
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 switch (ctrl_x_mode)
1847 {
1848 case 0: /* Not in any CTRL-X mode */
1849 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1850 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001851 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1853 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1854 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001855 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1856 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 case CTRL_X_SCROLL:
1858 return (c == Ctrl_Y || c == Ctrl_E);
1859 case CTRL_X_WHOLE_LINE:
1860 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1861 case CTRL_X_FILES:
1862 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1863 case CTRL_X_DICTIONARY:
1864 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1865 case CTRL_X_THESAURUS:
1866 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1867 case CTRL_X_TAGS:
1868 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1869#ifdef FEAT_FIND_ID
1870 case CTRL_X_PATH_PATTERNS:
1871 return (c == Ctrl_P || c == Ctrl_N);
1872 case CTRL_X_PATH_DEFINES:
1873 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1874#endif
1875 case CTRL_X_CMDLINE:
1876 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1877 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001878#ifdef FEAT_COMPL_FUNC
1879 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001880 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001881 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001882 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001883#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001884 case CTRL_X_SPELL:
1885 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 }
1887 EMSG(_(e_internal));
1888 return FALSE;
1889}
1890
1891/*
1892 * This is like ins_compl_add(), but if ic and inf are set, then the
1893 * case of the originally typed text is used, and the case of the completed
1894 * text is infered, ie this tries to work out what case you probably wanted
1895 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001896 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 */
1898 int
Bram Moolenaar572cb562005-08-05 21:35:02 +00001899ins_compl_add_infercase(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 char_u *str;
1901 int len;
1902 char_u *fname;
1903 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001904 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905{
1906 int has_lower = FALSE;
1907 int was_letter = FALSE;
1908 int idx;
1909
1910 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
1911 {
1912 /* Infer case of completed part -- webb */
1913 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001914 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
1916 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001917 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001919 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
1921 has_lower = TRUE;
1922 if (isupper(IObuff[idx]))
1923 {
1924 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001925 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
1927 break;
1928 }
1929 }
1930 }
1931
1932 /*
1933 * Rule 2: No lower case, 2nd consecutive letter converted to
1934 * upper case.
1935 */
1936 if (!has_lower)
1937 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001938 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001940 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 && islower(IObuff[idx]))
1942 {
1943 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001944 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
1946 break;
1947 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001948 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 }
1950 }
1951
1952 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001953 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954
Bram Moolenaar572cb562005-08-05 21:35:02 +00001955 return ins_compl_add(IObuff, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00001957 return ins_compl_add(str, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958}
1959
1960/*
1961 * Add a match to the list of matches.
1962 * If the given string is already in the list of completions, then return
1963 * FAIL, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaar572cb562005-08-05 21:35:02 +00001964 * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb.
1965 *
1966 * New:
1967 * If the given string is already in the list of completions, then return
1968 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
1969 * maybe because alloc() returns NULL, then FAIL is returned -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00001971 int
1972ins_compl_add(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 char_u *str;
1974 int len;
1975 char_u *fname;
1976 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001977 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978{
Bram Moolenaar572cb562005-08-05 21:35:02 +00001979 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
1981 ui_breakcheck();
1982 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00001983 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (len < 0)
1985 len = (int)STRLEN(str);
1986
1987 /*
1988 * If the same match is already present, don't add it.
1989 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001990 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001992 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 do
1994 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00001995 if ( !(match->cp_flags & ORIGINAL_TEXT)
1996 && STRNCMP(match->cp_str, str, (size_t)len) == 0
1997 && match->cp_str[len] == NUL)
1998 return NOTDONE;
1999 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002000 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
2002
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002003 /* Remove any popup menu before changing the list of matches. */
2004 ins_compl_del_pum();
2005
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 /*
2007 * Allocate a new match structure.
2008 * Copy the values to the new match structure.
2009 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002010 match = (compl_T *)alloc((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002012 return FAIL;
2013 match->cp_number = -1;
2014 if (flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002016 match->cp_number = 0;
2017 match->cp_str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002019 else if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 {
2021 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002022 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 }
2024 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002025 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2027 * - NULL otherwise. --Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002028 if (fname && compl_curr_match && compl_curr_match->cp_fname
2029 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
2030 match->cp_fname = compl_curr_match->cp_fname;
2031 else if (fname && (match->cp_fname = vim_strsave(fname)) != NULL)
2032 flags |= FREE_FNAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002034 match->cp_fname = NULL;
2035 match->cp_flags = flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036
2037 /*
2038 * Link the new match structure in the list of matches.
2039 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002040 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002041 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 else if (dir == FORWARD)
2043 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002044 match->cp_next = compl_curr_match->cp_next;
2045 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
2047 else /* BACKWARD */
2048 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002049 match->cp_next = compl_curr_match;
2050 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002052 if (match->cp_next)
2053 match->cp_next->cp_prev = match;
2054 if (match->cp_prev)
2055 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002057 compl_first_match = match;
2058 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059
2060 return OK;
2061}
2062
2063/*
2064 * Add an array of matches to the list of matches.
2065 * Frees matches[].
2066 */
2067 static void
2068ins_compl_add_matches(num_matches, matches, dir)
2069 int num_matches;
2070 char_u **matches;
2071 int dir;
2072{
2073 int i;
2074 int add_r = OK;
2075 int ldir = dir;
2076
Bram Moolenaar572cb562005-08-05 21:35:02 +00002077 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 if ((add_r = ins_compl_add(matches[i], -1, NULL, ldir, 0)) == OK)
2079 /* if dir was BACKWARD then honor it just once */
2080 ldir = FORWARD;
2081 FreeWild(num_matches, matches);
2082}
2083
2084/* Make the completion list cyclic.
2085 * Return the number of matches (excluding the original).
2086 */
2087 static int
2088ins_compl_make_cyclic()
2089{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002090 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 int count = 0;
2092
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002093 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
2095 /*
2096 * Find the end of the list.
2097 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002098 match = compl_first_match;
2099 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002100 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002102 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 ++count;
2104 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002105 match->cp_next = compl_first_match;
2106 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
2108 return count;
2109}
2110
Bram Moolenaar9372a112005-12-06 19:59:18 +00002111/* "compl_match_array" points the currently displayed list of entries in the
2112 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002113static char_u **compl_match_array = NULL;
2114static int compl_match_arraysize;
2115
2116/*
2117 * Update the screen and when there is any scrolling remove the popup menu.
2118 */
2119 static void
2120ins_compl_upd_pum()
2121{
2122 int h;
2123
2124 if (compl_match_array != NULL)
2125 {
2126 h = curwin->w_cline_height;
2127 update_screen(0);
2128 if (h != curwin->w_cline_height)
2129 ins_compl_del_pum();
2130 }
2131}
2132
2133/*
2134 * Remove any popup menu.
2135 */
2136 static void
2137ins_compl_del_pum()
2138{
2139 if (compl_match_array != NULL)
2140 {
2141 pum_undisplay();
2142 vim_free(compl_match_array);
2143 compl_match_array = NULL;
2144 }
2145}
2146
2147/*
2148 * Return TRUE if the popup menu should be displayed.
2149 */
2150 static int
2151pum_wanted()
2152{
2153 compl_T *compl;
2154 int i;
2155
2156 /* 'completeopt' must contain "menu" */
2157 if (*p_cot == NUL)
2158 return FALSE;
2159
2160 /* The display looks bad on a B&W display. */
2161 if (t_colors < 8
2162#ifdef FEAT_GUI
2163 && !gui.in_use
2164#endif
2165 )
2166 return FALSE;
2167
2168 /* Don't display the popup menu if there are no matches or there is only
2169 * one (ignoring the original text). */
2170 compl = compl_first_match;
2171 i = 0;
2172 do
2173 {
2174 if (compl == NULL
2175 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2176 break;
2177 compl = compl->cp_next;
2178 } while (compl != compl_first_match);
2179
2180 return (i >= 2);
2181}
2182
2183/*
2184 * Show the popup menu for the list of matches.
2185 */
2186 static void
2187ins_compl_show_pum()
2188{
2189 compl_T *compl;
2190 int i;
2191 int cur = -1;
2192 colnr_T col;
2193
2194 if (!pum_wanted())
2195 return;
2196
2197 /* Update the screen before drawing the popup menu over it. */
2198 update_screen(0);
2199
2200 if (compl_match_array == NULL)
2201 {
2202 /* Need to build the popup menu list. */
2203 compl_match_arraysize = 0;
2204 compl = compl_first_match;
2205 do
2206 {
2207 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2208 ++compl_match_arraysize;
2209 compl = compl->cp_next;
2210 } while (compl != NULL && compl != compl_first_match);
2211 compl_match_array = (char_u **)alloc((unsigned)(sizeof(char_u **)
2212 * compl_match_arraysize));
2213 if (compl_match_array != NULL)
2214 {
2215 i = 0;
2216 compl = compl_first_match;
2217 do
2218 {
2219 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2220 {
2221 if (compl == compl_shown_match)
2222 cur = i;
2223 compl_match_array[i++] = compl->cp_str;
2224 }
2225 compl = compl->cp_next;
2226 } while (compl != NULL && compl != compl_first_match);
2227 }
2228 }
2229 else
2230 {
2231 /* popup menu already exists, only need to find the current item.*/
2232 i = 0;
2233 compl = compl_first_match;
2234 do
2235 {
2236 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2237 {
2238 if (compl == compl_shown_match)
2239 {
2240 cur = i;
2241 break;
2242 }
2243 ++i;
2244 }
2245 compl = compl->cp_next;
2246 } while (compl != NULL && compl != compl_first_match);
2247 }
2248
2249 if (compl_match_array != NULL)
2250 {
2251 /* Compute the screen column of the start of the completed text.
2252 * Use the cursor to get all wrapping and other settings right. */
2253 col = curwin->w_cursor.col;
2254 curwin->w_cursor.col = compl_col;
2255 validate_cursor_col();
2256 pum_display(compl_match_array, compl_match_arraysize, cur,
2257 curwin->w_cline_row + W_WINROW(curwin),
2258 curwin->w_cline_height,
2259 curwin->w_wcol + W_WINCOL(curwin));
2260 curwin->w_cursor.col = col;
2261 }
2262}
2263
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264#define DICT_FIRST (1) /* use just first element in "dict" */
2265#define DICT_EXACT (2) /* "dict" is the exact name of a file */
2266/*
2267 * Add any identifiers that match the given pattern to the list of
2268 * completions.
2269 */
2270 static void
2271ins_compl_dictionaries(dict, pat, dir, flags, thesaurus)
2272 char_u *dict;
2273 char_u *pat;
2274 int dir;
2275 int flags;
2276 int thesaurus;
2277{
2278 char_u *ptr;
2279 char_u *buf;
2280 FILE *fp;
2281 regmatch_T regmatch;
2282 int add_r;
2283 char_u **files;
2284 int count;
2285 int i;
2286 int save_p_scs;
2287
2288 buf = alloc(LSIZE);
2289 /* If 'infercase' is set, don't use 'smartcase' here */
2290 save_p_scs = p_scs;
2291 if (curbuf->b_p_inf)
2292 p_scs = FALSE;
2293 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2294 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2295 regmatch.rm_ic = ignorecase(pat);
2296 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002297 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
2299 /* copy one dictionary file name into buf */
2300 if (flags == DICT_EXACT)
2301 {
2302 count = 1;
2303 files = &dict;
2304 }
2305 else
2306 {
2307 /* Expand wildcards in the dictionary name, but do not allow
2308 * backticks (for security, the 'dict' option may have been set in
2309 * a modeline). */
2310 copy_option_part(&dict, buf, LSIZE, ",");
2311 if (vim_strchr(buf, '`') != NULL
2312 || expand_wildcards(1, &buf, &count, &files,
2313 EW_FILE|EW_SILENT) != OK)
2314 count = 0;
2315 }
2316
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002317 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
2319 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2320 if (flags != DICT_EXACT)
2321 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002322 vim_snprintf((char *)IObuff, IOSIZE,
2323 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2325 }
2326
2327 if (fp != NULL)
2328 {
2329 /*
2330 * Read dictionary file line by line.
2331 * Check each line for a match.
2332 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002333 while (!got_int && !compl_interrupted
2334 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
2336 ptr = buf;
2337 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2338 {
2339 ptr = regmatch.startp[0];
2340 ptr = find_word_end(ptr);
2341 add_r = ins_compl_add_infercase(regmatch.startp[0],
2342 (int)(ptr - regmatch.startp[0]),
2343 files[i], dir, 0);
2344 if (thesaurus)
2345 {
2346 char_u *wstart;
2347
2348 /*
2349 * Add the other matches on the line
2350 */
2351 while (!got_int)
2352 {
2353 /* Find start of the next word. Skip white
2354 * space and punctuation. */
2355 ptr = find_word_start(ptr);
2356 if (*ptr == NUL || *ptr == NL)
2357 break;
2358 wstart = ptr;
2359
2360 /* Find end of the word and add it. */
2361#ifdef FEAT_MBYTE
2362 if (has_mbyte)
2363 /* Japanese words may have characters in
2364 * different classes, only separate words
2365 * with single-byte non-word characters. */
2366 while (*ptr != NUL)
2367 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002368 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 if (l < 2 && !vim_iswordc(*ptr))
2371 break;
2372 ptr += l;
2373 }
2374 else
2375#endif
2376 ptr = find_word_end(ptr);
2377 add_r = ins_compl_add_infercase(wstart,
2378 (int)(ptr - wstart), files[i], dir, 0);
2379 }
2380 }
2381 if (add_r == OK)
2382 /* if dir was BACKWARD then honor it just once */
2383 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002384 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 break;
2386 /* avoid expensive call to vim_regexec() when at end
2387 * of line */
2388 if (*ptr == '\n' || got_int)
2389 break;
2390 }
2391 line_breakcheck();
Bram Moolenaar572cb562005-08-05 21:35:02 +00002392 ins_compl_check_keys(50);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
2394 fclose(fp);
2395 }
2396 }
2397 if (flags != DICT_EXACT)
2398 FreeWild(count, files);
2399 if (flags)
2400 break;
2401 }
2402 p_scs = save_p_scs;
2403 vim_free(regmatch.regprog);
2404 vim_free(buf);
2405}
2406
2407/*
2408 * Find the start of the next word.
2409 * Returns a pointer to the first char of the word. Also stops at a NUL.
2410 */
2411 char_u *
2412find_word_start(ptr)
2413 char_u *ptr;
2414{
2415#ifdef FEAT_MBYTE
2416 if (has_mbyte)
2417 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002418 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 else
2420#endif
2421 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2422 ++ptr;
2423 return ptr;
2424}
2425
2426/*
2427 * Find the end of the word. Assumes it starts inside a word.
2428 * Returns a pointer to just after the word.
2429 */
2430 char_u *
2431find_word_end(ptr)
2432 char_u *ptr;
2433{
2434#ifdef FEAT_MBYTE
2435 int start_class;
2436
2437 if (has_mbyte)
2438 {
2439 start_class = mb_get_class(ptr);
2440 if (start_class > 1)
2441 while (*ptr != NUL)
2442 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002443 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 if (mb_get_class(ptr) != start_class)
2445 break;
2446 }
2447 }
2448 else
2449#endif
2450 while (vim_iswordc(*ptr))
2451 ++ptr;
2452 return ptr;
2453}
2454
2455/*
2456 * Free the list of completions
2457 */
2458 static void
2459ins_compl_free()
2460{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002461 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002463 vim_free(compl_pattern);
2464 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002466 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002468
2469 ins_compl_del_pum();
2470 pum_clear();
2471
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002472 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 do
2474 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002475 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002476 compl_curr_match = compl_curr_match->cp_next;
2477 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002479 if (match->cp_flags & FREE_FNAME)
2480 vim_free(match->cp_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002482 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2483 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484}
2485
2486 static void
2487ins_compl_clear()
2488{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002489 compl_cont_status = 0;
2490 compl_started = FALSE;
2491 compl_matches = 0;
2492 vim_free(compl_pattern);
2493 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 save_sm = -1;
2495 edit_submode_extra = NULL;
2496}
2497
2498/*
2499 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002500 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002501 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002503 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504ins_compl_prep(c)
2505 int c;
2506{
2507 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 int temp;
2509 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002510 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
2512 /* Forget any previous 'special' messages if this is actually
2513 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2514 */
2515 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2516 edit_submode_extra = NULL;
2517
2518 /* Ignore end of Select mode mapping */
2519 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002520 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521
2522 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2523 {
2524 /*
2525 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2526 * it will be yet. Now we decide.
2527 */
2528 switch (c)
2529 {
2530 case Ctrl_E:
2531 case Ctrl_Y:
2532 ctrl_x_mode = CTRL_X_SCROLL;
2533 if (!(State & REPLACE_FLAG))
2534 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2535 else
2536 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2537 edit_submode_pre = NULL;
2538 showmode();
2539 break;
2540 case Ctrl_L:
2541 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2542 break;
2543 case Ctrl_F:
2544 ctrl_x_mode = CTRL_X_FILES;
2545 break;
2546 case Ctrl_K:
2547 ctrl_x_mode = CTRL_X_DICTIONARY;
2548 break;
2549 case Ctrl_R:
2550 /* Simply allow ^R to happen without affecting ^X mode */
2551 break;
2552 case Ctrl_T:
2553 ctrl_x_mode = CTRL_X_THESAURUS;
2554 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002555#ifdef FEAT_COMPL_FUNC
2556 case Ctrl_U:
2557 ctrl_x_mode = CTRL_X_FUNCTION;
2558 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002559 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002560 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002561 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002562#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002563 case 's':
2564 case Ctrl_S:
2565 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002566#ifdef FEAT_SYN_HL
2567 spell_back_to_badword();
2568#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002569 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 case Ctrl_RSB:
2571 ctrl_x_mode = CTRL_X_TAGS;
2572 break;
2573#ifdef FEAT_FIND_ID
2574 case Ctrl_I:
2575 case K_S_TAB:
2576 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2577 break;
2578 case Ctrl_D:
2579 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2580 break;
2581#endif
2582 case Ctrl_V:
2583 case Ctrl_Q:
2584 ctrl_x_mode = CTRL_X_CMDLINE;
2585 break;
2586 case Ctrl_P:
2587 case Ctrl_N:
2588 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
2589 * just started ^X mode, or there were enough ^X's to cancel
2590 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2591 * do normal expansion when interrupting a different mode (say
2592 * ^X^F^X^P or ^P^X^X^P, see below)
2593 * nothing changes if interrupting mode 0, (eg, the flag
2594 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002595 if (!(compl_cont_status & CONT_INTRPT))
2596 compl_cont_status |= CONT_LOCAL;
2597 else if (compl_cont_mode != 0)
2598 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 /* FALLTHROUGH */
2600 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002601 /* If we have typed at least 2 ^X's... for modes != 0, we set
2602 * compl_cont_status = 0 (eg, as if we had just started ^X
2603 * mode).
2604 * For mode 0, we set "compl_cont_mode" to an impossible
2605 * value, in both cases ^X^X can be used to restart the same
2606 * mode (avoiding ADDING mode).
2607 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2608 * 'complete' and local ^P expansions respectively.
2609 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2610 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 if (c == Ctrl_X)
2612 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002613 if (compl_cont_mode != 0)
2614 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002616 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
2618 ctrl_x_mode = 0;
2619 edit_submode = NULL;
2620 showmode();
2621 break;
2622 }
2623 }
2624 else if (ctrl_x_mode != 0)
2625 {
2626 /* We're already in CTRL-X mode, do we stay in it? */
2627 if (!vim_is_ctrl_x_key(c))
2628 {
2629 if (ctrl_x_mode == CTRL_X_SCROLL)
2630 ctrl_x_mode = 0;
2631 else
2632 ctrl_x_mode = CTRL_X_FINISHED;
2633 edit_submode = NULL;
2634 }
2635 showmode();
2636 }
2637
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002638 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 {
2640 /* Show error message from attempted keyword completion (probably
2641 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002642 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 showmode();
2644 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R)
2645 || ctrl_x_mode == CTRL_X_FINISHED)
2646 {
2647 /* Get here when we have finished typing a sequence of ^N and
2648 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002649 * memory that was used, and make sure we can redo the insert. */
2650 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002652 char_u *p;
2653
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 /*
2655 * If any of the original typed text has been changed,
2656 * eg when ignorecase is set, we must add back-spaces to
2657 * the redo buffer. We add as few as necessary to delete
2658 * just the part of the original text that has changed.
2659 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002660 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002661 p = compl_orig_text;
2662 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002664 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 ++ptr;
2666 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002667 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002669 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
2671
2672#ifdef FEAT_CINDENT
2673 want_cindent = (can_cindent && cindent_on());
2674#endif
2675 /*
2676 * When completing whole lines: fix indent for 'cindent'.
2677 * Otherwise, break line if it's too long.
2678 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002679 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 {
2681#ifdef FEAT_CINDENT
2682 /* re-indent the current line */
2683 if (want_cindent)
2684 {
2685 do_c_expr_indent();
2686 want_cindent = FALSE; /* don't do it again */
2687 }
2688#endif
2689 }
2690 else
2691 {
2692 /* put the cursor on the last char, for 'tw' formatting */
2693 curwin->w_cursor.col--;
2694 if (stop_arrow() == OK)
2695 insertchar(NUL, 0, -1);
2696 curwin->w_cursor.col++;
2697 }
2698
2699 auto_format(FALSE, TRUE);
2700
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002701 /* if the popup menu is displayed hitting Enter means accepting
2702 * the selection without inserting anything. */
2703 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
2704 retval = TRUE;
2705
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002707 compl_started = FALSE;
2708 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 msg_clr_cmdline(); /* necessary for "noshowmode" */
2710 ctrl_x_mode = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002711 if (save_sm >= 0)
2712 p_sm = save_sm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 if (edit_submode != NULL)
2714 {
2715 edit_submode = NULL;
2716 showmode();
2717 }
2718
2719#ifdef FEAT_CINDENT
2720 /*
2721 * Indent now if a key was typed that is in 'cinkeys'.
2722 */
2723 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2724 do_c_expr_indent();
2725#endif
2726 }
2727 }
2728
2729 /* reset continue_* if we left expansion-mode, if we stay they'll be
2730 * (re)set properly in ins_complete() */
2731 if (!vim_is_ctrl_x_key(c))
2732 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002733 compl_cont_status = 0;
2734 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002736
2737 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738}
2739
2740/*
2741 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2742 * (depending on flag) starting from buf and looking for a non-scanned
2743 * buffer (other than curbuf). curbuf is special, if it is called with
2744 * buf=curbuf then it has to be the first call for a given flag/expansion.
2745 *
2746 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2747 */
2748 static buf_T *
2749ins_compl_next_buf(buf, flag)
2750 buf_T *buf;
2751 int flag;
2752{
2753#ifdef FEAT_WINDOWS
2754 static win_T *wp;
2755#endif
2756
2757 if (flag == 'w') /* just windows */
2758 {
2759#ifdef FEAT_WINDOWS
2760 if (buf == curbuf) /* first call for this flag/expansion */
2761 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002762 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 && wp->w_buffer->b_scanned)
2764 ;
2765 buf = wp->w_buffer;
2766#else
2767 buf = curbuf;
2768#endif
2769 }
2770 else
2771 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2772 * (unlisted buffers)
2773 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002774 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 && ((flag == 'U'
2776 ? buf->b_p_bl
2777 : (!buf->b_p_bl
2778 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2779 || buf->b_scanned
2780 || (buf->b_ml.ml_mfp == NULL
2781 && ctrl_x_mode == CTRL_X_WHOLE_LINE)))
2782 ;
2783 return buf;
2784}
2785
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002786#ifdef FEAT_COMPL_FUNC
Bram Moolenaare344bea2005-09-01 20:46:49 +00002787static int expand_by_function __ARGS((int type, char_u *base, char_u ***matches));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002788
2789/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002790 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00002791 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002792 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002793 */
2794 static int
Bram Moolenaare344bea2005-09-01 20:46:49 +00002795expand_by_function(type, base, matches)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002796 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002797 char_u *base;
2798 char_u ***matches;
2799{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002800 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002801 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002802 listitem_T *li;
2803 garray_T ga;
2804 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002805 char_u *funcname;
2806 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002807
Bram Moolenaare344bea2005-09-01 20:46:49 +00002808 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
2809 if (*funcname == NUL)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002810 return 0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002811
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002812 /* Call 'completefunc' to obtain the list of matches. */
2813 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00002814 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002815
Bram Moolenaare344bea2005-09-01 20:46:49 +00002816 pos = curwin->w_cursor;
2817 matchlist = call_func_retlist(funcname, 2, args, FALSE);
2818 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002819 if (matchlist == NULL)
2820 return 0;
2821
2822 /* Go through the List with matches and put them in an array. */
2823 ga_init2(&ga, (int)sizeof(char_u *), 8);
2824 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002825 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002826 p = get_tv_string_chk(&li->li_tv);
2827 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002828 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002829 if (ga_grow(&ga, 1) == FAIL)
2830 break;
2831 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p);
2832 ++ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002833 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002834 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002835
2836 list_unref(matchlist);
2837 *matches = (char_u **)ga.ga_data;
2838 return ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002839}
2840#endif /* FEAT_COMPL_FUNC */
2841
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002842/*
2843 * Get the next expansion(s), using "compl_pattern".
2844 * The search starts at position "ini" in curbuf and in the direction dir.
2845 * When "compl_started" is FALSE start at that position, otherwise
2846 * continue where we stopped searching before.
2847 * This may return before finding all the matches.
2848 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 */
2850 static int
2851ins_compl_get_exp(ini, dir)
2852 pos_T *ini;
2853 int dir;
2854{
2855 static pos_T first_match_pos;
2856 static pos_T last_match_pos;
2857 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002858 static int found_all = FALSE; /* Found all matches of a
2859 certain type. */
2860 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861
Bram Moolenaar572cb562005-08-05 21:35:02 +00002862 pos_T *pos;
2863 char_u **matches;
2864 int save_p_scs;
2865 int save_p_ws;
2866 int save_p_ic;
2867 int i;
2868 int num_matches;
2869 int len;
2870 int found_new_match;
2871 int type = ctrl_x_mode;
2872 char_u *ptr;
2873 char_u *dict = NULL;
2874 int dict_f = 0;
2875 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002877 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
2879 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
2880 ins_buf->b_scanned = 0;
2881 found_all = FALSE;
2882 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002883 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002884 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 last_match_pos = first_match_pos = *ini;
2886 }
2887
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002888 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 pos = (dir == FORWARD) ? &last_match_pos : &first_match_pos;
2890 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
2891 for (;;)
2892 {
2893 found_new_match = FAIL;
2894
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002895 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 * or if found_all says this entry is done. For ^X^L only use the
2897 * entries from 'complete' that look in loaded buffers. */
2898 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002899 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 {
2901 found_all = FALSE;
2902 while (*e_cpt == ',' || *e_cpt == ' ')
2903 e_cpt++;
2904 if (*e_cpt == '.' && !curbuf->b_scanned)
2905 {
2906 ins_buf = curbuf;
2907 first_match_pos = *ini;
2908 /* So that ^N can match word immediately after cursor */
2909 if (ctrl_x_mode == 0)
2910 dec(&first_match_pos);
2911 last_match_pos = first_match_pos;
2912 type = 0;
2913 }
2914 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2915 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2916 {
2917 /* Scan a buffer, but not the current one. */
2918 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
2919 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002920 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 first_match_pos.col = last_match_pos.col = 0;
2922 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2923 last_match_pos.lnum = 0;
2924 type = 0;
2925 }
2926 else /* unloaded buffer, scan like dictionary */
2927 {
2928 found_all = TRUE;
2929 if (ins_buf->b_fname == NULL)
2930 continue;
2931 type = CTRL_X_DICTIONARY;
2932 dict = ins_buf->b_fname;
2933 dict_f = DICT_EXACT;
2934 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00002935 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 ins_buf->b_fname == NULL
2937 ? buf_spname(ins_buf)
2938 : ins_buf->b_sfname == NULL
2939 ? (char *)ins_buf->b_fname
2940 : (char *)ins_buf->b_sfname);
2941 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2942 }
2943 else if (*e_cpt == NUL)
2944 break;
2945 else
2946 {
2947 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2948 type = -1;
2949 else if (*e_cpt == 'k' || *e_cpt == 's')
2950 {
2951 if (*e_cpt == 'k')
2952 type = CTRL_X_DICTIONARY;
2953 else
2954 type = CTRL_X_THESAURUS;
2955 if (*++e_cpt != ',' && *e_cpt != NUL)
2956 {
2957 dict = e_cpt;
2958 dict_f = DICT_FIRST;
2959 }
2960 }
2961#ifdef FEAT_FIND_ID
2962 else if (*e_cpt == 'i')
2963 type = CTRL_X_PATH_PATTERNS;
2964 else if (*e_cpt == 'd')
2965 type = CTRL_X_PATH_DEFINES;
2966#endif
2967 else if (*e_cpt == ']' || *e_cpt == 't')
2968 {
2969 type = CTRL_X_TAGS;
2970 sprintf((char*)IObuff, _("Scanning tags."));
2971 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2972 }
2973 else
2974 type = -1;
2975
2976 /* in any case e_cpt is advanced to the next entry */
2977 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2978
2979 found_all = TRUE;
2980 if (type == -1)
2981 continue;
2982 }
2983 }
2984
2985 switch (type)
2986 {
2987 case -1:
2988 break;
2989#ifdef FEAT_FIND_ID
2990 case CTRL_X_PATH_PATTERNS:
2991 case CTRL_X_PATH_DEFINES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002992 find_pattern_in_path(compl_pattern, dir,
2993 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002995 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
2997 (linenr_T)1, (linenr_T)MAXLNUM);
2998 break;
2999#endif
3000
3001 case CTRL_X_DICTIONARY:
3002 case CTRL_X_THESAURUS:
3003 ins_compl_dictionaries(
3004 dict ? dict
3005 : (type == CTRL_X_THESAURUS
3006 ? (*curbuf->b_p_tsr == NUL
3007 ? p_tsr
3008 : curbuf->b_p_tsr)
3009 : (*curbuf->b_p_dict == NUL
3010 ? p_dict
3011 : curbuf->b_p_dict)),
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003012 compl_pattern, dir,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
3014 dict = NULL;
3015 break;
3016
3017 case CTRL_X_TAGS:
3018 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3019 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003020 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
3022 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003023 * of matches is found when compl_pattern is empty */
3024 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3026 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3027 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3028 {
3029 ins_compl_add_matches(num_matches, matches, dir);
3030 }
3031 p_ic = save_p_ic;
3032 break;
3033
3034 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003035 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3037 {
3038
3039 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003040 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 ins_compl_add_matches(num_matches, matches, dir);
3042 }
3043 break;
3044
3045 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003046 if (expand_cmdline(&compl_xp, compl_pattern,
3047 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 &num_matches, &matches) == EXPAND_OK)
3049 ins_compl_add_matches(num_matches, matches, dir);
3050 break;
3051
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003052#ifdef FEAT_COMPL_FUNC
3053 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003054 case CTRL_X_OMNI:
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003055 num_matches = expand_by_function(type, compl_pattern, &matches);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003056 if (num_matches > 0)
3057 ins_compl_add_matches(num_matches, matches, dir);
3058 break;
3059#endif
3060
Bram Moolenaar488c6512005-08-11 20:09:58 +00003061 case CTRL_X_SPELL:
3062#ifdef FEAT_SYN_HL
3063 num_matches = expand_spelling(first_match_pos.lnum,
3064 first_match_pos.col, compl_pattern, &matches);
3065 if (num_matches > 0)
3066 ins_compl_add_matches(num_matches, matches, dir);
3067#endif
3068 break;
3069
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 default: /* normal ^P/^N and ^X^L */
3071 /*
3072 * If 'infercase' is set, don't use 'smartcase' here
3073 */
3074 save_p_scs = p_scs;
3075 if (ins_buf->b_p_inf)
3076 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003077
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 /* buffers other than curbuf are scanned from the beginning or the
3079 * end but never from the middle, thus setting nowrapscan in this
3080 * buffers is a good idea, on the other hand, we always set
3081 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3082 save_p_ws = p_ws;
3083 if (ins_buf != curbuf)
3084 p_ws = FALSE;
3085 else if (*e_cpt == '.')
3086 p_ws = TRUE;
3087 for (;;)
3088 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003089 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003091 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3092 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003094 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003096 dir, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 else
3098 found_new_match = searchit(NULL, ins_buf, pos, dir,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003099 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 RE_LAST);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003101 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003103 /* set compl_started even on fail */
3104 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 first_match_pos = *pos;
3106 last_match_pos = *pos;
3107 }
3108 else if (first_match_pos.lnum == last_match_pos.lnum
3109 && first_match_pos.col == last_match_pos.col)
3110 found_new_match = FAIL;
3111 if (found_new_match == FAIL)
3112 {
3113 if (ins_buf == curbuf)
3114 found_all = TRUE;
3115 break;
3116 }
3117
3118 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003119 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 && ini->lnum == pos->lnum
3121 && ini->col == pos->col)
3122 continue;
3123 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3124 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3125 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003126 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 {
3128 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3129 continue;
3130 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3131 if (!p_paste)
3132 ptr = skipwhite(ptr);
3133 }
3134 len = (int)STRLEN(ptr);
3135 }
3136 else
3137 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003138 char_u *tmp_ptr = ptr;
3139
3140 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003142 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 /* Skip if already inside a word. */
3144 if (vim_iswordp(tmp_ptr))
3145 continue;
3146 /* Find start of next word. */
3147 tmp_ptr = find_word_start(tmp_ptr);
3148 }
3149 /* Find end of this word. */
3150 tmp_ptr = find_word_end(tmp_ptr);
3151 len = (int)(tmp_ptr - ptr);
3152
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003153 if ((compl_cont_status & CONT_ADDING)
3154 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 {
3156 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3157 {
3158 /* Try next line, if any. the new word will be
3159 * "join" as if the normal command "J" was used.
3160 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003161 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 * works -- Acevedo */
3163 STRNCPY(IObuff, ptr, len);
3164 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3165 tmp_ptr = ptr = skipwhite(ptr);
3166 /* Find start of next word. */
3167 tmp_ptr = find_word_start(tmp_ptr);
3168 /* Find end of next word. */
3169 tmp_ptr = find_word_end(tmp_ptr);
3170 if (tmp_ptr > ptr)
3171 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003172 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003174 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 IObuff[len++] = ' ';
3176 /* IObuf =~ "\k.* ", thus len >= 2 */
3177 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003178 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 || (vim_strchr(p_cpo, CPO_JOINSP)
3180 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003181 && (IObuff[len - 2] == '?'
3182 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 IObuff[len++] = ' ';
3184 }
3185 /* copy as much as posible of the new word */
3186 if (tmp_ptr - ptr >= IOSIZE - len)
3187 tmp_ptr = ptr + IOSIZE - len - 1;
3188 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3189 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003190 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 }
3192 IObuff[len] = NUL;
3193 ptr = IObuff;
3194 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003195 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 continue;
3197 }
3198 }
3199 if (ins_compl_add_infercase(ptr, len,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003200 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar572cb562005-08-05 21:35:02 +00003201 dir, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 {
3203 found_new_match = OK;
3204 break;
3205 }
3206 }
3207 p_scs = save_p_scs;
3208 p_ws = save_p_ws;
3209 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003210
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003211 /* check if compl_curr_match has changed, (e.g. other type of
3212 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003213 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 found_new_match = OK;
3215
3216 /* break the loop for specialized modes (use 'complete' just for the
3217 * generic ctrl_x_mode == 0) or when we've found a new match */
3218 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003219 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003220 {
3221 if (got_int)
3222 break;
3223 if (pum_wanted() && type != -1)
3224 /* Fill the popup menu as soon as possible. */
3225 ins_compl_check_keys(0);
3226 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3227 || compl_interrupted)
3228 break;
3229 compl_started = TRUE;
3230 }
3231 else
3232 {
3233 /* Mark a buffer scanned when it has been scanned completely */
3234 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3235 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003237 compl_started = FALSE;
3238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003240 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241
3242 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3243 && *e_cpt == NUL) /* Got to end of 'complete' */
3244 found_new_match = FAIL;
3245
3246 i = -1; /* total of matches, unknown */
3247 if (found_new_match == FAIL
3248 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3249 i = ins_compl_make_cyclic();
3250
3251 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003252 * just been made cyclic then we have to move compl_curr_match to the next
3253 * or previous entry (if any) -- Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003254 compl_curr_match = dir == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003255 if (compl_curr_match == NULL)
3256 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 return i;
3258}
3259
3260/* Delete the old text being completed. */
3261 static void
3262ins_compl_delete()
3263{
3264 int i;
3265
3266 /*
3267 * In insert mode: Delete the typed part.
3268 * In replace mode: Put the old characters back, if any.
3269 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003270 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 backspace_until_column(i);
3272 changed_cline_bef_curs();
3273}
3274
3275/* Insert the new text being completed. */
3276 static void
3277ins_compl_insert()
3278{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003279 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280}
3281
3282/*
3283 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003284 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3285 * get more completions. If it is FALSE, then we just do nothing when there
3286 * are no more completions in a given direction. The latter case is used when
3287 * we are still in the middle of finding completions, to allow browsing
3288 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 * Return the total number of matches, or -1 if still unknown -- webb.
3290 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003291 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3292 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 *
3294 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003295 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3296 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 */
3298 static int
Bram Moolenaare3226be2005-12-18 22:10:00 +00003299ins_compl_next(allow_get_expansion, count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003301 int count; /* repeat completion this many times; should
3302 be at least 1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
3304 int num_matches = -1;
3305 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003306 int todo = count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307
3308 if (allow_get_expansion)
3309 {
3310 /* Delete old text to be replaced */
3311 ins_compl_delete();
3312 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003313 compl_pending = FALSE;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003314
3315 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3316 * around. */
3317 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003319 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003321 compl_shown_match = compl_shown_match->cp_next;
3322 if (compl_shown_match->cp_next != NULL
3323 && compl_shown_match->cp_next == compl_first_match)
3324 break;
3325 }
3326 else if (compl_shows_dir == BACKWARD
3327 && compl_shown_match->cp_prev != NULL)
3328 {
3329 compl_shown_match = compl_shown_match->cp_prev;
3330 if (compl_shown_match == compl_first_match)
3331 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 }
3333 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003334 {
3335 compl_pending = TRUE;
3336 if (allow_get_expansion)
3337 {
3338 num_matches = ins_compl_get_exp(&compl_startpos,
3339 compl_direction);
3340 if (compl_pending)
3341 {
3342 if (compl_direction == compl_shows_dir)
3343 compl_shown_match = compl_curr_match;
3344 }
3345 }
3346 else
3347 return -1;
3348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 }
3350
3351 /* Insert the text of the new completion */
3352 ins_compl_insert();
3353
3354 if (!allow_get_expansion)
3355 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003356 /* may undisplay the popup menu first */
3357 ins_compl_upd_pum();
3358
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 /* Display the current match. */
3360 update_screen(0);
3361
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003362 /* display the updated popup menu */
3363 ins_compl_show_pum();
3364
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 /* Delete old text to be replaced, since we're still searching and
3366 * don't want to match ourselves! */
3367 ins_compl_delete();
3368 }
3369
3370 /*
3371 * Show the file name for the match (if any)
3372 * Truncate the file name to avoid a wait for return.
3373 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003374 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 {
3376 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003377 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 if (i <= 0)
3379 i = 0;
3380 else
3381 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003382 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 msg(IObuff);
3384 redraw_cmdline = FALSE; /* don't overwrite! */
3385 }
3386
3387 return num_matches;
3388}
3389
3390/*
3391 * Call this while finding completions, to check whether the user has hit a key
3392 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003393 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003395 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 */
3397 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003398ins_compl_check_keys(frequency)
3399 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
3401 static int count = 0;
3402
3403 int c;
3404
3405 /* Don't check when reading keys from a script. That would break the test
3406 * scripts */
3407 if (using_script())
3408 return;
3409
3410 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003411 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 return;
3413 count = 0;
3414
3415 ++no_mapping;
3416 c = vpeekc_any();
3417 --no_mapping;
3418 if (c != NUL)
3419 {
3420 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3421 {
3422 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003423 compl_shows_dir = ins_compl_key2dir(c);
3424 (void)ins_compl_next(FALSE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
3426 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003427 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003429 if (compl_pending && !got_int)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003430 (void)ins_compl_next(FALSE, 1);
3431}
3432
3433/*
3434 * Decide the direction of Insert mode complete from the key typed.
3435 * Returns BACKWARD or FORWARD.
3436 */
3437 static int
3438ins_compl_key2dir(c)
3439 int c;
3440{
3441 if (c == Ctrl_P || c == Ctrl_L || (pum_visible()
3442 && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP)))
3443 return BACKWARD;
3444 return FORWARD;
3445}
3446
3447/*
3448 * Decide the number of completions to move forward.
3449 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3450 */
3451 static int
3452ins_compl_key2count(c)
3453 int c;
3454{
3455 int h;
3456
3457 if (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
3458 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN))
3459 {
3460 h = pum_get_height();
3461 if (h > 3)
3462 h -= 2; /* keep some context */
3463 return h;
3464 }
3465 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466}
3467
3468/*
3469 * Do Insert mode completion.
3470 * Called when character "c" was typed, which has a meaning for completion.
3471 * Returns OK if completion was done, FAIL if something failed (out of mem).
3472 */
3473 static int
3474ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003475 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003477 char_u *line;
3478 int startcol = 0; /* column where searched text starts */
3479 colnr_T curs_col; /* cursor column */
3480 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
Bram Moolenaare3226be2005-12-18 22:10:00 +00003482 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003483 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
3485 /* First time we hit ^N or ^P (in a row, I mean) */
3486
3487 /* Turn off 'sm' so we don't show matches with ^X^L */
3488 save_sm = p_sm;
3489 p_sm = FALSE;
3490
3491 did_ai = FALSE;
3492#ifdef FEAT_SMARTINDENT
3493 did_si = FALSE;
3494 can_si = FALSE;
3495 can_si_back = FALSE;
3496#endif
3497 if (stop_arrow() == FAIL)
3498 return FAIL;
3499
3500 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003501 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
3503 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003504 * "compl_startpos" to the cursor as a pattern to add a new word
3505 * instead of expand the one before the cursor, in word-wise if
3506 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 * is not in the same line as the cursor then fix it (the line has
3508 * been split because it was longer than 'tw'). if SOL is set then
3509 * skip the previous pattern, a word at the beginning of the line has
3510 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003511 if ((compl_cont_status & CONT_INTRPT) && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
3513 /*
3514 * it is a continued search
3515 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003516 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3518 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3519 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003520 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003522 /* line (probably) wrapped, set compl_startpos to the
3523 * first non_blank in the line, if it is not a wordchar
3524 * include it to get a better pattern, but then we don't
3525 * want the "\\<" prefix, check it bellow */
3526 compl_col = (colnr_T)(skipwhite(line) - line);
3527 compl_startpos.col = compl_col;
3528 compl_startpos.lnum = curwin->w_cursor.lnum;
3529 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
3531 else
3532 {
3533 /* S_IPOS was set when we inserted a word that was at the
3534 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003535 * mode but first we need to redefine compl_startpos */
3536 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003538 compl_cont_status |= CONT_SOL;
3539 compl_startpos.col = (colnr_T)(skipwhite(
3540 line + compl_length
3541 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003543 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003545 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003546 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 * have enough space? just being paranoic */
3548#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003549 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003551 compl_cont_status &= ~CONT_SOL;
3552 compl_length = (IOSIZE - MIN_SPACE);
3553 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003555 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3556 if (compl_length < 1)
3557 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
3559 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003560 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003562 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
3564 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003565 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003567 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003569 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003571 compl_cont_status = 0;
3572 compl_cont_status |= CONT_N_ADDS;
3573 compl_startpos = curwin->w_cursor;
3574 startcol = (int)curs_col;
3575 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
3577
3578 /* Work out completion pattern and original text -- webb */
3579 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3580 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003581 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3583 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003584 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003586 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003588 compl_col += ++startcol;
3589 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
3591 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003592 compl_pattern = str_foldcase(line + compl_col,
3593 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003595 compl_pattern = vim_strnsave(line + compl_col,
3596 compl_length);
3597 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 return FAIL;
3599 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003600 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
3602 char_u *prefix = (char_u *)"\\<";
3603
3604 /* we need 3 extra chars, 1 for the NUL and
3605 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003606 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3607 compl_length) + 3);
3608 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003610 if (!vim_iswordp(line + compl_col)
3611 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 && (
3613#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003614 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003616 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617#endif
3618 )))
3619 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003620 STRCPY((char *)compl_pattern, prefix);
3621 (void)quote_meta(compl_pattern + STRLEN(prefix),
3622 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003624 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003626 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003628 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629#endif
3630 )
3631 {
3632 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003633 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3634 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003636 compl_col += curs_col;
3637 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 }
3639 else
3640 {
3641#ifdef FEAT_MBYTE
3642 /* Search the point of change class of multibyte character
3643 * or not a word single byte character backward. */
3644 if (has_mbyte)
3645 {
3646 int base_class;
3647 int head_off;
3648
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003649 startcol -= (*mb_head_off)(line, line + startcol);
3650 base_class = mb_get_class(line + startcol);
3651 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003653 head_off = (*mb_head_off)(line, line + startcol);
3654 if (base_class != mb_get_class(line + startcol
3655 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003657 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
3659 }
3660 else
3661#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003662 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003664 compl_col += ++startcol;
3665 compl_length = (int)curs_col - startcol;
3666 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
3668 /* Only match word with at least two chars -- webb
3669 * there's no need to call quote_meta,
3670 * alloc(7) is enough -- Acevedo
3671 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003672 compl_pattern = alloc(7);
3673 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003675 STRCPY((char *)compl_pattern, "\\<");
3676 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3677 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 }
3679 else
3680 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003681 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3682 compl_length) + 3);
3683 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003685 STRCPY((char *)compl_pattern, "\\<");
3686 (void)quote_meta(compl_pattern + 2, line + compl_col,
3687 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
3689 }
3690 }
3691 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3692 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003693 compl_col = skipwhite(line) - line;
3694 compl_length = (int)curs_col - (int)compl_col;
3695 if (compl_length < 0) /* cursor in indent: empty pattern */
3696 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003698 compl_pattern = str_foldcase(line + compl_col, compl_length,
3699 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003701 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3702 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 return FAIL;
3704 }
3705 else if (ctrl_x_mode == CTRL_X_FILES)
3706 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003707 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003709 compl_col += ++startcol;
3710 compl_length = (int)curs_col - startcol;
3711 compl_pattern = addstar(line + compl_col, compl_length,
3712 EXPAND_FILES);
3713 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 return FAIL;
3715 }
3716 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3717 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003718 compl_pattern = vim_strnsave(line, curs_col);
3719 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003721 set_cmd_context(&compl_xp, compl_pattern,
3722 (int)STRLEN(compl_pattern), curs_col);
3723 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3724 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003726 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
3727 compl_col = startcol;
3728 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003730 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003731 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00003732#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003733 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00003734 * Call user defined function 'completefunc' with "a:findstart"
3735 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003736 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00003737 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003738 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003739 char_u *funcname;
3740 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003741
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003742 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00003743 * string */
3744 funcname = ctrl_x_mode == CTRL_X_FUNCTION
3745 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3746 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003747 {
3748 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3749 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003750 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003751 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003752
3753 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003754 args[1] = NULL;
3755 pos = curwin->w_cursor;
3756 col = call_func_retnr(funcname, 2, args, FALSE);
3757 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003758
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003759 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003760 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003761 compl_col = col;
3762 if ((colnr_T)compl_col > curs_col)
3763 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003764
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003765 /* Setup variables for completion. Need to obtain "line" again,
3766 * it may have become invalid. */
3767 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003768 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003769 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3770 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003771#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003772 return FAIL;
3773 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00003774 else if (ctrl_x_mode == CTRL_X_SPELL)
3775 {
3776#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00003777 if (spell_bad_len > 0)
3778 compl_col = curs_col - spell_bad_len;
3779 else
3780 compl_col = spell_word_start(startcol);
3781 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00003782 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003783 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003784 compl_length = (int)curs_col - compl_col;
3785 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3786 if (compl_pattern == NULL)
3787#endif
3788 return FAIL;
3789 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003790 else
3791 {
3792 EMSG2(_(e_intern2), "ins_complete()");
3793 return FAIL;
3794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003796 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 {
3798 edit_submode_pre = (char_u *)_(" Adding");
3799 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3800 {
3801 /* Insert a new line, keep indentation but ignore 'comments' */
3802#ifdef FEAT_COMMENTS
3803 char_u *old = curbuf->b_p_com;
3804
3805 curbuf->b_p_com = (char_u *)"";
3806#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003807 compl_startpos.lnum = curwin->w_cursor.lnum;
3808 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 ins_eol('\r');
3810#ifdef FEAT_COMMENTS
3811 curbuf->b_p_com = old;
3812#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003813 compl_length = 0;
3814 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816 }
3817 else
3818 {
3819 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003820 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003823 if (compl_cont_status & CONT_LOCAL)
3824 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 else
3826 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
3827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003829 * "compl_orig_text" itself (not a copy) is added, it will be freed
3830 * when the list of matches is freed. */
3831 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
3832 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
3833 -1, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003835 vim_free(compl_pattern);
3836 compl_pattern = NULL;
3837 vim_free(compl_orig_text);
3838 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 return FAIL;
3840 }
3841
3842 /* showmode might reset the internal line pointers, so it must
3843 * be called before line = ml_get(), or when this address is no
3844 * longer needed. -- Acevedo.
3845 */
3846 edit_submode_extra = (char_u *)_("-- Searching...");
3847 edit_submode_highl = HLF_COUNT;
3848 showmode();
3849 edit_submode_extra = NULL;
3850 out_flush();
3851 }
3852
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003853 compl_shown_match = compl_curr_match;
3854 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855
3856 /*
3857 * Find next match.
3858 */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003859 n = ins_compl_next(TRUE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003861 /* may undisplay the popup menu */
3862 ins_compl_upd_pum();
3863
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003864 if (n > 1) /* all matches have been found */
3865 compl_matches = n;
3866 compl_curr_match = compl_shown_match;
3867 compl_direction = compl_shows_dir;
3868 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869
3870 /* eat the ESC to avoid leaving insert mode */
3871 if (got_int && !global_busy)
3872 {
3873 (void)vgetc();
3874 got_int = FALSE;
3875 }
3876
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003877 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003878 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003880 edit_submode_extra = (compl_cont_status & CONT_ADDING)
3881 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
3883 edit_submode_highl = HLF_E;
3884 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
3885 * because we couldn't expand anything at first place, but if we used
3886 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
3887 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003888 if ( compl_length > 1
3889 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 || (ctrl_x_mode != 0
3891 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
3892 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003893 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
3895
Bram Moolenaar572cb562005-08-05 21:35:02 +00003896 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003897 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003899 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
3901 if (edit_submode_extra == NULL)
3902 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003903 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 {
3905 edit_submode_extra = (char_u *)_("Back at original");
3906 edit_submode_highl = HLF_W;
3907 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003908 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
3910 edit_submode_extra = (char_u *)_("Word from other line");
3911 edit_submode_highl = HLF_COUNT;
3912 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00003913 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 {
3915 edit_submode_extra = (char_u *)_("The only match");
3916 edit_submode_highl = HLF_COUNT;
3917 }
3918 else
3919 {
3920 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003921 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003923 int number = 0;
3924 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003926 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 {
3928 /* search backwards for the first valid (!= -1) number.
3929 * This should normally succeed already at the first loop
3930 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003931 for (match = compl_curr_match->cp_prev; match != NULL
3932 && match != compl_first_match;
3933 match = match->cp_prev)
3934 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003936 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 break;
3938 }
3939 if (match != NULL)
3940 /* go up and assign all numbers which are not assigned
3941 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003942 for (match = match->cp_next;
3943 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003944 match = match->cp_next)
3945 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947 else /* BACKWARD */
3948 {
3949 /* search forwards (upwards) for the first valid (!= -1)
3950 * number. This should normally succeed already at the
3951 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003952 for (match = compl_curr_match->cp_next; match != NULL
3953 && match != compl_first_match;
3954 match = match->cp_next)
3955 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003957 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 break;
3959 }
3960 if (match != NULL)
3961 /* go down and assign all numbers which are not
3962 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003963 for (match = match->cp_prev; match
3964 && match->cp_number == -1;
3965 match = match->cp_prev)
3966 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 }
3968 }
3969
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003970 /* The match should always have a sequence number now, this is
3971 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003972 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 {
3974 /* Space for 10 text chars. + 2x10-digit no.s */
3975 static char_u match_ref[31];
3976
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003977 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00003979 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003981 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00003982 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003983 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 edit_submode_extra = match_ref;
3985 edit_submode_highl = HLF_R;
3986 if (dollar_vcol)
3987 curs_columns(FALSE);
3988 }
3989 }
3990 }
3991
3992 /* Show a message about what (completion) mode we're in. */
3993 showmode();
3994 if (edit_submode_extra != NULL)
3995 {
3996 if (!p_smd)
3997 msg_attr(edit_submode_extra,
3998 edit_submode_highl < HLF_COUNT
3999 ? hl_attr(edit_submode_highl) : 0);
4000 }
4001 else
4002 msg_clr_cmdline(); /* necessary for "noshowmode" */
4003
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004004 ins_compl_show_pum();
4005
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 return OK;
4007}
4008
4009/*
4010 * Looks in the first "len" chars. of "src" for search-metachars.
4011 * If dest is not NULL the chars. are copied there quoting (with
4012 * a backslash) the metachars, and dest would be NUL terminated.
4013 * Returns the length (needed) of dest
4014 */
4015 static int
4016quote_meta(dest, src, len)
4017 char_u *dest;
4018 char_u *src;
4019 int len;
4020{
4021 int m;
4022
4023 for (m = len; --len >= 0; src++)
4024 {
4025 switch (*src)
4026 {
4027 case '.':
4028 case '*':
4029 case '[':
4030 if (ctrl_x_mode == CTRL_X_DICTIONARY
4031 || ctrl_x_mode == CTRL_X_THESAURUS)
4032 break;
4033 case '~':
4034 if (!p_magic) /* quote these only if magic is set */
4035 break;
4036 case '\\':
4037 if (ctrl_x_mode == CTRL_X_DICTIONARY
4038 || ctrl_x_mode == CTRL_X_THESAURUS)
4039 break;
4040 case '^': /* currently it's not needed. */
4041 case '$':
4042 m++;
4043 if (dest != NULL)
4044 *dest++ = '\\';
4045 break;
4046 }
4047 if (dest != NULL)
4048 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004049# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 /* Copy remaining bytes of a multibyte character. */
4051 if (has_mbyte)
4052 {
4053 int i, mb_len;
4054
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004055 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 if (mb_len > 0 && len >= mb_len)
4057 for (i = 0; i < mb_len; ++i)
4058 {
4059 --len;
4060 ++src;
4061 if (dest != NULL)
4062 *dest++ = *src;
4063 }
4064 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004065# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067 if (dest != NULL)
4068 *dest = NUL;
4069
4070 return m;
4071}
4072#endif /* FEAT_INS_EXPAND */
4073
4074/*
4075 * Next character is interpreted literally.
4076 * A one, two or three digit decimal number is interpreted as its byte value.
4077 * If one or two digits are entered, the next character is given to vungetc().
4078 * For Unicode a character > 255 may be returned.
4079 */
4080 int
4081get_literal()
4082{
4083 int cc;
4084 int nc;
4085 int i;
4086 int hex = FALSE;
4087 int octal = FALSE;
4088#ifdef FEAT_MBYTE
4089 int unicode = 0;
4090#endif
4091
4092 if (got_int)
4093 return Ctrl_C;
4094
4095#ifdef FEAT_GUI
4096 /*
4097 * In GUI there is no point inserting the internal code for a special key.
4098 * It is more useful to insert the string "<KEY>" instead. This would
4099 * probably be useful in a text window too, but it would not be
4100 * vi-compatible (maybe there should be an option for it?) -- webb
4101 */
4102 if (gui.in_use)
4103 ++allow_keys;
4104#endif
4105#ifdef USE_ON_FLY_SCROLL
4106 dont_scroll = TRUE; /* disallow scrolling here */
4107#endif
4108 ++no_mapping; /* don't map the next key hits */
4109 cc = 0;
4110 i = 0;
4111 for (;;)
4112 {
4113 do
4114 nc = safe_vgetc();
4115 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4116 || nc == K_HOR_SCROLLBAR);
4117#ifdef FEAT_CMDL_INFO
4118 if (!(State & CMDLINE)
4119# ifdef FEAT_MBYTE
4120 && MB_BYTE2LEN_CHECK(nc) == 1
4121# endif
4122 )
4123 add_to_showcmd(nc);
4124#endif
4125 if (nc == 'x' || nc == 'X')
4126 hex = TRUE;
4127 else if (nc == 'o' || nc == 'O')
4128 octal = TRUE;
4129#ifdef FEAT_MBYTE
4130 else if (nc == 'u' || nc == 'U')
4131 unicode = nc;
4132#endif
4133 else
4134 {
4135 if (hex
4136#ifdef FEAT_MBYTE
4137 || unicode != 0
4138#endif
4139 )
4140 {
4141 if (!vim_isxdigit(nc))
4142 break;
4143 cc = cc * 16 + hex2nr(nc);
4144 }
4145 else if (octal)
4146 {
4147 if (nc < '0' || nc > '7')
4148 break;
4149 cc = cc * 8 + nc - '0';
4150 }
4151 else
4152 {
4153 if (!VIM_ISDIGIT(nc))
4154 break;
4155 cc = cc * 10 + nc - '0';
4156 }
4157
4158 ++i;
4159 }
4160
4161 if (cc > 255
4162#ifdef FEAT_MBYTE
4163 && unicode == 0
4164#endif
4165 )
4166 cc = 255; /* limit range to 0-255 */
4167 nc = 0;
4168
4169 if (hex) /* hex: up to two chars */
4170 {
4171 if (i >= 2)
4172 break;
4173 }
4174#ifdef FEAT_MBYTE
4175 else if (unicode) /* Unicode: up to four or eight chars */
4176 {
4177 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4178 break;
4179 }
4180#endif
4181 else if (i >= 3) /* decimal or octal: up to three chars */
4182 break;
4183 }
4184 if (i == 0) /* no number entered */
4185 {
4186 if (nc == K_ZERO) /* NUL is stored as NL */
4187 {
4188 cc = '\n';
4189 nc = 0;
4190 }
4191 else
4192 {
4193 cc = nc;
4194 nc = 0;
4195 }
4196 }
4197
4198 if (cc == 0) /* NUL is stored as NL */
4199 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004200#ifdef FEAT_MBYTE
4201 if (enc_dbcs && (cc & 0xff) == 0)
4202 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4203 second byte will cause trouble! */
4204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
4206 --no_mapping;
4207#ifdef FEAT_GUI
4208 if (gui.in_use)
4209 --allow_keys;
4210#endif
4211 if (nc)
4212 vungetc(nc);
4213 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4214 return cc;
4215}
4216
4217/*
4218 * Insert character, taking care of special keys and mod_mask
4219 */
4220 static void
4221insert_special(c, allow_modmask, ctrlv)
4222 int c;
4223 int allow_modmask;
4224 int ctrlv; /* c was typed after CTRL-V */
4225{
4226 char_u *p;
4227 int len;
4228
4229 /*
4230 * Special function key, translate into "<Key>". Up to the last '>' is
4231 * inserted with ins_str(), so as not to replace characters in replace
4232 * mode.
4233 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4234 * unless 'allow_modmask' is TRUE.
4235 */
4236#ifdef MACOS
4237 /* Command-key never produces a normal key */
4238 if (mod_mask & MOD_MASK_CMD)
4239 allow_modmask = TRUE;
4240#endif
4241 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4242 {
4243 p = get_special_key_name(c, mod_mask);
4244 len = (int)STRLEN(p);
4245 c = p[len - 1];
4246 if (len > 2)
4247 {
4248 if (stop_arrow() == FAIL)
4249 return;
4250 p[len - 1] = NUL;
4251 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004252 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 ctrlv = FALSE;
4254 }
4255 }
4256 if (stop_arrow() == OK)
4257 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4258}
4259
4260/*
4261 * Special characters in this context are those that need processing other
4262 * than the simple insertion that can be performed here. This includes ESC
4263 * which terminates the insert, and CR/NL which need special processing to
4264 * open up a new line. This routine tries to optimize insertions performed by
4265 * the "redo", "undo" or "put" commands, so it needs to know when it should
4266 * stop and defer processing to the "normal" mechanism.
4267 * '0' and '^' are special, because they can be followed by CTRL-D.
4268 */
4269#ifdef EBCDIC
4270# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4271#else
4272# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4273#endif
4274
4275#ifdef FEAT_MBYTE
4276# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4277#else
4278# define WHITECHAR(cc) vim_iswhite(cc)
4279#endif
4280
4281 void
4282insertchar(c, flags, second_indent)
4283 int c; /* character to insert or NUL */
4284 int flags; /* INSCHAR_FORMAT, etc. */
4285 int second_indent; /* indent for second line if >= 0 */
4286{
4287 int haveto_redraw = FALSE;
4288 int textwidth;
4289#ifdef FEAT_COMMENTS
4290 colnr_T leader_len;
4291 char_u *p;
4292 int no_leader = FALSE;
4293 int do_comments = (flags & INSCHAR_DO_COM);
4294#endif
4295 int fo_white_par;
4296 int first_line = TRUE;
4297 int fo_ins_blank;
4298#ifdef FEAT_MBYTE
4299 int fo_multibyte;
4300#endif
4301 int save_char = NUL;
4302 int cc;
4303
4304 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4305 fo_ins_blank = has_format_option(FO_INS_BLANK);
4306#ifdef FEAT_MBYTE
4307 fo_multibyte = has_format_option(FO_MBYTE_BREAK);
4308#endif
4309 fo_white_par = has_format_option(FO_WHITE_PAR);
4310
4311 /*
4312 * Try to break the line in two or more pieces when:
4313 * - Always do this if we have been called to do formatting only.
4314 * - Always do this when 'formatoptions' has the 'a' flag and the line
4315 * ends in white space.
4316 * - Otherwise:
4317 * - Don't do this if inserting a blank
4318 * - Don't do this if an existing character is being replaced, unless
4319 * we're in VREPLACE mode.
4320 * - Do this if the cursor is not on the line where insert started
4321 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4322 * before the insert.
4323 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4324 * before 'textwidth'
4325 */
4326 if (textwidth
4327 && ((flags & INSCHAR_FORMAT)
4328 || (!vim_iswhite(c)
4329 && !((State & REPLACE_FLAG)
4330#ifdef FEAT_VREPLACE
4331 && !(State & VREPLACE_FLAG)
4332#endif
4333 && *ml_get_cursor() != NUL)
4334 && (curwin->w_cursor.lnum != Insstart.lnum
4335 || ((!has_format_option(FO_INS_LONG)
4336 || Insstart_textlen <= (colnr_T)textwidth)
4337 && (!fo_ins_blank
4338 || Insstart_blank_vcol <= (colnr_T)textwidth
4339 ))))))
4340 {
4341 /*
4342 * When 'ai' is off we don't want a space under the cursor to be
4343 * deleted. Replace it with an 'x' temporarily.
4344 */
4345 if (!curbuf->b_p_ai)
4346 {
4347 cc = gchar_cursor();
4348 if (vim_iswhite(cc))
4349 {
4350 save_char = cc;
4351 pchar_cursor('x');
4352 }
4353 }
4354
4355 /*
4356 * Repeat breaking lines, until the current line is not too long.
4357 */
4358 while (!got_int)
4359 {
4360 int startcol; /* Cursor column at entry */
4361 int wantcol; /* column at textwidth border */
4362 int foundcol; /* column for start of spaces */
4363 int end_foundcol = 0; /* column for start of word */
4364 colnr_T len;
4365 colnr_T virtcol;
4366#ifdef FEAT_VREPLACE
4367 int orig_col = 0;
4368 char_u *saved_text = NULL;
4369#endif
4370 colnr_T col;
4371
4372 virtcol = get_nolist_virtcol();
4373 if (virtcol < (colnr_T)textwidth)
4374 break;
4375
4376#ifdef FEAT_COMMENTS
4377 if (no_leader)
4378 do_comments = FALSE;
4379 else if (!(flags & INSCHAR_FORMAT)
4380 && has_format_option(FO_WRAP_COMS))
4381 do_comments = TRUE;
4382
4383 /* Don't break until after the comment leader */
4384 if (do_comments)
4385 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
4386 else
4387 leader_len = 0;
4388
4389 /* If the line doesn't start with a comment leader, then don't
4390 * start one in a following broken line. Avoids that a %word
4391 * moved to the start of the next line causes all following lines
4392 * to start with %. */
4393 if (leader_len == 0)
4394 no_leader = TRUE;
4395#endif
4396 if (!(flags & INSCHAR_FORMAT)
4397#ifdef FEAT_COMMENTS
4398 && leader_len == 0
4399#endif
4400 && !has_format_option(FO_WRAP))
4401
4402 {
4403 textwidth = 0;
4404 break;
4405 }
4406 if ((startcol = curwin->w_cursor.col) == 0)
4407 break;
4408
4409 /* find column of textwidth border */
4410 coladvance((colnr_T)textwidth);
4411 wantcol = curwin->w_cursor.col;
4412
4413 curwin->w_cursor.col = startcol - 1;
4414#ifdef FEAT_MBYTE
4415 /* Correct cursor for multi-byte character. */
4416 if (has_mbyte)
4417 mb_adjust_cursor();
4418#endif
4419 foundcol = 0;
4420
4421 /*
4422 * Find position to break at.
4423 * Stop at first entered white when 'formatoptions' has 'v'
4424 */
4425 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
4426 || curwin->w_cursor.lnum != Insstart.lnum
4427 || curwin->w_cursor.col >= Insstart.col)
4428 {
4429 cc = gchar_cursor();
4430 if (WHITECHAR(cc))
4431 {
4432 /* remember position of blank just before text */
4433 end_foundcol = curwin->w_cursor.col;
4434
4435 /* find start of sequence of blanks */
4436 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
4437 {
4438 dec_cursor();
4439 cc = gchar_cursor();
4440 }
4441 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
4442 break; /* only spaces in front of text */
4443#ifdef FEAT_COMMENTS
4444 /* Don't break until after the comment leader */
4445 if (curwin->w_cursor.col < leader_len)
4446 break;
4447#endif
4448 if (has_format_option(FO_ONE_LETTER))
4449 {
4450 /* do not break after one-letter words */
4451 if (curwin->w_cursor.col == 0)
4452 break; /* one-letter word at begin */
4453
4454 col = curwin->w_cursor.col;
4455 dec_cursor();
4456 cc = gchar_cursor();
4457
4458 if (WHITECHAR(cc))
4459 continue; /* one-letter, continue */
4460 curwin->w_cursor.col = col;
4461 }
4462#ifdef FEAT_MBYTE
4463 if (has_mbyte)
4464 foundcol = curwin->w_cursor.col
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004465 + (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 else
4467#endif
4468 foundcol = curwin->w_cursor.col + 1;
4469 if (curwin->w_cursor.col < (colnr_T)wantcol)
4470 break;
4471 }
4472#ifdef FEAT_MBYTE
4473 else if (cc >= 0x100 && fo_multibyte
4474 && curwin->w_cursor.col <= (colnr_T)wantcol)
4475 {
4476 /* Break after or before a multi-byte character. */
4477 foundcol = curwin->w_cursor.col;
4478 if (curwin->w_cursor.col < (colnr_T)wantcol)
4479 foundcol += (*mb_char2len)(cc);
4480 end_foundcol = foundcol;
4481 break;
4482 }
4483#endif
4484 if (curwin->w_cursor.col == 0)
4485 break;
4486 dec_cursor();
4487 }
4488
4489 if (foundcol == 0) /* no spaces, cannot break line */
4490 {
4491 curwin->w_cursor.col = startcol;
4492 break;
4493 }
4494
4495 /* Going to break the line, remove any "$" now. */
4496 undisplay_dollar();
4497
4498 /*
4499 * Offset between cursor position and line break is used by replace
4500 * stack functions. VREPLACE does not use this, and backspaces
4501 * over the text instead.
4502 */
4503#ifdef FEAT_VREPLACE
4504 if (State & VREPLACE_FLAG)
4505 orig_col = startcol; /* Will start backspacing from here */
4506 else
4507#endif
4508 replace_offset = startcol - end_foundcol - 1;
4509
4510 /*
4511 * adjust startcol for spaces that will be deleted and
4512 * characters that will remain on top line
4513 */
4514 curwin->w_cursor.col = foundcol;
4515 while (cc = gchar_cursor(), WHITECHAR(cc))
4516 inc_cursor();
4517 startcol -= curwin->w_cursor.col;
4518 if (startcol < 0)
4519 startcol = 0;
4520
4521#ifdef FEAT_VREPLACE
4522 if (State & VREPLACE_FLAG)
4523 {
4524 /*
4525 * In VREPLACE mode, we will backspace over the text to be
4526 * wrapped, so save a copy now to put on the next line.
4527 */
4528 saved_text = vim_strsave(ml_get_cursor());
4529 curwin->w_cursor.col = orig_col;
4530 if (saved_text == NULL)
4531 break; /* Can't do it, out of memory */
4532 saved_text[startcol] = NUL;
4533
4534 /* Backspace over characters that will move to the next line */
4535 if (!fo_white_par)
4536 backspace_until_column(foundcol);
4537 }
4538 else
4539#endif
4540 {
4541 /* put cursor after pos. to break line */
4542 if (!fo_white_par)
4543 curwin->w_cursor.col = foundcol;
4544 }
4545
4546 /*
4547 * Split the line just before the margin.
4548 * Only insert/delete lines, but don't really redraw the window.
4549 */
4550 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
4551 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
4552#ifdef FEAT_COMMENTS
4553 + (do_comments ? OPENLINE_DO_COM : 0)
4554#endif
4555 , old_indent);
4556 old_indent = 0;
4557
4558 replace_offset = 0;
4559 if (first_line)
4560 {
4561 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
4562 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
4563 if (second_indent >= 0)
4564 {
4565#ifdef FEAT_VREPLACE
4566 if (State & VREPLACE_FLAG)
4567 change_indent(INDENT_SET, second_indent, FALSE, NUL);
4568 else
4569#endif
4570 (void)set_indent(second_indent, SIN_CHANGED);
4571 }
4572 first_line = FALSE;
4573 }
4574
4575#ifdef FEAT_VREPLACE
4576 if (State & VREPLACE_FLAG)
4577 {
4578 /*
4579 * In VREPLACE mode we have backspaced over the text to be
4580 * moved, now we re-insert it into the new line.
4581 */
4582 ins_bytes(saved_text);
4583 vim_free(saved_text);
4584 }
4585 else
4586#endif
4587 {
4588 /*
4589 * Check if cursor is not past the NUL off the line, cindent
4590 * may have added or removed indent.
4591 */
4592 curwin->w_cursor.col += startcol;
4593 len = (colnr_T)STRLEN(ml_get_curline());
4594 if (curwin->w_cursor.col > len)
4595 curwin->w_cursor.col = len;
4596 }
4597
4598 haveto_redraw = TRUE;
4599#ifdef FEAT_CINDENT
4600 can_cindent = TRUE;
4601#endif
4602 /* moved the cursor, don't autoindent or cindent now */
4603 did_ai = FALSE;
4604#ifdef FEAT_SMARTINDENT
4605 did_si = FALSE;
4606 can_si = FALSE;
4607 can_si_back = FALSE;
4608#endif
4609 line_breakcheck();
4610 }
4611
4612 if (save_char) /* put back space after cursor */
4613 pchar_cursor(save_char);
4614
4615 if (c == NUL) /* formatting only */
4616 return;
4617 if (haveto_redraw)
4618 {
4619 update_topline();
4620 redraw_curbuf_later(VALID);
4621 }
4622 }
4623 if (c == NUL) /* only formatting was wanted */
4624 return;
4625
4626#ifdef FEAT_COMMENTS
4627 /* Check whether this character should end a comment. */
4628 if (did_ai && (int)c == end_comment_pending)
4629 {
4630 char_u *line;
4631 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4632 int middle_len, end_len;
4633 int i;
4634
4635 /*
4636 * Need to remove existing (middle) comment leader and insert end
4637 * comment leader. First, check what comment leader we can find.
4638 */
4639 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4640 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4641 {
4642 /* Skip middle-comment string */
4643 while (*p && p[-1] != ':') /* find end of middle flags */
4644 ++p;
4645 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4646 /* Don't count trailing white space for middle_len */
4647 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4648 --middle_len;
4649
4650 /* Find the end-comment string */
4651 while (*p && p[-1] != ':') /* find end of end flags */
4652 ++p;
4653 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4654
4655 /* Skip white space before the cursor */
4656 i = curwin->w_cursor.col;
4657 while (--i >= 0 && vim_iswhite(line[i]))
4658 ;
4659 i++;
4660
4661 /* Skip to before the middle leader */
4662 i -= middle_len;
4663
4664 /* Check some expected things before we go on */
4665 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4666 {
4667 /* Backspace over all the stuff we want to replace */
4668 backspace_until_column(i);
4669
4670 /*
4671 * Insert the end-comment string, except for the last
4672 * character, which will get inserted as normal later.
4673 */
4674 ins_bytes_len(lead_end, end_len - 1);
4675 }
4676 }
4677 }
4678 end_comment_pending = NUL;
4679#endif
4680
4681 did_ai = FALSE;
4682#ifdef FEAT_SMARTINDENT
4683 did_si = FALSE;
4684 can_si = FALSE;
4685 can_si_back = FALSE;
4686#endif
4687
4688 /*
4689 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4690 * This speeds up normal text input considerably.
4691 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4692 * need to re-indent at a ':', or any other character (but not what
4693 * 'paste' is set)..
4694 */
4695#ifdef USE_ON_FLY_SCROLL
4696 dont_scroll = FALSE; /* allow scrolling here */
4697#endif
4698
4699 if ( !ISSPECIAL(c)
4700#ifdef FEAT_MBYTE
4701 && (!has_mbyte || (*mb_char2len)(c) == 1)
4702#endif
4703 && vpeekc() != NUL
4704 && !(State & REPLACE_FLAG)
4705#ifdef FEAT_CINDENT
4706 && !cindent_on()
4707#endif
4708#ifdef FEAT_RIGHTLEFT
4709 && !p_ri
4710#endif
4711 )
4712 {
4713#define INPUT_BUFLEN 100
4714 char_u buf[INPUT_BUFLEN + 1];
4715 int i;
4716 colnr_T virtcol = 0;
4717
4718 buf[0] = c;
4719 i = 1;
4720 if (textwidth)
4721 virtcol = get_nolist_virtcol();
4722 /*
4723 * Stop the string when:
4724 * - no more chars available
4725 * - finding a special character (command key)
4726 * - buffer is full
4727 * - running into the 'textwidth' boundary
4728 * - need to check for abbreviation: A non-word char after a word-char
4729 */
4730 while ( (c = vpeekc()) != NUL
4731 && !ISSPECIAL(c)
4732#ifdef FEAT_MBYTE
4733 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4734#endif
4735 && i < INPUT_BUFLEN
4736 && (textwidth == 0
4737 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4738 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4739 {
4740#ifdef FEAT_RIGHTLEFT
4741 c = vgetc();
4742 if (p_hkmap && KeyTyped)
4743 c = hkmap(c); /* Hebrew mode mapping */
4744# ifdef FEAT_FKMAP
4745 if (p_fkmap && KeyTyped)
4746 c = fkmap(c); /* Farsi mode mapping */
4747# endif
4748 buf[i++] = c;
4749#else
4750 buf[i++] = vgetc();
4751#endif
4752 }
4753
4754#ifdef FEAT_DIGRAPHS
4755 do_digraph(-1); /* clear digraphs */
4756 do_digraph(buf[i-1]); /* may be the start of a digraph */
4757#endif
4758 buf[i] = NUL;
4759 ins_str(buf);
4760 if (flags & INSCHAR_CTRLV)
4761 {
4762 redo_literal(*buf);
4763 i = 1;
4764 }
4765 else
4766 i = 0;
4767 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004768 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 }
4770 else
4771 {
4772#ifdef FEAT_MBYTE
4773 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
4774 {
4775 char_u buf[MB_MAXBYTES + 1];
4776
4777 (*mb_char2bytes)(c, buf);
4778 buf[cc] = NUL;
4779 ins_char_bytes(buf, cc);
4780 AppendCharToRedobuff(c);
4781 }
4782 else
4783#endif
4784 {
4785 ins_char(c);
4786 if (flags & INSCHAR_CTRLV)
4787 redo_literal(c);
4788 else
4789 AppendCharToRedobuff(c);
4790 }
4791 }
4792}
4793
4794/*
4795 * Called after inserting or deleting text: When 'formatoptions' includes the
4796 * 'a' flag format from the current line until the end of the paragraph.
4797 * Keep the cursor at the same position relative to the text.
4798 * The caller must have saved the cursor line for undo, following ones will be
4799 * saved here.
4800 */
4801 void
4802auto_format(trailblank, prev_line)
4803 int trailblank; /* when TRUE also format with trailing blank */
4804 int prev_line; /* may start in previous line */
4805{
4806 pos_T pos;
4807 colnr_T len;
4808 char_u *old;
4809 char_u *new, *pnew;
4810 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004811 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 if (!has_format_option(FO_AUTO))
4814 return;
4815
4816 pos = curwin->w_cursor;
4817 old = ml_get_curline();
4818
4819 /* may remove added space */
4820 check_auto_format(FALSE);
4821
4822 /* Don't format in Insert mode when the cursor is on a trailing blank, the
4823 * user might insert normal text next. Also skip formatting when "1" is
4824 * in 'formatoptions' and there is a single character before the cursor.
4825 * Otherwise the line would be broken and when typing another non-white
4826 * next they are not joined back together. */
4827 wasatend = (pos.col == STRLEN(old));
4828 if (*old != NUL && !trailblank && wasatend)
4829 {
4830 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004831 cc = gchar_cursor();
4832 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
4833 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004835 cc = gchar_cursor();
4836 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 {
4838 curwin->w_cursor = pos;
4839 return;
4840 }
4841 curwin->w_cursor = pos;
4842 }
4843
4844#ifdef FEAT_COMMENTS
4845 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
4846 * comments. */
4847 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
4848 && get_leader_len(old, NULL, FALSE) == 0)
4849 return;
4850#endif
4851
4852 /*
4853 * May start formatting in a previous line, so that after "x" a word is
4854 * moved to the previous line if it fits there now. Only when this is not
4855 * the start of a paragraph.
4856 */
4857 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
4858 {
4859 --curwin->w_cursor.lnum;
4860 if (u_save_cursor() == FAIL)
4861 return;
4862 }
4863
4864 /*
4865 * Do the formatting and restore the cursor position. "saved_cursor" will
4866 * be adjusted for the text formatting.
4867 */
4868 saved_cursor = pos;
4869 format_lines((linenr_T)-1);
4870 curwin->w_cursor = saved_cursor;
4871 saved_cursor.lnum = 0;
4872
4873 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4874 {
4875 /* "cannot happen" */
4876 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4877 coladvance((colnr_T)MAXCOL);
4878 }
4879 else
4880 check_cursor_col();
4881
4882 /* Insert mode: If the cursor is now after the end of the line while it
4883 * previously wasn't, the line was broken. Because of the rule above we
4884 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
4885 * formatted. */
4886 if (!wasatend && has_format_option(FO_WHITE_PAR))
4887 {
4888 new = ml_get_curline();
4889 len = STRLEN(new);
4890 if (curwin->w_cursor.col == len)
4891 {
4892 pnew = vim_strnsave(new, len + 2);
4893 pnew[len] = ' ';
4894 pnew[len + 1] = NUL;
4895 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
4896 /* remove the space later */
4897 did_add_space = TRUE;
4898 }
4899 else
4900 /* may remove added space */
4901 check_auto_format(FALSE);
4902 }
4903
4904 check_cursor();
4905}
4906
4907/*
4908 * When an extra space was added to continue a paragraph for auto-formatting,
4909 * delete it now. The space must be under the cursor, just after the insert
4910 * position.
4911 */
4912 static void
4913check_auto_format(end_insert)
4914 int end_insert; /* TRUE when ending Insert mode */
4915{
4916 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004917 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
4919 if (did_add_space)
4920 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004921 cc = gchar_cursor();
4922 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 /* Somehow the space was removed already. */
4924 did_add_space = FALSE;
4925 else
4926 {
4927 if (!end_insert)
4928 {
4929 inc_cursor();
4930 c = gchar_cursor();
4931 dec_cursor();
4932 }
4933 if (c != NUL)
4934 {
4935 /* The space is no longer at the end of the line, delete it. */
4936 del_char(FALSE);
4937 did_add_space = FALSE;
4938 }
4939 }
4940 }
4941}
4942
4943/*
4944 * Find out textwidth to be used for formatting:
4945 * if 'textwidth' option is set, use it
4946 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
4947 * if invalid value, use 0.
4948 * Set default to window width (maximum 79) for "gq" operator.
4949 */
4950 int
4951comp_textwidth(ff)
4952 int ff; /* force formatting (for "Q" command) */
4953{
4954 int textwidth;
4955
4956 textwidth = curbuf->b_p_tw;
4957 if (textwidth == 0 && curbuf->b_p_wm)
4958 {
4959 /* The width is the window width minus 'wrapmargin' minus all the
4960 * things that add to the margin. */
4961 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
4962#ifdef FEAT_CMDWIN
4963 if (cmdwin_type != 0)
4964 textwidth -= 1;
4965#endif
4966#ifdef FEAT_FOLDING
4967 textwidth -= curwin->w_p_fdc;
4968#endif
4969#ifdef FEAT_SIGNS
4970 if (curwin->w_buffer->b_signlist != NULL
4971# ifdef FEAT_NETBEANS_INTG
4972 || usingNetbeans
4973# endif
4974 )
4975 textwidth -= 1;
4976#endif
4977 if (curwin->w_p_nu)
4978 textwidth -= 8;
4979 }
4980 if (textwidth < 0)
4981 textwidth = 0;
4982 if (ff && textwidth == 0)
4983 {
4984 textwidth = W_WIDTH(curwin) - 1;
4985 if (textwidth > 79)
4986 textwidth = 79;
4987 }
4988 return textwidth;
4989}
4990
4991/*
4992 * Put a character in the redo buffer, for when just after a CTRL-V.
4993 */
4994 static void
4995redo_literal(c)
4996 int c;
4997{
4998 char_u buf[10];
4999
5000 /* Only digits need special treatment. Translate them into a string of
5001 * three digits. */
5002 if (VIM_ISDIGIT(c))
5003 {
5004 sprintf((char *)buf, "%03d", c);
5005 AppendToRedobuff(buf);
5006 }
5007 else
5008 AppendCharToRedobuff(c);
5009}
5010
5011/*
5012 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005013 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 */
5015 static void
5016start_arrow(end_insert_pos)
5017 pos_T *end_insert_pos;
5018{
5019 if (!arrow_used) /* something has been inserted */
5020 {
5021 AppendToRedobuff(ESC_STR);
5022 stop_insert(end_insert_pos, FALSE);
5023 arrow_used = TRUE; /* this means we stopped the current insert */
5024 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005025#ifdef FEAT_SYN_HL
5026 check_spell_redraw();
5027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028}
5029
Bram Moolenaar217ad922005-03-20 22:37:15 +00005030#ifdef FEAT_SYN_HL
5031/*
5032 * If we skipped highlighting word at cursor, do it now.
5033 * It may be skipped again, thus reset spell_redraw_lnum first.
5034 */
5035 static void
5036check_spell_redraw()
5037{
5038 if (spell_redraw_lnum != 0)
5039 {
5040 linenr_T lnum = spell_redraw_lnum;
5041
5042 spell_redraw_lnum = 0;
5043 redrawWinline(lnum, FALSE);
5044 }
5045}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005046
5047/*
5048 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5049 * spelled word, if there is one.
5050 */
5051 static void
5052spell_back_to_badword()
5053{
5054 pos_T tpos = curwin->w_cursor;
5055
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005056 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005057 if (curwin->w_cursor.col != tpos.col)
5058 start_arrow(&tpos);
5059}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005060#endif
5061
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062/*
5063 * stop_arrow() is called before a change is made in insert mode.
5064 * If an arrow key has been used, start a new insertion.
5065 * Returns FAIL if undo is impossible, shouldn't insert then.
5066 */
5067 int
5068stop_arrow()
5069{
5070 if (arrow_used)
5071 {
5072 if (u_save_cursor() == OK)
5073 {
5074 arrow_used = FALSE;
5075 ins_need_undo = FALSE;
5076 }
5077 Insstart = curwin->w_cursor; /* new insertion starts here */
5078 Insstart_textlen = linetabsize(ml_get_curline());
5079 ai_col = 0;
5080#ifdef FEAT_VREPLACE
5081 if (State & VREPLACE_FLAG)
5082 {
5083 orig_line_count = curbuf->b_ml.ml_line_count;
5084 vr_lines_changed = 1;
5085 }
5086#endif
5087 ResetRedobuff();
5088 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005089 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 }
5091 else if (ins_need_undo)
5092 {
5093 if (u_save_cursor() == OK)
5094 ins_need_undo = FALSE;
5095 }
5096
5097#ifdef FEAT_FOLDING
5098 /* Always open fold at the cursor line when inserting something. */
5099 foldOpenCursor();
5100#endif
5101
5102 return (arrow_used || ins_need_undo ? FAIL : OK);
5103}
5104
5105/*
5106 * do a few things to stop inserting
5107 */
5108 static void
5109stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005110 pos_T *end_insert_pos; /* where insert ended */
5111 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005113 int cc;
5114 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115
5116 stop_redo_ins();
5117 replace_flush(); /* abandon replace stack */
5118
5119 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005120 * Save the inserted text for later redo with ^@ and CTRL-A.
5121 * Don't do it when "restart_edit" was set and nothing was inserted,
5122 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005124 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005125 if (did_restart_edit == 0 || (ptr != NULL
5126 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005127 {
5128 vim_free(last_insert);
5129 last_insert = ptr;
5130 last_insert_skip = new_insert_skip;
5131 }
5132 else
5133 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134
5135 if (!arrow_used)
5136 {
5137 /* Auto-format now. It may seem strange to do this when stopping an
5138 * insertion (or moving the cursor), but it's required when appending
5139 * a line and having it end in a space. But only do it when something
5140 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005141 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005143 pos_T tpos = curwin->w_cursor;
5144
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 /* When the cursor is at the end of the line after a space the
5146 * formatting will move it to the following word. Avoid that by
5147 * moving the cursor onto the space. */
5148 cc = 'x';
5149 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5150 {
5151 dec_cursor();
5152 cc = gchar_cursor();
5153 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005154 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
5156
5157 auto_format(TRUE, FALSE);
5158
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005159 if (vim_iswhite(cc))
5160 {
5161 if (gchar_cursor() != NUL)
5162 inc_cursor();
5163#ifdef FEAT_VIRTUALEDIT
5164 /* If the cursor is still at the same character, also keep
5165 * the "coladd". */
5166 if (gchar_cursor() == NUL
5167 && curwin->w_cursor.lnum == tpos.lnum
5168 && curwin->w_cursor.col == tpos.col)
5169 curwin->w_cursor.coladd = tpos.coladd;
5170#endif
5171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173
5174 /* If a space was inserted for auto-formatting, remove it now. */
5175 check_auto_format(TRUE);
5176
5177 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005178 * of the line, and put the cursor back.
5179 * Do this when ESC was used or moving the cursor up/down. */
5180 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5181 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005183 pos_T tpos = curwin->w_cursor;
5184
5185 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5187 --curwin->w_cursor.col;
5188 while (cc = gchar_cursor(), vim_iswhite(cc))
5189 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005190 if (curwin->w_cursor.lnum != tpos.lnum)
5191 curwin->w_cursor = tpos;
5192 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5194
5195#ifdef FEAT_VISUAL
5196 /* <C-S-Right> may have started Visual mode, adjust the position for
5197 * deleted characters. */
5198 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5199 {
5200 cc = STRLEN(ml_get_curline());
5201 if (VIsual.col > (colnr_T)cc)
5202 {
5203 VIsual.col = cc;
5204# ifdef FEAT_VIRTUALEDIT
5205 VIsual.coladd = 0;
5206# endif
5207 }
5208 }
5209#endif
5210 }
5211 }
5212 did_ai = FALSE;
5213#ifdef FEAT_SMARTINDENT
5214 did_si = FALSE;
5215 can_si = FALSE;
5216 can_si_back = FALSE;
5217#endif
5218
5219 /* set '[ and '] to the inserted text */
5220 curbuf->b_op_start = Insstart;
5221 curbuf->b_op_end = *end_insert_pos;
5222}
5223
5224/*
5225 * Set the last inserted text to a single character.
5226 * Used for the replace command.
5227 */
5228 void
5229set_last_insert(c)
5230 int c;
5231{
5232 char_u *s;
5233
5234 vim_free(last_insert);
5235#ifdef FEAT_MBYTE
5236 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5237#else
5238 last_insert = alloc(6);
5239#endif
5240 if (last_insert != NULL)
5241 {
5242 s = last_insert;
5243 /* Use the CTRL-V only when entering a special char */
5244 if (c < ' ' || c == DEL)
5245 *s++ = Ctrl_V;
5246 s = add_char2buf(c, s);
5247 *s++ = ESC;
5248 *s++ = NUL;
5249 last_insert_skip = 0;
5250 }
5251}
5252
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005253#if defined(EXITFREE) || defined(PROTO)
5254 void
5255free_last_insert()
5256{
5257 vim_free(last_insert);
5258 last_insert = NULL;
5259}
5260#endif
5261
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262/*
5263 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5264 * and CSI. Handle multi-byte characters.
5265 * Returns a pointer to after the added bytes.
5266 */
5267 char_u *
5268add_char2buf(c, s)
5269 int c;
5270 char_u *s;
5271{
5272#ifdef FEAT_MBYTE
5273 char_u temp[MB_MAXBYTES];
5274 int i;
5275 int len;
5276
5277 len = (*mb_char2bytes)(c, temp);
5278 for (i = 0; i < len; ++i)
5279 {
5280 c = temp[i];
5281#endif
5282 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5283 if (c == K_SPECIAL)
5284 {
5285 *s++ = K_SPECIAL;
5286 *s++ = KS_SPECIAL;
5287 *s++ = KE_FILLER;
5288 }
5289#ifdef FEAT_GUI
5290 else if (c == CSI)
5291 {
5292 *s++ = CSI;
5293 *s++ = KS_EXTRA;
5294 *s++ = (int)KE_CSI;
5295 }
5296#endif
5297 else
5298 *s++ = c;
5299#ifdef FEAT_MBYTE
5300 }
5301#endif
5302 return s;
5303}
5304
5305/*
5306 * move cursor to start of line
5307 * if flags & BL_WHITE move to first non-white
5308 * if flags & BL_SOL move to first non-white if startofline is set,
5309 * otherwise keep "curswant" column
5310 * if flags & BL_FIX don't leave the cursor on a NUL.
5311 */
5312 void
5313beginline(flags)
5314 int flags;
5315{
5316 if ((flags & BL_SOL) && !p_sol)
5317 coladvance(curwin->w_curswant);
5318 else
5319 {
5320 curwin->w_cursor.col = 0;
5321#ifdef FEAT_VIRTUALEDIT
5322 curwin->w_cursor.coladd = 0;
5323#endif
5324
5325 if (flags & (BL_WHITE | BL_SOL))
5326 {
5327 char_u *ptr;
5328
5329 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5330 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5331 ++curwin->w_cursor.col;
5332 }
5333 curwin->w_set_curswant = TRUE;
5334 }
5335}
5336
5337/*
5338 * oneright oneleft cursor_down cursor_up
5339 *
5340 * Move one char {right,left,down,up}.
5341 * Doesn't move onto the NUL past the end of the line.
5342 * Return OK when successful, FAIL when we hit a line of file boundary.
5343 */
5344
5345 int
5346oneright()
5347{
5348 char_u *ptr;
5349#ifdef FEAT_MBYTE
5350 int l;
5351#endif
5352
5353#ifdef FEAT_VIRTUALEDIT
5354 if (virtual_active())
5355 {
5356 pos_T prevpos = curwin->w_cursor;
5357
5358 /* Adjust for multi-wide char (excluding TAB) */
5359 ptr = ml_get_cursor();
5360 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5361#ifdef FEAT_MBYTE
5362 (*mb_ptr2char)(ptr)
5363#else
5364 *ptr
5365#endif
5366 ))
5367 ? ptr2cells(ptr) : 1));
5368 curwin->w_set_curswant = TRUE;
5369 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5370 return (prevpos.col != curwin->w_cursor.col
5371 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5372 }
5373#endif
5374
5375 ptr = ml_get_cursor();
5376#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005377 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
5379 /* The character under the cursor is a multi-byte character, move
5380 * several bytes right, but don't end up on the NUL. */
5381 if (ptr[l] == NUL)
5382 return FAIL;
5383 curwin->w_cursor.col += l;
5384 }
5385 else
5386#endif
5387 {
5388 if (*ptr++ == NUL || *ptr == NUL)
5389 return FAIL;
5390 ++curwin->w_cursor.col;
5391 }
5392
5393 curwin->w_set_curswant = TRUE;
5394 return OK;
5395}
5396
5397 int
5398oneleft()
5399{
5400#ifdef FEAT_VIRTUALEDIT
5401 if (virtual_active())
5402 {
5403 int width;
5404 int v = getviscol();
5405
5406 if (v == 0)
5407 return FAIL;
5408
5409# ifdef FEAT_LINEBREAK
5410 /* We might get stuck on 'showbreak', skip over it. */
5411 width = 1;
5412 for (;;)
5413 {
5414 coladvance(v - width);
5415 /* getviscol() is slow, skip it when 'showbreak' is empty and
5416 * there are no multi-byte characters */
5417 if ((*p_sbr == NUL
5418# ifdef FEAT_MBYTE
5419 && !has_mbyte
5420# endif
5421 ) || getviscol() < v)
5422 break;
5423 ++width;
5424 }
5425# else
5426 coladvance(v - 1);
5427# endif
5428
5429 if (curwin->w_cursor.coladd == 1)
5430 {
5431 char_u *ptr;
5432
5433 /* Adjust for multi-wide char (not a TAB) */
5434 ptr = ml_get_cursor();
5435 if (*ptr != TAB && vim_isprintc(
5436# ifdef FEAT_MBYTE
5437 (*mb_ptr2char)(ptr)
5438# else
5439 *ptr
5440# endif
5441 ) && ptr2cells(ptr) > 1)
5442 curwin->w_cursor.coladd = 0;
5443 }
5444
5445 curwin->w_set_curswant = TRUE;
5446 return OK;
5447 }
5448#endif
5449
5450 if (curwin->w_cursor.col == 0)
5451 return FAIL;
5452
5453 curwin->w_set_curswant = TRUE;
5454 --curwin->w_cursor.col;
5455
5456#ifdef FEAT_MBYTE
5457 /* if the character on the left of the current cursor is a multi-byte
5458 * character, move to its first byte */
5459 if (has_mbyte)
5460 mb_adjust_cursor();
5461#endif
5462 return OK;
5463}
5464
5465 int
5466cursor_up(n, upd_topline)
5467 long n;
5468 int upd_topline; /* When TRUE: update topline */
5469{
5470 linenr_T lnum;
5471
5472 if (n > 0)
5473 {
5474 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005475 /* This fails if the cursor is already in the first line or the count
5476 * is larger than the line number and '-' is in 'cpoptions' */
5477 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 return FAIL;
5479 if (n >= lnum)
5480 lnum = 1;
5481 else
5482#ifdef FEAT_FOLDING
5483 if (hasAnyFolding(curwin))
5484 {
5485 /*
5486 * Count each sequence of folded lines as one logical line.
5487 */
5488 /* go to the the start of the current fold */
5489 (void)hasFolding(lnum, &lnum, NULL);
5490
5491 while (n--)
5492 {
5493 /* move up one line */
5494 --lnum;
5495 if (lnum <= 1)
5496 break;
5497 /* If we entered a fold, move to the beginning, unless in
5498 * Insert mode or when 'foldopen' contains "all": it will open
5499 * in a moment. */
5500 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
5501 (void)hasFolding(lnum, &lnum, NULL);
5502 }
5503 if (lnum < 1)
5504 lnum = 1;
5505 }
5506 else
5507#endif
5508 lnum -= n;
5509 curwin->w_cursor.lnum = lnum;
5510 }
5511
5512 /* try to advance to the column we want to be at */
5513 coladvance(curwin->w_curswant);
5514
5515 if (upd_topline)
5516 update_topline(); /* make sure curwin->w_topline is valid */
5517
5518 return OK;
5519}
5520
5521/*
5522 * Cursor down a number of logical lines.
5523 */
5524 int
5525cursor_down(n, upd_topline)
5526 long n;
5527 int upd_topline; /* When TRUE: update topline */
5528{
5529 linenr_T lnum;
5530
5531 if (n > 0)
5532 {
5533 lnum = curwin->w_cursor.lnum;
5534#ifdef FEAT_FOLDING
5535 /* Move to last line of fold, will fail if it's the end-of-file. */
5536 (void)hasFolding(lnum, NULL, &lnum);
5537#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00005538 /* This fails if the cursor is already in the last line or would move
5539 * beyound the last line and '-' is in 'cpoptions' */
5540 if (lnum >= curbuf->b_ml.ml_line_count
5541 || (lnum + n > curbuf->b_ml.ml_line_count
5542 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 return FAIL;
5544 if (lnum + n >= curbuf->b_ml.ml_line_count)
5545 lnum = curbuf->b_ml.ml_line_count;
5546 else
5547#ifdef FEAT_FOLDING
5548 if (hasAnyFolding(curwin))
5549 {
5550 linenr_T last;
5551
5552 /* count each sequence of folded lines as one logical line */
5553 while (n--)
5554 {
5555 if (hasFolding(lnum, NULL, &last))
5556 lnum = last + 1;
5557 else
5558 ++lnum;
5559 if (lnum >= curbuf->b_ml.ml_line_count)
5560 break;
5561 }
5562 if (lnum > curbuf->b_ml.ml_line_count)
5563 lnum = curbuf->b_ml.ml_line_count;
5564 }
5565 else
5566#endif
5567 lnum += n;
5568 curwin->w_cursor.lnum = lnum;
5569 }
5570
5571 /* try to advance to the column we want to be at */
5572 coladvance(curwin->w_curswant);
5573
5574 if (upd_topline)
5575 update_topline(); /* make sure curwin->w_topline is valid */
5576
5577 return OK;
5578}
5579
5580/*
5581 * Stuff the last inserted text in the read buffer.
5582 * Last_insert actually is a copy of the redo buffer, so we
5583 * first have to remove the command.
5584 */
5585 int
5586stuff_inserted(c, count, no_esc)
5587 int c; /* Command character to be inserted */
5588 long count; /* Repeat this many times */
5589 int no_esc; /* Don't add an ESC at the end */
5590{
5591 char_u *esc_ptr;
5592 char_u *ptr;
5593 char_u *last_ptr;
5594 char_u last = NUL;
5595
5596 ptr = get_last_insert();
5597 if (ptr == NULL)
5598 {
5599 EMSG(_(e_noinstext));
5600 return FAIL;
5601 }
5602
5603 /* may want to stuff the command character, to start Insert mode */
5604 if (c != NUL)
5605 stuffcharReadbuff(c);
5606 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
5607 *esc_ptr = NUL; /* remove the ESC */
5608
5609 /* when the last char is either "0" or "^" it will be quoted if no ESC
5610 * comes after it OR if it will inserted more than once and "ptr"
5611 * starts with ^D. -- Acevedo
5612 */
5613 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
5614 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
5615 && (no_esc || (*ptr == Ctrl_D && count > 1)))
5616 {
5617 last = *last_ptr;
5618 *last_ptr = NUL;
5619 }
5620
5621 do
5622 {
5623 stuffReadbuff(ptr);
5624 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
5625 if (last)
5626 stuffReadbuff((char_u *)(last == '0'
5627 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
5628 : IF_EB("\026^", CTRL_V_STR "^")));
5629 }
5630 while (--count > 0);
5631
5632 if (last)
5633 *last_ptr = last;
5634
5635 if (esc_ptr != NULL)
5636 *esc_ptr = ESC; /* put the ESC back */
5637
5638 /* may want to stuff a trailing ESC, to get out of Insert mode */
5639 if (!no_esc)
5640 stuffcharReadbuff(ESC);
5641
5642 return OK;
5643}
5644
5645 char_u *
5646get_last_insert()
5647{
5648 if (last_insert == NULL)
5649 return NULL;
5650 return last_insert + last_insert_skip;
5651}
5652
5653/*
5654 * Get last inserted string, and remove trailing <Esc>.
5655 * Returns pointer to allocated memory (must be freed) or NULL.
5656 */
5657 char_u *
5658get_last_insert_save()
5659{
5660 char_u *s;
5661 int len;
5662
5663 if (last_insert == NULL)
5664 return NULL;
5665 s = vim_strsave(last_insert + last_insert_skip);
5666 if (s != NULL)
5667 {
5668 len = (int)STRLEN(s);
5669 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
5670 s[len - 1] = NUL;
5671 }
5672 return s;
5673}
5674
5675/*
5676 * Check the word in front of the cursor for an abbreviation.
5677 * Called when the non-id character "c" has been entered.
5678 * When an abbreviation is recognized it is removed from the text and
5679 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
5680 */
5681 static int
5682echeck_abbr(c)
5683 int c;
5684{
5685 /* Don't check for abbreviation in paste mode, when disabled and just
5686 * after moving around with cursor keys. */
5687 if (p_paste || no_abbr || arrow_used)
5688 return FALSE;
5689
5690 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
5691 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
5692}
5693
5694/*
5695 * replace-stack functions
5696 *
5697 * When replacing characters, the replaced characters are remembered for each
5698 * new character. This is used to re-insert the old text when backspacing.
5699 *
5700 * There is a NUL headed list of characters for each character that is
5701 * currently in the file after the insertion point. When BS is used, one NUL
5702 * headed list is put back for the deleted character.
5703 *
5704 * For a newline, there are two NUL headed lists. One contains the characters
5705 * that the NL replaced. The extra one stores the characters after the cursor
5706 * that were deleted (always white space).
5707 *
5708 * Replace_offset is normally 0, in which case replace_push will add a new
5709 * character at the end of the stack. If replace_offset is not 0, that many
5710 * characters will be left on the stack above the newly inserted character.
5711 */
5712
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00005713static char_u *replace_stack = NULL;
5714static long replace_stack_nr = 0; /* next entry in replace stack */
5715static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
5717 void
5718replace_push(c)
5719 int c; /* character that is replaced (NUL is none) */
5720{
5721 char_u *p;
5722
5723 if (replace_stack_nr < replace_offset) /* nothing to do */
5724 return;
5725 if (replace_stack_len <= replace_stack_nr)
5726 {
5727 replace_stack_len += 50;
5728 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
5729 if (p == NULL) /* out of memory */
5730 {
5731 replace_stack_len -= 50;
5732 return;
5733 }
5734 if (replace_stack != NULL)
5735 {
5736 mch_memmove(p, replace_stack,
5737 (size_t)(replace_stack_nr * sizeof(char_u)));
5738 vim_free(replace_stack);
5739 }
5740 replace_stack = p;
5741 }
5742 p = replace_stack + replace_stack_nr - replace_offset;
5743 if (replace_offset)
5744 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
5745 *p = c;
5746 ++replace_stack_nr;
5747}
5748
5749/*
5750 * call replace_push(c) with replace_offset set to the first NUL.
5751 */
5752 static void
5753replace_push_off(c)
5754 int c;
5755{
5756 char_u *p;
5757
5758 p = replace_stack + replace_stack_nr;
5759 for (replace_offset = 1; replace_offset < replace_stack_nr;
5760 ++replace_offset)
5761 if (*--p == NUL)
5762 break;
5763 replace_push(c);
5764 replace_offset = 0;
5765}
5766
5767/*
5768 * Pop one item from the replace stack.
5769 * return -1 if stack empty
5770 * return replaced character or NUL otherwise
5771 */
5772 static int
5773replace_pop()
5774{
5775 if (replace_stack_nr == 0)
5776 return -1;
5777 return (int)replace_stack[--replace_stack_nr];
5778}
5779
5780/*
5781 * Join the top two items on the replace stack. This removes to "off"'th NUL
5782 * encountered.
5783 */
5784 static void
5785replace_join(off)
5786 int off; /* offset for which NUL to remove */
5787{
5788 int i;
5789
5790 for (i = replace_stack_nr; --i >= 0; )
5791 if (replace_stack[i] == NUL && off-- <= 0)
5792 {
5793 --replace_stack_nr;
5794 mch_memmove(replace_stack + i, replace_stack + i + 1,
5795 (size_t)(replace_stack_nr - i));
5796 return;
5797 }
5798}
5799
5800/*
5801 * Pop bytes from the replace stack until a NUL is found, and insert them
5802 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
5803 */
5804 static void
5805replace_pop_ins()
5806{
5807 int cc;
5808 int oldState = State;
5809
5810 State = NORMAL; /* don't want REPLACE here */
5811 while ((cc = replace_pop()) > 0)
5812 {
5813#ifdef FEAT_MBYTE
5814 mb_replace_pop_ins(cc);
5815#else
5816 ins_char(cc);
5817#endif
5818 dec_cursor();
5819 }
5820 State = oldState;
5821}
5822
5823#ifdef FEAT_MBYTE
5824/*
5825 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
5826 * indicates a multi-byte char, pop the other bytes too.
5827 */
5828 static void
5829mb_replace_pop_ins(cc)
5830 int cc;
5831{
5832 int n;
5833 char_u buf[MB_MAXBYTES];
5834 int i;
5835 int c;
5836
5837 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
5838 {
5839 buf[0] = cc;
5840 for (i = 1; i < n; ++i)
5841 buf[i] = replace_pop();
5842 ins_bytes_len(buf, n);
5843 }
5844 else
5845 ins_char(cc);
5846
5847 if (enc_utf8)
5848 /* Handle composing chars. */
5849 for (;;)
5850 {
5851 c = replace_pop();
5852 if (c == -1) /* stack empty */
5853 break;
5854 if ((n = MB_BYTE2LEN(c)) == 1)
5855 {
5856 /* Not a multi-byte char, put it back. */
5857 replace_push(c);
5858 break;
5859 }
5860 else
5861 {
5862 buf[0] = c;
5863 for (i = 1; i < n; ++i)
5864 buf[i] = replace_pop();
5865 if (utf_iscomposing(utf_ptr2char(buf)))
5866 ins_bytes_len(buf, n);
5867 else
5868 {
5869 /* Not a composing char, put it back. */
5870 for (i = n - 1; i >= 0; --i)
5871 replace_push(buf[i]);
5872 break;
5873 }
5874 }
5875 }
5876}
5877#endif
5878
5879/*
5880 * make the replace stack empty
5881 * (called when exiting replace mode)
5882 */
5883 static void
5884replace_flush()
5885{
5886 vim_free(replace_stack);
5887 replace_stack = NULL;
5888 replace_stack_len = 0;
5889 replace_stack_nr = 0;
5890}
5891
5892/*
5893 * Handle doing a BS for one character.
5894 * cc < 0: replace stack empty, just move cursor
5895 * cc == 0: character was inserted, delete it
5896 * cc > 0: character was replaced, put cc (first byte of original char) back
5897 * and check for more characters to be put back
5898 */
5899 static void
5900replace_do_bs()
5901{
5902 int cc;
5903#ifdef FEAT_VREPLACE
5904 int orig_len = 0;
5905 int ins_len;
5906 int orig_vcols = 0;
5907 colnr_T start_vcol;
5908 char_u *p;
5909 int i;
5910 int vcol;
5911#endif
5912
5913 cc = replace_pop();
5914 if (cc > 0)
5915 {
5916#ifdef FEAT_VREPLACE
5917 if (State & VREPLACE_FLAG)
5918 {
5919 /* Get the number of screen cells used by the character we are
5920 * going to delete. */
5921 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
5922 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
5923 }
5924#endif
5925#ifdef FEAT_MBYTE
5926 if (has_mbyte)
5927 {
5928 del_char(FALSE);
5929# ifdef FEAT_VREPLACE
5930 if (State & VREPLACE_FLAG)
5931 orig_len = STRLEN(ml_get_cursor());
5932# endif
5933 replace_push(cc);
5934 }
5935 else
5936#endif
5937 {
5938 pchar_cursor(cc);
5939#ifdef FEAT_VREPLACE
5940 if (State & VREPLACE_FLAG)
5941 orig_len = STRLEN(ml_get_cursor()) - 1;
5942#endif
5943 }
5944 replace_pop_ins();
5945
5946#ifdef FEAT_VREPLACE
5947 if (State & VREPLACE_FLAG)
5948 {
5949 /* Get the number of screen cells used by the inserted characters */
5950 p = ml_get_cursor();
5951 ins_len = STRLEN(p) - orig_len;
5952 vcol = start_vcol;
5953 for (i = 0; i < ins_len; ++i)
5954 {
5955 vcol += chartabsize(p + i, vcol);
5956#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005957 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958#endif
5959 }
5960 vcol -= start_vcol;
5961
5962 /* Delete spaces that were inserted after the cursor to keep the
5963 * text aligned. */
5964 curwin->w_cursor.col += ins_len;
5965 while (vcol > orig_vcols && gchar_cursor() == ' ')
5966 {
5967 del_char(FALSE);
5968 ++orig_vcols;
5969 }
5970 curwin->w_cursor.col -= ins_len;
5971 }
5972#endif
5973
5974 /* mark the buffer as changed and prepare for displaying */
5975 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
5976 }
5977 else if (cc == 0)
5978 (void)del_char(FALSE);
5979}
5980
5981#ifdef FEAT_CINDENT
5982/*
5983 * Return TRUE if C-indenting is on.
5984 */
5985 static int
5986cindent_on()
5987{
5988 return (!p_paste && (curbuf->b_p_cin
5989# ifdef FEAT_EVAL
5990 || *curbuf->b_p_inde != NUL
5991# endif
5992 ));
5993}
5994#endif
5995
5996#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
5997/*
5998 * Re-indent the current line, based on the current contents of it and the
5999 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6000 * confused what all the part that handles Control-T is doing that I'm not.
6001 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6002 */
6003
6004 void
6005fixthisline(get_the_indent)
6006 int (*get_the_indent) __ARGS((void));
6007{
6008 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6009 if (linewhite(curwin->w_cursor.lnum))
6010 did_ai = TRUE; /* delete the indent if the line stays empty */
6011}
6012
6013 void
6014fix_indent()
6015{
6016 if (p_paste)
6017 return;
6018# ifdef FEAT_LISP
6019 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6020 fixthisline(get_lisp_indent);
6021# endif
6022# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6023 else
6024# endif
6025# ifdef FEAT_CINDENT
6026 if (cindent_on())
6027 do_c_expr_indent();
6028# endif
6029}
6030
6031#endif
6032
6033#ifdef FEAT_CINDENT
6034/*
6035 * return TRUE if 'cinkeys' contains the key "keytyped",
6036 * when == '*': Only if key is preceded with '*' (indent before insert)
6037 * when == '!': Only if key is prededed with '!' (don't insert)
6038 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6039 *
6040 * "keytyped" can have a few special values:
6041 * KEY_OPEN_FORW
6042 * KEY_OPEN_BACK
6043 * KEY_COMPLETE just finished completion.
6044 *
6045 * If line_is_empty is TRUE accept keys with '0' before them.
6046 */
6047 int
6048in_cinkeys(keytyped, when, line_is_empty)
6049 int keytyped;
6050 int when;
6051 int line_is_empty;
6052{
6053 char_u *look;
6054 int try_match;
6055 int try_match_word;
6056 char_u *p;
6057 char_u *line;
6058 int icase;
6059 int i;
6060
6061#ifdef FEAT_EVAL
6062 if (*curbuf->b_p_inde != NUL)
6063 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6064 else
6065#endif
6066 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6067 while (*look)
6068 {
6069 /*
6070 * Find out if we want to try a match with this key, depending on
6071 * 'when' and a '*' or '!' before the key.
6072 */
6073 switch (when)
6074 {
6075 case '*': try_match = (*look == '*'); break;
6076 case '!': try_match = (*look == '!'); break;
6077 default: try_match = (*look != '*'); break;
6078 }
6079 if (*look == '*' || *look == '!')
6080 ++look;
6081
6082 /*
6083 * If there is a '0', only accept a match if the line is empty.
6084 * But may still match when typing last char of a word.
6085 */
6086 if (*look == '0')
6087 {
6088 try_match_word = try_match;
6089 if (!line_is_empty)
6090 try_match = FALSE;
6091 ++look;
6092 }
6093 else
6094 try_match_word = FALSE;
6095
6096 /*
6097 * does it look like a control character?
6098 */
6099 if (*look == '^'
6100#ifdef EBCDIC
6101 && (Ctrl_chr(look[1]) != 0)
6102#else
6103 && look[1] >= '?' && look[1] <= '_'
6104#endif
6105 )
6106 {
6107 if (try_match && keytyped == Ctrl_chr(look[1]))
6108 return TRUE;
6109 look += 2;
6110 }
6111 /*
6112 * 'o' means "o" command, open forward.
6113 * 'O' means "O" command, open backward.
6114 */
6115 else if (*look == 'o')
6116 {
6117 if (try_match && keytyped == KEY_OPEN_FORW)
6118 return TRUE;
6119 ++look;
6120 }
6121 else if (*look == 'O')
6122 {
6123 if (try_match && keytyped == KEY_OPEN_BACK)
6124 return TRUE;
6125 ++look;
6126 }
6127
6128 /*
6129 * 'e' means to check for "else" at start of line and just before the
6130 * cursor.
6131 */
6132 else if (*look == 'e')
6133 {
6134 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6135 {
6136 p = ml_get_curline();
6137 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6138 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6139 return TRUE;
6140 }
6141 ++look;
6142 }
6143
6144 /*
6145 * ':' only causes an indent if it is at the end of a label or case
6146 * statement, or when it was before typing the ':' (to fix
6147 * class::method for C++).
6148 */
6149 else if (*look == ':')
6150 {
6151 if (try_match && keytyped == ':')
6152 {
6153 p = ml_get_curline();
6154 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6155 return TRUE;
6156 if (curwin->w_cursor.col > 2
6157 && p[curwin->w_cursor.col - 1] == ':'
6158 && p[curwin->w_cursor.col - 2] == ':')
6159 {
6160 p[curwin->w_cursor.col - 1] = ' ';
6161 i = (cin_iscase(p) || cin_isscopedecl(p)
6162 || cin_islabel(30));
6163 p = ml_get_curline();
6164 p[curwin->w_cursor.col - 1] = ':';
6165 if (i)
6166 return TRUE;
6167 }
6168 }
6169 ++look;
6170 }
6171
6172
6173 /*
6174 * Is it a key in <>, maybe?
6175 */
6176 else if (*look == '<')
6177 {
6178 if (try_match)
6179 {
6180 /*
6181 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6182 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6183 * >, *, : and ! keys if they really really want to.
6184 */
6185 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6186 && keytyped == look[1])
6187 return TRUE;
6188
6189 if (keytyped == get_special_key_code(look + 1))
6190 return TRUE;
6191 }
6192 while (*look && *look != '>')
6193 look++;
6194 while (*look == '>')
6195 look++;
6196 }
6197
6198 /*
6199 * Is it a word: "=word"?
6200 */
6201 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6202 {
6203 ++look;
6204 if (*look == '~')
6205 {
6206 icase = TRUE;
6207 ++look;
6208 }
6209 else
6210 icase = FALSE;
6211 p = vim_strchr(look, ',');
6212 if (p == NULL)
6213 p = look + STRLEN(look);
6214 if ((try_match || try_match_word)
6215 && curwin->w_cursor.col >= (colnr_T)(p - look))
6216 {
6217 int match = FALSE;
6218
6219#ifdef FEAT_INS_EXPAND
6220 if (keytyped == KEY_COMPLETE)
6221 {
6222 char_u *s;
6223
6224 /* Just completed a word, check if it starts with "look".
6225 * search back for the start of a word. */
6226 line = ml_get_curline();
6227# ifdef FEAT_MBYTE
6228 if (has_mbyte)
6229 {
6230 char_u *n;
6231
6232 for (s = line + curwin->w_cursor.col; s > line; s = n)
6233 {
6234 n = mb_prevptr(line, s);
6235 if (!vim_iswordp(n))
6236 break;
6237 }
6238 }
6239 else
6240# endif
6241 for (s = line + curwin->w_cursor.col; s > line; --s)
6242 if (!vim_iswordc(s[-1]))
6243 break;
6244 if (s + (p - look) <= line + curwin->w_cursor.col
6245 && (icase
6246 ? MB_STRNICMP(s, look, p - look)
6247 : STRNCMP(s, look, p - look)) == 0)
6248 match = TRUE;
6249 }
6250 else
6251#endif
6252 /* TODO: multi-byte */
6253 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6254 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6255 {
6256 line = ml_get_cursor();
6257 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6258 || !vim_iswordc(line[-(p - look) - 1]))
6259 && (icase
6260 ? MB_STRNICMP(line - (p - look), look, p - look)
6261 : STRNCMP(line - (p - look), look, p - look))
6262 == 0)
6263 match = TRUE;
6264 }
6265 if (match && try_match_word && !try_match)
6266 {
6267 /* "0=word": Check if there are only blanks before the
6268 * word. */
6269 line = ml_get_curline();
6270 if ((int)(skipwhite(line) - line) !=
6271 (int)(curwin->w_cursor.col - (p - look)))
6272 match = FALSE;
6273 }
6274 if (match)
6275 return TRUE;
6276 }
6277 look = p;
6278 }
6279
6280 /*
6281 * ok, it's a boring generic character.
6282 */
6283 else
6284 {
6285 if (try_match && *look == keytyped)
6286 return TRUE;
6287 ++look;
6288 }
6289
6290 /*
6291 * Skip over ", ".
6292 */
6293 look = skip_to_option_part(look);
6294 }
6295 return FALSE;
6296}
6297#endif /* FEAT_CINDENT */
6298
6299#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6300/*
6301 * Map Hebrew keyboard when in hkmap mode.
6302 */
6303 int
6304hkmap(c)
6305 int c;
6306{
6307 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6308 {
6309 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6310 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6311 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6312 static char_u map[26] =
6313 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6314 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6315 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6316 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6317 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6318 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6319 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6320 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6321 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6322
6323 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6324 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6325 /* '-1'='sofit' */
6326 else if (c == 'x')
6327 return 'X';
6328 else if (c == 'q')
6329 return '\''; /* {geresh}={'} */
6330 else if (c == 246)
6331 return ' '; /* \"o --> ' ' for a german keyboard */
6332 else if (c == 228)
6333 return ' '; /* \"a --> ' ' -- / -- */
6334 else if (c == 252)
6335 return ' '; /* \"u --> ' ' -- / -- */
6336#ifdef EBCDIC
6337 else if (islower(c))
6338#else
6339 /* NOTE: islower() does not do the right thing for us on Linux so we
6340 * do this the same was as 5.7 and previous, so it works correctly on
6341 * all systems. Specifically, the e.g. Delete and Arrow keys are
6342 * munged and won't work if e.g. searching for Hebrew text.
6343 */
6344 else if (c >= 'a' && c <= 'z')
6345#endif
6346 return (int)(map[CharOrdLow(c)] + p_aleph);
6347 else
6348 return c;
6349 }
6350 else
6351 {
6352 switch (c)
6353 {
6354 case '`': return ';';
6355 case '/': return '.';
6356 case '\'': return ',';
6357 case 'q': return '/';
6358 case 'w': return '\'';
6359
6360 /* Hebrew letters - set offset from 'a' */
6361 case ',': c = '{'; break;
6362 case '.': c = 'v'; break;
6363 case ';': c = 't'; break;
6364 default: {
6365 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6366
6367#ifdef EBCDIC
6368 /* see note about islower() above */
6369 if (!islower(c))
6370#else
6371 if (c < 'a' || c > 'z')
6372#endif
6373 return c;
6374 c = str[CharOrdLow(c)];
6375 break;
6376 }
6377 }
6378
6379 return (int)(CharOrdLow(c) + p_aleph);
6380 }
6381}
6382#endif
6383
6384 static void
6385ins_reg()
6386{
6387 int need_redraw = FALSE;
6388 int regname;
6389 int literally = 0;
6390
6391 /*
6392 * If we are going to wait for a character, show a '"'.
6393 */
6394 pc_status = PC_STATUS_UNSET;
6395 if (redrawing() && !char_avail())
6396 {
6397 /* may need to redraw when no more chars available now */
6398 ins_redraw();
6399
6400 edit_putchar('"', TRUE);
6401#ifdef FEAT_CMDL_INFO
6402 add_to_showcmd_c(Ctrl_R);
6403#endif
6404 }
6405
6406#ifdef USE_ON_FLY_SCROLL
6407 dont_scroll = TRUE; /* disallow scrolling here */
6408#endif
6409
6410 /*
6411 * Don't map the register name. This also prevents the mode message to be
6412 * deleted when ESC is hit.
6413 */
6414 ++no_mapping;
6415 regname = safe_vgetc();
6416#ifdef FEAT_LANGMAP
6417 LANGMAP_ADJUST(regname, TRUE);
6418#endif
6419 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6420 {
6421 /* Get a third key for literal register insertion */
6422 literally = regname;
6423#ifdef FEAT_CMDL_INFO
6424 add_to_showcmd_c(literally);
6425#endif
6426 regname = safe_vgetc();
6427#ifdef FEAT_LANGMAP
6428 LANGMAP_ADJUST(regname, TRUE);
6429#endif
6430 }
6431 --no_mapping;
6432
6433#ifdef FEAT_EVAL
6434 /*
6435 * Don't call u_sync() while getting the expression,
6436 * evaluating it or giving an error message for it!
6437 */
6438 ++no_u_sync;
6439 if (regname == '=')
6440 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006441# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006445# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 /* Restore the Input Method. */
6447 if (im_on)
6448 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006451 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6452 {
6453 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 else
6457 {
6458#endif
6459 if (literally == Ctrl_O || literally == Ctrl_P)
6460 {
6461 /* Append the command to the redo buffer. */
6462 AppendCharToRedobuff(Ctrl_R);
6463 AppendCharToRedobuff(literally);
6464 AppendCharToRedobuff(regname);
6465
6466 do_put(regname, BACKWARD, 1L,
6467 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6468 }
6469 else if (insert_reg(regname, literally) == FAIL)
6470 {
6471 vim_beep();
6472 need_redraw = TRUE; /* remove the '"' */
6473 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006474 else if (stop_insert_mode)
6475 /* When the '=' register was used and a function was invoked that
6476 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
6477 * insert anything, need to remove the '"' */
6478 need_redraw = TRUE;
6479
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480#ifdef FEAT_EVAL
6481 }
6482 --no_u_sync;
6483#endif
6484#ifdef FEAT_CMDL_INFO
6485 clear_showcmd();
6486#endif
6487
6488 /* If the inserted register is empty, we need to remove the '"' */
6489 if (need_redraw || stuff_empty())
6490 edit_unputchar();
6491}
6492
6493/*
6494 * CTRL-G commands in Insert mode.
6495 */
6496 static void
6497ins_ctrl_g()
6498{
6499 int c;
6500
6501#ifdef FEAT_INS_EXPAND
6502 /* Right after CTRL-X the cursor will be after the ruler. */
6503 setcursor();
6504#endif
6505
6506 /*
6507 * Don't map the second key. This also prevents the mode message to be
6508 * deleted when ESC is hit.
6509 */
6510 ++no_mapping;
6511 c = safe_vgetc();
6512 --no_mapping;
6513 switch (c)
6514 {
6515 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
6516 case K_UP:
6517 case Ctrl_K:
6518 case 'k': ins_up(TRUE);
6519 break;
6520
6521 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
6522 case K_DOWN:
6523 case Ctrl_J:
6524 case 'j': ins_down(TRUE);
6525 break;
6526
6527 /* CTRL-G u: start new undoable edit */
6528 case 'u': u_sync();
6529 ins_need_undo = TRUE;
6530 break;
6531
6532 /* Unknown CTRL-G command, reserved for future expansion. */
6533 default: vim_beep();
6534 }
6535}
6536
6537/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006538 * CTRL-^ in Insert mode.
6539 */
6540 static void
6541ins_ctrl_hat()
6542{
6543 if (map_to_exists_mode((char_u *)"", LANGMAP))
6544 {
6545 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
6546 if (State & LANGMAP)
6547 {
6548 curbuf->b_p_iminsert = B_IMODE_NONE;
6549 State &= ~LANGMAP;
6550 }
6551 else
6552 {
6553 curbuf->b_p_iminsert = B_IMODE_LMAP;
6554 State |= LANGMAP;
6555#ifdef USE_IM_CONTROL
6556 im_set_active(FALSE);
6557#endif
6558 }
6559 }
6560#ifdef USE_IM_CONTROL
6561 else
6562 {
6563 /* There are no ":lmap" mappings, toggle IM */
6564 if (im_get_status())
6565 {
6566 curbuf->b_p_iminsert = B_IMODE_NONE;
6567 im_set_active(FALSE);
6568 }
6569 else
6570 {
6571 curbuf->b_p_iminsert = B_IMODE_IM;
6572 State &= ~LANGMAP;
6573 im_set_active(TRUE);
6574 }
6575 }
6576#endif
6577 set_iminsert_global();
6578 showmode();
6579#ifdef FEAT_GUI
6580 /* may show different cursor shape or color */
6581 if (gui.in_use)
6582 gui_update_cursor(TRUE, FALSE);
6583#endif
6584#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6585 /* Show/unshow value of 'keymap' in status lines. */
6586 status_redraw_curbuf();
6587#endif
6588}
6589
6590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 * Handle ESC in insert mode.
6592 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
6593 * insert.
6594 */
6595 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00006596ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 long *count;
6598 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00006599 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600{
6601 int temp;
6602 static int disabled_redraw = FALSE;
6603
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006604#ifdef FEAT_SYN_HL
6605 check_spell_redraw();
6606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607#if defined(FEAT_HANGULIN)
6608# if defined(ESC_CHG_TO_ENG_MODE)
6609 hangul_input_state_set(0);
6610# endif
6611 if (composing_hangul)
6612 {
6613 push_raw_key(composing_hangul_buffer, 2);
6614 composing_hangul = 0;
6615 }
6616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617
6618 temp = curwin->w_cursor.col;
6619 if (disabled_redraw)
6620 {
6621 --RedrawingDisabled;
6622 disabled_redraw = FALSE;
6623 }
6624 if (!arrow_used)
6625 {
6626 /*
6627 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00006628 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
6629 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 */
6631 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00006632 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633
6634 /*
6635 * Repeating insert may take a long time. Check for
6636 * interrupt now and then.
6637 */
6638 if (*count > 0)
6639 {
6640 line_breakcheck();
6641 if (got_int)
6642 *count = 0;
6643 }
6644
6645 if (--*count > 0) /* repeat what was typed */
6646 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006647 /* Vi repeats the insert without replacing characters. */
6648 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
6649 State &= ~REPLACE_FLAG;
6650
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 (void)start_redo_ins();
6652 if (cmdchar == 'r' || cmdchar == 'v')
6653 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
6654 ++RedrawingDisabled;
6655 disabled_redraw = TRUE;
6656 return FALSE; /* repeat the insert */
6657 }
6658 stop_insert(&curwin->w_cursor, TRUE);
6659 undisplay_dollar();
6660 }
6661
6662 /* When an autoindent was removed, curswant stays after the
6663 * indent */
6664 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
6665 curwin->w_set_curswant = TRUE;
6666
6667 /* Remember the last Insert position in the '^ mark. */
6668 if (!cmdmod.keepjumps)
6669 curbuf->b_last_insert = curwin->w_cursor;
6670
6671 /*
6672 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00006673 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00006675 if (!nomove
6676 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677#ifdef FEAT_VIRTUALEDIT
6678 || curwin->w_cursor.coladd > 0
6679#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006680 )
6681 && (restart_edit == NUL
6682 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00006684 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006686 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687#ifdef FEAT_RIGHTLEFT
6688 && !revins_on
6689#endif
6690 )
6691 {
6692#ifdef FEAT_VIRTUALEDIT
6693 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
6694 {
6695 oneleft();
6696 if (restart_edit != NUL)
6697 ++curwin->w_cursor.coladd;
6698 }
6699 else
6700#endif
6701 {
6702 --curwin->w_cursor.col;
6703#ifdef FEAT_MBYTE
6704 /* Correct cursor for multi-byte character. */
6705 if (has_mbyte)
6706 mb_adjust_cursor();
6707#endif
6708 }
6709 }
6710
6711#ifdef USE_IM_CONTROL
6712 /* Disable IM to allow typing English directly for Normal mode commands.
6713 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
6714 * well). */
6715 if (!(State & LANGMAP))
6716 im_save_status(&curbuf->b_p_iminsert);
6717 im_set_active(FALSE);
6718#endif
6719
6720 State = NORMAL;
6721 /* need to position cursor again (e.g. when on a TAB ) */
6722 changed_cline_bef_curs();
6723
6724#ifdef FEAT_MOUSE
6725 setmouse();
6726#endif
6727#ifdef CURSOR_SHAPE
6728 ui_cursor_shape(); /* may show different cursor shape */
6729#endif
6730
6731 /*
6732 * When recording or for CTRL-O, need to display the new mode.
6733 * Otherwise remove the mode message.
6734 */
6735 if (Recording || restart_edit != NUL)
6736 showmode();
6737 else if (p_smd)
6738 MSG("");
6739
6740 return TRUE; /* exit Insert mode */
6741}
6742
6743#ifdef FEAT_RIGHTLEFT
6744/*
6745 * Toggle language: hkmap and revins_on.
6746 * Move to end of reverse inserted text.
6747 */
6748 static void
6749ins_ctrl_()
6750{
6751 if (revins_on && revins_chars && revins_scol >= 0)
6752 {
6753 while (gchar_cursor() != NUL && revins_chars--)
6754 ++curwin->w_cursor.col;
6755 }
6756 p_ri = !p_ri;
6757 revins_on = (State == INSERT && p_ri);
6758 if (revins_on)
6759 {
6760 revins_scol = curwin->w_cursor.col;
6761 revins_legal++;
6762 revins_chars = 0;
6763 undisplay_dollar();
6764 }
6765 else
6766 revins_scol = -1;
6767#ifdef FEAT_FKMAP
6768 if (p_altkeymap)
6769 {
6770 /*
6771 * to be consistent also for redo command, using '.'
6772 * set arrow_used to true and stop it - causing to redo
6773 * characters entered in one mode (normal/reverse insert).
6774 */
6775 arrow_used = TRUE;
6776 (void)stop_arrow();
6777 p_fkmap = curwin->w_p_rl ^ p_ri;
6778 if (p_fkmap && p_ri)
6779 State = INSERT;
6780 }
6781 else
6782#endif
6783 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
6784 showmode();
6785}
6786#endif
6787
6788#ifdef FEAT_VISUAL
6789/*
6790 * If 'keymodel' contains "startsel", may start selection.
6791 * Returns TRUE when a CTRL-O and other keys stuffed.
6792 */
6793 static int
6794ins_start_select(c)
6795 int c;
6796{
6797 if (km_startsel)
6798 switch (c)
6799 {
6800 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 case K_PAGEUP:
6803 case K_KPAGEUP:
6804 case K_PAGEDOWN:
6805 case K_KPAGEDOWN:
6806# ifdef MACOS
6807 case K_LEFT:
6808 case K_RIGHT:
6809 case K_UP:
6810 case K_DOWN:
6811 case K_END:
6812 case K_HOME:
6813# endif
6814 if (!(mod_mask & MOD_MASK_SHIFT))
6815 break;
6816 /* FALLTHROUGH */
6817 case K_S_LEFT:
6818 case K_S_RIGHT:
6819 case K_S_UP:
6820 case K_S_DOWN:
6821 case K_S_END:
6822 case K_S_HOME:
6823 /* Start selection right away, the cursor can move with
6824 * CTRL-O when beyond the end of the line. */
6825 start_selection();
6826
6827 /* Execute the key in (insert) Select mode. */
6828 stuffcharReadbuff(Ctrl_O);
6829 if (mod_mask)
6830 {
6831 char_u buf[4];
6832
6833 buf[0] = K_SPECIAL;
6834 buf[1] = KS_MODIFIER;
6835 buf[2] = mod_mask;
6836 buf[3] = NUL;
6837 stuffReadbuff(buf);
6838 }
6839 stuffcharReadbuff(c);
6840 return TRUE;
6841 }
6842 return FALSE;
6843}
6844#endif
6845
6846/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006847 * <Insert> key in Insert mode: toggle insert/remplace mode.
6848 */
6849 static void
6850ins_insert(replaceState)
6851 int replaceState;
6852{
6853#ifdef FEAT_FKMAP
6854 if (p_fkmap && p_ri)
6855 {
6856 beep_flush();
6857 EMSG(farsi_text_3); /* encoded in Farsi */
6858 return;
6859 }
6860#endif
6861
6862#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00006863# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006864 set_vim_var_string(VV_INSERTMODE,
6865 (char_u *)((State & REPLACE_FLAG) ? "i" :
6866 replaceState == VREPLACE ? "v" : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00006867# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006868 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
6869#endif
6870 if (State & REPLACE_FLAG)
6871 State = INSERT | (State & LANGMAP);
6872 else
6873 State = replaceState | (State & LANGMAP);
6874 AppendCharToRedobuff(K_INS);
6875 showmode();
6876#ifdef CURSOR_SHAPE
6877 ui_cursor_shape(); /* may show different cursor shape */
6878#endif
6879}
6880
6881/*
6882 * Pressed CTRL-O in Insert mode.
6883 */
6884 static void
6885ins_ctrl_o()
6886{
6887#ifdef FEAT_VREPLACE
6888 if (State & VREPLACE_FLAG)
6889 restart_edit = 'V';
6890 else
6891#endif
6892 if (State & REPLACE_FLAG)
6893 restart_edit = 'R';
6894 else
6895 restart_edit = 'I';
6896#ifdef FEAT_VIRTUALEDIT
6897 if (virtual_active())
6898 ins_at_eol = FALSE; /* cursor always keeps its column */
6899 else
6900#endif
6901 ins_at_eol = (gchar_cursor() == NUL);
6902}
6903
6904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 * If the cursor is on an indent, ^T/^D insert/delete one
6906 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
6907 * Always round the indent to 'shiftwith', this is compatible
6908 * with vi. But vi only supports ^T and ^D after an
6909 * autoindent, we support it everywhere.
6910 */
6911 static void
6912ins_shift(c, lastc)
6913 int c;
6914 int lastc;
6915{
6916 if (stop_arrow() == FAIL)
6917 return;
6918 AppendCharToRedobuff(c);
6919
6920 /*
6921 * 0^D and ^^D: remove all indent.
6922 */
6923 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
6924 {
6925 --curwin->w_cursor.col;
6926 (void)del_char(FALSE); /* delete the '^' or '0' */
6927 /* In Replace mode, restore the characters that '^' or '0' replaced. */
6928 if (State & REPLACE_FLAG)
6929 replace_pop_ins();
6930 if (lastc == '^')
6931 old_indent = get_indent(); /* remember curr. indent */
6932 change_indent(INDENT_SET, 0, TRUE, 0);
6933 }
6934 else
6935 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
6936
6937 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
6938 did_ai = FALSE;
6939#ifdef FEAT_SMARTINDENT
6940 did_si = FALSE;
6941 can_si = FALSE;
6942 can_si_back = FALSE;
6943#endif
6944#ifdef FEAT_CINDENT
6945 can_cindent = FALSE; /* no cindenting after ^D or ^T */
6946#endif
6947}
6948
6949 static void
6950ins_del()
6951{
6952 int temp;
6953
6954 if (stop_arrow() == FAIL)
6955 return;
6956 if (gchar_cursor() == NUL) /* delete newline */
6957 {
6958 temp = curwin->w_cursor.col;
6959 if (!can_bs(BS_EOL) /* only if "eol" included */
6960 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
6961 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
6962 || do_join(FALSE) == FAIL)
6963 vim_beep();
6964 else
6965 curwin->w_cursor.col = temp;
6966 }
6967 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
6968 vim_beep();
6969 did_ai = FALSE;
6970#ifdef FEAT_SMARTINDENT
6971 did_si = FALSE;
6972 can_si = FALSE;
6973 can_si_back = FALSE;
6974#endif
6975 AppendCharToRedobuff(K_DEL);
6976}
6977
6978/*
6979 * Handle Backspace, delete-word and delete-line in Insert mode.
6980 * Return TRUE when backspace was actually used.
6981 */
6982 static int
6983ins_bs(c, mode, inserted_space_p)
6984 int c;
6985 int mode;
6986 int *inserted_space_p;
6987{
6988 linenr_T lnum;
6989 int cc;
6990 int temp = 0; /* init for GCC */
6991 colnr_T mincol;
6992 int did_backspace = FALSE;
6993 int in_indent;
6994 int oldState;
6995#ifdef FEAT_MBYTE
6996 int p1, p2;
6997#endif
6998
6999 /*
7000 * can't delete anything in an empty file
7001 * can't backup past first character in buffer
7002 * can't backup past starting point unless 'backspace' > 1
7003 * can backup to a previous line if 'backspace' == 0
7004 */
7005 if ( bufempty()
7006 || (
7007#ifdef FEAT_RIGHTLEFT
7008 !revins_on &&
7009#endif
7010 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7011 || (!can_bs(BS_START)
7012 && (arrow_used
7013 || (curwin->w_cursor.lnum == Insstart.lnum
7014 && curwin->w_cursor.col <= Insstart.col)))
7015 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7016 && curwin->w_cursor.col <= ai_col)
7017 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7018 {
7019 vim_beep();
7020 return FALSE;
7021 }
7022
7023 if (stop_arrow() == FAIL)
7024 return FALSE;
7025 in_indent = inindent(0);
7026#ifdef FEAT_CINDENT
7027 if (in_indent)
7028 can_cindent = FALSE;
7029#endif
7030#ifdef FEAT_COMMENTS
7031 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7032#endif
7033#ifdef FEAT_RIGHTLEFT
7034 if (revins_on) /* put cursor after last inserted char */
7035 inc_cursor();
7036#endif
7037
7038#ifdef FEAT_VIRTUALEDIT
7039 /* Virtualedit:
7040 * BACKSPACE_CHAR eats a virtual space
7041 * BACKSPACE_WORD eats all coladd
7042 * BACKSPACE_LINE eats all coladd and keeps going
7043 */
7044 if (curwin->w_cursor.coladd > 0)
7045 {
7046 if (mode == BACKSPACE_CHAR)
7047 {
7048 --curwin->w_cursor.coladd;
7049 return TRUE;
7050 }
7051 if (mode == BACKSPACE_WORD)
7052 {
7053 curwin->w_cursor.coladd = 0;
7054 return TRUE;
7055 }
7056 curwin->w_cursor.coladd = 0;
7057 }
7058#endif
7059
7060 /*
7061 * delete newline!
7062 */
7063 if (curwin->w_cursor.col == 0)
7064 {
7065 lnum = Insstart.lnum;
7066 if (curwin->w_cursor.lnum == Insstart.lnum
7067#ifdef FEAT_RIGHTLEFT
7068 || revins_on
7069#endif
7070 )
7071 {
7072 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7073 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7074 return FALSE;
7075 --Insstart.lnum;
7076 Insstart.col = MAXCOL;
7077 }
7078 /*
7079 * In replace mode:
7080 * cc < 0: NL was inserted, delete it
7081 * cc >= 0: NL was replaced, put original characters back
7082 */
7083 cc = -1;
7084 if (State & REPLACE_FLAG)
7085 cc = replace_pop(); /* returns -1 if NL was inserted */
7086 /*
7087 * In replace mode, in the line we started replacing, we only move the
7088 * cursor.
7089 */
7090 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7091 {
7092 dec_cursor();
7093 }
7094 else
7095 {
7096#ifdef FEAT_VREPLACE
7097 if (!(State & VREPLACE_FLAG)
7098 || curwin->w_cursor.lnum > orig_line_count)
7099#endif
7100 {
7101 temp = gchar_cursor(); /* remember current char */
7102 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007103
7104 /* When "aw" is in 'formatoptions' we must delete the space at
7105 * the end of the line, otherwise the line will be broken
7106 * again when auto-formatting. */
7107 if (has_format_option(FO_AUTO)
7108 && has_format_option(FO_WHITE_PAR))
7109 {
7110 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7111 TRUE);
7112 int len;
7113
7114 len = STRLEN(ptr);
7115 if (len > 0 && ptr[len - 1] == ' ')
7116 ptr[len - 1] = NUL;
7117 }
7118
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 (void)do_join(FALSE);
7120 if (temp == NUL && gchar_cursor() != NUL)
7121 inc_cursor();
7122 }
7123#ifdef FEAT_VREPLACE
7124 else
7125 dec_cursor();
7126#endif
7127
7128 /*
7129 * In REPLACE mode we have to put back the text that was replaced
7130 * by the NL. On the replace stack is first a NUL-terminated
7131 * sequence of characters that were deleted and then the
7132 * characters that NL replaced.
7133 */
7134 if (State & REPLACE_FLAG)
7135 {
7136 /*
7137 * Do the next ins_char() in NORMAL state, to
7138 * prevent ins_char() from replacing characters and
7139 * avoiding showmatch().
7140 */
7141 oldState = State;
7142 State = NORMAL;
7143 /*
7144 * restore characters (blanks) deleted after cursor
7145 */
7146 while (cc > 0)
7147 {
7148 temp = curwin->w_cursor.col;
7149#ifdef FEAT_MBYTE
7150 mb_replace_pop_ins(cc);
7151#else
7152 ins_char(cc);
7153#endif
7154 curwin->w_cursor.col = temp;
7155 cc = replace_pop();
7156 }
7157 /* restore the characters that NL replaced */
7158 replace_pop_ins();
7159 State = oldState;
7160 }
7161 }
7162 did_ai = FALSE;
7163 }
7164 else
7165 {
7166 /*
7167 * Delete character(s) before the cursor.
7168 */
7169#ifdef FEAT_RIGHTLEFT
7170 if (revins_on) /* put cursor on last inserted char */
7171 dec_cursor();
7172#endif
7173 mincol = 0;
7174 /* keep indent */
7175 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7176#ifdef FEAT_RIGHTLEFT
7177 && !revins_on
7178#endif
7179 )
7180 {
7181 temp = curwin->w_cursor.col;
7182 beginline(BL_WHITE);
7183 if (curwin->w_cursor.col < (colnr_T)temp)
7184 mincol = curwin->w_cursor.col;
7185 curwin->w_cursor.col = temp;
7186 }
7187
7188 /*
7189 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7190 */
7191 if ( mode == BACKSPACE_CHAR
7192 && ((p_sta && in_indent)
7193 || (curbuf->b_p_sts
7194 && (*(ml_get_cursor() - 1) == TAB
7195 || (*(ml_get_cursor() - 1) == ' '
7196 && (!*inserted_space_p
7197 || arrow_used))))))
7198 {
7199 int ts;
7200 colnr_T vcol;
7201 colnr_T want_vcol;
7202 int extra = 0;
7203
7204 *inserted_space_p = FALSE;
7205 if (p_sta)
7206 ts = curbuf->b_p_sw;
7207 else
7208 ts = curbuf->b_p_sts;
7209 /* Compute the virtual column where we want to be. Since
7210 * 'showbreak' may get in the way, need to get the last column of
7211 * the previous character. */
7212 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7213 dec_cursor();
7214 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7215 inc_cursor();
7216 want_vcol = (want_vcol / ts) * ts;
7217
7218 /* delete characters until we are at or before want_vcol */
7219 while (vcol > want_vcol
7220 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7221 {
7222 dec_cursor();
7223 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7224 if (State & REPLACE_FLAG)
7225 {
7226 /* Don't delete characters before the insert point when in
7227 * Replace mode */
7228 if (curwin->w_cursor.lnum != Insstart.lnum
7229 || curwin->w_cursor.col >= Insstart.col)
7230 {
7231#if 0 /* what was this for? It causes problems when sw != ts. */
7232 if (State == REPLACE && (int)vcol < want_vcol)
7233 {
7234 (void)del_char(FALSE);
7235 extra = 2; /* don't pop too much */
7236 }
7237 else
7238#endif
7239 replace_do_bs();
7240 }
7241 }
7242 else
7243 (void)del_char(FALSE);
7244 }
7245
7246 /* insert extra spaces until we are at want_vcol */
7247 while (vcol < want_vcol)
7248 {
7249 /* Remember the first char we inserted */
7250 if (curwin->w_cursor.lnum == Insstart.lnum
7251 && curwin->w_cursor.col < Insstart.col)
7252 Insstart.col = curwin->w_cursor.col;
7253
7254#ifdef FEAT_VREPLACE
7255 if (State & VREPLACE_FLAG)
7256 ins_char(' ');
7257 else
7258#endif
7259 {
7260 ins_str((char_u *)" ");
7261 if ((State & REPLACE_FLAG) && extra <= 1)
7262 {
7263 if (extra)
7264 replace_push_off(NUL);
7265 else
7266 replace_push(NUL);
7267 }
7268 if (extra == 2)
7269 extra = 1;
7270 }
7271 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7272 }
7273 }
7274
7275 /*
7276 * Delete upto starting point, start of line or previous word.
7277 */
7278 else do
7279 {
7280#ifdef FEAT_RIGHTLEFT
7281 if (!revins_on) /* put cursor on char to be deleted */
7282#endif
7283 dec_cursor();
7284
7285 /* start of word? */
7286 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7287 {
7288 mode = BACKSPACE_WORD_NOT_SPACE;
7289 temp = vim_iswordc(gchar_cursor());
7290 }
7291 /* end of word? */
7292 else if (mode == BACKSPACE_WORD_NOT_SPACE
7293 && (vim_isspace(cc = gchar_cursor())
7294 || vim_iswordc(cc) != temp))
7295 {
7296#ifdef FEAT_RIGHTLEFT
7297 if (!revins_on)
7298#endif
7299 inc_cursor();
7300#ifdef FEAT_RIGHTLEFT
7301 else if (State & REPLACE_FLAG)
7302 dec_cursor();
7303#endif
7304 break;
7305 }
7306 if (State & REPLACE_FLAG)
7307 replace_do_bs();
7308 else
7309 {
7310#ifdef FEAT_MBYTE
7311 if (enc_utf8 && p_deco)
7312 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7313#endif
7314 (void)del_char(FALSE);
7315#ifdef FEAT_MBYTE
7316 /*
7317 * If p1 or p2 is non-zero, there are combining characters we
7318 * need to take account of. Don't back up before the base
7319 * character.
7320 */
7321 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7322 inc_cursor();
7323#endif
7324#ifdef FEAT_RIGHTLEFT
7325 if (revins_chars)
7326 {
7327 revins_chars--;
7328 revins_legal++;
7329 }
7330 if (revins_on && gchar_cursor() == NUL)
7331 break;
7332#endif
7333 }
7334 /* Just a single backspace?: */
7335 if (mode == BACKSPACE_CHAR)
7336 break;
7337 } while (
7338#ifdef FEAT_RIGHTLEFT
7339 revins_on ||
7340#endif
7341 (curwin->w_cursor.col > mincol
7342 && (curwin->w_cursor.lnum != Insstart.lnum
7343 || curwin->w_cursor.col != Insstart.col)));
7344 did_backspace = TRUE;
7345 }
7346#ifdef FEAT_SMARTINDENT
7347 did_si = FALSE;
7348 can_si = FALSE;
7349 can_si_back = FALSE;
7350#endif
7351 if (curwin->w_cursor.col <= 1)
7352 did_ai = FALSE;
7353 /*
7354 * It's a little strange to put backspaces into the redo
7355 * buffer, but it makes auto-indent a lot easier to deal
7356 * with.
7357 */
7358 AppendCharToRedobuff(c);
7359
7360 /* If deleted before the insertion point, adjust it */
7361 if (curwin->w_cursor.lnum == Insstart.lnum
7362 && curwin->w_cursor.col < Insstart.col)
7363 Insstart.col = curwin->w_cursor.col;
7364
7365 /* vi behaviour: the cursor moves backward but the character that
7366 * was there remains visible
7367 * Vim behaviour: the cursor moves backward and the character that
7368 * was there is erased from the screen.
7369 * We can emulate the vi behaviour by pretending there is a dollar
7370 * displayed even when there isn't.
7371 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7372 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7373 dollar_vcol = curwin->w_virtcol;
7374
7375 return did_backspace;
7376}
7377
7378#ifdef FEAT_MOUSE
7379 static void
7380ins_mouse(c)
7381 int c;
7382{
7383 pos_T tpos;
7384
7385# ifdef FEAT_GUI
7386 /* When GUI is active, also move/paste when 'mouse' is empty */
7387 if (!gui.in_use)
7388# endif
7389 if (!mouse_has(MOUSE_INSERT))
7390 return;
7391
7392 undisplay_dollar();
7393 tpos = curwin->w_cursor;
7394 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7395 {
7396 start_arrow(&tpos);
7397# ifdef FEAT_CINDENT
7398 can_cindent = TRUE;
7399# endif
7400 }
7401
7402#ifdef FEAT_WINDOWS
7403 /* redraw status lines (in case another window became active) */
7404 redraw_statuslines();
7405#endif
7406}
7407
7408 static void
7409ins_mousescroll(up)
7410 int up;
7411{
7412 pos_T tpos;
7413# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7414 win_T *old_curwin;
7415# endif
7416
7417 tpos = curwin->w_cursor;
7418
7419# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7420 old_curwin = curwin;
7421
7422 /* Currently the mouse coordinates are only known in the GUI. */
7423 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7424 {
7425 int row, col;
7426
7427 row = mouse_row;
7428 col = mouse_col;
7429
7430 /* find the window at the pointer coordinates */
7431 curwin = mouse_find_win(&row, &col);
7432 curbuf = curwin->w_buffer;
7433 }
7434 if (curwin == old_curwin)
7435# endif
7436 undisplay_dollar();
7437
7438 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7439 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7440 else
7441 scroll_redraw(up, 3L);
7442
7443# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7444 curwin->w_redr_status = TRUE;
7445
7446 curwin = old_curwin;
7447 curbuf = curwin->w_buffer;
7448# endif
7449
7450 if (!equalpos(curwin->w_cursor, tpos))
7451 {
7452 start_arrow(&tpos);
7453# ifdef FEAT_CINDENT
7454 can_cindent = TRUE;
7455# endif
7456 }
7457}
7458#endif
7459
7460#ifdef FEAT_GUI
7461 void
7462ins_scroll()
7463{
7464 pos_T tpos;
7465
7466 undisplay_dollar();
7467 tpos = curwin->w_cursor;
7468 if (gui_do_scroll())
7469 {
7470 start_arrow(&tpos);
7471# ifdef FEAT_CINDENT
7472 can_cindent = TRUE;
7473# endif
7474 }
7475}
7476
7477 void
7478ins_horscroll()
7479{
7480 pos_T tpos;
7481
7482 undisplay_dollar();
7483 tpos = curwin->w_cursor;
7484 if (gui_do_horiz_scroll())
7485 {
7486 start_arrow(&tpos);
7487# ifdef FEAT_CINDENT
7488 can_cindent = TRUE;
7489# endif
7490 }
7491}
7492#endif
7493
7494 static void
7495ins_left()
7496{
7497 pos_T tpos;
7498
7499#ifdef FEAT_FOLDING
7500 if ((fdo_flags & FDO_HOR) && KeyTyped)
7501 foldOpenCursor();
7502#endif
7503 undisplay_dollar();
7504 tpos = curwin->w_cursor;
7505 if (oneleft() == OK)
7506 {
7507 start_arrow(&tpos);
7508#ifdef FEAT_RIGHTLEFT
7509 /* If exit reversed string, position is fixed */
7510 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
7511 revins_legal++;
7512 revins_chars++;
7513#endif
7514 }
7515
7516 /*
7517 * if 'whichwrap' set for cursor in insert mode may go to
7518 * previous line
7519 */
7520 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
7521 {
7522 start_arrow(&tpos);
7523 --(curwin->w_cursor.lnum);
7524 coladvance((colnr_T)MAXCOL);
7525 curwin->w_set_curswant = TRUE; /* so we stay at the end */
7526 }
7527 else
7528 vim_beep();
7529}
7530
7531 static void
7532ins_home(c)
7533 int c;
7534{
7535 pos_T tpos;
7536
7537#ifdef FEAT_FOLDING
7538 if ((fdo_flags & FDO_HOR) && KeyTyped)
7539 foldOpenCursor();
7540#endif
7541 undisplay_dollar();
7542 tpos = curwin->w_cursor;
7543 if (c == K_C_HOME)
7544 curwin->w_cursor.lnum = 1;
7545 curwin->w_cursor.col = 0;
7546#ifdef FEAT_VIRTUALEDIT
7547 curwin->w_cursor.coladd = 0;
7548#endif
7549 curwin->w_curswant = 0;
7550 start_arrow(&tpos);
7551}
7552
7553 static void
7554ins_end(c)
7555 int c;
7556{
7557 pos_T tpos;
7558
7559#ifdef FEAT_FOLDING
7560 if ((fdo_flags & FDO_HOR) && KeyTyped)
7561 foldOpenCursor();
7562#endif
7563 undisplay_dollar();
7564 tpos = curwin->w_cursor;
7565 if (c == K_C_END)
7566 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7567 coladvance((colnr_T)MAXCOL);
7568 curwin->w_curswant = MAXCOL;
7569
7570 start_arrow(&tpos);
7571}
7572
7573 static void
7574ins_s_left()
7575{
7576#ifdef FEAT_FOLDING
7577 if ((fdo_flags & FDO_HOR) && KeyTyped)
7578 foldOpenCursor();
7579#endif
7580 undisplay_dollar();
7581 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
7582 {
7583 start_arrow(&curwin->w_cursor);
7584 (void)bck_word(1L, FALSE, FALSE);
7585 curwin->w_set_curswant = TRUE;
7586 }
7587 else
7588 vim_beep();
7589}
7590
7591 static void
7592ins_right()
7593{
7594#ifdef FEAT_FOLDING
7595 if ((fdo_flags & FDO_HOR) && KeyTyped)
7596 foldOpenCursor();
7597#endif
7598 undisplay_dollar();
7599 if (gchar_cursor() != NUL || virtual_active()
7600 )
7601 {
7602 start_arrow(&curwin->w_cursor);
7603 curwin->w_set_curswant = TRUE;
7604#ifdef FEAT_VIRTUALEDIT
7605 if (virtual_active())
7606 oneright();
7607 else
7608#endif
7609 {
7610#ifdef FEAT_MBYTE
7611 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007612 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 else
7614#endif
7615 ++curwin->w_cursor.col;
7616 }
7617
7618#ifdef FEAT_RIGHTLEFT
7619 revins_legal++;
7620 if (revins_chars)
7621 revins_chars--;
7622#endif
7623 }
7624 /* if 'whichwrap' set for cursor in insert mode, may move the
7625 * cursor to the next line */
7626 else if (vim_strchr(p_ww, ']') != NULL
7627 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7628 {
7629 start_arrow(&curwin->w_cursor);
7630 curwin->w_set_curswant = TRUE;
7631 ++curwin->w_cursor.lnum;
7632 curwin->w_cursor.col = 0;
7633 }
7634 else
7635 vim_beep();
7636}
7637
7638 static void
7639ins_s_right()
7640{
7641#ifdef FEAT_FOLDING
7642 if ((fdo_flags & FDO_HOR) && KeyTyped)
7643 foldOpenCursor();
7644#endif
7645 undisplay_dollar();
7646 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
7647 || gchar_cursor() != NUL)
7648 {
7649 start_arrow(&curwin->w_cursor);
7650 (void)fwd_word(1L, FALSE, 0);
7651 curwin->w_set_curswant = TRUE;
7652 }
7653 else
7654 vim_beep();
7655}
7656
7657 static void
7658ins_up(startcol)
7659 int startcol; /* when TRUE move to Insstart.col */
7660{
7661 pos_T tpos;
7662 linenr_T old_topline = curwin->w_topline;
7663#ifdef FEAT_DIFF
7664 int old_topfill = curwin->w_topfill;
7665#endif
7666
7667 undisplay_dollar();
7668 tpos = curwin->w_cursor;
7669 if (cursor_up(1L, TRUE) == OK)
7670 {
7671 if (startcol)
7672 coladvance(getvcol_nolist(&Insstart));
7673 if (old_topline != curwin->w_topline
7674#ifdef FEAT_DIFF
7675 || old_topfill != curwin->w_topfill
7676#endif
7677 )
7678 redraw_later(VALID);
7679 start_arrow(&tpos);
7680#ifdef FEAT_CINDENT
7681 can_cindent = TRUE;
7682#endif
7683 }
7684 else
7685 vim_beep();
7686}
7687
7688 static void
7689ins_pageup()
7690{
7691 pos_T tpos;
7692
7693 undisplay_dollar();
7694 tpos = curwin->w_cursor;
7695 if (onepage(BACKWARD, 1L) == OK)
7696 {
7697 start_arrow(&tpos);
7698#ifdef FEAT_CINDENT
7699 can_cindent = TRUE;
7700#endif
7701 }
7702 else
7703 vim_beep();
7704}
7705
7706 static void
7707ins_down(startcol)
7708 int startcol; /* when TRUE move to Insstart.col */
7709{
7710 pos_T tpos;
7711 linenr_T old_topline = curwin->w_topline;
7712#ifdef FEAT_DIFF
7713 int old_topfill = curwin->w_topfill;
7714#endif
7715
7716 undisplay_dollar();
7717 tpos = curwin->w_cursor;
7718 if (cursor_down(1L, TRUE) == OK)
7719 {
7720 if (startcol)
7721 coladvance(getvcol_nolist(&Insstart));
7722 if (old_topline != curwin->w_topline
7723#ifdef FEAT_DIFF
7724 || old_topfill != curwin->w_topfill
7725#endif
7726 )
7727 redraw_later(VALID);
7728 start_arrow(&tpos);
7729#ifdef FEAT_CINDENT
7730 can_cindent = TRUE;
7731#endif
7732 }
7733 else
7734 vim_beep();
7735}
7736
7737 static void
7738ins_pagedown()
7739{
7740 pos_T tpos;
7741
7742 undisplay_dollar();
7743 tpos = curwin->w_cursor;
7744 if (onepage(FORWARD, 1L) == OK)
7745 {
7746 start_arrow(&tpos);
7747#ifdef FEAT_CINDENT
7748 can_cindent = TRUE;
7749#endif
7750 }
7751 else
7752 vim_beep();
7753}
7754
7755#ifdef FEAT_DND
7756 static void
7757ins_drop()
7758{
7759 do_put('~', BACKWARD, 1L, PUT_CURSEND);
7760}
7761#endif
7762
7763/*
7764 * Handle TAB in Insert or Replace mode.
7765 * Return TRUE when the TAB needs to be inserted like a normal character.
7766 */
7767 static int
7768ins_tab()
7769{
7770 int ind;
7771 int i;
7772 int temp;
7773
7774 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
7775 Insstart_blank_vcol = get_nolist_virtcol();
7776 if (echeck_abbr(TAB + ABBR_OFF))
7777 return FALSE;
7778
7779 ind = inindent(0);
7780#ifdef FEAT_CINDENT
7781 if (ind)
7782 can_cindent = FALSE;
7783#endif
7784
7785 /*
7786 * When nothing special, insert TAB like a normal character
7787 */
7788 if (!curbuf->b_p_et
7789 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
7790 && curbuf->b_p_sts == 0)
7791 return TRUE;
7792
7793 if (stop_arrow() == FAIL)
7794 return TRUE;
7795
7796 did_ai = FALSE;
7797#ifdef FEAT_SMARTINDENT
7798 did_si = FALSE;
7799 can_si = FALSE;
7800 can_si_back = FALSE;
7801#endif
7802 AppendToRedobuff((char_u *)"\t");
7803
7804 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
7805 temp = (int)curbuf->b_p_sw;
7806 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
7807 temp = (int)curbuf->b_p_sts;
7808 else /* otherwise use 'tabstop' */
7809 temp = (int)curbuf->b_p_ts;
7810 temp -= get_nolist_virtcol() % temp;
7811
7812 /*
7813 * Insert the first space with ins_char(). It will delete one char in
7814 * replace mode. Insert the rest with ins_str(); it will not delete any
7815 * chars. For VREPLACE mode, we use ins_char() for all characters.
7816 */
7817 ins_char(' ');
7818 while (--temp > 0)
7819 {
7820#ifdef FEAT_VREPLACE
7821 if (State & VREPLACE_FLAG)
7822 ins_char(' ');
7823 else
7824#endif
7825 {
7826 ins_str((char_u *)" ");
7827 if (State & REPLACE_FLAG) /* no char replaced */
7828 replace_push(NUL);
7829 }
7830 }
7831
7832 /*
7833 * When 'expandtab' not set: Replace spaces by TABs where possible.
7834 */
7835 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
7836 {
7837 char_u *ptr;
7838#ifdef FEAT_VREPLACE
7839 char_u *saved_line = NULL; /* init for GCC */
7840 pos_T pos;
7841#endif
7842 pos_T fpos;
7843 pos_T *cursor;
7844 colnr_T want_vcol, vcol;
7845 int change_col = -1;
7846 int save_list = curwin->w_p_list;
7847
7848 /*
7849 * Get the current line. For VREPLACE mode, don't make real changes
7850 * yet, just work on a copy of the line.
7851 */
7852#ifdef FEAT_VREPLACE
7853 if (State & VREPLACE_FLAG)
7854 {
7855 pos = curwin->w_cursor;
7856 cursor = &pos;
7857 saved_line = vim_strsave(ml_get_curline());
7858 if (saved_line == NULL)
7859 return FALSE;
7860 ptr = saved_line + pos.col;
7861 }
7862 else
7863#endif
7864 {
7865 ptr = ml_get_cursor();
7866 cursor = &curwin->w_cursor;
7867 }
7868
7869 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
7870 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
7871 curwin->w_p_list = FALSE;
7872
7873 /* Find first white before the cursor */
7874 fpos = curwin->w_cursor;
7875 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
7876 {
7877 --fpos.col;
7878 --ptr;
7879 }
7880
7881 /* In Replace mode, don't change characters before the insert point. */
7882 if ((State & REPLACE_FLAG)
7883 && fpos.lnum == Insstart.lnum
7884 && fpos.col < Insstart.col)
7885 {
7886 ptr += Insstart.col - fpos.col;
7887 fpos.col = Insstart.col;
7888 }
7889
7890 /* compute virtual column numbers of first white and cursor */
7891 getvcol(curwin, &fpos, &vcol, NULL, NULL);
7892 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
7893
7894 /* Use as many TABs as possible. Beware of 'showbreak' and
7895 * 'linebreak' adding extra virtual columns. */
7896 while (vim_iswhite(*ptr))
7897 {
7898 i = lbr_chartabsize((char_u *)"\t", vcol);
7899 if (vcol + i > want_vcol)
7900 break;
7901 if (*ptr != TAB)
7902 {
7903 *ptr = TAB;
7904 if (change_col < 0)
7905 {
7906 change_col = fpos.col; /* Column of first change */
7907 /* May have to adjust Insstart */
7908 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
7909 Insstart.col = fpos.col;
7910 }
7911 }
7912 ++fpos.col;
7913 ++ptr;
7914 vcol += i;
7915 }
7916
7917 if (change_col >= 0)
7918 {
7919 int repl_off = 0;
7920
7921 /* Skip over the spaces we need. */
7922 while (vcol < want_vcol && *ptr == ' ')
7923 {
7924 vcol += lbr_chartabsize(ptr, vcol);
7925 ++ptr;
7926 ++repl_off;
7927 }
7928 if (vcol > want_vcol)
7929 {
7930 /* Must have a char with 'showbreak' just before it. */
7931 --ptr;
7932 --repl_off;
7933 }
7934 fpos.col += repl_off;
7935
7936 /* Delete following spaces. */
7937 i = cursor->col - fpos.col;
7938 if (i > 0)
7939 {
7940 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
7941 /* correct replace stack. */
7942 if ((State & REPLACE_FLAG)
7943#ifdef FEAT_VREPLACE
7944 && !(State & VREPLACE_FLAG)
7945#endif
7946 )
7947 for (temp = i; --temp >= 0; )
7948 replace_join(repl_off);
7949 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00007950#ifdef FEAT_NETBEANS_INTG
7951 if (usingNetbeans)
7952 {
7953 netbeans_removed(curbuf, fpos.lnum, cursor->col,
7954 (long)(i + 1));
7955 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
7956 (char_u *)"\t", 1);
7957 }
7958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 cursor->col -= i;
7960
7961#ifdef FEAT_VREPLACE
7962 /*
7963 * In VREPLACE mode, we haven't changed anything yet. Do it now by
7964 * backspacing over the changed spacing and then inserting the new
7965 * spacing.
7966 */
7967 if (State & VREPLACE_FLAG)
7968 {
7969 /* Backspace from real cursor to change_col */
7970 backspace_until_column(change_col);
7971
7972 /* Insert each char in saved_line from changed_col to
7973 * ptr-cursor */
7974 ins_bytes_len(saved_line + change_col,
7975 cursor->col - change_col);
7976 }
7977#endif
7978 }
7979
7980#ifdef FEAT_VREPLACE
7981 if (State & VREPLACE_FLAG)
7982 vim_free(saved_line);
7983#endif
7984 curwin->w_p_list = save_list;
7985 }
7986
7987 return FALSE;
7988}
7989
7990/*
7991 * Handle CR or NL in insert mode.
7992 * Return TRUE when out of memory or can't undo.
7993 */
7994 static int
7995ins_eol(c)
7996 int c;
7997{
7998 int i;
7999
8000 if (echeck_abbr(c + ABBR_OFF))
8001 return FALSE;
8002 if (stop_arrow() == FAIL)
8003 return TRUE;
8004 undisplay_dollar();
8005
8006 /*
8007 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8008 * character under the cursor. Only push a NUL on the replace stack,
8009 * nothing to put back when the NL is deleted.
8010 */
8011 if ((State & REPLACE_FLAG)
8012#ifdef FEAT_VREPLACE
8013 && !(State & VREPLACE_FLAG)
8014#endif
8015 )
8016 replace_push(NUL);
8017
8018 /*
8019 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8020 * replacing the next line, so we push all of the characters left on the
8021 * line onto the replace stack. This is not done here though, it is done
8022 * in open_line().
8023 */
8024
8025#ifdef FEAT_RIGHTLEFT
8026# ifdef FEAT_FKMAP
8027 if (p_altkeymap && p_fkmap)
8028 fkmap(NL);
8029# endif
8030 /* NL in reverse insert will always start in the end of
8031 * current line. */
8032 if (revins_on)
8033 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8034#endif
8035
8036 AppendToRedobuff(NL_STR);
8037 i = open_line(FORWARD,
8038#ifdef FEAT_COMMENTS
8039 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8040#endif
8041 0, old_indent);
8042 old_indent = 0;
8043#ifdef FEAT_CINDENT
8044 can_cindent = TRUE;
8045#endif
8046
8047 return (!i);
8048}
8049
8050#ifdef FEAT_DIGRAPHS
8051/*
8052 * Handle digraph in insert mode.
8053 * Returns character still to be inserted, or NUL when nothing remaining to be
8054 * done.
8055 */
8056 static int
8057ins_digraph()
8058{
8059 int c;
8060 int cc;
8061
8062 pc_status = PC_STATUS_UNSET;
8063 if (redrawing() && !char_avail())
8064 {
8065 /* may need to redraw when no more chars available now */
8066 ins_redraw();
8067
8068 edit_putchar('?', TRUE);
8069#ifdef FEAT_CMDL_INFO
8070 add_to_showcmd_c(Ctrl_K);
8071#endif
8072 }
8073
8074#ifdef USE_ON_FLY_SCROLL
8075 dont_scroll = TRUE; /* disallow scrolling here */
8076#endif
8077
8078 /* don't map the digraph chars. This also prevents the
8079 * mode message to be deleted when ESC is hit */
8080 ++no_mapping;
8081 ++allow_keys;
8082 c = safe_vgetc();
8083 --no_mapping;
8084 --allow_keys;
8085 if (IS_SPECIAL(c) || mod_mask) /* special key */
8086 {
8087#ifdef FEAT_CMDL_INFO
8088 clear_showcmd();
8089#endif
8090 insert_special(c, TRUE, FALSE);
8091 return NUL;
8092 }
8093 if (c != ESC)
8094 {
8095 if (redrawing() && !char_avail())
8096 {
8097 /* may need to redraw when no more chars available now */
8098 ins_redraw();
8099
8100 if (char2cells(c) == 1)
8101 {
8102 /* first remove the '?', otherwise it's restored when typing
8103 * an ESC next */
8104 edit_unputchar();
8105 ins_redraw();
8106 edit_putchar(c, TRUE);
8107 }
8108#ifdef FEAT_CMDL_INFO
8109 add_to_showcmd_c(c);
8110#endif
8111 }
8112 ++no_mapping;
8113 ++allow_keys;
8114 cc = safe_vgetc();
8115 --no_mapping;
8116 --allow_keys;
8117 if (cc != ESC)
8118 {
8119 AppendToRedobuff((char_u *)CTRL_V_STR);
8120 c = getdigraph(c, cc, TRUE);
8121#ifdef FEAT_CMDL_INFO
8122 clear_showcmd();
8123#endif
8124 return c;
8125 }
8126 }
8127 edit_unputchar();
8128#ifdef FEAT_CMDL_INFO
8129 clear_showcmd();
8130#endif
8131 return NUL;
8132}
8133#endif
8134
8135/*
8136 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8137 * Returns the char to be inserted, or NUL if none found.
8138 */
8139 static int
8140ins_copychar(lnum)
8141 linenr_T lnum;
8142{
8143 int c;
8144 int temp;
8145 char_u *ptr, *prev_ptr;
8146
8147 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8148 {
8149 vim_beep();
8150 return NUL;
8151 }
8152
8153 /* try to advance to the cursor column */
8154 temp = 0;
8155 ptr = ml_get(lnum);
8156 prev_ptr = ptr;
8157 validate_virtcol();
8158 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8159 {
8160 prev_ptr = ptr;
8161 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8162 }
8163 if ((colnr_T)temp > curwin->w_virtcol)
8164 ptr = prev_ptr;
8165
8166#ifdef FEAT_MBYTE
8167 c = (*mb_ptr2char)(ptr);
8168#else
8169 c = *ptr;
8170#endif
8171 if (c == NUL)
8172 vim_beep();
8173 return c;
8174}
8175
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008176/*
8177 * CTRL-Y or CTRL-E typed in Insert mode.
8178 */
8179 static int
8180ins_ctrl_ey(tc)
8181 int tc;
8182{
8183 int c = tc;
8184
8185#ifdef FEAT_INS_EXPAND
8186 if (ctrl_x_mode == CTRL_X_SCROLL)
8187 {
8188 if (c == Ctrl_Y)
8189 scrolldown_clamp();
8190 else
8191 scrollup_clamp();
8192 redraw_later(VALID);
8193 }
8194 else
8195#endif
8196 {
8197 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8198 if (c != NUL)
8199 {
8200 long tw_save;
8201
8202 /* The character must be taken literally, insert like it
8203 * was typed after a CTRL-V, and pretend 'textwidth'
8204 * wasn't set. Digits, 'o' and 'x' are special after a
8205 * CTRL-V, don't use it for these. */
8206 if (c < 256 && !isalnum(c))
8207 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8208 tw_save = curbuf->b_p_tw;
8209 curbuf->b_p_tw = -1;
8210 insert_special(c, TRUE, FALSE);
8211 curbuf->b_p_tw = tw_save;
8212#ifdef FEAT_RIGHTLEFT
8213 revins_chars++;
8214 revins_legal++;
8215#endif
8216 c = Ctrl_V; /* pretend CTRL-V is last character */
8217 auto_format(FALSE, TRUE);
8218 }
8219 }
8220 return c;
8221}
8222
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223#ifdef FEAT_SMARTINDENT
8224/*
8225 * Try to do some very smart auto-indenting.
8226 * Used when inserting a "normal" character.
8227 */
8228 static void
8229ins_try_si(c)
8230 int c;
8231{
8232 pos_T *pos, old_pos;
8233 char_u *ptr;
8234 int i;
8235 int temp;
8236
8237 /*
8238 * do some very smart indenting when entering '{' or '}'
8239 */
8240 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8241 {
8242 /*
8243 * for '}' set indent equal to indent of line containing matching '{'
8244 */
8245 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8246 {
8247 old_pos = curwin->w_cursor;
8248 /*
8249 * If the matching '{' has a ')' immediately before it (ignoring
8250 * white-space), then line up with the start of the line
8251 * containing the matching '(' if there is one. This handles the
8252 * case where an "if (..\n..) {" statement continues over multiple
8253 * lines -- webb
8254 */
8255 ptr = ml_get(pos->lnum);
8256 i = pos->col;
8257 if (i > 0) /* skip blanks before '{' */
8258 while (--i > 0 && vim_iswhite(ptr[i]))
8259 ;
8260 curwin->w_cursor.lnum = pos->lnum;
8261 curwin->w_cursor.col = i;
8262 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8263 curwin->w_cursor = *pos;
8264 i = get_indent();
8265 curwin->w_cursor = old_pos;
8266#ifdef FEAT_VREPLACE
8267 if (State & VREPLACE_FLAG)
8268 change_indent(INDENT_SET, i, FALSE, NUL);
8269 else
8270#endif
8271 (void)set_indent(i, SIN_CHANGED);
8272 }
8273 else if (curwin->w_cursor.col > 0)
8274 {
8275 /*
8276 * when inserting '{' after "O" reduce indent, but not
8277 * more than indent of previous line
8278 */
8279 temp = TRUE;
8280 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8281 {
8282 old_pos = curwin->w_cursor;
8283 i = get_indent();
8284 while (curwin->w_cursor.lnum > 1)
8285 {
8286 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8287
8288 /* ignore empty lines and lines starting with '#'. */
8289 if (*ptr != '#' && *ptr != NUL)
8290 break;
8291 }
8292 if (get_indent() >= i)
8293 temp = FALSE;
8294 curwin->w_cursor = old_pos;
8295 }
8296 if (temp)
8297 shift_line(TRUE, FALSE, 1);
8298 }
8299 }
8300
8301 /*
8302 * set indent of '#' always to 0
8303 */
8304 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8305 {
8306 /* remember current indent for next line */
8307 old_indent = get_indent();
8308 (void)set_indent(0, SIN_CHANGED);
8309 }
8310
8311 /* Adjust ai_col, the char at this position can be deleted. */
8312 if (ai_col > curwin->w_cursor.col)
8313 ai_col = curwin->w_cursor.col;
8314}
8315#endif
8316
8317/*
8318 * Get the value that w_virtcol would have when 'list' is off.
8319 * Unless 'cpo' contains the 'L' flag.
8320 */
8321 static colnr_T
8322get_nolist_virtcol()
8323{
8324 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8325 return getvcol_nolist(&curwin->w_cursor);
8326 validate_virtcol();
8327 return curwin->w_virtcol;
8328}