blob: ee574ad6c9ccbaa6c56e26b9522f087e15e09572 [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 Moolenaara6557602006-02-04 22:43:20 +000090/* When "compl_leader" is not NULL only matches that start with this string
91 * are used. */
92static char_u *compl_leader = NULL;
93
94static int compl_used_match; /* Selected one of the matches. When
95 FALSE the match was edited or using
96 the longest common string. */
97
Bram Moolenaar4be06f92005-07-29 22:36:03 +000098/* When the first completion is done "compl_started" is set. When it's
99 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000100static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000101
Bram Moolenaar572cb562005-08-05 21:35:02 +0000102static int compl_matches = 0;
103static char_u *compl_pattern = NULL;
104static int compl_direction = FORWARD;
105static int compl_shows_dir = FORWARD;
106static int compl_pending = FALSE;
107static pos_T compl_startpos;
108static colnr_T compl_col = 0; /* column where the text starts
109 * that is being completed */
110static int save_sm = -1;
111static char_u *compl_orig_text = NULL; /* text as it was before
112 * completion started */
113static int compl_cont_mode = 0;
114static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000116static void ins_ctrl_x __ARGS((void));
117static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int dir));
119static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000120static void ins_compl_upd_pum __ARGS((void));
121static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000122static int pum_wanted __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000123static int pum_two_or_more __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int dir, int flags, int thesaurus));
125static void ins_compl_free __ARGS((void));
126static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000127static int ins_compl_bs __ARGS((void));
128static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000129static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
131static int ins_compl_get_exp __ARGS((pos_T *ini, int dir));
132static void ins_compl_delete __ARGS((void));
133static void ins_compl_insert __ARGS((void));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000134static int ins_compl_next __ARGS((int allow_get_expansion, int count));
135static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000136static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000137static int ins_compl_key2count __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138static int ins_complete __ARGS((int c));
139static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
140#endif /* FEAT_INS_EXPAND */
141
142#define BACKSPACE_CHAR 1
143#define BACKSPACE_WORD 2
144#define BACKSPACE_WORD_NOT_SPACE 3
145#define BACKSPACE_LINE 4
146
147static void ins_redraw __ARGS((void));
148static void ins_ctrl_v __ARGS((void));
149static void undisplay_dollar __ARGS((void));
150static void insert_special __ARGS((int, int, int));
151static void check_auto_format __ARGS((int));
152static void redo_literal __ARGS((int c));
153static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000154#ifdef FEAT_SYN_HL
155static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000156static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000157static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
160static int echeck_abbr __ARGS((int));
161static void replace_push_off __ARGS((int c));
162static int replace_pop __ARGS((void));
163static void replace_join __ARGS((int off));
164static void replace_pop_ins __ARGS((void));
165#ifdef FEAT_MBYTE
166static void mb_replace_pop_ins __ARGS((int cc));
167#endif
168static void replace_flush __ARGS((void));
169static void replace_do_bs __ARGS((void));
170#ifdef FEAT_CINDENT
171static int cindent_on __ARGS((void));
172#endif
173static void ins_reg __ARGS((void));
174static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000175static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000176static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177#ifdef FEAT_RIGHTLEFT
178static void ins_ctrl_ __ARGS((void));
179#endif
180#ifdef FEAT_VISUAL
181static int ins_start_select __ARGS((int c));
182#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000183static void ins_insert __ARGS((int replaceState));
184static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185static void ins_shift __ARGS((int c, int lastc));
186static void ins_del __ARGS((void));
187static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
188#ifdef FEAT_MOUSE
189static void ins_mouse __ARGS((int c));
190static void ins_mousescroll __ARGS((int up));
191#endif
192static void ins_left __ARGS((void));
193static void ins_home __ARGS((int c));
194static void ins_end __ARGS((int c));
195static void ins_s_left __ARGS((void));
196static void ins_right __ARGS((void));
197static void ins_s_right __ARGS((void));
198static void ins_up __ARGS((int startcol));
199static void ins_pageup __ARGS((void));
200static void ins_down __ARGS((int startcol));
201static void ins_pagedown __ARGS((void));
202#ifdef FEAT_DND
203static void ins_drop __ARGS((void));
204#endif
205static int ins_tab __ARGS((void));
206static int ins_eol __ARGS((int c));
207#ifdef FEAT_DIGRAPHS
208static int ins_digraph __ARGS((void));
209#endif
210static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000211static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#ifdef FEAT_SMARTINDENT
213static void ins_try_si __ARGS((int c));
214#endif
215static colnr_T get_nolist_virtcol __ARGS((void));
216
217static colnr_T Insstart_textlen; /* length of line when insert started */
218static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
219
220static char_u *last_insert = NULL; /* the text of the previous insert,
221 K_SPECIAL and CSI are escaped */
222static int last_insert_skip; /* nr of chars in front of previous insert */
223static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000224static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
226#ifdef FEAT_CINDENT
227static int can_cindent; /* may do cindenting on this line */
228#endif
229
230static int old_indent = 0; /* for ^^D command in insert mode */
231
232#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000233static int revins_on; /* reverse insert mode on */
234static int revins_chars; /* how much to skip after edit */
235static int revins_legal; /* was the last char 'legal'? */
236static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#endif
238
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239static int ins_need_undo; /* call u_save() before inserting a
240 char. Set when edit() is called.
241 after that arrow_used is used. */
242
243static int did_add_space = FALSE; /* auto_format() added an extra space
244 under the cursor */
245
246/*
247 * edit(): Start inserting text.
248 *
249 * "cmdchar" can be:
250 * 'i' normal insert command
251 * 'a' normal append command
252 * 'R' replace command
253 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
254 * but still only one <CR> is inserted. The <Esc> is not used for redo.
255 * 'g' "gI" command.
256 * 'V' "gR" command for Virtual Replace mode.
257 * 'v' "gr" command for single character Virtual Replace mode.
258 *
259 * This function is not called recursively. For CTRL-O commands, it returns
260 * and lets the caller handle the Normal-mode command.
261 *
262 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
263 */
264 int
265edit(cmdchar, startln, count)
266 int cmdchar;
267 int startln; /* if set, insert at start of line */
268 long count;
269{
270 int c = 0;
271 char_u *ptr;
272 int lastc;
273 colnr_T mincol;
274 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 int i;
276 int did_backspace = TRUE; /* previous char was backspace */
277#ifdef FEAT_CINDENT
278 int line_is_white = FALSE; /* line is empty before insert */
279#endif
280 linenr_T old_topline = 0; /* topline before insertion */
281#ifdef FEAT_DIFF
282 int old_topfill = -1;
283#endif
284 int inserted_space = FALSE; /* just inserted a space */
285 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000286 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000288 /* Remember whether editing was restarted after CTRL-O. */
289 did_restart_edit = restart_edit;
290
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 /* sleep before redrawing, needed for "CTRL-O :" that results in an
292 * error message */
293 check_for_delay(TRUE);
294
295#ifdef HAVE_SANDBOX
296 /* Don't allow inserting in the sandbox. */
297 if (sandbox != 0)
298 {
299 EMSG(_(e_sandbox));
300 return FALSE;
301 }
302#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000303 /* Don't allow changes in the buffer while editing the cmdline. The
304 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000305 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000306 {
307 EMSG(_(e_secure));
308 return FALSE;
309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310
311#ifdef FEAT_INS_EXPAND
312 ins_compl_clear(); /* clear stuff for CTRL-X mode */
313#endif
314
Bram Moolenaar843ee412004-06-30 16:16:41 +0000315#ifdef FEAT_AUTOCMD
316 /*
317 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
318 */
319 if (cmdchar != 'r' && cmdchar != 'v')
320 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000321# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000322 if (cmdchar == 'R')
323 ptr = (char_u *)"r";
324 else if (cmdchar == 'V')
325 ptr = (char_u *)"v";
326 else
327 ptr = (char_u *)"i";
328 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000329# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000330 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
331 }
332#endif
333
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#ifdef FEAT_MOUSE
335 /*
336 * When doing a paste with the middle mouse button, Insstart is set to
337 * where the paste started.
338 */
339 if (where_paste_started.lnum != 0)
340 Insstart = where_paste_started;
341 else
342#endif
343 {
344 Insstart = curwin->w_cursor;
345 if (startln)
346 Insstart.col = 0;
347 }
348 Insstart_textlen = linetabsize(ml_get_curline());
349 Insstart_blank_vcol = MAXCOL;
350 if (!did_ai)
351 ai_col = 0;
352
353 if (cmdchar != NUL && restart_edit == 0)
354 {
355 ResetRedobuff();
356 AppendNumberToRedobuff(count);
357#ifdef FEAT_VREPLACE
358 if (cmdchar == 'V' || cmdchar == 'v')
359 {
360 /* "gR" or "gr" command */
361 AppendCharToRedobuff('g');
362 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
363 }
364 else
365#endif
366 {
367 AppendCharToRedobuff(cmdchar);
368 if (cmdchar == 'g') /* "gI" command */
369 AppendCharToRedobuff('I');
370 else if (cmdchar == 'r') /* "r<CR>" command */
371 count = 1; /* insert only one <CR> */
372 }
373 }
374
375 if (cmdchar == 'R')
376 {
377#ifdef FEAT_FKMAP
378 if (p_fkmap && p_ri)
379 {
380 beep_flush();
381 EMSG(farsi_text_3); /* encoded in Farsi */
382 State = INSERT;
383 }
384 else
385#endif
386 State = REPLACE;
387 }
388#ifdef FEAT_VREPLACE
389 else if (cmdchar == 'V' || cmdchar == 'v')
390 {
391 State = VREPLACE;
392 replaceState = VREPLACE;
393 orig_line_count = curbuf->b_ml.ml_line_count;
394 vr_lines_changed = 1;
395 }
396#endif
397 else
398 State = INSERT;
399
400 stop_insert_mode = FALSE;
401
402 /*
403 * Need to recompute the cursor position, it might move when the cursor is
404 * on a TAB or special character.
405 */
406 curs_columns(TRUE);
407
408 /*
409 * Enable langmap or IME, indicated by 'iminsert'.
410 * Note that IME may enabled/disabled without us noticing here, thus the
411 * 'iminsert' value may not reflect what is actually used. It is updated
412 * when hitting <Esc>.
413 */
414 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
415 State |= LANGMAP;
416#ifdef USE_IM_CONTROL
417 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
418#endif
419
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420#ifdef FEAT_MOUSE
421 setmouse();
422#endif
423#ifdef FEAT_CMDL_INFO
424 clear_showcmd();
425#endif
426#ifdef FEAT_RIGHTLEFT
427 /* there is no reverse replace mode */
428 revins_on = (State == INSERT && p_ri);
429 if (revins_on)
430 undisplay_dollar();
431 revins_chars = 0;
432 revins_legal = 0;
433 revins_scol = -1;
434#endif
435
436 /*
437 * Handle restarting Insert mode.
438 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
439 * restart_edit non-zero, and something in the stuff buffer.
440 */
441 if (restart_edit != 0 && stuff_empty())
442 {
443#ifdef FEAT_MOUSE
444 /*
445 * After a paste we consider text typed to be part of the insert for
446 * the pasted text. You can backspace over the pasted text too.
447 */
448 if (where_paste_started.lnum)
449 arrow_used = FALSE;
450 else
451#endif
452 arrow_used = TRUE;
453 restart_edit = 0;
454
455 /*
456 * If the cursor was after the end-of-line before the CTRL-O and it is
457 * now at the end-of-line, put it after the end-of-line (this is not
458 * correct in very rare cases).
459 * Also do this if curswant is greater than the current virtual
460 * column. Eg after "^O$" or "^O80|".
461 */
462 validate_virtcol();
463 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000464 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 || curwin->w_curswant > curwin->w_virtcol)
466 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
467 {
468 if (ptr[1] == NUL)
469 ++curwin->w_cursor.col;
470#ifdef FEAT_MBYTE
471 else if (has_mbyte)
472 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000473 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 if (ptr[i] == NUL)
475 curwin->w_cursor.col += i;
476 }
477#endif
478 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000479 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 }
481 else
482 arrow_used = FALSE;
483
484 /* we are in insert mode now, don't need to start it anymore */
485 need_start_insertmode = FALSE;
486
487 /* Need to save the line for undo before inserting the first char. */
488 ins_need_undo = TRUE;
489
490#ifdef FEAT_MOUSE
491 where_paste_started.lnum = 0;
492#endif
493#ifdef FEAT_CINDENT
494 can_cindent = TRUE;
495#endif
496#ifdef FEAT_FOLDING
497 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
498 * restarting. */
499 if (!p_im && did_restart_edit == 0)
500 foldOpenCursor();
501#endif
502
503 /*
504 * If 'showmode' is set, show the current (insert/replace/..) mode.
505 * A warning message for changing a readonly file is given here, before
506 * actually changing anything. It's put after the mode, if any.
507 */
508 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000509 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 i = showmode();
511
512 if (!p_im && did_restart_edit == 0)
513 change_warning(i + 1);
514
515#ifdef CURSOR_SHAPE
516 ui_cursor_shape(); /* may show different cursor shape */
517#endif
518#ifdef FEAT_DIGRAPHS
519 do_digraph(-1); /* clear digraphs */
520#endif
521
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000522 /*
523 * Get the current length of the redo buffer, those characters have to be
524 * skipped if we want to get to the inserted characters.
525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 ptr = get_inserted();
527 if (ptr == NULL)
528 new_insert_skip = 0;
529 else
530 {
531 new_insert_skip = (int)STRLEN(ptr);
532 vim_free(ptr);
533 }
534
535 old_indent = 0;
536
537 /*
538 * Main loop in Insert mode: repeat until Insert mode is left.
539 */
540 for (;;)
541 {
542#ifdef FEAT_RIGHTLEFT
543 if (!revins_legal)
544 revins_scol = -1; /* reset on illegal motions */
545 else
546 revins_legal = 0;
547#endif
548 if (arrow_used) /* don't repeat insert when arrow key used */
549 count = 0;
550
551 if (stop_insert_mode)
552 {
553 /* ":stopinsert" used or 'insertmode' reset */
554 count = 0;
555 goto doESCkey;
556 }
557
558 /* set curwin->w_curswant for next K_DOWN or K_UP */
559 if (!arrow_used)
560 curwin->w_set_curswant = TRUE;
561
562 /* If there is no typeahead may check for timestamps (e.g., for when a
563 * menu invoked a shell command). */
564 if (stuff_empty())
565 {
566 did_check_timestamps = FALSE;
567 if (need_check_timestamps)
568 check_timestamps(FALSE);
569 }
570
571 /*
572 * When emsg() was called msg_scroll will have been set.
573 */
574 msg_scroll = FALSE;
575
576#ifdef FEAT_GUI
577 /* When 'mousefocus' is set a mouse movement may have taken us to
578 * another window. "need_mouse_correct" may then be set because of an
579 * autocommand. */
580 if (need_mouse_correct)
581 gui_mouse_correct();
582#endif
583
584#ifdef FEAT_FOLDING
585 /* Open fold at the cursor line, according to 'foldopen'. */
586 if (fdo_flags & FDO_INSERT)
587 foldOpenCursor();
588 /* Close folds where the cursor isn't, according to 'foldclose' */
589 if (!char_avail())
590 foldCheckClose();
591#endif
592
593 /*
594 * If we inserted a character at the last position of the last line in
595 * the window, scroll the window one line up. This avoids an extra
596 * redraw.
597 * This is detected when the cursor column is smaller after inserting
598 * something.
599 * Don't do this when the topline changed already, it has
600 * already been adjusted (by insertchar() calling open_line())).
601 */
602 if (curbuf->b_mod_set
603 && curwin->w_p_wrap
604 && !did_backspace
605 && curwin->w_topline == old_topline
606#ifdef FEAT_DIFF
607 && curwin->w_topfill == old_topfill
608#endif
609 )
610 {
611 mincol = curwin->w_wcol;
612 validate_cursor_col();
613
614 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
615 && curwin->w_wrow == W_WINROW(curwin)
616 + curwin->w_height - 1 - p_so
617 && (curwin->w_cursor.lnum != curwin->w_topline
618#ifdef FEAT_DIFF
619 || curwin->w_topfill > 0
620#endif
621 ))
622 {
623#ifdef FEAT_DIFF
624 if (curwin->w_topfill > 0)
625 --curwin->w_topfill;
626 else
627#endif
628#ifdef FEAT_FOLDING
629 if (hasFolding(curwin->w_topline, NULL, &old_topline))
630 set_topline(curwin, old_topline + 1);
631 else
632#endif
633 set_topline(curwin, curwin->w_topline + 1);
634 }
635 }
636
637 /* May need to adjust w_topline to show the cursor. */
638 update_topline();
639
640 did_backspace = FALSE;
641
642 validate_cursor(); /* may set must_redraw */
643
644 /*
645 * Redraw the display when no characters are waiting.
646 * Also shows mode, ruler and positions cursor.
647 */
648 ins_redraw();
649
650#ifdef FEAT_SCROLLBIND
651 if (curwin->w_p_scb)
652 do_check_scrollbind(TRUE);
653#endif
654
655 update_curswant();
656 old_topline = curwin->w_topline;
657#ifdef FEAT_DIFF
658 old_topfill = curwin->w_topfill;
659#endif
660
661#ifdef USE_ON_FLY_SCROLL
662 dont_scroll = FALSE; /* allow scrolling here */
663#endif
664
665 /*
666 * Get a character for Insert mode.
667 */
668 lastc = c; /* remember previous char for CTRL-D */
669 c = safe_vgetc();
670
671#ifdef FEAT_RIGHTLEFT
672 if (p_hkmap && KeyTyped)
673 c = hkmap(c); /* Hebrew mode mapping */
674#endif
675#ifdef FEAT_FKMAP
676 if (p_fkmap && KeyTyped)
677 c = fkmap(c); /* Farsi mode mapping */
678#endif
679
680#ifdef FEAT_INS_EXPAND
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000681 /* When the popup menu is visible cursor keys change the selection. */
682 if (c == K_UP && pum_visible())
683 c = Ctrl_P;
684 if (c == K_DOWN && pum_visible())
685 c = Ctrl_N;
686
Bram Moolenaara6557602006-02-04 22:43:20 +0000687 /* When using BS while the popup menu is wanted and still after the
688 * character where completion started: Change the subset of matches to
689 * what matches "compl_leader". */
690 if (compl_started && pum_wanted() && curwin->w_cursor.col > compl_col)
691 {
692 if ((c == K_BS || c == Ctrl_H) && ins_compl_bs())
693 continue;
694
695 /* Editing the word. */
696 if (!compl_used_match && vim_isprintc(c))
697 {
698 ins_compl_addleader(c);
699 continue;
700 }
701 }
702
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
704 * it does fix up the text when finishing completion. */
Bram Moolenaara6557602006-02-04 22:43:20 +0000705 if (c != K_IGNORE && ins_compl_prep(c))
706 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#endif
708
Bram Moolenaar488c6512005-08-11 20:09:58 +0000709 /* CTRL-\ CTRL-N goes to Normal mode,
710 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
711 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 if (c == Ctrl_BSL)
713 {
714 /* may need to redraw when no more chars available now */
715 ins_redraw();
716 ++no_mapping;
717 ++allow_keys;
718 c = safe_vgetc();
719 --no_mapping;
720 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000721 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000723 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 vungetc(c);
725 c = Ctrl_BSL;
726 }
727 else if (c == Ctrl_G && p_im)
728 continue;
729 else
730 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000731 if (c == Ctrl_O)
732 {
733 ins_ctrl_o();
734 ins_at_eol = FALSE; /* cursor keeps its column */
735 nomove = TRUE;
736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 count = 0;
738 goto doESCkey;
739 }
740 }
741
742#ifdef FEAT_DIGRAPHS
743 c = do_digraph(c);
744#endif
745
746#ifdef FEAT_INS_EXPAND
747 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
748 goto docomplete;
749#endif
750 if (c == Ctrl_V || c == Ctrl_Q)
751 {
752 ins_ctrl_v();
753 c = Ctrl_V; /* pretend CTRL-V is last typed character */
754 continue;
755 }
756
757#ifdef FEAT_CINDENT
758 if (cindent_on()
759# ifdef FEAT_INS_EXPAND
760 && ctrl_x_mode == 0
761# endif
762 )
763 {
764 /* A key name preceded by a bang means this key is not to be
765 * inserted. Skip ahead to the re-indenting below.
766 * A key name preceded by a star means that indenting has to be
767 * done before inserting the key. */
768 line_is_white = inindent(0);
769 if (in_cinkeys(c, '!', line_is_white))
770 goto force_cindent;
771 if (can_cindent && in_cinkeys(c, '*', line_is_white)
772 && stop_arrow() == OK)
773 do_c_expr_indent();
774 }
775#endif
776
777#ifdef FEAT_RIGHTLEFT
778 if (curwin->w_p_rl)
779 switch (c)
780 {
781 case K_LEFT: c = K_RIGHT; break;
782 case K_S_LEFT: c = K_S_RIGHT; break;
783 case K_C_LEFT: c = K_C_RIGHT; break;
784 case K_RIGHT: c = K_LEFT; break;
785 case K_S_RIGHT: c = K_S_LEFT; break;
786 case K_C_RIGHT: c = K_C_LEFT; break;
787 }
788#endif
789
790#ifdef FEAT_VISUAL
791 /*
792 * If 'keymodel' contains "startsel", may start selection. If it
793 * does, a CTRL-O and c will be stuffed, we need to get these
794 * characters.
795 */
796 if (ins_start_select(c))
797 continue;
798#endif
799
800 /*
801 * The big switch to handle a character in insert mode.
802 */
803 switch (c)
804 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000805 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (echeck_abbr(ESC + ABBR_OFF))
807 break;
808 /*FALLTHROUGH*/
809
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000810 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811#ifdef FEAT_CMDWIN
812 if (c == Ctrl_C && cmdwin_type != 0)
813 {
814 /* Close the cmdline window. */
815 cmdwin_result = K_IGNORE;
816 got_int = FALSE; /* don't stop executing autocommands et al. */
817 goto doESCkey;
818 }
819#endif
820
821#ifdef UNIX
822do_intr:
823#endif
824 /* when 'insertmode' set, and not halfway a mapping, don't leave
825 * Insert mode */
826 if (goto_im())
827 {
828 if (got_int)
829 {
830 (void)vgetc(); /* flush all buffers */
831 got_int = FALSE;
832 }
833 else
834 vim_beep();
835 break;
836 }
837doESCkey:
838 /*
839 * This is the ONLY return from edit()!
840 */
841 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
842 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000843 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 o_lnum = curwin->w_cursor.lnum;
845
Bram Moolenaar488c6512005-08-11 20:09:58 +0000846 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000847 {
848#ifdef FEAT_AUTOCMD
849 if (cmdchar != 'r' && cmdchar != 'v')
850 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
851 FALSE, curbuf);
852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 continue;
856
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000857 case Ctrl_Z: /* suspend when 'insertmode' set */
858 if (!p_im)
859 goto normalchar; /* insert CTRL-Z as normal char */
860 stuffReadbuff((char_u *)":st\r");
861 c = Ctrl_O;
862 /*FALLTHROUGH*/
863
864 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000865#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000866 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000867 goto docomplete;
868#endif
869 if (echeck_abbr(Ctrl_O + ABBR_OFF))
870 break;
871 ins_ctrl_o();
872 count = 0;
873 goto doESCkey;
874
Bram Moolenaar572cb562005-08-05 21:35:02 +0000875 case K_INS: /* toggle insert/replace mode */
876 case K_KINS:
877 ins_insert(replaceState);
878 break;
879
880 case K_SELECT: /* end of Select mode mapping - ignore */
881 break;
882
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000883#ifdef FEAT_SNIFF
884 case K_SNIFF: /* Sniff command received */
885 stuffcharReadbuff(K_SNIFF);
886 goto doESCkey;
887#endif
888
889 case K_HELP: /* Help key works like <ESC> <Help> */
890 case K_F1:
891 case K_XF1:
892 stuffcharReadbuff(K_HELP);
893 if (p_im)
894 need_start_insertmode = TRUE;
895 goto doESCkey;
896
897#ifdef FEAT_NETBEANS_INTG
898 case K_F21: /* NetBeans command */
899 ++no_mapping; /* don't map the next key hits */
900 i = safe_vgetc();
901 --no_mapping;
902 netbeans_keycommand(i);
903 break;
904#endif
905
906 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 case NUL:
908 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000909 /* For ^@ the trailing ESC will end the insert, unless there is an
910 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
912 && c != Ctrl_A && !p_im)
913 goto doESCkey; /* quit insert mode */
914 inserted_space = FALSE;
915 break;
916
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000917 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 ins_reg();
919 auto_format(FALSE, TRUE);
920 inserted_space = FALSE;
921 break;
922
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000923 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 ins_ctrl_g();
925 break;
926
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000927 case Ctrl_HAT: /* switch input mode and/or langmap */
928 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 break;
930
931#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000932 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 if (!p_ari)
934 goto normalchar;
935 ins_ctrl_();
936 break;
937#endif
938
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000939 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
941 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
942 goto docomplete;
943#endif
944 /* FALLTHROUGH */
945
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000946 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947# ifdef FEAT_INS_EXPAND
948 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
949 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000950 if (has_compl_option(FALSE))
951 goto docomplete;
952 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
954# endif
955 ins_shift(c, lastc);
956 auto_format(FALSE, TRUE);
957 inserted_space = FALSE;
958 break;
959
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000960 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 case K_KDEL:
962 ins_del();
963 auto_format(FALSE, TRUE);
964 break;
965
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000966 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 case Ctrl_H:
968 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
969 auto_format(FALSE, TRUE);
970 break;
971
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000972 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
974 auto_format(FALSE, TRUE);
975 break;
976
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000977 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000978# ifdef FEAT_COMPL_FUNC
979 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000980 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000981 goto docomplete;
982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
984 auto_format(FALSE, TRUE);
985 inserted_space = FALSE;
986 break;
987
988#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000989 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 case K_LEFTMOUSE_NM:
991 case K_LEFTDRAG:
992 case K_LEFTRELEASE:
993 case K_LEFTRELEASE_NM:
994 case K_MIDDLEMOUSE:
995 case K_MIDDLEDRAG:
996 case K_MIDDLERELEASE:
997 case K_RIGHTMOUSE:
998 case K_RIGHTDRAG:
999 case K_RIGHTRELEASE:
1000 case K_X1MOUSE:
1001 case K_X1DRAG:
1002 case K_X1RELEASE:
1003 case K_X2MOUSE:
1004 case K_X2DRAG:
1005 case K_X2RELEASE:
1006 ins_mouse(c);
1007 break;
1008
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001009 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 ins_mousescroll(FALSE);
1011 break;
1012
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001013 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 ins_mousescroll(TRUE);
1015 break;
1016#endif
1017
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001018 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 break;
1020
Bram Moolenaar4770d092006-01-12 23:22:24 +00001021#ifdef FEAT_GUI_W32
1022 /* On Win32 ignore <M-F4>, we get it when closing the window was
1023 * cancelled. */
1024 case K_F4:
1025 if (mod_mask != MOD_MASK_ALT)
1026 goto normalchar;
1027 break;
1028#endif
1029
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030#ifdef FEAT_GUI
1031 case K_VER_SCROLLBAR:
1032 ins_scroll();
1033 break;
1034
1035 case K_HOR_SCROLLBAR:
1036 ins_horscroll();
1037 break;
1038#endif
1039
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001040 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 case K_S_HOME:
1043 case K_C_HOME:
1044 ins_home(c);
1045 break;
1046
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001047 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 case K_S_END:
1050 case K_C_END:
1051 ins_end(c);
1052 break;
1053
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001054 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001055 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1056 ins_s_left();
1057 else
1058 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 break;
1060
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 case K_C_LEFT:
1063 ins_s_left();
1064 break;
1065
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001066 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001067 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1068 ins_s_right();
1069 else
1070 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 break;
1072
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001073 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 case K_C_RIGHT:
1075 ins_s_right();
1076 break;
1077
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001078 case K_UP: /* <Up> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001079 if (mod_mask & MOD_MASK_SHIFT)
1080 ins_pageup();
1081 else
1082 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 break;
1084
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001085 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 case K_PAGEUP:
1087 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001088#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001089 if (pum_visible())
1090 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 ins_pageup();
1093 break;
1094
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001095 case K_DOWN: /* <Down> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001096 if (mod_mask & MOD_MASK_SHIFT)
1097 ins_pagedown();
1098 else
1099 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 break;
1101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 case K_PAGEDOWN:
1104 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001105#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001106 if (pum_visible())
1107 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ins_pagedown();
1110 break;
1111
1112#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001113 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 ins_drop();
1115 break;
1116#endif
1117
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001118 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 c = TAB;
1120 /* FALLTHROUGH */
1121
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001122 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1124 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1125 goto docomplete;
1126#endif
1127 inserted_space = FALSE;
1128 if (ins_tab())
1129 goto normalchar; /* insert TAB as a normal char */
1130 auto_format(FALSE, TRUE);
1131 break;
1132
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001133 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 c = CAR;
1135 /* FALLTHROUGH */
1136 case CAR:
1137 case NL:
1138#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1139 /* In a quickfix window a <CR> jumps to the error under the
1140 * cursor. */
1141 if (bt_quickfix(curbuf) && c == CAR)
1142 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001143 if (curwin->w_llist_ref == NULL) /* quickfix window */
1144 do_cmdline_cmd((char_u *)".cc");
1145 else /* location list window */
1146 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 break;
1148 }
1149#endif
1150#ifdef FEAT_CMDWIN
1151 if (cmdwin_type != 0)
1152 {
1153 /* Execute the command in the cmdline window. */
1154 cmdwin_result = CAR;
1155 goto doESCkey;
1156 }
1157#endif
1158 if (ins_eol(c) && !p_im)
1159 goto doESCkey; /* out of memory */
1160 auto_format(FALSE, FALSE);
1161 inserted_space = FALSE;
1162 break;
1163
1164#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166# ifdef FEAT_INS_EXPAND
1167 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1168 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 if (has_compl_option(TRUE))
1170 goto docomplete;
1171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
1173# endif
1174# ifdef FEAT_DIGRAPHS
1175 c = ins_digraph();
1176 if (c == NUL)
1177 break;
1178# endif
1179 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
1182#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001183 case Ctrl_X: /* Enter CTRL-X mode */
1184 ins_ctrl_x();
1185 break;
1186
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001187 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (ctrl_x_mode != CTRL_X_TAGS)
1189 goto normalchar;
1190 goto docomplete;
1191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001192 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if (ctrl_x_mode != CTRL_X_FILES)
1194 goto normalchar;
1195 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001196
1197 case 's': /* Spelling completion after ^X */
1198 case Ctrl_S:
1199 if (ctrl_x_mode != CTRL_X_SPELL)
1200 goto normalchar;
1201 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202#endif
1203
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001204 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205#ifdef FEAT_INS_EXPAND
1206 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1207#endif
1208 {
1209 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1210 if (p_im)
1211 {
1212 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1213 break;
1214 goto doESCkey;
1215 }
1216 goto normalchar;
1217 }
1218#ifdef FEAT_INS_EXPAND
1219 /* FALLTHROUGH */
1220
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001221 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 case Ctrl_N:
1223 /* if 'complete' is empty then plain ^P is no longer special,
1224 * but it is under other ^X modes */
1225 if (*curbuf->b_p_cpt == NUL
1226 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001227 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 goto normalchar;
1229
1230docomplete:
1231 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001232 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 break;
1234#endif /* FEAT_INS_EXPAND */
1235
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001236 case Ctrl_Y: /* copy from previous line or scroll down */
1237 case Ctrl_E: /* copy from next line or scroll up */
1238 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 break;
1240
1241 default:
1242#ifdef UNIX
1243 if (c == intr_char) /* special interrupt char */
1244 goto do_intr;
1245#endif
1246
1247 /*
1248 * Insert a nomal character.
1249 */
1250normalchar:
1251#ifdef FEAT_SMARTINDENT
1252 /* Try to perform smart-indenting. */
1253 ins_try_si(c);
1254#endif
1255
1256 if (c == ' ')
1257 {
1258 inserted_space = TRUE;
1259#ifdef FEAT_CINDENT
1260 if (inindent(0))
1261 can_cindent = FALSE;
1262#endif
1263 if (Insstart_blank_vcol == MAXCOL
1264 && curwin->w_cursor.lnum == Insstart.lnum)
1265 Insstart_blank_vcol = get_nolist_virtcol();
1266 }
1267
1268 if (vim_iswordc(c) || !echeck_abbr(
1269#ifdef FEAT_MBYTE
1270 /* Add ABBR_OFF for characters above 0x100, this is
1271 * what check_abbr() expects. */
1272 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1273#endif
1274 c))
1275 {
1276 insert_special(c, FALSE, FALSE);
1277#ifdef FEAT_RIGHTLEFT
1278 revins_legal++;
1279 revins_chars++;
1280#endif
1281 }
1282
1283 auto_format(FALSE, TRUE);
1284
1285#ifdef FEAT_FOLDING
1286 /* When inserting a character the cursor line must never be in a
1287 * closed fold. */
1288 foldOpenCursor();
1289#endif
1290 break;
1291 } /* end of switch (c) */
1292
1293 /* If the cursor was moved we didn't just insert a space */
1294 if (arrow_used)
1295 inserted_space = FALSE;
1296
1297#ifdef FEAT_CINDENT
1298 if (can_cindent && cindent_on()
1299# ifdef FEAT_INS_EXPAND
1300 && ctrl_x_mode == 0
1301# endif
1302 )
1303 {
1304force_cindent:
1305 /*
1306 * Indent now if a key was typed that is in 'cinkeys'.
1307 */
1308 if (in_cinkeys(c, ' ', line_is_white))
1309 {
1310 if (stop_arrow() == OK)
1311 /* re-indent the current line */
1312 do_c_expr_indent();
1313 }
1314 }
1315#endif /* FEAT_CINDENT */
1316
1317 } /* for (;;) */
1318 /* NOTREACHED */
1319}
1320
1321/*
1322 * Redraw for Insert mode.
1323 * This is postponed until getting the next character to make '$' in the 'cpo'
1324 * option work correctly.
1325 * Only redraw when there are no characters available. This speeds up
1326 * inserting sequences of characters (e.g., for CTRL-R).
1327 */
1328 static void
1329ins_redraw()
1330{
1331 if (!char_avail())
1332 {
1333 if (must_redraw)
1334 update_screen(0);
1335 else if (clear_cmdline || redraw_cmdline)
1336 showmode(); /* clear cmdline and show mode */
1337 showruler(FALSE);
1338 setcursor();
1339 emsg_on_display = FALSE; /* may remove error message now */
1340 }
1341}
1342
1343/*
1344 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1345 */
1346 static void
1347ins_ctrl_v()
1348{
1349 int c;
1350
1351 /* may need to redraw when no more chars available now */
1352 ins_redraw();
1353
1354 if (redrawing() && !char_avail())
1355 edit_putchar('^', TRUE);
1356 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1357
1358#ifdef FEAT_CMDL_INFO
1359 add_to_showcmd_c(Ctrl_V);
1360#endif
1361
1362 c = get_literal();
1363#ifdef FEAT_CMDL_INFO
1364 clear_showcmd();
1365#endif
1366 insert_special(c, FALSE, TRUE);
1367#ifdef FEAT_RIGHTLEFT
1368 revins_chars++;
1369 revins_legal++;
1370#endif
1371}
1372
1373/*
1374 * Put a character directly onto the screen. It's not stored in a buffer.
1375 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1376 */
1377static int pc_status;
1378#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1379#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1380#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1381#define PC_STATUS_SET 3 /* pc_bytes was filled */
1382#ifdef FEAT_MBYTE
1383static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1384#else
1385static char_u pc_bytes[2]; /* saved bytes */
1386#endif
1387static int pc_attr;
1388static int pc_row;
1389static int pc_col;
1390
1391 void
1392edit_putchar(c, highlight)
1393 int c;
1394 int highlight;
1395{
1396 int attr;
1397
1398 if (ScreenLines != NULL)
1399 {
1400 update_topline(); /* just in case w_topline isn't valid */
1401 validate_cursor();
1402 if (highlight)
1403 attr = hl_attr(HLF_8);
1404 else
1405 attr = 0;
1406 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1407 pc_col = W_WINCOL(curwin);
1408#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1409 pc_status = PC_STATUS_UNSET;
1410#endif
1411#ifdef FEAT_RIGHTLEFT
1412 if (curwin->w_p_rl)
1413 {
1414 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1415# ifdef FEAT_MBYTE
1416 if (has_mbyte)
1417 {
1418 int fix_col = mb_fix_col(pc_col, pc_row);
1419
1420 if (fix_col != pc_col)
1421 {
1422 screen_putchar(' ', pc_row, fix_col, attr);
1423 --curwin->w_wcol;
1424 pc_status = PC_STATUS_RIGHT;
1425 }
1426 }
1427# endif
1428 }
1429 else
1430#endif
1431 {
1432 pc_col += curwin->w_wcol;
1433#ifdef FEAT_MBYTE
1434 if (mb_lefthalve(pc_row, pc_col))
1435 pc_status = PC_STATUS_LEFT;
1436#endif
1437 }
1438
1439 /* save the character to be able to put it back */
1440#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1441 if (pc_status == PC_STATUS_UNSET)
1442#endif
1443 {
1444 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1445 pc_status = PC_STATUS_SET;
1446 }
1447 screen_putchar(c, pc_row, pc_col, attr);
1448 }
1449}
1450
1451/*
1452 * Undo the previous edit_putchar().
1453 */
1454 void
1455edit_unputchar()
1456{
1457 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1458 {
1459#if defined(FEAT_MBYTE)
1460 if (pc_status == PC_STATUS_RIGHT)
1461 ++curwin->w_wcol;
1462 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1463 redrawWinline(curwin->w_cursor.lnum, FALSE);
1464 else
1465#endif
1466 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1467 }
1468}
1469
1470/*
1471 * Called when p_dollar is set: display a '$' at the end of the changed text
1472 * Only works when cursor is in the line that changes.
1473 */
1474 void
1475display_dollar(col)
1476 colnr_T col;
1477{
1478 colnr_T save_col;
1479
1480 if (!redrawing())
1481 return;
1482
1483 cursor_off();
1484 save_col = curwin->w_cursor.col;
1485 curwin->w_cursor.col = col;
1486#ifdef FEAT_MBYTE
1487 if (has_mbyte)
1488 {
1489 char_u *p;
1490
1491 /* If on the last byte of a multi-byte move to the first byte. */
1492 p = ml_get_curline();
1493 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1494 }
1495#endif
1496 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1497 if (curwin->w_wcol < W_WIDTH(curwin))
1498 {
1499 edit_putchar('$', FALSE);
1500 dollar_vcol = curwin->w_virtcol;
1501 }
1502 curwin->w_cursor.col = save_col;
1503}
1504
1505/*
1506 * Call this function before moving the cursor from the normal insert position
1507 * in insert mode.
1508 */
1509 static void
1510undisplay_dollar()
1511{
1512 if (dollar_vcol)
1513 {
1514 dollar_vcol = 0;
1515 redrawWinline(curwin->w_cursor.lnum, FALSE);
1516 }
1517}
1518
1519/*
1520 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1521 * Keep the cursor on the same character.
1522 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1523 * type == INDENT_DEC decrease indent (for CTRL-D)
1524 * type == INDENT_SET set indent to "amount"
1525 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1526 */
1527 void
1528change_indent(type, amount, round, replaced)
1529 int type;
1530 int amount;
1531 int round;
1532 int replaced; /* replaced character, put on replace stack */
1533{
1534 int vcol;
1535 int last_vcol;
1536 int insstart_less; /* reduction for Insstart.col */
1537 int new_cursor_col;
1538 int i;
1539 char_u *ptr;
1540 int save_p_list;
1541 int start_col;
1542 colnr_T vc;
1543#ifdef FEAT_VREPLACE
1544 colnr_T orig_col = 0; /* init for GCC */
1545 char_u *new_line, *orig_line = NULL; /* init for GCC */
1546
1547 /* VREPLACE mode needs to know what the line was like before changing */
1548 if (State & VREPLACE_FLAG)
1549 {
1550 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1551 orig_col = curwin->w_cursor.col;
1552 }
1553#endif
1554
1555 /* for the following tricks we don't want list mode */
1556 save_p_list = curwin->w_p_list;
1557 curwin->w_p_list = FALSE;
1558 vc = getvcol_nolist(&curwin->w_cursor);
1559 vcol = vc;
1560
1561 /*
1562 * For Replace mode we need to fix the replace stack later, which is only
1563 * possible when the cursor is in the indent. Remember the number of
1564 * characters before the cursor if it's possible.
1565 */
1566 start_col = curwin->w_cursor.col;
1567
1568 /* determine offset from first non-blank */
1569 new_cursor_col = curwin->w_cursor.col;
1570 beginline(BL_WHITE);
1571 new_cursor_col -= curwin->w_cursor.col;
1572
1573 insstart_less = curwin->w_cursor.col;
1574
1575 /*
1576 * If the cursor is in the indent, compute how many screen columns the
1577 * cursor is to the left of the first non-blank.
1578 */
1579 if (new_cursor_col < 0)
1580 vcol = get_indent() - vcol;
1581
1582 if (new_cursor_col > 0) /* can't fix replace stack */
1583 start_col = -1;
1584
1585 /*
1586 * Set the new indent. The cursor will be put on the first non-blank.
1587 */
1588 if (type == INDENT_SET)
1589 (void)set_indent(amount, SIN_CHANGED);
1590 else
1591 {
1592#ifdef FEAT_VREPLACE
1593 int save_State = State;
1594
1595 /* Avoid being called recursively. */
1596 if (State & VREPLACE_FLAG)
1597 State = INSERT;
1598#endif
1599 shift_line(type == INDENT_DEC, round, 1);
1600#ifdef FEAT_VREPLACE
1601 State = save_State;
1602#endif
1603 }
1604 insstart_less -= curwin->w_cursor.col;
1605
1606 /*
1607 * Try to put cursor on same character.
1608 * If the cursor is at or after the first non-blank in the line,
1609 * compute the cursor column relative to the column of the first
1610 * non-blank character.
1611 * If we are not in insert mode, leave the cursor on the first non-blank.
1612 * If the cursor is before the first non-blank, position it relative
1613 * to the first non-blank, counted in screen columns.
1614 */
1615 if (new_cursor_col >= 0)
1616 {
1617 /*
1618 * When changing the indent while the cursor is touching it, reset
1619 * Insstart_col to 0.
1620 */
1621 if (new_cursor_col == 0)
1622 insstart_less = MAXCOL;
1623 new_cursor_col += curwin->w_cursor.col;
1624 }
1625 else if (!(State & INSERT))
1626 new_cursor_col = curwin->w_cursor.col;
1627 else
1628 {
1629 /*
1630 * Compute the screen column where the cursor should be.
1631 */
1632 vcol = get_indent() - vcol;
1633 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1634
1635 /*
1636 * Advance the cursor until we reach the right screen column.
1637 */
1638 vcol = last_vcol = 0;
1639 new_cursor_col = -1;
1640 ptr = ml_get_curline();
1641 while (vcol <= (int)curwin->w_virtcol)
1642 {
1643 last_vcol = vcol;
1644#ifdef FEAT_MBYTE
1645 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001646 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 else
1648#endif
1649 ++new_cursor_col;
1650 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1651 }
1652 vcol = last_vcol;
1653
1654 /*
1655 * May need to insert spaces to be able to position the cursor on
1656 * the right screen column.
1657 */
1658 if (vcol != (int)curwin->w_virtcol)
1659 {
1660 curwin->w_cursor.col = new_cursor_col;
1661 i = (int)curwin->w_virtcol - vcol;
1662 ptr = alloc(i + 1);
1663 if (ptr != NULL)
1664 {
1665 new_cursor_col += i;
1666 ptr[i] = NUL;
1667 while (--i >= 0)
1668 ptr[i] = ' ';
1669 ins_str(ptr);
1670 vim_free(ptr);
1671 }
1672 }
1673
1674 /*
1675 * When changing the indent while the cursor is in it, reset
1676 * Insstart_col to 0.
1677 */
1678 insstart_less = MAXCOL;
1679 }
1680
1681 curwin->w_p_list = save_p_list;
1682
1683 if (new_cursor_col <= 0)
1684 curwin->w_cursor.col = 0;
1685 else
1686 curwin->w_cursor.col = new_cursor_col;
1687 curwin->w_set_curswant = TRUE;
1688 changed_cline_bef_curs();
1689
1690 /*
1691 * May have to adjust the start of the insert.
1692 */
1693 if (State & INSERT)
1694 {
1695 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1696 {
1697 if ((int)Insstart.col <= insstart_less)
1698 Insstart.col = 0;
1699 else
1700 Insstart.col -= insstart_less;
1701 }
1702 if ((int)ai_col <= insstart_less)
1703 ai_col = 0;
1704 else
1705 ai_col -= insstart_less;
1706 }
1707
1708 /*
1709 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1710 * If the number of characters before the cursor decreased, need to pop a
1711 * few characters from the replace stack.
1712 * If the number of characters before the cursor increased, need to push a
1713 * few NULs onto the replace stack.
1714 */
1715 if (REPLACE_NORMAL(State) && start_col >= 0)
1716 {
1717 while (start_col > (int)curwin->w_cursor.col)
1718 {
1719 replace_join(0); /* remove a NUL from the replace stack */
1720 --start_col;
1721 }
1722 while (start_col < (int)curwin->w_cursor.col || replaced)
1723 {
1724 replace_push(NUL);
1725 if (replaced)
1726 {
1727 replace_push(replaced);
1728 replaced = NUL;
1729 }
1730 ++start_col;
1731 }
1732 }
1733
1734#ifdef FEAT_VREPLACE
1735 /*
1736 * For VREPLACE mode, we also have to fix the replace stack. In this case
1737 * it is always possible because we backspace over the whole line and then
1738 * put it back again the way we wanted it.
1739 */
1740 if (State & VREPLACE_FLAG)
1741 {
1742 /* If orig_line didn't allocate, just return. At least we did the job,
1743 * even if you can't backspace. */
1744 if (orig_line == NULL)
1745 return;
1746
1747 /* Save new line */
1748 new_line = vim_strsave(ml_get_curline());
1749 if (new_line == NULL)
1750 return;
1751
1752 /* We only put back the new line up to the cursor */
1753 new_line[curwin->w_cursor.col] = NUL;
1754
1755 /* Put back original line */
1756 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1757 curwin->w_cursor.col = orig_col;
1758
1759 /* Backspace from cursor to start of line */
1760 backspace_until_column(0);
1761
1762 /* Insert new stuff into line again */
1763 ins_bytes(new_line);
1764
1765 vim_free(new_line);
1766 }
1767#endif
1768}
1769
1770/*
1771 * Truncate the space at the end of a line. This is to be used only in an
1772 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1773 * modes.
1774 */
1775 void
1776truncate_spaces(line)
1777 char_u *line;
1778{
1779 int i;
1780
1781 /* find start of trailing white space */
1782 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1783 {
1784 if (State & REPLACE_FLAG)
1785 replace_join(0); /* remove a NUL from the replace stack */
1786 }
1787 line[i + 1] = NUL;
1788}
1789
1790#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1791 || defined(FEAT_COMMENTS) || defined(PROTO)
1792/*
1793 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1794 * modes correctly. May also be used when not in insert mode at all.
1795 */
1796 void
1797backspace_until_column(col)
1798 int col;
1799{
1800 while ((int)curwin->w_cursor.col > col)
1801 {
1802 curwin->w_cursor.col--;
1803 if (State & REPLACE_FLAG)
1804 replace_do_bs();
1805 else
1806 (void)del_char(FALSE);
1807 }
1808}
1809#endif
1810
1811#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1812/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001813 * CTRL-X pressed in Insert mode.
1814 */
1815 static void
1816ins_ctrl_x()
1817{
1818 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1819 * CTRL-V works like CTRL-N */
1820 if (ctrl_x_mode != CTRL_X_CMDLINE)
1821 {
1822 /* if the next ^X<> won't ADD nothing, then reset
1823 * compl_cont_status */
1824 if (compl_cont_status & CONT_N_ADDS)
1825 compl_cont_status = (compl_cont_status | CONT_INTRPT);
1826 else
1827 compl_cont_status = 0;
1828 /* We're not sure which CTRL-X mode it will be yet */
1829 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1830 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1831 edit_submode_pre = NULL;
1832 showmode();
1833 }
1834}
1835
1836/*
1837 * Return TRUE if the 'dict' or 'tsr' option can be used.
1838 */
1839 static int
1840has_compl_option(dict_opt)
1841 int dict_opt;
1842{
1843 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1844 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1845 {
1846 ctrl_x_mode = 0;
1847 edit_submode = NULL;
1848 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1849 : (char_u *)_("'thesaurus' option is empty"),
1850 hl_attr(HLF_E));
1851 if (emsg_silent == 0)
1852 {
1853 vim_beep();
1854 setcursor();
1855 out_flush();
1856 ui_delay(2000L, FALSE);
1857 }
1858 return FALSE;
1859 }
1860 return TRUE;
1861}
1862
1863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1865 * This depends on the current mode.
1866 */
1867 int
1868vim_is_ctrl_x_key(c)
1869 int c;
1870{
1871 /* Always allow ^R - let it's results then be checked */
1872 if (c == Ctrl_R)
1873 return TRUE;
1874
Bram Moolenaare3226be2005-12-18 22:10:00 +00001875 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001876 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001877 return TRUE;
1878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 switch (ctrl_x_mode)
1880 {
1881 case 0: /* Not in any CTRL-X mode */
1882 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1883 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001884 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1886 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1887 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001888 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1889 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 case CTRL_X_SCROLL:
1891 return (c == Ctrl_Y || c == Ctrl_E);
1892 case CTRL_X_WHOLE_LINE:
1893 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1894 case CTRL_X_FILES:
1895 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1896 case CTRL_X_DICTIONARY:
1897 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1898 case CTRL_X_THESAURUS:
1899 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1900 case CTRL_X_TAGS:
1901 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1902#ifdef FEAT_FIND_ID
1903 case CTRL_X_PATH_PATTERNS:
1904 return (c == Ctrl_P || c == Ctrl_N);
1905 case CTRL_X_PATH_DEFINES:
1906 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1907#endif
1908 case CTRL_X_CMDLINE:
1909 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1910 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001911#ifdef FEAT_COMPL_FUNC
1912 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001913 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001914 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001915 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001916#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001917 case CTRL_X_SPELL:
1918 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 }
1920 EMSG(_(e_internal));
1921 return FALSE;
1922}
1923
1924/*
1925 * This is like ins_compl_add(), but if ic and inf are set, then the
1926 * case of the originally typed text is used, and the case of the completed
1927 * text is infered, ie this tries to work out what case you probably wanted
1928 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001929 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 */
1931 int
Bram Moolenaar572cb562005-08-05 21:35:02 +00001932ins_compl_add_infercase(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 char_u *str;
1934 int len;
1935 char_u *fname;
1936 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001937 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938{
1939 int has_lower = FALSE;
1940 int was_letter = FALSE;
1941 int idx;
1942
1943 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
1944 {
1945 /* Infer case of completed part -- webb */
1946 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001947 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948
1949 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001950 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001952 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 {
1954 has_lower = TRUE;
1955 if (isupper(IObuff[idx]))
1956 {
1957 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001958 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
1960 break;
1961 }
1962 }
1963 }
1964
1965 /*
1966 * Rule 2: No lower case, 2nd consecutive letter converted to
1967 * upper case.
1968 */
1969 if (!has_lower)
1970 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001971 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001973 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 && islower(IObuff[idx]))
1975 {
1976 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001977 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
1979 break;
1980 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001981 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 }
1983 }
1984
1985 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001986 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987
Bram Moolenaar572cb562005-08-05 21:35:02 +00001988 return ins_compl_add(IObuff, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00001990 return ins_compl_add(str, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991}
1992
1993/*
1994 * Add a match to the list of matches.
1995 * If the given string is already in the list of completions, then return
1996 * FAIL, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaar572cb562005-08-05 21:35:02 +00001997 * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb.
1998 *
1999 * New:
2000 * If the given string is already in the list of completions, then return
2001 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2002 * maybe because alloc() returns NULL, then FAIL is returned -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002004 int
2005ins_compl_add(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 char_u *str;
2007 int len;
2008 char_u *fname;
2009 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002010 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002012 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013
2014 ui_breakcheck();
2015 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002016 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (len < 0)
2018 len = (int)STRLEN(str);
2019
2020 /*
2021 * If the same match is already present, don't add it.
2022 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002023 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002025 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 do
2027 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002028 if ( !(match->cp_flags & ORIGINAL_TEXT)
2029 && STRNCMP(match->cp_str, str, (size_t)len) == 0
2030 && match->cp_str[len] == NUL)
2031 return NOTDONE;
2032 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002033 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 }
2035
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002036 /* Remove any popup menu before changing the list of matches. */
2037 ins_compl_del_pum();
2038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 /*
2040 * Allocate a new match structure.
2041 * Copy the values to the new match structure.
2042 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002043 match = (compl_T *)alloc((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002045 return FAIL;
2046 match->cp_number = -1;
2047 if (flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002049 match->cp_number = 0;
2050 match->cp_str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002052 else if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 {
2054 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002055 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 }
2057 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002058 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2060 * - NULL otherwise. --Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002061 if (fname && compl_curr_match && compl_curr_match->cp_fname
2062 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
2063 match->cp_fname = compl_curr_match->cp_fname;
2064 else if (fname && (match->cp_fname = vim_strsave(fname)) != NULL)
2065 flags |= FREE_FNAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002067 match->cp_fname = NULL;
2068 match->cp_flags = flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070 /*
2071 * Link the new match structure in the list of matches.
2072 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002073 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002074 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 else if (dir == FORWARD)
2076 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002077 match->cp_next = compl_curr_match->cp_next;
2078 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
2080 else /* BACKWARD */
2081 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002082 match->cp_next = compl_curr_match;
2083 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002085 if (match->cp_next)
2086 match->cp_next->cp_prev = match;
2087 if (match->cp_prev)
2088 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002090 compl_first_match = match;
2091 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092
2093 return OK;
2094}
2095
2096/*
2097 * Add an array of matches to the list of matches.
2098 * Frees matches[].
2099 */
2100 static void
2101ins_compl_add_matches(num_matches, matches, dir)
2102 int num_matches;
2103 char_u **matches;
2104 int dir;
2105{
2106 int i;
2107 int add_r = OK;
2108 int ldir = dir;
2109
Bram Moolenaar572cb562005-08-05 21:35:02 +00002110 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 if ((add_r = ins_compl_add(matches[i], -1, NULL, ldir, 0)) == OK)
2112 /* if dir was BACKWARD then honor it just once */
2113 ldir = FORWARD;
2114 FreeWild(num_matches, matches);
2115}
2116
2117/* Make the completion list cyclic.
2118 * Return the number of matches (excluding the original).
2119 */
2120 static int
2121ins_compl_make_cyclic()
2122{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002123 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 int count = 0;
2125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002126 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
2128 /*
2129 * Find the end of the list.
2130 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002131 match = compl_first_match;
2132 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002133 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002135 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 ++count;
2137 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002138 match->cp_next = compl_first_match;
2139 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 }
2141 return count;
2142}
2143
Bram Moolenaar9372a112005-12-06 19:59:18 +00002144/* "compl_match_array" points the currently displayed list of entries in the
2145 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002146static char_u **compl_match_array = NULL;
2147static int compl_match_arraysize;
2148
2149/*
2150 * Update the screen and when there is any scrolling remove the popup menu.
2151 */
2152 static void
2153ins_compl_upd_pum()
2154{
2155 int h;
2156
2157 if (compl_match_array != NULL)
2158 {
2159 h = curwin->w_cline_height;
2160 update_screen(0);
2161 if (h != curwin->w_cline_height)
2162 ins_compl_del_pum();
2163 }
2164}
2165
2166/*
2167 * Remove any popup menu.
2168 */
2169 static void
2170ins_compl_del_pum()
2171{
2172 if (compl_match_array != NULL)
2173 {
2174 pum_undisplay();
2175 vim_free(compl_match_array);
2176 compl_match_array = NULL;
2177 }
2178}
2179
2180/*
2181 * Return TRUE if the popup menu should be displayed.
2182 */
2183 static int
2184pum_wanted()
2185{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002186 /* 'completeopt' must contain "menu" */
2187 if (*p_cot == NUL)
2188 return FALSE;
2189
2190 /* The display looks bad on a B&W display. */
2191 if (t_colors < 8
2192#ifdef FEAT_GUI
2193 && !gui.in_use
2194#endif
2195 )
2196 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002197 return TRUE;
2198}
2199
2200/*
2201 * Return TRUE if there are two or more matches to be shown in the popup menu.
2202 */
2203 static int
2204pum_two_or_more()
2205{
2206 compl_T *compl;
2207 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002208
2209 /* Don't display the popup menu if there are no matches or there is only
2210 * one (ignoring the original text). */
2211 compl = compl_first_match;
2212 i = 0;
2213 do
2214 {
2215 if (compl == NULL
2216 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2217 break;
2218 compl = compl->cp_next;
2219 } while (compl != compl_first_match);
2220
2221 return (i >= 2);
2222}
2223
2224/*
2225 * Show the popup menu for the list of matches.
2226 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002227 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002228ins_compl_show_pum()
2229{
2230 compl_T *compl;
2231 int i;
2232 int cur = -1;
2233 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002234 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002235
Bram Moolenaara6557602006-02-04 22:43:20 +00002236 if (!pum_wanted() || !pum_two_or_more())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002237 return;
2238
2239 /* Update the screen before drawing the popup menu over it. */
2240 update_screen(0);
2241
2242 if (compl_match_array == NULL)
2243 {
2244 /* Need to build the popup menu list. */
2245 compl_match_arraysize = 0;
2246 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002247 if (compl_leader != NULL)
2248 lead_len = STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002249 do
2250 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002251 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2252 && (compl_leader == NULL
2253 || STRNCMP(compl->cp_str, compl_leader, lead_len) == 0))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002254 ++compl_match_arraysize;
2255 compl = compl->cp_next;
2256 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002257 if (compl_match_arraysize == 0)
2258 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002259 compl_match_array = (char_u **)alloc((unsigned)(sizeof(char_u **)
2260 * compl_match_arraysize));
2261 if (compl_match_array != NULL)
2262 {
2263 i = 0;
2264 compl = compl_first_match;
2265 do
2266 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002267 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2268 && (compl_leader == NULL
2269 || STRNCMP(compl->cp_str, compl_leader,
2270 lead_len) == 0))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002271 {
2272 if (compl == compl_shown_match)
2273 cur = i;
2274 compl_match_array[i++] = compl->cp_str;
2275 }
2276 compl = compl->cp_next;
2277 } while (compl != NULL && compl != compl_first_match);
2278 }
2279 }
2280 else
2281 {
2282 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002283 for (i = 0; i < compl_match_arraysize; ++i)
2284 if (compl_match_array[i] == compl_shown_match->cp_str)
2285 break;
2286 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002287 }
2288
2289 if (compl_match_array != NULL)
2290 {
2291 /* Compute the screen column of the start of the completed text.
2292 * Use the cursor to get all wrapping and other settings right. */
2293 col = curwin->w_cursor.col;
2294 curwin->w_cursor.col = compl_col;
2295 validate_cursor_col();
2296 pum_display(compl_match_array, compl_match_arraysize, cur,
2297 curwin->w_cline_row + W_WINROW(curwin),
2298 curwin->w_cline_height,
Bram Moolenaar280f1262006-01-30 00:14:18 +00002299 curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002300 curwin->w_cursor.col = col;
2301 }
2302}
2303
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304#define DICT_FIRST (1) /* use just first element in "dict" */
2305#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307/*
2308 * Add any identifiers that match the given pattern to the list of
2309 * completions.
2310 */
2311 static void
2312ins_compl_dictionaries(dict, pat, dir, flags, thesaurus)
2313 char_u *dict;
2314 char_u *pat;
2315 int dir;
2316 int flags;
2317 int thesaurus;
2318{
2319 char_u *ptr;
2320 char_u *buf;
2321 FILE *fp;
2322 regmatch_T regmatch;
2323 int add_r;
2324 char_u **files;
2325 int count;
2326 int i;
2327 int save_p_scs;
2328
2329 buf = alloc(LSIZE);
2330 /* If 'infercase' is set, don't use 'smartcase' here */
2331 save_p_scs = p_scs;
2332 if (curbuf->b_p_inf)
2333 p_scs = FALSE;
2334 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2335 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2336 regmatch.rm_ic = ignorecase(pat);
2337 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002338 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
2340 /* copy one dictionary file name into buf */
2341 if (flags == DICT_EXACT)
2342 {
2343 count = 1;
2344 files = &dict;
2345 }
2346 else
2347 {
2348 /* Expand wildcards in the dictionary name, but do not allow
2349 * backticks (for security, the 'dict' option may have been set in
2350 * a modeline). */
2351 copy_option_part(&dict, buf, LSIZE, ",");
2352 if (vim_strchr(buf, '`') != NULL
2353 || expand_wildcards(1, &buf, &count, &files,
2354 EW_FILE|EW_SILENT) != OK)
2355 count = 0;
2356 }
2357
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002358 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
2360 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2361 if (flags != DICT_EXACT)
2362 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002363 vim_snprintf((char *)IObuff, IOSIZE,
2364 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2366 }
2367
2368 if (fp != NULL)
2369 {
2370 /*
2371 * Read dictionary file line by line.
2372 * Check each line for a match.
2373 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002374 while (!got_int && !compl_interrupted
2375 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 {
2377 ptr = buf;
2378 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2379 {
2380 ptr = regmatch.startp[0];
2381 ptr = find_word_end(ptr);
2382 add_r = ins_compl_add_infercase(regmatch.startp[0],
2383 (int)(ptr - regmatch.startp[0]),
2384 files[i], dir, 0);
2385 if (thesaurus)
2386 {
2387 char_u *wstart;
2388
2389 /*
2390 * Add the other matches on the line
2391 */
2392 while (!got_int)
2393 {
2394 /* Find start of the next word. Skip white
2395 * space and punctuation. */
2396 ptr = find_word_start(ptr);
2397 if (*ptr == NUL || *ptr == NL)
2398 break;
2399 wstart = ptr;
2400
2401 /* Find end of the word and add it. */
2402#ifdef FEAT_MBYTE
2403 if (has_mbyte)
2404 /* Japanese words may have characters in
2405 * different classes, only separate words
2406 * with single-byte non-word characters. */
2407 while (*ptr != NUL)
2408 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002409 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
2411 if (l < 2 && !vim_iswordc(*ptr))
2412 break;
2413 ptr += l;
2414 }
2415 else
2416#endif
2417 ptr = find_word_end(ptr);
2418 add_r = ins_compl_add_infercase(wstart,
2419 (int)(ptr - wstart), files[i], dir, 0);
2420 }
2421 }
2422 if (add_r == OK)
2423 /* if dir was BACKWARD then honor it just once */
2424 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002425 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 break;
2427 /* avoid expensive call to vim_regexec() when at end
2428 * of line */
2429 if (*ptr == '\n' || got_int)
2430 break;
2431 }
2432 line_breakcheck();
Bram Moolenaar572cb562005-08-05 21:35:02 +00002433 ins_compl_check_keys(50);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435 fclose(fp);
2436 }
2437 }
2438 if (flags != DICT_EXACT)
2439 FreeWild(count, files);
2440 if (flags)
2441 break;
2442 }
2443 p_scs = save_p_scs;
2444 vim_free(regmatch.regprog);
2445 vim_free(buf);
2446}
2447
2448/*
2449 * Find the start of the next word.
2450 * Returns a pointer to the first char of the word. Also stops at a NUL.
2451 */
2452 char_u *
2453find_word_start(ptr)
2454 char_u *ptr;
2455{
2456#ifdef FEAT_MBYTE
2457 if (has_mbyte)
2458 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002459 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 else
2461#endif
2462 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2463 ++ptr;
2464 return ptr;
2465}
2466
2467/*
2468 * Find the end of the word. Assumes it starts inside a word.
2469 * Returns a pointer to just after the word.
2470 */
2471 char_u *
2472find_word_end(ptr)
2473 char_u *ptr;
2474{
2475#ifdef FEAT_MBYTE
2476 int start_class;
2477
2478 if (has_mbyte)
2479 {
2480 start_class = mb_get_class(ptr);
2481 if (start_class > 1)
2482 while (*ptr != NUL)
2483 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002484 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 if (mb_get_class(ptr) != start_class)
2486 break;
2487 }
2488 }
2489 else
2490#endif
2491 while (vim_iswordc(*ptr))
2492 ++ptr;
2493 return ptr;
2494}
2495
2496/*
2497 * Free the list of completions
2498 */
2499 static void
2500ins_compl_free()
2501{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002502 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002504 vim_free(compl_pattern);
2505 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002506 vim_free(compl_leader);
2507 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002509 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002511
2512 ins_compl_del_pum();
2513 pum_clear();
2514
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002515 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 do
2517 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002518 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002519 compl_curr_match = compl_curr_match->cp_next;
2520 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002522 if (match->cp_flags & FREE_FNAME)
2523 vim_free(match->cp_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002525 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2526 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527}
2528
2529 static void
2530ins_compl_clear()
2531{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002532 compl_cont_status = 0;
2533 compl_started = FALSE;
2534 compl_matches = 0;
2535 vim_free(compl_pattern);
2536 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002537 vim_free(compl_leader);
2538 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 save_sm = -1;
2540 edit_submode_extra = NULL;
2541}
2542
2543/*
Bram Moolenaara6557602006-02-04 22:43:20 +00002544 * Delete one character before the cursor and make a subset of the matches
2545 * that match now.
2546 * Returns TRUE if the work is done and another char to be got from the user.
2547 */
2548 static int
2549ins_compl_bs()
2550{
2551 char_u *line;
2552 char_u *p;
2553
2554 if (curwin->w_cursor.col <= compl_col + compl_length)
2555 {
2556 /* Deleted more than what was used to find matches, need to look for
2557 * matches all over again. */
2558 ins_compl_free();
2559 compl_started = FALSE;
2560 compl_matches = 0;
2561 }
2562
2563 line = ml_get_curline();
2564 p = line + curwin->w_cursor.col;
2565 mb_ptr_back(line, p);
2566
2567 vim_free(compl_leader);
2568 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2569 if (compl_leader != NULL)
2570 {
2571 ins_compl_del_pum();
2572 ins_compl_delete();
2573 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2574
2575 if (!compl_started)
2576 {
2577 /* Matches were cleared, need to search for them now. */
2578 if (ins_complete(Ctrl_N) == FAIL)
2579 compl_cont_status = 0;
2580 else
2581 {
2582 /* Remove the completed word again. */
2583 ins_compl_delete();
2584 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2585 }
2586 }
2587
2588 /* Show the popup menu with a different set of matches. */
2589 ins_compl_show_pum();
2590 compl_used_match = FALSE;
2591
2592 return TRUE;
2593 }
2594 return FALSE;
2595}
2596
2597/*
2598 * Append one character to the match leader. May reduce the number of
2599 * matches.
2600 */
2601 static void
2602ins_compl_addleader(c)
2603 int c;
2604{
2605#ifdef FEAT_MBYTE
2606 int cc;
2607
2608 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2609 {
2610 char_u buf[MB_MAXBYTES + 1];
2611
2612 (*mb_char2bytes)(c, buf);
2613 buf[cc] = NUL;
2614 ins_char_bytes(buf, cc);
2615 }
2616 else
2617#endif
2618 ins_char(c);
2619
2620 vim_free(compl_leader);
2621 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
2622 curwin->w_cursor.col - compl_col);
2623 if (compl_leader != NULL)
2624 {
2625 /* Show the popup menu with a different set of matches. */
2626 ins_compl_del_pum();
2627 ins_compl_show_pum();
2628 compl_used_match = FALSE;
2629 }
2630}
2631
2632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002634 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002635 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002637 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638ins_compl_prep(c)
2639 int c;
2640{
2641 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 int temp;
2643 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002644 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
2646 /* Forget any previous 'special' messages if this is actually
2647 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2648 */
2649 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2650 edit_submode_extra = NULL;
2651
2652 /* Ignore end of Select mode mapping */
2653 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002654 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
2656 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2657 {
2658 /*
2659 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2660 * it will be yet. Now we decide.
2661 */
2662 switch (c)
2663 {
2664 case Ctrl_E:
2665 case Ctrl_Y:
2666 ctrl_x_mode = CTRL_X_SCROLL;
2667 if (!(State & REPLACE_FLAG))
2668 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2669 else
2670 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2671 edit_submode_pre = NULL;
2672 showmode();
2673 break;
2674 case Ctrl_L:
2675 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2676 break;
2677 case Ctrl_F:
2678 ctrl_x_mode = CTRL_X_FILES;
2679 break;
2680 case Ctrl_K:
2681 ctrl_x_mode = CTRL_X_DICTIONARY;
2682 break;
2683 case Ctrl_R:
2684 /* Simply allow ^R to happen without affecting ^X mode */
2685 break;
2686 case Ctrl_T:
2687 ctrl_x_mode = CTRL_X_THESAURUS;
2688 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002689#ifdef FEAT_COMPL_FUNC
2690 case Ctrl_U:
2691 ctrl_x_mode = CTRL_X_FUNCTION;
2692 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002693 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002694 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002695 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002696#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002697 case 's':
2698 case Ctrl_S:
2699 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002700#ifdef FEAT_SYN_HL
2701 spell_back_to_badword();
2702#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002703 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 case Ctrl_RSB:
2705 ctrl_x_mode = CTRL_X_TAGS;
2706 break;
2707#ifdef FEAT_FIND_ID
2708 case Ctrl_I:
2709 case K_S_TAB:
2710 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2711 break;
2712 case Ctrl_D:
2713 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2714 break;
2715#endif
2716 case Ctrl_V:
2717 case Ctrl_Q:
2718 ctrl_x_mode = CTRL_X_CMDLINE;
2719 break;
2720 case Ctrl_P:
2721 case Ctrl_N:
2722 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
2723 * just started ^X mode, or there were enough ^X's to cancel
2724 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2725 * do normal expansion when interrupting a different mode (say
2726 * ^X^F^X^P or ^P^X^X^P, see below)
2727 * nothing changes if interrupting mode 0, (eg, the flag
2728 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002729 if (!(compl_cont_status & CONT_INTRPT))
2730 compl_cont_status |= CONT_LOCAL;
2731 else if (compl_cont_mode != 0)
2732 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 /* FALLTHROUGH */
2734 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002735 /* If we have typed at least 2 ^X's... for modes != 0, we set
2736 * compl_cont_status = 0 (eg, as if we had just started ^X
2737 * mode).
2738 * For mode 0, we set "compl_cont_mode" to an impossible
2739 * value, in both cases ^X^X can be used to restart the same
2740 * mode (avoiding ADDING mode).
2741 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2742 * 'complete' and local ^P expansions respectively.
2743 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2744 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if (c == Ctrl_X)
2746 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002747 if (compl_cont_mode != 0)
2748 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002750 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 }
2752 ctrl_x_mode = 0;
2753 edit_submode = NULL;
2754 showmode();
2755 break;
2756 }
2757 }
2758 else if (ctrl_x_mode != 0)
2759 {
2760 /* We're already in CTRL-X mode, do we stay in it? */
2761 if (!vim_is_ctrl_x_key(c))
2762 {
2763 if (ctrl_x_mode == CTRL_X_SCROLL)
2764 ctrl_x_mode = 0;
2765 else
2766 ctrl_x_mode = CTRL_X_FINISHED;
2767 edit_submode = NULL;
2768 }
2769 showmode();
2770 }
2771
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002772 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 {
2774 /* Show error message from attempted keyword completion (probably
2775 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002776 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002778 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
2779 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 || ctrl_x_mode == CTRL_X_FINISHED)
2781 {
2782 /* Get here when we have finished typing a sequence of ^N and
2783 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002784 * memory that was used, and make sure we can redo the insert. */
2785 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002787 char_u *p;
2788
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 /*
2790 * If any of the original typed text has been changed,
2791 * eg when ignorecase is set, we must add back-spaces to
2792 * the redo buffer. We add as few as necessary to delete
2793 * just the part of the original text that has changed.
2794 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002795 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002796 p = compl_orig_text;
2797 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002799 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 ++ptr;
2801 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002802 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002804 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
2806
2807#ifdef FEAT_CINDENT
2808 want_cindent = (can_cindent && cindent_on());
2809#endif
2810 /*
2811 * When completing whole lines: fix indent for 'cindent'.
2812 * Otherwise, break line if it's too long.
2813 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002814 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 {
2816#ifdef FEAT_CINDENT
2817 /* re-indent the current line */
2818 if (want_cindent)
2819 {
2820 do_c_expr_indent();
2821 want_cindent = FALSE; /* don't do it again */
2822 }
2823#endif
2824 }
2825 else
2826 {
2827 /* put the cursor on the last char, for 'tw' formatting */
2828 curwin->w_cursor.col--;
2829 if (stop_arrow() == OK)
2830 insertchar(NUL, 0, -1);
2831 curwin->w_cursor.col++;
2832 }
2833
2834 auto_format(FALSE, TRUE);
2835
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002836 /* if the popup menu is displayed hitting Enter means accepting
2837 * the selection without inserting anything. */
2838 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
2839 retval = TRUE;
2840
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002842 compl_started = FALSE;
2843 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 msg_clr_cmdline(); /* necessary for "noshowmode" */
2845 ctrl_x_mode = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002846 if (save_sm >= 0)
2847 p_sm = save_sm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 if (edit_submode != NULL)
2849 {
2850 edit_submode = NULL;
2851 showmode();
2852 }
2853
2854#ifdef FEAT_CINDENT
2855 /*
2856 * Indent now if a key was typed that is in 'cinkeys'.
2857 */
2858 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2859 do_c_expr_indent();
2860#endif
2861 }
2862 }
2863
2864 /* reset continue_* if we left expansion-mode, if we stay they'll be
2865 * (re)set properly in ins_complete() */
2866 if (!vim_is_ctrl_x_key(c))
2867 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002868 compl_cont_status = 0;
2869 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002871
2872 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873}
2874
2875/*
2876 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2877 * (depending on flag) starting from buf and looking for a non-scanned
2878 * buffer (other than curbuf). curbuf is special, if it is called with
2879 * buf=curbuf then it has to be the first call for a given flag/expansion.
2880 *
2881 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2882 */
2883 static buf_T *
2884ins_compl_next_buf(buf, flag)
2885 buf_T *buf;
2886 int flag;
2887{
2888#ifdef FEAT_WINDOWS
2889 static win_T *wp;
2890#endif
2891
2892 if (flag == 'w') /* just windows */
2893 {
2894#ifdef FEAT_WINDOWS
2895 if (buf == curbuf) /* first call for this flag/expansion */
2896 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002897 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 && wp->w_buffer->b_scanned)
2899 ;
2900 buf = wp->w_buffer;
2901#else
2902 buf = curbuf;
2903#endif
2904 }
2905 else
2906 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2907 * (unlisted buffers)
2908 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002909 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 && ((flag == 'U'
2911 ? buf->b_p_bl
2912 : (!buf->b_p_bl
2913 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2914 || buf->b_scanned
2915 || (buf->b_ml.ml_mfp == NULL
2916 && ctrl_x_mode == CTRL_X_WHOLE_LINE)))
2917 ;
2918 return buf;
2919}
2920
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002921#ifdef FEAT_COMPL_FUNC
Bram Moolenaare344bea2005-09-01 20:46:49 +00002922static int expand_by_function __ARGS((int type, char_u *base, char_u ***matches));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002923
2924/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002925 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00002926 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002927 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002928 */
2929 static int
Bram Moolenaare344bea2005-09-01 20:46:49 +00002930expand_by_function(type, base, matches)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002931 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002932 char_u *base;
2933 char_u ***matches;
2934{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002935 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002936 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002937 listitem_T *li;
2938 garray_T ga;
2939 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002940 char_u *funcname;
2941 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002942
Bram Moolenaare344bea2005-09-01 20:46:49 +00002943 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
2944 if (*funcname == NUL)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002945 return 0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002946
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002947 /* Call 'completefunc' to obtain the list of matches. */
2948 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00002949 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002950
Bram Moolenaare344bea2005-09-01 20:46:49 +00002951 pos = curwin->w_cursor;
2952 matchlist = call_func_retlist(funcname, 2, args, FALSE);
2953 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002954 if (matchlist == NULL)
2955 return 0;
2956
2957 /* Go through the List with matches and put them in an array. */
2958 ga_init2(&ga, (int)sizeof(char_u *), 8);
2959 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002960 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002961 p = get_tv_string_chk(&li->li_tv);
2962 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002963 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002964 if (ga_grow(&ga, 1) == FAIL)
2965 break;
2966 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p);
2967 ++ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002968 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00002969 else if (did_emsg)
2970 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002971 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002972
2973 list_unref(matchlist);
2974 *matches = (char_u **)ga.ga_data;
2975 return ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002976}
2977#endif /* FEAT_COMPL_FUNC */
2978
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002979/*
2980 * Get the next expansion(s), using "compl_pattern".
2981 * The search starts at position "ini" in curbuf and in the direction dir.
2982 * When "compl_started" is FALSE start at that position, otherwise
2983 * continue where we stopped searching before.
2984 * This may return before finding all the matches.
2985 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 */
2987 static int
2988ins_compl_get_exp(ini, dir)
2989 pos_T *ini;
2990 int dir;
2991{
2992 static pos_T first_match_pos;
2993 static pos_T last_match_pos;
2994 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002995 static int found_all = FALSE; /* Found all matches of a
2996 certain type. */
2997 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998
Bram Moolenaar572cb562005-08-05 21:35:02 +00002999 pos_T *pos;
3000 char_u **matches;
3001 int save_p_scs;
3002 int save_p_ws;
3003 int save_p_ic;
3004 int i;
3005 int num_matches;
3006 int len;
3007 int found_new_match;
3008 int type = ctrl_x_mode;
3009 char_u *ptr;
3010 char_u *dict = NULL;
3011 int dict_f = 0;
3012 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003014 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 {
3016 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3017 ins_buf->b_scanned = 0;
3018 found_all = FALSE;
3019 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003020 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003021 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 last_match_pos = first_match_pos = *ini;
3023 }
3024
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003025 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 pos = (dir == FORWARD) ? &last_match_pos : &first_match_pos;
3027 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3028 for (;;)
3029 {
3030 found_new_match = FAIL;
3031
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003032 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 * or if found_all says this entry is done. For ^X^L only use the
3034 * entries from 'complete' that look in loaded buffers. */
3035 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003036 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 {
3038 found_all = FALSE;
3039 while (*e_cpt == ',' || *e_cpt == ' ')
3040 e_cpt++;
3041 if (*e_cpt == '.' && !curbuf->b_scanned)
3042 {
3043 ins_buf = curbuf;
3044 first_match_pos = *ini;
3045 /* So that ^N can match word immediately after cursor */
3046 if (ctrl_x_mode == 0)
3047 dec(&first_match_pos);
3048 last_match_pos = first_match_pos;
3049 type = 0;
3050 }
3051 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3052 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3053 {
3054 /* Scan a buffer, but not the current one. */
3055 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3056 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003057 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 first_match_pos.col = last_match_pos.col = 0;
3059 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3060 last_match_pos.lnum = 0;
3061 type = 0;
3062 }
3063 else /* unloaded buffer, scan like dictionary */
3064 {
3065 found_all = TRUE;
3066 if (ins_buf->b_fname == NULL)
3067 continue;
3068 type = CTRL_X_DICTIONARY;
3069 dict = ins_buf->b_fname;
3070 dict_f = DICT_EXACT;
3071 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003072 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 ins_buf->b_fname == NULL
3074 ? buf_spname(ins_buf)
3075 : ins_buf->b_sfname == NULL
3076 ? (char *)ins_buf->b_fname
3077 : (char *)ins_buf->b_sfname);
3078 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3079 }
3080 else if (*e_cpt == NUL)
3081 break;
3082 else
3083 {
3084 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3085 type = -1;
3086 else if (*e_cpt == 'k' || *e_cpt == 's')
3087 {
3088 if (*e_cpt == 'k')
3089 type = CTRL_X_DICTIONARY;
3090 else
3091 type = CTRL_X_THESAURUS;
3092 if (*++e_cpt != ',' && *e_cpt != NUL)
3093 {
3094 dict = e_cpt;
3095 dict_f = DICT_FIRST;
3096 }
3097 }
3098#ifdef FEAT_FIND_ID
3099 else if (*e_cpt == 'i')
3100 type = CTRL_X_PATH_PATTERNS;
3101 else if (*e_cpt == 'd')
3102 type = CTRL_X_PATH_DEFINES;
3103#endif
3104 else if (*e_cpt == ']' || *e_cpt == 't')
3105 {
3106 type = CTRL_X_TAGS;
3107 sprintf((char*)IObuff, _("Scanning tags."));
3108 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3109 }
3110 else
3111 type = -1;
3112
3113 /* in any case e_cpt is advanced to the next entry */
3114 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3115
3116 found_all = TRUE;
3117 if (type == -1)
3118 continue;
3119 }
3120 }
3121
3122 switch (type)
3123 {
3124 case -1:
3125 break;
3126#ifdef FEAT_FIND_ID
3127 case CTRL_X_PATH_PATTERNS:
3128 case CTRL_X_PATH_DEFINES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003129 find_pattern_in_path(compl_pattern, dir,
3130 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003132 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3134 (linenr_T)1, (linenr_T)MAXLNUM);
3135 break;
3136#endif
3137
3138 case CTRL_X_DICTIONARY:
3139 case CTRL_X_THESAURUS:
3140 ins_compl_dictionaries(
3141 dict ? dict
3142 : (type == CTRL_X_THESAURUS
3143 ? (*curbuf->b_p_tsr == NUL
3144 ? p_tsr
3145 : curbuf->b_p_tsr)
3146 : (*curbuf->b_p_dict == NUL
3147 ? p_dict
3148 : curbuf->b_p_dict)),
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003149 compl_pattern, dir,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
3151 dict = NULL;
3152 break;
3153
3154 case CTRL_X_TAGS:
3155 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3156 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003157 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
3159 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003160 * of matches is found when compl_pattern is empty */
3161 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3163 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3164 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3165 {
3166 ins_compl_add_matches(num_matches, matches, dir);
3167 }
3168 p_ic = save_p_ic;
3169 break;
3170
3171 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003172 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3174 {
3175
3176 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003177 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 ins_compl_add_matches(num_matches, matches, dir);
3179 }
3180 break;
3181
3182 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003183 if (expand_cmdline(&compl_xp, compl_pattern,
3184 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 &num_matches, &matches) == EXPAND_OK)
3186 ins_compl_add_matches(num_matches, matches, dir);
3187 break;
3188
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003189#ifdef FEAT_COMPL_FUNC
3190 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003191 case CTRL_X_OMNI:
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003192 num_matches = expand_by_function(type, compl_pattern, &matches);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003193 if (num_matches > 0)
3194 ins_compl_add_matches(num_matches, matches, dir);
3195 break;
3196#endif
3197
Bram Moolenaar488c6512005-08-11 20:09:58 +00003198 case CTRL_X_SPELL:
3199#ifdef FEAT_SYN_HL
3200 num_matches = expand_spelling(first_match_pos.lnum,
3201 first_match_pos.col, compl_pattern, &matches);
3202 if (num_matches > 0)
3203 ins_compl_add_matches(num_matches, matches, dir);
3204#endif
3205 break;
3206
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 default: /* normal ^P/^N and ^X^L */
3208 /*
3209 * If 'infercase' is set, don't use 'smartcase' here
3210 */
3211 save_p_scs = p_scs;
3212 if (ins_buf->b_p_inf)
3213 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003214
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 /* buffers other than curbuf are scanned from the beginning or the
3216 * end but never from the middle, thus setting nowrapscan in this
3217 * buffers is a good idea, on the other hand, we always set
3218 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3219 save_p_ws = p_ws;
3220 if (ins_buf != curbuf)
3221 p_ws = FALSE;
3222 else if (*e_cpt == '.')
3223 p_ws = TRUE;
3224 for (;;)
3225 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003226 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003228 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3229 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003231 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003233 dir, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 else
3235 found_new_match = searchit(NULL, ins_buf, pos, dir,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003236 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 RE_LAST);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003238 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003240 /* set compl_started even on fail */
3241 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 first_match_pos = *pos;
3243 last_match_pos = *pos;
3244 }
3245 else if (first_match_pos.lnum == last_match_pos.lnum
3246 && first_match_pos.col == last_match_pos.col)
3247 found_new_match = FAIL;
3248 if (found_new_match == FAIL)
3249 {
3250 if (ins_buf == curbuf)
3251 found_all = TRUE;
3252 break;
3253 }
3254
3255 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003256 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 && ini->lnum == pos->lnum
3258 && ini->col == pos->col)
3259 continue;
3260 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3261 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3262 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003263 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
3265 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3266 continue;
3267 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3268 if (!p_paste)
3269 ptr = skipwhite(ptr);
3270 }
3271 len = (int)STRLEN(ptr);
3272 }
3273 else
3274 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003275 char_u *tmp_ptr = ptr;
3276
3277 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003279 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 /* Skip if already inside a word. */
3281 if (vim_iswordp(tmp_ptr))
3282 continue;
3283 /* Find start of next word. */
3284 tmp_ptr = find_word_start(tmp_ptr);
3285 }
3286 /* Find end of this word. */
3287 tmp_ptr = find_word_end(tmp_ptr);
3288 len = (int)(tmp_ptr - ptr);
3289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003290 if ((compl_cont_status & CONT_ADDING)
3291 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
3293 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3294 {
3295 /* Try next line, if any. the new word will be
3296 * "join" as if the normal command "J" was used.
3297 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003298 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 * works -- Acevedo */
3300 STRNCPY(IObuff, ptr, len);
3301 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3302 tmp_ptr = ptr = skipwhite(ptr);
3303 /* Find start of next word. */
3304 tmp_ptr = find_word_start(tmp_ptr);
3305 /* Find end of next word. */
3306 tmp_ptr = find_word_end(tmp_ptr);
3307 if (tmp_ptr > ptr)
3308 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003309 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003311 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 IObuff[len++] = ' ';
3313 /* IObuf =~ "\k.* ", thus len >= 2 */
3314 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003315 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 || (vim_strchr(p_cpo, CPO_JOINSP)
3317 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003318 && (IObuff[len - 2] == '?'
3319 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 IObuff[len++] = ' ';
3321 }
3322 /* copy as much as posible of the new word */
3323 if (tmp_ptr - ptr >= IOSIZE - len)
3324 tmp_ptr = ptr + IOSIZE - len - 1;
3325 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3326 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003327 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 }
3329 IObuff[len] = NUL;
3330 ptr = IObuff;
3331 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003332 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 continue;
3334 }
3335 }
3336 if (ins_compl_add_infercase(ptr, len,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003337 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar572cb562005-08-05 21:35:02 +00003338 dir, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 {
3340 found_new_match = OK;
3341 break;
3342 }
3343 }
3344 p_scs = save_p_scs;
3345 p_ws = save_p_ws;
3346 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003347
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003348 /* check if compl_curr_match has changed, (e.g. other type of
3349 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003350 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 found_new_match = OK;
3352
3353 /* break the loop for specialized modes (use 'complete' just for the
3354 * generic ctrl_x_mode == 0) or when we've found a new match */
3355 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003356 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003357 {
3358 if (got_int)
3359 break;
3360 if (pum_wanted() && type != -1)
3361 /* Fill the popup menu as soon as possible. */
3362 ins_compl_check_keys(0);
3363 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3364 || compl_interrupted)
3365 break;
3366 compl_started = TRUE;
3367 }
3368 else
3369 {
3370 /* Mark a buffer scanned when it has been scanned completely */
3371 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3372 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003374 compl_started = FALSE;
3375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003377 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378
3379 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3380 && *e_cpt == NUL) /* Got to end of 'complete' */
3381 found_new_match = FAIL;
3382
3383 i = -1; /* total of matches, unknown */
3384 if (found_new_match == FAIL
3385 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3386 i = ins_compl_make_cyclic();
3387
3388 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003389 * just been made cyclic then we have to move compl_curr_match to the next
3390 * or previous entry (if any) -- Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003391 compl_curr_match = dir == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003392 if (compl_curr_match == NULL)
3393 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 return i;
3395}
3396
3397/* Delete the old text being completed. */
3398 static void
3399ins_compl_delete()
3400{
3401 int i;
3402
3403 /*
3404 * In insert mode: Delete the typed part.
3405 * In replace mode: Put the old characters back, if any.
3406 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003407 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 backspace_until_column(i);
3409 changed_cline_bef_curs();
3410}
3411
3412/* Insert the new text being completed. */
3413 static void
3414ins_compl_insert()
3415{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003416 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003417 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418}
3419
3420/*
3421 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003422 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3423 * get more completions. If it is FALSE, then we just do nothing when there
3424 * are no more completions in a given direction. The latter case is used when
3425 * we are still in the middle of finding completions, to allow browsing
3426 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 * Return the total number of matches, or -1 if still unknown -- webb.
3428 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003429 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3430 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 *
3432 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003433 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3434 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 */
3436 static int
Bram Moolenaare3226be2005-12-18 22:10:00 +00003437ins_compl_next(allow_get_expansion, count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003439 int count; /* repeat completion this many times; should
3440 be at least 1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
3442 int num_matches = -1;
3443 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003444 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003445 compl_T *found_compl = NULL;
3446 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
3448 if (allow_get_expansion)
3449 {
3450 /* Delete old text to be replaced */
3451 ins_compl_delete();
3452 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003453 compl_pending = FALSE;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003454
3455 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3456 * around. */
3457 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003459 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003461 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003462 found_end = (compl_first_match != NULL
3463 && (compl_shown_match->cp_next == compl_first_match
3464 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003465 }
3466 else if (compl_shows_dir == BACKWARD
3467 && compl_shown_match->cp_prev != NULL)
3468 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003469 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003470 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003471 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003474 {
3475 compl_pending = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003476 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003477 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003478
3479 num_matches = ins_compl_get_exp(&compl_startpos, compl_direction);
3480 if (compl_pending && compl_direction == compl_shows_dir)
3481 compl_shown_match = compl_curr_match;
3482 found_end = FALSE;
3483 }
3484 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
3485 && compl_leader != NULL
3486 && STRNCMP(compl_shown_match->cp_str,
3487 compl_leader, STRLEN(compl_leader)) != 0)
3488 ++todo;
3489 else
3490 /* Remember a matching item. */
3491 found_compl = compl_shown_match;
3492
3493 /* Stop at the end of the list when we found a usable match. */
3494 if (found_end)
3495 {
3496 if (found_compl != NULL)
3497 {
3498 compl_shown_match = found_compl;
3499 break;
3500 }
3501 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
3504
3505 /* Insert the text of the new completion */
3506 ins_compl_insert();
3507
3508 if (!allow_get_expansion)
3509 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003510 /* may undisplay the popup menu first */
3511 ins_compl_upd_pum();
3512
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003513 /* display the updated popup menu */
3514 ins_compl_show_pum();
3515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 /* Delete old text to be replaced, since we're still searching and
3517 * don't want to match ourselves! */
3518 ins_compl_delete();
3519 }
3520
3521 /*
3522 * Show the file name for the match (if any)
3523 * Truncate the file name to avoid a wait for return.
3524 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003525 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
3527 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003528 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 if (i <= 0)
3530 i = 0;
3531 else
3532 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003533 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 msg(IObuff);
3535 redraw_cmdline = FALSE; /* don't overwrite! */
3536 }
3537
3538 return num_matches;
3539}
3540
3541/*
3542 * Call this while finding completions, to check whether the user has hit a key
3543 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003544 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003546 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 */
3548 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003549ins_compl_check_keys(frequency)
3550 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
3552 static int count = 0;
3553
3554 int c;
3555
3556 /* Don't check when reading keys from a script. That would break the test
3557 * scripts */
3558 if (using_script())
3559 return;
3560
3561 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003562 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 return;
3564 count = 0;
3565
3566 ++no_mapping;
3567 c = vpeekc_any();
3568 --no_mapping;
3569 if (c != NUL)
3570 {
3571 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3572 {
3573 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003574 compl_shows_dir = ins_compl_key2dir(c);
3575 (void)ins_compl_next(FALSE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
3577 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003578 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003580 if (compl_pending && !got_int)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003581 (void)ins_compl_next(FALSE, 1);
3582}
3583
3584/*
3585 * Decide the direction of Insert mode complete from the key typed.
3586 * Returns BACKWARD or FORWARD.
3587 */
3588 static int
3589ins_compl_key2dir(c)
3590 int c;
3591{
3592 if (c == Ctrl_P || c == Ctrl_L || (pum_visible()
3593 && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP)))
3594 return BACKWARD;
3595 return FORWARD;
3596}
3597
3598/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003599 * Return TRUE for keys that are used for completion only when the popup menu
3600 * is visible.
3601 */
3602 static int
3603ins_compl_pum_key(c)
3604 int c;
3605{
3606 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
3607 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN);
3608}
3609
3610/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00003611 * Decide the number of completions to move forward.
3612 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3613 */
3614 static int
3615ins_compl_key2count(c)
3616 int c;
3617{
3618 int h;
3619
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003620 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00003621 {
3622 h = pum_get_height();
3623 if (h > 3)
3624 h -= 2; /* keep some context */
3625 return h;
3626 }
3627 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628}
3629
3630/*
3631 * Do Insert mode completion.
3632 * Called when character "c" was typed, which has a meaning for completion.
3633 * Returns OK if completion was done, FAIL if something failed (out of mem).
3634 */
3635 static int
3636ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003637 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003639 char_u *line;
3640 int startcol = 0; /* column where searched text starts */
3641 colnr_T curs_col; /* cursor column */
3642 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaare3226be2005-12-18 22:10:00 +00003644 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003645 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
3647 /* First time we hit ^N or ^P (in a row, I mean) */
3648
3649 /* Turn off 'sm' so we don't show matches with ^X^L */
3650 save_sm = p_sm;
3651 p_sm = FALSE;
3652
3653 did_ai = FALSE;
3654#ifdef FEAT_SMARTINDENT
3655 did_si = FALSE;
3656 can_si = FALSE;
3657 can_si_back = FALSE;
3658#endif
3659 if (stop_arrow() == FAIL)
3660 return FAIL;
3661
3662 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003663 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
3665 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003666 * "compl_startpos" to the cursor as a pattern to add a new word
3667 * instead of expand the one before the cursor, in word-wise if
3668 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 * is not in the same line as the cursor then fix it (the line has
3670 * been split because it was longer than 'tw'). if SOL is set then
3671 * skip the previous pattern, a word at the beginning of the line has
3672 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003673 if ((compl_cont_status & CONT_INTRPT) && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
3675 /*
3676 * it is a continued search
3677 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003678 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3680 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3681 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003682 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003684 /* line (probably) wrapped, set compl_startpos to the
3685 * first non_blank in the line, if it is not a wordchar
3686 * include it to get a better pattern, but then we don't
3687 * want the "\\<" prefix, check it bellow */
3688 compl_col = (colnr_T)(skipwhite(line) - line);
3689 compl_startpos.col = compl_col;
3690 compl_startpos.lnum = curwin->w_cursor.lnum;
3691 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
3693 else
3694 {
3695 /* S_IPOS was set when we inserted a word that was at the
3696 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003697 * mode but first we need to redefine compl_startpos */
3698 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003700 compl_cont_status |= CONT_SOL;
3701 compl_startpos.col = (colnr_T)(skipwhite(
3702 line + compl_length
3703 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003705 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003707 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003708 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 * have enough space? just being paranoic */
3710#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003711 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003713 compl_cont_status &= ~CONT_SOL;
3714 compl_length = (IOSIZE - MIN_SPACE);
3715 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003717 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3718 if (compl_length < 1)
3719 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
3721 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003722 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003724 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
3726 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003727 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003729 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003731 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003733 compl_cont_status = 0;
3734 compl_cont_status |= CONT_N_ADDS;
3735 compl_startpos = curwin->w_cursor;
3736 startcol = (int)curs_col;
3737 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 }
3739
3740 /* Work out completion pattern and original text -- webb */
3741 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3742 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003743 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3745 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003746 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003748 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003750 compl_col += ++startcol;
3751 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
3753 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003754 compl_pattern = str_foldcase(line + compl_col,
3755 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003757 compl_pattern = vim_strnsave(line + compl_col,
3758 compl_length);
3759 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 return FAIL;
3761 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003762 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 {
3764 char_u *prefix = (char_u *)"\\<";
3765
3766 /* we need 3 extra chars, 1 for the NUL and
3767 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003768 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3769 compl_length) + 3);
3770 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003772 if (!vim_iswordp(line + compl_col)
3773 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 && (
3775#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003776 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003778 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779#endif
3780 )))
3781 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003782 STRCPY((char *)compl_pattern, prefix);
3783 (void)quote_meta(compl_pattern + STRLEN(prefix),
3784 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003786 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003788 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003790 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791#endif
3792 )
3793 {
3794 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003795 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3796 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003798 compl_col += curs_col;
3799 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 }
3801 else
3802 {
3803#ifdef FEAT_MBYTE
3804 /* Search the point of change class of multibyte character
3805 * or not a word single byte character backward. */
3806 if (has_mbyte)
3807 {
3808 int base_class;
3809 int head_off;
3810
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003811 startcol -= (*mb_head_off)(line, line + startcol);
3812 base_class = mb_get_class(line + startcol);
3813 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003815 head_off = (*mb_head_off)(line, line + startcol);
3816 if (base_class != mb_get_class(line + startcol
3817 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003819 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 }
3821 }
3822 else
3823#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003824 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003826 compl_col += ++startcol;
3827 compl_length = (int)curs_col - startcol;
3828 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 {
3830 /* Only match word with at least two chars -- webb
3831 * there's no need to call quote_meta,
3832 * alloc(7) is enough -- Acevedo
3833 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003834 compl_pattern = alloc(7);
3835 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003837 STRCPY((char *)compl_pattern, "\\<");
3838 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3839 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841 else
3842 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003843 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3844 compl_length) + 3);
3845 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003847 STRCPY((char *)compl_pattern, "\\<");
3848 (void)quote_meta(compl_pattern + 2, line + compl_col,
3849 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 }
3851 }
3852 }
3853 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3854 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003855 compl_col = skipwhite(line) - line;
3856 compl_length = (int)curs_col - (int)compl_col;
3857 if (compl_length < 0) /* cursor in indent: empty pattern */
3858 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003860 compl_pattern = str_foldcase(line + compl_col, compl_length,
3861 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003863 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3864 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 return FAIL;
3866 }
3867 else if (ctrl_x_mode == CTRL_X_FILES)
3868 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003869 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003871 compl_col += ++startcol;
3872 compl_length = (int)curs_col - startcol;
3873 compl_pattern = addstar(line + compl_col, compl_length,
3874 EXPAND_FILES);
3875 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 return FAIL;
3877 }
3878 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3879 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003880 compl_pattern = vim_strnsave(line, curs_col);
3881 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003883 set_cmd_context(&compl_xp, compl_pattern,
3884 (int)STRLEN(compl_pattern), curs_col);
3885 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3886 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003888 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
3889 compl_col = startcol;
3890 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003892 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003893 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00003894#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003895 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00003896 * Call user defined function 'completefunc' with "a:findstart"
3897 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003898 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00003899 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003900 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003901 char_u *funcname;
3902 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003903
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003904 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00003905 * string */
3906 funcname = ctrl_x_mode == CTRL_X_FUNCTION
3907 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3908 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003909 {
3910 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3911 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003912 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003913 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003914
3915 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003916 args[1] = NULL;
3917 pos = curwin->w_cursor;
3918 col = call_func_retnr(funcname, 2, args, FALSE);
3919 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003920
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003921 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003922 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003923 compl_col = col;
3924 if ((colnr_T)compl_col > curs_col)
3925 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003926
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003927 /* Setup variables for completion. Need to obtain "line" again,
3928 * it may have become invalid. */
3929 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003930 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003931 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3932 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003933#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003934 return FAIL;
3935 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00003936 else if (ctrl_x_mode == CTRL_X_SPELL)
3937 {
3938#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00003939 if (spell_bad_len > 0)
3940 compl_col = curs_col - spell_bad_len;
3941 else
3942 compl_col = spell_word_start(startcol);
3943 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00003944 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003945 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003946 compl_length = (int)curs_col - compl_col;
3947 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3948 if (compl_pattern == NULL)
3949#endif
3950 return FAIL;
3951 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003952 else
3953 {
3954 EMSG2(_(e_intern2), "ins_complete()");
3955 return FAIL;
3956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003958 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 {
3960 edit_submode_pre = (char_u *)_(" Adding");
3961 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3962 {
3963 /* Insert a new line, keep indentation but ignore 'comments' */
3964#ifdef FEAT_COMMENTS
3965 char_u *old = curbuf->b_p_com;
3966
3967 curbuf->b_p_com = (char_u *)"";
3968#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003969 compl_startpos.lnum = curwin->w_cursor.lnum;
3970 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 ins_eol('\r');
3972#ifdef FEAT_COMMENTS
3973 curbuf->b_p_com = old;
3974#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003975 compl_length = 0;
3976 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978 }
3979 else
3980 {
3981 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003982 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
3984
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003985 if (compl_cont_status & CONT_LOCAL)
3986 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 else
3988 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
3989
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003991 * "compl_orig_text" itself (not a copy) is added, it will be freed
3992 * when the list of matches is freed. */
3993 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
3994 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
3995 -1, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003997 vim_free(compl_pattern);
3998 compl_pattern = NULL;
3999 vim_free(compl_orig_text);
4000 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 return FAIL;
4002 }
4003
4004 /* showmode might reset the internal line pointers, so it must
4005 * be called before line = ml_get(), or when this address is no
4006 * longer needed. -- Acevedo.
4007 */
4008 edit_submode_extra = (char_u *)_("-- Searching...");
4009 edit_submode_highl = HLF_COUNT;
4010 showmode();
4011 edit_submode_extra = NULL;
4012 out_flush();
4013 }
4014
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004015 compl_shown_match = compl_curr_match;
4016 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
4018 /*
4019 * Find next match.
4020 */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004021 n = ins_compl_next(TRUE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004023 /* may undisplay the popup menu */
4024 ins_compl_upd_pum();
4025
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004026 if (n > 1) /* all matches have been found */
4027 compl_matches = n;
4028 compl_curr_match = compl_shown_match;
4029 compl_direction = compl_shows_dir;
4030 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031
4032 /* eat the ESC to avoid leaving insert mode */
4033 if (got_int && !global_busy)
4034 {
4035 (void)vgetc();
4036 got_int = FALSE;
4037 }
4038
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004039 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004040 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004042 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4043 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4045 edit_submode_highl = HLF_E;
4046 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4047 * because we couldn't expand anything at first place, but if we used
4048 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4049 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004050 if ( compl_length > 1
4051 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 || (ctrl_x_mode != 0
4053 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4054 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004055 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 }
4057
Bram Moolenaar572cb562005-08-05 21:35:02 +00004058 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004059 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004061 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063 if (edit_submode_extra == NULL)
4064 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004065 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 {
4067 edit_submode_extra = (char_u *)_("Back at original");
4068 edit_submode_highl = HLF_W;
4069 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004070 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
4072 edit_submode_extra = (char_u *)_("Word from other line");
4073 edit_submode_highl = HLF_COUNT;
4074 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004075 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
4077 edit_submode_extra = (char_u *)_("The only match");
4078 edit_submode_highl = HLF_COUNT;
4079 }
4080 else
4081 {
4082 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004083 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004085 int number = 0;
4086 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004088 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 {
4090 /* search backwards for the first valid (!= -1) number.
4091 * This should normally succeed already at the first loop
4092 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004093 for (match = compl_curr_match->cp_prev; match != NULL
4094 && match != compl_first_match;
4095 match = match->cp_prev)
4096 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004098 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 break;
4100 }
4101 if (match != NULL)
4102 /* go up and assign all numbers which are not assigned
4103 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004104 for (match = match->cp_next;
4105 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004106 match = match->cp_next)
4107 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109 else /* BACKWARD */
4110 {
4111 /* search forwards (upwards) for the first valid (!= -1)
4112 * number. This should normally succeed already at the
4113 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004114 for (match = compl_curr_match->cp_next; match != NULL
4115 && match != compl_first_match;
4116 match = match->cp_next)
4117 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004119 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 break;
4121 }
4122 if (match != NULL)
4123 /* go down and assign all numbers which are not
4124 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004125 for (match = match->cp_prev; match
4126 && match->cp_number == -1;
4127 match = match->cp_prev)
4128 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
4130 }
4131
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004132 /* The match should always have a sequence number now, this is
4133 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004134 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 {
4136 /* Space for 10 text chars. + 2x10-digit no.s */
4137 static char_u match_ref[31];
4138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004139 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004141 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004143 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004144 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004145 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 edit_submode_extra = match_ref;
4147 edit_submode_highl = HLF_R;
4148 if (dollar_vcol)
4149 curs_columns(FALSE);
4150 }
4151 }
4152 }
4153
4154 /* Show a message about what (completion) mode we're in. */
4155 showmode();
4156 if (edit_submode_extra != NULL)
4157 {
4158 if (!p_smd)
4159 msg_attr(edit_submode_extra,
4160 edit_submode_highl < HLF_COUNT
4161 ? hl_attr(edit_submode_highl) : 0);
4162 }
4163 else
4164 msg_clr_cmdline(); /* necessary for "noshowmode" */
4165
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004166 ins_compl_show_pum();
4167
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return OK;
4169}
4170
4171/*
4172 * Looks in the first "len" chars. of "src" for search-metachars.
4173 * If dest is not NULL the chars. are copied there quoting (with
4174 * a backslash) the metachars, and dest would be NUL terminated.
4175 * Returns the length (needed) of dest
4176 */
4177 static int
4178quote_meta(dest, src, len)
4179 char_u *dest;
4180 char_u *src;
4181 int len;
4182{
4183 int m;
4184
4185 for (m = len; --len >= 0; src++)
4186 {
4187 switch (*src)
4188 {
4189 case '.':
4190 case '*':
4191 case '[':
4192 if (ctrl_x_mode == CTRL_X_DICTIONARY
4193 || ctrl_x_mode == CTRL_X_THESAURUS)
4194 break;
4195 case '~':
4196 if (!p_magic) /* quote these only if magic is set */
4197 break;
4198 case '\\':
4199 if (ctrl_x_mode == CTRL_X_DICTIONARY
4200 || ctrl_x_mode == CTRL_X_THESAURUS)
4201 break;
4202 case '^': /* currently it's not needed. */
4203 case '$':
4204 m++;
4205 if (dest != NULL)
4206 *dest++ = '\\';
4207 break;
4208 }
4209 if (dest != NULL)
4210 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004211# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 /* Copy remaining bytes of a multibyte character. */
4213 if (has_mbyte)
4214 {
4215 int i, mb_len;
4216
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004217 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 if (mb_len > 0 && len >= mb_len)
4219 for (i = 0; i < mb_len; ++i)
4220 {
4221 --len;
4222 ++src;
4223 if (dest != NULL)
4224 *dest++ = *src;
4225 }
4226 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004227# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
4229 if (dest != NULL)
4230 *dest = NUL;
4231
4232 return m;
4233}
4234#endif /* FEAT_INS_EXPAND */
4235
4236/*
4237 * Next character is interpreted literally.
4238 * A one, two or three digit decimal number is interpreted as its byte value.
4239 * If one or two digits are entered, the next character is given to vungetc().
4240 * For Unicode a character > 255 may be returned.
4241 */
4242 int
4243get_literal()
4244{
4245 int cc;
4246 int nc;
4247 int i;
4248 int hex = FALSE;
4249 int octal = FALSE;
4250#ifdef FEAT_MBYTE
4251 int unicode = 0;
4252#endif
4253
4254 if (got_int)
4255 return Ctrl_C;
4256
4257#ifdef FEAT_GUI
4258 /*
4259 * In GUI there is no point inserting the internal code for a special key.
4260 * It is more useful to insert the string "<KEY>" instead. This would
4261 * probably be useful in a text window too, but it would not be
4262 * vi-compatible (maybe there should be an option for it?) -- webb
4263 */
4264 if (gui.in_use)
4265 ++allow_keys;
4266#endif
4267#ifdef USE_ON_FLY_SCROLL
4268 dont_scroll = TRUE; /* disallow scrolling here */
4269#endif
4270 ++no_mapping; /* don't map the next key hits */
4271 cc = 0;
4272 i = 0;
4273 for (;;)
4274 {
4275 do
4276 nc = safe_vgetc();
4277 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4278 || nc == K_HOR_SCROLLBAR);
4279#ifdef FEAT_CMDL_INFO
4280 if (!(State & CMDLINE)
4281# ifdef FEAT_MBYTE
4282 && MB_BYTE2LEN_CHECK(nc) == 1
4283# endif
4284 )
4285 add_to_showcmd(nc);
4286#endif
4287 if (nc == 'x' || nc == 'X')
4288 hex = TRUE;
4289 else if (nc == 'o' || nc == 'O')
4290 octal = TRUE;
4291#ifdef FEAT_MBYTE
4292 else if (nc == 'u' || nc == 'U')
4293 unicode = nc;
4294#endif
4295 else
4296 {
4297 if (hex
4298#ifdef FEAT_MBYTE
4299 || unicode != 0
4300#endif
4301 )
4302 {
4303 if (!vim_isxdigit(nc))
4304 break;
4305 cc = cc * 16 + hex2nr(nc);
4306 }
4307 else if (octal)
4308 {
4309 if (nc < '0' || nc > '7')
4310 break;
4311 cc = cc * 8 + nc - '0';
4312 }
4313 else
4314 {
4315 if (!VIM_ISDIGIT(nc))
4316 break;
4317 cc = cc * 10 + nc - '0';
4318 }
4319
4320 ++i;
4321 }
4322
4323 if (cc > 255
4324#ifdef FEAT_MBYTE
4325 && unicode == 0
4326#endif
4327 )
4328 cc = 255; /* limit range to 0-255 */
4329 nc = 0;
4330
4331 if (hex) /* hex: up to two chars */
4332 {
4333 if (i >= 2)
4334 break;
4335 }
4336#ifdef FEAT_MBYTE
4337 else if (unicode) /* Unicode: up to four or eight chars */
4338 {
4339 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4340 break;
4341 }
4342#endif
4343 else if (i >= 3) /* decimal or octal: up to three chars */
4344 break;
4345 }
4346 if (i == 0) /* no number entered */
4347 {
4348 if (nc == K_ZERO) /* NUL is stored as NL */
4349 {
4350 cc = '\n';
4351 nc = 0;
4352 }
4353 else
4354 {
4355 cc = nc;
4356 nc = 0;
4357 }
4358 }
4359
4360 if (cc == 0) /* NUL is stored as NL */
4361 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004362#ifdef FEAT_MBYTE
4363 if (enc_dbcs && (cc & 0xff) == 0)
4364 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4365 second byte will cause trouble! */
4366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
4368 --no_mapping;
4369#ifdef FEAT_GUI
4370 if (gui.in_use)
4371 --allow_keys;
4372#endif
4373 if (nc)
4374 vungetc(nc);
4375 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4376 return cc;
4377}
4378
4379/*
4380 * Insert character, taking care of special keys and mod_mask
4381 */
4382 static void
4383insert_special(c, allow_modmask, ctrlv)
4384 int c;
4385 int allow_modmask;
4386 int ctrlv; /* c was typed after CTRL-V */
4387{
4388 char_u *p;
4389 int len;
4390
4391 /*
4392 * Special function key, translate into "<Key>". Up to the last '>' is
4393 * inserted with ins_str(), so as not to replace characters in replace
4394 * mode.
4395 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4396 * unless 'allow_modmask' is TRUE.
4397 */
4398#ifdef MACOS
4399 /* Command-key never produces a normal key */
4400 if (mod_mask & MOD_MASK_CMD)
4401 allow_modmask = TRUE;
4402#endif
4403 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4404 {
4405 p = get_special_key_name(c, mod_mask);
4406 len = (int)STRLEN(p);
4407 c = p[len - 1];
4408 if (len > 2)
4409 {
4410 if (stop_arrow() == FAIL)
4411 return;
4412 p[len - 1] = NUL;
4413 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004414 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 ctrlv = FALSE;
4416 }
4417 }
4418 if (stop_arrow() == OK)
4419 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4420}
4421
4422/*
4423 * Special characters in this context are those that need processing other
4424 * than the simple insertion that can be performed here. This includes ESC
4425 * which terminates the insert, and CR/NL which need special processing to
4426 * open up a new line. This routine tries to optimize insertions performed by
4427 * the "redo", "undo" or "put" commands, so it needs to know when it should
4428 * stop and defer processing to the "normal" mechanism.
4429 * '0' and '^' are special, because they can be followed by CTRL-D.
4430 */
4431#ifdef EBCDIC
4432# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4433#else
4434# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4435#endif
4436
4437#ifdef FEAT_MBYTE
4438# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4439#else
4440# define WHITECHAR(cc) vim_iswhite(cc)
4441#endif
4442
4443 void
4444insertchar(c, flags, second_indent)
4445 int c; /* character to insert or NUL */
4446 int flags; /* INSCHAR_FORMAT, etc. */
4447 int second_indent; /* indent for second line if >= 0 */
4448{
4449 int haveto_redraw = FALSE;
4450 int textwidth;
4451#ifdef FEAT_COMMENTS
4452 colnr_T leader_len;
4453 char_u *p;
4454 int no_leader = FALSE;
4455 int do_comments = (flags & INSCHAR_DO_COM);
4456#endif
4457 int fo_white_par;
4458 int first_line = TRUE;
4459 int fo_ins_blank;
4460#ifdef FEAT_MBYTE
4461 int fo_multibyte;
4462#endif
4463 int save_char = NUL;
4464 int cc;
4465
4466 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4467 fo_ins_blank = has_format_option(FO_INS_BLANK);
4468#ifdef FEAT_MBYTE
4469 fo_multibyte = has_format_option(FO_MBYTE_BREAK);
4470#endif
4471 fo_white_par = has_format_option(FO_WHITE_PAR);
4472
4473 /*
4474 * Try to break the line in two or more pieces when:
4475 * - Always do this if we have been called to do formatting only.
4476 * - Always do this when 'formatoptions' has the 'a' flag and the line
4477 * ends in white space.
4478 * - Otherwise:
4479 * - Don't do this if inserting a blank
4480 * - Don't do this if an existing character is being replaced, unless
4481 * we're in VREPLACE mode.
4482 * - Do this if the cursor is not on the line where insert started
4483 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4484 * before the insert.
4485 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4486 * before 'textwidth'
4487 */
4488 if (textwidth
4489 && ((flags & INSCHAR_FORMAT)
4490 || (!vim_iswhite(c)
4491 && !((State & REPLACE_FLAG)
4492#ifdef FEAT_VREPLACE
4493 && !(State & VREPLACE_FLAG)
4494#endif
4495 && *ml_get_cursor() != NUL)
4496 && (curwin->w_cursor.lnum != Insstart.lnum
4497 || ((!has_format_option(FO_INS_LONG)
4498 || Insstart_textlen <= (colnr_T)textwidth)
4499 && (!fo_ins_blank
4500 || Insstart_blank_vcol <= (colnr_T)textwidth
4501 ))))))
4502 {
4503 /*
4504 * When 'ai' is off we don't want a space under the cursor to be
4505 * deleted. Replace it with an 'x' temporarily.
4506 */
4507 if (!curbuf->b_p_ai)
4508 {
4509 cc = gchar_cursor();
4510 if (vim_iswhite(cc))
4511 {
4512 save_char = cc;
4513 pchar_cursor('x');
4514 }
4515 }
4516
4517 /*
4518 * Repeat breaking lines, until the current line is not too long.
4519 */
4520 while (!got_int)
4521 {
4522 int startcol; /* Cursor column at entry */
4523 int wantcol; /* column at textwidth border */
4524 int foundcol; /* column for start of spaces */
4525 int end_foundcol = 0; /* column for start of word */
4526 colnr_T len;
4527 colnr_T virtcol;
4528#ifdef FEAT_VREPLACE
4529 int orig_col = 0;
4530 char_u *saved_text = NULL;
4531#endif
4532 colnr_T col;
4533
4534 virtcol = get_nolist_virtcol();
4535 if (virtcol < (colnr_T)textwidth)
4536 break;
4537
4538#ifdef FEAT_COMMENTS
4539 if (no_leader)
4540 do_comments = FALSE;
4541 else if (!(flags & INSCHAR_FORMAT)
4542 && has_format_option(FO_WRAP_COMS))
4543 do_comments = TRUE;
4544
4545 /* Don't break until after the comment leader */
4546 if (do_comments)
4547 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
4548 else
4549 leader_len = 0;
4550
4551 /* If the line doesn't start with a comment leader, then don't
4552 * start one in a following broken line. Avoids that a %word
4553 * moved to the start of the next line causes all following lines
4554 * to start with %. */
4555 if (leader_len == 0)
4556 no_leader = TRUE;
4557#endif
4558 if (!(flags & INSCHAR_FORMAT)
4559#ifdef FEAT_COMMENTS
4560 && leader_len == 0
4561#endif
4562 && !has_format_option(FO_WRAP))
4563
4564 {
4565 textwidth = 0;
4566 break;
4567 }
4568 if ((startcol = curwin->w_cursor.col) == 0)
4569 break;
4570
4571 /* find column of textwidth border */
4572 coladvance((colnr_T)textwidth);
4573 wantcol = curwin->w_cursor.col;
4574
4575 curwin->w_cursor.col = startcol - 1;
4576#ifdef FEAT_MBYTE
4577 /* Correct cursor for multi-byte character. */
4578 if (has_mbyte)
4579 mb_adjust_cursor();
4580#endif
4581 foundcol = 0;
4582
4583 /*
4584 * Find position to break at.
4585 * Stop at first entered white when 'formatoptions' has 'v'
4586 */
4587 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
4588 || curwin->w_cursor.lnum != Insstart.lnum
4589 || curwin->w_cursor.col >= Insstart.col)
4590 {
4591 cc = gchar_cursor();
4592 if (WHITECHAR(cc))
4593 {
4594 /* remember position of blank just before text */
4595 end_foundcol = curwin->w_cursor.col;
4596
4597 /* find start of sequence of blanks */
4598 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
4599 {
4600 dec_cursor();
4601 cc = gchar_cursor();
4602 }
4603 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
4604 break; /* only spaces in front of text */
4605#ifdef FEAT_COMMENTS
4606 /* Don't break until after the comment leader */
4607 if (curwin->w_cursor.col < leader_len)
4608 break;
4609#endif
4610 if (has_format_option(FO_ONE_LETTER))
4611 {
4612 /* do not break after one-letter words */
4613 if (curwin->w_cursor.col == 0)
4614 break; /* one-letter word at begin */
4615
4616 col = curwin->w_cursor.col;
4617 dec_cursor();
4618 cc = gchar_cursor();
4619
4620 if (WHITECHAR(cc))
4621 continue; /* one-letter, continue */
4622 curwin->w_cursor.col = col;
4623 }
4624#ifdef FEAT_MBYTE
4625 if (has_mbyte)
4626 foundcol = curwin->w_cursor.col
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004627 + (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 else
4629#endif
4630 foundcol = curwin->w_cursor.col + 1;
4631 if (curwin->w_cursor.col < (colnr_T)wantcol)
4632 break;
4633 }
4634#ifdef FEAT_MBYTE
4635 else if (cc >= 0x100 && fo_multibyte
4636 && curwin->w_cursor.col <= (colnr_T)wantcol)
4637 {
4638 /* Break after or before a multi-byte character. */
4639 foundcol = curwin->w_cursor.col;
4640 if (curwin->w_cursor.col < (colnr_T)wantcol)
4641 foundcol += (*mb_char2len)(cc);
4642 end_foundcol = foundcol;
4643 break;
4644 }
4645#endif
4646 if (curwin->w_cursor.col == 0)
4647 break;
4648 dec_cursor();
4649 }
4650
4651 if (foundcol == 0) /* no spaces, cannot break line */
4652 {
4653 curwin->w_cursor.col = startcol;
4654 break;
4655 }
4656
4657 /* Going to break the line, remove any "$" now. */
4658 undisplay_dollar();
4659
4660 /*
4661 * Offset between cursor position and line break is used by replace
4662 * stack functions. VREPLACE does not use this, and backspaces
4663 * over the text instead.
4664 */
4665#ifdef FEAT_VREPLACE
4666 if (State & VREPLACE_FLAG)
4667 orig_col = startcol; /* Will start backspacing from here */
4668 else
4669#endif
4670 replace_offset = startcol - end_foundcol - 1;
4671
4672 /*
4673 * adjust startcol for spaces that will be deleted and
4674 * characters that will remain on top line
4675 */
4676 curwin->w_cursor.col = foundcol;
4677 while (cc = gchar_cursor(), WHITECHAR(cc))
4678 inc_cursor();
4679 startcol -= curwin->w_cursor.col;
4680 if (startcol < 0)
4681 startcol = 0;
4682
4683#ifdef FEAT_VREPLACE
4684 if (State & VREPLACE_FLAG)
4685 {
4686 /*
4687 * In VREPLACE mode, we will backspace over the text to be
4688 * wrapped, so save a copy now to put on the next line.
4689 */
4690 saved_text = vim_strsave(ml_get_cursor());
4691 curwin->w_cursor.col = orig_col;
4692 if (saved_text == NULL)
4693 break; /* Can't do it, out of memory */
4694 saved_text[startcol] = NUL;
4695
4696 /* Backspace over characters that will move to the next line */
4697 if (!fo_white_par)
4698 backspace_until_column(foundcol);
4699 }
4700 else
4701#endif
4702 {
4703 /* put cursor after pos. to break line */
4704 if (!fo_white_par)
4705 curwin->w_cursor.col = foundcol;
4706 }
4707
4708 /*
4709 * Split the line just before the margin.
4710 * Only insert/delete lines, but don't really redraw the window.
4711 */
4712 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
4713 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
4714#ifdef FEAT_COMMENTS
4715 + (do_comments ? OPENLINE_DO_COM : 0)
4716#endif
4717 , old_indent);
4718 old_indent = 0;
4719
4720 replace_offset = 0;
4721 if (first_line)
4722 {
4723 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
4724 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
4725 if (second_indent >= 0)
4726 {
4727#ifdef FEAT_VREPLACE
4728 if (State & VREPLACE_FLAG)
4729 change_indent(INDENT_SET, second_indent, FALSE, NUL);
4730 else
4731#endif
4732 (void)set_indent(second_indent, SIN_CHANGED);
4733 }
4734 first_line = FALSE;
4735 }
4736
4737#ifdef FEAT_VREPLACE
4738 if (State & VREPLACE_FLAG)
4739 {
4740 /*
4741 * In VREPLACE mode we have backspaced over the text to be
4742 * moved, now we re-insert it into the new line.
4743 */
4744 ins_bytes(saved_text);
4745 vim_free(saved_text);
4746 }
4747 else
4748#endif
4749 {
4750 /*
4751 * Check if cursor is not past the NUL off the line, cindent
4752 * may have added or removed indent.
4753 */
4754 curwin->w_cursor.col += startcol;
4755 len = (colnr_T)STRLEN(ml_get_curline());
4756 if (curwin->w_cursor.col > len)
4757 curwin->w_cursor.col = len;
4758 }
4759
4760 haveto_redraw = TRUE;
4761#ifdef FEAT_CINDENT
4762 can_cindent = TRUE;
4763#endif
4764 /* moved the cursor, don't autoindent or cindent now */
4765 did_ai = FALSE;
4766#ifdef FEAT_SMARTINDENT
4767 did_si = FALSE;
4768 can_si = FALSE;
4769 can_si_back = FALSE;
4770#endif
4771 line_breakcheck();
4772 }
4773
4774 if (save_char) /* put back space after cursor */
4775 pchar_cursor(save_char);
4776
4777 if (c == NUL) /* formatting only */
4778 return;
4779 if (haveto_redraw)
4780 {
4781 update_topline();
4782 redraw_curbuf_later(VALID);
4783 }
4784 }
4785 if (c == NUL) /* only formatting was wanted */
4786 return;
4787
4788#ifdef FEAT_COMMENTS
4789 /* Check whether this character should end a comment. */
4790 if (did_ai && (int)c == end_comment_pending)
4791 {
4792 char_u *line;
4793 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4794 int middle_len, end_len;
4795 int i;
4796
4797 /*
4798 * Need to remove existing (middle) comment leader and insert end
4799 * comment leader. First, check what comment leader we can find.
4800 */
4801 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4802 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4803 {
4804 /* Skip middle-comment string */
4805 while (*p && p[-1] != ':') /* find end of middle flags */
4806 ++p;
4807 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4808 /* Don't count trailing white space for middle_len */
4809 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4810 --middle_len;
4811
4812 /* Find the end-comment string */
4813 while (*p && p[-1] != ':') /* find end of end flags */
4814 ++p;
4815 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4816
4817 /* Skip white space before the cursor */
4818 i = curwin->w_cursor.col;
4819 while (--i >= 0 && vim_iswhite(line[i]))
4820 ;
4821 i++;
4822
4823 /* Skip to before the middle leader */
4824 i -= middle_len;
4825
4826 /* Check some expected things before we go on */
4827 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4828 {
4829 /* Backspace over all the stuff we want to replace */
4830 backspace_until_column(i);
4831
4832 /*
4833 * Insert the end-comment string, except for the last
4834 * character, which will get inserted as normal later.
4835 */
4836 ins_bytes_len(lead_end, end_len - 1);
4837 }
4838 }
4839 }
4840 end_comment_pending = NUL;
4841#endif
4842
4843 did_ai = FALSE;
4844#ifdef FEAT_SMARTINDENT
4845 did_si = FALSE;
4846 can_si = FALSE;
4847 can_si_back = FALSE;
4848#endif
4849
4850 /*
4851 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4852 * This speeds up normal text input considerably.
4853 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4854 * need to re-indent at a ':', or any other character (but not what
4855 * 'paste' is set)..
4856 */
4857#ifdef USE_ON_FLY_SCROLL
4858 dont_scroll = FALSE; /* allow scrolling here */
4859#endif
4860
4861 if ( !ISSPECIAL(c)
4862#ifdef FEAT_MBYTE
4863 && (!has_mbyte || (*mb_char2len)(c) == 1)
4864#endif
4865 && vpeekc() != NUL
4866 && !(State & REPLACE_FLAG)
4867#ifdef FEAT_CINDENT
4868 && !cindent_on()
4869#endif
4870#ifdef FEAT_RIGHTLEFT
4871 && !p_ri
4872#endif
4873 )
4874 {
4875#define INPUT_BUFLEN 100
4876 char_u buf[INPUT_BUFLEN + 1];
4877 int i;
4878 colnr_T virtcol = 0;
4879
4880 buf[0] = c;
4881 i = 1;
4882 if (textwidth)
4883 virtcol = get_nolist_virtcol();
4884 /*
4885 * Stop the string when:
4886 * - no more chars available
4887 * - finding a special character (command key)
4888 * - buffer is full
4889 * - running into the 'textwidth' boundary
4890 * - need to check for abbreviation: A non-word char after a word-char
4891 */
4892 while ( (c = vpeekc()) != NUL
4893 && !ISSPECIAL(c)
4894#ifdef FEAT_MBYTE
4895 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4896#endif
4897 && i < INPUT_BUFLEN
4898 && (textwidth == 0
4899 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4900 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4901 {
4902#ifdef FEAT_RIGHTLEFT
4903 c = vgetc();
4904 if (p_hkmap && KeyTyped)
4905 c = hkmap(c); /* Hebrew mode mapping */
4906# ifdef FEAT_FKMAP
4907 if (p_fkmap && KeyTyped)
4908 c = fkmap(c); /* Farsi mode mapping */
4909# endif
4910 buf[i++] = c;
4911#else
4912 buf[i++] = vgetc();
4913#endif
4914 }
4915
4916#ifdef FEAT_DIGRAPHS
4917 do_digraph(-1); /* clear digraphs */
4918 do_digraph(buf[i-1]); /* may be the start of a digraph */
4919#endif
4920 buf[i] = NUL;
4921 ins_str(buf);
4922 if (flags & INSCHAR_CTRLV)
4923 {
4924 redo_literal(*buf);
4925 i = 1;
4926 }
4927 else
4928 i = 0;
4929 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004930 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932 else
4933 {
4934#ifdef FEAT_MBYTE
4935 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
4936 {
4937 char_u buf[MB_MAXBYTES + 1];
4938
4939 (*mb_char2bytes)(c, buf);
4940 buf[cc] = NUL;
4941 ins_char_bytes(buf, cc);
4942 AppendCharToRedobuff(c);
4943 }
4944 else
4945#endif
4946 {
4947 ins_char(c);
4948 if (flags & INSCHAR_CTRLV)
4949 redo_literal(c);
4950 else
4951 AppendCharToRedobuff(c);
4952 }
4953 }
4954}
4955
4956/*
4957 * Called after inserting or deleting text: When 'formatoptions' includes the
4958 * 'a' flag format from the current line until the end of the paragraph.
4959 * Keep the cursor at the same position relative to the text.
4960 * The caller must have saved the cursor line for undo, following ones will be
4961 * saved here.
4962 */
4963 void
4964auto_format(trailblank, prev_line)
4965 int trailblank; /* when TRUE also format with trailing blank */
4966 int prev_line; /* may start in previous line */
4967{
4968 pos_T pos;
4969 colnr_T len;
4970 char_u *old;
4971 char_u *new, *pnew;
4972 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004973 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974
4975 if (!has_format_option(FO_AUTO))
4976 return;
4977
4978 pos = curwin->w_cursor;
4979 old = ml_get_curline();
4980
4981 /* may remove added space */
4982 check_auto_format(FALSE);
4983
4984 /* Don't format in Insert mode when the cursor is on a trailing blank, the
4985 * user might insert normal text next. Also skip formatting when "1" is
4986 * in 'formatoptions' and there is a single character before the cursor.
4987 * Otherwise the line would be broken and when typing another non-white
4988 * next they are not joined back together. */
4989 wasatend = (pos.col == STRLEN(old));
4990 if (*old != NUL && !trailblank && wasatend)
4991 {
4992 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004993 cc = gchar_cursor();
4994 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
4995 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004997 cc = gchar_cursor();
4998 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
5000 curwin->w_cursor = pos;
5001 return;
5002 }
5003 curwin->w_cursor = pos;
5004 }
5005
5006#ifdef FEAT_COMMENTS
5007 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5008 * comments. */
5009 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5010 && get_leader_len(old, NULL, FALSE) == 0)
5011 return;
5012#endif
5013
5014 /*
5015 * May start formatting in a previous line, so that after "x" a word is
5016 * moved to the previous line if it fits there now. Only when this is not
5017 * the start of a paragraph.
5018 */
5019 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5020 {
5021 --curwin->w_cursor.lnum;
5022 if (u_save_cursor() == FAIL)
5023 return;
5024 }
5025
5026 /*
5027 * Do the formatting and restore the cursor position. "saved_cursor" will
5028 * be adjusted for the text formatting.
5029 */
5030 saved_cursor = pos;
5031 format_lines((linenr_T)-1);
5032 curwin->w_cursor = saved_cursor;
5033 saved_cursor.lnum = 0;
5034
5035 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5036 {
5037 /* "cannot happen" */
5038 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5039 coladvance((colnr_T)MAXCOL);
5040 }
5041 else
5042 check_cursor_col();
5043
5044 /* Insert mode: If the cursor is now after the end of the line while it
5045 * previously wasn't, the line was broken. Because of the rule above we
5046 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5047 * formatted. */
5048 if (!wasatend && has_format_option(FO_WHITE_PAR))
5049 {
5050 new = ml_get_curline();
5051 len = STRLEN(new);
5052 if (curwin->w_cursor.col == len)
5053 {
5054 pnew = vim_strnsave(new, len + 2);
5055 pnew[len] = ' ';
5056 pnew[len + 1] = NUL;
5057 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5058 /* remove the space later */
5059 did_add_space = TRUE;
5060 }
5061 else
5062 /* may remove added space */
5063 check_auto_format(FALSE);
5064 }
5065
5066 check_cursor();
5067}
5068
5069/*
5070 * When an extra space was added to continue a paragraph for auto-formatting,
5071 * delete it now. The space must be under the cursor, just after the insert
5072 * position.
5073 */
5074 static void
5075check_auto_format(end_insert)
5076 int end_insert; /* TRUE when ending Insert mode */
5077{
5078 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005079 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080
5081 if (did_add_space)
5082 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005083 cc = gchar_cursor();
5084 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 /* Somehow the space was removed already. */
5086 did_add_space = FALSE;
5087 else
5088 {
5089 if (!end_insert)
5090 {
5091 inc_cursor();
5092 c = gchar_cursor();
5093 dec_cursor();
5094 }
5095 if (c != NUL)
5096 {
5097 /* The space is no longer at the end of the line, delete it. */
5098 del_char(FALSE);
5099 did_add_space = FALSE;
5100 }
5101 }
5102 }
5103}
5104
5105/*
5106 * Find out textwidth to be used for formatting:
5107 * if 'textwidth' option is set, use it
5108 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5109 * if invalid value, use 0.
5110 * Set default to window width (maximum 79) for "gq" operator.
5111 */
5112 int
5113comp_textwidth(ff)
5114 int ff; /* force formatting (for "Q" command) */
5115{
5116 int textwidth;
5117
5118 textwidth = curbuf->b_p_tw;
5119 if (textwidth == 0 && curbuf->b_p_wm)
5120 {
5121 /* The width is the window width minus 'wrapmargin' minus all the
5122 * things that add to the margin. */
5123 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5124#ifdef FEAT_CMDWIN
5125 if (cmdwin_type != 0)
5126 textwidth -= 1;
5127#endif
5128#ifdef FEAT_FOLDING
5129 textwidth -= curwin->w_p_fdc;
5130#endif
5131#ifdef FEAT_SIGNS
5132 if (curwin->w_buffer->b_signlist != NULL
5133# ifdef FEAT_NETBEANS_INTG
5134 || usingNetbeans
5135# endif
5136 )
5137 textwidth -= 1;
5138#endif
5139 if (curwin->w_p_nu)
5140 textwidth -= 8;
5141 }
5142 if (textwidth < 0)
5143 textwidth = 0;
5144 if (ff && textwidth == 0)
5145 {
5146 textwidth = W_WIDTH(curwin) - 1;
5147 if (textwidth > 79)
5148 textwidth = 79;
5149 }
5150 return textwidth;
5151}
5152
5153/*
5154 * Put a character in the redo buffer, for when just after a CTRL-V.
5155 */
5156 static void
5157redo_literal(c)
5158 int c;
5159{
5160 char_u buf[10];
5161
5162 /* Only digits need special treatment. Translate them into a string of
5163 * three digits. */
5164 if (VIM_ISDIGIT(c))
5165 {
5166 sprintf((char *)buf, "%03d", c);
5167 AppendToRedobuff(buf);
5168 }
5169 else
5170 AppendCharToRedobuff(c);
5171}
5172
5173/*
5174 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005175 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 */
5177 static void
5178start_arrow(end_insert_pos)
5179 pos_T *end_insert_pos;
5180{
5181 if (!arrow_used) /* something has been inserted */
5182 {
5183 AppendToRedobuff(ESC_STR);
5184 stop_insert(end_insert_pos, FALSE);
5185 arrow_used = TRUE; /* this means we stopped the current insert */
5186 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005187#ifdef FEAT_SYN_HL
5188 check_spell_redraw();
5189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190}
5191
Bram Moolenaar217ad922005-03-20 22:37:15 +00005192#ifdef FEAT_SYN_HL
5193/*
5194 * If we skipped highlighting word at cursor, do it now.
5195 * It may be skipped again, thus reset spell_redraw_lnum first.
5196 */
5197 static void
5198check_spell_redraw()
5199{
5200 if (spell_redraw_lnum != 0)
5201 {
5202 linenr_T lnum = spell_redraw_lnum;
5203
5204 spell_redraw_lnum = 0;
5205 redrawWinline(lnum, FALSE);
5206 }
5207}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005208
5209/*
5210 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5211 * spelled word, if there is one.
5212 */
5213 static void
5214spell_back_to_badword()
5215{
5216 pos_T tpos = curwin->w_cursor;
5217
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005218 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005219 if (curwin->w_cursor.col != tpos.col)
5220 start_arrow(&tpos);
5221}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005222#endif
5223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224/*
5225 * stop_arrow() is called before a change is made in insert mode.
5226 * If an arrow key has been used, start a new insertion.
5227 * Returns FAIL if undo is impossible, shouldn't insert then.
5228 */
5229 int
5230stop_arrow()
5231{
5232 if (arrow_used)
5233 {
5234 if (u_save_cursor() == OK)
5235 {
5236 arrow_used = FALSE;
5237 ins_need_undo = FALSE;
5238 }
5239 Insstart = curwin->w_cursor; /* new insertion starts here */
5240 Insstart_textlen = linetabsize(ml_get_curline());
5241 ai_col = 0;
5242#ifdef FEAT_VREPLACE
5243 if (State & VREPLACE_FLAG)
5244 {
5245 orig_line_count = curbuf->b_ml.ml_line_count;
5246 vr_lines_changed = 1;
5247 }
5248#endif
5249 ResetRedobuff();
5250 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005251 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 }
5253 else if (ins_need_undo)
5254 {
5255 if (u_save_cursor() == OK)
5256 ins_need_undo = FALSE;
5257 }
5258
5259#ifdef FEAT_FOLDING
5260 /* Always open fold at the cursor line when inserting something. */
5261 foldOpenCursor();
5262#endif
5263
5264 return (arrow_used || ins_need_undo ? FAIL : OK);
5265}
5266
5267/*
5268 * do a few things to stop inserting
5269 */
5270 static void
5271stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005272 pos_T *end_insert_pos; /* where insert ended */
5273 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005275 int cc;
5276 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278 stop_redo_ins();
5279 replace_flush(); /* abandon replace stack */
5280
5281 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005282 * Save the inserted text for later redo with ^@ and CTRL-A.
5283 * Don't do it when "restart_edit" was set and nothing was inserted,
5284 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005286 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005287 if (did_restart_edit == 0 || (ptr != NULL
5288 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005289 {
5290 vim_free(last_insert);
5291 last_insert = ptr;
5292 last_insert_skip = new_insert_skip;
5293 }
5294 else
5295 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
5297 if (!arrow_used)
5298 {
5299 /* Auto-format now. It may seem strange to do this when stopping an
5300 * insertion (or moving the cursor), but it's required when appending
5301 * a line and having it end in a space. But only do it when something
5302 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005303 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005305 pos_T tpos = curwin->w_cursor;
5306
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 /* When the cursor is at the end of the line after a space the
5308 * formatting will move it to the following word. Avoid that by
5309 * moving the cursor onto the space. */
5310 cc = 'x';
5311 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5312 {
5313 dec_cursor();
5314 cc = gchar_cursor();
5315 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005316 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 }
5318
5319 auto_format(TRUE, FALSE);
5320
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005321 if (vim_iswhite(cc))
5322 {
5323 if (gchar_cursor() != NUL)
5324 inc_cursor();
5325#ifdef FEAT_VIRTUALEDIT
5326 /* If the cursor is still at the same character, also keep
5327 * the "coladd". */
5328 if (gchar_cursor() == NUL
5329 && curwin->w_cursor.lnum == tpos.lnum
5330 && curwin->w_cursor.col == tpos.col)
5331 curwin->w_cursor.coladd = tpos.coladd;
5332#endif
5333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335
5336 /* If a space was inserted for auto-formatting, remove it now. */
5337 check_auto_format(TRUE);
5338
5339 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005340 * of the line, and put the cursor back.
5341 * Do this when ESC was used or moving the cursor up/down. */
5342 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5343 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005345 pos_T tpos = curwin->w_cursor;
5346
5347 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5349 --curwin->w_cursor.col;
5350 while (cc = gchar_cursor(), vim_iswhite(cc))
5351 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005352 if (curwin->w_cursor.lnum != tpos.lnum)
5353 curwin->w_cursor = tpos;
5354 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5356
5357#ifdef FEAT_VISUAL
5358 /* <C-S-Right> may have started Visual mode, adjust the position for
5359 * deleted characters. */
5360 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5361 {
5362 cc = STRLEN(ml_get_curline());
5363 if (VIsual.col > (colnr_T)cc)
5364 {
5365 VIsual.col = cc;
5366# ifdef FEAT_VIRTUALEDIT
5367 VIsual.coladd = 0;
5368# endif
5369 }
5370 }
5371#endif
5372 }
5373 }
5374 did_ai = FALSE;
5375#ifdef FEAT_SMARTINDENT
5376 did_si = FALSE;
5377 can_si = FALSE;
5378 can_si_back = FALSE;
5379#endif
5380
5381 /* set '[ and '] to the inserted text */
5382 curbuf->b_op_start = Insstart;
5383 curbuf->b_op_end = *end_insert_pos;
5384}
5385
5386/*
5387 * Set the last inserted text to a single character.
5388 * Used for the replace command.
5389 */
5390 void
5391set_last_insert(c)
5392 int c;
5393{
5394 char_u *s;
5395
5396 vim_free(last_insert);
5397#ifdef FEAT_MBYTE
5398 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5399#else
5400 last_insert = alloc(6);
5401#endif
5402 if (last_insert != NULL)
5403 {
5404 s = last_insert;
5405 /* Use the CTRL-V only when entering a special char */
5406 if (c < ' ' || c == DEL)
5407 *s++ = Ctrl_V;
5408 s = add_char2buf(c, s);
5409 *s++ = ESC;
5410 *s++ = NUL;
5411 last_insert_skip = 0;
5412 }
5413}
5414
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005415#if defined(EXITFREE) || defined(PROTO)
5416 void
5417free_last_insert()
5418{
5419 vim_free(last_insert);
5420 last_insert = NULL;
5421}
5422#endif
5423
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424/*
5425 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5426 * and CSI. Handle multi-byte characters.
5427 * Returns a pointer to after the added bytes.
5428 */
5429 char_u *
5430add_char2buf(c, s)
5431 int c;
5432 char_u *s;
5433{
5434#ifdef FEAT_MBYTE
5435 char_u temp[MB_MAXBYTES];
5436 int i;
5437 int len;
5438
5439 len = (*mb_char2bytes)(c, temp);
5440 for (i = 0; i < len; ++i)
5441 {
5442 c = temp[i];
5443#endif
5444 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5445 if (c == K_SPECIAL)
5446 {
5447 *s++ = K_SPECIAL;
5448 *s++ = KS_SPECIAL;
5449 *s++ = KE_FILLER;
5450 }
5451#ifdef FEAT_GUI
5452 else if (c == CSI)
5453 {
5454 *s++ = CSI;
5455 *s++ = KS_EXTRA;
5456 *s++ = (int)KE_CSI;
5457 }
5458#endif
5459 else
5460 *s++ = c;
5461#ifdef FEAT_MBYTE
5462 }
5463#endif
5464 return s;
5465}
5466
5467/*
5468 * move cursor to start of line
5469 * if flags & BL_WHITE move to first non-white
5470 * if flags & BL_SOL move to first non-white if startofline is set,
5471 * otherwise keep "curswant" column
5472 * if flags & BL_FIX don't leave the cursor on a NUL.
5473 */
5474 void
5475beginline(flags)
5476 int flags;
5477{
5478 if ((flags & BL_SOL) && !p_sol)
5479 coladvance(curwin->w_curswant);
5480 else
5481 {
5482 curwin->w_cursor.col = 0;
5483#ifdef FEAT_VIRTUALEDIT
5484 curwin->w_cursor.coladd = 0;
5485#endif
5486
5487 if (flags & (BL_WHITE | BL_SOL))
5488 {
5489 char_u *ptr;
5490
5491 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5492 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5493 ++curwin->w_cursor.col;
5494 }
5495 curwin->w_set_curswant = TRUE;
5496 }
5497}
5498
5499/*
5500 * oneright oneleft cursor_down cursor_up
5501 *
5502 * Move one char {right,left,down,up}.
5503 * Doesn't move onto the NUL past the end of the line.
5504 * Return OK when successful, FAIL when we hit a line of file boundary.
5505 */
5506
5507 int
5508oneright()
5509{
5510 char_u *ptr;
5511#ifdef FEAT_MBYTE
5512 int l;
5513#endif
5514
5515#ifdef FEAT_VIRTUALEDIT
5516 if (virtual_active())
5517 {
5518 pos_T prevpos = curwin->w_cursor;
5519
5520 /* Adjust for multi-wide char (excluding TAB) */
5521 ptr = ml_get_cursor();
5522 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5523#ifdef FEAT_MBYTE
5524 (*mb_ptr2char)(ptr)
5525#else
5526 *ptr
5527#endif
5528 ))
5529 ? ptr2cells(ptr) : 1));
5530 curwin->w_set_curswant = TRUE;
5531 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5532 return (prevpos.col != curwin->w_cursor.col
5533 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5534 }
5535#endif
5536
5537 ptr = ml_get_cursor();
5538#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005539 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 {
5541 /* The character under the cursor is a multi-byte character, move
5542 * several bytes right, but don't end up on the NUL. */
5543 if (ptr[l] == NUL)
5544 return FAIL;
5545 curwin->w_cursor.col += l;
5546 }
5547 else
5548#endif
5549 {
5550 if (*ptr++ == NUL || *ptr == NUL)
5551 return FAIL;
5552 ++curwin->w_cursor.col;
5553 }
5554
5555 curwin->w_set_curswant = TRUE;
5556 return OK;
5557}
5558
5559 int
5560oneleft()
5561{
5562#ifdef FEAT_VIRTUALEDIT
5563 if (virtual_active())
5564 {
5565 int width;
5566 int v = getviscol();
5567
5568 if (v == 0)
5569 return FAIL;
5570
5571# ifdef FEAT_LINEBREAK
5572 /* We might get stuck on 'showbreak', skip over it. */
5573 width = 1;
5574 for (;;)
5575 {
5576 coladvance(v - width);
5577 /* getviscol() is slow, skip it when 'showbreak' is empty and
5578 * there are no multi-byte characters */
5579 if ((*p_sbr == NUL
5580# ifdef FEAT_MBYTE
5581 && !has_mbyte
5582# endif
5583 ) || getviscol() < v)
5584 break;
5585 ++width;
5586 }
5587# else
5588 coladvance(v - 1);
5589# endif
5590
5591 if (curwin->w_cursor.coladd == 1)
5592 {
5593 char_u *ptr;
5594
5595 /* Adjust for multi-wide char (not a TAB) */
5596 ptr = ml_get_cursor();
5597 if (*ptr != TAB && vim_isprintc(
5598# ifdef FEAT_MBYTE
5599 (*mb_ptr2char)(ptr)
5600# else
5601 *ptr
5602# endif
5603 ) && ptr2cells(ptr) > 1)
5604 curwin->w_cursor.coladd = 0;
5605 }
5606
5607 curwin->w_set_curswant = TRUE;
5608 return OK;
5609 }
5610#endif
5611
5612 if (curwin->w_cursor.col == 0)
5613 return FAIL;
5614
5615 curwin->w_set_curswant = TRUE;
5616 --curwin->w_cursor.col;
5617
5618#ifdef FEAT_MBYTE
5619 /* if the character on the left of the current cursor is a multi-byte
5620 * character, move to its first byte */
5621 if (has_mbyte)
5622 mb_adjust_cursor();
5623#endif
5624 return OK;
5625}
5626
5627 int
5628cursor_up(n, upd_topline)
5629 long n;
5630 int upd_topline; /* When TRUE: update topline */
5631{
5632 linenr_T lnum;
5633
5634 if (n > 0)
5635 {
5636 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005637 /* This fails if the cursor is already in the first line or the count
5638 * is larger than the line number and '-' is in 'cpoptions' */
5639 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 return FAIL;
5641 if (n >= lnum)
5642 lnum = 1;
5643 else
5644#ifdef FEAT_FOLDING
5645 if (hasAnyFolding(curwin))
5646 {
5647 /*
5648 * Count each sequence of folded lines as one logical line.
5649 */
5650 /* go to the the start of the current fold */
5651 (void)hasFolding(lnum, &lnum, NULL);
5652
5653 while (n--)
5654 {
5655 /* move up one line */
5656 --lnum;
5657 if (lnum <= 1)
5658 break;
5659 /* If we entered a fold, move to the beginning, unless in
5660 * Insert mode or when 'foldopen' contains "all": it will open
5661 * in a moment. */
5662 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
5663 (void)hasFolding(lnum, &lnum, NULL);
5664 }
5665 if (lnum < 1)
5666 lnum = 1;
5667 }
5668 else
5669#endif
5670 lnum -= n;
5671 curwin->w_cursor.lnum = lnum;
5672 }
5673
5674 /* try to advance to the column we want to be at */
5675 coladvance(curwin->w_curswant);
5676
5677 if (upd_topline)
5678 update_topline(); /* make sure curwin->w_topline is valid */
5679
5680 return OK;
5681}
5682
5683/*
5684 * Cursor down a number of logical lines.
5685 */
5686 int
5687cursor_down(n, upd_topline)
5688 long n;
5689 int upd_topline; /* When TRUE: update topline */
5690{
5691 linenr_T lnum;
5692
5693 if (n > 0)
5694 {
5695 lnum = curwin->w_cursor.lnum;
5696#ifdef FEAT_FOLDING
5697 /* Move to last line of fold, will fail if it's the end-of-file. */
5698 (void)hasFolding(lnum, NULL, &lnum);
5699#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00005700 /* This fails if the cursor is already in the last line or would move
5701 * beyound the last line and '-' is in 'cpoptions' */
5702 if (lnum >= curbuf->b_ml.ml_line_count
5703 || (lnum + n > curbuf->b_ml.ml_line_count
5704 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 return FAIL;
5706 if (lnum + n >= curbuf->b_ml.ml_line_count)
5707 lnum = curbuf->b_ml.ml_line_count;
5708 else
5709#ifdef FEAT_FOLDING
5710 if (hasAnyFolding(curwin))
5711 {
5712 linenr_T last;
5713
5714 /* count each sequence of folded lines as one logical line */
5715 while (n--)
5716 {
5717 if (hasFolding(lnum, NULL, &last))
5718 lnum = last + 1;
5719 else
5720 ++lnum;
5721 if (lnum >= curbuf->b_ml.ml_line_count)
5722 break;
5723 }
5724 if (lnum > curbuf->b_ml.ml_line_count)
5725 lnum = curbuf->b_ml.ml_line_count;
5726 }
5727 else
5728#endif
5729 lnum += n;
5730 curwin->w_cursor.lnum = lnum;
5731 }
5732
5733 /* try to advance to the column we want to be at */
5734 coladvance(curwin->w_curswant);
5735
5736 if (upd_topline)
5737 update_topline(); /* make sure curwin->w_topline is valid */
5738
5739 return OK;
5740}
5741
5742/*
5743 * Stuff the last inserted text in the read buffer.
5744 * Last_insert actually is a copy of the redo buffer, so we
5745 * first have to remove the command.
5746 */
5747 int
5748stuff_inserted(c, count, no_esc)
5749 int c; /* Command character to be inserted */
5750 long count; /* Repeat this many times */
5751 int no_esc; /* Don't add an ESC at the end */
5752{
5753 char_u *esc_ptr;
5754 char_u *ptr;
5755 char_u *last_ptr;
5756 char_u last = NUL;
5757
5758 ptr = get_last_insert();
5759 if (ptr == NULL)
5760 {
5761 EMSG(_(e_noinstext));
5762 return FAIL;
5763 }
5764
5765 /* may want to stuff the command character, to start Insert mode */
5766 if (c != NUL)
5767 stuffcharReadbuff(c);
5768 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
5769 *esc_ptr = NUL; /* remove the ESC */
5770
5771 /* when the last char is either "0" or "^" it will be quoted if no ESC
5772 * comes after it OR if it will inserted more than once and "ptr"
5773 * starts with ^D. -- Acevedo
5774 */
5775 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
5776 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
5777 && (no_esc || (*ptr == Ctrl_D && count > 1)))
5778 {
5779 last = *last_ptr;
5780 *last_ptr = NUL;
5781 }
5782
5783 do
5784 {
5785 stuffReadbuff(ptr);
5786 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
5787 if (last)
5788 stuffReadbuff((char_u *)(last == '0'
5789 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
5790 : IF_EB("\026^", CTRL_V_STR "^")));
5791 }
5792 while (--count > 0);
5793
5794 if (last)
5795 *last_ptr = last;
5796
5797 if (esc_ptr != NULL)
5798 *esc_ptr = ESC; /* put the ESC back */
5799
5800 /* may want to stuff a trailing ESC, to get out of Insert mode */
5801 if (!no_esc)
5802 stuffcharReadbuff(ESC);
5803
5804 return OK;
5805}
5806
5807 char_u *
5808get_last_insert()
5809{
5810 if (last_insert == NULL)
5811 return NULL;
5812 return last_insert + last_insert_skip;
5813}
5814
5815/*
5816 * Get last inserted string, and remove trailing <Esc>.
5817 * Returns pointer to allocated memory (must be freed) or NULL.
5818 */
5819 char_u *
5820get_last_insert_save()
5821{
5822 char_u *s;
5823 int len;
5824
5825 if (last_insert == NULL)
5826 return NULL;
5827 s = vim_strsave(last_insert + last_insert_skip);
5828 if (s != NULL)
5829 {
5830 len = (int)STRLEN(s);
5831 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
5832 s[len - 1] = NUL;
5833 }
5834 return s;
5835}
5836
5837/*
5838 * Check the word in front of the cursor for an abbreviation.
5839 * Called when the non-id character "c" has been entered.
5840 * When an abbreviation is recognized it is removed from the text and
5841 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
5842 */
5843 static int
5844echeck_abbr(c)
5845 int c;
5846{
5847 /* Don't check for abbreviation in paste mode, when disabled and just
5848 * after moving around with cursor keys. */
5849 if (p_paste || no_abbr || arrow_used)
5850 return FALSE;
5851
5852 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
5853 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
5854}
5855
5856/*
5857 * replace-stack functions
5858 *
5859 * When replacing characters, the replaced characters are remembered for each
5860 * new character. This is used to re-insert the old text when backspacing.
5861 *
5862 * There is a NUL headed list of characters for each character that is
5863 * currently in the file after the insertion point. When BS is used, one NUL
5864 * headed list is put back for the deleted character.
5865 *
5866 * For a newline, there are two NUL headed lists. One contains the characters
5867 * that the NL replaced. The extra one stores the characters after the cursor
5868 * that were deleted (always white space).
5869 *
5870 * Replace_offset is normally 0, in which case replace_push will add a new
5871 * character at the end of the stack. If replace_offset is not 0, that many
5872 * characters will be left on the stack above the newly inserted character.
5873 */
5874
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00005875static char_u *replace_stack = NULL;
5876static long replace_stack_nr = 0; /* next entry in replace stack */
5877static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878
5879 void
5880replace_push(c)
5881 int c; /* character that is replaced (NUL is none) */
5882{
5883 char_u *p;
5884
5885 if (replace_stack_nr < replace_offset) /* nothing to do */
5886 return;
5887 if (replace_stack_len <= replace_stack_nr)
5888 {
5889 replace_stack_len += 50;
5890 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
5891 if (p == NULL) /* out of memory */
5892 {
5893 replace_stack_len -= 50;
5894 return;
5895 }
5896 if (replace_stack != NULL)
5897 {
5898 mch_memmove(p, replace_stack,
5899 (size_t)(replace_stack_nr * sizeof(char_u)));
5900 vim_free(replace_stack);
5901 }
5902 replace_stack = p;
5903 }
5904 p = replace_stack + replace_stack_nr - replace_offset;
5905 if (replace_offset)
5906 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
5907 *p = c;
5908 ++replace_stack_nr;
5909}
5910
5911/*
5912 * call replace_push(c) with replace_offset set to the first NUL.
5913 */
5914 static void
5915replace_push_off(c)
5916 int c;
5917{
5918 char_u *p;
5919
5920 p = replace_stack + replace_stack_nr;
5921 for (replace_offset = 1; replace_offset < replace_stack_nr;
5922 ++replace_offset)
5923 if (*--p == NUL)
5924 break;
5925 replace_push(c);
5926 replace_offset = 0;
5927}
5928
5929/*
5930 * Pop one item from the replace stack.
5931 * return -1 if stack empty
5932 * return replaced character or NUL otherwise
5933 */
5934 static int
5935replace_pop()
5936{
5937 if (replace_stack_nr == 0)
5938 return -1;
5939 return (int)replace_stack[--replace_stack_nr];
5940}
5941
5942/*
5943 * Join the top two items on the replace stack. This removes to "off"'th NUL
5944 * encountered.
5945 */
5946 static void
5947replace_join(off)
5948 int off; /* offset for which NUL to remove */
5949{
5950 int i;
5951
5952 for (i = replace_stack_nr; --i >= 0; )
5953 if (replace_stack[i] == NUL && off-- <= 0)
5954 {
5955 --replace_stack_nr;
5956 mch_memmove(replace_stack + i, replace_stack + i + 1,
5957 (size_t)(replace_stack_nr - i));
5958 return;
5959 }
5960}
5961
5962/*
5963 * Pop bytes from the replace stack until a NUL is found, and insert them
5964 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
5965 */
5966 static void
5967replace_pop_ins()
5968{
5969 int cc;
5970 int oldState = State;
5971
5972 State = NORMAL; /* don't want REPLACE here */
5973 while ((cc = replace_pop()) > 0)
5974 {
5975#ifdef FEAT_MBYTE
5976 mb_replace_pop_ins(cc);
5977#else
5978 ins_char(cc);
5979#endif
5980 dec_cursor();
5981 }
5982 State = oldState;
5983}
5984
5985#ifdef FEAT_MBYTE
5986/*
5987 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
5988 * indicates a multi-byte char, pop the other bytes too.
5989 */
5990 static void
5991mb_replace_pop_ins(cc)
5992 int cc;
5993{
5994 int n;
5995 char_u buf[MB_MAXBYTES];
5996 int i;
5997 int c;
5998
5999 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6000 {
6001 buf[0] = cc;
6002 for (i = 1; i < n; ++i)
6003 buf[i] = replace_pop();
6004 ins_bytes_len(buf, n);
6005 }
6006 else
6007 ins_char(cc);
6008
6009 if (enc_utf8)
6010 /* Handle composing chars. */
6011 for (;;)
6012 {
6013 c = replace_pop();
6014 if (c == -1) /* stack empty */
6015 break;
6016 if ((n = MB_BYTE2LEN(c)) == 1)
6017 {
6018 /* Not a multi-byte char, put it back. */
6019 replace_push(c);
6020 break;
6021 }
6022 else
6023 {
6024 buf[0] = c;
6025 for (i = 1; i < n; ++i)
6026 buf[i] = replace_pop();
6027 if (utf_iscomposing(utf_ptr2char(buf)))
6028 ins_bytes_len(buf, n);
6029 else
6030 {
6031 /* Not a composing char, put it back. */
6032 for (i = n - 1; i >= 0; --i)
6033 replace_push(buf[i]);
6034 break;
6035 }
6036 }
6037 }
6038}
6039#endif
6040
6041/*
6042 * make the replace stack empty
6043 * (called when exiting replace mode)
6044 */
6045 static void
6046replace_flush()
6047{
6048 vim_free(replace_stack);
6049 replace_stack = NULL;
6050 replace_stack_len = 0;
6051 replace_stack_nr = 0;
6052}
6053
6054/*
6055 * Handle doing a BS for one character.
6056 * cc < 0: replace stack empty, just move cursor
6057 * cc == 0: character was inserted, delete it
6058 * cc > 0: character was replaced, put cc (first byte of original char) back
6059 * and check for more characters to be put back
6060 */
6061 static void
6062replace_do_bs()
6063{
6064 int cc;
6065#ifdef FEAT_VREPLACE
6066 int orig_len = 0;
6067 int ins_len;
6068 int orig_vcols = 0;
6069 colnr_T start_vcol;
6070 char_u *p;
6071 int i;
6072 int vcol;
6073#endif
6074
6075 cc = replace_pop();
6076 if (cc > 0)
6077 {
6078#ifdef FEAT_VREPLACE
6079 if (State & VREPLACE_FLAG)
6080 {
6081 /* Get the number of screen cells used by the character we are
6082 * going to delete. */
6083 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6084 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6085 }
6086#endif
6087#ifdef FEAT_MBYTE
6088 if (has_mbyte)
6089 {
6090 del_char(FALSE);
6091# ifdef FEAT_VREPLACE
6092 if (State & VREPLACE_FLAG)
6093 orig_len = STRLEN(ml_get_cursor());
6094# endif
6095 replace_push(cc);
6096 }
6097 else
6098#endif
6099 {
6100 pchar_cursor(cc);
6101#ifdef FEAT_VREPLACE
6102 if (State & VREPLACE_FLAG)
6103 orig_len = STRLEN(ml_get_cursor()) - 1;
6104#endif
6105 }
6106 replace_pop_ins();
6107
6108#ifdef FEAT_VREPLACE
6109 if (State & VREPLACE_FLAG)
6110 {
6111 /* Get the number of screen cells used by the inserted characters */
6112 p = ml_get_cursor();
6113 ins_len = STRLEN(p) - orig_len;
6114 vcol = start_vcol;
6115 for (i = 0; i < ins_len; ++i)
6116 {
6117 vcol += chartabsize(p + i, vcol);
6118#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006119 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120#endif
6121 }
6122 vcol -= start_vcol;
6123
6124 /* Delete spaces that were inserted after the cursor to keep the
6125 * text aligned. */
6126 curwin->w_cursor.col += ins_len;
6127 while (vcol > orig_vcols && gchar_cursor() == ' ')
6128 {
6129 del_char(FALSE);
6130 ++orig_vcols;
6131 }
6132 curwin->w_cursor.col -= ins_len;
6133 }
6134#endif
6135
6136 /* mark the buffer as changed and prepare for displaying */
6137 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6138 }
6139 else if (cc == 0)
6140 (void)del_char(FALSE);
6141}
6142
6143#ifdef FEAT_CINDENT
6144/*
6145 * Return TRUE if C-indenting is on.
6146 */
6147 static int
6148cindent_on()
6149{
6150 return (!p_paste && (curbuf->b_p_cin
6151# ifdef FEAT_EVAL
6152 || *curbuf->b_p_inde != NUL
6153# endif
6154 ));
6155}
6156#endif
6157
6158#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6159/*
6160 * Re-indent the current line, based on the current contents of it and the
6161 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6162 * confused what all the part that handles Control-T is doing that I'm not.
6163 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6164 */
6165
6166 void
6167fixthisline(get_the_indent)
6168 int (*get_the_indent) __ARGS((void));
6169{
6170 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6171 if (linewhite(curwin->w_cursor.lnum))
6172 did_ai = TRUE; /* delete the indent if the line stays empty */
6173}
6174
6175 void
6176fix_indent()
6177{
6178 if (p_paste)
6179 return;
6180# ifdef FEAT_LISP
6181 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6182 fixthisline(get_lisp_indent);
6183# endif
6184# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6185 else
6186# endif
6187# ifdef FEAT_CINDENT
6188 if (cindent_on())
6189 do_c_expr_indent();
6190# endif
6191}
6192
6193#endif
6194
6195#ifdef FEAT_CINDENT
6196/*
6197 * return TRUE if 'cinkeys' contains the key "keytyped",
6198 * when == '*': Only if key is preceded with '*' (indent before insert)
6199 * when == '!': Only if key is prededed with '!' (don't insert)
6200 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6201 *
6202 * "keytyped" can have a few special values:
6203 * KEY_OPEN_FORW
6204 * KEY_OPEN_BACK
6205 * KEY_COMPLETE just finished completion.
6206 *
6207 * If line_is_empty is TRUE accept keys with '0' before them.
6208 */
6209 int
6210in_cinkeys(keytyped, when, line_is_empty)
6211 int keytyped;
6212 int when;
6213 int line_is_empty;
6214{
6215 char_u *look;
6216 int try_match;
6217 int try_match_word;
6218 char_u *p;
6219 char_u *line;
6220 int icase;
6221 int i;
6222
6223#ifdef FEAT_EVAL
6224 if (*curbuf->b_p_inde != NUL)
6225 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6226 else
6227#endif
6228 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6229 while (*look)
6230 {
6231 /*
6232 * Find out if we want to try a match with this key, depending on
6233 * 'when' and a '*' or '!' before the key.
6234 */
6235 switch (when)
6236 {
6237 case '*': try_match = (*look == '*'); break;
6238 case '!': try_match = (*look == '!'); break;
6239 default: try_match = (*look != '*'); break;
6240 }
6241 if (*look == '*' || *look == '!')
6242 ++look;
6243
6244 /*
6245 * If there is a '0', only accept a match if the line is empty.
6246 * But may still match when typing last char of a word.
6247 */
6248 if (*look == '0')
6249 {
6250 try_match_word = try_match;
6251 if (!line_is_empty)
6252 try_match = FALSE;
6253 ++look;
6254 }
6255 else
6256 try_match_word = FALSE;
6257
6258 /*
6259 * does it look like a control character?
6260 */
6261 if (*look == '^'
6262#ifdef EBCDIC
6263 && (Ctrl_chr(look[1]) != 0)
6264#else
6265 && look[1] >= '?' && look[1] <= '_'
6266#endif
6267 )
6268 {
6269 if (try_match && keytyped == Ctrl_chr(look[1]))
6270 return TRUE;
6271 look += 2;
6272 }
6273 /*
6274 * 'o' means "o" command, open forward.
6275 * 'O' means "O" command, open backward.
6276 */
6277 else if (*look == 'o')
6278 {
6279 if (try_match && keytyped == KEY_OPEN_FORW)
6280 return TRUE;
6281 ++look;
6282 }
6283 else if (*look == 'O')
6284 {
6285 if (try_match && keytyped == KEY_OPEN_BACK)
6286 return TRUE;
6287 ++look;
6288 }
6289
6290 /*
6291 * 'e' means to check for "else" at start of line and just before the
6292 * cursor.
6293 */
6294 else if (*look == 'e')
6295 {
6296 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6297 {
6298 p = ml_get_curline();
6299 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6300 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6301 return TRUE;
6302 }
6303 ++look;
6304 }
6305
6306 /*
6307 * ':' only causes an indent if it is at the end of a label or case
6308 * statement, or when it was before typing the ':' (to fix
6309 * class::method for C++).
6310 */
6311 else if (*look == ':')
6312 {
6313 if (try_match && keytyped == ':')
6314 {
6315 p = ml_get_curline();
6316 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6317 return TRUE;
6318 if (curwin->w_cursor.col > 2
6319 && p[curwin->w_cursor.col - 1] == ':'
6320 && p[curwin->w_cursor.col - 2] == ':')
6321 {
6322 p[curwin->w_cursor.col - 1] = ' ';
6323 i = (cin_iscase(p) || cin_isscopedecl(p)
6324 || cin_islabel(30));
6325 p = ml_get_curline();
6326 p[curwin->w_cursor.col - 1] = ':';
6327 if (i)
6328 return TRUE;
6329 }
6330 }
6331 ++look;
6332 }
6333
6334
6335 /*
6336 * Is it a key in <>, maybe?
6337 */
6338 else if (*look == '<')
6339 {
6340 if (try_match)
6341 {
6342 /*
6343 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6344 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6345 * >, *, : and ! keys if they really really want to.
6346 */
6347 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6348 && keytyped == look[1])
6349 return TRUE;
6350
6351 if (keytyped == get_special_key_code(look + 1))
6352 return TRUE;
6353 }
6354 while (*look && *look != '>')
6355 look++;
6356 while (*look == '>')
6357 look++;
6358 }
6359
6360 /*
6361 * Is it a word: "=word"?
6362 */
6363 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6364 {
6365 ++look;
6366 if (*look == '~')
6367 {
6368 icase = TRUE;
6369 ++look;
6370 }
6371 else
6372 icase = FALSE;
6373 p = vim_strchr(look, ',');
6374 if (p == NULL)
6375 p = look + STRLEN(look);
6376 if ((try_match || try_match_word)
6377 && curwin->w_cursor.col >= (colnr_T)(p - look))
6378 {
6379 int match = FALSE;
6380
6381#ifdef FEAT_INS_EXPAND
6382 if (keytyped == KEY_COMPLETE)
6383 {
6384 char_u *s;
6385
6386 /* Just completed a word, check if it starts with "look".
6387 * search back for the start of a word. */
6388 line = ml_get_curline();
6389# ifdef FEAT_MBYTE
6390 if (has_mbyte)
6391 {
6392 char_u *n;
6393
6394 for (s = line + curwin->w_cursor.col; s > line; s = n)
6395 {
6396 n = mb_prevptr(line, s);
6397 if (!vim_iswordp(n))
6398 break;
6399 }
6400 }
6401 else
6402# endif
6403 for (s = line + curwin->w_cursor.col; s > line; --s)
6404 if (!vim_iswordc(s[-1]))
6405 break;
6406 if (s + (p - look) <= line + curwin->w_cursor.col
6407 && (icase
6408 ? MB_STRNICMP(s, look, p - look)
6409 : STRNCMP(s, look, p - look)) == 0)
6410 match = TRUE;
6411 }
6412 else
6413#endif
6414 /* TODO: multi-byte */
6415 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6416 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6417 {
6418 line = ml_get_cursor();
6419 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6420 || !vim_iswordc(line[-(p - look) - 1]))
6421 && (icase
6422 ? MB_STRNICMP(line - (p - look), look, p - look)
6423 : STRNCMP(line - (p - look), look, p - look))
6424 == 0)
6425 match = TRUE;
6426 }
6427 if (match && try_match_word && !try_match)
6428 {
6429 /* "0=word": Check if there are only blanks before the
6430 * word. */
6431 line = ml_get_curline();
6432 if ((int)(skipwhite(line) - line) !=
6433 (int)(curwin->w_cursor.col - (p - look)))
6434 match = FALSE;
6435 }
6436 if (match)
6437 return TRUE;
6438 }
6439 look = p;
6440 }
6441
6442 /*
6443 * ok, it's a boring generic character.
6444 */
6445 else
6446 {
6447 if (try_match && *look == keytyped)
6448 return TRUE;
6449 ++look;
6450 }
6451
6452 /*
6453 * Skip over ", ".
6454 */
6455 look = skip_to_option_part(look);
6456 }
6457 return FALSE;
6458}
6459#endif /* FEAT_CINDENT */
6460
6461#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6462/*
6463 * Map Hebrew keyboard when in hkmap mode.
6464 */
6465 int
6466hkmap(c)
6467 int c;
6468{
6469 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6470 {
6471 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6472 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6473 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6474 static char_u map[26] =
6475 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6476 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6477 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6478 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6479 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6480 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6481 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6482 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6483 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6484
6485 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6486 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6487 /* '-1'='sofit' */
6488 else if (c == 'x')
6489 return 'X';
6490 else if (c == 'q')
6491 return '\''; /* {geresh}={'} */
6492 else if (c == 246)
6493 return ' '; /* \"o --> ' ' for a german keyboard */
6494 else if (c == 228)
6495 return ' '; /* \"a --> ' ' -- / -- */
6496 else if (c == 252)
6497 return ' '; /* \"u --> ' ' -- / -- */
6498#ifdef EBCDIC
6499 else if (islower(c))
6500#else
6501 /* NOTE: islower() does not do the right thing for us on Linux so we
6502 * do this the same was as 5.7 and previous, so it works correctly on
6503 * all systems. Specifically, the e.g. Delete and Arrow keys are
6504 * munged and won't work if e.g. searching for Hebrew text.
6505 */
6506 else if (c >= 'a' && c <= 'z')
6507#endif
6508 return (int)(map[CharOrdLow(c)] + p_aleph);
6509 else
6510 return c;
6511 }
6512 else
6513 {
6514 switch (c)
6515 {
6516 case '`': return ';';
6517 case '/': return '.';
6518 case '\'': return ',';
6519 case 'q': return '/';
6520 case 'w': return '\'';
6521
6522 /* Hebrew letters - set offset from 'a' */
6523 case ',': c = '{'; break;
6524 case '.': c = 'v'; break;
6525 case ';': c = 't'; break;
6526 default: {
6527 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6528
6529#ifdef EBCDIC
6530 /* see note about islower() above */
6531 if (!islower(c))
6532#else
6533 if (c < 'a' || c > 'z')
6534#endif
6535 return c;
6536 c = str[CharOrdLow(c)];
6537 break;
6538 }
6539 }
6540
6541 return (int)(CharOrdLow(c) + p_aleph);
6542 }
6543}
6544#endif
6545
6546 static void
6547ins_reg()
6548{
6549 int need_redraw = FALSE;
6550 int regname;
6551 int literally = 0;
6552
6553 /*
6554 * If we are going to wait for a character, show a '"'.
6555 */
6556 pc_status = PC_STATUS_UNSET;
6557 if (redrawing() && !char_avail())
6558 {
6559 /* may need to redraw when no more chars available now */
6560 ins_redraw();
6561
6562 edit_putchar('"', TRUE);
6563#ifdef FEAT_CMDL_INFO
6564 add_to_showcmd_c(Ctrl_R);
6565#endif
6566 }
6567
6568#ifdef USE_ON_FLY_SCROLL
6569 dont_scroll = TRUE; /* disallow scrolling here */
6570#endif
6571
6572 /*
6573 * Don't map the register name. This also prevents the mode message to be
6574 * deleted when ESC is hit.
6575 */
6576 ++no_mapping;
6577 regname = safe_vgetc();
6578#ifdef FEAT_LANGMAP
6579 LANGMAP_ADJUST(regname, TRUE);
6580#endif
6581 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6582 {
6583 /* Get a third key for literal register insertion */
6584 literally = regname;
6585#ifdef FEAT_CMDL_INFO
6586 add_to_showcmd_c(literally);
6587#endif
6588 regname = safe_vgetc();
6589#ifdef FEAT_LANGMAP
6590 LANGMAP_ADJUST(regname, TRUE);
6591#endif
6592 }
6593 --no_mapping;
6594
6595#ifdef FEAT_EVAL
6596 /*
6597 * Don't call u_sync() while getting the expression,
6598 * evaluating it or giving an error message for it!
6599 */
6600 ++no_u_sync;
6601 if (regname == '=')
6602 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006603# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006605# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006607# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 /* Restore the Input Method. */
6609 if (im_on)
6610 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006611# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006613 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6614 {
6615 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 else
6619 {
6620#endif
6621 if (literally == Ctrl_O || literally == Ctrl_P)
6622 {
6623 /* Append the command to the redo buffer. */
6624 AppendCharToRedobuff(Ctrl_R);
6625 AppendCharToRedobuff(literally);
6626 AppendCharToRedobuff(regname);
6627
6628 do_put(regname, BACKWARD, 1L,
6629 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6630 }
6631 else if (insert_reg(regname, literally) == FAIL)
6632 {
6633 vim_beep();
6634 need_redraw = TRUE; /* remove the '"' */
6635 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006636 else if (stop_insert_mode)
6637 /* When the '=' register was used and a function was invoked that
6638 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
6639 * insert anything, need to remove the '"' */
6640 need_redraw = TRUE;
6641
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642#ifdef FEAT_EVAL
6643 }
6644 --no_u_sync;
6645#endif
6646#ifdef FEAT_CMDL_INFO
6647 clear_showcmd();
6648#endif
6649
6650 /* If the inserted register is empty, we need to remove the '"' */
6651 if (need_redraw || stuff_empty())
6652 edit_unputchar();
6653}
6654
6655/*
6656 * CTRL-G commands in Insert mode.
6657 */
6658 static void
6659ins_ctrl_g()
6660{
6661 int c;
6662
6663#ifdef FEAT_INS_EXPAND
6664 /* Right after CTRL-X the cursor will be after the ruler. */
6665 setcursor();
6666#endif
6667
6668 /*
6669 * Don't map the second key. This also prevents the mode message to be
6670 * deleted when ESC is hit.
6671 */
6672 ++no_mapping;
6673 c = safe_vgetc();
6674 --no_mapping;
6675 switch (c)
6676 {
6677 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
6678 case K_UP:
6679 case Ctrl_K:
6680 case 'k': ins_up(TRUE);
6681 break;
6682
6683 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
6684 case K_DOWN:
6685 case Ctrl_J:
6686 case 'j': ins_down(TRUE);
6687 break;
6688
6689 /* CTRL-G u: start new undoable edit */
6690 case 'u': u_sync();
6691 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006692
6693 /* Need to reset Insstart, esp. because a BS that joins
6694 * aline to the previous one must save for undo. */
6695 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 break;
6697
6698 /* Unknown CTRL-G command, reserved for future expansion. */
6699 default: vim_beep();
6700 }
6701}
6702
6703/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006704 * CTRL-^ in Insert mode.
6705 */
6706 static void
6707ins_ctrl_hat()
6708{
6709 if (map_to_exists_mode((char_u *)"", LANGMAP))
6710 {
6711 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
6712 if (State & LANGMAP)
6713 {
6714 curbuf->b_p_iminsert = B_IMODE_NONE;
6715 State &= ~LANGMAP;
6716 }
6717 else
6718 {
6719 curbuf->b_p_iminsert = B_IMODE_LMAP;
6720 State |= LANGMAP;
6721#ifdef USE_IM_CONTROL
6722 im_set_active(FALSE);
6723#endif
6724 }
6725 }
6726#ifdef USE_IM_CONTROL
6727 else
6728 {
6729 /* There are no ":lmap" mappings, toggle IM */
6730 if (im_get_status())
6731 {
6732 curbuf->b_p_iminsert = B_IMODE_NONE;
6733 im_set_active(FALSE);
6734 }
6735 else
6736 {
6737 curbuf->b_p_iminsert = B_IMODE_IM;
6738 State &= ~LANGMAP;
6739 im_set_active(TRUE);
6740 }
6741 }
6742#endif
6743 set_iminsert_global();
6744 showmode();
6745#ifdef FEAT_GUI
6746 /* may show different cursor shape or color */
6747 if (gui.in_use)
6748 gui_update_cursor(TRUE, FALSE);
6749#endif
6750#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6751 /* Show/unshow value of 'keymap' in status lines. */
6752 status_redraw_curbuf();
6753#endif
6754}
6755
6756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 * Handle ESC in insert mode.
6758 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
6759 * insert.
6760 */
6761 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00006762ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 long *count;
6764 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00006765 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766{
6767 int temp;
6768 static int disabled_redraw = FALSE;
6769
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006770#ifdef FEAT_SYN_HL
6771 check_spell_redraw();
6772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773#if defined(FEAT_HANGULIN)
6774# if defined(ESC_CHG_TO_ENG_MODE)
6775 hangul_input_state_set(0);
6776# endif
6777 if (composing_hangul)
6778 {
6779 push_raw_key(composing_hangul_buffer, 2);
6780 composing_hangul = 0;
6781 }
6782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783
6784 temp = curwin->w_cursor.col;
6785 if (disabled_redraw)
6786 {
6787 --RedrawingDisabled;
6788 disabled_redraw = FALSE;
6789 }
6790 if (!arrow_used)
6791 {
6792 /*
6793 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00006794 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
6795 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 */
6797 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00006798 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799
6800 /*
6801 * Repeating insert may take a long time. Check for
6802 * interrupt now and then.
6803 */
6804 if (*count > 0)
6805 {
6806 line_breakcheck();
6807 if (got_int)
6808 *count = 0;
6809 }
6810
6811 if (--*count > 0) /* repeat what was typed */
6812 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006813 /* Vi repeats the insert without replacing characters. */
6814 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
6815 State &= ~REPLACE_FLAG;
6816
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 (void)start_redo_ins();
6818 if (cmdchar == 'r' || cmdchar == 'v')
6819 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
6820 ++RedrawingDisabled;
6821 disabled_redraw = TRUE;
6822 return FALSE; /* repeat the insert */
6823 }
6824 stop_insert(&curwin->w_cursor, TRUE);
6825 undisplay_dollar();
6826 }
6827
6828 /* When an autoindent was removed, curswant stays after the
6829 * indent */
6830 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
6831 curwin->w_set_curswant = TRUE;
6832
6833 /* Remember the last Insert position in the '^ mark. */
6834 if (!cmdmod.keepjumps)
6835 curbuf->b_last_insert = curwin->w_cursor;
6836
6837 /*
6838 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00006839 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00006841 if (!nomove
6842 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843#ifdef FEAT_VIRTUALEDIT
6844 || curwin->w_cursor.coladd > 0
6845#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006846 )
6847 && (restart_edit == NUL
6848 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00006850 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006852 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853#ifdef FEAT_RIGHTLEFT
6854 && !revins_on
6855#endif
6856 )
6857 {
6858#ifdef FEAT_VIRTUALEDIT
6859 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
6860 {
6861 oneleft();
6862 if (restart_edit != NUL)
6863 ++curwin->w_cursor.coladd;
6864 }
6865 else
6866#endif
6867 {
6868 --curwin->w_cursor.col;
6869#ifdef FEAT_MBYTE
6870 /* Correct cursor for multi-byte character. */
6871 if (has_mbyte)
6872 mb_adjust_cursor();
6873#endif
6874 }
6875 }
6876
6877#ifdef USE_IM_CONTROL
6878 /* Disable IM to allow typing English directly for Normal mode commands.
6879 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
6880 * well). */
6881 if (!(State & LANGMAP))
6882 im_save_status(&curbuf->b_p_iminsert);
6883 im_set_active(FALSE);
6884#endif
6885
6886 State = NORMAL;
6887 /* need to position cursor again (e.g. when on a TAB ) */
6888 changed_cline_bef_curs();
6889
6890#ifdef FEAT_MOUSE
6891 setmouse();
6892#endif
6893#ifdef CURSOR_SHAPE
6894 ui_cursor_shape(); /* may show different cursor shape */
6895#endif
6896
6897 /*
6898 * When recording or for CTRL-O, need to display the new mode.
6899 * Otherwise remove the mode message.
6900 */
6901 if (Recording || restart_edit != NUL)
6902 showmode();
6903 else if (p_smd)
6904 MSG("");
6905
6906 return TRUE; /* exit Insert mode */
6907}
6908
6909#ifdef FEAT_RIGHTLEFT
6910/*
6911 * Toggle language: hkmap and revins_on.
6912 * Move to end of reverse inserted text.
6913 */
6914 static void
6915ins_ctrl_()
6916{
6917 if (revins_on && revins_chars && revins_scol >= 0)
6918 {
6919 while (gchar_cursor() != NUL && revins_chars--)
6920 ++curwin->w_cursor.col;
6921 }
6922 p_ri = !p_ri;
6923 revins_on = (State == INSERT && p_ri);
6924 if (revins_on)
6925 {
6926 revins_scol = curwin->w_cursor.col;
6927 revins_legal++;
6928 revins_chars = 0;
6929 undisplay_dollar();
6930 }
6931 else
6932 revins_scol = -1;
6933#ifdef FEAT_FKMAP
6934 if (p_altkeymap)
6935 {
6936 /*
6937 * to be consistent also for redo command, using '.'
6938 * set arrow_used to true and stop it - causing to redo
6939 * characters entered in one mode (normal/reverse insert).
6940 */
6941 arrow_used = TRUE;
6942 (void)stop_arrow();
6943 p_fkmap = curwin->w_p_rl ^ p_ri;
6944 if (p_fkmap && p_ri)
6945 State = INSERT;
6946 }
6947 else
6948#endif
6949 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
6950 showmode();
6951}
6952#endif
6953
6954#ifdef FEAT_VISUAL
6955/*
6956 * If 'keymodel' contains "startsel", may start selection.
6957 * Returns TRUE when a CTRL-O and other keys stuffed.
6958 */
6959 static int
6960ins_start_select(c)
6961 int c;
6962{
6963 if (km_startsel)
6964 switch (c)
6965 {
6966 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 case K_PAGEUP:
6969 case K_KPAGEUP:
6970 case K_PAGEDOWN:
6971 case K_KPAGEDOWN:
6972# ifdef MACOS
6973 case K_LEFT:
6974 case K_RIGHT:
6975 case K_UP:
6976 case K_DOWN:
6977 case K_END:
6978 case K_HOME:
6979# endif
6980 if (!(mod_mask & MOD_MASK_SHIFT))
6981 break;
6982 /* FALLTHROUGH */
6983 case K_S_LEFT:
6984 case K_S_RIGHT:
6985 case K_S_UP:
6986 case K_S_DOWN:
6987 case K_S_END:
6988 case K_S_HOME:
6989 /* Start selection right away, the cursor can move with
6990 * CTRL-O when beyond the end of the line. */
6991 start_selection();
6992
6993 /* Execute the key in (insert) Select mode. */
6994 stuffcharReadbuff(Ctrl_O);
6995 if (mod_mask)
6996 {
6997 char_u buf[4];
6998
6999 buf[0] = K_SPECIAL;
7000 buf[1] = KS_MODIFIER;
7001 buf[2] = mod_mask;
7002 buf[3] = NUL;
7003 stuffReadbuff(buf);
7004 }
7005 stuffcharReadbuff(c);
7006 return TRUE;
7007 }
7008 return FALSE;
7009}
7010#endif
7011
7012/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007013 * <Insert> key in Insert mode: toggle insert/remplace mode.
7014 */
7015 static void
7016ins_insert(replaceState)
7017 int replaceState;
7018{
7019#ifdef FEAT_FKMAP
7020 if (p_fkmap && p_ri)
7021 {
7022 beep_flush();
7023 EMSG(farsi_text_3); /* encoded in Farsi */
7024 return;
7025 }
7026#endif
7027
7028#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007029# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007030 set_vim_var_string(VV_INSERTMODE,
7031 (char_u *)((State & REPLACE_FLAG) ? "i" :
7032 replaceState == VREPLACE ? "v" : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007033# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007034 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7035#endif
7036 if (State & REPLACE_FLAG)
7037 State = INSERT | (State & LANGMAP);
7038 else
7039 State = replaceState | (State & LANGMAP);
7040 AppendCharToRedobuff(K_INS);
7041 showmode();
7042#ifdef CURSOR_SHAPE
7043 ui_cursor_shape(); /* may show different cursor shape */
7044#endif
7045}
7046
7047/*
7048 * Pressed CTRL-O in Insert mode.
7049 */
7050 static void
7051ins_ctrl_o()
7052{
7053#ifdef FEAT_VREPLACE
7054 if (State & VREPLACE_FLAG)
7055 restart_edit = 'V';
7056 else
7057#endif
7058 if (State & REPLACE_FLAG)
7059 restart_edit = 'R';
7060 else
7061 restart_edit = 'I';
7062#ifdef FEAT_VIRTUALEDIT
7063 if (virtual_active())
7064 ins_at_eol = FALSE; /* cursor always keeps its column */
7065 else
7066#endif
7067 ins_at_eol = (gchar_cursor() == NUL);
7068}
7069
7070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 * If the cursor is on an indent, ^T/^D insert/delete one
7072 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7073 * Always round the indent to 'shiftwith', this is compatible
7074 * with vi. But vi only supports ^T and ^D after an
7075 * autoindent, we support it everywhere.
7076 */
7077 static void
7078ins_shift(c, lastc)
7079 int c;
7080 int lastc;
7081{
7082 if (stop_arrow() == FAIL)
7083 return;
7084 AppendCharToRedobuff(c);
7085
7086 /*
7087 * 0^D and ^^D: remove all indent.
7088 */
7089 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7090 {
7091 --curwin->w_cursor.col;
7092 (void)del_char(FALSE); /* delete the '^' or '0' */
7093 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7094 if (State & REPLACE_FLAG)
7095 replace_pop_ins();
7096 if (lastc == '^')
7097 old_indent = get_indent(); /* remember curr. indent */
7098 change_indent(INDENT_SET, 0, TRUE, 0);
7099 }
7100 else
7101 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7102
7103 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7104 did_ai = FALSE;
7105#ifdef FEAT_SMARTINDENT
7106 did_si = FALSE;
7107 can_si = FALSE;
7108 can_si_back = FALSE;
7109#endif
7110#ifdef FEAT_CINDENT
7111 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7112#endif
7113}
7114
7115 static void
7116ins_del()
7117{
7118 int temp;
7119
7120 if (stop_arrow() == FAIL)
7121 return;
7122 if (gchar_cursor() == NUL) /* delete newline */
7123 {
7124 temp = curwin->w_cursor.col;
7125 if (!can_bs(BS_EOL) /* only if "eol" included */
7126 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7127 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7128 || do_join(FALSE) == FAIL)
7129 vim_beep();
7130 else
7131 curwin->w_cursor.col = temp;
7132 }
7133 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7134 vim_beep();
7135 did_ai = FALSE;
7136#ifdef FEAT_SMARTINDENT
7137 did_si = FALSE;
7138 can_si = FALSE;
7139 can_si_back = FALSE;
7140#endif
7141 AppendCharToRedobuff(K_DEL);
7142}
7143
7144/*
7145 * Handle Backspace, delete-word and delete-line in Insert mode.
7146 * Return TRUE when backspace was actually used.
7147 */
7148 static int
7149ins_bs(c, mode, inserted_space_p)
7150 int c;
7151 int mode;
7152 int *inserted_space_p;
7153{
7154 linenr_T lnum;
7155 int cc;
7156 int temp = 0; /* init for GCC */
7157 colnr_T mincol;
7158 int did_backspace = FALSE;
7159 int in_indent;
7160 int oldState;
7161#ifdef FEAT_MBYTE
7162 int p1, p2;
7163#endif
7164
7165 /*
7166 * can't delete anything in an empty file
7167 * can't backup past first character in buffer
7168 * can't backup past starting point unless 'backspace' > 1
7169 * can backup to a previous line if 'backspace' == 0
7170 */
7171 if ( bufempty()
7172 || (
7173#ifdef FEAT_RIGHTLEFT
7174 !revins_on &&
7175#endif
7176 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7177 || (!can_bs(BS_START)
7178 && (arrow_used
7179 || (curwin->w_cursor.lnum == Insstart.lnum
7180 && curwin->w_cursor.col <= Insstart.col)))
7181 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7182 && curwin->w_cursor.col <= ai_col)
7183 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7184 {
7185 vim_beep();
7186 return FALSE;
7187 }
7188
7189 if (stop_arrow() == FAIL)
7190 return FALSE;
7191 in_indent = inindent(0);
7192#ifdef FEAT_CINDENT
7193 if (in_indent)
7194 can_cindent = FALSE;
7195#endif
7196#ifdef FEAT_COMMENTS
7197 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7198#endif
7199#ifdef FEAT_RIGHTLEFT
7200 if (revins_on) /* put cursor after last inserted char */
7201 inc_cursor();
7202#endif
7203
7204#ifdef FEAT_VIRTUALEDIT
7205 /* Virtualedit:
7206 * BACKSPACE_CHAR eats a virtual space
7207 * BACKSPACE_WORD eats all coladd
7208 * BACKSPACE_LINE eats all coladd and keeps going
7209 */
7210 if (curwin->w_cursor.coladd > 0)
7211 {
7212 if (mode == BACKSPACE_CHAR)
7213 {
7214 --curwin->w_cursor.coladd;
7215 return TRUE;
7216 }
7217 if (mode == BACKSPACE_WORD)
7218 {
7219 curwin->w_cursor.coladd = 0;
7220 return TRUE;
7221 }
7222 curwin->w_cursor.coladd = 0;
7223 }
7224#endif
7225
7226 /*
7227 * delete newline!
7228 */
7229 if (curwin->w_cursor.col == 0)
7230 {
7231 lnum = Insstart.lnum;
7232 if (curwin->w_cursor.lnum == Insstart.lnum
7233#ifdef FEAT_RIGHTLEFT
7234 || revins_on
7235#endif
7236 )
7237 {
7238 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7239 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7240 return FALSE;
7241 --Insstart.lnum;
7242 Insstart.col = MAXCOL;
7243 }
7244 /*
7245 * In replace mode:
7246 * cc < 0: NL was inserted, delete it
7247 * cc >= 0: NL was replaced, put original characters back
7248 */
7249 cc = -1;
7250 if (State & REPLACE_FLAG)
7251 cc = replace_pop(); /* returns -1 if NL was inserted */
7252 /*
7253 * In replace mode, in the line we started replacing, we only move the
7254 * cursor.
7255 */
7256 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7257 {
7258 dec_cursor();
7259 }
7260 else
7261 {
7262#ifdef FEAT_VREPLACE
7263 if (!(State & VREPLACE_FLAG)
7264 || curwin->w_cursor.lnum > orig_line_count)
7265#endif
7266 {
7267 temp = gchar_cursor(); /* remember current char */
7268 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007269
7270 /* When "aw" is in 'formatoptions' we must delete the space at
7271 * the end of the line, otherwise the line will be broken
7272 * again when auto-formatting. */
7273 if (has_format_option(FO_AUTO)
7274 && has_format_option(FO_WHITE_PAR))
7275 {
7276 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7277 TRUE);
7278 int len;
7279
7280 len = STRLEN(ptr);
7281 if (len > 0 && ptr[len - 1] == ' ')
7282 ptr[len - 1] = NUL;
7283 }
7284
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 (void)do_join(FALSE);
7286 if (temp == NUL && gchar_cursor() != NUL)
7287 inc_cursor();
7288 }
7289#ifdef FEAT_VREPLACE
7290 else
7291 dec_cursor();
7292#endif
7293
7294 /*
7295 * In REPLACE mode we have to put back the text that was replaced
7296 * by the NL. On the replace stack is first a NUL-terminated
7297 * sequence of characters that were deleted and then the
7298 * characters that NL replaced.
7299 */
7300 if (State & REPLACE_FLAG)
7301 {
7302 /*
7303 * Do the next ins_char() in NORMAL state, to
7304 * prevent ins_char() from replacing characters and
7305 * avoiding showmatch().
7306 */
7307 oldState = State;
7308 State = NORMAL;
7309 /*
7310 * restore characters (blanks) deleted after cursor
7311 */
7312 while (cc > 0)
7313 {
7314 temp = curwin->w_cursor.col;
7315#ifdef FEAT_MBYTE
7316 mb_replace_pop_ins(cc);
7317#else
7318 ins_char(cc);
7319#endif
7320 curwin->w_cursor.col = temp;
7321 cc = replace_pop();
7322 }
7323 /* restore the characters that NL replaced */
7324 replace_pop_ins();
7325 State = oldState;
7326 }
7327 }
7328 did_ai = FALSE;
7329 }
7330 else
7331 {
7332 /*
7333 * Delete character(s) before the cursor.
7334 */
7335#ifdef FEAT_RIGHTLEFT
7336 if (revins_on) /* put cursor on last inserted char */
7337 dec_cursor();
7338#endif
7339 mincol = 0;
7340 /* keep indent */
7341 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7342#ifdef FEAT_RIGHTLEFT
7343 && !revins_on
7344#endif
7345 )
7346 {
7347 temp = curwin->w_cursor.col;
7348 beginline(BL_WHITE);
7349 if (curwin->w_cursor.col < (colnr_T)temp)
7350 mincol = curwin->w_cursor.col;
7351 curwin->w_cursor.col = temp;
7352 }
7353
7354 /*
7355 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7356 */
7357 if ( mode == BACKSPACE_CHAR
7358 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007359 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 && (*(ml_get_cursor() - 1) == TAB
7361 || (*(ml_get_cursor() - 1) == ' '
7362 && (!*inserted_space_p
7363 || arrow_used))))))
7364 {
7365 int ts;
7366 colnr_T vcol;
7367 colnr_T want_vcol;
7368 int extra = 0;
7369
7370 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007371 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 ts = curbuf->b_p_sw;
7373 else
7374 ts = curbuf->b_p_sts;
7375 /* Compute the virtual column where we want to be. Since
7376 * 'showbreak' may get in the way, need to get the last column of
7377 * the previous character. */
7378 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7379 dec_cursor();
7380 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7381 inc_cursor();
7382 want_vcol = (want_vcol / ts) * ts;
7383
7384 /* delete characters until we are at or before want_vcol */
7385 while (vcol > want_vcol
7386 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7387 {
7388 dec_cursor();
7389 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7390 if (State & REPLACE_FLAG)
7391 {
7392 /* Don't delete characters before the insert point when in
7393 * Replace mode */
7394 if (curwin->w_cursor.lnum != Insstart.lnum
7395 || curwin->w_cursor.col >= Insstart.col)
7396 {
7397#if 0 /* what was this for? It causes problems when sw != ts. */
7398 if (State == REPLACE && (int)vcol < want_vcol)
7399 {
7400 (void)del_char(FALSE);
7401 extra = 2; /* don't pop too much */
7402 }
7403 else
7404#endif
7405 replace_do_bs();
7406 }
7407 }
7408 else
7409 (void)del_char(FALSE);
7410 }
7411
7412 /* insert extra spaces until we are at want_vcol */
7413 while (vcol < want_vcol)
7414 {
7415 /* Remember the first char we inserted */
7416 if (curwin->w_cursor.lnum == Insstart.lnum
7417 && curwin->w_cursor.col < Insstart.col)
7418 Insstart.col = curwin->w_cursor.col;
7419
7420#ifdef FEAT_VREPLACE
7421 if (State & VREPLACE_FLAG)
7422 ins_char(' ');
7423 else
7424#endif
7425 {
7426 ins_str((char_u *)" ");
7427 if ((State & REPLACE_FLAG) && extra <= 1)
7428 {
7429 if (extra)
7430 replace_push_off(NUL);
7431 else
7432 replace_push(NUL);
7433 }
7434 if (extra == 2)
7435 extra = 1;
7436 }
7437 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7438 }
7439 }
7440
7441 /*
7442 * Delete upto starting point, start of line or previous word.
7443 */
7444 else do
7445 {
7446#ifdef FEAT_RIGHTLEFT
7447 if (!revins_on) /* put cursor on char to be deleted */
7448#endif
7449 dec_cursor();
7450
7451 /* start of word? */
7452 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7453 {
7454 mode = BACKSPACE_WORD_NOT_SPACE;
7455 temp = vim_iswordc(gchar_cursor());
7456 }
7457 /* end of word? */
7458 else if (mode == BACKSPACE_WORD_NOT_SPACE
7459 && (vim_isspace(cc = gchar_cursor())
7460 || vim_iswordc(cc) != temp))
7461 {
7462#ifdef FEAT_RIGHTLEFT
7463 if (!revins_on)
7464#endif
7465 inc_cursor();
7466#ifdef FEAT_RIGHTLEFT
7467 else if (State & REPLACE_FLAG)
7468 dec_cursor();
7469#endif
7470 break;
7471 }
7472 if (State & REPLACE_FLAG)
7473 replace_do_bs();
7474 else
7475 {
7476#ifdef FEAT_MBYTE
7477 if (enc_utf8 && p_deco)
7478 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7479#endif
7480 (void)del_char(FALSE);
7481#ifdef FEAT_MBYTE
7482 /*
7483 * If p1 or p2 is non-zero, there are combining characters we
7484 * need to take account of. Don't back up before the base
7485 * character.
7486 */
7487 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7488 inc_cursor();
7489#endif
7490#ifdef FEAT_RIGHTLEFT
7491 if (revins_chars)
7492 {
7493 revins_chars--;
7494 revins_legal++;
7495 }
7496 if (revins_on && gchar_cursor() == NUL)
7497 break;
7498#endif
7499 }
7500 /* Just a single backspace?: */
7501 if (mode == BACKSPACE_CHAR)
7502 break;
7503 } while (
7504#ifdef FEAT_RIGHTLEFT
7505 revins_on ||
7506#endif
7507 (curwin->w_cursor.col > mincol
7508 && (curwin->w_cursor.lnum != Insstart.lnum
7509 || curwin->w_cursor.col != Insstart.col)));
7510 did_backspace = TRUE;
7511 }
7512#ifdef FEAT_SMARTINDENT
7513 did_si = FALSE;
7514 can_si = FALSE;
7515 can_si_back = FALSE;
7516#endif
7517 if (curwin->w_cursor.col <= 1)
7518 did_ai = FALSE;
7519 /*
7520 * It's a little strange to put backspaces into the redo
7521 * buffer, but it makes auto-indent a lot easier to deal
7522 * with.
7523 */
7524 AppendCharToRedobuff(c);
7525
7526 /* If deleted before the insertion point, adjust it */
7527 if (curwin->w_cursor.lnum == Insstart.lnum
7528 && curwin->w_cursor.col < Insstart.col)
7529 Insstart.col = curwin->w_cursor.col;
7530
7531 /* vi behaviour: the cursor moves backward but the character that
7532 * was there remains visible
7533 * Vim behaviour: the cursor moves backward and the character that
7534 * was there is erased from the screen.
7535 * We can emulate the vi behaviour by pretending there is a dollar
7536 * displayed even when there isn't.
7537 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7538 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7539 dollar_vcol = curwin->w_virtcol;
7540
7541 return did_backspace;
7542}
7543
7544#ifdef FEAT_MOUSE
7545 static void
7546ins_mouse(c)
7547 int c;
7548{
7549 pos_T tpos;
7550
7551# ifdef FEAT_GUI
7552 /* When GUI is active, also move/paste when 'mouse' is empty */
7553 if (!gui.in_use)
7554# endif
7555 if (!mouse_has(MOUSE_INSERT))
7556 return;
7557
7558 undisplay_dollar();
7559 tpos = curwin->w_cursor;
7560 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7561 {
7562 start_arrow(&tpos);
7563# ifdef FEAT_CINDENT
7564 can_cindent = TRUE;
7565# endif
7566 }
7567
7568#ifdef FEAT_WINDOWS
7569 /* redraw status lines (in case another window became active) */
7570 redraw_statuslines();
7571#endif
7572}
7573
7574 static void
7575ins_mousescroll(up)
7576 int up;
7577{
7578 pos_T tpos;
7579# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7580 win_T *old_curwin;
7581# endif
7582
7583 tpos = curwin->w_cursor;
7584
7585# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7586 old_curwin = curwin;
7587
7588 /* Currently the mouse coordinates are only known in the GUI. */
7589 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7590 {
7591 int row, col;
7592
7593 row = mouse_row;
7594 col = mouse_col;
7595
7596 /* find the window at the pointer coordinates */
7597 curwin = mouse_find_win(&row, &col);
7598 curbuf = curwin->w_buffer;
7599 }
7600 if (curwin == old_curwin)
7601# endif
7602 undisplay_dollar();
7603
7604 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7605 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7606 else
7607 scroll_redraw(up, 3L);
7608
7609# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7610 curwin->w_redr_status = TRUE;
7611
7612 curwin = old_curwin;
7613 curbuf = curwin->w_buffer;
7614# endif
7615
7616 if (!equalpos(curwin->w_cursor, tpos))
7617 {
7618 start_arrow(&tpos);
7619# ifdef FEAT_CINDENT
7620 can_cindent = TRUE;
7621# endif
7622 }
7623}
7624#endif
7625
7626#ifdef FEAT_GUI
7627 void
7628ins_scroll()
7629{
7630 pos_T tpos;
7631
7632 undisplay_dollar();
7633 tpos = curwin->w_cursor;
7634 if (gui_do_scroll())
7635 {
7636 start_arrow(&tpos);
7637# ifdef FEAT_CINDENT
7638 can_cindent = TRUE;
7639# endif
7640 }
7641}
7642
7643 void
7644ins_horscroll()
7645{
7646 pos_T tpos;
7647
7648 undisplay_dollar();
7649 tpos = curwin->w_cursor;
7650 if (gui_do_horiz_scroll())
7651 {
7652 start_arrow(&tpos);
7653# ifdef FEAT_CINDENT
7654 can_cindent = TRUE;
7655# endif
7656 }
7657}
7658#endif
7659
7660 static void
7661ins_left()
7662{
7663 pos_T tpos;
7664
7665#ifdef FEAT_FOLDING
7666 if ((fdo_flags & FDO_HOR) && KeyTyped)
7667 foldOpenCursor();
7668#endif
7669 undisplay_dollar();
7670 tpos = curwin->w_cursor;
7671 if (oneleft() == OK)
7672 {
7673 start_arrow(&tpos);
7674#ifdef FEAT_RIGHTLEFT
7675 /* If exit reversed string, position is fixed */
7676 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
7677 revins_legal++;
7678 revins_chars++;
7679#endif
7680 }
7681
7682 /*
7683 * if 'whichwrap' set for cursor in insert mode may go to
7684 * previous line
7685 */
7686 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
7687 {
7688 start_arrow(&tpos);
7689 --(curwin->w_cursor.lnum);
7690 coladvance((colnr_T)MAXCOL);
7691 curwin->w_set_curswant = TRUE; /* so we stay at the end */
7692 }
7693 else
7694 vim_beep();
7695}
7696
7697 static void
7698ins_home(c)
7699 int c;
7700{
7701 pos_T tpos;
7702
7703#ifdef FEAT_FOLDING
7704 if ((fdo_flags & FDO_HOR) && KeyTyped)
7705 foldOpenCursor();
7706#endif
7707 undisplay_dollar();
7708 tpos = curwin->w_cursor;
7709 if (c == K_C_HOME)
7710 curwin->w_cursor.lnum = 1;
7711 curwin->w_cursor.col = 0;
7712#ifdef FEAT_VIRTUALEDIT
7713 curwin->w_cursor.coladd = 0;
7714#endif
7715 curwin->w_curswant = 0;
7716 start_arrow(&tpos);
7717}
7718
7719 static void
7720ins_end(c)
7721 int c;
7722{
7723 pos_T tpos;
7724
7725#ifdef FEAT_FOLDING
7726 if ((fdo_flags & FDO_HOR) && KeyTyped)
7727 foldOpenCursor();
7728#endif
7729 undisplay_dollar();
7730 tpos = curwin->w_cursor;
7731 if (c == K_C_END)
7732 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7733 coladvance((colnr_T)MAXCOL);
7734 curwin->w_curswant = MAXCOL;
7735
7736 start_arrow(&tpos);
7737}
7738
7739 static void
7740ins_s_left()
7741{
7742#ifdef FEAT_FOLDING
7743 if ((fdo_flags & FDO_HOR) && KeyTyped)
7744 foldOpenCursor();
7745#endif
7746 undisplay_dollar();
7747 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
7748 {
7749 start_arrow(&curwin->w_cursor);
7750 (void)bck_word(1L, FALSE, FALSE);
7751 curwin->w_set_curswant = TRUE;
7752 }
7753 else
7754 vim_beep();
7755}
7756
7757 static void
7758ins_right()
7759{
7760#ifdef FEAT_FOLDING
7761 if ((fdo_flags & FDO_HOR) && KeyTyped)
7762 foldOpenCursor();
7763#endif
7764 undisplay_dollar();
7765 if (gchar_cursor() != NUL || virtual_active()
7766 )
7767 {
7768 start_arrow(&curwin->w_cursor);
7769 curwin->w_set_curswant = TRUE;
7770#ifdef FEAT_VIRTUALEDIT
7771 if (virtual_active())
7772 oneright();
7773 else
7774#endif
7775 {
7776#ifdef FEAT_MBYTE
7777 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007778 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 else
7780#endif
7781 ++curwin->w_cursor.col;
7782 }
7783
7784#ifdef FEAT_RIGHTLEFT
7785 revins_legal++;
7786 if (revins_chars)
7787 revins_chars--;
7788#endif
7789 }
7790 /* if 'whichwrap' set for cursor in insert mode, may move the
7791 * cursor to the next line */
7792 else if (vim_strchr(p_ww, ']') != NULL
7793 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7794 {
7795 start_arrow(&curwin->w_cursor);
7796 curwin->w_set_curswant = TRUE;
7797 ++curwin->w_cursor.lnum;
7798 curwin->w_cursor.col = 0;
7799 }
7800 else
7801 vim_beep();
7802}
7803
7804 static void
7805ins_s_right()
7806{
7807#ifdef FEAT_FOLDING
7808 if ((fdo_flags & FDO_HOR) && KeyTyped)
7809 foldOpenCursor();
7810#endif
7811 undisplay_dollar();
7812 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
7813 || gchar_cursor() != NUL)
7814 {
7815 start_arrow(&curwin->w_cursor);
7816 (void)fwd_word(1L, FALSE, 0);
7817 curwin->w_set_curswant = TRUE;
7818 }
7819 else
7820 vim_beep();
7821}
7822
7823 static void
7824ins_up(startcol)
7825 int startcol; /* when TRUE move to Insstart.col */
7826{
7827 pos_T tpos;
7828 linenr_T old_topline = curwin->w_topline;
7829#ifdef FEAT_DIFF
7830 int old_topfill = curwin->w_topfill;
7831#endif
7832
7833 undisplay_dollar();
7834 tpos = curwin->w_cursor;
7835 if (cursor_up(1L, TRUE) == OK)
7836 {
7837 if (startcol)
7838 coladvance(getvcol_nolist(&Insstart));
7839 if (old_topline != curwin->w_topline
7840#ifdef FEAT_DIFF
7841 || old_topfill != curwin->w_topfill
7842#endif
7843 )
7844 redraw_later(VALID);
7845 start_arrow(&tpos);
7846#ifdef FEAT_CINDENT
7847 can_cindent = TRUE;
7848#endif
7849 }
7850 else
7851 vim_beep();
7852}
7853
7854 static void
7855ins_pageup()
7856{
7857 pos_T tpos;
7858
7859 undisplay_dollar();
7860 tpos = curwin->w_cursor;
7861 if (onepage(BACKWARD, 1L) == OK)
7862 {
7863 start_arrow(&tpos);
7864#ifdef FEAT_CINDENT
7865 can_cindent = TRUE;
7866#endif
7867 }
7868 else
7869 vim_beep();
7870}
7871
7872 static void
7873ins_down(startcol)
7874 int startcol; /* when TRUE move to Insstart.col */
7875{
7876 pos_T tpos;
7877 linenr_T old_topline = curwin->w_topline;
7878#ifdef FEAT_DIFF
7879 int old_topfill = curwin->w_topfill;
7880#endif
7881
7882 undisplay_dollar();
7883 tpos = curwin->w_cursor;
7884 if (cursor_down(1L, TRUE) == OK)
7885 {
7886 if (startcol)
7887 coladvance(getvcol_nolist(&Insstart));
7888 if (old_topline != curwin->w_topline
7889#ifdef FEAT_DIFF
7890 || old_topfill != curwin->w_topfill
7891#endif
7892 )
7893 redraw_later(VALID);
7894 start_arrow(&tpos);
7895#ifdef FEAT_CINDENT
7896 can_cindent = TRUE;
7897#endif
7898 }
7899 else
7900 vim_beep();
7901}
7902
7903 static void
7904ins_pagedown()
7905{
7906 pos_T tpos;
7907
7908 undisplay_dollar();
7909 tpos = curwin->w_cursor;
7910 if (onepage(FORWARD, 1L) == OK)
7911 {
7912 start_arrow(&tpos);
7913#ifdef FEAT_CINDENT
7914 can_cindent = TRUE;
7915#endif
7916 }
7917 else
7918 vim_beep();
7919}
7920
7921#ifdef FEAT_DND
7922 static void
7923ins_drop()
7924{
7925 do_put('~', BACKWARD, 1L, PUT_CURSEND);
7926}
7927#endif
7928
7929/*
7930 * Handle TAB in Insert or Replace mode.
7931 * Return TRUE when the TAB needs to be inserted like a normal character.
7932 */
7933 static int
7934ins_tab()
7935{
7936 int ind;
7937 int i;
7938 int temp;
7939
7940 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
7941 Insstart_blank_vcol = get_nolist_virtcol();
7942 if (echeck_abbr(TAB + ABBR_OFF))
7943 return FALSE;
7944
7945 ind = inindent(0);
7946#ifdef FEAT_CINDENT
7947 if (ind)
7948 can_cindent = FALSE;
7949#endif
7950
7951 /*
7952 * When nothing special, insert TAB like a normal character
7953 */
7954 if (!curbuf->b_p_et
7955 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
7956 && curbuf->b_p_sts == 0)
7957 return TRUE;
7958
7959 if (stop_arrow() == FAIL)
7960 return TRUE;
7961
7962 did_ai = FALSE;
7963#ifdef FEAT_SMARTINDENT
7964 did_si = FALSE;
7965 can_si = FALSE;
7966 can_si_back = FALSE;
7967#endif
7968 AppendToRedobuff((char_u *)"\t");
7969
7970 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
7971 temp = (int)curbuf->b_p_sw;
7972 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
7973 temp = (int)curbuf->b_p_sts;
7974 else /* otherwise use 'tabstop' */
7975 temp = (int)curbuf->b_p_ts;
7976 temp -= get_nolist_virtcol() % temp;
7977
7978 /*
7979 * Insert the first space with ins_char(). It will delete one char in
7980 * replace mode. Insert the rest with ins_str(); it will not delete any
7981 * chars. For VREPLACE mode, we use ins_char() for all characters.
7982 */
7983 ins_char(' ');
7984 while (--temp > 0)
7985 {
7986#ifdef FEAT_VREPLACE
7987 if (State & VREPLACE_FLAG)
7988 ins_char(' ');
7989 else
7990#endif
7991 {
7992 ins_str((char_u *)" ");
7993 if (State & REPLACE_FLAG) /* no char replaced */
7994 replace_push(NUL);
7995 }
7996 }
7997
7998 /*
7999 * When 'expandtab' not set: Replace spaces by TABs where possible.
8000 */
8001 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8002 {
8003 char_u *ptr;
8004#ifdef FEAT_VREPLACE
8005 char_u *saved_line = NULL; /* init for GCC */
8006 pos_T pos;
8007#endif
8008 pos_T fpos;
8009 pos_T *cursor;
8010 colnr_T want_vcol, vcol;
8011 int change_col = -1;
8012 int save_list = curwin->w_p_list;
8013
8014 /*
8015 * Get the current line. For VREPLACE mode, don't make real changes
8016 * yet, just work on a copy of the line.
8017 */
8018#ifdef FEAT_VREPLACE
8019 if (State & VREPLACE_FLAG)
8020 {
8021 pos = curwin->w_cursor;
8022 cursor = &pos;
8023 saved_line = vim_strsave(ml_get_curline());
8024 if (saved_line == NULL)
8025 return FALSE;
8026 ptr = saved_line + pos.col;
8027 }
8028 else
8029#endif
8030 {
8031 ptr = ml_get_cursor();
8032 cursor = &curwin->w_cursor;
8033 }
8034
8035 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8036 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8037 curwin->w_p_list = FALSE;
8038
8039 /* Find first white before the cursor */
8040 fpos = curwin->w_cursor;
8041 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8042 {
8043 --fpos.col;
8044 --ptr;
8045 }
8046
8047 /* In Replace mode, don't change characters before the insert point. */
8048 if ((State & REPLACE_FLAG)
8049 && fpos.lnum == Insstart.lnum
8050 && fpos.col < Insstart.col)
8051 {
8052 ptr += Insstart.col - fpos.col;
8053 fpos.col = Insstart.col;
8054 }
8055
8056 /* compute virtual column numbers of first white and cursor */
8057 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8058 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8059
8060 /* Use as many TABs as possible. Beware of 'showbreak' and
8061 * 'linebreak' adding extra virtual columns. */
8062 while (vim_iswhite(*ptr))
8063 {
8064 i = lbr_chartabsize((char_u *)"\t", vcol);
8065 if (vcol + i > want_vcol)
8066 break;
8067 if (*ptr != TAB)
8068 {
8069 *ptr = TAB;
8070 if (change_col < 0)
8071 {
8072 change_col = fpos.col; /* Column of first change */
8073 /* May have to adjust Insstart */
8074 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8075 Insstart.col = fpos.col;
8076 }
8077 }
8078 ++fpos.col;
8079 ++ptr;
8080 vcol += i;
8081 }
8082
8083 if (change_col >= 0)
8084 {
8085 int repl_off = 0;
8086
8087 /* Skip over the spaces we need. */
8088 while (vcol < want_vcol && *ptr == ' ')
8089 {
8090 vcol += lbr_chartabsize(ptr, vcol);
8091 ++ptr;
8092 ++repl_off;
8093 }
8094 if (vcol > want_vcol)
8095 {
8096 /* Must have a char with 'showbreak' just before it. */
8097 --ptr;
8098 --repl_off;
8099 }
8100 fpos.col += repl_off;
8101
8102 /* Delete following spaces. */
8103 i = cursor->col - fpos.col;
8104 if (i > 0)
8105 {
8106 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8107 /* correct replace stack. */
8108 if ((State & REPLACE_FLAG)
8109#ifdef FEAT_VREPLACE
8110 && !(State & VREPLACE_FLAG)
8111#endif
8112 )
8113 for (temp = i; --temp >= 0; )
8114 replace_join(repl_off);
8115 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008116#ifdef FEAT_NETBEANS_INTG
8117 if (usingNetbeans)
8118 {
8119 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8120 (long)(i + 1));
8121 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8122 (char_u *)"\t", 1);
8123 }
8124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 cursor->col -= i;
8126
8127#ifdef FEAT_VREPLACE
8128 /*
8129 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8130 * backspacing over the changed spacing and then inserting the new
8131 * spacing.
8132 */
8133 if (State & VREPLACE_FLAG)
8134 {
8135 /* Backspace from real cursor to change_col */
8136 backspace_until_column(change_col);
8137
8138 /* Insert each char in saved_line from changed_col to
8139 * ptr-cursor */
8140 ins_bytes_len(saved_line + change_col,
8141 cursor->col - change_col);
8142 }
8143#endif
8144 }
8145
8146#ifdef FEAT_VREPLACE
8147 if (State & VREPLACE_FLAG)
8148 vim_free(saved_line);
8149#endif
8150 curwin->w_p_list = save_list;
8151 }
8152
8153 return FALSE;
8154}
8155
8156/*
8157 * Handle CR or NL in insert mode.
8158 * Return TRUE when out of memory or can't undo.
8159 */
8160 static int
8161ins_eol(c)
8162 int c;
8163{
8164 int i;
8165
8166 if (echeck_abbr(c + ABBR_OFF))
8167 return FALSE;
8168 if (stop_arrow() == FAIL)
8169 return TRUE;
8170 undisplay_dollar();
8171
8172 /*
8173 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8174 * character under the cursor. Only push a NUL on the replace stack,
8175 * nothing to put back when the NL is deleted.
8176 */
8177 if ((State & REPLACE_FLAG)
8178#ifdef FEAT_VREPLACE
8179 && !(State & VREPLACE_FLAG)
8180#endif
8181 )
8182 replace_push(NUL);
8183
8184 /*
8185 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8186 * replacing the next line, so we push all of the characters left on the
8187 * line onto the replace stack. This is not done here though, it is done
8188 * in open_line().
8189 */
8190
8191#ifdef FEAT_RIGHTLEFT
8192# ifdef FEAT_FKMAP
8193 if (p_altkeymap && p_fkmap)
8194 fkmap(NL);
8195# endif
8196 /* NL in reverse insert will always start in the end of
8197 * current line. */
8198 if (revins_on)
8199 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8200#endif
8201
8202 AppendToRedobuff(NL_STR);
8203 i = open_line(FORWARD,
8204#ifdef FEAT_COMMENTS
8205 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8206#endif
8207 0, old_indent);
8208 old_indent = 0;
8209#ifdef FEAT_CINDENT
8210 can_cindent = TRUE;
8211#endif
8212
8213 return (!i);
8214}
8215
8216#ifdef FEAT_DIGRAPHS
8217/*
8218 * Handle digraph in insert mode.
8219 * Returns character still to be inserted, or NUL when nothing remaining to be
8220 * done.
8221 */
8222 static int
8223ins_digraph()
8224{
8225 int c;
8226 int cc;
8227
8228 pc_status = PC_STATUS_UNSET;
8229 if (redrawing() && !char_avail())
8230 {
8231 /* may need to redraw when no more chars available now */
8232 ins_redraw();
8233
8234 edit_putchar('?', TRUE);
8235#ifdef FEAT_CMDL_INFO
8236 add_to_showcmd_c(Ctrl_K);
8237#endif
8238 }
8239
8240#ifdef USE_ON_FLY_SCROLL
8241 dont_scroll = TRUE; /* disallow scrolling here */
8242#endif
8243
8244 /* don't map the digraph chars. This also prevents the
8245 * mode message to be deleted when ESC is hit */
8246 ++no_mapping;
8247 ++allow_keys;
8248 c = safe_vgetc();
8249 --no_mapping;
8250 --allow_keys;
8251 if (IS_SPECIAL(c) || mod_mask) /* special key */
8252 {
8253#ifdef FEAT_CMDL_INFO
8254 clear_showcmd();
8255#endif
8256 insert_special(c, TRUE, FALSE);
8257 return NUL;
8258 }
8259 if (c != ESC)
8260 {
8261 if (redrawing() && !char_avail())
8262 {
8263 /* may need to redraw when no more chars available now */
8264 ins_redraw();
8265
8266 if (char2cells(c) == 1)
8267 {
8268 /* first remove the '?', otherwise it's restored when typing
8269 * an ESC next */
8270 edit_unputchar();
8271 ins_redraw();
8272 edit_putchar(c, TRUE);
8273 }
8274#ifdef FEAT_CMDL_INFO
8275 add_to_showcmd_c(c);
8276#endif
8277 }
8278 ++no_mapping;
8279 ++allow_keys;
8280 cc = safe_vgetc();
8281 --no_mapping;
8282 --allow_keys;
8283 if (cc != ESC)
8284 {
8285 AppendToRedobuff((char_u *)CTRL_V_STR);
8286 c = getdigraph(c, cc, TRUE);
8287#ifdef FEAT_CMDL_INFO
8288 clear_showcmd();
8289#endif
8290 return c;
8291 }
8292 }
8293 edit_unputchar();
8294#ifdef FEAT_CMDL_INFO
8295 clear_showcmd();
8296#endif
8297 return NUL;
8298}
8299#endif
8300
8301/*
8302 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8303 * Returns the char to be inserted, or NUL if none found.
8304 */
8305 static int
8306ins_copychar(lnum)
8307 linenr_T lnum;
8308{
8309 int c;
8310 int temp;
8311 char_u *ptr, *prev_ptr;
8312
8313 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8314 {
8315 vim_beep();
8316 return NUL;
8317 }
8318
8319 /* try to advance to the cursor column */
8320 temp = 0;
8321 ptr = ml_get(lnum);
8322 prev_ptr = ptr;
8323 validate_virtcol();
8324 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8325 {
8326 prev_ptr = ptr;
8327 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8328 }
8329 if ((colnr_T)temp > curwin->w_virtcol)
8330 ptr = prev_ptr;
8331
8332#ifdef FEAT_MBYTE
8333 c = (*mb_ptr2char)(ptr);
8334#else
8335 c = *ptr;
8336#endif
8337 if (c == NUL)
8338 vim_beep();
8339 return c;
8340}
8341
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008342/*
8343 * CTRL-Y or CTRL-E typed in Insert mode.
8344 */
8345 static int
8346ins_ctrl_ey(tc)
8347 int tc;
8348{
8349 int c = tc;
8350
8351#ifdef FEAT_INS_EXPAND
8352 if (ctrl_x_mode == CTRL_X_SCROLL)
8353 {
8354 if (c == Ctrl_Y)
8355 scrolldown_clamp();
8356 else
8357 scrollup_clamp();
8358 redraw_later(VALID);
8359 }
8360 else
8361#endif
8362 {
8363 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8364 if (c != NUL)
8365 {
8366 long tw_save;
8367
8368 /* The character must be taken literally, insert like it
8369 * was typed after a CTRL-V, and pretend 'textwidth'
8370 * wasn't set. Digits, 'o' and 'x' are special after a
8371 * CTRL-V, don't use it for these. */
8372 if (c < 256 && !isalnum(c))
8373 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8374 tw_save = curbuf->b_p_tw;
8375 curbuf->b_p_tw = -1;
8376 insert_special(c, TRUE, FALSE);
8377 curbuf->b_p_tw = tw_save;
8378#ifdef FEAT_RIGHTLEFT
8379 revins_chars++;
8380 revins_legal++;
8381#endif
8382 c = Ctrl_V; /* pretend CTRL-V is last character */
8383 auto_format(FALSE, TRUE);
8384 }
8385 }
8386 return c;
8387}
8388
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389#ifdef FEAT_SMARTINDENT
8390/*
8391 * Try to do some very smart auto-indenting.
8392 * Used when inserting a "normal" character.
8393 */
8394 static void
8395ins_try_si(c)
8396 int c;
8397{
8398 pos_T *pos, old_pos;
8399 char_u *ptr;
8400 int i;
8401 int temp;
8402
8403 /*
8404 * do some very smart indenting when entering '{' or '}'
8405 */
8406 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8407 {
8408 /*
8409 * for '}' set indent equal to indent of line containing matching '{'
8410 */
8411 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8412 {
8413 old_pos = curwin->w_cursor;
8414 /*
8415 * If the matching '{' has a ')' immediately before it (ignoring
8416 * white-space), then line up with the start of the line
8417 * containing the matching '(' if there is one. This handles the
8418 * case where an "if (..\n..) {" statement continues over multiple
8419 * lines -- webb
8420 */
8421 ptr = ml_get(pos->lnum);
8422 i = pos->col;
8423 if (i > 0) /* skip blanks before '{' */
8424 while (--i > 0 && vim_iswhite(ptr[i]))
8425 ;
8426 curwin->w_cursor.lnum = pos->lnum;
8427 curwin->w_cursor.col = i;
8428 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8429 curwin->w_cursor = *pos;
8430 i = get_indent();
8431 curwin->w_cursor = old_pos;
8432#ifdef FEAT_VREPLACE
8433 if (State & VREPLACE_FLAG)
8434 change_indent(INDENT_SET, i, FALSE, NUL);
8435 else
8436#endif
8437 (void)set_indent(i, SIN_CHANGED);
8438 }
8439 else if (curwin->w_cursor.col > 0)
8440 {
8441 /*
8442 * when inserting '{' after "O" reduce indent, but not
8443 * more than indent of previous line
8444 */
8445 temp = TRUE;
8446 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8447 {
8448 old_pos = curwin->w_cursor;
8449 i = get_indent();
8450 while (curwin->w_cursor.lnum > 1)
8451 {
8452 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8453
8454 /* ignore empty lines and lines starting with '#'. */
8455 if (*ptr != '#' && *ptr != NUL)
8456 break;
8457 }
8458 if (get_indent() >= i)
8459 temp = FALSE;
8460 curwin->w_cursor = old_pos;
8461 }
8462 if (temp)
8463 shift_line(TRUE, FALSE, 1);
8464 }
8465 }
8466
8467 /*
8468 * set indent of '#' always to 0
8469 */
8470 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8471 {
8472 /* remember current indent for next line */
8473 old_indent = get_indent();
8474 (void)set_indent(0, SIN_CHANGED);
8475 }
8476
8477 /* Adjust ai_col, the char at this position can be deleted. */
8478 if (ai_col > curwin->w_cursor.col)
8479 ai_col = curwin->w_cursor.col;
8480}
8481#endif
8482
8483/*
8484 * Get the value that w_virtcol would have when 'list' is off.
8485 * Unless 'cpo' contains the 'L' flag.
8486 */
8487 static colnr_T
8488get_nolist_virtcol()
8489{
8490 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8491 return getvcol_nolist(&curwin->w_cursor);
8492 validate_virtcol();
8493 return curwin->w_virtcol;
8494}