blob: f4f3f9a77092f46e6daf3046a4a7b677da6d6934 [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
990#ifdef FEAT_GUI
991 case K_VER_SCROLLBAR:
992 ins_scroll();
993 break;
994
995 case K_HOR_SCROLLBAR:
996 ins_horscroll();
997 break;
998#endif
999
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001000 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 case K_S_HOME:
1003 case K_C_HOME:
1004 ins_home(c);
1005 break;
1006
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001007 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 case K_S_END:
1010 case K_C_END:
1011 ins_end(c);
1012 break;
1013
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001014 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001015 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1016 ins_s_left();
1017 else
1018 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 break;
1020
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001021 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 case K_C_LEFT:
1023 ins_s_left();
1024 break;
1025
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001026 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001027 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1028 ins_s_right();
1029 else
1030 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 break;
1032
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001033 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 case K_C_RIGHT:
1035 ins_s_right();
1036 break;
1037
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001038 case K_UP: /* <Up> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001039 if (mod_mask & MOD_MASK_SHIFT)
1040 ins_pageup();
1041 else
1042 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 break;
1044
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001045 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 case K_PAGEUP:
1047 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001048#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001049 if (pum_visible())
1050 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 ins_pageup();
1053 break;
1054
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 case K_DOWN: /* <Down> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001056 if (mod_mask & MOD_MASK_SHIFT)
1057 ins_pagedown();
1058 else
1059 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 break;
1061
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001062 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 case K_PAGEDOWN:
1064 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001065#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001066 if (pum_visible())
1067 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 ins_pagedown();
1070 break;
1071
1072#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001073 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 ins_drop();
1075 break;
1076#endif
1077
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001078 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 c = TAB;
1080 /* FALLTHROUGH */
1081
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001082 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1084 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1085 goto docomplete;
1086#endif
1087 inserted_space = FALSE;
1088 if (ins_tab())
1089 goto normalchar; /* insert TAB as a normal char */
1090 auto_format(FALSE, TRUE);
1091 break;
1092
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001093 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 c = CAR;
1095 /* FALLTHROUGH */
1096 case CAR:
1097 case NL:
1098#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1099 /* In a quickfix window a <CR> jumps to the error under the
1100 * cursor. */
1101 if (bt_quickfix(curbuf) && c == CAR)
1102 {
1103 do_cmdline_cmd((char_u *)".cc");
1104 break;
1105 }
1106#endif
1107#ifdef FEAT_CMDWIN
1108 if (cmdwin_type != 0)
1109 {
1110 /* Execute the command in the cmdline window. */
1111 cmdwin_result = CAR;
1112 goto doESCkey;
1113 }
1114#endif
1115 if (ins_eol(c) && !p_im)
1116 goto doESCkey; /* out of memory */
1117 auto_format(FALSE, FALSE);
1118 inserted_space = FALSE;
1119 break;
1120
1121#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001122 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123# ifdef FEAT_INS_EXPAND
1124 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1125 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001126 if (has_compl_option(TRUE))
1127 goto docomplete;
1128 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 }
1130# endif
1131# ifdef FEAT_DIGRAPHS
1132 c = ins_digraph();
1133 if (c == NUL)
1134 break;
1135# endif
1136 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138
1139#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001140 case Ctrl_X: /* Enter CTRL-X mode */
1141 ins_ctrl_x();
1142 break;
1143
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001144 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 if (ctrl_x_mode != CTRL_X_TAGS)
1146 goto normalchar;
1147 goto docomplete;
1148
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001149 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 if (ctrl_x_mode != CTRL_X_FILES)
1151 goto normalchar;
1152 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001153
1154 case 's': /* Spelling completion after ^X */
1155 case Ctrl_S:
1156 if (ctrl_x_mode != CTRL_X_SPELL)
1157 goto normalchar;
1158 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159#endif
1160
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001161 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162#ifdef FEAT_INS_EXPAND
1163 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1164#endif
1165 {
1166 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1167 if (p_im)
1168 {
1169 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1170 break;
1171 goto doESCkey;
1172 }
1173 goto normalchar;
1174 }
1175#ifdef FEAT_INS_EXPAND
1176 /* FALLTHROUGH */
1177
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001178 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 case Ctrl_N:
1180 /* if 'complete' is empty then plain ^P is no longer special,
1181 * but it is under other ^X modes */
1182 if (*curbuf->b_p_cpt == NUL
1183 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001184 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 goto normalchar;
1186
1187docomplete:
1188 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001189 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 break;
1191#endif /* FEAT_INS_EXPAND */
1192
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001193 case Ctrl_Y: /* copy from previous line or scroll down */
1194 case Ctrl_E: /* copy from next line or scroll up */
1195 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 break;
1197
1198 default:
1199#ifdef UNIX
1200 if (c == intr_char) /* special interrupt char */
1201 goto do_intr;
1202#endif
1203
1204 /*
1205 * Insert a nomal character.
1206 */
1207normalchar:
1208#ifdef FEAT_SMARTINDENT
1209 /* Try to perform smart-indenting. */
1210 ins_try_si(c);
1211#endif
1212
1213 if (c == ' ')
1214 {
1215 inserted_space = TRUE;
1216#ifdef FEAT_CINDENT
1217 if (inindent(0))
1218 can_cindent = FALSE;
1219#endif
1220 if (Insstart_blank_vcol == MAXCOL
1221 && curwin->w_cursor.lnum == Insstart.lnum)
1222 Insstart_blank_vcol = get_nolist_virtcol();
1223 }
1224
1225 if (vim_iswordc(c) || !echeck_abbr(
1226#ifdef FEAT_MBYTE
1227 /* Add ABBR_OFF for characters above 0x100, this is
1228 * what check_abbr() expects. */
1229 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1230#endif
1231 c))
1232 {
1233 insert_special(c, FALSE, FALSE);
1234#ifdef FEAT_RIGHTLEFT
1235 revins_legal++;
1236 revins_chars++;
1237#endif
1238 }
1239
1240 auto_format(FALSE, TRUE);
1241
1242#ifdef FEAT_FOLDING
1243 /* When inserting a character the cursor line must never be in a
1244 * closed fold. */
1245 foldOpenCursor();
1246#endif
1247 break;
1248 } /* end of switch (c) */
1249
1250 /* If the cursor was moved we didn't just insert a space */
1251 if (arrow_used)
1252 inserted_space = FALSE;
1253
1254#ifdef FEAT_CINDENT
1255 if (can_cindent && cindent_on()
1256# ifdef FEAT_INS_EXPAND
1257 && ctrl_x_mode == 0
1258# endif
1259 )
1260 {
1261force_cindent:
1262 /*
1263 * Indent now if a key was typed that is in 'cinkeys'.
1264 */
1265 if (in_cinkeys(c, ' ', line_is_white))
1266 {
1267 if (stop_arrow() == OK)
1268 /* re-indent the current line */
1269 do_c_expr_indent();
1270 }
1271 }
1272#endif /* FEAT_CINDENT */
1273
1274 } /* for (;;) */
1275 /* NOTREACHED */
1276}
1277
1278/*
1279 * Redraw for Insert mode.
1280 * This is postponed until getting the next character to make '$' in the 'cpo'
1281 * option work correctly.
1282 * Only redraw when there are no characters available. This speeds up
1283 * inserting sequences of characters (e.g., for CTRL-R).
1284 */
1285 static void
1286ins_redraw()
1287{
1288 if (!char_avail())
1289 {
1290 if (must_redraw)
1291 update_screen(0);
1292 else if (clear_cmdline || redraw_cmdline)
1293 showmode(); /* clear cmdline and show mode */
1294 showruler(FALSE);
1295 setcursor();
1296 emsg_on_display = FALSE; /* may remove error message now */
1297 }
1298}
1299
1300/*
1301 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1302 */
1303 static void
1304ins_ctrl_v()
1305{
1306 int c;
1307
1308 /* may need to redraw when no more chars available now */
1309 ins_redraw();
1310
1311 if (redrawing() && !char_avail())
1312 edit_putchar('^', TRUE);
1313 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1314
1315#ifdef FEAT_CMDL_INFO
1316 add_to_showcmd_c(Ctrl_V);
1317#endif
1318
1319 c = get_literal();
1320#ifdef FEAT_CMDL_INFO
1321 clear_showcmd();
1322#endif
1323 insert_special(c, FALSE, TRUE);
1324#ifdef FEAT_RIGHTLEFT
1325 revins_chars++;
1326 revins_legal++;
1327#endif
1328}
1329
1330/*
1331 * Put a character directly onto the screen. It's not stored in a buffer.
1332 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1333 */
1334static int pc_status;
1335#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1336#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1337#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1338#define PC_STATUS_SET 3 /* pc_bytes was filled */
1339#ifdef FEAT_MBYTE
1340static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1341#else
1342static char_u pc_bytes[2]; /* saved bytes */
1343#endif
1344static int pc_attr;
1345static int pc_row;
1346static int pc_col;
1347
1348 void
1349edit_putchar(c, highlight)
1350 int c;
1351 int highlight;
1352{
1353 int attr;
1354
1355 if (ScreenLines != NULL)
1356 {
1357 update_topline(); /* just in case w_topline isn't valid */
1358 validate_cursor();
1359 if (highlight)
1360 attr = hl_attr(HLF_8);
1361 else
1362 attr = 0;
1363 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1364 pc_col = W_WINCOL(curwin);
1365#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1366 pc_status = PC_STATUS_UNSET;
1367#endif
1368#ifdef FEAT_RIGHTLEFT
1369 if (curwin->w_p_rl)
1370 {
1371 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1372# ifdef FEAT_MBYTE
1373 if (has_mbyte)
1374 {
1375 int fix_col = mb_fix_col(pc_col, pc_row);
1376
1377 if (fix_col != pc_col)
1378 {
1379 screen_putchar(' ', pc_row, fix_col, attr);
1380 --curwin->w_wcol;
1381 pc_status = PC_STATUS_RIGHT;
1382 }
1383 }
1384# endif
1385 }
1386 else
1387#endif
1388 {
1389 pc_col += curwin->w_wcol;
1390#ifdef FEAT_MBYTE
1391 if (mb_lefthalve(pc_row, pc_col))
1392 pc_status = PC_STATUS_LEFT;
1393#endif
1394 }
1395
1396 /* save the character to be able to put it back */
1397#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1398 if (pc_status == PC_STATUS_UNSET)
1399#endif
1400 {
1401 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1402 pc_status = PC_STATUS_SET;
1403 }
1404 screen_putchar(c, pc_row, pc_col, attr);
1405 }
1406}
1407
1408/*
1409 * Undo the previous edit_putchar().
1410 */
1411 void
1412edit_unputchar()
1413{
1414 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1415 {
1416#if defined(FEAT_MBYTE)
1417 if (pc_status == PC_STATUS_RIGHT)
1418 ++curwin->w_wcol;
1419 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1420 redrawWinline(curwin->w_cursor.lnum, FALSE);
1421 else
1422#endif
1423 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1424 }
1425}
1426
1427/*
1428 * Called when p_dollar is set: display a '$' at the end of the changed text
1429 * Only works when cursor is in the line that changes.
1430 */
1431 void
1432display_dollar(col)
1433 colnr_T col;
1434{
1435 colnr_T save_col;
1436
1437 if (!redrawing())
1438 return;
1439
1440 cursor_off();
1441 save_col = curwin->w_cursor.col;
1442 curwin->w_cursor.col = col;
1443#ifdef FEAT_MBYTE
1444 if (has_mbyte)
1445 {
1446 char_u *p;
1447
1448 /* If on the last byte of a multi-byte move to the first byte. */
1449 p = ml_get_curline();
1450 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1451 }
1452#endif
1453 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1454 if (curwin->w_wcol < W_WIDTH(curwin))
1455 {
1456 edit_putchar('$', FALSE);
1457 dollar_vcol = curwin->w_virtcol;
1458 }
1459 curwin->w_cursor.col = save_col;
1460}
1461
1462/*
1463 * Call this function before moving the cursor from the normal insert position
1464 * in insert mode.
1465 */
1466 static void
1467undisplay_dollar()
1468{
1469 if (dollar_vcol)
1470 {
1471 dollar_vcol = 0;
1472 redrawWinline(curwin->w_cursor.lnum, FALSE);
1473 }
1474}
1475
1476/*
1477 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1478 * Keep the cursor on the same character.
1479 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1480 * type == INDENT_DEC decrease indent (for CTRL-D)
1481 * type == INDENT_SET set indent to "amount"
1482 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1483 */
1484 void
1485change_indent(type, amount, round, replaced)
1486 int type;
1487 int amount;
1488 int round;
1489 int replaced; /* replaced character, put on replace stack */
1490{
1491 int vcol;
1492 int last_vcol;
1493 int insstart_less; /* reduction for Insstart.col */
1494 int new_cursor_col;
1495 int i;
1496 char_u *ptr;
1497 int save_p_list;
1498 int start_col;
1499 colnr_T vc;
1500#ifdef FEAT_VREPLACE
1501 colnr_T orig_col = 0; /* init for GCC */
1502 char_u *new_line, *orig_line = NULL; /* init for GCC */
1503
1504 /* VREPLACE mode needs to know what the line was like before changing */
1505 if (State & VREPLACE_FLAG)
1506 {
1507 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1508 orig_col = curwin->w_cursor.col;
1509 }
1510#endif
1511
1512 /* for the following tricks we don't want list mode */
1513 save_p_list = curwin->w_p_list;
1514 curwin->w_p_list = FALSE;
1515 vc = getvcol_nolist(&curwin->w_cursor);
1516 vcol = vc;
1517
1518 /*
1519 * For Replace mode we need to fix the replace stack later, which is only
1520 * possible when the cursor is in the indent. Remember the number of
1521 * characters before the cursor if it's possible.
1522 */
1523 start_col = curwin->w_cursor.col;
1524
1525 /* determine offset from first non-blank */
1526 new_cursor_col = curwin->w_cursor.col;
1527 beginline(BL_WHITE);
1528 new_cursor_col -= curwin->w_cursor.col;
1529
1530 insstart_less = curwin->w_cursor.col;
1531
1532 /*
1533 * If the cursor is in the indent, compute how many screen columns the
1534 * cursor is to the left of the first non-blank.
1535 */
1536 if (new_cursor_col < 0)
1537 vcol = get_indent() - vcol;
1538
1539 if (new_cursor_col > 0) /* can't fix replace stack */
1540 start_col = -1;
1541
1542 /*
1543 * Set the new indent. The cursor will be put on the first non-blank.
1544 */
1545 if (type == INDENT_SET)
1546 (void)set_indent(amount, SIN_CHANGED);
1547 else
1548 {
1549#ifdef FEAT_VREPLACE
1550 int save_State = State;
1551
1552 /* Avoid being called recursively. */
1553 if (State & VREPLACE_FLAG)
1554 State = INSERT;
1555#endif
1556 shift_line(type == INDENT_DEC, round, 1);
1557#ifdef FEAT_VREPLACE
1558 State = save_State;
1559#endif
1560 }
1561 insstart_less -= curwin->w_cursor.col;
1562
1563 /*
1564 * Try to put cursor on same character.
1565 * If the cursor is at or after the first non-blank in the line,
1566 * compute the cursor column relative to the column of the first
1567 * non-blank character.
1568 * If we are not in insert mode, leave the cursor on the first non-blank.
1569 * If the cursor is before the first non-blank, position it relative
1570 * to the first non-blank, counted in screen columns.
1571 */
1572 if (new_cursor_col >= 0)
1573 {
1574 /*
1575 * When changing the indent while the cursor is touching it, reset
1576 * Insstart_col to 0.
1577 */
1578 if (new_cursor_col == 0)
1579 insstart_less = MAXCOL;
1580 new_cursor_col += curwin->w_cursor.col;
1581 }
1582 else if (!(State & INSERT))
1583 new_cursor_col = curwin->w_cursor.col;
1584 else
1585 {
1586 /*
1587 * Compute the screen column where the cursor should be.
1588 */
1589 vcol = get_indent() - vcol;
1590 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1591
1592 /*
1593 * Advance the cursor until we reach the right screen column.
1594 */
1595 vcol = last_vcol = 0;
1596 new_cursor_col = -1;
1597 ptr = ml_get_curline();
1598 while (vcol <= (int)curwin->w_virtcol)
1599 {
1600 last_vcol = vcol;
1601#ifdef FEAT_MBYTE
1602 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001603 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 else
1605#endif
1606 ++new_cursor_col;
1607 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1608 }
1609 vcol = last_vcol;
1610
1611 /*
1612 * May need to insert spaces to be able to position the cursor on
1613 * the right screen column.
1614 */
1615 if (vcol != (int)curwin->w_virtcol)
1616 {
1617 curwin->w_cursor.col = new_cursor_col;
1618 i = (int)curwin->w_virtcol - vcol;
1619 ptr = alloc(i + 1);
1620 if (ptr != NULL)
1621 {
1622 new_cursor_col += i;
1623 ptr[i] = NUL;
1624 while (--i >= 0)
1625 ptr[i] = ' ';
1626 ins_str(ptr);
1627 vim_free(ptr);
1628 }
1629 }
1630
1631 /*
1632 * When changing the indent while the cursor is in it, reset
1633 * Insstart_col to 0.
1634 */
1635 insstart_less = MAXCOL;
1636 }
1637
1638 curwin->w_p_list = save_p_list;
1639
1640 if (new_cursor_col <= 0)
1641 curwin->w_cursor.col = 0;
1642 else
1643 curwin->w_cursor.col = new_cursor_col;
1644 curwin->w_set_curswant = TRUE;
1645 changed_cline_bef_curs();
1646
1647 /*
1648 * May have to adjust the start of the insert.
1649 */
1650 if (State & INSERT)
1651 {
1652 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1653 {
1654 if ((int)Insstart.col <= insstart_less)
1655 Insstart.col = 0;
1656 else
1657 Insstart.col -= insstart_less;
1658 }
1659 if ((int)ai_col <= insstart_less)
1660 ai_col = 0;
1661 else
1662 ai_col -= insstart_less;
1663 }
1664
1665 /*
1666 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1667 * If the number of characters before the cursor decreased, need to pop a
1668 * few characters from the replace stack.
1669 * If the number of characters before the cursor increased, need to push a
1670 * few NULs onto the replace stack.
1671 */
1672 if (REPLACE_NORMAL(State) && start_col >= 0)
1673 {
1674 while (start_col > (int)curwin->w_cursor.col)
1675 {
1676 replace_join(0); /* remove a NUL from the replace stack */
1677 --start_col;
1678 }
1679 while (start_col < (int)curwin->w_cursor.col || replaced)
1680 {
1681 replace_push(NUL);
1682 if (replaced)
1683 {
1684 replace_push(replaced);
1685 replaced = NUL;
1686 }
1687 ++start_col;
1688 }
1689 }
1690
1691#ifdef FEAT_VREPLACE
1692 /*
1693 * For VREPLACE mode, we also have to fix the replace stack. In this case
1694 * it is always possible because we backspace over the whole line and then
1695 * put it back again the way we wanted it.
1696 */
1697 if (State & VREPLACE_FLAG)
1698 {
1699 /* If orig_line didn't allocate, just return. At least we did the job,
1700 * even if you can't backspace. */
1701 if (orig_line == NULL)
1702 return;
1703
1704 /* Save new line */
1705 new_line = vim_strsave(ml_get_curline());
1706 if (new_line == NULL)
1707 return;
1708
1709 /* We only put back the new line up to the cursor */
1710 new_line[curwin->w_cursor.col] = NUL;
1711
1712 /* Put back original line */
1713 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1714 curwin->w_cursor.col = orig_col;
1715
1716 /* Backspace from cursor to start of line */
1717 backspace_until_column(0);
1718
1719 /* Insert new stuff into line again */
1720 ins_bytes(new_line);
1721
1722 vim_free(new_line);
1723 }
1724#endif
1725}
1726
1727/*
1728 * Truncate the space at the end of a line. This is to be used only in an
1729 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1730 * modes.
1731 */
1732 void
1733truncate_spaces(line)
1734 char_u *line;
1735{
1736 int i;
1737
1738 /* find start of trailing white space */
1739 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1740 {
1741 if (State & REPLACE_FLAG)
1742 replace_join(0); /* remove a NUL from the replace stack */
1743 }
1744 line[i + 1] = NUL;
1745}
1746
1747#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1748 || defined(FEAT_COMMENTS) || defined(PROTO)
1749/*
1750 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1751 * modes correctly. May also be used when not in insert mode at all.
1752 */
1753 void
1754backspace_until_column(col)
1755 int col;
1756{
1757 while ((int)curwin->w_cursor.col > col)
1758 {
1759 curwin->w_cursor.col--;
1760 if (State & REPLACE_FLAG)
1761 replace_do_bs();
1762 else
1763 (void)del_char(FALSE);
1764 }
1765}
1766#endif
1767
1768#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1769/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001770 * CTRL-X pressed in Insert mode.
1771 */
1772 static void
1773ins_ctrl_x()
1774{
1775 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1776 * CTRL-V works like CTRL-N */
1777 if (ctrl_x_mode != CTRL_X_CMDLINE)
1778 {
1779 /* if the next ^X<> won't ADD nothing, then reset
1780 * compl_cont_status */
1781 if (compl_cont_status & CONT_N_ADDS)
1782 compl_cont_status = (compl_cont_status | CONT_INTRPT);
1783 else
1784 compl_cont_status = 0;
1785 /* We're not sure which CTRL-X mode it will be yet */
1786 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1787 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1788 edit_submode_pre = NULL;
1789 showmode();
1790 }
1791}
1792
1793/*
1794 * Return TRUE if the 'dict' or 'tsr' option can be used.
1795 */
1796 static int
1797has_compl_option(dict_opt)
1798 int dict_opt;
1799{
1800 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1801 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1802 {
1803 ctrl_x_mode = 0;
1804 edit_submode = NULL;
1805 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1806 : (char_u *)_("'thesaurus' option is empty"),
1807 hl_attr(HLF_E));
1808 if (emsg_silent == 0)
1809 {
1810 vim_beep();
1811 setcursor();
1812 out_flush();
1813 ui_delay(2000L, FALSE);
1814 }
1815 return FALSE;
1816 }
1817 return TRUE;
1818}
1819
1820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1822 * This depends on the current mode.
1823 */
1824 int
1825vim_is_ctrl_x_key(c)
1826 int c;
1827{
1828 /* Always allow ^R - let it's results then be checked */
1829 if (c == Ctrl_R)
1830 return TRUE;
1831
Bram Moolenaare3226be2005-12-18 22:10:00 +00001832 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
1833 if (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
1834 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN))
1835 return TRUE;
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 switch (ctrl_x_mode)
1838 {
1839 case 0: /* Not in any CTRL-X mode */
1840 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1841 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001842 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1844 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1845 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001846 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1847 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 case CTRL_X_SCROLL:
1849 return (c == Ctrl_Y || c == Ctrl_E);
1850 case CTRL_X_WHOLE_LINE:
1851 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1852 case CTRL_X_FILES:
1853 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1854 case CTRL_X_DICTIONARY:
1855 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1856 case CTRL_X_THESAURUS:
1857 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1858 case CTRL_X_TAGS:
1859 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1860#ifdef FEAT_FIND_ID
1861 case CTRL_X_PATH_PATTERNS:
1862 return (c == Ctrl_P || c == Ctrl_N);
1863 case CTRL_X_PATH_DEFINES:
1864 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1865#endif
1866 case CTRL_X_CMDLINE:
1867 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1868 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001869#ifdef FEAT_COMPL_FUNC
1870 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001871 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001872 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001873 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001874#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001875 case CTRL_X_SPELL:
1876 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 }
1878 EMSG(_(e_internal));
1879 return FALSE;
1880}
1881
1882/*
1883 * This is like ins_compl_add(), but if ic and inf are set, then the
1884 * case of the originally typed text is used, and the case of the completed
1885 * text is infered, ie this tries to work out what case you probably wanted
1886 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001887 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 */
1889 int
Bram Moolenaar572cb562005-08-05 21:35:02 +00001890ins_compl_add_infercase(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 char_u *str;
1892 int len;
1893 char_u *fname;
1894 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001895 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 int has_lower = FALSE;
1898 int was_letter = FALSE;
1899 int idx;
1900
1901 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
1902 {
1903 /* Infer case of completed part -- webb */
1904 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001905 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906
1907 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001908 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001910 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 {
1912 has_lower = TRUE;
1913 if (isupper(IObuff[idx]))
1914 {
1915 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001916 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
1918 break;
1919 }
1920 }
1921 }
1922
1923 /*
1924 * Rule 2: No lower case, 2nd consecutive letter converted to
1925 * upper case.
1926 */
1927 if (!has_lower)
1928 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001929 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001931 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 && islower(IObuff[idx]))
1933 {
1934 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001935 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
1937 break;
1938 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001939 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 }
1941 }
1942
1943 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001944 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945
Bram Moolenaar572cb562005-08-05 21:35:02 +00001946 return ins_compl_add(IObuff, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00001948 return ins_compl_add(str, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949}
1950
1951/*
1952 * Add a match to the list of matches.
1953 * If the given string is already in the list of completions, then return
1954 * FAIL, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaar572cb562005-08-05 21:35:02 +00001955 * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb.
1956 *
1957 * New:
1958 * If the given string is already in the list of completions, then return
1959 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
1960 * maybe because alloc() returns NULL, then FAIL is returned -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00001962 int
1963ins_compl_add(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 char_u *str;
1965 int len;
1966 char_u *fname;
1967 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001968 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969{
Bram Moolenaar572cb562005-08-05 21:35:02 +00001970 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
1972 ui_breakcheck();
1973 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00001974 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 if (len < 0)
1976 len = (int)STRLEN(str);
1977
1978 /*
1979 * If the same match is already present, don't add it.
1980 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001981 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001983 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 do
1985 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00001986 if ( !(match->cp_flags & ORIGINAL_TEXT)
1987 && STRNCMP(match->cp_str, str, (size_t)len) == 0
1988 && match->cp_str[len] == NUL)
1989 return NOTDONE;
1990 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001991 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 }
1993
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001994 /* Remove any popup menu before changing the list of matches. */
1995 ins_compl_del_pum();
1996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 /*
1998 * Allocate a new match structure.
1999 * Copy the values to the new match structure.
2000 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002001 match = (compl_T *)alloc((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002003 return FAIL;
2004 match->cp_number = -1;
2005 if (flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002007 match->cp_number = 0;
2008 match->cp_str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002010 else if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 {
2012 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002013 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 }
2015 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002016 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2018 * - NULL otherwise. --Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002019 if (fname && compl_curr_match && compl_curr_match->cp_fname
2020 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
2021 match->cp_fname = compl_curr_match->cp_fname;
2022 else if (fname && (match->cp_fname = vim_strsave(fname)) != NULL)
2023 flags |= FREE_FNAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002025 match->cp_fname = NULL;
2026 match->cp_flags = flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
2028 /*
2029 * Link the new match structure in the list of matches.
2030 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002031 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002032 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 else if (dir == FORWARD)
2034 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002035 match->cp_next = compl_curr_match->cp_next;
2036 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
2038 else /* BACKWARD */
2039 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002040 match->cp_next = compl_curr_match;
2041 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002043 if (match->cp_next)
2044 match->cp_next->cp_prev = match;
2045 if (match->cp_prev)
2046 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002048 compl_first_match = match;
2049 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 return OK;
2052}
2053
2054/*
2055 * Add an array of matches to the list of matches.
2056 * Frees matches[].
2057 */
2058 static void
2059ins_compl_add_matches(num_matches, matches, dir)
2060 int num_matches;
2061 char_u **matches;
2062 int dir;
2063{
2064 int i;
2065 int add_r = OK;
2066 int ldir = dir;
2067
Bram Moolenaar572cb562005-08-05 21:35:02 +00002068 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 if ((add_r = ins_compl_add(matches[i], -1, NULL, ldir, 0)) == OK)
2070 /* if dir was BACKWARD then honor it just once */
2071 ldir = FORWARD;
2072 FreeWild(num_matches, matches);
2073}
2074
2075/* Make the completion list cyclic.
2076 * Return the number of matches (excluding the original).
2077 */
2078 static int
2079ins_compl_make_cyclic()
2080{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002081 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 int count = 0;
2083
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002084 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
2086 /*
2087 * Find the end of the list.
2088 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002089 match = compl_first_match;
2090 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002091 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002093 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 ++count;
2095 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002096 match->cp_next = compl_first_match;
2097 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 }
2099 return count;
2100}
2101
Bram Moolenaar9372a112005-12-06 19:59:18 +00002102/* "compl_match_array" points the currently displayed list of entries in the
2103 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002104static char_u **compl_match_array = NULL;
2105static int compl_match_arraysize;
2106
2107/*
2108 * Update the screen and when there is any scrolling remove the popup menu.
2109 */
2110 static void
2111ins_compl_upd_pum()
2112{
2113 int h;
2114
2115 if (compl_match_array != NULL)
2116 {
2117 h = curwin->w_cline_height;
2118 update_screen(0);
2119 if (h != curwin->w_cline_height)
2120 ins_compl_del_pum();
2121 }
2122}
2123
2124/*
2125 * Remove any popup menu.
2126 */
2127 static void
2128ins_compl_del_pum()
2129{
2130 if (compl_match_array != NULL)
2131 {
2132 pum_undisplay();
2133 vim_free(compl_match_array);
2134 compl_match_array = NULL;
2135 }
2136}
2137
2138/*
2139 * Return TRUE if the popup menu should be displayed.
2140 */
2141 static int
2142pum_wanted()
2143{
2144 compl_T *compl;
2145 int i;
2146
2147 /* 'completeopt' must contain "menu" */
2148 if (*p_cot == NUL)
2149 return FALSE;
2150
2151 /* The display looks bad on a B&W display. */
2152 if (t_colors < 8
2153#ifdef FEAT_GUI
2154 && !gui.in_use
2155#endif
2156 )
2157 return FALSE;
2158
2159 /* Don't display the popup menu if there are no matches or there is only
2160 * one (ignoring the original text). */
2161 compl = compl_first_match;
2162 i = 0;
2163 do
2164 {
2165 if (compl == NULL
2166 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2167 break;
2168 compl = compl->cp_next;
2169 } while (compl != compl_first_match);
2170
2171 return (i >= 2);
2172}
2173
2174/*
2175 * Show the popup menu for the list of matches.
2176 */
2177 static void
2178ins_compl_show_pum()
2179{
2180 compl_T *compl;
2181 int i;
2182 int cur = -1;
2183 colnr_T col;
2184
2185 if (!pum_wanted())
2186 return;
2187
2188 /* Update the screen before drawing the popup menu over it. */
2189 update_screen(0);
2190
2191 if (compl_match_array == NULL)
2192 {
2193 /* Need to build the popup menu list. */
2194 compl_match_arraysize = 0;
2195 compl = compl_first_match;
2196 do
2197 {
2198 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2199 ++compl_match_arraysize;
2200 compl = compl->cp_next;
2201 } while (compl != NULL && compl != compl_first_match);
2202 compl_match_array = (char_u **)alloc((unsigned)(sizeof(char_u **)
2203 * compl_match_arraysize));
2204 if (compl_match_array != NULL)
2205 {
2206 i = 0;
2207 compl = compl_first_match;
2208 do
2209 {
2210 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2211 {
2212 if (compl == compl_shown_match)
2213 cur = i;
2214 compl_match_array[i++] = compl->cp_str;
2215 }
2216 compl = compl->cp_next;
2217 } while (compl != NULL && compl != compl_first_match);
2218 }
2219 }
2220 else
2221 {
2222 /* popup menu already exists, only need to find the current item.*/
2223 i = 0;
2224 compl = compl_first_match;
2225 do
2226 {
2227 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2228 {
2229 if (compl == compl_shown_match)
2230 {
2231 cur = i;
2232 break;
2233 }
2234 ++i;
2235 }
2236 compl = compl->cp_next;
2237 } while (compl != NULL && compl != compl_first_match);
2238 }
2239
2240 if (compl_match_array != NULL)
2241 {
2242 /* Compute the screen column of the start of the completed text.
2243 * Use the cursor to get all wrapping and other settings right. */
2244 col = curwin->w_cursor.col;
2245 curwin->w_cursor.col = compl_col;
2246 validate_cursor_col();
2247 pum_display(compl_match_array, compl_match_arraysize, cur,
2248 curwin->w_cline_row + W_WINROW(curwin),
2249 curwin->w_cline_height,
2250 curwin->w_wcol + W_WINCOL(curwin));
2251 curwin->w_cursor.col = col;
2252 }
2253}
2254
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255#define DICT_FIRST (1) /* use just first element in "dict" */
2256#define DICT_EXACT (2) /* "dict" is the exact name of a file */
2257/*
2258 * Add any identifiers that match the given pattern to the list of
2259 * completions.
2260 */
2261 static void
2262ins_compl_dictionaries(dict, pat, dir, flags, thesaurus)
2263 char_u *dict;
2264 char_u *pat;
2265 int dir;
2266 int flags;
2267 int thesaurus;
2268{
2269 char_u *ptr;
2270 char_u *buf;
2271 FILE *fp;
2272 regmatch_T regmatch;
2273 int add_r;
2274 char_u **files;
2275 int count;
2276 int i;
2277 int save_p_scs;
2278
2279 buf = alloc(LSIZE);
2280 /* If 'infercase' is set, don't use 'smartcase' here */
2281 save_p_scs = p_scs;
2282 if (curbuf->b_p_inf)
2283 p_scs = FALSE;
2284 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2285 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2286 regmatch.rm_ic = ignorecase(pat);
2287 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002288 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 /* copy one dictionary file name into buf */
2291 if (flags == DICT_EXACT)
2292 {
2293 count = 1;
2294 files = &dict;
2295 }
2296 else
2297 {
2298 /* Expand wildcards in the dictionary name, but do not allow
2299 * backticks (for security, the 'dict' option may have been set in
2300 * a modeline). */
2301 copy_option_part(&dict, buf, LSIZE, ",");
2302 if (vim_strchr(buf, '`') != NULL
2303 || expand_wildcards(1, &buf, &count, &files,
2304 EW_FILE|EW_SILENT) != OK)
2305 count = 0;
2306 }
2307
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002308 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
2310 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2311 if (flags != DICT_EXACT)
2312 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002313 vim_snprintf((char *)IObuff, IOSIZE,
2314 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2316 }
2317
2318 if (fp != NULL)
2319 {
2320 /*
2321 * Read dictionary file line by line.
2322 * Check each line for a match.
2323 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002324 while (!got_int && !compl_interrupted
2325 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
2327 ptr = buf;
2328 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2329 {
2330 ptr = regmatch.startp[0];
2331 ptr = find_word_end(ptr);
2332 add_r = ins_compl_add_infercase(regmatch.startp[0],
2333 (int)(ptr - regmatch.startp[0]),
2334 files[i], dir, 0);
2335 if (thesaurus)
2336 {
2337 char_u *wstart;
2338
2339 /*
2340 * Add the other matches on the line
2341 */
2342 while (!got_int)
2343 {
2344 /* Find start of the next word. Skip white
2345 * space and punctuation. */
2346 ptr = find_word_start(ptr);
2347 if (*ptr == NUL || *ptr == NL)
2348 break;
2349 wstart = ptr;
2350
2351 /* Find end of the word and add it. */
2352#ifdef FEAT_MBYTE
2353 if (has_mbyte)
2354 /* Japanese words may have characters in
2355 * different classes, only separate words
2356 * with single-byte non-word characters. */
2357 while (*ptr != NUL)
2358 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002359 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360
2361 if (l < 2 && !vim_iswordc(*ptr))
2362 break;
2363 ptr += l;
2364 }
2365 else
2366#endif
2367 ptr = find_word_end(ptr);
2368 add_r = ins_compl_add_infercase(wstart,
2369 (int)(ptr - wstart), files[i], dir, 0);
2370 }
2371 }
2372 if (add_r == OK)
2373 /* if dir was BACKWARD then honor it just once */
2374 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002375 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 break;
2377 /* avoid expensive call to vim_regexec() when at end
2378 * of line */
2379 if (*ptr == '\n' || got_int)
2380 break;
2381 }
2382 line_breakcheck();
Bram Moolenaar572cb562005-08-05 21:35:02 +00002383 ins_compl_check_keys(50);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 }
2385 fclose(fp);
2386 }
2387 }
2388 if (flags != DICT_EXACT)
2389 FreeWild(count, files);
2390 if (flags)
2391 break;
2392 }
2393 p_scs = save_p_scs;
2394 vim_free(regmatch.regprog);
2395 vim_free(buf);
2396}
2397
2398/*
2399 * Find the start of the next word.
2400 * Returns a pointer to the first char of the word. Also stops at a NUL.
2401 */
2402 char_u *
2403find_word_start(ptr)
2404 char_u *ptr;
2405{
2406#ifdef FEAT_MBYTE
2407 if (has_mbyte)
2408 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002409 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 else
2411#endif
2412 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2413 ++ptr;
2414 return ptr;
2415}
2416
2417/*
2418 * Find the end of the word. Assumes it starts inside a word.
2419 * Returns a pointer to just after the word.
2420 */
2421 char_u *
2422find_word_end(ptr)
2423 char_u *ptr;
2424{
2425#ifdef FEAT_MBYTE
2426 int start_class;
2427
2428 if (has_mbyte)
2429 {
2430 start_class = mb_get_class(ptr);
2431 if (start_class > 1)
2432 while (*ptr != NUL)
2433 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002434 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 if (mb_get_class(ptr) != start_class)
2436 break;
2437 }
2438 }
2439 else
2440#endif
2441 while (vim_iswordc(*ptr))
2442 ++ptr;
2443 return ptr;
2444}
2445
2446/*
2447 * Free the list of completions
2448 */
2449 static void
2450ins_compl_free()
2451{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002452 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002454 vim_free(compl_pattern);
2455 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002457 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002459
2460 ins_compl_del_pum();
2461 pum_clear();
2462
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002463 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 do
2465 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002466 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002467 compl_curr_match = compl_curr_match->cp_next;
2468 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002470 if (match->cp_flags & FREE_FNAME)
2471 vim_free(match->cp_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002473 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2474 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475}
2476
2477 static void
2478ins_compl_clear()
2479{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002480 compl_cont_status = 0;
2481 compl_started = FALSE;
2482 compl_matches = 0;
2483 vim_free(compl_pattern);
2484 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 save_sm = -1;
2486 edit_submode_extra = NULL;
2487}
2488
2489/*
2490 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002491 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002492 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002494 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495ins_compl_prep(c)
2496 int c;
2497{
2498 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 int temp;
2500 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002501 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503 /* Forget any previous 'special' messages if this is actually
2504 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2505 */
2506 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2507 edit_submode_extra = NULL;
2508
2509 /* Ignore end of Select mode mapping */
2510 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002511 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
2513 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2514 {
2515 /*
2516 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2517 * it will be yet. Now we decide.
2518 */
2519 switch (c)
2520 {
2521 case Ctrl_E:
2522 case Ctrl_Y:
2523 ctrl_x_mode = CTRL_X_SCROLL;
2524 if (!(State & REPLACE_FLAG))
2525 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2526 else
2527 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2528 edit_submode_pre = NULL;
2529 showmode();
2530 break;
2531 case Ctrl_L:
2532 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2533 break;
2534 case Ctrl_F:
2535 ctrl_x_mode = CTRL_X_FILES;
2536 break;
2537 case Ctrl_K:
2538 ctrl_x_mode = CTRL_X_DICTIONARY;
2539 break;
2540 case Ctrl_R:
2541 /* Simply allow ^R to happen without affecting ^X mode */
2542 break;
2543 case Ctrl_T:
2544 ctrl_x_mode = CTRL_X_THESAURUS;
2545 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002546#ifdef FEAT_COMPL_FUNC
2547 case Ctrl_U:
2548 ctrl_x_mode = CTRL_X_FUNCTION;
2549 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002550 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002551 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002552 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002553#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002554 case 's':
2555 case Ctrl_S:
2556 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002557#ifdef FEAT_SYN_HL
2558 spell_back_to_badword();
2559#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002560 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 case Ctrl_RSB:
2562 ctrl_x_mode = CTRL_X_TAGS;
2563 break;
2564#ifdef FEAT_FIND_ID
2565 case Ctrl_I:
2566 case K_S_TAB:
2567 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2568 break;
2569 case Ctrl_D:
2570 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2571 break;
2572#endif
2573 case Ctrl_V:
2574 case Ctrl_Q:
2575 ctrl_x_mode = CTRL_X_CMDLINE;
2576 break;
2577 case Ctrl_P:
2578 case Ctrl_N:
2579 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
2580 * just started ^X mode, or there were enough ^X's to cancel
2581 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2582 * do normal expansion when interrupting a different mode (say
2583 * ^X^F^X^P or ^P^X^X^P, see below)
2584 * nothing changes if interrupting mode 0, (eg, the flag
2585 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002586 if (!(compl_cont_status & CONT_INTRPT))
2587 compl_cont_status |= CONT_LOCAL;
2588 else if (compl_cont_mode != 0)
2589 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 /* FALLTHROUGH */
2591 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002592 /* If we have typed at least 2 ^X's... for modes != 0, we set
2593 * compl_cont_status = 0 (eg, as if we had just started ^X
2594 * mode).
2595 * For mode 0, we set "compl_cont_mode" to an impossible
2596 * value, in both cases ^X^X can be used to restart the same
2597 * mode (avoiding ADDING mode).
2598 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2599 * 'complete' and local ^P expansions respectively.
2600 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2601 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 if (c == Ctrl_X)
2603 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002604 if (compl_cont_mode != 0)
2605 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002607 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 }
2609 ctrl_x_mode = 0;
2610 edit_submode = NULL;
2611 showmode();
2612 break;
2613 }
2614 }
2615 else if (ctrl_x_mode != 0)
2616 {
2617 /* We're already in CTRL-X mode, do we stay in it? */
2618 if (!vim_is_ctrl_x_key(c))
2619 {
2620 if (ctrl_x_mode == CTRL_X_SCROLL)
2621 ctrl_x_mode = 0;
2622 else
2623 ctrl_x_mode = CTRL_X_FINISHED;
2624 edit_submode = NULL;
2625 }
2626 showmode();
2627 }
2628
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002629 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
2631 /* Show error message from attempted keyword completion (probably
2632 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002633 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 showmode();
2635 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R)
2636 || ctrl_x_mode == CTRL_X_FINISHED)
2637 {
2638 /* Get here when we have finished typing a sequence of ^N and
2639 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002640 * memory that was used, and make sure we can redo the insert. */
2641 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002643 char_u *p;
2644
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 /*
2646 * If any of the original typed text has been changed,
2647 * eg when ignorecase is set, we must add back-spaces to
2648 * the redo buffer. We add as few as necessary to delete
2649 * just the part of the original text that has changed.
2650 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002651 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002652 p = compl_orig_text;
2653 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002655 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 ++ptr;
2657 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002658 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 AppendCharToRedobuff(K_BS);
2660 AppendToRedobuffLit(ptr);
2661 }
2662
2663#ifdef FEAT_CINDENT
2664 want_cindent = (can_cindent && cindent_on());
2665#endif
2666 /*
2667 * When completing whole lines: fix indent for 'cindent'.
2668 * Otherwise, break line if it's too long.
2669 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002670 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {
2672#ifdef FEAT_CINDENT
2673 /* re-indent the current line */
2674 if (want_cindent)
2675 {
2676 do_c_expr_indent();
2677 want_cindent = FALSE; /* don't do it again */
2678 }
2679#endif
2680 }
2681 else
2682 {
2683 /* put the cursor on the last char, for 'tw' formatting */
2684 curwin->w_cursor.col--;
2685 if (stop_arrow() == OK)
2686 insertchar(NUL, 0, -1);
2687 curwin->w_cursor.col++;
2688 }
2689
2690 auto_format(FALSE, TRUE);
2691
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002692 /* if the popup menu is displayed hitting Enter means accepting
2693 * the selection without inserting anything. */
2694 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
2695 retval = TRUE;
2696
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002698 compl_started = FALSE;
2699 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 msg_clr_cmdline(); /* necessary for "noshowmode" */
2701 ctrl_x_mode = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002702 if (save_sm >= 0)
2703 p_sm = save_sm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if (edit_submode != NULL)
2705 {
2706 edit_submode = NULL;
2707 showmode();
2708 }
2709
2710#ifdef FEAT_CINDENT
2711 /*
2712 * Indent now if a key was typed that is in 'cinkeys'.
2713 */
2714 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2715 do_c_expr_indent();
2716#endif
2717 }
2718 }
2719
2720 /* reset continue_* if we left expansion-mode, if we stay they'll be
2721 * (re)set properly in ins_complete() */
2722 if (!vim_is_ctrl_x_key(c))
2723 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002724 compl_cont_status = 0;
2725 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002727
2728 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729}
2730
2731/*
2732 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2733 * (depending on flag) starting from buf and looking for a non-scanned
2734 * buffer (other than curbuf). curbuf is special, if it is called with
2735 * buf=curbuf then it has to be the first call for a given flag/expansion.
2736 *
2737 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2738 */
2739 static buf_T *
2740ins_compl_next_buf(buf, flag)
2741 buf_T *buf;
2742 int flag;
2743{
2744#ifdef FEAT_WINDOWS
2745 static win_T *wp;
2746#endif
2747
2748 if (flag == 'w') /* just windows */
2749 {
2750#ifdef FEAT_WINDOWS
2751 if (buf == curbuf) /* first call for this flag/expansion */
2752 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002753 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 && wp->w_buffer->b_scanned)
2755 ;
2756 buf = wp->w_buffer;
2757#else
2758 buf = curbuf;
2759#endif
2760 }
2761 else
2762 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2763 * (unlisted buffers)
2764 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002765 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 && ((flag == 'U'
2767 ? buf->b_p_bl
2768 : (!buf->b_p_bl
2769 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2770 || buf->b_scanned
2771 || (buf->b_ml.ml_mfp == NULL
2772 && ctrl_x_mode == CTRL_X_WHOLE_LINE)))
2773 ;
2774 return buf;
2775}
2776
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002777#ifdef FEAT_COMPL_FUNC
Bram Moolenaare344bea2005-09-01 20:46:49 +00002778static int expand_by_function __ARGS((int type, char_u *base, char_u ***matches));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002779
2780/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002781 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00002782 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002783 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002784 */
2785 static int
Bram Moolenaare344bea2005-09-01 20:46:49 +00002786expand_by_function(type, base, matches)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002787 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002788 char_u *base;
2789 char_u ***matches;
2790{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002791 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002792 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002793 listitem_T *li;
2794 garray_T ga;
2795 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002796 char_u *funcname;
2797 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002798
Bram Moolenaare344bea2005-09-01 20:46:49 +00002799 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
2800 if (*funcname == NUL)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002801 return 0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002802
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002803 /* Call 'completefunc' to obtain the list of matches. */
2804 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00002805 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002806
Bram Moolenaare344bea2005-09-01 20:46:49 +00002807 pos = curwin->w_cursor;
2808 matchlist = call_func_retlist(funcname, 2, args, FALSE);
2809 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002810 if (matchlist == NULL)
2811 return 0;
2812
2813 /* Go through the List with matches and put them in an array. */
2814 ga_init2(&ga, (int)sizeof(char_u *), 8);
2815 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002816 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002817 p = get_tv_string_chk(&li->li_tv);
2818 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002819 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002820 if (ga_grow(&ga, 1) == FAIL)
2821 break;
2822 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p);
2823 ++ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002824 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002825 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002826
2827 list_unref(matchlist);
2828 *matches = (char_u **)ga.ga_data;
2829 return ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002830}
2831#endif /* FEAT_COMPL_FUNC */
2832
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002833/*
2834 * Get the next expansion(s), using "compl_pattern".
2835 * The search starts at position "ini" in curbuf and in the direction dir.
2836 * When "compl_started" is FALSE start at that position, otherwise
2837 * continue where we stopped searching before.
2838 * This may return before finding all the matches.
2839 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 */
2841 static int
2842ins_compl_get_exp(ini, dir)
2843 pos_T *ini;
2844 int dir;
2845{
2846 static pos_T first_match_pos;
2847 static pos_T last_match_pos;
2848 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002849 static int found_all = FALSE; /* Found all matches of a
2850 certain type. */
2851 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852
Bram Moolenaar572cb562005-08-05 21:35:02 +00002853 pos_T *pos;
2854 char_u **matches;
2855 int save_p_scs;
2856 int save_p_ws;
2857 int save_p_ic;
2858 int i;
2859 int num_matches;
2860 int len;
2861 int found_new_match;
2862 int type = ctrl_x_mode;
2863 char_u *ptr;
2864 char_u *dict = NULL;
2865 int dict_f = 0;
2866 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002868 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {
2870 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
2871 ins_buf->b_scanned = 0;
2872 found_all = FALSE;
2873 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002874 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002875 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 last_match_pos = first_match_pos = *ini;
2877 }
2878
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002879 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 pos = (dir == FORWARD) ? &last_match_pos : &first_match_pos;
2881 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
2882 for (;;)
2883 {
2884 found_new_match = FAIL;
2885
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002886 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 * or if found_all says this entry is done. For ^X^L only use the
2888 * entries from 'complete' that look in loaded buffers. */
2889 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002890 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 {
2892 found_all = FALSE;
2893 while (*e_cpt == ',' || *e_cpt == ' ')
2894 e_cpt++;
2895 if (*e_cpt == '.' && !curbuf->b_scanned)
2896 {
2897 ins_buf = curbuf;
2898 first_match_pos = *ini;
2899 /* So that ^N can match word immediately after cursor */
2900 if (ctrl_x_mode == 0)
2901 dec(&first_match_pos);
2902 last_match_pos = first_match_pos;
2903 type = 0;
2904 }
2905 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2906 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2907 {
2908 /* Scan a buffer, but not the current one. */
2909 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
2910 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002911 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 first_match_pos.col = last_match_pos.col = 0;
2913 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2914 last_match_pos.lnum = 0;
2915 type = 0;
2916 }
2917 else /* unloaded buffer, scan like dictionary */
2918 {
2919 found_all = TRUE;
2920 if (ins_buf->b_fname == NULL)
2921 continue;
2922 type = CTRL_X_DICTIONARY;
2923 dict = ins_buf->b_fname;
2924 dict_f = DICT_EXACT;
2925 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00002926 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 ins_buf->b_fname == NULL
2928 ? buf_spname(ins_buf)
2929 : ins_buf->b_sfname == NULL
2930 ? (char *)ins_buf->b_fname
2931 : (char *)ins_buf->b_sfname);
2932 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2933 }
2934 else if (*e_cpt == NUL)
2935 break;
2936 else
2937 {
2938 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2939 type = -1;
2940 else if (*e_cpt == 'k' || *e_cpt == 's')
2941 {
2942 if (*e_cpt == 'k')
2943 type = CTRL_X_DICTIONARY;
2944 else
2945 type = CTRL_X_THESAURUS;
2946 if (*++e_cpt != ',' && *e_cpt != NUL)
2947 {
2948 dict = e_cpt;
2949 dict_f = DICT_FIRST;
2950 }
2951 }
2952#ifdef FEAT_FIND_ID
2953 else if (*e_cpt == 'i')
2954 type = CTRL_X_PATH_PATTERNS;
2955 else if (*e_cpt == 'd')
2956 type = CTRL_X_PATH_DEFINES;
2957#endif
2958 else if (*e_cpt == ']' || *e_cpt == 't')
2959 {
2960 type = CTRL_X_TAGS;
2961 sprintf((char*)IObuff, _("Scanning tags."));
2962 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2963 }
2964 else
2965 type = -1;
2966
2967 /* in any case e_cpt is advanced to the next entry */
2968 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2969
2970 found_all = TRUE;
2971 if (type == -1)
2972 continue;
2973 }
2974 }
2975
2976 switch (type)
2977 {
2978 case -1:
2979 break;
2980#ifdef FEAT_FIND_ID
2981 case CTRL_X_PATH_PATTERNS:
2982 case CTRL_X_PATH_DEFINES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002983 find_pattern_in_path(compl_pattern, dir,
2984 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002986 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
2988 (linenr_T)1, (linenr_T)MAXLNUM);
2989 break;
2990#endif
2991
2992 case CTRL_X_DICTIONARY:
2993 case CTRL_X_THESAURUS:
2994 ins_compl_dictionaries(
2995 dict ? dict
2996 : (type == CTRL_X_THESAURUS
2997 ? (*curbuf->b_p_tsr == NUL
2998 ? p_tsr
2999 : curbuf->b_p_tsr)
3000 : (*curbuf->b_p_dict == NUL
3001 ? p_dict
3002 : curbuf->b_p_dict)),
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003003 compl_pattern, dir,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
3005 dict = NULL;
3006 break;
3007
3008 case CTRL_X_TAGS:
3009 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3010 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003011 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012
3013 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003014 * of matches is found when compl_pattern is empty */
3015 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3017 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3018 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3019 {
3020 ins_compl_add_matches(num_matches, matches, dir);
3021 }
3022 p_ic = save_p_ic;
3023 break;
3024
3025 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003026 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3028 {
3029
3030 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003031 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 ins_compl_add_matches(num_matches, matches, dir);
3033 }
3034 break;
3035
3036 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003037 if (expand_cmdline(&compl_xp, compl_pattern,
3038 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 &num_matches, &matches) == EXPAND_OK)
3040 ins_compl_add_matches(num_matches, matches, dir);
3041 break;
3042
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003043#ifdef FEAT_COMPL_FUNC
3044 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003045 case CTRL_X_OMNI:
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003046 num_matches = expand_by_function(type, compl_pattern, &matches);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003047 if (num_matches > 0)
3048 ins_compl_add_matches(num_matches, matches, dir);
3049 break;
3050#endif
3051
Bram Moolenaar488c6512005-08-11 20:09:58 +00003052 case CTRL_X_SPELL:
3053#ifdef FEAT_SYN_HL
3054 num_matches = expand_spelling(first_match_pos.lnum,
3055 first_match_pos.col, compl_pattern, &matches);
3056 if (num_matches > 0)
3057 ins_compl_add_matches(num_matches, matches, dir);
3058#endif
3059 break;
3060
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 default: /* normal ^P/^N and ^X^L */
3062 /*
3063 * If 'infercase' is set, don't use 'smartcase' here
3064 */
3065 save_p_scs = p_scs;
3066 if (ins_buf->b_p_inf)
3067 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003068
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 /* buffers other than curbuf are scanned from the beginning or the
3070 * end but never from the middle, thus setting nowrapscan in this
3071 * buffers is a good idea, on the other hand, we always set
3072 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3073 save_p_ws = p_ws;
3074 if (ins_buf != curbuf)
3075 p_ws = FALSE;
3076 else if (*e_cpt == '.')
3077 p_ws = TRUE;
3078 for (;;)
3079 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003080 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003082 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3083 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003085 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003087 dir, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 else
3089 found_new_match = searchit(NULL, ins_buf, pos, dir,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003090 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 RE_LAST);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003092 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003094 /* set compl_started even on fail */
3095 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 first_match_pos = *pos;
3097 last_match_pos = *pos;
3098 }
3099 else if (first_match_pos.lnum == last_match_pos.lnum
3100 && first_match_pos.col == last_match_pos.col)
3101 found_new_match = FAIL;
3102 if (found_new_match == FAIL)
3103 {
3104 if (ins_buf == curbuf)
3105 found_all = TRUE;
3106 break;
3107 }
3108
3109 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003110 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 && ini->lnum == pos->lnum
3112 && ini->col == pos->col)
3113 continue;
3114 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3115 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3116 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003117 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 {
3119 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3120 continue;
3121 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3122 if (!p_paste)
3123 ptr = skipwhite(ptr);
3124 }
3125 len = (int)STRLEN(ptr);
3126 }
3127 else
3128 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003129 char_u *tmp_ptr = ptr;
3130
3131 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003133 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 /* Skip if already inside a word. */
3135 if (vim_iswordp(tmp_ptr))
3136 continue;
3137 /* Find start of next word. */
3138 tmp_ptr = find_word_start(tmp_ptr);
3139 }
3140 /* Find end of this word. */
3141 tmp_ptr = find_word_end(tmp_ptr);
3142 len = (int)(tmp_ptr - ptr);
3143
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003144 if ((compl_cont_status & CONT_ADDING)
3145 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 {
3147 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3148 {
3149 /* Try next line, if any. the new word will be
3150 * "join" as if the normal command "J" was used.
3151 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003152 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 * works -- Acevedo */
3154 STRNCPY(IObuff, ptr, len);
3155 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3156 tmp_ptr = ptr = skipwhite(ptr);
3157 /* Find start of next word. */
3158 tmp_ptr = find_word_start(tmp_ptr);
3159 /* Find end of next word. */
3160 tmp_ptr = find_word_end(tmp_ptr);
3161 if (tmp_ptr > ptr)
3162 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003163 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003165 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 IObuff[len++] = ' ';
3167 /* IObuf =~ "\k.* ", thus len >= 2 */
3168 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003169 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 || (vim_strchr(p_cpo, CPO_JOINSP)
3171 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003172 && (IObuff[len - 2] == '?'
3173 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 IObuff[len++] = ' ';
3175 }
3176 /* copy as much as posible of the new word */
3177 if (tmp_ptr - ptr >= IOSIZE - len)
3178 tmp_ptr = ptr + IOSIZE - len - 1;
3179 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3180 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003181 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 }
3183 IObuff[len] = NUL;
3184 ptr = IObuff;
3185 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003186 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 continue;
3188 }
3189 }
3190 if (ins_compl_add_infercase(ptr, len,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003191 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar572cb562005-08-05 21:35:02 +00003192 dir, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 {
3194 found_new_match = OK;
3195 break;
3196 }
3197 }
3198 p_scs = save_p_scs;
3199 p_ws = save_p_ws;
3200 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003201
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003202 /* check if compl_curr_match has changed, (e.g. other type of
3203 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003204 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 found_new_match = OK;
3206
3207 /* break the loop for specialized modes (use 'complete' just for the
3208 * generic ctrl_x_mode == 0) or when we've found a new match */
3209 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003210 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003211 {
3212 if (got_int)
3213 break;
3214 if (pum_wanted() && type != -1)
3215 /* Fill the popup menu as soon as possible. */
3216 ins_compl_check_keys(0);
3217 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3218 || compl_interrupted)
3219 break;
3220 compl_started = TRUE;
3221 }
3222 else
3223 {
3224 /* Mark a buffer scanned when it has been scanned completely */
3225 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3226 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003228 compl_started = FALSE;
3229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003231 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232
3233 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3234 && *e_cpt == NUL) /* Got to end of 'complete' */
3235 found_new_match = FAIL;
3236
3237 i = -1; /* total of matches, unknown */
3238 if (found_new_match == FAIL
3239 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3240 i = ins_compl_make_cyclic();
3241
3242 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003243 * just been made cyclic then we have to move compl_curr_match to the next
3244 * or previous entry (if any) -- Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003245 compl_curr_match = dir == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003246 if (compl_curr_match == NULL)
3247 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 return i;
3249}
3250
3251/* Delete the old text being completed. */
3252 static void
3253ins_compl_delete()
3254{
3255 int i;
3256
3257 /*
3258 * In insert mode: Delete the typed part.
3259 * In replace mode: Put the old characters back, if any.
3260 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003261 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 backspace_until_column(i);
3263 changed_cline_bef_curs();
3264}
3265
3266/* Insert the new text being completed. */
3267 static void
3268ins_compl_insert()
3269{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003270 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271}
3272
3273/*
3274 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003275 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3276 * get more completions. If it is FALSE, then we just do nothing when there
3277 * are no more completions in a given direction. The latter case is used when
3278 * we are still in the middle of finding completions, to allow browsing
3279 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 * Return the total number of matches, or -1 if still unknown -- webb.
3281 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003282 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3283 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 *
3285 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003286 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3287 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 */
3289 static int
Bram Moolenaare3226be2005-12-18 22:10:00 +00003290ins_compl_next(allow_get_expansion, count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003292 int count; /* repeat completion this many times; should
3293 be at least 1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295 int num_matches = -1;
3296 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003297 int todo = count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298
3299 if (allow_get_expansion)
3300 {
3301 /* Delete old text to be replaced */
3302 ins_compl_delete();
3303 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003304 compl_pending = FALSE;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003305
3306 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3307 * around. */
3308 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003310 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003312 compl_shown_match = compl_shown_match->cp_next;
3313 if (compl_shown_match->cp_next != NULL
3314 && compl_shown_match->cp_next == compl_first_match)
3315 break;
3316 }
3317 else if (compl_shows_dir == BACKWARD
3318 && compl_shown_match->cp_prev != NULL)
3319 {
3320 compl_shown_match = compl_shown_match->cp_prev;
3321 if (compl_shown_match == compl_first_match)
3322 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 }
3324 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003325 {
3326 compl_pending = TRUE;
3327 if (allow_get_expansion)
3328 {
3329 num_matches = ins_compl_get_exp(&compl_startpos,
3330 compl_direction);
3331 if (compl_pending)
3332 {
3333 if (compl_direction == compl_shows_dir)
3334 compl_shown_match = compl_curr_match;
3335 }
3336 }
3337 else
3338 return -1;
3339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 }
3341
3342 /* Insert the text of the new completion */
3343 ins_compl_insert();
3344
3345 if (!allow_get_expansion)
3346 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003347 /* may undisplay the popup menu first */
3348 ins_compl_upd_pum();
3349
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 /* Display the current match. */
3351 update_screen(0);
3352
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003353 /* display the updated popup menu */
3354 ins_compl_show_pum();
3355
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 /* Delete old text to be replaced, since we're still searching and
3357 * don't want to match ourselves! */
3358 ins_compl_delete();
3359 }
3360
3361 /*
3362 * Show the file name for the match (if any)
3363 * Truncate the file name to avoid a wait for return.
3364 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003365 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
3367 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003368 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 if (i <= 0)
3370 i = 0;
3371 else
3372 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003373 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 msg(IObuff);
3375 redraw_cmdline = FALSE; /* don't overwrite! */
3376 }
3377
3378 return num_matches;
3379}
3380
3381/*
3382 * Call this while finding completions, to check whether the user has hit a key
3383 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003384 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003386 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 */
3388 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003389ins_compl_check_keys(frequency)
3390 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
3392 static int count = 0;
3393
3394 int c;
3395
3396 /* Don't check when reading keys from a script. That would break the test
3397 * scripts */
3398 if (using_script())
3399 return;
3400
3401 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003402 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 return;
3404 count = 0;
3405
3406 ++no_mapping;
3407 c = vpeekc_any();
3408 --no_mapping;
3409 if (c != NUL)
3410 {
3411 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3412 {
3413 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003414 compl_shows_dir = ins_compl_key2dir(c);
3415 (void)ins_compl_next(FALSE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
3417 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003418 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003420 if (compl_pending && !got_int)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003421 (void)ins_compl_next(FALSE, 1);
3422}
3423
3424/*
3425 * Decide the direction of Insert mode complete from the key typed.
3426 * Returns BACKWARD or FORWARD.
3427 */
3428 static int
3429ins_compl_key2dir(c)
3430 int c;
3431{
3432 if (c == Ctrl_P || c == Ctrl_L || (pum_visible()
3433 && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP)))
3434 return BACKWARD;
3435 return FORWARD;
3436}
3437
3438/*
3439 * Decide the number of completions to move forward.
3440 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3441 */
3442 static int
3443ins_compl_key2count(c)
3444 int c;
3445{
3446 int h;
3447
3448 if (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
3449 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN))
3450 {
3451 h = pum_get_height();
3452 if (h > 3)
3453 h -= 2; /* keep some context */
3454 return h;
3455 }
3456 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457}
3458
3459/*
3460 * Do Insert mode completion.
3461 * Called when character "c" was typed, which has a meaning for completion.
3462 * Returns OK if completion was done, FAIL if something failed (out of mem).
3463 */
3464 static int
3465ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003466 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003468 char_u *line;
3469 int startcol = 0; /* column where searched text starts */
3470 colnr_T curs_col; /* cursor column */
3471 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
Bram Moolenaare3226be2005-12-18 22:10:00 +00003473 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003474 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 /* First time we hit ^N or ^P (in a row, I mean) */
3477
3478 /* Turn off 'sm' so we don't show matches with ^X^L */
3479 save_sm = p_sm;
3480 p_sm = FALSE;
3481
3482 did_ai = FALSE;
3483#ifdef FEAT_SMARTINDENT
3484 did_si = FALSE;
3485 can_si = FALSE;
3486 can_si_back = FALSE;
3487#endif
3488 if (stop_arrow() == FAIL)
3489 return FAIL;
3490
3491 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003492 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
3494 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003495 * "compl_startpos" to the cursor as a pattern to add a new word
3496 * instead of expand the one before the cursor, in word-wise if
3497 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 * is not in the same line as the cursor then fix it (the line has
3499 * been split because it was longer than 'tw'). if SOL is set then
3500 * skip the previous pattern, a word at the beginning of the line has
3501 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003502 if ((compl_cont_status & CONT_INTRPT) && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
3504 /*
3505 * it is a continued search
3506 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003507 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3509 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3510 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003511 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003513 /* line (probably) wrapped, set compl_startpos to the
3514 * first non_blank in the line, if it is not a wordchar
3515 * include it to get a better pattern, but then we don't
3516 * want the "\\<" prefix, check it bellow */
3517 compl_col = (colnr_T)(skipwhite(line) - line);
3518 compl_startpos.col = compl_col;
3519 compl_startpos.lnum = curwin->w_cursor.lnum;
3520 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
3522 else
3523 {
3524 /* S_IPOS was set when we inserted a word that was at the
3525 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003526 * mode but first we need to redefine compl_startpos */
3527 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003529 compl_cont_status |= CONT_SOL;
3530 compl_startpos.col = (colnr_T)(skipwhite(
3531 line + compl_length
3532 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003534 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003536 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003537 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 * have enough space? just being paranoic */
3539#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003540 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003542 compl_cont_status &= ~CONT_SOL;
3543 compl_length = (IOSIZE - MIN_SPACE);
3544 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003546 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3547 if (compl_length < 1)
3548 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
3550 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003551 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003553 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003556 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003558 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003560 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003562 compl_cont_status = 0;
3563 compl_cont_status |= CONT_N_ADDS;
3564 compl_startpos = curwin->w_cursor;
3565 startcol = (int)curs_col;
3566 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
3568
3569 /* Work out completion pattern and original text -- webb */
3570 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3571 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003572 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3574 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003575 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003577 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003579 compl_col += ++startcol;
3580 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
3582 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003583 compl_pattern = str_foldcase(line + compl_col,
3584 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003586 compl_pattern = vim_strnsave(line + compl_col,
3587 compl_length);
3588 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 return FAIL;
3590 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003591 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
3593 char_u *prefix = (char_u *)"\\<";
3594
3595 /* we need 3 extra chars, 1 for the NUL and
3596 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003597 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3598 compl_length) + 3);
3599 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003601 if (!vim_iswordp(line + compl_col)
3602 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 && (
3604#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003605 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003607 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608#endif
3609 )))
3610 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003611 STRCPY((char *)compl_pattern, prefix);
3612 (void)quote_meta(compl_pattern + STRLEN(prefix),
3613 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003615 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003617 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003619 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620#endif
3621 )
3622 {
3623 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003624 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3625 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003627 compl_col += curs_col;
3628 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
3630 else
3631 {
3632#ifdef FEAT_MBYTE
3633 /* Search the point of change class of multibyte character
3634 * or not a word single byte character backward. */
3635 if (has_mbyte)
3636 {
3637 int base_class;
3638 int head_off;
3639
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003640 startcol -= (*mb_head_off)(line, line + startcol);
3641 base_class = mb_get_class(line + startcol);
3642 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003644 head_off = (*mb_head_off)(line, line + startcol);
3645 if (base_class != mb_get_class(line + startcol
3646 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003648 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 }
3650 }
3651 else
3652#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003653 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003655 compl_col += ++startcol;
3656 compl_length = (int)curs_col - startcol;
3657 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
3659 /* Only match word with at least two chars -- webb
3660 * there's no need to call quote_meta,
3661 * alloc(7) is enough -- Acevedo
3662 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003663 compl_pattern = alloc(7);
3664 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003666 STRCPY((char *)compl_pattern, "\\<");
3667 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3668 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
3670 else
3671 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003672 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3673 compl_length) + 3);
3674 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003676 STRCPY((char *)compl_pattern, "\\<");
3677 (void)quote_meta(compl_pattern + 2, line + compl_col,
3678 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680 }
3681 }
3682 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3683 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003684 compl_col = skipwhite(line) - line;
3685 compl_length = (int)curs_col - (int)compl_col;
3686 if (compl_length < 0) /* cursor in indent: empty pattern */
3687 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003689 compl_pattern = str_foldcase(line + compl_col, compl_length,
3690 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003692 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3693 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 return FAIL;
3695 }
3696 else if (ctrl_x_mode == CTRL_X_FILES)
3697 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003698 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003700 compl_col += ++startcol;
3701 compl_length = (int)curs_col - startcol;
3702 compl_pattern = addstar(line + compl_col, compl_length,
3703 EXPAND_FILES);
3704 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 return FAIL;
3706 }
3707 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3708 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003709 compl_pattern = vim_strnsave(line, curs_col);
3710 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003712 set_cmd_context(&compl_xp, compl_pattern,
3713 (int)STRLEN(compl_pattern), curs_col);
3714 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3715 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003717 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
3718 compl_col = startcol;
3719 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003721 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003722 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00003723#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003724 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00003725 * Call user defined function 'completefunc' with "a:findstart"
3726 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003727 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00003728 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003729 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003730 char_u *funcname;
3731 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003732
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003733 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00003734 * string */
3735 funcname = ctrl_x_mode == CTRL_X_FUNCTION
3736 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3737 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003738 {
3739 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3740 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003741 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003742 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003743
3744 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003745 args[1] = NULL;
3746 pos = curwin->w_cursor;
3747 col = call_func_retnr(funcname, 2, args, FALSE);
3748 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003749
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003750 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003751 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003752 compl_col = col;
3753 if ((colnr_T)compl_col > curs_col)
3754 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003755
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003756 /* Setup variables for completion. Need to obtain "line" again,
3757 * it may have become invalid. */
3758 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003759 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003760 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3761 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003762#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003763 return FAIL;
3764 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00003765 else if (ctrl_x_mode == CTRL_X_SPELL)
3766 {
3767#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00003768 if (spell_bad_len > 0)
3769 compl_col = curs_col - spell_bad_len;
3770 else
3771 compl_col = spell_word_start(startcol);
3772 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00003773 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003774 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003775 compl_length = (int)curs_col - compl_col;
3776 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3777 if (compl_pattern == NULL)
3778#endif
3779 return FAIL;
3780 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003781 else
3782 {
3783 EMSG2(_(e_intern2), "ins_complete()");
3784 return FAIL;
3785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003787 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
3789 edit_submode_pre = (char_u *)_(" Adding");
3790 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3791 {
3792 /* Insert a new line, keep indentation but ignore 'comments' */
3793#ifdef FEAT_COMMENTS
3794 char_u *old = curbuf->b_p_com;
3795
3796 curbuf->b_p_com = (char_u *)"";
3797#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003798 compl_startpos.lnum = curwin->w_cursor.lnum;
3799 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 ins_eol('\r');
3801#ifdef FEAT_COMMENTS
3802 curbuf->b_p_com = old;
3803#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003804 compl_length = 0;
3805 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 }
3807 }
3808 else
3809 {
3810 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003811 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 }
3813
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003814 if (compl_cont_status & CONT_LOCAL)
3815 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 else
3817 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
3818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003820 * "compl_orig_text" itself (not a copy) is added, it will be freed
3821 * when the list of matches is freed. */
3822 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
3823 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
3824 -1, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003826 vim_free(compl_pattern);
3827 compl_pattern = NULL;
3828 vim_free(compl_orig_text);
3829 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 return FAIL;
3831 }
3832
3833 /* showmode might reset the internal line pointers, so it must
3834 * be called before line = ml_get(), or when this address is no
3835 * longer needed. -- Acevedo.
3836 */
3837 edit_submode_extra = (char_u *)_("-- Searching...");
3838 edit_submode_highl = HLF_COUNT;
3839 showmode();
3840 edit_submode_extra = NULL;
3841 out_flush();
3842 }
3843
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003844 compl_shown_match = compl_curr_match;
3845 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846
3847 /*
3848 * Find next match.
3849 */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003850 n = ins_compl_next(TRUE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003852 /* may undisplay the popup menu */
3853 ins_compl_upd_pum();
3854
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003855 if (n > 1) /* all matches have been found */
3856 compl_matches = n;
3857 compl_curr_match = compl_shown_match;
3858 compl_direction = compl_shows_dir;
3859 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
3861 /* eat the ESC to avoid leaving insert mode */
3862 if (got_int && !global_busy)
3863 {
3864 (void)vgetc();
3865 got_int = FALSE;
3866 }
3867
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003868 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003869 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003871 edit_submode_extra = (compl_cont_status & CONT_ADDING)
3872 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
3874 edit_submode_highl = HLF_E;
3875 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
3876 * because we couldn't expand anything at first place, but if we used
3877 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
3878 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003879 if ( compl_length > 1
3880 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 || (ctrl_x_mode != 0
3882 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
3883 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003884 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 }
3886
Bram Moolenaar572cb562005-08-05 21:35:02 +00003887 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003888 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003890 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
3892 if (edit_submode_extra == NULL)
3893 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003894 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
3896 edit_submode_extra = (char_u *)_("Back at original");
3897 edit_submode_highl = HLF_W;
3898 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003899 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 {
3901 edit_submode_extra = (char_u *)_("Word from other line");
3902 edit_submode_highl = HLF_COUNT;
3903 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00003904 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
3906 edit_submode_extra = (char_u *)_("The only match");
3907 edit_submode_highl = HLF_COUNT;
3908 }
3909 else
3910 {
3911 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003912 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003914 int number = 0;
3915 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003917 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 {
3919 /* search backwards for the first valid (!= -1) number.
3920 * This should normally succeed already at the first loop
3921 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003922 for (match = compl_curr_match->cp_prev; match != NULL
3923 && match != compl_first_match;
3924 match = match->cp_prev)
3925 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003927 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 break;
3929 }
3930 if (match != NULL)
3931 /* go up and assign all numbers which are not assigned
3932 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003933 for (match = match->cp_next;
3934 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003935 match = match->cp_next)
3936 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938 else /* BACKWARD */
3939 {
3940 /* search forwards (upwards) for the first valid (!= -1)
3941 * number. This should normally succeed already at the
3942 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003943 for (match = compl_curr_match->cp_next; match != NULL
3944 && match != compl_first_match;
3945 match = match->cp_next)
3946 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003948 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 break;
3950 }
3951 if (match != NULL)
3952 /* go down and assign all numbers which are not
3953 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003954 for (match = match->cp_prev; match
3955 && match->cp_number == -1;
3956 match = match->cp_prev)
3957 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
3959 }
3960
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003961 /* The match should always have a sequence number now, this is
3962 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003963 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
3965 /* Space for 10 text chars. + 2x10-digit no.s */
3966 static char_u match_ref[31];
3967
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003968 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00003970 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003972 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00003973 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003974 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 edit_submode_extra = match_ref;
3976 edit_submode_highl = HLF_R;
3977 if (dollar_vcol)
3978 curs_columns(FALSE);
3979 }
3980 }
3981 }
3982
3983 /* Show a message about what (completion) mode we're in. */
3984 showmode();
3985 if (edit_submode_extra != NULL)
3986 {
3987 if (!p_smd)
3988 msg_attr(edit_submode_extra,
3989 edit_submode_highl < HLF_COUNT
3990 ? hl_attr(edit_submode_highl) : 0);
3991 }
3992 else
3993 msg_clr_cmdline(); /* necessary for "noshowmode" */
3994
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003995 ins_compl_show_pum();
3996
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 return OK;
3998}
3999
4000/*
4001 * Looks in the first "len" chars. of "src" for search-metachars.
4002 * If dest is not NULL the chars. are copied there quoting (with
4003 * a backslash) the metachars, and dest would be NUL terminated.
4004 * Returns the length (needed) of dest
4005 */
4006 static int
4007quote_meta(dest, src, len)
4008 char_u *dest;
4009 char_u *src;
4010 int len;
4011{
4012 int m;
4013
4014 for (m = len; --len >= 0; src++)
4015 {
4016 switch (*src)
4017 {
4018 case '.':
4019 case '*':
4020 case '[':
4021 if (ctrl_x_mode == CTRL_X_DICTIONARY
4022 || ctrl_x_mode == CTRL_X_THESAURUS)
4023 break;
4024 case '~':
4025 if (!p_magic) /* quote these only if magic is set */
4026 break;
4027 case '\\':
4028 if (ctrl_x_mode == CTRL_X_DICTIONARY
4029 || ctrl_x_mode == CTRL_X_THESAURUS)
4030 break;
4031 case '^': /* currently it's not needed. */
4032 case '$':
4033 m++;
4034 if (dest != NULL)
4035 *dest++ = '\\';
4036 break;
4037 }
4038 if (dest != NULL)
4039 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004040# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 /* Copy remaining bytes of a multibyte character. */
4042 if (has_mbyte)
4043 {
4044 int i, mb_len;
4045
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004046 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 if (mb_len > 0 && len >= mb_len)
4048 for (i = 0; i < mb_len; ++i)
4049 {
4050 --len;
4051 ++src;
4052 if (dest != NULL)
4053 *dest++ = *src;
4054 }
4055 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004056# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
4058 if (dest != NULL)
4059 *dest = NUL;
4060
4061 return m;
4062}
4063#endif /* FEAT_INS_EXPAND */
4064
4065/*
4066 * Next character is interpreted literally.
4067 * A one, two or three digit decimal number is interpreted as its byte value.
4068 * If one or two digits are entered, the next character is given to vungetc().
4069 * For Unicode a character > 255 may be returned.
4070 */
4071 int
4072get_literal()
4073{
4074 int cc;
4075 int nc;
4076 int i;
4077 int hex = FALSE;
4078 int octal = FALSE;
4079#ifdef FEAT_MBYTE
4080 int unicode = 0;
4081#endif
4082
4083 if (got_int)
4084 return Ctrl_C;
4085
4086#ifdef FEAT_GUI
4087 /*
4088 * In GUI there is no point inserting the internal code for a special key.
4089 * It is more useful to insert the string "<KEY>" instead. This would
4090 * probably be useful in a text window too, but it would not be
4091 * vi-compatible (maybe there should be an option for it?) -- webb
4092 */
4093 if (gui.in_use)
4094 ++allow_keys;
4095#endif
4096#ifdef USE_ON_FLY_SCROLL
4097 dont_scroll = TRUE; /* disallow scrolling here */
4098#endif
4099 ++no_mapping; /* don't map the next key hits */
4100 cc = 0;
4101 i = 0;
4102 for (;;)
4103 {
4104 do
4105 nc = safe_vgetc();
4106 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4107 || nc == K_HOR_SCROLLBAR);
4108#ifdef FEAT_CMDL_INFO
4109 if (!(State & CMDLINE)
4110# ifdef FEAT_MBYTE
4111 && MB_BYTE2LEN_CHECK(nc) == 1
4112# endif
4113 )
4114 add_to_showcmd(nc);
4115#endif
4116 if (nc == 'x' || nc == 'X')
4117 hex = TRUE;
4118 else if (nc == 'o' || nc == 'O')
4119 octal = TRUE;
4120#ifdef FEAT_MBYTE
4121 else if (nc == 'u' || nc == 'U')
4122 unicode = nc;
4123#endif
4124 else
4125 {
4126 if (hex
4127#ifdef FEAT_MBYTE
4128 || unicode != 0
4129#endif
4130 )
4131 {
4132 if (!vim_isxdigit(nc))
4133 break;
4134 cc = cc * 16 + hex2nr(nc);
4135 }
4136 else if (octal)
4137 {
4138 if (nc < '0' || nc > '7')
4139 break;
4140 cc = cc * 8 + nc - '0';
4141 }
4142 else
4143 {
4144 if (!VIM_ISDIGIT(nc))
4145 break;
4146 cc = cc * 10 + nc - '0';
4147 }
4148
4149 ++i;
4150 }
4151
4152 if (cc > 255
4153#ifdef FEAT_MBYTE
4154 && unicode == 0
4155#endif
4156 )
4157 cc = 255; /* limit range to 0-255 */
4158 nc = 0;
4159
4160 if (hex) /* hex: up to two chars */
4161 {
4162 if (i >= 2)
4163 break;
4164 }
4165#ifdef FEAT_MBYTE
4166 else if (unicode) /* Unicode: up to four or eight chars */
4167 {
4168 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4169 break;
4170 }
4171#endif
4172 else if (i >= 3) /* decimal or octal: up to three chars */
4173 break;
4174 }
4175 if (i == 0) /* no number entered */
4176 {
4177 if (nc == K_ZERO) /* NUL is stored as NL */
4178 {
4179 cc = '\n';
4180 nc = 0;
4181 }
4182 else
4183 {
4184 cc = nc;
4185 nc = 0;
4186 }
4187 }
4188
4189 if (cc == 0) /* NUL is stored as NL */
4190 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004191#ifdef FEAT_MBYTE
4192 if (enc_dbcs && (cc & 0xff) == 0)
4193 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4194 second byte will cause trouble! */
4195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
4197 --no_mapping;
4198#ifdef FEAT_GUI
4199 if (gui.in_use)
4200 --allow_keys;
4201#endif
4202 if (nc)
4203 vungetc(nc);
4204 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4205 return cc;
4206}
4207
4208/*
4209 * Insert character, taking care of special keys and mod_mask
4210 */
4211 static void
4212insert_special(c, allow_modmask, ctrlv)
4213 int c;
4214 int allow_modmask;
4215 int ctrlv; /* c was typed after CTRL-V */
4216{
4217 char_u *p;
4218 int len;
4219
4220 /*
4221 * Special function key, translate into "<Key>". Up to the last '>' is
4222 * inserted with ins_str(), so as not to replace characters in replace
4223 * mode.
4224 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4225 * unless 'allow_modmask' is TRUE.
4226 */
4227#ifdef MACOS
4228 /* Command-key never produces a normal key */
4229 if (mod_mask & MOD_MASK_CMD)
4230 allow_modmask = TRUE;
4231#endif
4232 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4233 {
4234 p = get_special_key_name(c, mod_mask);
4235 len = (int)STRLEN(p);
4236 c = p[len - 1];
4237 if (len > 2)
4238 {
4239 if (stop_arrow() == FAIL)
4240 return;
4241 p[len - 1] = NUL;
4242 ins_str(p);
4243 AppendToRedobuffLit(p);
4244 ctrlv = FALSE;
4245 }
4246 }
4247 if (stop_arrow() == OK)
4248 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4249}
4250
4251/*
4252 * Special characters in this context are those that need processing other
4253 * than the simple insertion that can be performed here. This includes ESC
4254 * which terminates the insert, and CR/NL which need special processing to
4255 * open up a new line. This routine tries to optimize insertions performed by
4256 * the "redo", "undo" or "put" commands, so it needs to know when it should
4257 * stop and defer processing to the "normal" mechanism.
4258 * '0' and '^' are special, because they can be followed by CTRL-D.
4259 */
4260#ifdef EBCDIC
4261# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4262#else
4263# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4264#endif
4265
4266#ifdef FEAT_MBYTE
4267# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4268#else
4269# define WHITECHAR(cc) vim_iswhite(cc)
4270#endif
4271
4272 void
4273insertchar(c, flags, second_indent)
4274 int c; /* character to insert or NUL */
4275 int flags; /* INSCHAR_FORMAT, etc. */
4276 int second_indent; /* indent for second line if >= 0 */
4277{
4278 int haveto_redraw = FALSE;
4279 int textwidth;
4280#ifdef FEAT_COMMENTS
4281 colnr_T leader_len;
4282 char_u *p;
4283 int no_leader = FALSE;
4284 int do_comments = (flags & INSCHAR_DO_COM);
4285#endif
4286 int fo_white_par;
4287 int first_line = TRUE;
4288 int fo_ins_blank;
4289#ifdef FEAT_MBYTE
4290 int fo_multibyte;
4291#endif
4292 int save_char = NUL;
4293 int cc;
4294
4295 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4296 fo_ins_blank = has_format_option(FO_INS_BLANK);
4297#ifdef FEAT_MBYTE
4298 fo_multibyte = has_format_option(FO_MBYTE_BREAK);
4299#endif
4300 fo_white_par = has_format_option(FO_WHITE_PAR);
4301
4302 /*
4303 * Try to break the line in two or more pieces when:
4304 * - Always do this if we have been called to do formatting only.
4305 * - Always do this when 'formatoptions' has the 'a' flag and the line
4306 * ends in white space.
4307 * - Otherwise:
4308 * - Don't do this if inserting a blank
4309 * - Don't do this if an existing character is being replaced, unless
4310 * we're in VREPLACE mode.
4311 * - Do this if the cursor is not on the line where insert started
4312 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4313 * before the insert.
4314 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4315 * before 'textwidth'
4316 */
4317 if (textwidth
4318 && ((flags & INSCHAR_FORMAT)
4319 || (!vim_iswhite(c)
4320 && !((State & REPLACE_FLAG)
4321#ifdef FEAT_VREPLACE
4322 && !(State & VREPLACE_FLAG)
4323#endif
4324 && *ml_get_cursor() != NUL)
4325 && (curwin->w_cursor.lnum != Insstart.lnum
4326 || ((!has_format_option(FO_INS_LONG)
4327 || Insstart_textlen <= (colnr_T)textwidth)
4328 && (!fo_ins_blank
4329 || Insstart_blank_vcol <= (colnr_T)textwidth
4330 ))))))
4331 {
4332 /*
4333 * When 'ai' is off we don't want a space under the cursor to be
4334 * deleted. Replace it with an 'x' temporarily.
4335 */
4336 if (!curbuf->b_p_ai)
4337 {
4338 cc = gchar_cursor();
4339 if (vim_iswhite(cc))
4340 {
4341 save_char = cc;
4342 pchar_cursor('x');
4343 }
4344 }
4345
4346 /*
4347 * Repeat breaking lines, until the current line is not too long.
4348 */
4349 while (!got_int)
4350 {
4351 int startcol; /* Cursor column at entry */
4352 int wantcol; /* column at textwidth border */
4353 int foundcol; /* column for start of spaces */
4354 int end_foundcol = 0; /* column for start of word */
4355 colnr_T len;
4356 colnr_T virtcol;
4357#ifdef FEAT_VREPLACE
4358 int orig_col = 0;
4359 char_u *saved_text = NULL;
4360#endif
4361 colnr_T col;
4362
4363 virtcol = get_nolist_virtcol();
4364 if (virtcol < (colnr_T)textwidth)
4365 break;
4366
4367#ifdef FEAT_COMMENTS
4368 if (no_leader)
4369 do_comments = FALSE;
4370 else if (!(flags & INSCHAR_FORMAT)
4371 && has_format_option(FO_WRAP_COMS))
4372 do_comments = TRUE;
4373
4374 /* Don't break until after the comment leader */
4375 if (do_comments)
4376 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
4377 else
4378 leader_len = 0;
4379
4380 /* If the line doesn't start with a comment leader, then don't
4381 * start one in a following broken line. Avoids that a %word
4382 * moved to the start of the next line causes all following lines
4383 * to start with %. */
4384 if (leader_len == 0)
4385 no_leader = TRUE;
4386#endif
4387 if (!(flags & INSCHAR_FORMAT)
4388#ifdef FEAT_COMMENTS
4389 && leader_len == 0
4390#endif
4391 && !has_format_option(FO_WRAP))
4392
4393 {
4394 textwidth = 0;
4395 break;
4396 }
4397 if ((startcol = curwin->w_cursor.col) == 0)
4398 break;
4399
4400 /* find column of textwidth border */
4401 coladvance((colnr_T)textwidth);
4402 wantcol = curwin->w_cursor.col;
4403
4404 curwin->w_cursor.col = startcol - 1;
4405#ifdef FEAT_MBYTE
4406 /* Correct cursor for multi-byte character. */
4407 if (has_mbyte)
4408 mb_adjust_cursor();
4409#endif
4410 foundcol = 0;
4411
4412 /*
4413 * Find position to break at.
4414 * Stop at first entered white when 'formatoptions' has 'v'
4415 */
4416 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
4417 || curwin->w_cursor.lnum != Insstart.lnum
4418 || curwin->w_cursor.col >= Insstart.col)
4419 {
4420 cc = gchar_cursor();
4421 if (WHITECHAR(cc))
4422 {
4423 /* remember position of blank just before text */
4424 end_foundcol = curwin->w_cursor.col;
4425
4426 /* find start of sequence of blanks */
4427 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
4428 {
4429 dec_cursor();
4430 cc = gchar_cursor();
4431 }
4432 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
4433 break; /* only spaces in front of text */
4434#ifdef FEAT_COMMENTS
4435 /* Don't break until after the comment leader */
4436 if (curwin->w_cursor.col < leader_len)
4437 break;
4438#endif
4439 if (has_format_option(FO_ONE_LETTER))
4440 {
4441 /* do not break after one-letter words */
4442 if (curwin->w_cursor.col == 0)
4443 break; /* one-letter word at begin */
4444
4445 col = curwin->w_cursor.col;
4446 dec_cursor();
4447 cc = gchar_cursor();
4448
4449 if (WHITECHAR(cc))
4450 continue; /* one-letter, continue */
4451 curwin->w_cursor.col = col;
4452 }
4453#ifdef FEAT_MBYTE
4454 if (has_mbyte)
4455 foundcol = curwin->w_cursor.col
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004456 + (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 else
4458#endif
4459 foundcol = curwin->w_cursor.col + 1;
4460 if (curwin->w_cursor.col < (colnr_T)wantcol)
4461 break;
4462 }
4463#ifdef FEAT_MBYTE
4464 else if (cc >= 0x100 && fo_multibyte
4465 && curwin->w_cursor.col <= (colnr_T)wantcol)
4466 {
4467 /* Break after or before a multi-byte character. */
4468 foundcol = curwin->w_cursor.col;
4469 if (curwin->w_cursor.col < (colnr_T)wantcol)
4470 foundcol += (*mb_char2len)(cc);
4471 end_foundcol = foundcol;
4472 break;
4473 }
4474#endif
4475 if (curwin->w_cursor.col == 0)
4476 break;
4477 dec_cursor();
4478 }
4479
4480 if (foundcol == 0) /* no spaces, cannot break line */
4481 {
4482 curwin->w_cursor.col = startcol;
4483 break;
4484 }
4485
4486 /* Going to break the line, remove any "$" now. */
4487 undisplay_dollar();
4488
4489 /*
4490 * Offset between cursor position and line break is used by replace
4491 * stack functions. VREPLACE does not use this, and backspaces
4492 * over the text instead.
4493 */
4494#ifdef FEAT_VREPLACE
4495 if (State & VREPLACE_FLAG)
4496 orig_col = startcol; /* Will start backspacing from here */
4497 else
4498#endif
4499 replace_offset = startcol - end_foundcol - 1;
4500
4501 /*
4502 * adjust startcol for spaces that will be deleted and
4503 * characters that will remain on top line
4504 */
4505 curwin->w_cursor.col = foundcol;
4506 while (cc = gchar_cursor(), WHITECHAR(cc))
4507 inc_cursor();
4508 startcol -= curwin->w_cursor.col;
4509 if (startcol < 0)
4510 startcol = 0;
4511
4512#ifdef FEAT_VREPLACE
4513 if (State & VREPLACE_FLAG)
4514 {
4515 /*
4516 * In VREPLACE mode, we will backspace over the text to be
4517 * wrapped, so save a copy now to put on the next line.
4518 */
4519 saved_text = vim_strsave(ml_get_cursor());
4520 curwin->w_cursor.col = orig_col;
4521 if (saved_text == NULL)
4522 break; /* Can't do it, out of memory */
4523 saved_text[startcol] = NUL;
4524
4525 /* Backspace over characters that will move to the next line */
4526 if (!fo_white_par)
4527 backspace_until_column(foundcol);
4528 }
4529 else
4530#endif
4531 {
4532 /* put cursor after pos. to break line */
4533 if (!fo_white_par)
4534 curwin->w_cursor.col = foundcol;
4535 }
4536
4537 /*
4538 * Split the line just before the margin.
4539 * Only insert/delete lines, but don't really redraw the window.
4540 */
4541 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
4542 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
4543#ifdef FEAT_COMMENTS
4544 + (do_comments ? OPENLINE_DO_COM : 0)
4545#endif
4546 , old_indent);
4547 old_indent = 0;
4548
4549 replace_offset = 0;
4550 if (first_line)
4551 {
4552 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
4553 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
4554 if (second_indent >= 0)
4555 {
4556#ifdef FEAT_VREPLACE
4557 if (State & VREPLACE_FLAG)
4558 change_indent(INDENT_SET, second_indent, FALSE, NUL);
4559 else
4560#endif
4561 (void)set_indent(second_indent, SIN_CHANGED);
4562 }
4563 first_line = FALSE;
4564 }
4565
4566#ifdef FEAT_VREPLACE
4567 if (State & VREPLACE_FLAG)
4568 {
4569 /*
4570 * In VREPLACE mode we have backspaced over the text to be
4571 * moved, now we re-insert it into the new line.
4572 */
4573 ins_bytes(saved_text);
4574 vim_free(saved_text);
4575 }
4576 else
4577#endif
4578 {
4579 /*
4580 * Check if cursor is not past the NUL off the line, cindent
4581 * may have added or removed indent.
4582 */
4583 curwin->w_cursor.col += startcol;
4584 len = (colnr_T)STRLEN(ml_get_curline());
4585 if (curwin->w_cursor.col > len)
4586 curwin->w_cursor.col = len;
4587 }
4588
4589 haveto_redraw = TRUE;
4590#ifdef FEAT_CINDENT
4591 can_cindent = TRUE;
4592#endif
4593 /* moved the cursor, don't autoindent or cindent now */
4594 did_ai = FALSE;
4595#ifdef FEAT_SMARTINDENT
4596 did_si = FALSE;
4597 can_si = FALSE;
4598 can_si_back = FALSE;
4599#endif
4600 line_breakcheck();
4601 }
4602
4603 if (save_char) /* put back space after cursor */
4604 pchar_cursor(save_char);
4605
4606 if (c == NUL) /* formatting only */
4607 return;
4608 if (haveto_redraw)
4609 {
4610 update_topline();
4611 redraw_curbuf_later(VALID);
4612 }
4613 }
4614 if (c == NUL) /* only formatting was wanted */
4615 return;
4616
4617#ifdef FEAT_COMMENTS
4618 /* Check whether this character should end a comment. */
4619 if (did_ai && (int)c == end_comment_pending)
4620 {
4621 char_u *line;
4622 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4623 int middle_len, end_len;
4624 int i;
4625
4626 /*
4627 * Need to remove existing (middle) comment leader and insert end
4628 * comment leader. First, check what comment leader we can find.
4629 */
4630 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4631 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4632 {
4633 /* Skip middle-comment string */
4634 while (*p && p[-1] != ':') /* find end of middle flags */
4635 ++p;
4636 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4637 /* Don't count trailing white space for middle_len */
4638 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4639 --middle_len;
4640
4641 /* Find the end-comment string */
4642 while (*p && p[-1] != ':') /* find end of end flags */
4643 ++p;
4644 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4645
4646 /* Skip white space before the cursor */
4647 i = curwin->w_cursor.col;
4648 while (--i >= 0 && vim_iswhite(line[i]))
4649 ;
4650 i++;
4651
4652 /* Skip to before the middle leader */
4653 i -= middle_len;
4654
4655 /* Check some expected things before we go on */
4656 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4657 {
4658 /* Backspace over all the stuff we want to replace */
4659 backspace_until_column(i);
4660
4661 /*
4662 * Insert the end-comment string, except for the last
4663 * character, which will get inserted as normal later.
4664 */
4665 ins_bytes_len(lead_end, end_len - 1);
4666 }
4667 }
4668 }
4669 end_comment_pending = NUL;
4670#endif
4671
4672 did_ai = FALSE;
4673#ifdef FEAT_SMARTINDENT
4674 did_si = FALSE;
4675 can_si = FALSE;
4676 can_si_back = FALSE;
4677#endif
4678
4679 /*
4680 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4681 * This speeds up normal text input considerably.
4682 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4683 * need to re-indent at a ':', or any other character (but not what
4684 * 'paste' is set)..
4685 */
4686#ifdef USE_ON_FLY_SCROLL
4687 dont_scroll = FALSE; /* allow scrolling here */
4688#endif
4689
4690 if ( !ISSPECIAL(c)
4691#ifdef FEAT_MBYTE
4692 && (!has_mbyte || (*mb_char2len)(c) == 1)
4693#endif
4694 && vpeekc() != NUL
4695 && !(State & REPLACE_FLAG)
4696#ifdef FEAT_CINDENT
4697 && !cindent_on()
4698#endif
4699#ifdef FEAT_RIGHTLEFT
4700 && !p_ri
4701#endif
4702 )
4703 {
4704#define INPUT_BUFLEN 100
4705 char_u buf[INPUT_BUFLEN + 1];
4706 int i;
4707 colnr_T virtcol = 0;
4708
4709 buf[0] = c;
4710 i = 1;
4711 if (textwidth)
4712 virtcol = get_nolist_virtcol();
4713 /*
4714 * Stop the string when:
4715 * - no more chars available
4716 * - finding a special character (command key)
4717 * - buffer is full
4718 * - running into the 'textwidth' boundary
4719 * - need to check for abbreviation: A non-word char after a word-char
4720 */
4721 while ( (c = vpeekc()) != NUL
4722 && !ISSPECIAL(c)
4723#ifdef FEAT_MBYTE
4724 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4725#endif
4726 && i < INPUT_BUFLEN
4727 && (textwidth == 0
4728 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4729 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4730 {
4731#ifdef FEAT_RIGHTLEFT
4732 c = vgetc();
4733 if (p_hkmap && KeyTyped)
4734 c = hkmap(c); /* Hebrew mode mapping */
4735# ifdef FEAT_FKMAP
4736 if (p_fkmap && KeyTyped)
4737 c = fkmap(c); /* Farsi mode mapping */
4738# endif
4739 buf[i++] = c;
4740#else
4741 buf[i++] = vgetc();
4742#endif
4743 }
4744
4745#ifdef FEAT_DIGRAPHS
4746 do_digraph(-1); /* clear digraphs */
4747 do_digraph(buf[i-1]); /* may be the start of a digraph */
4748#endif
4749 buf[i] = NUL;
4750 ins_str(buf);
4751 if (flags & INSCHAR_CTRLV)
4752 {
4753 redo_literal(*buf);
4754 i = 1;
4755 }
4756 else
4757 i = 0;
4758 if (buf[i] != NUL)
4759 AppendToRedobuffLit(buf + i);
4760 }
4761 else
4762 {
4763#ifdef FEAT_MBYTE
4764 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
4765 {
4766 char_u buf[MB_MAXBYTES + 1];
4767
4768 (*mb_char2bytes)(c, buf);
4769 buf[cc] = NUL;
4770 ins_char_bytes(buf, cc);
4771 AppendCharToRedobuff(c);
4772 }
4773 else
4774#endif
4775 {
4776 ins_char(c);
4777 if (flags & INSCHAR_CTRLV)
4778 redo_literal(c);
4779 else
4780 AppendCharToRedobuff(c);
4781 }
4782 }
4783}
4784
4785/*
4786 * Called after inserting or deleting text: When 'formatoptions' includes the
4787 * 'a' flag format from the current line until the end of the paragraph.
4788 * Keep the cursor at the same position relative to the text.
4789 * The caller must have saved the cursor line for undo, following ones will be
4790 * saved here.
4791 */
4792 void
4793auto_format(trailblank, prev_line)
4794 int trailblank; /* when TRUE also format with trailing blank */
4795 int prev_line; /* may start in previous line */
4796{
4797 pos_T pos;
4798 colnr_T len;
4799 char_u *old;
4800 char_u *new, *pnew;
4801 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004802 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803
4804 if (!has_format_option(FO_AUTO))
4805 return;
4806
4807 pos = curwin->w_cursor;
4808 old = ml_get_curline();
4809
4810 /* may remove added space */
4811 check_auto_format(FALSE);
4812
4813 /* Don't format in Insert mode when the cursor is on a trailing blank, the
4814 * user might insert normal text next. Also skip formatting when "1" is
4815 * in 'formatoptions' and there is a single character before the cursor.
4816 * Otherwise the line would be broken and when typing another non-white
4817 * next they are not joined back together. */
4818 wasatend = (pos.col == STRLEN(old));
4819 if (*old != NUL && !trailblank && wasatend)
4820 {
4821 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004822 cc = gchar_cursor();
4823 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
4824 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004826 cc = gchar_cursor();
4827 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 {
4829 curwin->w_cursor = pos;
4830 return;
4831 }
4832 curwin->w_cursor = pos;
4833 }
4834
4835#ifdef FEAT_COMMENTS
4836 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
4837 * comments. */
4838 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
4839 && get_leader_len(old, NULL, FALSE) == 0)
4840 return;
4841#endif
4842
4843 /*
4844 * May start formatting in a previous line, so that after "x" a word is
4845 * moved to the previous line if it fits there now. Only when this is not
4846 * the start of a paragraph.
4847 */
4848 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
4849 {
4850 --curwin->w_cursor.lnum;
4851 if (u_save_cursor() == FAIL)
4852 return;
4853 }
4854
4855 /*
4856 * Do the formatting and restore the cursor position. "saved_cursor" will
4857 * be adjusted for the text formatting.
4858 */
4859 saved_cursor = pos;
4860 format_lines((linenr_T)-1);
4861 curwin->w_cursor = saved_cursor;
4862 saved_cursor.lnum = 0;
4863
4864 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4865 {
4866 /* "cannot happen" */
4867 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4868 coladvance((colnr_T)MAXCOL);
4869 }
4870 else
4871 check_cursor_col();
4872
4873 /* Insert mode: If the cursor is now after the end of the line while it
4874 * previously wasn't, the line was broken. Because of the rule above we
4875 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
4876 * formatted. */
4877 if (!wasatend && has_format_option(FO_WHITE_PAR))
4878 {
4879 new = ml_get_curline();
4880 len = STRLEN(new);
4881 if (curwin->w_cursor.col == len)
4882 {
4883 pnew = vim_strnsave(new, len + 2);
4884 pnew[len] = ' ';
4885 pnew[len + 1] = NUL;
4886 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
4887 /* remove the space later */
4888 did_add_space = TRUE;
4889 }
4890 else
4891 /* may remove added space */
4892 check_auto_format(FALSE);
4893 }
4894
4895 check_cursor();
4896}
4897
4898/*
4899 * When an extra space was added to continue a paragraph for auto-formatting,
4900 * delete it now. The space must be under the cursor, just after the insert
4901 * position.
4902 */
4903 static void
4904check_auto_format(end_insert)
4905 int end_insert; /* TRUE when ending Insert mode */
4906{
4907 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004908 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
4910 if (did_add_space)
4911 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004912 cc = gchar_cursor();
4913 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 /* Somehow the space was removed already. */
4915 did_add_space = FALSE;
4916 else
4917 {
4918 if (!end_insert)
4919 {
4920 inc_cursor();
4921 c = gchar_cursor();
4922 dec_cursor();
4923 }
4924 if (c != NUL)
4925 {
4926 /* The space is no longer at the end of the line, delete it. */
4927 del_char(FALSE);
4928 did_add_space = FALSE;
4929 }
4930 }
4931 }
4932}
4933
4934/*
4935 * Find out textwidth to be used for formatting:
4936 * if 'textwidth' option is set, use it
4937 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
4938 * if invalid value, use 0.
4939 * Set default to window width (maximum 79) for "gq" operator.
4940 */
4941 int
4942comp_textwidth(ff)
4943 int ff; /* force formatting (for "Q" command) */
4944{
4945 int textwidth;
4946
4947 textwidth = curbuf->b_p_tw;
4948 if (textwidth == 0 && curbuf->b_p_wm)
4949 {
4950 /* The width is the window width minus 'wrapmargin' minus all the
4951 * things that add to the margin. */
4952 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
4953#ifdef FEAT_CMDWIN
4954 if (cmdwin_type != 0)
4955 textwidth -= 1;
4956#endif
4957#ifdef FEAT_FOLDING
4958 textwidth -= curwin->w_p_fdc;
4959#endif
4960#ifdef FEAT_SIGNS
4961 if (curwin->w_buffer->b_signlist != NULL
4962# ifdef FEAT_NETBEANS_INTG
4963 || usingNetbeans
4964# endif
4965 )
4966 textwidth -= 1;
4967#endif
4968 if (curwin->w_p_nu)
4969 textwidth -= 8;
4970 }
4971 if (textwidth < 0)
4972 textwidth = 0;
4973 if (ff && textwidth == 0)
4974 {
4975 textwidth = W_WIDTH(curwin) - 1;
4976 if (textwidth > 79)
4977 textwidth = 79;
4978 }
4979 return textwidth;
4980}
4981
4982/*
4983 * Put a character in the redo buffer, for when just after a CTRL-V.
4984 */
4985 static void
4986redo_literal(c)
4987 int c;
4988{
4989 char_u buf[10];
4990
4991 /* Only digits need special treatment. Translate them into a string of
4992 * three digits. */
4993 if (VIM_ISDIGIT(c))
4994 {
4995 sprintf((char *)buf, "%03d", c);
4996 AppendToRedobuff(buf);
4997 }
4998 else
4999 AppendCharToRedobuff(c);
5000}
5001
5002/*
5003 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005004 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 */
5006 static void
5007start_arrow(end_insert_pos)
5008 pos_T *end_insert_pos;
5009{
5010 if (!arrow_used) /* something has been inserted */
5011 {
5012 AppendToRedobuff(ESC_STR);
5013 stop_insert(end_insert_pos, FALSE);
5014 arrow_used = TRUE; /* this means we stopped the current insert */
5015 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005016#ifdef FEAT_SYN_HL
5017 check_spell_redraw();
5018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019}
5020
Bram Moolenaar217ad922005-03-20 22:37:15 +00005021#ifdef FEAT_SYN_HL
5022/*
5023 * If we skipped highlighting word at cursor, do it now.
5024 * It may be skipped again, thus reset spell_redraw_lnum first.
5025 */
5026 static void
5027check_spell_redraw()
5028{
5029 if (spell_redraw_lnum != 0)
5030 {
5031 linenr_T lnum = spell_redraw_lnum;
5032
5033 spell_redraw_lnum = 0;
5034 redrawWinline(lnum, FALSE);
5035 }
5036}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005037
5038/*
5039 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5040 * spelled word, if there is one.
5041 */
5042 static void
5043spell_back_to_badword()
5044{
5045 pos_T tpos = curwin->w_cursor;
5046
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005047 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005048 if (curwin->w_cursor.col != tpos.col)
5049 start_arrow(&tpos);
5050}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005051#endif
5052
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053/*
5054 * stop_arrow() is called before a change is made in insert mode.
5055 * If an arrow key has been used, start a new insertion.
5056 * Returns FAIL if undo is impossible, shouldn't insert then.
5057 */
5058 int
5059stop_arrow()
5060{
5061 if (arrow_used)
5062 {
5063 if (u_save_cursor() == OK)
5064 {
5065 arrow_used = FALSE;
5066 ins_need_undo = FALSE;
5067 }
5068 Insstart = curwin->w_cursor; /* new insertion starts here */
5069 Insstart_textlen = linetabsize(ml_get_curline());
5070 ai_col = 0;
5071#ifdef FEAT_VREPLACE
5072 if (State & VREPLACE_FLAG)
5073 {
5074 orig_line_count = curbuf->b_ml.ml_line_count;
5075 vr_lines_changed = 1;
5076 }
5077#endif
5078 ResetRedobuff();
5079 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005080 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
5082 else if (ins_need_undo)
5083 {
5084 if (u_save_cursor() == OK)
5085 ins_need_undo = FALSE;
5086 }
5087
5088#ifdef FEAT_FOLDING
5089 /* Always open fold at the cursor line when inserting something. */
5090 foldOpenCursor();
5091#endif
5092
5093 return (arrow_used || ins_need_undo ? FAIL : OK);
5094}
5095
5096/*
5097 * do a few things to stop inserting
5098 */
5099 static void
5100stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005101 pos_T *end_insert_pos; /* where insert ended */
5102 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005104 int cc;
5105 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
5107 stop_redo_ins();
5108 replace_flush(); /* abandon replace stack */
5109
5110 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005111 * Save the inserted text for later redo with ^@ and CTRL-A.
5112 * Don't do it when "restart_edit" was set and nothing was inserted,
5113 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005115 ptr = get_inserted();
5116 if (did_restart_edit == 0 || (ptr != NULL && STRLEN(ptr) > new_insert_skip))
5117 {
5118 vim_free(last_insert);
5119 last_insert = ptr;
5120 last_insert_skip = new_insert_skip;
5121 }
5122 else
5123 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124
5125 if (!arrow_used)
5126 {
5127 /* Auto-format now. It may seem strange to do this when stopping an
5128 * insertion (or moving the cursor), but it's required when appending
5129 * a line and having it end in a space. But only do it when something
5130 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005131 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005133 pos_T tpos = curwin->w_cursor;
5134
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 /* When the cursor is at the end of the line after a space the
5136 * formatting will move it to the following word. Avoid that by
5137 * moving the cursor onto the space. */
5138 cc = 'x';
5139 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5140 {
5141 dec_cursor();
5142 cc = gchar_cursor();
5143 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005144 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
5146
5147 auto_format(TRUE, FALSE);
5148
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005149 if (vim_iswhite(cc))
5150 {
5151 if (gchar_cursor() != NUL)
5152 inc_cursor();
5153#ifdef FEAT_VIRTUALEDIT
5154 /* If the cursor is still at the same character, also keep
5155 * the "coladd". */
5156 if (gchar_cursor() == NUL
5157 && curwin->w_cursor.lnum == tpos.lnum
5158 && curwin->w_cursor.col == tpos.col)
5159 curwin->w_cursor.coladd = tpos.coladd;
5160#endif
5161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
5163
5164 /* If a space was inserted for auto-formatting, remove it now. */
5165 check_auto_format(TRUE);
5166
5167 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005168 * of the line, and put the cursor back.
5169 * Do this when ESC was used or moving the cursor up/down. */
5170 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5171 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005173 pos_T tpos = curwin->w_cursor;
5174
5175 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5177 --curwin->w_cursor.col;
5178 while (cc = gchar_cursor(), vim_iswhite(cc))
5179 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005180 if (curwin->w_cursor.lnum != tpos.lnum)
5181 curwin->w_cursor = tpos;
5182 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5184
5185#ifdef FEAT_VISUAL
5186 /* <C-S-Right> may have started Visual mode, adjust the position for
5187 * deleted characters. */
5188 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5189 {
5190 cc = STRLEN(ml_get_curline());
5191 if (VIsual.col > (colnr_T)cc)
5192 {
5193 VIsual.col = cc;
5194# ifdef FEAT_VIRTUALEDIT
5195 VIsual.coladd = 0;
5196# endif
5197 }
5198 }
5199#endif
5200 }
5201 }
5202 did_ai = FALSE;
5203#ifdef FEAT_SMARTINDENT
5204 did_si = FALSE;
5205 can_si = FALSE;
5206 can_si_back = FALSE;
5207#endif
5208
5209 /* set '[ and '] to the inserted text */
5210 curbuf->b_op_start = Insstart;
5211 curbuf->b_op_end = *end_insert_pos;
5212}
5213
5214/*
5215 * Set the last inserted text to a single character.
5216 * Used for the replace command.
5217 */
5218 void
5219set_last_insert(c)
5220 int c;
5221{
5222 char_u *s;
5223
5224 vim_free(last_insert);
5225#ifdef FEAT_MBYTE
5226 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5227#else
5228 last_insert = alloc(6);
5229#endif
5230 if (last_insert != NULL)
5231 {
5232 s = last_insert;
5233 /* Use the CTRL-V only when entering a special char */
5234 if (c < ' ' || c == DEL)
5235 *s++ = Ctrl_V;
5236 s = add_char2buf(c, s);
5237 *s++ = ESC;
5238 *s++ = NUL;
5239 last_insert_skip = 0;
5240 }
5241}
5242
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005243#if defined(EXITFREE) || defined(PROTO)
5244 void
5245free_last_insert()
5246{
5247 vim_free(last_insert);
5248 last_insert = NULL;
5249}
5250#endif
5251
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252/*
5253 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5254 * and CSI. Handle multi-byte characters.
5255 * Returns a pointer to after the added bytes.
5256 */
5257 char_u *
5258add_char2buf(c, s)
5259 int c;
5260 char_u *s;
5261{
5262#ifdef FEAT_MBYTE
5263 char_u temp[MB_MAXBYTES];
5264 int i;
5265 int len;
5266
5267 len = (*mb_char2bytes)(c, temp);
5268 for (i = 0; i < len; ++i)
5269 {
5270 c = temp[i];
5271#endif
5272 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5273 if (c == K_SPECIAL)
5274 {
5275 *s++ = K_SPECIAL;
5276 *s++ = KS_SPECIAL;
5277 *s++ = KE_FILLER;
5278 }
5279#ifdef FEAT_GUI
5280 else if (c == CSI)
5281 {
5282 *s++ = CSI;
5283 *s++ = KS_EXTRA;
5284 *s++ = (int)KE_CSI;
5285 }
5286#endif
5287 else
5288 *s++ = c;
5289#ifdef FEAT_MBYTE
5290 }
5291#endif
5292 return s;
5293}
5294
5295/*
5296 * move cursor to start of line
5297 * if flags & BL_WHITE move to first non-white
5298 * if flags & BL_SOL move to first non-white if startofline is set,
5299 * otherwise keep "curswant" column
5300 * if flags & BL_FIX don't leave the cursor on a NUL.
5301 */
5302 void
5303beginline(flags)
5304 int flags;
5305{
5306 if ((flags & BL_SOL) && !p_sol)
5307 coladvance(curwin->w_curswant);
5308 else
5309 {
5310 curwin->w_cursor.col = 0;
5311#ifdef FEAT_VIRTUALEDIT
5312 curwin->w_cursor.coladd = 0;
5313#endif
5314
5315 if (flags & (BL_WHITE | BL_SOL))
5316 {
5317 char_u *ptr;
5318
5319 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5320 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5321 ++curwin->w_cursor.col;
5322 }
5323 curwin->w_set_curswant = TRUE;
5324 }
5325}
5326
5327/*
5328 * oneright oneleft cursor_down cursor_up
5329 *
5330 * Move one char {right,left,down,up}.
5331 * Doesn't move onto the NUL past the end of the line.
5332 * Return OK when successful, FAIL when we hit a line of file boundary.
5333 */
5334
5335 int
5336oneright()
5337{
5338 char_u *ptr;
5339#ifdef FEAT_MBYTE
5340 int l;
5341#endif
5342
5343#ifdef FEAT_VIRTUALEDIT
5344 if (virtual_active())
5345 {
5346 pos_T prevpos = curwin->w_cursor;
5347
5348 /* Adjust for multi-wide char (excluding TAB) */
5349 ptr = ml_get_cursor();
5350 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5351#ifdef FEAT_MBYTE
5352 (*mb_ptr2char)(ptr)
5353#else
5354 *ptr
5355#endif
5356 ))
5357 ? ptr2cells(ptr) : 1));
5358 curwin->w_set_curswant = TRUE;
5359 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5360 return (prevpos.col != curwin->w_cursor.col
5361 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5362 }
5363#endif
5364
5365 ptr = ml_get_cursor();
5366#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005367 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 {
5369 /* The character under the cursor is a multi-byte character, move
5370 * several bytes right, but don't end up on the NUL. */
5371 if (ptr[l] == NUL)
5372 return FAIL;
5373 curwin->w_cursor.col += l;
5374 }
5375 else
5376#endif
5377 {
5378 if (*ptr++ == NUL || *ptr == NUL)
5379 return FAIL;
5380 ++curwin->w_cursor.col;
5381 }
5382
5383 curwin->w_set_curswant = TRUE;
5384 return OK;
5385}
5386
5387 int
5388oneleft()
5389{
5390#ifdef FEAT_VIRTUALEDIT
5391 if (virtual_active())
5392 {
5393 int width;
5394 int v = getviscol();
5395
5396 if (v == 0)
5397 return FAIL;
5398
5399# ifdef FEAT_LINEBREAK
5400 /* We might get stuck on 'showbreak', skip over it. */
5401 width = 1;
5402 for (;;)
5403 {
5404 coladvance(v - width);
5405 /* getviscol() is slow, skip it when 'showbreak' is empty and
5406 * there are no multi-byte characters */
5407 if ((*p_sbr == NUL
5408# ifdef FEAT_MBYTE
5409 && !has_mbyte
5410# endif
5411 ) || getviscol() < v)
5412 break;
5413 ++width;
5414 }
5415# else
5416 coladvance(v - 1);
5417# endif
5418
5419 if (curwin->w_cursor.coladd == 1)
5420 {
5421 char_u *ptr;
5422
5423 /* Adjust for multi-wide char (not a TAB) */
5424 ptr = ml_get_cursor();
5425 if (*ptr != TAB && vim_isprintc(
5426# ifdef FEAT_MBYTE
5427 (*mb_ptr2char)(ptr)
5428# else
5429 *ptr
5430# endif
5431 ) && ptr2cells(ptr) > 1)
5432 curwin->w_cursor.coladd = 0;
5433 }
5434
5435 curwin->w_set_curswant = TRUE;
5436 return OK;
5437 }
5438#endif
5439
5440 if (curwin->w_cursor.col == 0)
5441 return FAIL;
5442
5443 curwin->w_set_curswant = TRUE;
5444 --curwin->w_cursor.col;
5445
5446#ifdef FEAT_MBYTE
5447 /* if the character on the left of the current cursor is a multi-byte
5448 * character, move to its first byte */
5449 if (has_mbyte)
5450 mb_adjust_cursor();
5451#endif
5452 return OK;
5453}
5454
5455 int
5456cursor_up(n, upd_topline)
5457 long n;
5458 int upd_topline; /* When TRUE: update topline */
5459{
5460 linenr_T lnum;
5461
5462 if (n > 0)
5463 {
5464 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005465 /* This fails if the cursor is already in the first line or the count
5466 * is larger than the line number and '-' is in 'cpoptions' */
5467 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 return FAIL;
5469 if (n >= lnum)
5470 lnum = 1;
5471 else
5472#ifdef FEAT_FOLDING
5473 if (hasAnyFolding(curwin))
5474 {
5475 /*
5476 * Count each sequence of folded lines as one logical line.
5477 */
5478 /* go to the the start of the current fold */
5479 (void)hasFolding(lnum, &lnum, NULL);
5480
5481 while (n--)
5482 {
5483 /* move up one line */
5484 --lnum;
5485 if (lnum <= 1)
5486 break;
5487 /* If we entered a fold, move to the beginning, unless in
5488 * Insert mode or when 'foldopen' contains "all": it will open
5489 * in a moment. */
5490 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
5491 (void)hasFolding(lnum, &lnum, NULL);
5492 }
5493 if (lnum < 1)
5494 lnum = 1;
5495 }
5496 else
5497#endif
5498 lnum -= n;
5499 curwin->w_cursor.lnum = lnum;
5500 }
5501
5502 /* try to advance to the column we want to be at */
5503 coladvance(curwin->w_curswant);
5504
5505 if (upd_topline)
5506 update_topline(); /* make sure curwin->w_topline is valid */
5507
5508 return OK;
5509}
5510
5511/*
5512 * Cursor down a number of logical lines.
5513 */
5514 int
5515cursor_down(n, upd_topline)
5516 long n;
5517 int upd_topline; /* When TRUE: update topline */
5518{
5519 linenr_T lnum;
5520
5521 if (n > 0)
5522 {
5523 lnum = curwin->w_cursor.lnum;
5524#ifdef FEAT_FOLDING
5525 /* Move to last line of fold, will fail if it's the end-of-file. */
5526 (void)hasFolding(lnum, NULL, &lnum);
5527#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00005528 /* This fails if the cursor is already in the last line or would move
5529 * beyound the last line and '-' is in 'cpoptions' */
5530 if (lnum >= curbuf->b_ml.ml_line_count
5531 || (lnum + n > curbuf->b_ml.ml_line_count
5532 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 return FAIL;
5534 if (lnum + n >= curbuf->b_ml.ml_line_count)
5535 lnum = curbuf->b_ml.ml_line_count;
5536 else
5537#ifdef FEAT_FOLDING
5538 if (hasAnyFolding(curwin))
5539 {
5540 linenr_T last;
5541
5542 /* count each sequence of folded lines as one logical line */
5543 while (n--)
5544 {
5545 if (hasFolding(lnum, NULL, &last))
5546 lnum = last + 1;
5547 else
5548 ++lnum;
5549 if (lnum >= curbuf->b_ml.ml_line_count)
5550 break;
5551 }
5552 if (lnum > curbuf->b_ml.ml_line_count)
5553 lnum = curbuf->b_ml.ml_line_count;
5554 }
5555 else
5556#endif
5557 lnum += n;
5558 curwin->w_cursor.lnum = lnum;
5559 }
5560
5561 /* try to advance to the column we want to be at */
5562 coladvance(curwin->w_curswant);
5563
5564 if (upd_topline)
5565 update_topline(); /* make sure curwin->w_topline is valid */
5566
5567 return OK;
5568}
5569
5570/*
5571 * Stuff the last inserted text in the read buffer.
5572 * Last_insert actually is a copy of the redo buffer, so we
5573 * first have to remove the command.
5574 */
5575 int
5576stuff_inserted(c, count, no_esc)
5577 int c; /* Command character to be inserted */
5578 long count; /* Repeat this many times */
5579 int no_esc; /* Don't add an ESC at the end */
5580{
5581 char_u *esc_ptr;
5582 char_u *ptr;
5583 char_u *last_ptr;
5584 char_u last = NUL;
5585
5586 ptr = get_last_insert();
5587 if (ptr == NULL)
5588 {
5589 EMSG(_(e_noinstext));
5590 return FAIL;
5591 }
5592
5593 /* may want to stuff the command character, to start Insert mode */
5594 if (c != NUL)
5595 stuffcharReadbuff(c);
5596 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
5597 *esc_ptr = NUL; /* remove the ESC */
5598
5599 /* when the last char is either "0" or "^" it will be quoted if no ESC
5600 * comes after it OR if it will inserted more than once and "ptr"
5601 * starts with ^D. -- Acevedo
5602 */
5603 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
5604 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
5605 && (no_esc || (*ptr == Ctrl_D && count > 1)))
5606 {
5607 last = *last_ptr;
5608 *last_ptr = NUL;
5609 }
5610
5611 do
5612 {
5613 stuffReadbuff(ptr);
5614 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
5615 if (last)
5616 stuffReadbuff((char_u *)(last == '0'
5617 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
5618 : IF_EB("\026^", CTRL_V_STR "^")));
5619 }
5620 while (--count > 0);
5621
5622 if (last)
5623 *last_ptr = last;
5624
5625 if (esc_ptr != NULL)
5626 *esc_ptr = ESC; /* put the ESC back */
5627
5628 /* may want to stuff a trailing ESC, to get out of Insert mode */
5629 if (!no_esc)
5630 stuffcharReadbuff(ESC);
5631
5632 return OK;
5633}
5634
5635 char_u *
5636get_last_insert()
5637{
5638 if (last_insert == NULL)
5639 return NULL;
5640 return last_insert + last_insert_skip;
5641}
5642
5643/*
5644 * Get last inserted string, and remove trailing <Esc>.
5645 * Returns pointer to allocated memory (must be freed) or NULL.
5646 */
5647 char_u *
5648get_last_insert_save()
5649{
5650 char_u *s;
5651 int len;
5652
5653 if (last_insert == NULL)
5654 return NULL;
5655 s = vim_strsave(last_insert + last_insert_skip);
5656 if (s != NULL)
5657 {
5658 len = (int)STRLEN(s);
5659 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
5660 s[len - 1] = NUL;
5661 }
5662 return s;
5663}
5664
5665/*
5666 * Check the word in front of the cursor for an abbreviation.
5667 * Called when the non-id character "c" has been entered.
5668 * When an abbreviation is recognized it is removed from the text and
5669 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
5670 */
5671 static int
5672echeck_abbr(c)
5673 int c;
5674{
5675 /* Don't check for abbreviation in paste mode, when disabled and just
5676 * after moving around with cursor keys. */
5677 if (p_paste || no_abbr || arrow_used)
5678 return FALSE;
5679
5680 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
5681 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
5682}
5683
5684/*
5685 * replace-stack functions
5686 *
5687 * When replacing characters, the replaced characters are remembered for each
5688 * new character. This is used to re-insert the old text when backspacing.
5689 *
5690 * There is a NUL headed list of characters for each character that is
5691 * currently in the file after the insertion point. When BS is used, one NUL
5692 * headed list is put back for the deleted character.
5693 *
5694 * For a newline, there are two NUL headed lists. One contains the characters
5695 * that the NL replaced. The extra one stores the characters after the cursor
5696 * that were deleted (always white space).
5697 *
5698 * Replace_offset is normally 0, in which case replace_push will add a new
5699 * character at the end of the stack. If replace_offset is not 0, that many
5700 * characters will be left on the stack above the newly inserted character.
5701 */
5702
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00005703static char_u *replace_stack = NULL;
5704static long replace_stack_nr = 0; /* next entry in replace stack */
5705static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706
5707 void
5708replace_push(c)
5709 int c; /* character that is replaced (NUL is none) */
5710{
5711 char_u *p;
5712
5713 if (replace_stack_nr < replace_offset) /* nothing to do */
5714 return;
5715 if (replace_stack_len <= replace_stack_nr)
5716 {
5717 replace_stack_len += 50;
5718 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
5719 if (p == NULL) /* out of memory */
5720 {
5721 replace_stack_len -= 50;
5722 return;
5723 }
5724 if (replace_stack != NULL)
5725 {
5726 mch_memmove(p, replace_stack,
5727 (size_t)(replace_stack_nr * sizeof(char_u)));
5728 vim_free(replace_stack);
5729 }
5730 replace_stack = p;
5731 }
5732 p = replace_stack + replace_stack_nr - replace_offset;
5733 if (replace_offset)
5734 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
5735 *p = c;
5736 ++replace_stack_nr;
5737}
5738
5739/*
5740 * call replace_push(c) with replace_offset set to the first NUL.
5741 */
5742 static void
5743replace_push_off(c)
5744 int c;
5745{
5746 char_u *p;
5747
5748 p = replace_stack + replace_stack_nr;
5749 for (replace_offset = 1; replace_offset < replace_stack_nr;
5750 ++replace_offset)
5751 if (*--p == NUL)
5752 break;
5753 replace_push(c);
5754 replace_offset = 0;
5755}
5756
5757/*
5758 * Pop one item from the replace stack.
5759 * return -1 if stack empty
5760 * return replaced character or NUL otherwise
5761 */
5762 static int
5763replace_pop()
5764{
5765 if (replace_stack_nr == 0)
5766 return -1;
5767 return (int)replace_stack[--replace_stack_nr];
5768}
5769
5770/*
5771 * Join the top two items on the replace stack. This removes to "off"'th NUL
5772 * encountered.
5773 */
5774 static void
5775replace_join(off)
5776 int off; /* offset for which NUL to remove */
5777{
5778 int i;
5779
5780 for (i = replace_stack_nr; --i >= 0; )
5781 if (replace_stack[i] == NUL && off-- <= 0)
5782 {
5783 --replace_stack_nr;
5784 mch_memmove(replace_stack + i, replace_stack + i + 1,
5785 (size_t)(replace_stack_nr - i));
5786 return;
5787 }
5788}
5789
5790/*
5791 * Pop bytes from the replace stack until a NUL is found, and insert them
5792 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
5793 */
5794 static void
5795replace_pop_ins()
5796{
5797 int cc;
5798 int oldState = State;
5799
5800 State = NORMAL; /* don't want REPLACE here */
5801 while ((cc = replace_pop()) > 0)
5802 {
5803#ifdef FEAT_MBYTE
5804 mb_replace_pop_ins(cc);
5805#else
5806 ins_char(cc);
5807#endif
5808 dec_cursor();
5809 }
5810 State = oldState;
5811}
5812
5813#ifdef FEAT_MBYTE
5814/*
5815 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
5816 * indicates a multi-byte char, pop the other bytes too.
5817 */
5818 static void
5819mb_replace_pop_ins(cc)
5820 int cc;
5821{
5822 int n;
5823 char_u buf[MB_MAXBYTES];
5824 int i;
5825 int c;
5826
5827 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
5828 {
5829 buf[0] = cc;
5830 for (i = 1; i < n; ++i)
5831 buf[i] = replace_pop();
5832 ins_bytes_len(buf, n);
5833 }
5834 else
5835 ins_char(cc);
5836
5837 if (enc_utf8)
5838 /* Handle composing chars. */
5839 for (;;)
5840 {
5841 c = replace_pop();
5842 if (c == -1) /* stack empty */
5843 break;
5844 if ((n = MB_BYTE2LEN(c)) == 1)
5845 {
5846 /* Not a multi-byte char, put it back. */
5847 replace_push(c);
5848 break;
5849 }
5850 else
5851 {
5852 buf[0] = c;
5853 for (i = 1; i < n; ++i)
5854 buf[i] = replace_pop();
5855 if (utf_iscomposing(utf_ptr2char(buf)))
5856 ins_bytes_len(buf, n);
5857 else
5858 {
5859 /* Not a composing char, put it back. */
5860 for (i = n - 1; i >= 0; --i)
5861 replace_push(buf[i]);
5862 break;
5863 }
5864 }
5865 }
5866}
5867#endif
5868
5869/*
5870 * make the replace stack empty
5871 * (called when exiting replace mode)
5872 */
5873 static void
5874replace_flush()
5875{
5876 vim_free(replace_stack);
5877 replace_stack = NULL;
5878 replace_stack_len = 0;
5879 replace_stack_nr = 0;
5880}
5881
5882/*
5883 * Handle doing a BS for one character.
5884 * cc < 0: replace stack empty, just move cursor
5885 * cc == 0: character was inserted, delete it
5886 * cc > 0: character was replaced, put cc (first byte of original char) back
5887 * and check for more characters to be put back
5888 */
5889 static void
5890replace_do_bs()
5891{
5892 int cc;
5893#ifdef FEAT_VREPLACE
5894 int orig_len = 0;
5895 int ins_len;
5896 int orig_vcols = 0;
5897 colnr_T start_vcol;
5898 char_u *p;
5899 int i;
5900 int vcol;
5901#endif
5902
5903 cc = replace_pop();
5904 if (cc > 0)
5905 {
5906#ifdef FEAT_VREPLACE
5907 if (State & VREPLACE_FLAG)
5908 {
5909 /* Get the number of screen cells used by the character we are
5910 * going to delete. */
5911 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
5912 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
5913 }
5914#endif
5915#ifdef FEAT_MBYTE
5916 if (has_mbyte)
5917 {
5918 del_char(FALSE);
5919# ifdef FEAT_VREPLACE
5920 if (State & VREPLACE_FLAG)
5921 orig_len = STRLEN(ml_get_cursor());
5922# endif
5923 replace_push(cc);
5924 }
5925 else
5926#endif
5927 {
5928 pchar_cursor(cc);
5929#ifdef FEAT_VREPLACE
5930 if (State & VREPLACE_FLAG)
5931 orig_len = STRLEN(ml_get_cursor()) - 1;
5932#endif
5933 }
5934 replace_pop_ins();
5935
5936#ifdef FEAT_VREPLACE
5937 if (State & VREPLACE_FLAG)
5938 {
5939 /* Get the number of screen cells used by the inserted characters */
5940 p = ml_get_cursor();
5941 ins_len = STRLEN(p) - orig_len;
5942 vcol = start_vcol;
5943 for (i = 0; i < ins_len; ++i)
5944 {
5945 vcol += chartabsize(p + i, vcol);
5946#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005947 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948#endif
5949 }
5950 vcol -= start_vcol;
5951
5952 /* Delete spaces that were inserted after the cursor to keep the
5953 * text aligned. */
5954 curwin->w_cursor.col += ins_len;
5955 while (vcol > orig_vcols && gchar_cursor() == ' ')
5956 {
5957 del_char(FALSE);
5958 ++orig_vcols;
5959 }
5960 curwin->w_cursor.col -= ins_len;
5961 }
5962#endif
5963
5964 /* mark the buffer as changed and prepare for displaying */
5965 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
5966 }
5967 else if (cc == 0)
5968 (void)del_char(FALSE);
5969}
5970
5971#ifdef FEAT_CINDENT
5972/*
5973 * Return TRUE if C-indenting is on.
5974 */
5975 static int
5976cindent_on()
5977{
5978 return (!p_paste && (curbuf->b_p_cin
5979# ifdef FEAT_EVAL
5980 || *curbuf->b_p_inde != NUL
5981# endif
5982 ));
5983}
5984#endif
5985
5986#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
5987/*
5988 * Re-indent the current line, based on the current contents of it and the
5989 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
5990 * confused what all the part that handles Control-T is doing that I'm not.
5991 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
5992 */
5993
5994 void
5995fixthisline(get_the_indent)
5996 int (*get_the_indent) __ARGS((void));
5997{
5998 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
5999 if (linewhite(curwin->w_cursor.lnum))
6000 did_ai = TRUE; /* delete the indent if the line stays empty */
6001}
6002
6003 void
6004fix_indent()
6005{
6006 if (p_paste)
6007 return;
6008# ifdef FEAT_LISP
6009 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6010 fixthisline(get_lisp_indent);
6011# endif
6012# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6013 else
6014# endif
6015# ifdef FEAT_CINDENT
6016 if (cindent_on())
6017 do_c_expr_indent();
6018# endif
6019}
6020
6021#endif
6022
6023#ifdef FEAT_CINDENT
6024/*
6025 * return TRUE if 'cinkeys' contains the key "keytyped",
6026 * when == '*': Only if key is preceded with '*' (indent before insert)
6027 * when == '!': Only if key is prededed with '!' (don't insert)
6028 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6029 *
6030 * "keytyped" can have a few special values:
6031 * KEY_OPEN_FORW
6032 * KEY_OPEN_BACK
6033 * KEY_COMPLETE just finished completion.
6034 *
6035 * If line_is_empty is TRUE accept keys with '0' before them.
6036 */
6037 int
6038in_cinkeys(keytyped, when, line_is_empty)
6039 int keytyped;
6040 int when;
6041 int line_is_empty;
6042{
6043 char_u *look;
6044 int try_match;
6045 int try_match_word;
6046 char_u *p;
6047 char_u *line;
6048 int icase;
6049 int i;
6050
6051#ifdef FEAT_EVAL
6052 if (*curbuf->b_p_inde != NUL)
6053 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6054 else
6055#endif
6056 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6057 while (*look)
6058 {
6059 /*
6060 * Find out if we want to try a match with this key, depending on
6061 * 'when' and a '*' or '!' before the key.
6062 */
6063 switch (when)
6064 {
6065 case '*': try_match = (*look == '*'); break;
6066 case '!': try_match = (*look == '!'); break;
6067 default: try_match = (*look != '*'); break;
6068 }
6069 if (*look == '*' || *look == '!')
6070 ++look;
6071
6072 /*
6073 * If there is a '0', only accept a match if the line is empty.
6074 * But may still match when typing last char of a word.
6075 */
6076 if (*look == '0')
6077 {
6078 try_match_word = try_match;
6079 if (!line_is_empty)
6080 try_match = FALSE;
6081 ++look;
6082 }
6083 else
6084 try_match_word = FALSE;
6085
6086 /*
6087 * does it look like a control character?
6088 */
6089 if (*look == '^'
6090#ifdef EBCDIC
6091 && (Ctrl_chr(look[1]) != 0)
6092#else
6093 && look[1] >= '?' && look[1] <= '_'
6094#endif
6095 )
6096 {
6097 if (try_match && keytyped == Ctrl_chr(look[1]))
6098 return TRUE;
6099 look += 2;
6100 }
6101 /*
6102 * 'o' means "o" command, open forward.
6103 * 'O' means "O" command, open backward.
6104 */
6105 else if (*look == 'o')
6106 {
6107 if (try_match && keytyped == KEY_OPEN_FORW)
6108 return TRUE;
6109 ++look;
6110 }
6111 else if (*look == 'O')
6112 {
6113 if (try_match && keytyped == KEY_OPEN_BACK)
6114 return TRUE;
6115 ++look;
6116 }
6117
6118 /*
6119 * 'e' means to check for "else" at start of line and just before the
6120 * cursor.
6121 */
6122 else if (*look == 'e')
6123 {
6124 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6125 {
6126 p = ml_get_curline();
6127 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6128 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6129 return TRUE;
6130 }
6131 ++look;
6132 }
6133
6134 /*
6135 * ':' only causes an indent if it is at the end of a label or case
6136 * statement, or when it was before typing the ':' (to fix
6137 * class::method for C++).
6138 */
6139 else if (*look == ':')
6140 {
6141 if (try_match && keytyped == ':')
6142 {
6143 p = ml_get_curline();
6144 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6145 return TRUE;
6146 if (curwin->w_cursor.col > 2
6147 && p[curwin->w_cursor.col - 1] == ':'
6148 && p[curwin->w_cursor.col - 2] == ':')
6149 {
6150 p[curwin->w_cursor.col - 1] = ' ';
6151 i = (cin_iscase(p) || cin_isscopedecl(p)
6152 || cin_islabel(30));
6153 p = ml_get_curline();
6154 p[curwin->w_cursor.col - 1] = ':';
6155 if (i)
6156 return TRUE;
6157 }
6158 }
6159 ++look;
6160 }
6161
6162
6163 /*
6164 * Is it a key in <>, maybe?
6165 */
6166 else if (*look == '<')
6167 {
6168 if (try_match)
6169 {
6170 /*
6171 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6172 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6173 * >, *, : and ! keys if they really really want to.
6174 */
6175 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6176 && keytyped == look[1])
6177 return TRUE;
6178
6179 if (keytyped == get_special_key_code(look + 1))
6180 return TRUE;
6181 }
6182 while (*look && *look != '>')
6183 look++;
6184 while (*look == '>')
6185 look++;
6186 }
6187
6188 /*
6189 * Is it a word: "=word"?
6190 */
6191 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6192 {
6193 ++look;
6194 if (*look == '~')
6195 {
6196 icase = TRUE;
6197 ++look;
6198 }
6199 else
6200 icase = FALSE;
6201 p = vim_strchr(look, ',');
6202 if (p == NULL)
6203 p = look + STRLEN(look);
6204 if ((try_match || try_match_word)
6205 && curwin->w_cursor.col >= (colnr_T)(p - look))
6206 {
6207 int match = FALSE;
6208
6209#ifdef FEAT_INS_EXPAND
6210 if (keytyped == KEY_COMPLETE)
6211 {
6212 char_u *s;
6213
6214 /* Just completed a word, check if it starts with "look".
6215 * search back for the start of a word. */
6216 line = ml_get_curline();
6217# ifdef FEAT_MBYTE
6218 if (has_mbyte)
6219 {
6220 char_u *n;
6221
6222 for (s = line + curwin->w_cursor.col; s > line; s = n)
6223 {
6224 n = mb_prevptr(line, s);
6225 if (!vim_iswordp(n))
6226 break;
6227 }
6228 }
6229 else
6230# endif
6231 for (s = line + curwin->w_cursor.col; s > line; --s)
6232 if (!vim_iswordc(s[-1]))
6233 break;
6234 if (s + (p - look) <= line + curwin->w_cursor.col
6235 && (icase
6236 ? MB_STRNICMP(s, look, p - look)
6237 : STRNCMP(s, look, p - look)) == 0)
6238 match = TRUE;
6239 }
6240 else
6241#endif
6242 /* TODO: multi-byte */
6243 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6244 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6245 {
6246 line = ml_get_cursor();
6247 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6248 || !vim_iswordc(line[-(p - look) - 1]))
6249 && (icase
6250 ? MB_STRNICMP(line - (p - look), look, p - look)
6251 : STRNCMP(line - (p - look), look, p - look))
6252 == 0)
6253 match = TRUE;
6254 }
6255 if (match && try_match_word && !try_match)
6256 {
6257 /* "0=word": Check if there are only blanks before the
6258 * word. */
6259 line = ml_get_curline();
6260 if ((int)(skipwhite(line) - line) !=
6261 (int)(curwin->w_cursor.col - (p - look)))
6262 match = FALSE;
6263 }
6264 if (match)
6265 return TRUE;
6266 }
6267 look = p;
6268 }
6269
6270 /*
6271 * ok, it's a boring generic character.
6272 */
6273 else
6274 {
6275 if (try_match && *look == keytyped)
6276 return TRUE;
6277 ++look;
6278 }
6279
6280 /*
6281 * Skip over ", ".
6282 */
6283 look = skip_to_option_part(look);
6284 }
6285 return FALSE;
6286}
6287#endif /* FEAT_CINDENT */
6288
6289#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6290/*
6291 * Map Hebrew keyboard when in hkmap mode.
6292 */
6293 int
6294hkmap(c)
6295 int c;
6296{
6297 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6298 {
6299 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6300 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6301 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6302 static char_u map[26] =
6303 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6304 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6305 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6306 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6307 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6308 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6309 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6310 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6311 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6312
6313 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6314 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6315 /* '-1'='sofit' */
6316 else if (c == 'x')
6317 return 'X';
6318 else if (c == 'q')
6319 return '\''; /* {geresh}={'} */
6320 else if (c == 246)
6321 return ' '; /* \"o --> ' ' for a german keyboard */
6322 else if (c == 228)
6323 return ' '; /* \"a --> ' ' -- / -- */
6324 else if (c == 252)
6325 return ' '; /* \"u --> ' ' -- / -- */
6326#ifdef EBCDIC
6327 else if (islower(c))
6328#else
6329 /* NOTE: islower() does not do the right thing for us on Linux so we
6330 * do this the same was as 5.7 and previous, so it works correctly on
6331 * all systems. Specifically, the e.g. Delete and Arrow keys are
6332 * munged and won't work if e.g. searching for Hebrew text.
6333 */
6334 else if (c >= 'a' && c <= 'z')
6335#endif
6336 return (int)(map[CharOrdLow(c)] + p_aleph);
6337 else
6338 return c;
6339 }
6340 else
6341 {
6342 switch (c)
6343 {
6344 case '`': return ';';
6345 case '/': return '.';
6346 case '\'': return ',';
6347 case 'q': return '/';
6348 case 'w': return '\'';
6349
6350 /* Hebrew letters - set offset from 'a' */
6351 case ',': c = '{'; break;
6352 case '.': c = 'v'; break;
6353 case ';': c = 't'; break;
6354 default: {
6355 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6356
6357#ifdef EBCDIC
6358 /* see note about islower() above */
6359 if (!islower(c))
6360#else
6361 if (c < 'a' || c > 'z')
6362#endif
6363 return c;
6364 c = str[CharOrdLow(c)];
6365 break;
6366 }
6367 }
6368
6369 return (int)(CharOrdLow(c) + p_aleph);
6370 }
6371}
6372#endif
6373
6374 static void
6375ins_reg()
6376{
6377 int need_redraw = FALSE;
6378 int regname;
6379 int literally = 0;
6380
6381 /*
6382 * If we are going to wait for a character, show a '"'.
6383 */
6384 pc_status = PC_STATUS_UNSET;
6385 if (redrawing() && !char_avail())
6386 {
6387 /* may need to redraw when no more chars available now */
6388 ins_redraw();
6389
6390 edit_putchar('"', TRUE);
6391#ifdef FEAT_CMDL_INFO
6392 add_to_showcmd_c(Ctrl_R);
6393#endif
6394 }
6395
6396#ifdef USE_ON_FLY_SCROLL
6397 dont_scroll = TRUE; /* disallow scrolling here */
6398#endif
6399
6400 /*
6401 * Don't map the register name. This also prevents the mode message to be
6402 * deleted when ESC is hit.
6403 */
6404 ++no_mapping;
6405 regname = safe_vgetc();
6406#ifdef FEAT_LANGMAP
6407 LANGMAP_ADJUST(regname, TRUE);
6408#endif
6409 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6410 {
6411 /* Get a third key for literal register insertion */
6412 literally = regname;
6413#ifdef FEAT_CMDL_INFO
6414 add_to_showcmd_c(literally);
6415#endif
6416 regname = safe_vgetc();
6417#ifdef FEAT_LANGMAP
6418 LANGMAP_ADJUST(regname, TRUE);
6419#endif
6420 }
6421 --no_mapping;
6422
6423#ifdef FEAT_EVAL
6424 /*
6425 * Don't call u_sync() while getting the expression,
6426 * evaluating it or giving an error message for it!
6427 */
6428 ++no_u_sync;
6429 if (regname == '=')
6430 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006431# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006435# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 /* Restore the Input Method. */
6437 if (im_on)
6438 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006441 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6442 {
6443 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 else
6447 {
6448#endif
6449 if (literally == Ctrl_O || literally == Ctrl_P)
6450 {
6451 /* Append the command to the redo buffer. */
6452 AppendCharToRedobuff(Ctrl_R);
6453 AppendCharToRedobuff(literally);
6454 AppendCharToRedobuff(regname);
6455
6456 do_put(regname, BACKWARD, 1L,
6457 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6458 }
6459 else if (insert_reg(regname, literally) == FAIL)
6460 {
6461 vim_beep();
6462 need_redraw = TRUE; /* remove the '"' */
6463 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006464 else if (stop_insert_mode)
6465 /* When the '=' register was used and a function was invoked that
6466 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
6467 * insert anything, need to remove the '"' */
6468 need_redraw = TRUE;
6469
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470#ifdef FEAT_EVAL
6471 }
6472 --no_u_sync;
6473#endif
6474#ifdef FEAT_CMDL_INFO
6475 clear_showcmd();
6476#endif
6477
6478 /* If the inserted register is empty, we need to remove the '"' */
6479 if (need_redraw || stuff_empty())
6480 edit_unputchar();
6481}
6482
6483/*
6484 * CTRL-G commands in Insert mode.
6485 */
6486 static void
6487ins_ctrl_g()
6488{
6489 int c;
6490
6491#ifdef FEAT_INS_EXPAND
6492 /* Right after CTRL-X the cursor will be after the ruler. */
6493 setcursor();
6494#endif
6495
6496 /*
6497 * Don't map the second key. This also prevents the mode message to be
6498 * deleted when ESC is hit.
6499 */
6500 ++no_mapping;
6501 c = safe_vgetc();
6502 --no_mapping;
6503 switch (c)
6504 {
6505 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
6506 case K_UP:
6507 case Ctrl_K:
6508 case 'k': ins_up(TRUE);
6509 break;
6510
6511 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
6512 case K_DOWN:
6513 case Ctrl_J:
6514 case 'j': ins_down(TRUE);
6515 break;
6516
6517 /* CTRL-G u: start new undoable edit */
6518 case 'u': u_sync();
6519 ins_need_undo = TRUE;
6520 break;
6521
6522 /* Unknown CTRL-G command, reserved for future expansion. */
6523 default: vim_beep();
6524 }
6525}
6526
6527/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006528 * CTRL-^ in Insert mode.
6529 */
6530 static void
6531ins_ctrl_hat()
6532{
6533 if (map_to_exists_mode((char_u *)"", LANGMAP))
6534 {
6535 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
6536 if (State & LANGMAP)
6537 {
6538 curbuf->b_p_iminsert = B_IMODE_NONE;
6539 State &= ~LANGMAP;
6540 }
6541 else
6542 {
6543 curbuf->b_p_iminsert = B_IMODE_LMAP;
6544 State |= LANGMAP;
6545#ifdef USE_IM_CONTROL
6546 im_set_active(FALSE);
6547#endif
6548 }
6549 }
6550#ifdef USE_IM_CONTROL
6551 else
6552 {
6553 /* There are no ":lmap" mappings, toggle IM */
6554 if (im_get_status())
6555 {
6556 curbuf->b_p_iminsert = B_IMODE_NONE;
6557 im_set_active(FALSE);
6558 }
6559 else
6560 {
6561 curbuf->b_p_iminsert = B_IMODE_IM;
6562 State &= ~LANGMAP;
6563 im_set_active(TRUE);
6564 }
6565 }
6566#endif
6567 set_iminsert_global();
6568 showmode();
6569#ifdef FEAT_GUI
6570 /* may show different cursor shape or color */
6571 if (gui.in_use)
6572 gui_update_cursor(TRUE, FALSE);
6573#endif
6574#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6575 /* Show/unshow value of 'keymap' in status lines. */
6576 status_redraw_curbuf();
6577#endif
6578}
6579
6580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 * Handle ESC in insert mode.
6582 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
6583 * insert.
6584 */
6585 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00006586ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 long *count;
6588 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00006589 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590{
6591 int temp;
6592 static int disabled_redraw = FALSE;
6593
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006594#ifdef FEAT_SYN_HL
6595 check_spell_redraw();
6596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597#if defined(FEAT_HANGULIN)
6598# if defined(ESC_CHG_TO_ENG_MODE)
6599 hangul_input_state_set(0);
6600# endif
6601 if (composing_hangul)
6602 {
6603 push_raw_key(composing_hangul_buffer, 2);
6604 composing_hangul = 0;
6605 }
6606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607
6608 temp = curwin->w_cursor.col;
6609 if (disabled_redraw)
6610 {
6611 --RedrawingDisabled;
6612 disabled_redraw = FALSE;
6613 }
6614 if (!arrow_used)
6615 {
6616 /*
6617 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00006618 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
6619 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 */
6621 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00006622 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623
6624 /*
6625 * Repeating insert may take a long time. Check for
6626 * interrupt now and then.
6627 */
6628 if (*count > 0)
6629 {
6630 line_breakcheck();
6631 if (got_int)
6632 *count = 0;
6633 }
6634
6635 if (--*count > 0) /* repeat what was typed */
6636 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006637 /* Vi repeats the insert without replacing characters. */
6638 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
6639 State &= ~REPLACE_FLAG;
6640
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 (void)start_redo_ins();
6642 if (cmdchar == 'r' || cmdchar == 'v')
6643 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
6644 ++RedrawingDisabled;
6645 disabled_redraw = TRUE;
6646 return FALSE; /* repeat the insert */
6647 }
6648 stop_insert(&curwin->w_cursor, TRUE);
6649 undisplay_dollar();
6650 }
6651
6652 /* When an autoindent was removed, curswant stays after the
6653 * indent */
6654 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
6655 curwin->w_set_curswant = TRUE;
6656
6657 /* Remember the last Insert position in the '^ mark. */
6658 if (!cmdmod.keepjumps)
6659 curbuf->b_last_insert = curwin->w_cursor;
6660
6661 /*
6662 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00006663 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00006665 if (!nomove
6666 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667#ifdef FEAT_VIRTUALEDIT
6668 || curwin->w_cursor.coladd > 0
6669#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006670 )
6671 && (restart_edit == NUL
6672 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00006674 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006676 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677#ifdef FEAT_RIGHTLEFT
6678 && !revins_on
6679#endif
6680 )
6681 {
6682#ifdef FEAT_VIRTUALEDIT
6683 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
6684 {
6685 oneleft();
6686 if (restart_edit != NUL)
6687 ++curwin->w_cursor.coladd;
6688 }
6689 else
6690#endif
6691 {
6692 --curwin->w_cursor.col;
6693#ifdef FEAT_MBYTE
6694 /* Correct cursor for multi-byte character. */
6695 if (has_mbyte)
6696 mb_adjust_cursor();
6697#endif
6698 }
6699 }
6700
6701#ifdef USE_IM_CONTROL
6702 /* Disable IM to allow typing English directly for Normal mode commands.
6703 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
6704 * well). */
6705 if (!(State & LANGMAP))
6706 im_save_status(&curbuf->b_p_iminsert);
6707 im_set_active(FALSE);
6708#endif
6709
6710 State = NORMAL;
6711 /* need to position cursor again (e.g. when on a TAB ) */
6712 changed_cline_bef_curs();
6713
6714#ifdef FEAT_MOUSE
6715 setmouse();
6716#endif
6717#ifdef CURSOR_SHAPE
6718 ui_cursor_shape(); /* may show different cursor shape */
6719#endif
6720
6721 /*
6722 * When recording or for CTRL-O, need to display the new mode.
6723 * Otherwise remove the mode message.
6724 */
6725 if (Recording || restart_edit != NUL)
6726 showmode();
6727 else if (p_smd)
6728 MSG("");
6729
6730 return TRUE; /* exit Insert mode */
6731}
6732
6733#ifdef FEAT_RIGHTLEFT
6734/*
6735 * Toggle language: hkmap and revins_on.
6736 * Move to end of reverse inserted text.
6737 */
6738 static void
6739ins_ctrl_()
6740{
6741 if (revins_on && revins_chars && revins_scol >= 0)
6742 {
6743 while (gchar_cursor() != NUL && revins_chars--)
6744 ++curwin->w_cursor.col;
6745 }
6746 p_ri = !p_ri;
6747 revins_on = (State == INSERT && p_ri);
6748 if (revins_on)
6749 {
6750 revins_scol = curwin->w_cursor.col;
6751 revins_legal++;
6752 revins_chars = 0;
6753 undisplay_dollar();
6754 }
6755 else
6756 revins_scol = -1;
6757#ifdef FEAT_FKMAP
6758 if (p_altkeymap)
6759 {
6760 /*
6761 * to be consistent also for redo command, using '.'
6762 * set arrow_used to true and stop it - causing to redo
6763 * characters entered in one mode (normal/reverse insert).
6764 */
6765 arrow_used = TRUE;
6766 (void)stop_arrow();
6767 p_fkmap = curwin->w_p_rl ^ p_ri;
6768 if (p_fkmap && p_ri)
6769 State = INSERT;
6770 }
6771 else
6772#endif
6773 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
6774 showmode();
6775}
6776#endif
6777
6778#ifdef FEAT_VISUAL
6779/*
6780 * If 'keymodel' contains "startsel", may start selection.
6781 * Returns TRUE when a CTRL-O and other keys stuffed.
6782 */
6783 static int
6784ins_start_select(c)
6785 int c;
6786{
6787 if (km_startsel)
6788 switch (c)
6789 {
6790 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 case K_PAGEUP:
6793 case K_KPAGEUP:
6794 case K_PAGEDOWN:
6795 case K_KPAGEDOWN:
6796# ifdef MACOS
6797 case K_LEFT:
6798 case K_RIGHT:
6799 case K_UP:
6800 case K_DOWN:
6801 case K_END:
6802 case K_HOME:
6803# endif
6804 if (!(mod_mask & MOD_MASK_SHIFT))
6805 break;
6806 /* FALLTHROUGH */
6807 case K_S_LEFT:
6808 case K_S_RIGHT:
6809 case K_S_UP:
6810 case K_S_DOWN:
6811 case K_S_END:
6812 case K_S_HOME:
6813 /* Start selection right away, the cursor can move with
6814 * CTRL-O when beyond the end of the line. */
6815 start_selection();
6816
6817 /* Execute the key in (insert) Select mode. */
6818 stuffcharReadbuff(Ctrl_O);
6819 if (mod_mask)
6820 {
6821 char_u buf[4];
6822
6823 buf[0] = K_SPECIAL;
6824 buf[1] = KS_MODIFIER;
6825 buf[2] = mod_mask;
6826 buf[3] = NUL;
6827 stuffReadbuff(buf);
6828 }
6829 stuffcharReadbuff(c);
6830 return TRUE;
6831 }
6832 return FALSE;
6833}
6834#endif
6835
6836/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006837 * <Insert> key in Insert mode: toggle insert/remplace mode.
6838 */
6839 static void
6840ins_insert(replaceState)
6841 int replaceState;
6842{
6843#ifdef FEAT_FKMAP
6844 if (p_fkmap && p_ri)
6845 {
6846 beep_flush();
6847 EMSG(farsi_text_3); /* encoded in Farsi */
6848 return;
6849 }
6850#endif
6851
6852#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00006853# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006854 set_vim_var_string(VV_INSERTMODE,
6855 (char_u *)((State & REPLACE_FLAG) ? "i" :
6856 replaceState == VREPLACE ? "v" : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00006857# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006858 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
6859#endif
6860 if (State & REPLACE_FLAG)
6861 State = INSERT | (State & LANGMAP);
6862 else
6863 State = replaceState | (State & LANGMAP);
6864 AppendCharToRedobuff(K_INS);
6865 showmode();
6866#ifdef CURSOR_SHAPE
6867 ui_cursor_shape(); /* may show different cursor shape */
6868#endif
6869}
6870
6871/*
6872 * Pressed CTRL-O in Insert mode.
6873 */
6874 static void
6875ins_ctrl_o()
6876{
6877#ifdef FEAT_VREPLACE
6878 if (State & VREPLACE_FLAG)
6879 restart_edit = 'V';
6880 else
6881#endif
6882 if (State & REPLACE_FLAG)
6883 restart_edit = 'R';
6884 else
6885 restart_edit = 'I';
6886#ifdef FEAT_VIRTUALEDIT
6887 if (virtual_active())
6888 ins_at_eol = FALSE; /* cursor always keeps its column */
6889 else
6890#endif
6891 ins_at_eol = (gchar_cursor() == NUL);
6892}
6893
6894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 * If the cursor is on an indent, ^T/^D insert/delete one
6896 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
6897 * Always round the indent to 'shiftwith', this is compatible
6898 * with vi. But vi only supports ^T and ^D after an
6899 * autoindent, we support it everywhere.
6900 */
6901 static void
6902ins_shift(c, lastc)
6903 int c;
6904 int lastc;
6905{
6906 if (stop_arrow() == FAIL)
6907 return;
6908 AppendCharToRedobuff(c);
6909
6910 /*
6911 * 0^D and ^^D: remove all indent.
6912 */
6913 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
6914 {
6915 --curwin->w_cursor.col;
6916 (void)del_char(FALSE); /* delete the '^' or '0' */
6917 /* In Replace mode, restore the characters that '^' or '0' replaced. */
6918 if (State & REPLACE_FLAG)
6919 replace_pop_ins();
6920 if (lastc == '^')
6921 old_indent = get_indent(); /* remember curr. indent */
6922 change_indent(INDENT_SET, 0, TRUE, 0);
6923 }
6924 else
6925 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
6926
6927 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
6928 did_ai = FALSE;
6929#ifdef FEAT_SMARTINDENT
6930 did_si = FALSE;
6931 can_si = FALSE;
6932 can_si_back = FALSE;
6933#endif
6934#ifdef FEAT_CINDENT
6935 can_cindent = FALSE; /* no cindenting after ^D or ^T */
6936#endif
6937}
6938
6939 static void
6940ins_del()
6941{
6942 int temp;
6943
6944 if (stop_arrow() == FAIL)
6945 return;
6946 if (gchar_cursor() == NUL) /* delete newline */
6947 {
6948 temp = curwin->w_cursor.col;
6949 if (!can_bs(BS_EOL) /* only if "eol" included */
6950 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
6951 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
6952 || do_join(FALSE) == FAIL)
6953 vim_beep();
6954 else
6955 curwin->w_cursor.col = temp;
6956 }
6957 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
6958 vim_beep();
6959 did_ai = FALSE;
6960#ifdef FEAT_SMARTINDENT
6961 did_si = FALSE;
6962 can_si = FALSE;
6963 can_si_back = FALSE;
6964#endif
6965 AppendCharToRedobuff(K_DEL);
6966}
6967
6968/*
6969 * Handle Backspace, delete-word and delete-line in Insert mode.
6970 * Return TRUE when backspace was actually used.
6971 */
6972 static int
6973ins_bs(c, mode, inserted_space_p)
6974 int c;
6975 int mode;
6976 int *inserted_space_p;
6977{
6978 linenr_T lnum;
6979 int cc;
6980 int temp = 0; /* init for GCC */
6981 colnr_T mincol;
6982 int did_backspace = FALSE;
6983 int in_indent;
6984 int oldState;
6985#ifdef FEAT_MBYTE
6986 int p1, p2;
6987#endif
6988
6989 /*
6990 * can't delete anything in an empty file
6991 * can't backup past first character in buffer
6992 * can't backup past starting point unless 'backspace' > 1
6993 * can backup to a previous line if 'backspace' == 0
6994 */
6995 if ( bufempty()
6996 || (
6997#ifdef FEAT_RIGHTLEFT
6998 !revins_on &&
6999#endif
7000 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7001 || (!can_bs(BS_START)
7002 && (arrow_used
7003 || (curwin->w_cursor.lnum == Insstart.lnum
7004 && curwin->w_cursor.col <= Insstart.col)))
7005 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7006 && curwin->w_cursor.col <= ai_col)
7007 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7008 {
7009 vim_beep();
7010 return FALSE;
7011 }
7012
7013 if (stop_arrow() == FAIL)
7014 return FALSE;
7015 in_indent = inindent(0);
7016#ifdef FEAT_CINDENT
7017 if (in_indent)
7018 can_cindent = FALSE;
7019#endif
7020#ifdef FEAT_COMMENTS
7021 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7022#endif
7023#ifdef FEAT_RIGHTLEFT
7024 if (revins_on) /* put cursor after last inserted char */
7025 inc_cursor();
7026#endif
7027
7028#ifdef FEAT_VIRTUALEDIT
7029 /* Virtualedit:
7030 * BACKSPACE_CHAR eats a virtual space
7031 * BACKSPACE_WORD eats all coladd
7032 * BACKSPACE_LINE eats all coladd and keeps going
7033 */
7034 if (curwin->w_cursor.coladd > 0)
7035 {
7036 if (mode == BACKSPACE_CHAR)
7037 {
7038 --curwin->w_cursor.coladd;
7039 return TRUE;
7040 }
7041 if (mode == BACKSPACE_WORD)
7042 {
7043 curwin->w_cursor.coladd = 0;
7044 return TRUE;
7045 }
7046 curwin->w_cursor.coladd = 0;
7047 }
7048#endif
7049
7050 /*
7051 * delete newline!
7052 */
7053 if (curwin->w_cursor.col == 0)
7054 {
7055 lnum = Insstart.lnum;
7056 if (curwin->w_cursor.lnum == Insstart.lnum
7057#ifdef FEAT_RIGHTLEFT
7058 || revins_on
7059#endif
7060 )
7061 {
7062 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7063 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7064 return FALSE;
7065 --Insstart.lnum;
7066 Insstart.col = MAXCOL;
7067 }
7068 /*
7069 * In replace mode:
7070 * cc < 0: NL was inserted, delete it
7071 * cc >= 0: NL was replaced, put original characters back
7072 */
7073 cc = -1;
7074 if (State & REPLACE_FLAG)
7075 cc = replace_pop(); /* returns -1 if NL was inserted */
7076 /*
7077 * In replace mode, in the line we started replacing, we only move the
7078 * cursor.
7079 */
7080 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7081 {
7082 dec_cursor();
7083 }
7084 else
7085 {
7086#ifdef FEAT_VREPLACE
7087 if (!(State & VREPLACE_FLAG)
7088 || curwin->w_cursor.lnum > orig_line_count)
7089#endif
7090 {
7091 temp = gchar_cursor(); /* remember current char */
7092 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007093
7094 /* When "aw" is in 'formatoptions' we must delete the space at
7095 * the end of the line, otherwise the line will be broken
7096 * again when auto-formatting. */
7097 if (has_format_option(FO_AUTO)
7098 && has_format_option(FO_WHITE_PAR))
7099 {
7100 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7101 TRUE);
7102 int len;
7103
7104 len = STRLEN(ptr);
7105 if (len > 0 && ptr[len - 1] == ' ')
7106 ptr[len - 1] = NUL;
7107 }
7108
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 (void)do_join(FALSE);
7110 if (temp == NUL && gchar_cursor() != NUL)
7111 inc_cursor();
7112 }
7113#ifdef FEAT_VREPLACE
7114 else
7115 dec_cursor();
7116#endif
7117
7118 /*
7119 * In REPLACE mode we have to put back the text that was replaced
7120 * by the NL. On the replace stack is first a NUL-terminated
7121 * sequence of characters that were deleted and then the
7122 * characters that NL replaced.
7123 */
7124 if (State & REPLACE_FLAG)
7125 {
7126 /*
7127 * Do the next ins_char() in NORMAL state, to
7128 * prevent ins_char() from replacing characters and
7129 * avoiding showmatch().
7130 */
7131 oldState = State;
7132 State = NORMAL;
7133 /*
7134 * restore characters (blanks) deleted after cursor
7135 */
7136 while (cc > 0)
7137 {
7138 temp = curwin->w_cursor.col;
7139#ifdef FEAT_MBYTE
7140 mb_replace_pop_ins(cc);
7141#else
7142 ins_char(cc);
7143#endif
7144 curwin->w_cursor.col = temp;
7145 cc = replace_pop();
7146 }
7147 /* restore the characters that NL replaced */
7148 replace_pop_ins();
7149 State = oldState;
7150 }
7151 }
7152 did_ai = FALSE;
7153 }
7154 else
7155 {
7156 /*
7157 * Delete character(s) before the cursor.
7158 */
7159#ifdef FEAT_RIGHTLEFT
7160 if (revins_on) /* put cursor on last inserted char */
7161 dec_cursor();
7162#endif
7163 mincol = 0;
7164 /* keep indent */
7165 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7166#ifdef FEAT_RIGHTLEFT
7167 && !revins_on
7168#endif
7169 )
7170 {
7171 temp = curwin->w_cursor.col;
7172 beginline(BL_WHITE);
7173 if (curwin->w_cursor.col < (colnr_T)temp)
7174 mincol = curwin->w_cursor.col;
7175 curwin->w_cursor.col = temp;
7176 }
7177
7178 /*
7179 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7180 */
7181 if ( mode == BACKSPACE_CHAR
7182 && ((p_sta && in_indent)
7183 || (curbuf->b_p_sts
7184 && (*(ml_get_cursor() - 1) == TAB
7185 || (*(ml_get_cursor() - 1) == ' '
7186 && (!*inserted_space_p
7187 || arrow_used))))))
7188 {
7189 int ts;
7190 colnr_T vcol;
7191 colnr_T want_vcol;
7192 int extra = 0;
7193
7194 *inserted_space_p = FALSE;
7195 if (p_sta)
7196 ts = curbuf->b_p_sw;
7197 else
7198 ts = curbuf->b_p_sts;
7199 /* Compute the virtual column where we want to be. Since
7200 * 'showbreak' may get in the way, need to get the last column of
7201 * the previous character. */
7202 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7203 dec_cursor();
7204 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7205 inc_cursor();
7206 want_vcol = (want_vcol / ts) * ts;
7207
7208 /* delete characters until we are at or before want_vcol */
7209 while (vcol > want_vcol
7210 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7211 {
7212 dec_cursor();
7213 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7214 if (State & REPLACE_FLAG)
7215 {
7216 /* Don't delete characters before the insert point when in
7217 * Replace mode */
7218 if (curwin->w_cursor.lnum != Insstart.lnum
7219 || curwin->w_cursor.col >= Insstart.col)
7220 {
7221#if 0 /* what was this for? It causes problems when sw != ts. */
7222 if (State == REPLACE && (int)vcol < want_vcol)
7223 {
7224 (void)del_char(FALSE);
7225 extra = 2; /* don't pop too much */
7226 }
7227 else
7228#endif
7229 replace_do_bs();
7230 }
7231 }
7232 else
7233 (void)del_char(FALSE);
7234 }
7235
7236 /* insert extra spaces until we are at want_vcol */
7237 while (vcol < want_vcol)
7238 {
7239 /* Remember the first char we inserted */
7240 if (curwin->w_cursor.lnum == Insstart.lnum
7241 && curwin->w_cursor.col < Insstart.col)
7242 Insstart.col = curwin->w_cursor.col;
7243
7244#ifdef FEAT_VREPLACE
7245 if (State & VREPLACE_FLAG)
7246 ins_char(' ');
7247 else
7248#endif
7249 {
7250 ins_str((char_u *)" ");
7251 if ((State & REPLACE_FLAG) && extra <= 1)
7252 {
7253 if (extra)
7254 replace_push_off(NUL);
7255 else
7256 replace_push(NUL);
7257 }
7258 if (extra == 2)
7259 extra = 1;
7260 }
7261 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7262 }
7263 }
7264
7265 /*
7266 * Delete upto starting point, start of line or previous word.
7267 */
7268 else do
7269 {
7270#ifdef FEAT_RIGHTLEFT
7271 if (!revins_on) /* put cursor on char to be deleted */
7272#endif
7273 dec_cursor();
7274
7275 /* start of word? */
7276 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7277 {
7278 mode = BACKSPACE_WORD_NOT_SPACE;
7279 temp = vim_iswordc(gchar_cursor());
7280 }
7281 /* end of word? */
7282 else if (mode == BACKSPACE_WORD_NOT_SPACE
7283 && (vim_isspace(cc = gchar_cursor())
7284 || vim_iswordc(cc) != temp))
7285 {
7286#ifdef FEAT_RIGHTLEFT
7287 if (!revins_on)
7288#endif
7289 inc_cursor();
7290#ifdef FEAT_RIGHTLEFT
7291 else if (State & REPLACE_FLAG)
7292 dec_cursor();
7293#endif
7294 break;
7295 }
7296 if (State & REPLACE_FLAG)
7297 replace_do_bs();
7298 else
7299 {
7300#ifdef FEAT_MBYTE
7301 if (enc_utf8 && p_deco)
7302 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7303#endif
7304 (void)del_char(FALSE);
7305#ifdef FEAT_MBYTE
7306 /*
7307 * If p1 or p2 is non-zero, there are combining characters we
7308 * need to take account of. Don't back up before the base
7309 * character.
7310 */
7311 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7312 inc_cursor();
7313#endif
7314#ifdef FEAT_RIGHTLEFT
7315 if (revins_chars)
7316 {
7317 revins_chars--;
7318 revins_legal++;
7319 }
7320 if (revins_on && gchar_cursor() == NUL)
7321 break;
7322#endif
7323 }
7324 /* Just a single backspace?: */
7325 if (mode == BACKSPACE_CHAR)
7326 break;
7327 } while (
7328#ifdef FEAT_RIGHTLEFT
7329 revins_on ||
7330#endif
7331 (curwin->w_cursor.col > mincol
7332 && (curwin->w_cursor.lnum != Insstart.lnum
7333 || curwin->w_cursor.col != Insstart.col)));
7334 did_backspace = TRUE;
7335 }
7336#ifdef FEAT_SMARTINDENT
7337 did_si = FALSE;
7338 can_si = FALSE;
7339 can_si_back = FALSE;
7340#endif
7341 if (curwin->w_cursor.col <= 1)
7342 did_ai = FALSE;
7343 /*
7344 * It's a little strange to put backspaces into the redo
7345 * buffer, but it makes auto-indent a lot easier to deal
7346 * with.
7347 */
7348 AppendCharToRedobuff(c);
7349
7350 /* If deleted before the insertion point, adjust it */
7351 if (curwin->w_cursor.lnum == Insstart.lnum
7352 && curwin->w_cursor.col < Insstart.col)
7353 Insstart.col = curwin->w_cursor.col;
7354
7355 /* vi behaviour: the cursor moves backward but the character that
7356 * was there remains visible
7357 * Vim behaviour: the cursor moves backward and the character that
7358 * was there is erased from the screen.
7359 * We can emulate the vi behaviour by pretending there is a dollar
7360 * displayed even when there isn't.
7361 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7362 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7363 dollar_vcol = curwin->w_virtcol;
7364
7365 return did_backspace;
7366}
7367
7368#ifdef FEAT_MOUSE
7369 static void
7370ins_mouse(c)
7371 int c;
7372{
7373 pos_T tpos;
7374
7375# ifdef FEAT_GUI
7376 /* When GUI is active, also move/paste when 'mouse' is empty */
7377 if (!gui.in_use)
7378# endif
7379 if (!mouse_has(MOUSE_INSERT))
7380 return;
7381
7382 undisplay_dollar();
7383 tpos = curwin->w_cursor;
7384 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7385 {
7386 start_arrow(&tpos);
7387# ifdef FEAT_CINDENT
7388 can_cindent = TRUE;
7389# endif
7390 }
7391
7392#ifdef FEAT_WINDOWS
7393 /* redraw status lines (in case another window became active) */
7394 redraw_statuslines();
7395#endif
7396}
7397
7398 static void
7399ins_mousescroll(up)
7400 int up;
7401{
7402 pos_T tpos;
7403# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7404 win_T *old_curwin;
7405# endif
7406
7407 tpos = curwin->w_cursor;
7408
7409# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7410 old_curwin = curwin;
7411
7412 /* Currently the mouse coordinates are only known in the GUI. */
7413 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7414 {
7415 int row, col;
7416
7417 row = mouse_row;
7418 col = mouse_col;
7419
7420 /* find the window at the pointer coordinates */
7421 curwin = mouse_find_win(&row, &col);
7422 curbuf = curwin->w_buffer;
7423 }
7424 if (curwin == old_curwin)
7425# endif
7426 undisplay_dollar();
7427
7428 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7429 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7430 else
7431 scroll_redraw(up, 3L);
7432
7433# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7434 curwin->w_redr_status = TRUE;
7435
7436 curwin = old_curwin;
7437 curbuf = curwin->w_buffer;
7438# endif
7439
7440 if (!equalpos(curwin->w_cursor, tpos))
7441 {
7442 start_arrow(&tpos);
7443# ifdef FEAT_CINDENT
7444 can_cindent = TRUE;
7445# endif
7446 }
7447}
7448#endif
7449
7450#ifdef FEAT_GUI
7451 void
7452ins_scroll()
7453{
7454 pos_T tpos;
7455
7456 undisplay_dollar();
7457 tpos = curwin->w_cursor;
7458 if (gui_do_scroll())
7459 {
7460 start_arrow(&tpos);
7461# ifdef FEAT_CINDENT
7462 can_cindent = TRUE;
7463# endif
7464 }
7465}
7466
7467 void
7468ins_horscroll()
7469{
7470 pos_T tpos;
7471
7472 undisplay_dollar();
7473 tpos = curwin->w_cursor;
7474 if (gui_do_horiz_scroll())
7475 {
7476 start_arrow(&tpos);
7477# ifdef FEAT_CINDENT
7478 can_cindent = TRUE;
7479# endif
7480 }
7481}
7482#endif
7483
7484 static void
7485ins_left()
7486{
7487 pos_T tpos;
7488
7489#ifdef FEAT_FOLDING
7490 if ((fdo_flags & FDO_HOR) && KeyTyped)
7491 foldOpenCursor();
7492#endif
7493 undisplay_dollar();
7494 tpos = curwin->w_cursor;
7495 if (oneleft() == OK)
7496 {
7497 start_arrow(&tpos);
7498#ifdef FEAT_RIGHTLEFT
7499 /* If exit reversed string, position is fixed */
7500 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
7501 revins_legal++;
7502 revins_chars++;
7503#endif
7504 }
7505
7506 /*
7507 * if 'whichwrap' set for cursor in insert mode may go to
7508 * previous line
7509 */
7510 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
7511 {
7512 start_arrow(&tpos);
7513 --(curwin->w_cursor.lnum);
7514 coladvance((colnr_T)MAXCOL);
7515 curwin->w_set_curswant = TRUE; /* so we stay at the end */
7516 }
7517 else
7518 vim_beep();
7519}
7520
7521 static void
7522ins_home(c)
7523 int c;
7524{
7525 pos_T tpos;
7526
7527#ifdef FEAT_FOLDING
7528 if ((fdo_flags & FDO_HOR) && KeyTyped)
7529 foldOpenCursor();
7530#endif
7531 undisplay_dollar();
7532 tpos = curwin->w_cursor;
7533 if (c == K_C_HOME)
7534 curwin->w_cursor.lnum = 1;
7535 curwin->w_cursor.col = 0;
7536#ifdef FEAT_VIRTUALEDIT
7537 curwin->w_cursor.coladd = 0;
7538#endif
7539 curwin->w_curswant = 0;
7540 start_arrow(&tpos);
7541}
7542
7543 static void
7544ins_end(c)
7545 int c;
7546{
7547 pos_T tpos;
7548
7549#ifdef FEAT_FOLDING
7550 if ((fdo_flags & FDO_HOR) && KeyTyped)
7551 foldOpenCursor();
7552#endif
7553 undisplay_dollar();
7554 tpos = curwin->w_cursor;
7555 if (c == K_C_END)
7556 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7557 coladvance((colnr_T)MAXCOL);
7558 curwin->w_curswant = MAXCOL;
7559
7560 start_arrow(&tpos);
7561}
7562
7563 static void
7564ins_s_left()
7565{
7566#ifdef FEAT_FOLDING
7567 if ((fdo_flags & FDO_HOR) && KeyTyped)
7568 foldOpenCursor();
7569#endif
7570 undisplay_dollar();
7571 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
7572 {
7573 start_arrow(&curwin->w_cursor);
7574 (void)bck_word(1L, FALSE, FALSE);
7575 curwin->w_set_curswant = TRUE;
7576 }
7577 else
7578 vim_beep();
7579}
7580
7581 static void
7582ins_right()
7583{
7584#ifdef FEAT_FOLDING
7585 if ((fdo_flags & FDO_HOR) && KeyTyped)
7586 foldOpenCursor();
7587#endif
7588 undisplay_dollar();
7589 if (gchar_cursor() != NUL || virtual_active()
7590 )
7591 {
7592 start_arrow(&curwin->w_cursor);
7593 curwin->w_set_curswant = TRUE;
7594#ifdef FEAT_VIRTUALEDIT
7595 if (virtual_active())
7596 oneright();
7597 else
7598#endif
7599 {
7600#ifdef FEAT_MBYTE
7601 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007602 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 else
7604#endif
7605 ++curwin->w_cursor.col;
7606 }
7607
7608#ifdef FEAT_RIGHTLEFT
7609 revins_legal++;
7610 if (revins_chars)
7611 revins_chars--;
7612#endif
7613 }
7614 /* if 'whichwrap' set for cursor in insert mode, may move the
7615 * cursor to the next line */
7616 else if (vim_strchr(p_ww, ']') != NULL
7617 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7618 {
7619 start_arrow(&curwin->w_cursor);
7620 curwin->w_set_curswant = TRUE;
7621 ++curwin->w_cursor.lnum;
7622 curwin->w_cursor.col = 0;
7623 }
7624 else
7625 vim_beep();
7626}
7627
7628 static void
7629ins_s_right()
7630{
7631#ifdef FEAT_FOLDING
7632 if ((fdo_flags & FDO_HOR) && KeyTyped)
7633 foldOpenCursor();
7634#endif
7635 undisplay_dollar();
7636 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
7637 || gchar_cursor() != NUL)
7638 {
7639 start_arrow(&curwin->w_cursor);
7640 (void)fwd_word(1L, FALSE, 0);
7641 curwin->w_set_curswant = TRUE;
7642 }
7643 else
7644 vim_beep();
7645}
7646
7647 static void
7648ins_up(startcol)
7649 int startcol; /* when TRUE move to Insstart.col */
7650{
7651 pos_T tpos;
7652 linenr_T old_topline = curwin->w_topline;
7653#ifdef FEAT_DIFF
7654 int old_topfill = curwin->w_topfill;
7655#endif
7656
7657 undisplay_dollar();
7658 tpos = curwin->w_cursor;
7659 if (cursor_up(1L, TRUE) == OK)
7660 {
7661 if (startcol)
7662 coladvance(getvcol_nolist(&Insstart));
7663 if (old_topline != curwin->w_topline
7664#ifdef FEAT_DIFF
7665 || old_topfill != curwin->w_topfill
7666#endif
7667 )
7668 redraw_later(VALID);
7669 start_arrow(&tpos);
7670#ifdef FEAT_CINDENT
7671 can_cindent = TRUE;
7672#endif
7673 }
7674 else
7675 vim_beep();
7676}
7677
7678 static void
7679ins_pageup()
7680{
7681 pos_T tpos;
7682
7683 undisplay_dollar();
7684 tpos = curwin->w_cursor;
7685 if (onepage(BACKWARD, 1L) == OK)
7686 {
7687 start_arrow(&tpos);
7688#ifdef FEAT_CINDENT
7689 can_cindent = TRUE;
7690#endif
7691 }
7692 else
7693 vim_beep();
7694}
7695
7696 static void
7697ins_down(startcol)
7698 int startcol; /* when TRUE move to Insstart.col */
7699{
7700 pos_T tpos;
7701 linenr_T old_topline = curwin->w_topline;
7702#ifdef FEAT_DIFF
7703 int old_topfill = curwin->w_topfill;
7704#endif
7705
7706 undisplay_dollar();
7707 tpos = curwin->w_cursor;
7708 if (cursor_down(1L, TRUE) == OK)
7709 {
7710 if (startcol)
7711 coladvance(getvcol_nolist(&Insstart));
7712 if (old_topline != curwin->w_topline
7713#ifdef FEAT_DIFF
7714 || old_topfill != curwin->w_topfill
7715#endif
7716 )
7717 redraw_later(VALID);
7718 start_arrow(&tpos);
7719#ifdef FEAT_CINDENT
7720 can_cindent = TRUE;
7721#endif
7722 }
7723 else
7724 vim_beep();
7725}
7726
7727 static void
7728ins_pagedown()
7729{
7730 pos_T tpos;
7731
7732 undisplay_dollar();
7733 tpos = curwin->w_cursor;
7734 if (onepage(FORWARD, 1L) == OK)
7735 {
7736 start_arrow(&tpos);
7737#ifdef FEAT_CINDENT
7738 can_cindent = TRUE;
7739#endif
7740 }
7741 else
7742 vim_beep();
7743}
7744
7745#ifdef FEAT_DND
7746 static void
7747ins_drop()
7748{
7749 do_put('~', BACKWARD, 1L, PUT_CURSEND);
7750}
7751#endif
7752
7753/*
7754 * Handle TAB in Insert or Replace mode.
7755 * Return TRUE when the TAB needs to be inserted like a normal character.
7756 */
7757 static int
7758ins_tab()
7759{
7760 int ind;
7761 int i;
7762 int temp;
7763
7764 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
7765 Insstart_blank_vcol = get_nolist_virtcol();
7766 if (echeck_abbr(TAB + ABBR_OFF))
7767 return FALSE;
7768
7769 ind = inindent(0);
7770#ifdef FEAT_CINDENT
7771 if (ind)
7772 can_cindent = FALSE;
7773#endif
7774
7775 /*
7776 * When nothing special, insert TAB like a normal character
7777 */
7778 if (!curbuf->b_p_et
7779 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
7780 && curbuf->b_p_sts == 0)
7781 return TRUE;
7782
7783 if (stop_arrow() == FAIL)
7784 return TRUE;
7785
7786 did_ai = FALSE;
7787#ifdef FEAT_SMARTINDENT
7788 did_si = FALSE;
7789 can_si = FALSE;
7790 can_si_back = FALSE;
7791#endif
7792 AppendToRedobuff((char_u *)"\t");
7793
7794 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
7795 temp = (int)curbuf->b_p_sw;
7796 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
7797 temp = (int)curbuf->b_p_sts;
7798 else /* otherwise use 'tabstop' */
7799 temp = (int)curbuf->b_p_ts;
7800 temp -= get_nolist_virtcol() % temp;
7801
7802 /*
7803 * Insert the first space with ins_char(). It will delete one char in
7804 * replace mode. Insert the rest with ins_str(); it will not delete any
7805 * chars. For VREPLACE mode, we use ins_char() for all characters.
7806 */
7807 ins_char(' ');
7808 while (--temp > 0)
7809 {
7810#ifdef FEAT_VREPLACE
7811 if (State & VREPLACE_FLAG)
7812 ins_char(' ');
7813 else
7814#endif
7815 {
7816 ins_str((char_u *)" ");
7817 if (State & REPLACE_FLAG) /* no char replaced */
7818 replace_push(NUL);
7819 }
7820 }
7821
7822 /*
7823 * When 'expandtab' not set: Replace spaces by TABs where possible.
7824 */
7825 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
7826 {
7827 char_u *ptr;
7828#ifdef FEAT_VREPLACE
7829 char_u *saved_line = NULL; /* init for GCC */
7830 pos_T pos;
7831#endif
7832 pos_T fpos;
7833 pos_T *cursor;
7834 colnr_T want_vcol, vcol;
7835 int change_col = -1;
7836 int save_list = curwin->w_p_list;
7837
7838 /*
7839 * Get the current line. For VREPLACE mode, don't make real changes
7840 * yet, just work on a copy of the line.
7841 */
7842#ifdef FEAT_VREPLACE
7843 if (State & VREPLACE_FLAG)
7844 {
7845 pos = curwin->w_cursor;
7846 cursor = &pos;
7847 saved_line = vim_strsave(ml_get_curline());
7848 if (saved_line == NULL)
7849 return FALSE;
7850 ptr = saved_line + pos.col;
7851 }
7852 else
7853#endif
7854 {
7855 ptr = ml_get_cursor();
7856 cursor = &curwin->w_cursor;
7857 }
7858
7859 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
7860 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
7861 curwin->w_p_list = FALSE;
7862
7863 /* Find first white before the cursor */
7864 fpos = curwin->w_cursor;
7865 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
7866 {
7867 --fpos.col;
7868 --ptr;
7869 }
7870
7871 /* In Replace mode, don't change characters before the insert point. */
7872 if ((State & REPLACE_FLAG)
7873 && fpos.lnum == Insstart.lnum
7874 && fpos.col < Insstart.col)
7875 {
7876 ptr += Insstart.col - fpos.col;
7877 fpos.col = Insstart.col;
7878 }
7879
7880 /* compute virtual column numbers of first white and cursor */
7881 getvcol(curwin, &fpos, &vcol, NULL, NULL);
7882 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
7883
7884 /* Use as many TABs as possible. Beware of 'showbreak' and
7885 * 'linebreak' adding extra virtual columns. */
7886 while (vim_iswhite(*ptr))
7887 {
7888 i = lbr_chartabsize((char_u *)"\t", vcol);
7889 if (vcol + i > want_vcol)
7890 break;
7891 if (*ptr != TAB)
7892 {
7893 *ptr = TAB;
7894 if (change_col < 0)
7895 {
7896 change_col = fpos.col; /* Column of first change */
7897 /* May have to adjust Insstart */
7898 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
7899 Insstart.col = fpos.col;
7900 }
7901 }
7902 ++fpos.col;
7903 ++ptr;
7904 vcol += i;
7905 }
7906
7907 if (change_col >= 0)
7908 {
7909 int repl_off = 0;
7910
7911 /* Skip over the spaces we need. */
7912 while (vcol < want_vcol && *ptr == ' ')
7913 {
7914 vcol += lbr_chartabsize(ptr, vcol);
7915 ++ptr;
7916 ++repl_off;
7917 }
7918 if (vcol > want_vcol)
7919 {
7920 /* Must have a char with 'showbreak' just before it. */
7921 --ptr;
7922 --repl_off;
7923 }
7924 fpos.col += repl_off;
7925
7926 /* Delete following spaces. */
7927 i = cursor->col - fpos.col;
7928 if (i > 0)
7929 {
7930 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
7931 /* correct replace stack. */
7932 if ((State & REPLACE_FLAG)
7933#ifdef FEAT_VREPLACE
7934 && !(State & VREPLACE_FLAG)
7935#endif
7936 )
7937 for (temp = i; --temp >= 0; )
7938 replace_join(repl_off);
7939 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00007940#ifdef FEAT_NETBEANS_INTG
7941 if (usingNetbeans)
7942 {
7943 netbeans_removed(curbuf, fpos.lnum, cursor->col,
7944 (long)(i + 1));
7945 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
7946 (char_u *)"\t", 1);
7947 }
7948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 cursor->col -= i;
7950
7951#ifdef FEAT_VREPLACE
7952 /*
7953 * In VREPLACE mode, we haven't changed anything yet. Do it now by
7954 * backspacing over the changed spacing and then inserting the new
7955 * spacing.
7956 */
7957 if (State & VREPLACE_FLAG)
7958 {
7959 /* Backspace from real cursor to change_col */
7960 backspace_until_column(change_col);
7961
7962 /* Insert each char in saved_line from changed_col to
7963 * ptr-cursor */
7964 ins_bytes_len(saved_line + change_col,
7965 cursor->col - change_col);
7966 }
7967#endif
7968 }
7969
7970#ifdef FEAT_VREPLACE
7971 if (State & VREPLACE_FLAG)
7972 vim_free(saved_line);
7973#endif
7974 curwin->w_p_list = save_list;
7975 }
7976
7977 return FALSE;
7978}
7979
7980/*
7981 * Handle CR or NL in insert mode.
7982 * Return TRUE when out of memory or can't undo.
7983 */
7984 static int
7985ins_eol(c)
7986 int c;
7987{
7988 int i;
7989
7990 if (echeck_abbr(c + ABBR_OFF))
7991 return FALSE;
7992 if (stop_arrow() == FAIL)
7993 return TRUE;
7994 undisplay_dollar();
7995
7996 /*
7997 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
7998 * character under the cursor. Only push a NUL on the replace stack,
7999 * nothing to put back when the NL is deleted.
8000 */
8001 if ((State & REPLACE_FLAG)
8002#ifdef FEAT_VREPLACE
8003 && !(State & VREPLACE_FLAG)
8004#endif
8005 )
8006 replace_push(NUL);
8007
8008 /*
8009 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8010 * replacing the next line, so we push all of the characters left on the
8011 * line onto the replace stack. This is not done here though, it is done
8012 * in open_line().
8013 */
8014
8015#ifdef FEAT_RIGHTLEFT
8016# ifdef FEAT_FKMAP
8017 if (p_altkeymap && p_fkmap)
8018 fkmap(NL);
8019# endif
8020 /* NL in reverse insert will always start in the end of
8021 * current line. */
8022 if (revins_on)
8023 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8024#endif
8025
8026 AppendToRedobuff(NL_STR);
8027 i = open_line(FORWARD,
8028#ifdef FEAT_COMMENTS
8029 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8030#endif
8031 0, old_indent);
8032 old_indent = 0;
8033#ifdef FEAT_CINDENT
8034 can_cindent = TRUE;
8035#endif
8036
8037 return (!i);
8038}
8039
8040#ifdef FEAT_DIGRAPHS
8041/*
8042 * Handle digraph in insert mode.
8043 * Returns character still to be inserted, or NUL when nothing remaining to be
8044 * done.
8045 */
8046 static int
8047ins_digraph()
8048{
8049 int c;
8050 int cc;
8051
8052 pc_status = PC_STATUS_UNSET;
8053 if (redrawing() && !char_avail())
8054 {
8055 /* may need to redraw when no more chars available now */
8056 ins_redraw();
8057
8058 edit_putchar('?', TRUE);
8059#ifdef FEAT_CMDL_INFO
8060 add_to_showcmd_c(Ctrl_K);
8061#endif
8062 }
8063
8064#ifdef USE_ON_FLY_SCROLL
8065 dont_scroll = TRUE; /* disallow scrolling here */
8066#endif
8067
8068 /* don't map the digraph chars. This also prevents the
8069 * mode message to be deleted when ESC is hit */
8070 ++no_mapping;
8071 ++allow_keys;
8072 c = safe_vgetc();
8073 --no_mapping;
8074 --allow_keys;
8075 if (IS_SPECIAL(c) || mod_mask) /* special key */
8076 {
8077#ifdef FEAT_CMDL_INFO
8078 clear_showcmd();
8079#endif
8080 insert_special(c, TRUE, FALSE);
8081 return NUL;
8082 }
8083 if (c != ESC)
8084 {
8085 if (redrawing() && !char_avail())
8086 {
8087 /* may need to redraw when no more chars available now */
8088 ins_redraw();
8089
8090 if (char2cells(c) == 1)
8091 {
8092 /* first remove the '?', otherwise it's restored when typing
8093 * an ESC next */
8094 edit_unputchar();
8095 ins_redraw();
8096 edit_putchar(c, TRUE);
8097 }
8098#ifdef FEAT_CMDL_INFO
8099 add_to_showcmd_c(c);
8100#endif
8101 }
8102 ++no_mapping;
8103 ++allow_keys;
8104 cc = safe_vgetc();
8105 --no_mapping;
8106 --allow_keys;
8107 if (cc != ESC)
8108 {
8109 AppendToRedobuff((char_u *)CTRL_V_STR);
8110 c = getdigraph(c, cc, TRUE);
8111#ifdef FEAT_CMDL_INFO
8112 clear_showcmd();
8113#endif
8114 return c;
8115 }
8116 }
8117 edit_unputchar();
8118#ifdef FEAT_CMDL_INFO
8119 clear_showcmd();
8120#endif
8121 return NUL;
8122}
8123#endif
8124
8125/*
8126 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8127 * Returns the char to be inserted, or NUL if none found.
8128 */
8129 static int
8130ins_copychar(lnum)
8131 linenr_T lnum;
8132{
8133 int c;
8134 int temp;
8135 char_u *ptr, *prev_ptr;
8136
8137 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8138 {
8139 vim_beep();
8140 return NUL;
8141 }
8142
8143 /* try to advance to the cursor column */
8144 temp = 0;
8145 ptr = ml_get(lnum);
8146 prev_ptr = ptr;
8147 validate_virtcol();
8148 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8149 {
8150 prev_ptr = ptr;
8151 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8152 }
8153 if ((colnr_T)temp > curwin->w_virtcol)
8154 ptr = prev_ptr;
8155
8156#ifdef FEAT_MBYTE
8157 c = (*mb_ptr2char)(ptr);
8158#else
8159 c = *ptr;
8160#endif
8161 if (c == NUL)
8162 vim_beep();
8163 return c;
8164}
8165
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008166/*
8167 * CTRL-Y or CTRL-E typed in Insert mode.
8168 */
8169 static int
8170ins_ctrl_ey(tc)
8171 int tc;
8172{
8173 int c = tc;
8174
8175#ifdef FEAT_INS_EXPAND
8176 if (ctrl_x_mode == CTRL_X_SCROLL)
8177 {
8178 if (c == Ctrl_Y)
8179 scrolldown_clamp();
8180 else
8181 scrollup_clamp();
8182 redraw_later(VALID);
8183 }
8184 else
8185#endif
8186 {
8187 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8188 if (c != NUL)
8189 {
8190 long tw_save;
8191
8192 /* The character must be taken literally, insert like it
8193 * was typed after a CTRL-V, and pretend 'textwidth'
8194 * wasn't set. Digits, 'o' and 'x' are special after a
8195 * CTRL-V, don't use it for these. */
8196 if (c < 256 && !isalnum(c))
8197 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8198 tw_save = curbuf->b_p_tw;
8199 curbuf->b_p_tw = -1;
8200 insert_special(c, TRUE, FALSE);
8201 curbuf->b_p_tw = tw_save;
8202#ifdef FEAT_RIGHTLEFT
8203 revins_chars++;
8204 revins_legal++;
8205#endif
8206 c = Ctrl_V; /* pretend CTRL-V is last character */
8207 auto_format(FALSE, TRUE);
8208 }
8209 }
8210 return c;
8211}
8212
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213#ifdef FEAT_SMARTINDENT
8214/*
8215 * Try to do some very smart auto-indenting.
8216 * Used when inserting a "normal" character.
8217 */
8218 static void
8219ins_try_si(c)
8220 int c;
8221{
8222 pos_T *pos, old_pos;
8223 char_u *ptr;
8224 int i;
8225 int temp;
8226
8227 /*
8228 * do some very smart indenting when entering '{' or '}'
8229 */
8230 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8231 {
8232 /*
8233 * for '}' set indent equal to indent of line containing matching '{'
8234 */
8235 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8236 {
8237 old_pos = curwin->w_cursor;
8238 /*
8239 * If the matching '{' has a ')' immediately before it (ignoring
8240 * white-space), then line up with the start of the line
8241 * containing the matching '(' if there is one. This handles the
8242 * case where an "if (..\n..) {" statement continues over multiple
8243 * lines -- webb
8244 */
8245 ptr = ml_get(pos->lnum);
8246 i = pos->col;
8247 if (i > 0) /* skip blanks before '{' */
8248 while (--i > 0 && vim_iswhite(ptr[i]))
8249 ;
8250 curwin->w_cursor.lnum = pos->lnum;
8251 curwin->w_cursor.col = i;
8252 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8253 curwin->w_cursor = *pos;
8254 i = get_indent();
8255 curwin->w_cursor = old_pos;
8256#ifdef FEAT_VREPLACE
8257 if (State & VREPLACE_FLAG)
8258 change_indent(INDENT_SET, i, FALSE, NUL);
8259 else
8260#endif
8261 (void)set_indent(i, SIN_CHANGED);
8262 }
8263 else if (curwin->w_cursor.col > 0)
8264 {
8265 /*
8266 * when inserting '{' after "O" reduce indent, but not
8267 * more than indent of previous line
8268 */
8269 temp = TRUE;
8270 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8271 {
8272 old_pos = curwin->w_cursor;
8273 i = get_indent();
8274 while (curwin->w_cursor.lnum > 1)
8275 {
8276 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8277
8278 /* ignore empty lines and lines starting with '#'. */
8279 if (*ptr != '#' && *ptr != NUL)
8280 break;
8281 }
8282 if (get_indent() >= i)
8283 temp = FALSE;
8284 curwin->w_cursor = old_pos;
8285 }
8286 if (temp)
8287 shift_line(TRUE, FALSE, 1);
8288 }
8289 }
8290
8291 /*
8292 * set indent of '#' always to 0
8293 */
8294 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8295 {
8296 /* remember current indent for next line */
8297 old_indent = get_indent();
8298 (void)set_indent(0, SIN_CHANGED);
8299 }
8300
8301 /* Adjust ai_col, the char at this position can be deleted. */
8302 if (ai_col > curwin->w_cursor.col)
8303 ai_col = curwin->w_cursor.col;
8304}
8305#endif
8306
8307/*
8308 * Get the value that w_virtcol would have when 'list' is off.
8309 * Unless 'cpo' contains the 'L' flag.
8310 */
8311 static colnr_T
8312get_nolist_virtcol()
8313{
8314 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8315 return getvcol_nolist(&curwin->w_cursor);
8316 validate_virtcol();
8317 return curwin->w_virtcol;
8318}