blob: e331c4c5a9617b8e865c94a7f42bf64b9587f9b7 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
39
40static char *ctrl_x_msgs[] =
41{
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar488c6512005-08-11 20:09:58 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^P^S^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000044 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 N_(" Whole line completion (^L^N^P)"),
46 N_(" File name completion (^F^N^P)"),
47 N_(" Tag completion (^]^N^P)"),
48 N_(" Path pattern completion (^N^P)"),
49 N_(" Definition completion (^D^N^P)"),
50 NULL,
51 N_(" Dictionary completion (^K^N^P)"),
52 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000053 N_(" Command-line completion (^V^N^P)"),
54 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000055 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar488c6512005-08-11 20:09:58 +000056 N_(" Spelling suggestion (^S^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000057 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +000058};
59
60static char_u e_hitend[] = N_("Hit end of paragraph");
61
62/*
63 * Structure used to store one match for insert completion.
64 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000065typedef struct Completion compl_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000066struct Completion
67{
Bram Moolenaar572cb562005-08-05 21:35:02 +000068 compl_T *cp_next;
69 compl_T *cp_prev;
70 char_u *cp_str; /* matched text */
71 char_u *cp_fname; /* file containing the match */
72 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
73 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000074};
75
Bram Moolenaar572cb562005-08-05 21:35:02 +000076#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077#define FREE_FNAME (2)
78
79/*
80 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000081 * "compl_first_match" points to the start of the list.
82 * "compl_curr_match" points to the currently selected entry.
83 * "compl_shown_match" is different from compl_curr_match during
84 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000086static compl_T *compl_first_match = NULL;
87static compl_T *compl_curr_match = NULL;
88static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000089
Bram Moolenaar4be06f92005-07-29 22:36:03 +000090/* When the first completion is done "compl_started" is set. When it's
91 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +000092static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000093
Bram Moolenaar572cb562005-08-05 21:35:02 +000094static int compl_matches = 0;
95static char_u *compl_pattern = NULL;
96static int compl_direction = FORWARD;
97static int compl_shows_dir = FORWARD;
98static int compl_pending = FALSE;
99static pos_T compl_startpos;
100static colnr_T compl_col = 0; /* column where the text starts
101 * that is being completed */
102static int save_sm = -1;
103static char_u *compl_orig_text = NULL; /* text as it was before
104 * completion started */
105static int compl_cont_mode = 0;
106static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000108static void ins_ctrl_x __ARGS((void));
109static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int dir));
111static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000112static void ins_compl_upd_pum __ARGS((void));
113static void ins_compl_del_pum __ARGS((void));
114static int pum_wanted __ARGS((void));
115static void ins_compl_show_pum __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int dir, int flags, int thesaurus));
117static void ins_compl_free __ARGS((void));
118static void ins_compl_clear __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000119static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
121static int ins_compl_get_exp __ARGS((pos_T *ini, int dir));
122static void ins_compl_delete __ARGS((void));
123static void ins_compl_insert __ARGS((void));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000124static int ins_compl_next __ARGS((int allow_get_expansion, int count));
125static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000126static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000127static int ins_compl_key2count __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128static int ins_complete __ARGS((int c));
129static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
130#endif /* FEAT_INS_EXPAND */
131
132#define BACKSPACE_CHAR 1
133#define BACKSPACE_WORD 2
134#define BACKSPACE_WORD_NOT_SPACE 3
135#define BACKSPACE_LINE 4
136
137static void ins_redraw __ARGS((void));
138static void ins_ctrl_v __ARGS((void));
139static void undisplay_dollar __ARGS((void));
140static void insert_special __ARGS((int, int, int));
141static void check_auto_format __ARGS((int));
142static void redo_literal __ARGS((int c));
143static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000144#ifdef FEAT_SYN_HL
145static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000146static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000147static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
150static int echeck_abbr __ARGS((int));
151static void replace_push_off __ARGS((int c));
152static int replace_pop __ARGS((void));
153static void replace_join __ARGS((int off));
154static void replace_pop_ins __ARGS((void));
155#ifdef FEAT_MBYTE
156static void mb_replace_pop_ins __ARGS((int cc));
157#endif
158static void replace_flush __ARGS((void));
159static void replace_do_bs __ARGS((void));
160#ifdef FEAT_CINDENT
161static int cindent_on __ARGS((void));
162#endif
163static void ins_reg __ARGS((void));
164static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000165static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000166static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#ifdef FEAT_RIGHTLEFT
168static void ins_ctrl_ __ARGS((void));
169#endif
170#ifdef FEAT_VISUAL
171static int ins_start_select __ARGS((int c));
172#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000173static void ins_insert __ARGS((int replaceState));
174static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static void ins_shift __ARGS((int c, int lastc));
176static void ins_del __ARGS((void));
177static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
178#ifdef FEAT_MOUSE
179static void ins_mouse __ARGS((int c));
180static void ins_mousescroll __ARGS((int up));
181#endif
182static void ins_left __ARGS((void));
183static void ins_home __ARGS((int c));
184static void ins_end __ARGS((int c));
185static void ins_s_left __ARGS((void));
186static void ins_right __ARGS((void));
187static void ins_s_right __ARGS((void));
188static void ins_up __ARGS((int startcol));
189static void ins_pageup __ARGS((void));
190static void ins_down __ARGS((int startcol));
191static void ins_pagedown __ARGS((void));
192#ifdef FEAT_DND
193static void ins_drop __ARGS((void));
194#endif
195static int ins_tab __ARGS((void));
196static int ins_eol __ARGS((int c));
197#ifdef FEAT_DIGRAPHS
198static int ins_digraph __ARGS((void));
199#endif
200static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000201static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#ifdef FEAT_SMARTINDENT
203static void ins_try_si __ARGS((int c));
204#endif
205static colnr_T get_nolist_virtcol __ARGS((void));
206
207static colnr_T Insstart_textlen; /* length of line when insert started */
208static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
209
210static char_u *last_insert = NULL; /* the text of the previous insert,
211 K_SPECIAL and CSI are escaped */
212static int last_insert_skip; /* nr of chars in front of previous insert */
213static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000214static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215
216#ifdef FEAT_CINDENT
217static int can_cindent; /* may do cindenting on this line */
218#endif
219
220static int old_indent = 0; /* for ^^D command in insert mode */
221
222#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000223static int revins_on; /* reverse insert mode on */
224static int revins_chars; /* how much to skip after edit */
225static int revins_legal; /* was the last char 'legal'? */
226static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#endif
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229static int ins_need_undo; /* call u_save() before inserting a
230 char. Set when edit() is called.
231 after that arrow_used is used. */
232
233static int did_add_space = FALSE; /* auto_format() added an extra space
234 under the cursor */
235
236/*
237 * edit(): Start inserting text.
238 *
239 * "cmdchar" can be:
240 * 'i' normal insert command
241 * 'a' normal append command
242 * 'R' replace command
243 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
244 * but still only one <CR> is inserted. The <Esc> is not used for redo.
245 * 'g' "gI" command.
246 * 'V' "gR" command for Virtual Replace mode.
247 * 'v' "gr" command for single character Virtual Replace mode.
248 *
249 * This function is not called recursively. For CTRL-O commands, it returns
250 * and lets the caller handle the Normal-mode command.
251 *
252 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
253 */
254 int
255edit(cmdchar, startln, count)
256 int cmdchar;
257 int startln; /* if set, insert at start of line */
258 long count;
259{
260 int c = 0;
261 char_u *ptr;
262 int lastc;
263 colnr_T mincol;
264 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 int i;
266 int did_backspace = TRUE; /* previous char was backspace */
267#ifdef FEAT_CINDENT
268 int line_is_white = FALSE; /* line is empty before insert */
269#endif
270 linenr_T old_topline = 0; /* topline before insertion */
271#ifdef FEAT_DIFF
272 int old_topfill = -1;
273#endif
274 int inserted_space = FALSE; /* just inserted a space */
275 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000276 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000278 /* Remember whether editing was restarted after CTRL-O. */
279 did_restart_edit = restart_edit;
280
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 /* sleep before redrawing, needed for "CTRL-O :" that results in an
282 * error message */
283 check_for_delay(TRUE);
284
285#ifdef HAVE_SANDBOX
286 /* Don't allow inserting in the sandbox. */
287 if (sandbox != 0)
288 {
289 EMSG(_(e_sandbox));
290 return FALSE;
291 }
292#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000293 /* Don't allow changes in the buffer while editing the cmdline. The
294 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000295 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000296 {
297 EMSG(_(e_secure));
298 return FALSE;
299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300
301#ifdef FEAT_INS_EXPAND
302 ins_compl_clear(); /* clear stuff for CTRL-X mode */
303#endif
304
Bram Moolenaar843ee412004-06-30 16:16:41 +0000305#ifdef FEAT_AUTOCMD
306 /*
307 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
308 */
309 if (cmdchar != 'r' && cmdchar != 'v')
310 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000311# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000312 if (cmdchar == 'R')
313 ptr = (char_u *)"r";
314 else if (cmdchar == 'V')
315 ptr = (char_u *)"v";
316 else
317 ptr = (char_u *)"i";
318 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000319# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000320 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
321 }
322#endif
323
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324#ifdef FEAT_MOUSE
325 /*
326 * When doing a paste with the middle mouse button, Insstart is set to
327 * where the paste started.
328 */
329 if (where_paste_started.lnum != 0)
330 Insstart = where_paste_started;
331 else
332#endif
333 {
334 Insstart = curwin->w_cursor;
335 if (startln)
336 Insstart.col = 0;
337 }
338 Insstart_textlen = linetabsize(ml_get_curline());
339 Insstart_blank_vcol = MAXCOL;
340 if (!did_ai)
341 ai_col = 0;
342
343 if (cmdchar != NUL && restart_edit == 0)
344 {
345 ResetRedobuff();
346 AppendNumberToRedobuff(count);
347#ifdef FEAT_VREPLACE
348 if (cmdchar == 'V' || cmdchar == 'v')
349 {
350 /* "gR" or "gr" command */
351 AppendCharToRedobuff('g');
352 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
353 }
354 else
355#endif
356 {
357 AppendCharToRedobuff(cmdchar);
358 if (cmdchar == 'g') /* "gI" command */
359 AppendCharToRedobuff('I');
360 else if (cmdchar == 'r') /* "r<CR>" command */
361 count = 1; /* insert only one <CR> */
362 }
363 }
364
365 if (cmdchar == 'R')
366 {
367#ifdef FEAT_FKMAP
368 if (p_fkmap && p_ri)
369 {
370 beep_flush();
371 EMSG(farsi_text_3); /* encoded in Farsi */
372 State = INSERT;
373 }
374 else
375#endif
376 State = REPLACE;
377 }
378#ifdef FEAT_VREPLACE
379 else if (cmdchar == 'V' || cmdchar == 'v')
380 {
381 State = VREPLACE;
382 replaceState = VREPLACE;
383 orig_line_count = curbuf->b_ml.ml_line_count;
384 vr_lines_changed = 1;
385 }
386#endif
387 else
388 State = INSERT;
389
390 stop_insert_mode = FALSE;
391
392 /*
393 * Need to recompute the cursor position, it might move when the cursor is
394 * on a TAB or special character.
395 */
396 curs_columns(TRUE);
397
398 /*
399 * Enable langmap or IME, indicated by 'iminsert'.
400 * Note that IME may enabled/disabled without us noticing here, thus the
401 * 'iminsert' value may not reflect what is actually used. It is updated
402 * when hitting <Esc>.
403 */
404 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
405 State |= LANGMAP;
406#ifdef USE_IM_CONTROL
407 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
408#endif
409
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#ifdef FEAT_MOUSE
411 setmouse();
412#endif
413#ifdef FEAT_CMDL_INFO
414 clear_showcmd();
415#endif
416#ifdef FEAT_RIGHTLEFT
417 /* there is no reverse replace mode */
418 revins_on = (State == INSERT && p_ri);
419 if (revins_on)
420 undisplay_dollar();
421 revins_chars = 0;
422 revins_legal = 0;
423 revins_scol = -1;
424#endif
425
426 /*
427 * Handle restarting Insert mode.
428 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
429 * restart_edit non-zero, and something in the stuff buffer.
430 */
431 if (restart_edit != 0 && stuff_empty())
432 {
433#ifdef FEAT_MOUSE
434 /*
435 * After a paste we consider text typed to be part of the insert for
436 * the pasted text. You can backspace over the pasted text too.
437 */
438 if (where_paste_started.lnum)
439 arrow_used = FALSE;
440 else
441#endif
442 arrow_used = TRUE;
443 restart_edit = 0;
444
445 /*
446 * If the cursor was after the end-of-line before the CTRL-O and it is
447 * now at the end-of-line, put it after the end-of-line (this is not
448 * correct in very rare cases).
449 * Also do this if curswant is greater than the current virtual
450 * column. Eg after "^O$" or "^O80|".
451 */
452 validate_virtcol();
453 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000454 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 || curwin->w_curswant > curwin->w_virtcol)
456 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
457 {
458 if (ptr[1] == NUL)
459 ++curwin->w_cursor.col;
460#ifdef FEAT_MBYTE
461 else if (has_mbyte)
462 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000463 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 if (ptr[i] == NUL)
465 curwin->w_cursor.col += i;
466 }
467#endif
468 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000469 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 }
471 else
472 arrow_used = FALSE;
473
474 /* we are in insert mode now, don't need to start it anymore */
475 need_start_insertmode = FALSE;
476
477 /* Need to save the line for undo before inserting the first char. */
478 ins_need_undo = TRUE;
479
480#ifdef FEAT_MOUSE
481 where_paste_started.lnum = 0;
482#endif
483#ifdef FEAT_CINDENT
484 can_cindent = TRUE;
485#endif
486#ifdef FEAT_FOLDING
487 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
488 * restarting. */
489 if (!p_im && did_restart_edit == 0)
490 foldOpenCursor();
491#endif
492
493 /*
494 * If 'showmode' is set, show the current (insert/replace/..) mode.
495 * A warning message for changing a readonly file is given here, before
496 * actually changing anything. It's put after the mode, if any.
497 */
498 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000499 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 i = showmode();
501
502 if (!p_im && did_restart_edit == 0)
503 change_warning(i + 1);
504
505#ifdef CURSOR_SHAPE
506 ui_cursor_shape(); /* may show different cursor shape */
507#endif
508#ifdef FEAT_DIGRAPHS
509 do_digraph(-1); /* clear digraphs */
510#endif
511
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000512 /*
513 * Get the current length of the redo buffer, those characters have to be
514 * skipped if we want to get to the inserted characters.
515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 ptr = get_inserted();
517 if (ptr == NULL)
518 new_insert_skip = 0;
519 else
520 {
521 new_insert_skip = (int)STRLEN(ptr);
522 vim_free(ptr);
523 }
524
525 old_indent = 0;
526
527 /*
528 * Main loop in Insert mode: repeat until Insert mode is left.
529 */
530 for (;;)
531 {
532#ifdef FEAT_RIGHTLEFT
533 if (!revins_legal)
534 revins_scol = -1; /* reset on illegal motions */
535 else
536 revins_legal = 0;
537#endif
538 if (arrow_used) /* don't repeat insert when arrow key used */
539 count = 0;
540
541 if (stop_insert_mode)
542 {
543 /* ":stopinsert" used or 'insertmode' reset */
544 count = 0;
545 goto doESCkey;
546 }
547
548 /* set curwin->w_curswant for next K_DOWN or K_UP */
549 if (!arrow_used)
550 curwin->w_set_curswant = TRUE;
551
552 /* If there is no typeahead may check for timestamps (e.g., for when a
553 * menu invoked a shell command). */
554 if (stuff_empty())
555 {
556 did_check_timestamps = FALSE;
557 if (need_check_timestamps)
558 check_timestamps(FALSE);
559 }
560
561 /*
562 * When emsg() was called msg_scroll will have been set.
563 */
564 msg_scroll = FALSE;
565
566#ifdef FEAT_GUI
567 /* When 'mousefocus' is set a mouse movement may have taken us to
568 * another window. "need_mouse_correct" may then be set because of an
569 * autocommand. */
570 if (need_mouse_correct)
571 gui_mouse_correct();
572#endif
573
574#ifdef FEAT_FOLDING
575 /* Open fold at the cursor line, according to 'foldopen'. */
576 if (fdo_flags & FDO_INSERT)
577 foldOpenCursor();
578 /* Close folds where the cursor isn't, according to 'foldclose' */
579 if (!char_avail())
580 foldCheckClose();
581#endif
582
583 /*
584 * If we inserted a character at the last position of the last line in
585 * the window, scroll the window one line up. This avoids an extra
586 * redraw.
587 * This is detected when the cursor column is smaller after inserting
588 * something.
589 * Don't do this when the topline changed already, it has
590 * already been adjusted (by insertchar() calling open_line())).
591 */
592 if (curbuf->b_mod_set
593 && curwin->w_p_wrap
594 && !did_backspace
595 && curwin->w_topline == old_topline
596#ifdef FEAT_DIFF
597 && curwin->w_topfill == old_topfill
598#endif
599 )
600 {
601 mincol = curwin->w_wcol;
602 validate_cursor_col();
603
604 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
605 && curwin->w_wrow == W_WINROW(curwin)
606 + curwin->w_height - 1 - p_so
607 && (curwin->w_cursor.lnum != curwin->w_topline
608#ifdef FEAT_DIFF
609 || curwin->w_topfill > 0
610#endif
611 ))
612 {
613#ifdef FEAT_DIFF
614 if (curwin->w_topfill > 0)
615 --curwin->w_topfill;
616 else
617#endif
618#ifdef FEAT_FOLDING
619 if (hasFolding(curwin->w_topline, NULL, &old_topline))
620 set_topline(curwin, old_topline + 1);
621 else
622#endif
623 set_topline(curwin, curwin->w_topline + 1);
624 }
625 }
626
627 /* May need to adjust w_topline to show the cursor. */
628 update_topline();
629
630 did_backspace = FALSE;
631
632 validate_cursor(); /* may set must_redraw */
633
634 /*
635 * Redraw the display when no characters are waiting.
636 * Also shows mode, ruler and positions cursor.
637 */
638 ins_redraw();
639
640#ifdef FEAT_SCROLLBIND
641 if (curwin->w_p_scb)
642 do_check_scrollbind(TRUE);
643#endif
644
645 update_curswant();
646 old_topline = curwin->w_topline;
647#ifdef FEAT_DIFF
648 old_topfill = curwin->w_topfill;
649#endif
650
651#ifdef USE_ON_FLY_SCROLL
652 dont_scroll = FALSE; /* allow scrolling here */
653#endif
654
655 /*
656 * Get a character for Insert mode.
657 */
658 lastc = c; /* remember previous char for CTRL-D */
659 c = safe_vgetc();
660
661#ifdef FEAT_RIGHTLEFT
662 if (p_hkmap && KeyTyped)
663 c = hkmap(c); /* Hebrew mode mapping */
664#endif
665#ifdef FEAT_FKMAP
666 if (p_fkmap && KeyTyped)
667 c = fkmap(c); /* Farsi mode mapping */
668#endif
669
670#ifdef FEAT_INS_EXPAND
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000671 /* When the popup menu is visible cursor keys change the selection. */
672 if (c == K_UP && pum_visible())
673 c = Ctrl_P;
674 if (c == K_DOWN && pum_visible())
675 c = Ctrl_N;
676
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
678 * it does fix up the text when finishing completion. */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000679 if (c != K_IGNORE)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000680 {
681 if (ins_compl_prep(c))
682 continue;
683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684#endif
685
Bram Moolenaar488c6512005-08-11 20:09:58 +0000686 /* CTRL-\ CTRL-N goes to Normal mode,
687 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
688 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 if (c == Ctrl_BSL)
690 {
691 /* may need to redraw when no more chars available now */
692 ins_redraw();
693 ++no_mapping;
694 ++allow_keys;
695 c = safe_vgetc();
696 --no_mapping;
697 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000698 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000700 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 vungetc(c);
702 c = Ctrl_BSL;
703 }
704 else if (c == Ctrl_G && p_im)
705 continue;
706 else
707 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000708 if (c == Ctrl_O)
709 {
710 ins_ctrl_o();
711 ins_at_eol = FALSE; /* cursor keeps its column */
712 nomove = TRUE;
713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 count = 0;
715 goto doESCkey;
716 }
717 }
718
719#ifdef FEAT_DIGRAPHS
720 c = do_digraph(c);
721#endif
722
723#ifdef FEAT_INS_EXPAND
724 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
725 goto docomplete;
726#endif
727 if (c == Ctrl_V || c == Ctrl_Q)
728 {
729 ins_ctrl_v();
730 c = Ctrl_V; /* pretend CTRL-V is last typed character */
731 continue;
732 }
733
734#ifdef FEAT_CINDENT
735 if (cindent_on()
736# ifdef FEAT_INS_EXPAND
737 && ctrl_x_mode == 0
738# endif
739 )
740 {
741 /* A key name preceded by a bang means this key is not to be
742 * inserted. Skip ahead to the re-indenting below.
743 * A key name preceded by a star means that indenting has to be
744 * done before inserting the key. */
745 line_is_white = inindent(0);
746 if (in_cinkeys(c, '!', line_is_white))
747 goto force_cindent;
748 if (can_cindent && in_cinkeys(c, '*', line_is_white)
749 && stop_arrow() == OK)
750 do_c_expr_indent();
751 }
752#endif
753
754#ifdef FEAT_RIGHTLEFT
755 if (curwin->w_p_rl)
756 switch (c)
757 {
758 case K_LEFT: c = K_RIGHT; break;
759 case K_S_LEFT: c = K_S_RIGHT; break;
760 case K_C_LEFT: c = K_C_RIGHT; break;
761 case K_RIGHT: c = K_LEFT; break;
762 case K_S_RIGHT: c = K_S_LEFT; break;
763 case K_C_RIGHT: c = K_C_LEFT; break;
764 }
765#endif
766
767#ifdef FEAT_VISUAL
768 /*
769 * If 'keymodel' contains "startsel", may start selection. If it
770 * does, a CTRL-O and c will be stuffed, we need to get these
771 * characters.
772 */
773 if (ins_start_select(c))
774 continue;
775#endif
776
777 /*
778 * The big switch to handle a character in insert mode.
779 */
780 switch (c)
781 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000782 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 if (echeck_abbr(ESC + ABBR_OFF))
784 break;
785 /*FALLTHROUGH*/
786
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000787 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#ifdef FEAT_CMDWIN
789 if (c == Ctrl_C && cmdwin_type != 0)
790 {
791 /* Close the cmdline window. */
792 cmdwin_result = K_IGNORE;
793 got_int = FALSE; /* don't stop executing autocommands et al. */
794 goto doESCkey;
795 }
796#endif
797
798#ifdef UNIX
799do_intr:
800#endif
801 /* when 'insertmode' set, and not halfway a mapping, don't leave
802 * Insert mode */
803 if (goto_im())
804 {
805 if (got_int)
806 {
807 (void)vgetc(); /* flush all buffers */
808 got_int = FALSE;
809 }
810 else
811 vim_beep();
812 break;
813 }
814doESCkey:
815 /*
816 * This is the ONLY return from edit()!
817 */
818 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
819 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000820 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 o_lnum = curwin->w_cursor.lnum;
822
Bram Moolenaar488c6512005-08-11 20:09:58 +0000823 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000824 {
825#ifdef FEAT_AUTOCMD
826 if (cmdchar != 'r' && cmdchar != 'v')
827 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
828 FALSE, curbuf);
829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 continue;
833
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000834 case Ctrl_Z: /* suspend when 'insertmode' set */
835 if (!p_im)
836 goto normalchar; /* insert CTRL-Z as normal char */
837 stuffReadbuff((char_u *)":st\r");
838 c = Ctrl_O;
839 /*FALLTHROUGH*/
840
841 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000842#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000843 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000844 goto docomplete;
845#endif
846 if (echeck_abbr(Ctrl_O + ABBR_OFF))
847 break;
848 ins_ctrl_o();
849 count = 0;
850 goto doESCkey;
851
Bram Moolenaar572cb562005-08-05 21:35:02 +0000852 case K_INS: /* toggle insert/replace mode */
853 case K_KINS:
854 ins_insert(replaceState);
855 break;
856
857 case K_SELECT: /* end of Select mode mapping - ignore */
858 break;
859
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000860#ifdef FEAT_SNIFF
861 case K_SNIFF: /* Sniff command received */
862 stuffcharReadbuff(K_SNIFF);
863 goto doESCkey;
864#endif
865
866 case K_HELP: /* Help key works like <ESC> <Help> */
867 case K_F1:
868 case K_XF1:
869 stuffcharReadbuff(K_HELP);
870 if (p_im)
871 need_start_insertmode = TRUE;
872 goto doESCkey;
873
874#ifdef FEAT_NETBEANS_INTG
875 case K_F21: /* NetBeans command */
876 ++no_mapping; /* don't map the next key hits */
877 i = safe_vgetc();
878 --no_mapping;
879 netbeans_keycommand(i);
880 break;
881#endif
882
883 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 case NUL:
885 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000886 /* For ^@ the trailing ESC will end the insert, unless there is an
887 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
889 && c != Ctrl_A && !p_im)
890 goto doESCkey; /* quit insert mode */
891 inserted_space = FALSE;
892 break;
893
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000894 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 ins_reg();
896 auto_format(FALSE, TRUE);
897 inserted_space = FALSE;
898 break;
899
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000900 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 ins_ctrl_g();
902 break;
903
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000904 case Ctrl_HAT: /* switch input mode and/or langmap */
905 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 break;
907
908#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000909 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 if (!p_ari)
911 goto normalchar;
912 ins_ctrl_();
913 break;
914#endif
915
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000916 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
918 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
919 goto docomplete;
920#endif
921 /* FALLTHROUGH */
922
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000923 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924# ifdef FEAT_INS_EXPAND
925 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
926 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000927 if (has_compl_option(FALSE))
928 goto docomplete;
929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 }
931# endif
932 ins_shift(c, lastc);
933 auto_format(FALSE, TRUE);
934 inserted_space = FALSE;
935 break;
936
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000937 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 case K_KDEL:
939 ins_del();
940 auto_format(FALSE, TRUE);
941 break;
942
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000943 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 case Ctrl_H:
945 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
946 auto_format(FALSE, TRUE);
947 break;
948
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000949 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
951 auto_format(FALSE, TRUE);
952 break;
953
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000954 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000955# ifdef FEAT_COMPL_FUNC
956 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000957 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000958 goto docomplete;
959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
961 auto_format(FALSE, TRUE);
962 inserted_space = FALSE;
963 break;
964
965#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000966 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 case K_LEFTMOUSE_NM:
968 case K_LEFTDRAG:
969 case K_LEFTRELEASE:
970 case K_LEFTRELEASE_NM:
971 case K_MIDDLEMOUSE:
972 case K_MIDDLEDRAG:
973 case K_MIDDLERELEASE:
974 case K_RIGHTMOUSE:
975 case K_RIGHTDRAG:
976 case K_RIGHTRELEASE:
977 case K_X1MOUSE:
978 case K_X1DRAG:
979 case K_X1RELEASE:
980 case K_X2MOUSE:
981 case K_X2DRAG:
982 case K_X2RELEASE:
983 ins_mouse(c);
984 break;
985
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000986 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 ins_mousescroll(FALSE);
988 break;
989
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000990 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 ins_mousescroll(TRUE);
992 break;
993#endif
994
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000995 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 break;
997
Bram Moolenaar4770d092006-01-12 23:22:24 +0000998#ifdef FEAT_GUI_W32
999 /* On Win32 ignore <M-F4>, we get it when closing the window was
1000 * cancelled. */
1001 case K_F4:
1002 if (mod_mask != MOD_MASK_ALT)
1003 goto normalchar;
1004 break;
1005#endif
1006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#ifdef FEAT_GUI
1008 case K_VER_SCROLLBAR:
1009 ins_scroll();
1010 break;
1011
1012 case K_HOR_SCROLLBAR:
1013 ins_horscroll();
1014 break;
1015#endif
1016
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001017 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 case K_S_HOME:
1020 case K_C_HOME:
1021 ins_home(c);
1022 break;
1023
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001024 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 case K_S_END:
1027 case K_C_END:
1028 ins_end(c);
1029 break;
1030
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001031 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001032 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1033 ins_s_left();
1034 else
1035 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 break;
1037
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001038 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 case K_C_LEFT:
1040 ins_s_left();
1041 break;
1042
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001043 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001044 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1045 ins_s_right();
1046 else
1047 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 break;
1049
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001050 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 case K_C_RIGHT:
1052 ins_s_right();
1053 break;
1054
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 case K_UP: /* <Up> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001056 if (mod_mask & MOD_MASK_SHIFT)
1057 ins_pageup();
1058 else
1059 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 break;
1061
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001062 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 case K_PAGEUP:
1064 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001065#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001066 if (pum_visible())
1067 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 ins_pageup();
1070 break;
1071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case K_DOWN: /* <Down> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001073 if (mod_mask & MOD_MASK_SHIFT)
1074 ins_pagedown();
1075 else
1076 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 break;
1078
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001079 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 case K_PAGEDOWN:
1081 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001082#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001083 if (pum_visible())
1084 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 ins_pagedown();
1087 break;
1088
1089#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001090 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 ins_drop();
1092 break;
1093#endif
1094
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001095 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 c = TAB;
1097 /* FALLTHROUGH */
1098
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001099 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1101 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1102 goto docomplete;
1103#endif
1104 inserted_space = FALSE;
1105 if (ins_tab())
1106 goto normalchar; /* insert TAB as a normal char */
1107 auto_format(FALSE, TRUE);
1108 break;
1109
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001110 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 c = CAR;
1112 /* FALLTHROUGH */
1113 case CAR:
1114 case NL:
1115#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1116 /* In a quickfix window a <CR> jumps to the error under the
1117 * cursor. */
1118 if (bt_quickfix(curbuf) && c == CAR)
1119 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001120 if (curwin->w_llist_ref == NULL) /* quickfix window */
1121 do_cmdline_cmd((char_u *)".cc");
1122 else /* location list window */
1123 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 break;
1125 }
1126#endif
1127#ifdef FEAT_CMDWIN
1128 if (cmdwin_type != 0)
1129 {
1130 /* Execute the command in the cmdline window. */
1131 cmdwin_result = CAR;
1132 goto doESCkey;
1133 }
1134#endif
1135 if (ins_eol(c) && !p_im)
1136 goto doESCkey; /* out of memory */
1137 auto_format(FALSE, FALSE);
1138 inserted_space = FALSE;
1139 break;
1140
1141#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001142 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143# ifdef FEAT_INS_EXPAND
1144 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1145 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146 if (has_compl_option(TRUE))
1147 goto docomplete;
1148 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 }
1150# endif
1151# ifdef FEAT_DIGRAPHS
1152 c = ins_digraph();
1153 if (c == NUL)
1154 break;
1155# endif
1156 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
1159#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001160 case Ctrl_X: /* Enter CTRL-X mode */
1161 ins_ctrl_x();
1162 break;
1163
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001164 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 if (ctrl_x_mode != CTRL_X_TAGS)
1166 goto normalchar;
1167 goto docomplete;
1168
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (ctrl_x_mode != CTRL_X_FILES)
1171 goto normalchar;
1172 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001173
1174 case 's': /* Spelling completion after ^X */
1175 case Ctrl_S:
1176 if (ctrl_x_mode != CTRL_X_SPELL)
1177 goto normalchar;
1178 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#endif
1180
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001181 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#ifdef FEAT_INS_EXPAND
1183 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1184#endif
1185 {
1186 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1187 if (p_im)
1188 {
1189 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1190 break;
1191 goto doESCkey;
1192 }
1193 goto normalchar;
1194 }
1195#ifdef FEAT_INS_EXPAND
1196 /* FALLTHROUGH */
1197
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001198 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 case Ctrl_N:
1200 /* if 'complete' is empty then plain ^P is no longer special,
1201 * but it is under other ^X modes */
1202 if (*curbuf->b_p_cpt == NUL
1203 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001204 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 goto normalchar;
1206
1207docomplete:
1208 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001209 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 break;
1211#endif /* FEAT_INS_EXPAND */
1212
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001213 case Ctrl_Y: /* copy from previous line or scroll down */
1214 case Ctrl_E: /* copy from next line or scroll up */
1215 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 break;
1217
1218 default:
1219#ifdef UNIX
1220 if (c == intr_char) /* special interrupt char */
1221 goto do_intr;
1222#endif
1223
1224 /*
1225 * Insert a nomal character.
1226 */
1227normalchar:
1228#ifdef FEAT_SMARTINDENT
1229 /* Try to perform smart-indenting. */
1230 ins_try_si(c);
1231#endif
1232
1233 if (c == ' ')
1234 {
1235 inserted_space = TRUE;
1236#ifdef FEAT_CINDENT
1237 if (inindent(0))
1238 can_cindent = FALSE;
1239#endif
1240 if (Insstart_blank_vcol == MAXCOL
1241 && curwin->w_cursor.lnum == Insstart.lnum)
1242 Insstart_blank_vcol = get_nolist_virtcol();
1243 }
1244
1245 if (vim_iswordc(c) || !echeck_abbr(
1246#ifdef FEAT_MBYTE
1247 /* Add ABBR_OFF for characters above 0x100, this is
1248 * what check_abbr() expects. */
1249 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1250#endif
1251 c))
1252 {
1253 insert_special(c, FALSE, FALSE);
1254#ifdef FEAT_RIGHTLEFT
1255 revins_legal++;
1256 revins_chars++;
1257#endif
1258 }
1259
1260 auto_format(FALSE, TRUE);
1261
1262#ifdef FEAT_FOLDING
1263 /* When inserting a character the cursor line must never be in a
1264 * closed fold. */
1265 foldOpenCursor();
1266#endif
1267 break;
1268 } /* end of switch (c) */
1269
1270 /* If the cursor was moved we didn't just insert a space */
1271 if (arrow_used)
1272 inserted_space = FALSE;
1273
1274#ifdef FEAT_CINDENT
1275 if (can_cindent && cindent_on()
1276# ifdef FEAT_INS_EXPAND
1277 && ctrl_x_mode == 0
1278# endif
1279 )
1280 {
1281force_cindent:
1282 /*
1283 * Indent now if a key was typed that is in 'cinkeys'.
1284 */
1285 if (in_cinkeys(c, ' ', line_is_white))
1286 {
1287 if (stop_arrow() == OK)
1288 /* re-indent the current line */
1289 do_c_expr_indent();
1290 }
1291 }
1292#endif /* FEAT_CINDENT */
1293
1294 } /* for (;;) */
1295 /* NOTREACHED */
1296}
1297
1298/*
1299 * Redraw for Insert mode.
1300 * This is postponed until getting the next character to make '$' in the 'cpo'
1301 * option work correctly.
1302 * Only redraw when there are no characters available. This speeds up
1303 * inserting sequences of characters (e.g., for CTRL-R).
1304 */
1305 static void
1306ins_redraw()
1307{
1308 if (!char_avail())
1309 {
1310 if (must_redraw)
1311 update_screen(0);
1312 else if (clear_cmdline || redraw_cmdline)
1313 showmode(); /* clear cmdline and show mode */
1314 showruler(FALSE);
1315 setcursor();
1316 emsg_on_display = FALSE; /* may remove error message now */
1317 }
1318}
1319
1320/*
1321 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1322 */
1323 static void
1324ins_ctrl_v()
1325{
1326 int c;
1327
1328 /* may need to redraw when no more chars available now */
1329 ins_redraw();
1330
1331 if (redrawing() && !char_avail())
1332 edit_putchar('^', TRUE);
1333 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1334
1335#ifdef FEAT_CMDL_INFO
1336 add_to_showcmd_c(Ctrl_V);
1337#endif
1338
1339 c = get_literal();
1340#ifdef FEAT_CMDL_INFO
1341 clear_showcmd();
1342#endif
1343 insert_special(c, FALSE, TRUE);
1344#ifdef FEAT_RIGHTLEFT
1345 revins_chars++;
1346 revins_legal++;
1347#endif
1348}
1349
1350/*
1351 * Put a character directly onto the screen. It's not stored in a buffer.
1352 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1353 */
1354static int pc_status;
1355#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1356#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1357#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1358#define PC_STATUS_SET 3 /* pc_bytes was filled */
1359#ifdef FEAT_MBYTE
1360static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1361#else
1362static char_u pc_bytes[2]; /* saved bytes */
1363#endif
1364static int pc_attr;
1365static int pc_row;
1366static int pc_col;
1367
1368 void
1369edit_putchar(c, highlight)
1370 int c;
1371 int highlight;
1372{
1373 int attr;
1374
1375 if (ScreenLines != NULL)
1376 {
1377 update_topline(); /* just in case w_topline isn't valid */
1378 validate_cursor();
1379 if (highlight)
1380 attr = hl_attr(HLF_8);
1381 else
1382 attr = 0;
1383 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1384 pc_col = W_WINCOL(curwin);
1385#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1386 pc_status = PC_STATUS_UNSET;
1387#endif
1388#ifdef FEAT_RIGHTLEFT
1389 if (curwin->w_p_rl)
1390 {
1391 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1392# ifdef FEAT_MBYTE
1393 if (has_mbyte)
1394 {
1395 int fix_col = mb_fix_col(pc_col, pc_row);
1396
1397 if (fix_col != pc_col)
1398 {
1399 screen_putchar(' ', pc_row, fix_col, attr);
1400 --curwin->w_wcol;
1401 pc_status = PC_STATUS_RIGHT;
1402 }
1403 }
1404# endif
1405 }
1406 else
1407#endif
1408 {
1409 pc_col += curwin->w_wcol;
1410#ifdef FEAT_MBYTE
1411 if (mb_lefthalve(pc_row, pc_col))
1412 pc_status = PC_STATUS_LEFT;
1413#endif
1414 }
1415
1416 /* save the character to be able to put it back */
1417#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1418 if (pc_status == PC_STATUS_UNSET)
1419#endif
1420 {
1421 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1422 pc_status = PC_STATUS_SET;
1423 }
1424 screen_putchar(c, pc_row, pc_col, attr);
1425 }
1426}
1427
1428/*
1429 * Undo the previous edit_putchar().
1430 */
1431 void
1432edit_unputchar()
1433{
1434 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1435 {
1436#if defined(FEAT_MBYTE)
1437 if (pc_status == PC_STATUS_RIGHT)
1438 ++curwin->w_wcol;
1439 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1440 redrawWinline(curwin->w_cursor.lnum, FALSE);
1441 else
1442#endif
1443 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1444 }
1445}
1446
1447/*
1448 * Called when p_dollar is set: display a '$' at the end of the changed text
1449 * Only works when cursor is in the line that changes.
1450 */
1451 void
1452display_dollar(col)
1453 colnr_T col;
1454{
1455 colnr_T save_col;
1456
1457 if (!redrawing())
1458 return;
1459
1460 cursor_off();
1461 save_col = curwin->w_cursor.col;
1462 curwin->w_cursor.col = col;
1463#ifdef FEAT_MBYTE
1464 if (has_mbyte)
1465 {
1466 char_u *p;
1467
1468 /* If on the last byte of a multi-byte move to the first byte. */
1469 p = ml_get_curline();
1470 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1471 }
1472#endif
1473 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1474 if (curwin->w_wcol < W_WIDTH(curwin))
1475 {
1476 edit_putchar('$', FALSE);
1477 dollar_vcol = curwin->w_virtcol;
1478 }
1479 curwin->w_cursor.col = save_col;
1480}
1481
1482/*
1483 * Call this function before moving the cursor from the normal insert position
1484 * in insert mode.
1485 */
1486 static void
1487undisplay_dollar()
1488{
1489 if (dollar_vcol)
1490 {
1491 dollar_vcol = 0;
1492 redrawWinline(curwin->w_cursor.lnum, FALSE);
1493 }
1494}
1495
1496/*
1497 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1498 * Keep the cursor on the same character.
1499 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1500 * type == INDENT_DEC decrease indent (for CTRL-D)
1501 * type == INDENT_SET set indent to "amount"
1502 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1503 */
1504 void
1505change_indent(type, amount, round, replaced)
1506 int type;
1507 int amount;
1508 int round;
1509 int replaced; /* replaced character, put on replace stack */
1510{
1511 int vcol;
1512 int last_vcol;
1513 int insstart_less; /* reduction for Insstart.col */
1514 int new_cursor_col;
1515 int i;
1516 char_u *ptr;
1517 int save_p_list;
1518 int start_col;
1519 colnr_T vc;
1520#ifdef FEAT_VREPLACE
1521 colnr_T orig_col = 0; /* init for GCC */
1522 char_u *new_line, *orig_line = NULL; /* init for GCC */
1523
1524 /* VREPLACE mode needs to know what the line was like before changing */
1525 if (State & VREPLACE_FLAG)
1526 {
1527 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1528 orig_col = curwin->w_cursor.col;
1529 }
1530#endif
1531
1532 /* for the following tricks we don't want list mode */
1533 save_p_list = curwin->w_p_list;
1534 curwin->w_p_list = FALSE;
1535 vc = getvcol_nolist(&curwin->w_cursor);
1536 vcol = vc;
1537
1538 /*
1539 * For Replace mode we need to fix the replace stack later, which is only
1540 * possible when the cursor is in the indent. Remember the number of
1541 * characters before the cursor if it's possible.
1542 */
1543 start_col = curwin->w_cursor.col;
1544
1545 /* determine offset from first non-blank */
1546 new_cursor_col = curwin->w_cursor.col;
1547 beginline(BL_WHITE);
1548 new_cursor_col -= curwin->w_cursor.col;
1549
1550 insstart_less = curwin->w_cursor.col;
1551
1552 /*
1553 * If the cursor is in the indent, compute how many screen columns the
1554 * cursor is to the left of the first non-blank.
1555 */
1556 if (new_cursor_col < 0)
1557 vcol = get_indent() - vcol;
1558
1559 if (new_cursor_col > 0) /* can't fix replace stack */
1560 start_col = -1;
1561
1562 /*
1563 * Set the new indent. The cursor will be put on the first non-blank.
1564 */
1565 if (type == INDENT_SET)
1566 (void)set_indent(amount, SIN_CHANGED);
1567 else
1568 {
1569#ifdef FEAT_VREPLACE
1570 int save_State = State;
1571
1572 /* Avoid being called recursively. */
1573 if (State & VREPLACE_FLAG)
1574 State = INSERT;
1575#endif
1576 shift_line(type == INDENT_DEC, round, 1);
1577#ifdef FEAT_VREPLACE
1578 State = save_State;
1579#endif
1580 }
1581 insstart_less -= curwin->w_cursor.col;
1582
1583 /*
1584 * Try to put cursor on same character.
1585 * If the cursor is at or after the first non-blank in the line,
1586 * compute the cursor column relative to the column of the first
1587 * non-blank character.
1588 * If we are not in insert mode, leave the cursor on the first non-blank.
1589 * If the cursor is before the first non-blank, position it relative
1590 * to the first non-blank, counted in screen columns.
1591 */
1592 if (new_cursor_col >= 0)
1593 {
1594 /*
1595 * When changing the indent while the cursor is touching it, reset
1596 * Insstart_col to 0.
1597 */
1598 if (new_cursor_col == 0)
1599 insstart_less = MAXCOL;
1600 new_cursor_col += curwin->w_cursor.col;
1601 }
1602 else if (!(State & INSERT))
1603 new_cursor_col = curwin->w_cursor.col;
1604 else
1605 {
1606 /*
1607 * Compute the screen column where the cursor should be.
1608 */
1609 vcol = get_indent() - vcol;
1610 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1611
1612 /*
1613 * Advance the cursor until we reach the right screen column.
1614 */
1615 vcol = last_vcol = 0;
1616 new_cursor_col = -1;
1617 ptr = ml_get_curline();
1618 while (vcol <= (int)curwin->w_virtcol)
1619 {
1620 last_vcol = vcol;
1621#ifdef FEAT_MBYTE
1622 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001623 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 else
1625#endif
1626 ++new_cursor_col;
1627 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1628 }
1629 vcol = last_vcol;
1630
1631 /*
1632 * May need to insert spaces to be able to position the cursor on
1633 * the right screen column.
1634 */
1635 if (vcol != (int)curwin->w_virtcol)
1636 {
1637 curwin->w_cursor.col = new_cursor_col;
1638 i = (int)curwin->w_virtcol - vcol;
1639 ptr = alloc(i + 1);
1640 if (ptr != NULL)
1641 {
1642 new_cursor_col += i;
1643 ptr[i] = NUL;
1644 while (--i >= 0)
1645 ptr[i] = ' ';
1646 ins_str(ptr);
1647 vim_free(ptr);
1648 }
1649 }
1650
1651 /*
1652 * When changing the indent while the cursor is in it, reset
1653 * Insstart_col to 0.
1654 */
1655 insstart_less = MAXCOL;
1656 }
1657
1658 curwin->w_p_list = save_p_list;
1659
1660 if (new_cursor_col <= 0)
1661 curwin->w_cursor.col = 0;
1662 else
1663 curwin->w_cursor.col = new_cursor_col;
1664 curwin->w_set_curswant = TRUE;
1665 changed_cline_bef_curs();
1666
1667 /*
1668 * May have to adjust the start of the insert.
1669 */
1670 if (State & INSERT)
1671 {
1672 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1673 {
1674 if ((int)Insstart.col <= insstart_less)
1675 Insstart.col = 0;
1676 else
1677 Insstart.col -= insstart_less;
1678 }
1679 if ((int)ai_col <= insstart_less)
1680 ai_col = 0;
1681 else
1682 ai_col -= insstart_less;
1683 }
1684
1685 /*
1686 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1687 * If the number of characters before the cursor decreased, need to pop a
1688 * few characters from the replace stack.
1689 * If the number of characters before the cursor increased, need to push a
1690 * few NULs onto the replace stack.
1691 */
1692 if (REPLACE_NORMAL(State) && start_col >= 0)
1693 {
1694 while (start_col > (int)curwin->w_cursor.col)
1695 {
1696 replace_join(0); /* remove a NUL from the replace stack */
1697 --start_col;
1698 }
1699 while (start_col < (int)curwin->w_cursor.col || replaced)
1700 {
1701 replace_push(NUL);
1702 if (replaced)
1703 {
1704 replace_push(replaced);
1705 replaced = NUL;
1706 }
1707 ++start_col;
1708 }
1709 }
1710
1711#ifdef FEAT_VREPLACE
1712 /*
1713 * For VREPLACE mode, we also have to fix the replace stack. In this case
1714 * it is always possible because we backspace over the whole line and then
1715 * put it back again the way we wanted it.
1716 */
1717 if (State & VREPLACE_FLAG)
1718 {
1719 /* If orig_line didn't allocate, just return. At least we did the job,
1720 * even if you can't backspace. */
1721 if (orig_line == NULL)
1722 return;
1723
1724 /* Save new line */
1725 new_line = vim_strsave(ml_get_curline());
1726 if (new_line == NULL)
1727 return;
1728
1729 /* We only put back the new line up to the cursor */
1730 new_line[curwin->w_cursor.col] = NUL;
1731
1732 /* Put back original line */
1733 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1734 curwin->w_cursor.col = orig_col;
1735
1736 /* Backspace from cursor to start of line */
1737 backspace_until_column(0);
1738
1739 /* Insert new stuff into line again */
1740 ins_bytes(new_line);
1741
1742 vim_free(new_line);
1743 }
1744#endif
1745}
1746
1747/*
1748 * Truncate the space at the end of a line. This is to be used only in an
1749 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1750 * modes.
1751 */
1752 void
1753truncate_spaces(line)
1754 char_u *line;
1755{
1756 int i;
1757
1758 /* find start of trailing white space */
1759 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1760 {
1761 if (State & REPLACE_FLAG)
1762 replace_join(0); /* remove a NUL from the replace stack */
1763 }
1764 line[i + 1] = NUL;
1765}
1766
1767#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1768 || defined(FEAT_COMMENTS) || defined(PROTO)
1769/*
1770 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1771 * modes correctly. May also be used when not in insert mode at all.
1772 */
1773 void
1774backspace_until_column(col)
1775 int col;
1776{
1777 while ((int)curwin->w_cursor.col > col)
1778 {
1779 curwin->w_cursor.col--;
1780 if (State & REPLACE_FLAG)
1781 replace_do_bs();
1782 else
1783 (void)del_char(FALSE);
1784 }
1785}
1786#endif
1787
1788#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1789/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001790 * CTRL-X pressed in Insert mode.
1791 */
1792 static void
1793ins_ctrl_x()
1794{
1795 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1796 * CTRL-V works like CTRL-N */
1797 if (ctrl_x_mode != CTRL_X_CMDLINE)
1798 {
1799 /* if the next ^X<> won't ADD nothing, then reset
1800 * compl_cont_status */
1801 if (compl_cont_status & CONT_N_ADDS)
1802 compl_cont_status = (compl_cont_status | CONT_INTRPT);
1803 else
1804 compl_cont_status = 0;
1805 /* We're not sure which CTRL-X mode it will be yet */
1806 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1807 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1808 edit_submode_pre = NULL;
1809 showmode();
1810 }
1811}
1812
1813/*
1814 * Return TRUE if the 'dict' or 'tsr' option can be used.
1815 */
1816 static int
1817has_compl_option(dict_opt)
1818 int dict_opt;
1819{
1820 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1821 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1822 {
1823 ctrl_x_mode = 0;
1824 edit_submode = NULL;
1825 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1826 : (char_u *)_("'thesaurus' option is empty"),
1827 hl_attr(HLF_E));
1828 if (emsg_silent == 0)
1829 {
1830 vim_beep();
1831 setcursor();
1832 out_flush();
1833 ui_delay(2000L, FALSE);
1834 }
1835 return FALSE;
1836 }
1837 return TRUE;
1838}
1839
1840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1842 * This depends on the current mode.
1843 */
1844 int
1845vim_is_ctrl_x_key(c)
1846 int c;
1847{
1848 /* Always allow ^R - let it's results then be checked */
1849 if (c == Ctrl_R)
1850 return TRUE;
1851
Bram Moolenaare3226be2005-12-18 22:10:00 +00001852 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001853 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001854 return TRUE;
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 switch (ctrl_x_mode)
1857 {
1858 case 0: /* Not in any CTRL-X mode */
1859 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1860 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001861 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1863 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1864 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001865 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1866 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 case CTRL_X_SCROLL:
1868 return (c == Ctrl_Y || c == Ctrl_E);
1869 case CTRL_X_WHOLE_LINE:
1870 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1871 case CTRL_X_FILES:
1872 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1873 case CTRL_X_DICTIONARY:
1874 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1875 case CTRL_X_THESAURUS:
1876 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1877 case CTRL_X_TAGS:
1878 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1879#ifdef FEAT_FIND_ID
1880 case CTRL_X_PATH_PATTERNS:
1881 return (c == Ctrl_P || c == Ctrl_N);
1882 case CTRL_X_PATH_DEFINES:
1883 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1884#endif
1885 case CTRL_X_CMDLINE:
1886 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1887 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001888#ifdef FEAT_COMPL_FUNC
1889 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001890 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001891 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001892 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001893#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001894 case CTRL_X_SPELL:
1895 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 EMSG(_(e_internal));
1898 return FALSE;
1899}
1900
1901/*
1902 * This is like ins_compl_add(), but if ic and inf are set, then the
1903 * case of the originally typed text is used, and the case of the completed
1904 * text is infered, ie this tries to work out what case you probably wanted
1905 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001906 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 */
1908 int
Bram Moolenaar572cb562005-08-05 21:35:02 +00001909ins_compl_add_infercase(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 char_u *str;
1911 int len;
1912 char_u *fname;
1913 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001914 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915{
1916 int has_lower = FALSE;
1917 int was_letter = FALSE;
1918 int idx;
1919
1920 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
1921 {
1922 /* Infer case of completed part -- webb */
1923 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001924 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
1926 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001927 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001929 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
1931 has_lower = TRUE;
1932 if (isupper(IObuff[idx]))
1933 {
1934 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001935 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
1937 break;
1938 }
1939 }
1940 }
1941
1942 /*
1943 * Rule 2: No lower case, 2nd consecutive letter converted to
1944 * upper case.
1945 */
1946 if (!has_lower)
1947 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001948 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001950 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 && islower(IObuff[idx]))
1952 {
1953 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001954 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
1956 break;
1957 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001958 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 }
1960 }
1961
1962 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001963 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964
Bram Moolenaar572cb562005-08-05 21:35:02 +00001965 return ins_compl_add(IObuff, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00001967 return ins_compl_add(str, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968}
1969
1970/*
1971 * Add a match to the list of matches.
1972 * If the given string is already in the list of completions, then return
1973 * FAIL, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaar572cb562005-08-05 21:35:02 +00001974 * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb.
1975 *
1976 * New:
1977 * If the given string is already in the list of completions, then return
1978 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
1979 * maybe because alloc() returns NULL, then FAIL is returned -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00001981 int
1982ins_compl_add(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 char_u *str;
1984 int len;
1985 char_u *fname;
1986 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001987 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
Bram Moolenaar572cb562005-08-05 21:35:02 +00001989 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990
1991 ui_breakcheck();
1992 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00001993 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 if (len < 0)
1995 len = (int)STRLEN(str);
1996
1997 /*
1998 * If the same match is already present, don't add it.
1999 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002000 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002002 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 do
2004 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002005 if ( !(match->cp_flags & ORIGINAL_TEXT)
2006 && STRNCMP(match->cp_str, str, (size_t)len) == 0
2007 && match->cp_str[len] == NUL)
2008 return NOTDONE;
2009 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002010 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002013 /* Remove any popup menu before changing the list of matches. */
2014 ins_compl_del_pum();
2015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 /*
2017 * Allocate a new match structure.
2018 * Copy the values to the new match structure.
2019 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002020 match = (compl_T *)alloc((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002022 return FAIL;
2023 match->cp_number = -1;
2024 if (flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002026 match->cp_number = 0;
2027 match->cp_str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002029 else if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
2031 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002032 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 }
2034 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002035 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2037 * - NULL otherwise. --Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002038 if (fname && compl_curr_match && compl_curr_match->cp_fname
2039 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
2040 match->cp_fname = compl_curr_match->cp_fname;
2041 else if (fname && (match->cp_fname = vim_strsave(fname)) != NULL)
2042 flags |= FREE_FNAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002044 match->cp_fname = NULL;
2045 match->cp_flags = flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047 /*
2048 * Link the new match structure in the list of matches.
2049 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002050 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002051 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 else if (dir == FORWARD)
2053 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002054 match->cp_next = compl_curr_match->cp_next;
2055 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 }
2057 else /* BACKWARD */
2058 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002059 match->cp_next = compl_curr_match;
2060 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002062 if (match->cp_next)
2063 match->cp_next->cp_prev = match;
2064 if (match->cp_prev)
2065 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002067 compl_first_match = match;
2068 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070 return OK;
2071}
2072
2073/*
2074 * Add an array of matches to the list of matches.
2075 * Frees matches[].
2076 */
2077 static void
2078ins_compl_add_matches(num_matches, matches, dir)
2079 int num_matches;
2080 char_u **matches;
2081 int dir;
2082{
2083 int i;
2084 int add_r = OK;
2085 int ldir = dir;
2086
Bram Moolenaar572cb562005-08-05 21:35:02 +00002087 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 if ((add_r = ins_compl_add(matches[i], -1, NULL, ldir, 0)) == OK)
2089 /* if dir was BACKWARD then honor it just once */
2090 ldir = FORWARD;
2091 FreeWild(num_matches, matches);
2092}
2093
2094/* Make the completion list cyclic.
2095 * Return the number of matches (excluding the original).
2096 */
2097 static int
2098ins_compl_make_cyclic()
2099{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002100 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 int count = 0;
2102
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002103 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
2105 /*
2106 * Find the end of the list.
2107 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002108 match = compl_first_match;
2109 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002110 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002112 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 ++count;
2114 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002115 match->cp_next = compl_first_match;
2116 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
2118 return count;
2119}
2120
Bram Moolenaar9372a112005-12-06 19:59:18 +00002121/* "compl_match_array" points the currently displayed list of entries in the
2122 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002123static char_u **compl_match_array = NULL;
2124static int compl_match_arraysize;
2125
2126/*
2127 * Update the screen and when there is any scrolling remove the popup menu.
2128 */
2129 static void
2130ins_compl_upd_pum()
2131{
2132 int h;
2133
2134 if (compl_match_array != NULL)
2135 {
2136 h = curwin->w_cline_height;
2137 update_screen(0);
2138 if (h != curwin->w_cline_height)
2139 ins_compl_del_pum();
2140 }
2141}
2142
2143/*
2144 * Remove any popup menu.
2145 */
2146 static void
2147ins_compl_del_pum()
2148{
2149 if (compl_match_array != NULL)
2150 {
2151 pum_undisplay();
2152 vim_free(compl_match_array);
2153 compl_match_array = NULL;
2154 }
2155}
2156
2157/*
2158 * Return TRUE if the popup menu should be displayed.
2159 */
2160 static int
2161pum_wanted()
2162{
2163 compl_T *compl;
2164 int i;
2165
2166 /* 'completeopt' must contain "menu" */
2167 if (*p_cot == NUL)
2168 return FALSE;
2169
2170 /* The display looks bad on a B&W display. */
2171 if (t_colors < 8
2172#ifdef FEAT_GUI
2173 && !gui.in_use
2174#endif
2175 )
2176 return FALSE;
2177
2178 /* Don't display the popup menu if there are no matches or there is only
2179 * one (ignoring the original text). */
2180 compl = compl_first_match;
2181 i = 0;
2182 do
2183 {
2184 if (compl == NULL
2185 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2186 break;
2187 compl = compl->cp_next;
2188 } while (compl != compl_first_match);
2189
2190 return (i >= 2);
2191}
2192
2193/*
2194 * Show the popup menu for the list of matches.
2195 */
2196 static void
2197ins_compl_show_pum()
2198{
2199 compl_T *compl;
2200 int i;
2201 int cur = -1;
2202 colnr_T col;
2203
2204 if (!pum_wanted())
2205 return;
2206
2207 /* Update the screen before drawing the popup menu over it. */
2208 update_screen(0);
2209
2210 if (compl_match_array == NULL)
2211 {
2212 /* Need to build the popup menu list. */
2213 compl_match_arraysize = 0;
2214 compl = compl_first_match;
2215 do
2216 {
2217 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2218 ++compl_match_arraysize;
2219 compl = compl->cp_next;
2220 } while (compl != NULL && compl != compl_first_match);
2221 compl_match_array = (char_u **)alloc((unsigned)(sizeof(char_u **)
2222 * compl_match_arraysize));
2223 if (compl_match_array != NULL)
2224 {
2225 i = 0;
2226 compl = compl_first_match;
2227 do
2228 {
2229 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2230 {
2231 if (compl == compl_shown_match)
2232 cur = i;
2233 compl_match_array[i++] = compl->cp_str;
2234 }
2235 compl = compl->cp_next;
2236 } while (compl != NULL && compl != compl_first_match);
2237 }
2238 }
2239 else
2240 {
2241 /* popup menu already exists, only need to find the current item.*/
2242 i = 0;
2243 compl = compl_first_match;
2244 do
2245 {
2246 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2247 {
2248 if (compl == compl_shown_match)
2249 {
2250 cur = i;
2251 break;
2252 }
2253 ++i;
2254 }
2255 compl = compl->cp_next;
2256 } while (compl != NULL && compl != compl_first_match);
2257 }
2258
2259 if (compl_match_array != NULL)
2260 {
2261 /* Compute the screen column of the start of the completed text.
2262 * Use the cursor to get all wrapping and other settings right. */
2263 col = curwin->w_cursor.col;
2264 curwin->w_cursor.col = compl_col;
2265 validate_cursor_col();
2266 pum_display(compl_match_array, compl_match_arraysize, cur,
2267 curwin->w_cline_row + W_WINROW(curwin),
2268 curwin->w_cline_height,
2269 curwin->w_wcol + W_WINCOL(curwin));
2270 curwin->w_cursor.col = col;
2271 }
2272}
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274#define DICT_FIRST (1) /* use just first element in "dict" */
2275#define DICT_EXACT (2) /* "dict" is the exact name of a file */
2276/*
2277 * Add any identifiers that match the given pattern to the list of
2278 * completions.
2279 */
2280 static void
2281ins_compl_dictionaries(dict, pat, dir, flags, thesaurus)
2282 char_u *dict;
2283 char_u *pat;
2284 int dir;
2285 int flags;
2286 int thesaurus;
2287{
2288 char_u *ptr;
2289 char_u *buf;
2290 FILE *fp;
2291 regmatch_T regmatch;
2292 int add_r;
2293 char_u **files;
2294 int count;
2295 int i;
2296 int save_p_scs;
2297
2298 buf = alloc(LSIZE);
2299 /* If 'infercase' is set, don't use 'smartcase' here */
2300 save_p_scs = p_scs;
2301 if (curbuf->b_p_inf)
2302 p_scs = FALSE;
2303 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2304 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2305 regmatch.rm_ic = ignorecase(pat);
2306 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002307 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
2309 /* copy one dictionary file name into buf */
2310 if (flags == DICT_EXACT)
2311 {
2312 count = 1;
2313 files = &dict;
2314 }
2315 else
2316 {
2317 /* Expand wildcards in the dictionary name, but do not allow
2318 * backticks (for security, the 'dict' option may have been set in
2319 * a modeline). */
2320 copy_option_part(&dict, buf, LSIZE, ",");
2321 if (vim_strchr(buf, '`') != NULL
2322 || expand_wildcards(1, &buf, &count, &files,
2323 EW_FILE|EW_SILENT) != OK)
2324 count = 0;
2325 }
2326
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002327 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
2329 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2330 if (flags != DICT_EXACT)
2331 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002332 vim_snprintf((char *)IObuff, IOSIZE,
2333 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2335 }
2336
2337 if (fp != NULL)
2338 {
2339 /*
2340 * Read dictionary file line by line.
2341 * Check each line for a match.
2342 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002343 while (!got_int && !compl_interrupted
2344 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
2346 ptr = buf;
2347 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2348 {
2349 ptr = regmatch.startp[0];
2350 ptr = find_word_end(ptr);
2351 add_r = ins_compl_add_infercase(regmatch.startp[0],
2352 (int)(ptr - regmatch.startp[0]),
2353 files[i], dir, 0);
2354 if (thesaurus)
2355 {
2356 char_u *wstart;
2357
2358 /*
2359 * Add the other matches on the line
2360 */
2361 while (!got_int)
2362 {
2363 /* Find start of the next word. Skip white
2364 * space and punctuation. */
2365 ptr = find_word_start(ptr);
2366 if (*ptr == NUL || *ptr == NL)
2367 break;
2368 wstart = ptr;
2369
2370 /* Find end of the word and add it. */
2371#ifdef FEAT_MBYTE
2372 if (has_mbyte)
2373 /* Japanese words may have characters in
2374 * different classes, only separate words
2375 * with single-byte non-word characters. */
2376 while (*ptr != NUL)
2377 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002378 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380 if (l < 2 && !vim_iswordc(*ptr))
2381 break;
2382 ptr += l;
2383 }
2384 else
2385#endif
2386 ptr = find_word_end(ptr);
2387 add_r = ins_compl_add_infercase(wstart,
2388 (int)(ptr - wstart), files[i], dir, 0);
2389 }
2390 }
2391 if (add_r == OK)
2392 /* if dir was BACKWARD then honor it just once */
2393 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002394 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 break;
2396 /* avoid expensive call to vim_regexec() when at end
2397 * of line */
2398 if (*ptr == '\n' || got_int)
2399 break;
2400 }
2401 line_breakcheck();
Bram Moolenaar572cb562005-08-05 21:35:02 +00002402 ins_compl_check_keys(50);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404 fclose(fp);
2405 }
2406 }
2407 if (flags != DICT_EXACT)
2408 FreeWild(count, files);
2409 if (flags)
2410 break;
2411 }
2412 p_scs = save_p_scs;
2413 vim_free(regmatch.regprog);
2414 vim_free(buf);
2415}
2416
2417/*
2418 * Find the start of the next word.
2419 * Returns a pointer to the first char of the word. Also stops at a NUL.
2420 */
2421 char_u *
2422find_word_start(ptr)
2423 char_u *ptr;
2424{
2425#ifdef FEAT_MBYTE
2426 if (has_mbyte)
2427 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002428 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 else
2430#endif
2431 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2432 ++ptr;
2433 return ptr;
2434}
2435
2436/*
2437 * Find the end of the word. Assumes it starts inside a word.
2438 * Returns a pointer to just after the word.
2439 */
2440 char_u *
2441find_word_end(ptr)
2442 char_u *ptr;
2443{
2444#ifdef FEAT_MBYTE
2445 int start_class;
2446
2447 if (has_mbyte)
2448 {
2449 start_class = mb_get_class(ptr);
2450 if (start_class > 1)
2451 while (*ptr != NUL)
2452 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002453 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 if (mb_get_class(ptr) != start_class)
2455 break;
2456 }
2457 }
2458 else
2459#endif
2460 while (vim_iswordc(*ptr))
2461 ++ptr;
2462 return ptr;
2463}
2464
2465/*
2466 * Free the list of completions
2467 */
2468 static void
2469ins_compl_free()
2470{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002471 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002473 vim_free(compl_pattern);
2474 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002476 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002478
2479 ins_compl_del_pum();
2480 pum_clear();
2481
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002482 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 do
2484 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002485 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002486 compl_curr_match = compl_curr_match->cp_next;
2487 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002489 if (match->cp_flags & FREE_FNAME)
2490 vim_free(match->cp_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002492 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2493 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494}
2495
2496 static void
2497ins_compl_clear()
2498{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002499 compl_cont_status = 0;
2500 compl_started = FALSE;
2501 compl_matches = 0;
2502 vim_free(compl_pattern);
2503 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 save_sm = -1;
2505 edit_submode_extra = NULL;
2506}
2507
2508/*
2509 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002510 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002511 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002513 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514ins_compl_prep(c)
2515 int c;
2516{
2517 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 int temp;
2519 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002520 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521
2522 /* Forget any previous 'special' messages if this is actually
2523 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2524 */
2525 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2526 edit_submode_extra = NULL;
2527
2528 /* Ignore end of Select mode mapping */
2529 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002530 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
2532 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2533 {
2534 /*
2535 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2536 * it will be yet. Now we decide.
2537 */
2538 switch (c)
2539 {
2540 case Ctrl_E:
2541 case Ctrl_Y:
2542 ctrl_x_mode = CTRL_X_SCROLL;
2543 if (!(State & REPLACE_FLAG))
2544 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2545 else
2546 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2547 edit_submode_pre = NULL;
2548 showmode();
2549 break;
2550 case Ctrl_L:
2551 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2552 break;
2553 case Ctrl_F:
2554 ctrl_x_mode = CTRL_X_FILES;
2555 break;
2556 case Ctrl_K:
2557 ctrl_x_mode = CTRL_X_DICTIONARY;
2558 break;
2559 case Ctrl_R:
2560 /* Simply allow ^R to happen without affecting ^X mode */
2561 break;
2562 case Ctrl_T:
2563 ctrl_x_mode = CTRL_X_THESAURUS;
2564 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002565#ifdef FEAT_COMPL_FUNC
2566 case Ctrl_U:
2567 ctrl_x_mode = CTRL_X_FUNCTION;
2568 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002569 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002570 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002571 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002572#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002573 case 's':
2574 case Ctrl_S:
2575 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002576#ifdef FEAT_SYN_HL
2577 spell_back_to_badword();
2578#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002579 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 case Ctrl_RSB:
2581 ctrl_x_mode = CTRL_X_TAGS;
2582 break;
2583#ifdef FEAT_FIND_ID
2584 case Ctrl_I:
2585 case K_S_TAB:
2586 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2587 break;
2588 case Ctrl_D:
2589 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2590 break;
2591#endif
2592 case Ctrl_V:
2593 case Ctrl_Q:
2594 ctrl_x_mode = CTRL_X_CMDLINE;
2595 break;
2596 case Ctrl_P:
2597 case Ctrl_N:
2598 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
2599 * just started ^X mode, or there were enough ^X's to cancel
2600 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2601 * do normal expansion when interrupting a different mode (say
2602 * ^X^F^X^P or ^P^X^X^P, see below)
2603 * nothing changes if interrupting mode 0, (eg, the flag
2604 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002605 if (!(compl_cont_status & CONT_INTRPT))
2606 compl_cont_status |= CONT_LOCAL;
2607 else if (compl_cont_mode != 0)
2608 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 /* FALLTHROUGH */
2610 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002611 /* If we have typed at least 2 ^X's... for modes != 0, we set
2612 * compl_cont_status = 0 (eg, as if we had just started ^X
2613 * mode).
2614 * For mode 0, we set "compl_cont_mode" to an impossible
2615 * value, in both cases ^X^X can be used to restart the same
2616 * mode (avoiding ADDING mode).
2617 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2618 * 'complete' and local ^P expansions respectively.
2619 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2620 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 if (c == Ctrl_X)
2622 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002623 if (compl_cont_mode != 0)
2624 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002626 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
2628 ctrl_x_mode = 0;
2629 edit_submode = NULL;
2630 showmode();
2631 break;
2632 }
2633 }
2634 else if (ctrl_x_mode != 0)
2635 {
2636 /* We're already in CTRL-X mode, do we stay in it? */
2637 if (!vim_is_ctrl_x_key(c))
2638 {
2639 if (ctrl_x_mode == CTRL_X_SCROLL)
2640 ctrl_x_mode = 0;
2641 else
2642 ctrl_x_mode = CTRL_X_FINISHED;
2643 edit_submode = NULL;
2644 }
2645 showmode();
2646 }
2647
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002648 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 {
2650 /* Show error message from attempted keyword completion (probably
2651 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002652 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002654 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
2655 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 || ctrl_x_mode == CTRL_X_FINISHED)
2657 {
2658 /* Get here when we have finished typing a sequence of ^N and
2659 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002660 * memory that was used, and make sure we can redo the insert. */
2661 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002663 char_u *p;
2664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 /*
2666 * If any of the original typed text has been changed,
2667 * eg when ignorecase is set, we must add back-spaces to
2668 * the redo buffer. We add as few as necessary to delete
2669 * just the part of the original text that has changed.
2670 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002671 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002672 p = compl_orig_text;
2673 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002675 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 ++ptr;
2677 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002678 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002680 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
2682
2683#ifdef FEAT_CINDENT
2684 want_cindent = (can_cindent && cindent_on());
2685#endif
2686 /*
2687 * When completing whole lines: fix indent for 'cindent'.
2688 * Otherwise, break line if it's too long.
2689 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002690 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 {
2692#ifdef FEAT_CINDENT
2693 /* re-indent the current line */
2694 if (want_cindent)
2695 {
2696 do_c_expr_indent();
2697 want_cindent = FALSE; /* don't do it again */
2698 }
2699#endif
2700 }
2701 else
2702 {
2703 /* put the cursor on the last char, for 'tw' formatting */
2704 curwin->w_cursor.col--;
2705 if (stop_arrow() == OK)
2706 insertchar(NUL, 0, -1);
2707 curwin->w_cursor.col++;
2708 }
2709
2710 auto_format(FALSE, TRUE);
2711
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002712 /* if the popup menu is displayed hitting Enter means accepting
2713 * the selection without inserting anything. */
2714 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
2715 retval = TRUE;
2716
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002718 compl_started = FALSE;
2719 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 msg_clr_cmdline(); /* necessary for "noshowmode" */
2721 ctrl_x_mode = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002722 if (save_sm >= 0)
2723 p_sm = save_sm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 if (edit_submode != NULL)
2725 {
2726 edit_submode = NULL;
2727 showmode();
2728 }
2729
2730#ifdef FEAT_CINDENT
2731 /*
2732 * Indent now if a key was typed that is in 'cinkeys'.
2733 */
2734 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2735 do_c_expr_indent();
2736#endif
2737 }
2738 }
2739
2740 /* reset continue_* if we left expansion-mode, if we stay they'll be
2741 * (re)set properly in ins_complete() */
2742 if (!vim_is_ctrl_x_key(c))
2743 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002744 compl_cont_status = 0;
2745 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002747
2748 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749}
2750
2751/*
2752 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2753 * (depending on flag) starting from buf and looking for a non-scanned
2754 * buffer (other than curbuf). curbuf is special, if it is called with
2755 * buf=curbuf then it has to be the first call for a given flag/expansion.
2756 *
2757 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2758 */
2759 static buf_T *
2760ins_compl_next_buf(buf, flag)
2761 buf_T *buf;
2762 int flag;
2763{
2764#ifdef FEAT_WINDOWS
2765 static win_T *wp;
2766#endif
2767
2768 if (flag == 'w') /* just windows */
2769 {
2770#ifdef FEAT_WINDOWS
2771 if (buf == curbuf) /* first call for this flag/expansion */
2772 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002773 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 && wp->w_buffer->b_scanned)
2775 ;
2776 buf = wp->w_buffer;
2777#else
2778 buf = curbuf;
2779#endif
2780 }
2781 else
2782 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2783 * (unlisted buffers)
2784 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002785 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 && ((flag == 'U'
2787 ? buf->b_p_bl
2788 : (!buf->b_p_bl
2789 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2790 || buf->b_scanned
2791 || (buf->b_ml.ml_mfp == NULL
2792 && ctrl_x_mode == CTRL_X_WHOLE_LINE)))
2793 ;
2794 return buf;
2795}
2796
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002797#ifdef FEAT_COMPL_FUNC
Bram Moolenaare344bea2005-09-01 20:46:49 +00002798static int expand_by_function __ARGS((int type, char_u *base, char_u ***matches));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002799
2800/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002801 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00002802 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002803 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002804 */
2805 static int
Bram Moolenaare344bea2005-09-01 20:46:49 +00002806expand_by_function(type, base, matches)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002807 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002808 char_u *base;
2809 char_u ***matches;
2810{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002811 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002812 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002813 listitem_T *li;
2814 garray_T ga;
2815 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002816 char_u *funcname;
2817 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002818
Bram Moolenaare344bea2005-09-01 20:46:49 +00002819 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
2820 if (*funcname == NUL)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002821 return 0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002822
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002823 /* Call 'completefunc' to obtain the list of matches. */
2824 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00002825 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002826
Bram Moolenaare344bea2005-09-01 20:46:49 +00002827 pos = curwin->w_cursor;
2828 matchlist = call_func_retlist(funcname, 2, args, FALSE);
2829 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002830 if (matchlist == NULL)
2831 return 0;
2832
2833 /* Go through the List with matches and put them in an array. */
2834 ga_init2(&ga, (int)sizeof(char_u *), 8);
2835 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002836 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002837 p = get_tv_string_chk(&li->li_tv);
2838 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002839 {
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002840 if (ga_grow(&ga, 1) == FAIL)
2841 break;
2842 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p);
2843 ++ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002844 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002845 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002846
2847 list_unref(matchlist);
2848 *matches = (char_u **)ga.ga_data;
2849 return ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002850}
2851#endif /* FEAT_COMPL_FUNC */
2852
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002853/*
2854 * Get the next expansion(s), using "compl_pattern".
2855 * The search starts at position "ini" in curbuf and in the direction dir.
2856 * When "compl_started" is FALSE start at that position, otherwise
2857 * continue where we stopped searching before.
2858 * This may return before finding all the matches.
2859 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 */
2861 static int
2862ins_compl_get_exp(ini, dir)
2863 pos_T *ini;
2864 int dir;
2865{
2866 static pos_T first_match_pos;
2867 static pos_T last_match_pos;
2868 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002869 static int found_all = FALSE; /* Found all matches of a
2870 certain type. */
2871 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
Bram Moolenaar572cb562005-08-05 21:35:02 +00002873 pos_T *pos;
2874 char_u **matches;
2875 int save_p_scs;
2876 int save_p_ws;
2877 int save_p_ic;
2878 int i;
2879 int num_matches;
2880 int len;
2881 int found_new_match;
2882 int type = ctrl_x_mode;
2883 char_u *ptr;
2884 char_u *dict = NULL;
2885 int dict_f = 0;
2886 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002888 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 {
2890 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
2891 ins_buf->b_scanned = 0;
2892 found_all = FALSE;
2893 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002894 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002895 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 last_match_pos = first_match_pos = *ini;
2897 }
2898
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002899 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 pos = (dir == FORWARD) ? &last_match_pos : &first_match_pos;
2901 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
2902 for (;;)
2903 {
2904 found_new_match = FAIL;
2905
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002906 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 * or if found_all says this entry is done. For ^X^L only use the
2908 * entries from 'complete' that look in loaded buffers. */
2909 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002910 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 {
2912 found_all = FALSE;
2913 while (*e_cpt == ',' || *e_cpt == ' ')
2914 e_cpt++;
2915 if (*e_cpt == '.' && !curbuf->b_scanned)
2916 {
2917 ins_buf = curbuf;
2918 first_match_pos = *ini;
2919 /* So that ^N can match word immediately after cursor */
2920 if (ctrl_x_mode == 0)
2921 dec(&first_match_pos);
2922 last_match_pos = first_match_pos;
2923 type = 0;
2924 }
2925 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2926 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2927 {
2928 /* Scan a buffer, but not the current one. */
2929 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
2930 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002931 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 first_match_pos.col = last_match_pos.col = 0;
2933 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2934 last_match_pos.lnum = 0;
2935 type = 0;
2936 }
2937 else /* unloaded buffer, scan like dictionary */
2938 {
2939 found_all = TRUE;
2940 if (ins_buf->b_fname == NULL)
2941 continue;
2942 type = CTRL_X_DICTIONARY;
2943 dict = ins_buf->b_fname;
2944 dict_f = DICT_EXACT;
2945 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00002946 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 ins_buf->b_fname == NULL
2948 ? buf_spname(ins_buf)
2949 : ins_buf->b_sfname == NULL
2950 ? (char *)ins_buf->b_fname
2951 : (char *)ins_buf->b_sfname);
2952 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2953 }
2954 else if (*e_cpt == NUL)
2955 break;
2956 else
2957 {
2958 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2959 type = -1;
2960 else if (*e_cpt == 'k' || *e_cpt == 's')
2961 {
2962 if (*e_cpt == 'k')
2963 type = CTRL_X_DICTIONARY;
2964 else
2965 type = CTRL_X_THESAURUS;
2966 if (*++e_cpt != ',' && *e_cpt != NUL)
2967 {
2968 dict = e_cpt;
2969 dict_f = DICT_FIRST;
2970 }
2971 }
2972#ifdef FEAT_FIND_ID
2973 else if (*e_cpt == 'i')
2974 type = CTRL_X_PATH_PATTERNS;
2975 else if (*e_cpt == 'd')
2976 type = CTRL_X_PATH_DEFINES;
2977#endif
2978 else if (*e_cpt == ']' || *e_cpt == 't')
2979 {
2980 type = CTRL_X_TAGS;
2981 sprintf((char*)IObuff, _("Scanning tags."));
2982 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2983 }
2984 else
2985 type = -1;
2986
2987 /* in any case e_cpt is advanced to the next entry */
2988 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2989
2990 found_all = TRUE;
2991 if (type == -1)
2992 continue;
2993 }
2994 }
2995
2996 switch (type)
2997 {
2998 case -1:
2999 break;
3000#ifdef FEAT_FIND_ID
3001 case CTRL_X_PATH_PATTERNS:
3002 case CTRL_X_PATH_DEFINES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003003 find_pattern_in_path(compl_pattern, dir,
3004 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003006 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3008 (linenr_T)1, (linenr_T)MAXLNUM);
3009 break;
3010#endif
3011
3012 case CTRL_X_DICTIONARY:
3013 case CTRL_X_THESAURUS:
3014 ins_compl_dictionaries(
3015 dict ? dict
3016 : (type == CTRL_X_THESAURUS
3017 ? (*curbuf->b_p_tsr == NUL
3018 ? p_tsr
3019 : curbuf->b_p_tsr)
3020 : (*curbuf->b_p_dict == NUL
3021 ? p_dict
3022 : curbuf->b_p_dict)),
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003023 compl_pattern, dir,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
3025 dict = NULL;
3026 break;
3027
3028 case CTRL_X_TAGS:
3029 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3030 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003031 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
3033 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003034 * of matches is found when compl_pattern is empty */
3035 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3037 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3038 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3039 {
3040 ins_compl_add_matches(num_matches, matches, dir);
3041 }
3042 p_ic = save_p_ic;
3043 break;
3044
3045 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003046 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3048 {
3049
3050 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003051 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 ins_compl_add_matches(num_matches, matches, dir);
3053 }
3054 break;
3055
3056 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003057 if (expand_cmdline(&compl_xp, compl_pattern,
3058 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 &num_matches, &matches) == EXPAND_OK)
3060 ins_compl_add_matches(num_matches, matches, dir);
3061 break;
3062
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003063#ifdef FEAT_COMPL_FUNC
3064 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003065 case CTRL_X_OMNI:
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003066 num_matches = expand_by_function(type, compl_pattern, &matches);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003067 if (num_matches > 0)
3068 ins_compl_add_matches(num_matches, matches, dir);
3069 break;
3070#endif
3071
Bram Moolenaar488c6512005-08-11 20:09:58 +00003072 case CTRL_X_SPELL:
3073#ifdef FEAT_SYN_HL
3074 num_matches = expand_spelling(first_match_pos.lnum,
3075 first_match_pos.col, compl_pattern, &matches);
3076 if (num_matches > 0)
3077 ins_compl_add_matches(num_matches, matches, dir);
3078#endif
3079 break;
3080
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 default: /* normal ^P/^N and ^X^L */
3082 /*
3083 * If 'infercase' is set, don't use 'smartcase' here
3084 */
3085 save_p_scs = p_scs;
3086 if (ins_buf->b_p_inf)
3087 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003088
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 /* buffers other than curbuf are scanned from the beginning or the
3090 * end but never from the middle, thus setting nowrapscan in this
3091 * buffers is a good idea, on the other hand, we always set
3092 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3093 save_p_ws = p_ws;
3094 if (ins_buf != curbuf)
3095 p_ws = FALSE;
3096 else if (*e_cpt == '.')
3097 p_ws = TRUE;
3098 for (;;)
3099 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003100 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003102 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3103 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003105 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003107 dir, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 else
3109 found_new_match = searchit(NULL, ins_buf, pos, dir,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003110 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 RE_LAST);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003112 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003114 /* set compl_started even on fail */
3115 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 first_match_pos = *pos;
3117 last_match_pos = *pos;
3118 }
3119 else if (first_match_pos.lnum == last_match_pos.lnum
3120 && first_match_pos.col == last_match_pos.col)
3121 found_new_match = FAIL;
3122 if (found_new_match == FAIL)
3123 {
3124 if (ins_buf == curbuf)
3125 found_all = TRUE;
3126 break;
3127 }
3128
3129 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003130 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 && ini->lnum == pos->lnum
3132 && ini->col == pos->col)
3133 continue;
3134 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3135 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3136 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003137 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
3139 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3140 continue;
3141 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3142 if (!p_paste)
3143 ptr = skipwhite(ptr);
3144 }
3145 len = (int)STRLEN(ptr);
3146 }
3147 else
3148 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003149 char_u *tmp_ptr = ptr;
3150
3151 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003153 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 /* Skip if already inside a word. */
3155 if (vim_iswordp(tmp_ptr))
3156 continue;
3157 /* Find start of next word. */
3158 tmp_ptr = find_word_start(tmp_ptr);
3159 }
3160 /* Find end of this word. */
3161 tmp_ptr = find_word_end(tmp_ptr);
3162 len = (int)(tmp_ptr - ptr);
3163
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003164 if ((compl_cont_status & CONT_ADDING)
3165 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 {
3167 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3168 {
3169 /* Try next line, if any. the new word will be
3170 * "join" as if the normal command "J" was used.
3171 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003172 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 * works -- Acevedo */
3174 STRNCPY(IObuff, ptr, len);
3175 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3176 tmp_ptr = ptr = skipwhite(ptr);
3177 /* Find start of next word. */
3178 tmp_ptr = find_word_start(tmp_ptr);
3179 /* Find end of next word. */
3180 tmp_ptr = find_word_end(tmp_ptr);
3181 if (tmp_ptr > ptr)
3182 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003183 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003185 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 IObuff[len++] = ' ';
3187 /* IObuf =~ "\k.* ", thus len >= 2 */
3188 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003189 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 || (vim_strchr(p_cpo, CPO_JOINSP)
3191 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003192 && (IObuff[len - 2] == '?'
3193 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 IObuff[len++] = ' ';
3195 }
3196 /* copy as much as posible of the new word */
3197 if (tmp_ptr - ptr >= IOSIZE - len)
3198 tmp_ptr = ptr + IOSIZE - len - 1;
3199 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3200 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003201 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 }
3203 IObuff[len] = NUL;
3204 ptr = IObuff;
3205 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003206 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 continue;
3208 }
3209 }
3210 if (ins_compl_add_infercase(ptr, len,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003211 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar572cb562005-08-05 21:35:02 +00003212 dir, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
3214 found_new_match = OK;
3215 break;
3216 }
3217 }
3218 p_scs = save_p_scs;
3219 p_ws = save_p_ws;
3220 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003221
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003222 /* check if compl_curr_match has changed, (e.g. other type of
3223 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003224 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 found_new_match = OK;
3226
3227 /* break the loop for specialized modes (use 'complete' just for the
3228 * generic ctrl_x_mode == 0) or when we've found a new match */
3229 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003230 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003231 {
3232 if (got_int)
3233 break;
3234 if (pum_wanted() && type != -1)
3235 /* Fill the popup menu as soon as possible. */
3236 ins_compl_check_keys(0);
3237 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3238 || compl_interrupted)
3239 break;
3240 compl_started = TRUE;
3241 }
3242 else
3243 {
3244 /* Mark a buffer scanned when it has been scanned completely */
3245 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3246 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003248 compl_started = FALSE;
3249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003251 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
3253 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3254 && *e_cpt == NUL) /* Got to end of 'complete' */
3255 found_new_match = FAIL;
3256
3257 i = -1; /* total of matches, unknown */
3258 if (found_new_match == FAIL
3259 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3260 i = ins_compl_make_cyclic();
3261
3262 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003263 * just been made cyclic then we have to move compl_curr_match to the next
3264 * or previous entry (if any) -- Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003265 compl_curr_match = dir == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003266 if (compl_curr_match == NULL)
3267 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 return i;
3269}
3270
3271/* Delete the old text being completed. */
3272 static void
3273ins_compl_delete()
3274{
3275 int i;
3276
3277 /*
3278 * In insert mode: Delete the typed part.
3279 * In replace mode: Put the old characters back, if any.
3280 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003281 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 backspace_until_column(i);
3283 changed_cline_bef_curs();
3284}
3285
3286/* Insert the new text being completed. */
3287 static void
3288ins_compl_insert()
3289{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003290 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291}
3292
3293/*
3294 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003295 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3296 * get more completions. If it is FALSE, then we just do nothing when there
3297 * are no more completions in a given direction. The latter case is used when
3298 * we are still in the middle of finding completions, to allow browsing
3299 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 * Return the total number of matches, or -1 if still unknown -- webb.
3301 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003302 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3303 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 *
3305 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003306 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3307 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 */
3309 static int
Bram Moolenaare3226be2005-12-18 22:10:00 +00003310ins_compl_next(allow_get_expansion, count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003312 int count; /* repeat completion this many times; should
3313 be at least 1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314{
3315 int num_matches = -1;
3316 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003317 int todo = count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318
3319 if (allow_get_expansion)
3320 {
3321 /* Delete old text to be replaced */
3322 ins_compl_delete();
3323 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003324 compl_pending = FALSE;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003325
3326 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3327 * around. */
3328 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003330 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003332 compl_shown_match = compl_shown_match->cp_next;
3333 if (compl_shown_match->cp_next != NULL
3334 && compl_shown_match->cp_next == compl_first_match)
3335 break;
3336 }
3337 else if (compl_shows_dir == BACKWARD
3338 && compl_shown_match->cp_prev != NULL)
3339 {
3340 compl_shown_match = compl_shown_match->cp_prev;
3341 if (compl_shown_match == compl_first_match)
3342 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
3344 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003345 {
3346 compl_pending = TRUE;
3347 if (allow_get_expansion)
3348 {
3349 num_matches = ins_compl_get_exp(&compl_startpos,
3350 compl_direction);
3351 if (compl_pending)
3352 {
3353 if (compl_direction == compl_shows_dir)
3354 compl_shown_match = compl_curr_match;
3355 }
3356 }
3357 else
3358 return -1;
3359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
3361
3362 /* Insert the text of the new completion */
3363 ins_compl_insert();
3364
3365 if (!allow_get_expansion)
3366 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003367 /* may undisplay the popup menu first */
3368 ins_compl_upd_pum();
3369
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 /* Display the current match. */
3371 update_screen(0);
3372
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003373 /* display the updated popup menu */
3374 ins_compl_show_pum();
3375
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 /* Delete old text to be replaced, since we're still searching and
3377 * don't want to match ourselves! */
3378 ins_compl_delete();
3379 }
3380
3381 /*
3382 * Show the file name for the match (if any)
3383 * Truncate the file name to avoid a wait for return.
3384 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003385 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
3387 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003388 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 if (i <= 0)
3390 i = 0;
3391 else
3392 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003393 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 msg(IObuff);
3395 redraw_cmdline = FALSE; /* don't overwrite! */
3396 }
3397
3398 return num_matches;
3399}
3400
3401/*
3402 * Call this while finding completions, to check whether the user has hit a key
3403 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003404 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003406 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 */
3408 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003409ins_compl_check_keys(frequency)
3410 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411{
3412 static int count = 0;
3413
3414 int c;
3415
3416 /* Don't check when reading keys from a script. That would break the test
3417 * scripts */
3418 if (using_script())
3419 return;
3420
3421 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003422 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 return;
3424 count = 0;
3425
3426 ++no_mapping;
3427 c = vpeekc_any();
3428 --no_mapping;
3429 if (c != NUL)
3430 {
3431 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3432 {
3433 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003434 compl_shows_dir = ins_compl_key2dir(c);
3435 (void)ins_compl_next(FALSE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003438 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003440 if (compl_pending && !got_int)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003441 (void)ins_compl_next(FALSE, 1);
3442}
3443
3444/*
3445 * Decide the direction of Insert mode complete from the key typed.
3446 * Returns BACKWARD or FORWARD.
3447 */
3448 static int
3449ins_compl_key2dir(c)
3450 int c;
3451{
3452 if (c == Ctrl_P || c == Ctrl_L || (pum_visible()
3453 && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP)))
3454 return BACKWARD;
3455 return FORWARD;
3456}
3457
3458/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003459 * Return TRUE for keys that are used for completion only when the popup menu
3460 * is visible.
3461 */
3462 static int
3463ins_compl_pum_key(c)
3464 int c;
3465{
3466 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
3467 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN);
3468}
3469
3470/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00003471 * Decide the number of completions to move forward.
3472 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3473 */
3474 static int
3475ins_compl_key2count(c)
3476 int c;
3477{
3478 int h;
3479
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003480 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00003481 {
3482 h = pum_get_height();
3483 if (h > 3)
3484 h -= 2; /* keep some context */
3485 return h;
3486 }
3487 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488}
3489
3490/*
3491 * Do Insert mode completion.
3492 * Called when character "c" was typed, which has a meaning for completion.
3493 * Returns OK if completion was done, FAIL if something failed (out of mem).
3494 */
3495 static int
3496ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003497 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003499 char_u *line;
3500 int startcol = 0; /* column where searched text starts */
3501 colnr_T curs_col; /* cursor column */
3502 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503
Bram Moolenaare3226be2005-12-18 22:10:00 +00003504 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003505 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
3507 /* First time we hit ^N or ^P (in a row, I mean) */
3508
3509 /* Turn off 'sm' so we don't show matches with ^X^L */
3510 save_sm = p_sm;
3511 p_sm = FALSE;
3512
3513 did_ai = FALSE;
3514#ifdef FEAT_SMARTINDENT
3515 did_si = FALSE;
3516 can_si = FALSE;
3517 can_si_back = FALSE;
3518#endif
3519 if (stop_arrow() == FAIL)
3520 return FAIL;
3521
3522 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003523 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
3525 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003526 * "compl_startpos" to the cursor as a pattern to add a new word
3527 * instead of expand the one before the cursor, in word-wise if
3528 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 * is not in the same line as the cursor then fix it (the line has
3530 * been split because it was longer than 'tw'). if SOL is set then
3531 * skip the previous pattern, a word at the beginning of the line has
3532 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003533 if ((compl_cont_status & CONT_INTRPT) && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
3535 /*
3536 * it is a continued search
3537 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003538 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3540 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3541 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003542 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003544 /* line (probably) wrapped, set compl_startpos to the
3545 * first non_blank in the line, if it is not a wordchar
3546 * include it to get a better pattern, but then we don't
3547 * want the "\\<" prefix, check it bellow */
3548 compl_col = (colnr_T)(skipwhite(line) - line);
3549 compl_startpos.col = compl_col;
3550 compl_startpos.lnum = curwin->w_cursor.lnum;
3551 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 }
3553 else
3554 {
3555 /* S_IPOS was set when we inserted a word that was at the
3556 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003557 * mode but first we need to redefine compl_startpos */
3558 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003560 compl_cont_status |= CONT_SOL;
3561 compl_startpos.col = (colnr_T)(skipwhite(
3562 line + compl_length
3563 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003565 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003567 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003568 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 * have enough space? just being paranoic */
3570#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003571 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003573 compl_cont_status &= ~CONT_SOL;
3574 compl_length = (IOSIZE - MIN_SPACE);
3575 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003577 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3578 if (compl_length < 1)
3579 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
3581 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003582 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003584 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
3586 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003587 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003589 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003591 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003593 compl_cont_status = 0;
3594 compl_cont_status |= CONT_N_ADDS;
3595 compl_startpos = curwin->w_cursor;
3596 startcol = (int)curs_col;
3597 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
3599
3600 /* Work out completion pattern and original text -- webb */
3601 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3602 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003603 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3605 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003606 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003608 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003610 compl_col += ++startcol;
3611 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
3613 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003614 compl_pattern = str_foldcase(line + compl_col,
3615 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003617 compl_pattern = vim_strnsave(line + compl_col,
3618 compl_length);
3619 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 return FAIL;
3621 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003622 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
3624 char_u *prefix = (char_u *)"\\<";
3625
3626 /* we need 3 extra chars, 1 for the NUL and
3627 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003628 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3629 compl_length) + 3);
3630 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003632 if (!vim_iswordp(line + compl_col)
3633 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 && (
3635#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003636 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003638 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639#endif
3640 )))
3641 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003642 STRCPY((char *)compl_pattern, prefix);
3643 (void)quote_meta(compl_pattern + STRLEN(prefix),
3644 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003646 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003648 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003650 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651#endif
3652 )
3653 {
3654 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003655 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3656 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003658 compl_col += curs_col;
3659 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 }
3661 else
3662 {
3663#ifdef FEAT_MBYTE
3664 /* Search the point of change class of multibyte character
3665 * or not a word single byte character backward. */
3666 if (has_mbyte)
3667 {
3668 int base_class;
3669 int head_off;
3670
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003671 startcol -= (*mb_head_off)(line, line + startcol);
3672 base_class = mb_get_class(line + startcol);
3673 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003675 head_off = (*mb_head_off)(line, line + startcol);
3676 if (base_class != mb_get_class(line + startcol
3677 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003679 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681 }
3682 else
3683#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003684 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003686 compl_col += ++startcol;
3687 compl_length = (int)curs_col - startcol;
3688 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 {
3690 /* Only match word with at least two chars -- webb
3691 * there's no need to call quote_meta,
3692 * alloc(7) is enough -- Acevedo
3693 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003694 compl_pattern = alloc(7);
3695 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003697 STRCPY((char *)compl_pattern, "\\<");
3698 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3699 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701 else
3702 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003703 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3704 compl_length) + 3);
3705 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003707 STRCPY((char *)compl_pattern, "\\<");
3708 (void)quote_meta(compl_pattern + 2, line + compl_col,
3709 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711 }
3712 }
3713 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3714 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003715 compl_col = skipwhite(line) - line;
3716 compl_length = (int)curs_col - (int)compl_col;
3717 if (compl_length < 0) /* cursor in indent: empty pattern */
3718 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003720 compl_pattern = str_foldcase(line + compl_col, compl_length,
3721 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003723 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3724 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 return FAIL;
3726 }
3727 else if (ctrl_x_mode == CTRL_X_FILES)
3728 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003729 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003731 compl_col += ++startcol;
3732 compl_length = (int)curs_col - startcol;
3733 compl_pattern = addstar(line + compl_col, compl_length,
3734 EXPAND_FILES);
3735 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 return FAIL;
3737 }
3738 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3739 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003740 compl_pattern = vim_strnsave(line, curs_col);
3741 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003743 set_cmd_context(&compl_xp, compl_pattern,
3744 (int)STRLEN(compl_pattern), curs_col);
3745 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3746 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003748 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
3749 compl_col = startcol;
3750 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003752 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003753 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00003754#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003755 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00003756 * Call user defined function 'completefunc' with "a:findstart"
3757 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003758 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00003759 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003760 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003761 char_u *funcname;
3762 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003763
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003764 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00003765 * string */
3766 funcname = ctrl_x_mode == CTRL_X_FUNCTION
3767 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3768 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003769 {
3770 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3771 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003772 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003773 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003774
3775 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003776 args[1] = NULL;
3777 pos = curwin->w_cursor;
3778 col = call_func_retnr(funcname, 2, args, FALSE);
3779 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003780
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003781 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003782 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003783 compl_col = col;
3784 if ((colnr_T)compl_col > curs_col)
3785 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003786
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003787 /* Setup variables for completion. Need to obtain "line" again,
3788 * it may have become invalid. */
3789 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003790 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003791 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3792 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003793#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003794 return FAIL;
3795 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00003796 else if (ctrl_x_mode == CTRL_X_SPELL)
3797 {
3798#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00003799 if (spell_bad_len > 0)
3800 compl_col = curs_col - spell_bad_len;
3801 else
3802 compl_col = spell_word_start(startcol);
3803 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00003804 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003805 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003806 compl_length = (int)curs_col - compl_col;
3807 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3808 if (compl_pattern == NULL)
3809#endif
3810 return FAIL;
3811 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003812 else
3813 {
3814 EMSG2(_(e_intern2), "ins_complete()");
3815 return FAIL;
3816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003818 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 {
3820 edit_submode_pre = (char_u *)_(" Adding");
3821 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3822 {
3823 /* Insert a new line, keep indentation but ignore 'comments' */
3824#ifdef FEAT_COMMENTS
3825 char_u *old = curbuf->b_p_com;
3826
3827 curbuf->b_p_com = (char_u *)"";
3828#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003829 compl_startpos.lnum = curwin->w_cursor.lnum;
3830 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 ins_eol('\r');
3832#ifdef FEAT_COMMENTS
3833 curbuf->b_p_com = old;
3834#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003835 compl_length = 0;
3836 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838 }
3839 else
3840 {
3841 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003842 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 }
3844
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003845 if (compl_cont_status & CONT_LOCAL)
3846 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 else
3848 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
3849
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003851 * "compl_orig_text" itself (not a copy) is added, it will be freed
3852 * when the list of matches is freed. */
3853 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
3854 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
3855 -1, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003857 vim_free(compl_pattern);
3858 compl_pattern = NULL;
3859 vim_free(compl_orig_text);
3860 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 return FAIL;
3862 }
3863
3864 /* showmode might reset the internal line pointers, so it must
3865 * be called before line = ml_get(), or when this address is no
3866 * longer needed. -- Acevedo.
3867 */
3868 edit_submode_extra = (char_u *)_("-- Searching...");
3869 edit_submode_highl = HLF_COUNT;
3870 showmode();
3871 edit_submode_extra = NULL;
3872 out_flush();
3873 }
3874
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003875 compl_shown_match = compl_curr_match;
3876 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877
3878 /*
3879 * Find next match.
3880 */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003881 n = ins_compl_next(TRUE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003883 /* may undisplay the popup menu */
3884 ins_compl_upd_pum();
3885
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003886 if (n > 1) /* all matches have been found */
3887 compl_matches = n;
3888 compl_curr_match = compl_shown_match;
3889 compl_direction = compl_shows_dir;
3890 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
3892 /* eat the ESC to avoid leaving insert mode */
3893 if (got_int && !global_busy)
3894 {
3895 (void)vgetc();
3896 got_int = FALSE;
3897 }
3898
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003899 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003900 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003902 edit_submode_extra = (compl_cont_status & CONT_ADDING)
3903 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
3905 edit_submode_highl = HLF_E;
3906 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
3907 * because we couldn't expand anything at first place, but if we used
3908 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
3909 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003910 if ( compl_length > 1
3911 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 || (ctrl_x_mode != 0
3913 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
3914 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003915 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 }
3917
Bram Moolenaar572cb562005-08-05 21:35:02 +00003918 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003919 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
3923 if (edit_submode_extra == NULL)
3924 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003925 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 {
3927 edit_submode_extra = (char_u *)_("Back at original");
3928 edit_submode_highl = HLF_W;
3929 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003930 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
3932 edit_submode_extra = (char_u *)_("Word from other line");
3933 edit_submode_highl = HLF_COUNT;
3934 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00003935 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 {
3937 edit_submode_extra = (char_u *)_("The only match");
3938 edit_submode_highl = HLF_COUNT;
3939 }
3940 else
3941 {
3942 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003943 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003945 int number = 0;
3946 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003948 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
3950 /* search backwards for the first valid (!= -1) number.
3951 * This should normally succeed already at the first loop
3952 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003953 for (match = compl_curr_match->cp_prev; match != NULL
3954 && match != compl_first_match;
3955 match = match->cp_prev)
3956 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003958 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 break;
3960 }
3961 if (match != NULL)
3962 /* go up and assign all numbers which are not assigned
3963 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003964 for (match = match->cp_next;
3965 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003966 match = match->cp_next)
3967 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 else /* BACKWARD */
3970 {
3971 /* search forwards (upwards) for the first valid (!= -1)
3972 * number. This should normally succeed already at the
3973 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003974 for (match = compl_curr_match->cp_next; match != NULL
3975 && match != compl_first_match;
3976 match = match->cp_next)
3977 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003979 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 break;
3981 }
3982 if (match != NULL)
3983 /* go down and assign all numbers which are not
3984 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003985 for (match = match->cp_prev; match
3986 && match->cp_number == -1;
3987 match = match->cp_prev)
3988 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
3990 }
3991
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003992 /* The match should always have a sequence number now, this is
3993 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003994 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 {
3996 /* Space for 10 text chars. + 2x10-digit no.s */
3997 static char_u match_ref[31];
3998
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003999 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004001 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004003 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004004 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004005 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 edit_submode_extra = match_ref;
4007 edit_submode_highl = HLF_R;
4008 if (dollar_vcol)
4009 curs_columns(FALSE);
4010 }
4011 }
4012 }
4013
4014 /* Show a message about what (completion) mode we're in. */
4015 showmode();
4016 if (edit_submode_extra != NULL)
4017 {
4018 if (!p_smd)
4019 msg_attr(edit_submode_extra,
4020 edit_submode_highl < HLF_COUNT
4021 ? hl_attr(edit_submode_highl) : 0);
4022 }
4023 else
4024 msg_clr_cmdline(); /* necessary for "noshowmode" */
4025
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004026 ins_compl_show_pum();
4027
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 return OK;
4029}
4030
4031/*
4032 * Looks in the first "len" chars. of "src" for search-metachars.
4033 * If dest is not NULL the chars. are copied there quoting (with
4034 * a backslash) the metachars, and dest would be NUL terminated.
4035 * Returns the length (needed) of dest
4036 */
4037 static int
4038quote_meta(dest, src, len)
4039 char_u *dest;
4040 char_u *src;
4041 int len;
4042{
4043 int m;
4044
4045 for (m = len; --len >= 0; src++)
4046 {
4047 switch (*src)
4048 {
4049 case '.':
4050 case '*':
4051 case '[':
4052 if (ctrl_x_mode == CTRL_X_DICTIONARY
4053 || ctrl_x_mode == CTRL_X_THESAURUS)
4054 break;
4055 case '~':
4056 if (!p_magic) /* quote these only if magic is set */
4057 break;
4058 case '\\':
4059 if (ctrl_x_mode == CTRL_X_DICTIONARY
4060 || ctrl_x_mode == CTRL_X_THESAURUS)
4061 break;
4062 case '^': /* currently it's not needed. */
4063 case '$':
4064 m++;
4065 if (dest != NULL)
4066 *dest++ = '\\';
4067 break;
4068 }
4069 if (dest != NULL)
4070 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004071# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 /* Copy remaining bytes of a multibyte character. */
4073 if (has_mbyte)
4074 {
4075 int i, mb_len;
4076
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004077 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 if (mb_len > 0 && len >= mb_len)
4079 for (i = 0; i < mb_len; ++i)
4080 {
4081 --len;
4082 ++src;
4083 if (dest != NULL)
4084 *dest++ = *src;
4085 }
4086 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004087# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 }
4089 if (dest != NULL)
4090 *dest = NUL;
4091
4092 return m;
4093}
4094#endif /* FEAT_INS_EXPAND */
4095
4096/*
4097 * Next character is interpreted literally.
4098 * A one, two or three digit decimal number is interpreted as its byte value.
4099 * If one or two digits are entered, the next character is given to vungetc().
4100 * For Unicode a character > 255 may be returned.
4101 */
4102 int
4103get_literal()
4104{
4105 int cc;
4106 int nc;
4107 int i;
4108 int hex = FALSE;
4109 int octal = FALSE;
4110#ifdef FEAT_MBYTE
4111 int unicode = 0;
4112#endif
4113
4114 if (got_int)
4115 return Ctrl_C;
4116
4117#ifdef FEAT_GUI
4118 /*
4119 * In GUI there is no point inserting the internal code for a special key.
4120 * It is more useful to insert the string "<KEY>" instead. This would
4121 * probably be useful in a text window too, but it would not be
4122 * vi-compatible (maybe there should be an option for it?) -- webb
4123 */
4124 if (gui.in_use)
4125 ++allow_keys;
4126#endif
4127#ifdef USE_ON_FLY_SCROLL
4128 dont_scroll = TRUE; /* disallow scrolling here */
4129#endif
4130 ++no_mapping; /* don't map the next key hits */
4131 cc = 0;
4132 i = 0;
4133 for (;;)
4134 {
4135 do
4136 nc = safe_vgetc();
4137 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4138 || nc == K_HOR_SCROLLBAR);
4139#ifdef FEAT_CMDL_INFO
4140 if (!(State & CMDLINE)
4141# ifdef FEAT_MBYTE
4142 && MB_BYTE2LEN_CHECK(nc) == 1
4143# endif
4144 )
4145 add_to_showcmd(nc);
4146#endif
4147 if (nc == 'x' || nc == 'X')
4148 hex = TRUE;
4149 else if (nc == 'o' || nc == 'O')
4150 octal = TRUE;
4151#ifdef FEAT_MBYTE
4152 else if (nc == 'u' || nc == 'U')
4153 unicode = nc;
4154#endif
4155 else
4156 {
4157 if (hex
4158#ifdef FEAT_MBYTE
4159 || unicode != 0
4160#endif
4161 )
4162 {
4163 if (!vim_isxdigit(nc))
4164 break;
4165 cc = cc * 16 + hex2nr(nc);
4166 }
4167 else if (octal)
4168 {
4169 if (nc < '0' || nc > '7')
4170 break;
4171 cc = cc * 8 + nc - '0';
4172 }
4173 else
4174 {
4175 if (!VIM_ISDIGIT(nc))
4176 break;
4177 cc = cc * 10 + nc - '0';
4178 }
4179
4180 ++i;
4181 }
4182
4183 if (cc > 255
4184#ifdef FEAT_MBYTE
4185 && unicode == 0
4186#endif
4187 )
4188 cc = 255; /* limit range to 0-255 */
4189 nc = 0;
4190
4191 if (hex) /* hex: up to two chars */
4192 {
4193 if (i >= 2)
4194 break;
4195 }
4196#ifdef FEAT_MBYTE
4197 else if (unicode) /* Unicode: up to four or eight chars */
4198 {
4199 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4200 break;
4201 }
4202#endif
4203 else if (i >= 3) /* decimal or octal: up to three chars */
4204 break;
4205 }
4206 if (i == 0) /* no number entered */
4207 {
4208 if (nc == K_ZERO) /* NUL is stored as NL */
4209 {
4210 cc = '\n';
4211 nc = 0;
4212 }
4213 else
4214 {
4215 cc = nc;
4216 nc = 0;
4217 }
4218 }
4219
4220 if (cc == 0) /* NUL is stored as NL */
4221 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004222#ifdef FEAT_MBYTE
4223 if (enc_dbcs && (cc & 0xff) == 0)
4224 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4225 second byte will cause trouble! */
4226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
4228 --no_mapping;
4229#ifdef FEAT_GUI
4230 if (gui.in_use)
4231 --allow_keys;
4232#endif
4233 if (nc)
4234 vungetc(nc);
4235 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4236 return cc;
4237}
4238
4239/*
4240 * Insert character, taking care of special keys and mod_mask
4241 */
4242 static void
4243insert_special(c, allow_modmask, ctrlv)
4244 int c;
4245 int allow_modmask;
4246 int ctrlv; /* c was typed after CTRL-V */
4247{
4248 char_u *p;
4249 int len;
4250
4251 /*
4252 * Special function key, translate into "<Key>". Up to the last '>' is
4253 * inserted with ins_str(), so as not to replace characters in replace
4254 * mode.
4255 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4256 * unless 'allow_modmask' is TRUE.
4257 */
4258#ifdef MACOS
4259 /* Command-key never produces a normal key */
4260 if (mod_mask & MOD_MASK_CMD)
4261 allow_modmask = TRUE;
4262#endif
4263 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4264 {
4265 p = get_special_key_name(c, mod_mask);
4266 len = (int)STRLEN(p);
4267 c = p[len - 1];
4268 if (len > 2)
4269 {
4270 if (stop_arrow() == FAIL)
4271 return;
4272 p[len - 1] = NUL;
4273 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004274 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 ctrlv = FALSE;
4276 }
4277 }
4278 if (stop_arrow() == OK)
4279 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4280}
4281
4282/*
4283 * Special characters in this context are those that need processing other
4284 * than the simple insertion that can be performed here. This includes ESC
4285 * which terminates the insert, and CR/NL which need special processing to
4286 * open up a new line. This routine tries to optimize insertions performed by
4287 * the "redo", "undo" or "put" commands, so it needs to know when it should
4288 * stop and defer processing to the "normal" mechanism.
4289 * '0' and '^' are special, because they can be followed by CTRL-D.
4290 */
4291#ifdef EBCDIC
4292# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4293#else
4294# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4295#endif
4296
4297#ifdef FEAT_MBYTE
4298# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4299#else
4300# define WHITECHAR(cc) vim_iswhite(cc)
4301#endif
4302
4303 void
4304insertchar(c, flags, second_indent)
4305 int c; /* character to insert or NUL */
4306 int flags; /* INSCHAR_FORMAT, etc. */
4307 int second_indent; /* indent for second line if >= 0 */
4308{
4309 int haveto_redraw = FALSE;
4310 int textwidth;
4311#ifdef FEAT_COMMENTS
4312 colnr_T leader_len;
4313 char_u *p;
4314 int no_leader = FALSE;
4315 int do_comments = (flags & INSCHAR_DO_COM);
4316#endif
4317 int fo_white_par;
4318 int first_line = TRUE;
4319 int fo_ins_blank;
4320#ifdef FEAT_MBYTE
4321 int fo_multibyte;
4322#endif
4323 int save_char = NUL;
4324 int cc;
4325
4326 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4327 fo_ins_blank = has_format_option(FO_INS_BLANK);
4328#ifdef FEAT_MBYTE
4329 fo_multibyte = has_format_option(FO_MBYTE_BREAK);
4330#endif
4331 fo_white_par = has_format_option(FO_WHITE_PAR);
4332
4333 /*
4334 * Try to break the line in two or more pieces when:
4335 * - Always do this if we have been called to do formatting only.
4336 * - Always do this when 'formatoptions' has the 'a' flag and the line
4337 * ends in white space.
4338 * - Otherwise:
4339 * - Don't do this if inserting a blank
4340 * - Don't do this if an existing character is being replaced, unless
4341 * we're in VREPLACE mode.
4342 * - Do this if the cursor is not on the line where insert started
4343 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4344 * before the insert.
4345 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4346 * before 'textwidth'
4347 */
4348 if (textwidth
4349 && ((flags & INSCHAR_FORMAT)
4350 || (!vim_iswhite(c)
4351 && !((State & REPLACE_FLAG)
4352#ifdef FEAT_VREPLACE
4353 && !(State & VREPLACE_FLAG)
4354#endif
4355 && *ml_get_cursor() != NUL)
4356 && (curwin->w_cursor.lnum != Insstart.lnum
4357 || ((!has_format_option(FO_INS_LONG)
4358 || Insstart_textlen <= (colnr_T)textwidth)
4359 && (!fo_ins_blank
4360 || Insstart_blank_vcol <= (colnr_T)textwidth
4361 ))))))
4362 {
4363 /*
4364 * When 'ai' is off we don't want a space under the cursor to be
4365 * deleted. Replace it with an 'x' temporarily.
4366 */
4367 if (!curbuf->b_p_ai)
4368 {
4369 cc = gchar_cursor();
4370 if (vim_iswhite(cc))
4371 {
4372 save_char = cc;
4373 pchar_cursor('x');
4374 }
4375 }
4376
4377 /*
4378 * Repeat breaking lines, until the current line is not too long.
4379 */
4380 while (!got_int)
4381 {
4382 int startcol; /* Cursor column at entry */
4383 int wantcol; /* column at textwidth border */
4384 int foundcol; /* column for start of spaces */
4385 int end_foundcol = 0; /* column for start of word */
4386 colnr_T len;
4387 colnr_T virtcol;
4388#ifdef FEAT_VREPLACE
4389 int orig_col = 0;
4390 char_u *saved_text = NULL;
4391#endif
4392 colnr_T col;
4393
4394 virtcol = get_nolist_virtcol();
4395 if (virtcol < (colnr_T)textwidth)
4396 break;
4397
4398#ifdef FEAT_COMMENTS
4399 if (no_leader)
4400 do_comments = FALSE;
4401 else if (!(flags & INSCHAR_FORMAT)
4402 && has_format_option(FO_WRAP_COMS))
4403 do_comments = TRUE;
4404
4405 /* Don't break until after the comment leader */
4406 if (do_comments)
4407 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
4408 else
4409 leader_len = 0;
4410
4411 /* If the line doesn't start with a comment leader, then don't
4412 * start one in a following broken line. Avoids that a %word
4413 * moved to the start of the next line causes all following lines
4414 * to start with %. */
4415 if (leader_len == 0)
4416 no_leader = TRUE;
4417#endif
4418 if (!(flags & INSCHAR_FORMAT)
4419#ifdef FEAT_COMMENTS
4420 && leader_len == 0
4421#endif
4422 && !has_format_option(FO_WRAP))
4423
4424 {
4425 textwidth = 0;
4426 break;
4427 }
4428 if ((startcol = curwin->w_cursor.col) == 0)
4429 break;
4430
4431 /* find column of textwidth border */
4432 coladvance((colnr_T)textwidth);
4433 wantcol = curwin->w_cursor.col;
4434
4435 curwin->w_cursor.col = startcol - 1;
4436#ifdef FEAT_MBYTE
4437 /* Correct cursor for multi-byte character. */
4438 if (has_mbyte)
4439 mb_adjust_cursor();
4440#endif
4441 foundcol = 0;
4442
4443 /*
4444 * Find position to break at.
4445 * Stop at first entered white when 'formatoptions' has 'v'
4446 */
4447 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
4448 || curwin->w_cursor.lnum != Insstart.lnum
4449 || curwin->w_cursor.col >= Insstart.col)
4450 {
4451 cc = gchar_cursor();
4452 if (WHITECHAR(cc))
4453 {
4454 /* remember position of blank just before text */
4455 end_foundcol = curwin->w_cursor.col;
4456
4457 /* find start of sequence of blanks */
4458 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
4459 {
4460 dec_cursor();
4461 cc = gchar_cursor();
4462 }
4463 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
4464 break; /* only spaces in front of text */
4465#ifdef FEAT_COMMENTS
4466 /* Don't break until after the comment leader */
4467 if (curwin->w_cursor.col < leader_len)
4468 break;
4469#endif
4470 if (has_format_option(FO_ONE_LETTER))
4471 {
4472 /* do not break after one-letter words */
4473 if (curwin->w_cursor.col == 0)
4474 break; /* one-letter word at begin */
4475
4476 col = curwin->w_cursor.col;
4477 dec_cursor();
4478 cc = gchar_cursor();
4479
4480 if (WHITECHAR(cc))
4481 continue; /* one-letter, continue */
4482 curwin->w_cursor.col = col;
4483 }
4484#ifdef FEAT_MBYTE
4485 if (has_mbyte)
4486 foundcol = curwin->w_cursor.col
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004487 + (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 else
4489#endif
4490 foundcol = curwin->w_cursor.col + 1;
4491 if (curwin->w_cursor.col < (colnr_T)wantcol)
4492 break;
4493 }
4494#ifdef FEAT_MBYTE
4495 else if (cc >= 0x100 && fo_multibyte
4496 && curwin->w_cursor.col <= (colnr_T)wantcol)
4497 {
4498 /* Break after or before a multi-byte character. */
4499 foundcol = curwin->w_cursor.col;
4500 if (curwin->w_cursor.col < (colnr_T)wantcol)
4501 foundcol += (*mb_char2len)(cc);
4502 end_foundcol = foundcol;
4503 break;
4504 }
4505#endif
4506 if (curwin->w_cursor.col == 0)
4507 break;
4508 dec_cursor();
4509 }
4510
4511 if (foundcol == 0) /* no spaces, cannot break line */
4512 {
4513 curwin->w_cursor.col = startcol;
4514 break;
4515 }
4516
4517 /* Going to break the line, remove any "$" now. */
4518 undisplay_dollar();
4519
4520 /*
4521 * Offset between cursor position and line break is used by replace
4522 * stack functions. VREPLACE does not use this, and backspaces
4523 * over the text instead.
4524 */
4525#ifdef FEAT_VREPLACE
4526 if (State & VREPLACE_FLAG)
4527 orig_col = startcol; /* Will start backspacing from here */
4528 else
4529#endif
4530 replace_offset = startcol - end_foundcol - 1;
4531
4532 /*
4533 * adjust startcol for spaces that will be deleted and
4534 * characters that will remain on top line
4535 */
4536 curwin->w_cursor.col = foundcol;
4537 while (cc = gchar_cursor(), WHITECHAR(cc))
4538 inc_cursor();
4539 startcol -= curwin->w_cursor.col;
4540 if (startcol < 0)
4541 startcol = 0;
4542
4543#ifdef FEAT_VREPLACE
4544 if (State & VREPLACE_FLAG)
4545 {
4546 /*
4547 * In VREPLACE mode, we will backspace over the text to be
4548 * wrapped, so save a copy now to put on the next line.
4549 */
4550 saved_text = vim_strsave(ml_get_cursor());
4551 curwin->w_cursor.col = orig_col;
4552 if (saved_text == NULL)
4553 break; /* Can't do it, out of memory */
4554 saved_text[startcol] = NUL;
4555
4556 /* Backspace over characters that will move to the next line */
4557 if (!fo_white_par)
4558 backspace_until_column(foundcol);
4559 }
4560 else
4561#endif
4562 {
4563 /* put cursor after pos. to break line */
4564 if (!fo_white_par)
4565 curwin->w_cursor.col = foundcol;
4566 }
4567
4568 /*
4569 * Split the line just before the margin.
4570 * Only insert/delete lines, but don't really redraw the window.
4571 */
4572 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
4573 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
4574#ifdef FEAT_COMMENTS
4575 + (do_comments ? OPENLINE_DO_COM : 0)
4576#endif
4577 , old_indent);
4578 old_indent = 0;
4579
4580 replace_offset = 0;
4581 if (first_line)
4582 {
4583 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
4584 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
4585 if (second_indent >= 0)
4586 {
4587#ifdef FEAT_VREPLACE
4588 if (State & VREPLACE_FLAG)
4589 change_indent(INDENT_SET, second_indent, FALSE, NUL);
4590 else
4591#endif
4592 (void)set_indent(second_indent, SIN_CHANGED);
4593 }
4594 first_line = FALSE;
4595 }
4596
4597#ifdef FEAT_VREPLACE
4598 if (State & VREPLACE_FLAG)
4599 {
4600 /*
4601 * In VREPLACE mode we have backspaced over the text to be
4602 * moved, now we re-insert it into the new line.
4603 */
4604 ins_bytes(saved_text);
4605 vim_free(saved_text);
4606 }
4607 else
4608#endif
4609 {
4610 /*
4611 * Check if cursor is not past the NUL off the line, cindent
4612 * may have added or removed indent.
4613 */
4614 curwin->w_cursor.col += startcol;
4615 len = (colnr_T)STRLEN(ml_get_curline());
4616 if (curwin->w_cursor.col > len)
4617 curwin->w_cursor.col = len;
4618 }
4619
4620 haveto_redraw = TRUE;
4621#ifdef FEAT_CINDENT
4622 can_cindent = TRUE;
4623#endif
4624 /* moved the cursor, don't autoindent or cindent now */
4625 did_ai = FALSE;
4626#ifdef FEAT_SMARTINDENT
4627 did_si = FALSE;
4628 can_si = FALSE;
4629 can_si_back = FALSE;
4630#endif
4631 line_breakcheck();
4632 }
4633
4634 if (save_char) /* put back space after cursor */
4635 pchar_cursor(save_char);
4636
4637 if (c == NUL) /* formatting only */
4638 return;
4639 if (haveto_redraw)
4640 {
4641 update_topline();
4642 redraw_curbuf_later(VALID);
4643 }
4644 }
4645 if (c == NUL) /* only formatting was wanted */
4646 return;
4647
4648#ifdef FEAT_COMMENTS
4649 /* Check whether this character should end a comment. */
4650 if (did_ai && (int)c == end_comment_pending)
4651 {
4652 char_u *line;
4653 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4654 int middle_len, end_len;
4655 int i;
4656
4657 /*
4658 * Need to remove existing (middle) comment leader and insert end
4659 * comment leader. First, check what comment leader we can find.
4660 */
4661 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4662 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4663 {
4664 /* Skip middle-comment string */
4665 while (*p && p[-1] != ':') /* find end of middle flags */
4666 ++p;
4667 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4668 /* Don't count trailing white space for middle_len */
4669 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4670 --middle_len;
4671
4672 /* Find the end-comment string */
4673 while (*p && p[-1] != ':') /* find end of end flags */
4674 ++p;
4675 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4676
4677 /* Skip white space before the cursor */
4678 i = curwin->w_cursor.col;
4679 while (--i >= 0 && vim_iswhite(line[i]))
4680 ;
4681 i++;
4682
4683 /* Skip to before the middle leader */
4684 i -= middle_len;
4685
4686 /* Check some expected things before we go on */
4687 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4688 {
4689 /* Backspace over all the stuff we want to replace */
4690 backspace_until_column(i);
4691
4692 /*
4693 * Insert the end-comment string, except for the last
4694 * character, which will get inserted as normal later.
4695 */
4696 ins_bytes_len(lead_end, end_len - 1);
4697 }
4698 }
4699 }
4700 end_comment_pending = NUL;
4701#endif
4702
4703 did_ai = FALSE;
4704#ifdef FEAT_SMARTINDENT
4705 did_si = FALSE;
4706 can_si = FALSE;
4707 can_si_back = FALSE;
4708#endif
4709
4710 /*
4711 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4712 * This speeds up normal text input considerably.
4713 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4714 * need to re-indent at a ':', or any other character (but not what
4715 * 'paste' is set)..
4716 */
4717#ifdef USE_ON_FLY_SCROLL
4718 dont_scroll = FALSE; /* allow scrolling here */
4719#endif
4720
4721 if ( !ISSPECIAL(c)
4722#ifdef FEAT_MBYTE
4723 && (!has_mbyte || (*mb_char2len)(c) == 1)
4724#endif
4725 && vpeekc() != NUL
4726 && !(State & REPLACE_FLAG)
4727#ifdef FEAT_CINDENT
4728 && !cindent_on()
4729#endif
4730#ifdef FEAT_RIGHTLEFT
4731 && !p_ri
4732#endif
4733 )
4734 {
4735#define INPUT_BUFLEN 100
4736 char_u buf[INPUT_BUFLEN + 1];
4737 int i;
4738 colnr_T virtcol = 0;
4739
4740 buf[0] = c;
4741 i = 1;
4742 if (textwidth)
4743 virtcol = get_nolist_virtcol();
4744 /*
4745 * Stop the string when:
4746 * - no more chars available
4747 * - finding a special character (command key)
4748 * - buffer is full
4749 * - running into the 'textwidth' boundary
4750 * - need to check for abbreviation: A non-word char after a word-char
4751 */
4752 while ( (c = vpeekc()) != NUL
4753 && !ISSPECIAL(c)
4754#ifdef FEAT_MBYTE
4755 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4756#endif
4757 && i < INPUT_BUFLEN
4758 && (textwidth == 0
4759 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4760 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4761 {
4762#ifdef FEAT_RIGHTLEFT
4763 c = vgetc();
4764 if (p_hkmap && KeyTyped)
4765 c = hkmap(c); /* Hebrew mode mapping */
4766# ifdef FEAT_FKMAP
4767 if (p_fkmap && KeyTyped)
4768 c = fkmap(c); /* Farsi mode mapping */
4769# endif
4770 buf[i++] = c;
4771#else
4772 buf[i++] = vgetc();
4773#endif
4774 }
4775
4776#ifdef FEAT_DIGRAPHS
4777 do_digraph(-1); /* clear digraphs */
4778 do_digraph(buf[i-1]); /* may be the start of a digraph */
4779#endif
4780 buf[i] = NUL;
4781 ins_str(buf);
4782 if (flags & INSCHAR_CTRLV)
4783 {
4784 redo_literal(*buf);
4785 i = 1;
4786 }
4787 else
4788 i = 0;
4789 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004790 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 else
4793 {
4794#ifdef FEAT_MBYTE
4795 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
4796 {
4797 char_u buf[MB_MAXBYTES + 1];
4798
4799 (*mb_char2bytes)(c, buf);
4800 buf[cc] = NUL;
4801 ins_char_bytes(buf, cc);
4802 AppendCharToRedobuff(c);
4803 }
4804 else
4805#endif
4806 {
4807 ins_char(c);
4808 if (flags & INSCHAR_CTRLV)
4809 redo_literal(c);
4810 else
4811 AppendCharToRedobuff(c);
4812 }
4813 }
4814}
4815
4816/*
4817 * Called after inserting or deleting text: When 'formatoptions' includes the
4818 * 'a' flag format from the current line until the end of the paragraph.
4819 * Keep the cursor at the same position relative to the text.
4820 * The caller must have saved the cursor line for undo, following ones will be
4821 * saved here.
4822 */
4823 void
4824auto_format(trailblank, prev_line)
4825 int trailblank; /* when TRUE also format with trailing blank */
4826 int prev_line; /* may start in previous line */
4827{
4828 pos_T pos;
4829 colnr_T len;
4830 char_u *old;
4831 char_u *new, *pnew;
4832 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004833 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
4835 if (!has_format_option(FO_AUTO))
4836 return;
4837
4838 pos = curwin->w_cursor;
4839 old = ml_get_curline();
4840
4841 /* may remove added space */
4842 check_auto_format(FALSE);
4843
4844 /* Don't format in Insert mode when the cursor is on a trailing blank, the
4845 * user might insert normal text next. Also skip formatting when "1" is
4846 * in 'formatoptions' and there is a single character before the cursor.
4847 * Otherwise the line would be broken and when typing another non-white
4848 * next they are not joined back together. */
4849 wasatend = (pos.col == STRLEN(old));
4850 if (*old != NUL && !trailblank && wasatend)
4851 {
4852 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004853 cc = gchar_cursor();
4854 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
4855 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004857 cc = gchar_cursor();
4858 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 {
4860 curwin->w_cursor = pos;
4861 return;
4862 }
4863 curwin->w_cursor = pos;
4864 }
4865
4866#ifdef FEAT_COMMENTS
4867 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
4868 * comments. */
4869 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
4870 && get_leader_len(old, NULL, FALSE) == 0)
4871 return;
4872#endif
4873
4874 /*
4875 * May start formatting in a previous line, so that after "x" a word is
4876 * moved to the previous line if it fits there now. Only when this is not
4877 * the start of a paragraph.
4878 */
4879 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
4880 {
4881 --curwin->w_cursor.lnum;
4882 if (u_save_cursor() == FAIL)
4883 return;
4884 }
4885
4886 /*
4887 * Do the formatting and restore the cursor position. "saved_cursor" will
4888 * be adjusted for the text formatting.
4889 */
4890 saved_cursor = pos;
4891 format_lines((linenr_T)-1);
4892 curwin->w_cursor = saved_cursor;
4893 saved_cursor.lnum = 0;
4894
4895 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4896 {
4897 /* "cannot happen" */
4898 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4899 coladvance((colnr_T)MAXCOL);
4900 }
4901 else
4902 check_cursor_col();
4903
4904 /* Insert mode: If the cursor is now after the end of the line while it
4905 * previously wasn't, the line was broken. Because of the rule above we
4906 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
4907 * formatted. */
4908 if (!wasatend && has_format_option(FO_WHITE_PAR))
4909 {
4910 new = ml_get_curline();
4911 len = STRLEN(new);
4912 if (curwin->w_cursor.col == len)
4913 {
4914 pnew = vim_strnsave(new, len + 2);
4915 pnew[len] = ' ';
4916 pnew[len + 1] = NUL;
4917 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
4918 /* remove the space later */
4919 did_add_space = TRUE;
4920 }
4921 else
4922 /* may remove added space */
4923 check_auto_format(FALSE);
4924 }
4925
4926 check_cursor();
4927}
4928
4929/*
4930 * When an extra space was added to continue a paragraph for auto-formatting,
4931 * delete it now. The space must be under the cursor, just after the insert
4932 * position.
4933 */
4934 static void
4935check_auto_format(end_insert)
4936 int end_insert; /* TRUE when ending Insert mode */
4937{
4938 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004939 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
4941 if (did_add_space)
4942 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004943 cc = gchar_cursor();
4944 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 /* Somehow the space was removed already. */
4946 did_add_space = FALSE;
4947 else
4948 {
4949 if (!end_insert)
4950 {
4951 inc_cursor();
4952 c = gchar_cursor();
4953 dec_cursor();
4954 }
4955 if (c != NUL)
4956 {
4957 /* The space is no longer at the end of the line, delete it. */
4958 del_char(FALSE);
4959 did_add_space = FALSE;
4960 }
4961 }
4962 }
4963}
4964
4965/*
4966 * Find out textwidth to be used for formatting:
4967 * if 'textwidth' option is set, use it
4968 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
4969 * if invalid value, use 0.
4970 * Set default to window width (maximum 79) for "gq" operator.
4971 */
4972 int
4973comp_textwidth(ff)
4974 int ff; /* force formatting (for "Q" command) */
4975{
4976 int textwidth;
4977
4978 textwidth = curbuf->b_p_tw;
4979 if (textwidth == 0 && curbuf->b_p_wm)
4980 {
4981 /* The width is the window width minus 'wrapmargin' minus all the
4982 * things that add to the margin. */
4983 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
4984#ifdef FEAT_CMDWIN
4985 if (cmdwin_type != 0)
4986 textwidth -= 1;
4987#endif
4988#ifdef FEAT_FOLDING
4989 textwidth -= curwin->w_p_fdc;
4990#endif
4991#ifdef FEAT_SIGNS
4992 if (curwin->w_buffer->b_signlist != NULL
4993# ifdef FEAT_NETBEANS_INTG
4994 || usingNetbeans
4995# endif
4996 )
4997 textwidth -= 1;
4998#endif
4999 if (curwin->w_p_nu)
5000 textwidth -= 8;
5001 }
5002 if (textwidth < 0)
5003 textwidth = 0;
5004 if (ff && textwidth == 0)
5005 {
5006 textwidth = W_WIDTH(curwin) - 1;
5007 if (textwidth > 79)
5008 textwidth = 79;
5009 }
5010 return textwidth;
5011}
5012
5013/*
5014 * Put a character in the redo buffer, for when just after a CTRL-V.
5015 */
5016 static void
5017redo_literal(c)
5018 int c;
5019{
5020 char_u buf[10];
5021
5022 /* Only digits need special treatment. Translate them into a string of
5023 * three digits. */
5024 if (VIM_ISDIGIT(c))
5025 {
5026 sprintf((char *)buf, "%03d", c);
5027 AppendToRedobuff(buf);
5028 }
5029 else
5030 AppendCharToRedobuff(c);
5031}
5032
5033/*
5034 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005035 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 */
5037 static void
5038start_arrow(end_insert_pos)
5039 pos_T *end_insert_pos;
5040{
5041 if (!arrow_used) /* something has been inserted */
5042 {
5043 AppendToRedobuff(ESC_STR);
5044 stop_insert(end_insert_pos, FALSE);
5045 arrow_used = TRUE; /* this means we stopped the current insert */
5046 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005047#ifdef FEAT_SYN_HL
5048 check_spell_redraw();
5049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050}
5051
Bram Moolenaar217ad922005-03-20 22:37:15 +00005052#ifdef FEAT_SYN_HL
5053/*
5054 * If we skipped highlighting word at cursor, do it now.
5055 * It may be skipped again, thus reset spell_redraw_lnum first.
5056 */
5057 static void
5058check_spell_redraw()
5059{
5060 if (spell_redraw_lnum != 0)
5061 {
5062 linenr_T lnum = spell_redraw_lnum;
5063
5064 spell_redraw_lnum = 0;
5065 redrawWinline(lnum, FALSE);
5066 }
5067}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005068
5069/*
5070 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5071 * spelled word, if there is one.
5072 */
5073 static void
5074spell_back_to_badword()
5075{
5076 pos_T tpos = curwin->w_cursor;
5077
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005078 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005079 if (curwin->w_cursor.col != tpos.col)
5080 start_arrow(&tpos);
5081}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005082#endif
5083
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084/*
5085 * stop_arrow() is called before a change is made in insert mode.
5086 * If an arrow key has been used, start a new insertion.
5087 * Returns FAIL if undo is impossible, shouldn't insert then.
5088 */
5089 int
5090stop_arrow()
5091{
5092 if (arrow_used)
5093 {
5094 if (u_save_cursor() == OK)
5095 {
5096 arrow_used = FALSE;
5097 ins_need_undo = FALSE;
5098 }
5099 Insstart = curwin->w_cursor; /* new insertion starts here */
5100 Insstart_textlen = linetabsize(ml_get_curline());
5101 ai_col = 0;
5102#ifdef FEAT_VREPLACE
5103 if (State & VREPLACE_FLAG)
5104 {
5105 orig_line_count = curbuf->b_ml.ml_line_count;
5106 vr_lines_changed = 1;
5107 }
5108#endif
5109 ResetRedobuff();
5110 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005111 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 }
5113 else if (ins_need_undo)
5114 {
5115 if (u_save_cursor() == OK)
5116 ins_need_undo = FALSE;
5117 }
5118
5119#ifdef FEAT_FOLDING
5120 /* Always open fold at the cursor line when inserting something. */
5121 foldOpenCursor();
5122#endif
5123
5124 return (arrow_used || ins_need_undo ? FAIL : OK);
5125}
5126
5127/*
5128 * do a few things to stop inserting
5129 */
5130 static void
5131stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005132 pos_T *end_insert_pos; /* where insert ended */
5133 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005135 int cc;
5136 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137
5138 stop_redo_ins();
5139 replace_flush(); /* abandon replace stack */
5140
5141 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005142 * Save the inserted text for later redo with ^@ and CTRL-A.
5143 * Don't do it when "restart_edit" was set and nothing was inserted,
5144 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005146 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005147 if (did_restart_edit == 0 || (ptr != NULL
5148 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005149 {
5150 vim_free(last_insert);
5151 last_insert = ptr;
5152 last_insert_skip = new_insert_skip;
5153 }
5154 else
5155 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
5157 if (!arrow_used)
5158 {
5159 /* Auto-format now. It may seem strange to do this when stopping an
5160 * insertion (or moving the cursor), but it's required when appending
5161 * a line and having it end in a space. But only do it when something
5162 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005163 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005165 pos_T tpos = curwin->w_cursor;
5166
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 /* When the cursor is at the end of the line after a space the
5168 * formatting will move it to the following word. Avoid that by
5169 * moving the cursor onto the space. */
5170 cc = 'x';
5171 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5172 {
5173 dec_cursor();
5174 cc = gchar_cursor();
5175 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005176 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178
5179 auto_format(TRUE, FALSE);
5180
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005181 if (vim_iswhite(cc))
5182 {
5183 if (gchar_cursor() != NUL)
5184 inc_cursor();
5185#ifdef FEAT_VIRTUALEDIT
5186 /* If the cursor is still at the same character, also keep
5187 * the "coladd". */
5188 if (gchar_cursor() == NUL
5189 && curwin->w_cursor.lnum == tpos.lnum
5190 && curwin->w_cursor.col == tpos.col)
5191 curwin->w_cursor.coladd = tpos.coladd;
5192#endif
5193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195
5196 /* If a space was inserted for auto-formatting, remove it now. */
5197 check_auto_format(TRUE);
5198
5199 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005200 * of the line, and put the cursor back.
5201 * Do this when ESC was used or moving the cursor up/down. */
5202 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5203 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005205 pos_T tpos = curwin->w_cursor;
5206
5207 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5209 --curwin->w_cursor.col;
5210 while (cc = gchar_cursor(), vim_iswhite(cc))
5211 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005212 if (curwin->w_cursor.lnum != tpos.lnum)
5213 curwin->w_cursor = tpos;
5214 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5216
5217#ifdef FEAT_VISUAL
5218 /* <C-S-Right> may have started Visual mode, adjust the position for
5219 * deleted characters. */
5220 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5221 {
5222 cc = STRLEN(ml_get_curline());
5223 if (VIsual.col > (colnr_T)cc)
5224 {
5225 VIsual.col = cc;
5226# ifdef FEAT_VIRTUALEDIT
5227 VIsual.coladd = 0;
5228# endif
5229 }
5230 }
5231#endif
5232 }
5233 }
5234 did_ai = FALSE;
5235#ifdef FEAT_SMARTINDENT
5236 did_si = FALSE;
5237 can_si = FALSE;
5238 can_si_back = FALSE;
5239#endif
5240
5241 /* set '[ and '] to the inserted text */
5242 curbuf->b_op_start = Insstart;
5243 curbuf->b_op_end = *end_insert_pos;
5244}
5245
5246/*
5247 * Set the last inserted text to a single character.
5248 * Used for the replace command.
5249 */
5250 void
5251set_last_insert(c)
5252 int c;
5253{
5254 char_u *s;
5255
5256 vim_free(last_insert);
5257#ifdef FEAT_MBYTE
5258 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5259#else
5260 last_insert = alloc(6);
5261#endif
5262 if (last_insert != NULL)
5263 {
5264 s = last_insert;
5265 /* Use the CTRL-V only when entering a special char */
5266 if (c < ' ' || c == DEL)
5267 *s++ = Ctrl_V;
5268 s = add_char2buf(c, s);
5269 *s++ = ESC;
5270 *s++ = NUL;
5271 last_insert_skip = 0;
5272 }
5273}
5274
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005275#if defined(EXITFREE) || defined(PROTO)
5276 void
5277free_last_insert()
5278{
5279 vim_free(last_insert);
5280 last_insert = NULL;
5281}
5282#endif
5283
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284/*
5285 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5286 * and CSI. Handle multi-byte characters.
5287 * Returns a pointer to after the added bytes.
5288 */
5289 char_u *
5290add_char2buf(c, s)
5291 int c;
5292 char_u *s;
5293{
5294#ifdef FEAT_MBYTE
5295 char_u temp[MB_MAXBYTES];
5296 int i;
5297 int len;
5298
5299 len = (*mb_char2bytes)(c, temp);
5300 for (i = 0; i < len; ++i)
5301 {
5302 c = temp[i];
5303#endif
5304 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5305 if (c == K_SPECIAL)
5306 {
5307 *s++ = K_SPECIAL;
5308 *s++ = KS_SPECIAL;
5309 *s++ = KE_FILLER;
5310 }
5311#ifdef FEAT_GUI
5312 else if (c == CSI)
5313 {
5314 *s++ = CSI;
5315 *s++ = KS_EXTRA;
5316 *s++ = (int)KE_CSI;
5317 }
5318#endif
5319 else
5320 *s++ = c;
5321#ifdef FEAT_MBYTE
5322 }
5323#endif
5324 return s;
5325}
5326
5327/*
5328 * move cursor to start of line
5329 * if flags & BL_WHITE move to first non-white
5330 * if flags & BL_SOL move to first non-white if startofline is set,
5331 * otherwise keep "curswant" column
5332 * if flags & BL_FIX don't leave the cursor on a NUL.
5333 */
5334 void
5335beginline(flags)
5336 int flags;
5337{
5338 if ((flags & BL_SOL) && !p_sol)
5339 coladvance(curwin->w_curswant);
5340 else
5341 {
5342 curwin->w_cursor.col = 0;
5343#ifdef FEAT_VIRTUALEDIT
5344 curwin->w_cursor.coladd = 0;
5345#endif
5346
5347 if (flags & (BL_WHITE | BL_SOL))
5348 {
5349 char_u *ptr;
5350
5351 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5352 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5353 ++curwin->w_cursor.col;
5354 }
5355 curwin->w_set_curswant = TRUE;
5356 }
5357}
5358
5359/*
5360 * oneright oneleft cursor_down cursor_up
5361 *
5362 * Move one char {right,left,down,up}.
5363 * Doesn't move onto the NUL past the end of the line.
5364 * Return OK when successful, FAIL when we hit a line of file boundary.
5365 */
5366
5367 int
5368oneright()
5369{
5370 char_u *ptr;
5371#ifdef FEAT_MBYTE
5372 int l;
5373#endif
5374
5375#ifdef FEAT_VIRTUALEDIT
5376 if (virtual_active())
5377 {
5378 pos_T prevpos = curwin->w_cursor;
5379
5380 /* Adjust for multi-wide char (excluding TAB) */
5381 ptr = ml_get_cursor();
5382 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5383#ifdef FEAT_MBYTE
5384 (*mb_ptr2char)(ptr)
5385#else
5386 *ptr
5387#endif
5388 ))
5389 ? ptr2cells(ptr) : 1));
5390 curwin->w_set_curswant = TRUE;
5391 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5392 return (prevpos.col != curwin->w_cursor.col
5393 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5394 }
5395#endif
5396
5397 ptr = ml_get_cursor();
5398#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005399 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 {
5401 /* The character under the cursor is a multi-byte character, move
5402 * several bytes right, but don't end up on the NUL. */
5403 if (ptr[l] == NUL)
5404 return FAIL;
5405 curwin->w_cursor.col += l;
5406 }
5407 else
5408#endif
5409 {
5410 if (*ptr++ == NUL || *ptr == NUL)
5411 return FAIL;
5412 ++curwin->w_cursor.col;
5413 }
5414
5415 curwin->w_set_curswant = TRUE;
5416 return OK;
5417}
5418
5419 int
5420oneleft()
5421{
5422#ifdef FEAT_VIRTUALEDIT
5423 if (virtual_active())
5424 {
5425 int width;
5426 int v = getviscol();
5427
5428 if (v == 0)
5429 return FAIL;
5430
5431# ifdef FEAT_LINEBREAK
5432 /* We might get stuck on 'showbreak', skip over it. */
5433 width = 1;
5434 for (;;)
5435 {
5436 coladvance(v - width);
5437 /* getviscol() is slow, skip it when 'showbreak' is empty and
5438 * there are no multi-byte characters */
5439 if ((*p_sbr == NUL
5440# ifdef FEAT_MBYTE
5441 && !has_mbyte
5442# endif
5443 ) || getviscol() < v)
5444 break;
5445 ++width;
5446 }
5447# else
5448 coladvance(v - 1);
5449# endif
5450
5451 if (curwin->w_cursor.coladd == 1)
5452 {
5453 char_u *ptr;
5454
5455 /* Adjust for multi-wide char (not a TAB) */
5456 ptr = ml_get_cursor();
5457 if (*ptr != TAB && vim_isprintc(
5458# ifdef FEAT_MBYTE
5459 (*mb_ptr2char)(ptr)
5460# else
5461 *ptr
5462# endif
5463 ) && ptr2cells(ptr) > 1)
5464 curwin->w_cursor.coladd = 0;
5465 }
5466
5467 curwin->w_set_curswant = TRUE;
5468 return OK;
5469 }
5470#endif
5471
5472 if (curwin->w_cursor.col == 0)
5473 return FAIL;
5474
5475 curwin->w_set_curswant = TRUE;
5476 --curwin->w_cursor.col;
5477
5478#ifdef FEAT_MBYTE
5479 /* if the character on the left of the current cursor is a multi-byte
5480 * character, move to its first byte */
5481 if (has_mbyte)
5482 mb_adjust_cursor();
5483#endif
5484 return OK;
5485}
5486
5487 int
5488cursor_up(n, upd_topline)
5489 long n;
5490 int upd_topline; /* When TRUE: update topline */
5491{
5492 linenr_T lnum;
5493
5494 if (n > 0)
5495 {
5496 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005497 /* This fails if the cursor is already in the first line or the count
5498 * is larger than the line number and '-' is in 'cpoptions' */
5499 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 return FAIL;
5501 if (n >= lnum)
5502 lnum = 1;
5503 else
5504#ifdef FEAT_FOLDING
5505 if (hasAnyFolding(curwin))
5506 {
5507 /*
5508 * Count each sequence of folded lines as one logical line.
5509 */
5510 /* go to the the start of the current fold */
5511 (void)hasFolding(lnum, &lnum, NULL);
5512
5513 while (n--)
5514 {
5515 /* move up one line */
5516 --lnum;
5517 if (lnum <= 1)
5518 break;
5519 /* If we entered a fold, move to the beginning, unless in
5520 * Insert mode or when 'foldopen' contains "all": it will open
5521 * in a moment. */
5522 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
5523 (void)hasFolding(lnum, &lnum, NULL);
5524 }
5525 if (lnum < 1)
5526 lnum = 1;
5527 }
5528 else
5529#endif
5530 lnum -= n;
5531 curwin->w_cursor.lnum = lnum;
5532 }
5533
5534 /* try to advance to the column we want to be at */
5535 coladvance(curwin->w_curswant);
5536
5537 if (upd_topline)
5538 update_topline(); /* make sure curwin->w_topline is valid */
5539
5540 return OK;
5541}
5542
5543/*
5544 * Cursor down a number of logical lines.
5545 */
5546 int
5547cursor_down(n, upd_topline)
5548 long n;
5549 int upd_topline; /* When TRUE: update topline */
5550{
5551 linenr_T lnum;
5552
5553 if (n > 0)
5554 {
5555 lnum = curwin->w_cursor.lnum;
5556#ifdef FEAT_FOLDING
5557 /* Move to last line of fold, will fail if it's the end-of-file. */
5558 (void)hasFolding(lnum, NULL, &lnum);
5559#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00005560 /* This fails if the cursor is already in the last line or would move
5561 * beyound the last line and '-' is in 'cpoptions' */
5562 if (lnum >= curbuf->b_ml.ml_line_count
5563 || (lnum + n > curbuf->b_ml.ml_line_count
5564 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 return FAIL;
5566 if (lnum + n >= curbuf->b_ml.ml_line_count)
5567 lnum = curbuf->b_ml.ml_line_count;
5568 else
5569#ifdef FEAT_FOLDING
5570 if (hasAnyFolding(curwin))
5571 {
5572 linenr_T last;
5573
5574 /* count each sequence of folded lines as one logical line */
5575 while (n--)
5576 {
5577 if (hasFolding(lnum, NULL, &last))
5578 lnum = last + 1;
5579 else
5580 ++lnum;
5581 if (lnum >= curbuf->b_ml.ml_line_count)
5582 break;
5583 }
5584 if (lnum > curbuf->b_ml.ml_line_count)
5585 lnum = curbuf->b_ml.ml_line_count;
5586 }
5587 else
5588#endif
5589 lnum += n;
5590 curwin->w_cursor.lnum = lnum;
5591 }
5592
5593 /* try to advance to the column we want to be at */
5594 coladvance(curwin->w_curswant);
5595
5596 if (upd_topline)
5597 update_topline(); /* make sure curwin->w_topline is valid */
5598
5599 return OK;
5600}
5601
5602/*
5603 * Stuff the last inserted text in the read buffer.
5604 * Last_insert actually is a copy of the redo buffer, so we
5605 * first have to remove the command.
5606 */
5607 int
5608stuff_inserted(c, count, no_esc)
5609 int c; /* Command character to be inserted */
5610 long count; /* Repeat this many times */
5611 int no_esc; /* Don't add an ESC at the end */
5612{
5613 char_u *esc_ptr;
5614 char_u *ptr;
5615 char_u *last_ptr;
5616 char_u last = NUL;
5617
5618 ptr = get_last_insert();
5619 if (ptr == NULL)
5620 {
5621 EMSG(_(e_noinstext));
5622 return FAIL;
5623 }
5624
5625 /* may want to stuff the command character, to start Insert mode */
5626 if (c != NUL)
5627 stuffcharReadbuff(c);
5628 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
5629 *esc_ptr = NUL; /* remove the ESC */
5630
5631 /* when the last char is either "0" or "^" it will be quoted if no ESC
5632 * comes after it OR if it will inserted more than once and "ptr"
5633 * starts with ^D. -- Acevedo
5634 */
5635 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
5636 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
5637 && (no_esc || (*ptr == Ctrl_D && count > 1)))
5638 {
5639 last = *last_ptr;
5640 *last_ptr = NUL;
5641 }
5642
5643 do
5644 {
5645 stuffReadbuff(ptr);
5646 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
5647 if (last)
5648 stuffReadbuff((char_u *)(last == '0'
5649 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
5650 : IF_EB("\026^", CTRL_V_STR "^")));
5651 }
5652 while (--count > 0);
5653
5654 if (last)
5655 *last_ptr = last;
5656
5657 if (esc_ptr != NULL)
5658 *esc_ptr = ESC; /* put the ESC back */
5659
5660 /* may want to stuff a trailing ESC, to get out of Insert mode */
5661 if (!no_esc)
5662 stuffcharReadbuff(ESC);
5663
5664 return OK;
5665}
5666
5667 char_u *
5668get_last_insert()
5669{
5670 if (last_insert == NULL)
5671 return NULL;
5672 return last_insert + last_insert_skip;
5673}
5674
5675/*
5676 * Get last inserted string, and remove trailing <Esc>.
5677 * Returns pointer to allocated memory (must be freed) or NULL.
5678 */
5679 char_u *
5680get_last_insert_save()
5681{
5682 char_u *s;
5683 int len;
5684
5685 if (last_insert == NULL)
5686 return NULL;
5687 s = vim_strsave(last_insert + last_insert_skip);
5688 if (s != NULL)
5689 {
5690 len = (int)STRLEN(s);
5691 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
5692 s[len - 1] = NUL;
5693 }
5694 return s;
5695}
5696
5697/*
5698 * Check the word in front of the cursor for an abbreviation.
5699 * Called when the non-id character "c" has been entered.
5700 * When an abbreviation is recognized it is removed from the text and
5701 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
5702 */
5703 static int
5704echeck_abbr(c)
5705 int c;
5706{
5707 /* Don't check for abbreviation in paste mode, when disabled and just
5708 * after moving around with cursor keys. */
5709 if (p_paste || no_abbr || arrow_used)
5710 return FALSE;
5711
5712 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
5713 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
5714}
5715
5716/*
5717 * replace-stack functions
5718 *
5719 * When replacing characters, the replaced characters are remembered for each
5720 * new character. This is used to re-insert the old text when backspacing.
5721 *
5722 * There is a NUL headed list of characters for each character that is
5723 * currently in the file after the insertion point. When BS is used, one NUL
5724 * headed list is put back for the deleted character.
5725 *
5726 * For a newline, there are two NUL headed lists. One contains the characters
5727 * that the NL replaced. The extra one stores the characters after the cursor
5728 * that were deleted (always white space).
5729 *
5730 * Replace_offset is normally 0, in which case replace_push will add a new
5731 * character at the end of the stack. If replace_offset is not 0, that many
5732 * characters will be left on the stack above the newly inserted character.
5733 */
5734
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00005735static char_u *replace_stack = NULL;
5736static long replace_stack_nr = 0; /* next entry in replace stack */
5737static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
5739 void
5740replace_push(c)
5741 int c; /* character that is replaced (NUL is none) */
5742{
5743 char_u *p;
5744
5745 if (replace_stack_nr < replace_offset) /* nothing to do */
5746 return;
5747 if (replace_stack_len <= replace_stack_nr)
5748 {
5749 replace_stack_len += 50;
5750 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
5751 if (p == NULL) /* out of memory */
5752 {
5753 replace_stack_len -= 50;
5754 return;
5755 }
5756 if (replace_stack != NULL)
5757 {
5758 mch_memmove(p, replace_stack,
5759 (size_t)(replace_stack_nr * sizeof(char_u)));
5760 vim_free(replace_stack);
5761 }
5762 replace_stack = p;
5763 }
5764 p = replace_stack + replace_stack_nr - replace_offset;
5765 if (replace_offset)
5766 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
5767 *p = c;
5768 ++replace_stack_nr;
5769}
5770
5771/*
5772 * call replace_push(c) with replace_offset set to the first NUL.
5773 */
5774 static void
5775replace_push_off(c)
5776 int c;
5777{
5778 char_u *p;
5779
5780 p = replace_stack + replace_stack_nr;
5781 for (replace_offset = 1; replace_offset < replace_stack_nr;
5782 ++replace_offset)
5783 if (*--p == NUL)
5784 break;
5785 replace_push(c);
5786 replace_offset = 0;
5787}
5788
5789/*
5790 * Pop one item from the replace stack.
5791 * return -1 if stack empty
5792 * return replaced character or NUL otherwise
5793 */
5794 static int
5795replace_pop()
5796{
5797 if (replace_stack_nr == 0)
5798 return -1;
5799 return (int)replace_stack[--replace_stack_nr];
5800}
5801
5802/*
5803 * Join the top two items on the replace stack. This removes to "off"'th NUL
5804 * encountered.
5805 */
5806 static void
5807replace_join(off)
5808 int off; /* offset for which NUL to remove */
5809{
5810 int i;
5811
5812 for (i = replace_stack_nr; --i >= 0; )
5813 if (replace_stack[i] == NUL && off-- <= 0)
5814 {
5815 --replace_stack_nr;
5816 mch_memmove(replace_stack + i, replace_stack + i + 1,
5817 (size_t)(replace_stack_nr - i));
5818 return;
5819 }
5820}
5821
5822/*
5823 * Pop bytes from the replace stack until a NUL is found, and insert them
5824 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
5825 */
5826 static void
5827replace_pop_ins()
5828{
5829 int cc;
5830 int oldState = State;
5831
5832 State = NORMAL; /* don't want REPLACE here */
5833 while ((cc = replace_pop()) > 0)
5834 {
5835#ifdef FEAT_MBYTE
5836 mb_replace_pop_ins(cc);
5837#else
5838 ins_char(cc);
5839#endif
5840 dec_cursor();
5841 }
5842 State = oldState;
5843}
5844
5845#ifdef FEAT_MBYTE
5846/*
5847 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
5848 * indicates a multi-byte char, pop the other bytes too.
5849 */
5850 static void
5851mb_replace_pop_ins(cc)
5852 int cc;
5853{
5854 int n;
5855 char_u buf[MB_MAXBYTES];
5856 int i;
5857 int c;
5858
5859 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
5860 {
5861 buf[0] = cc;
5862 for (i = 1; i < n; ++i)
5863 buf[i] = replace_pop();
5864 ins_bytes_len(buf, n);
5865 }
5866 else
5867 ins_char(cc);
5868
5869 if (enc_utf8)
5870 /* Handle composing chars. */
5871 for (;;)
5872 {
5873 c = replace_pop();
5874 if (c == -1) /* stack empty */
5875 break;
5876 if ((n = MB_BYTE2LEN(c)) == 1)
5877 {
5878 /* Not a multi-byte char, put it back. */
5879 replace_push(c);
5880 break;
5881 }
5882 else
5883 {
5884 buf[0] = c;
5885 for (i = 1; i < n; ++i)
5886 buf[i] = replace_pop();
5887 if (utf_iscomposing(utf_ptr2char(buf)))
5888 ins_bytes_len(buf, n);
5889 else
5890 {
5891 /* Not a composing char, put it back. */
5892 for (i = n - 1; i >= 0; --i)
5893 replace_push(buf[i]);
5894 break;
5895 }
5896 }
5897 }
5898}
5899#endif
5900
5901/*
5902 * make the replace stack empty
5903 * (called when exiting replace mode)
5904 */
5905 static void
5906replace_flush()
5907{
5908 vim_free(replace_stack);
5909 replace_stack = NULL;
5910 replace_stack_len = 0;
5911 replace_stack_nr = 0;
5912}
5913
5914/*
5915 * Handle doing a BS for one character.
5916 * cc < 0: replace stack empty, just move cursor
5917 * cc == 0: character was inserted, delete it
5918 * cc > 0: character was replaced, put cc (first byte of original char) back
5919 * and check for more characters to be put back
5920 */
5921 static void
5922replace_do_bs()
5923{
5924 int cc;
5925#ifdef FEAT_VREPLACE
5926 int orig_len = 0;
5927 int ins_len;
5928 int orig_vcols = 0;
5929 colnr_T start_vcol;
5930 char_u *p;
5931 int i;
5932 int vcol;
5933#endif
5934
5935 cc = replace_pop();
5936 if (cc > 0)
5937 {
5938#ifdef FEAT_VREPLACE
5939 if (State & VREPLACE_FLAG)
5940 {
5941 /* Get the number of screen cells used by the character we are
5942 * going to delete. */
5943 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
5944 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
5945 }
5946#endif
5947#ifdef FEAT_MBYTE
5948 if (has_mbyte)
5949 {
5950 del_char(FALSE);
5951# ifdef FEAT_VREPLACE
5952 if (State & VREPLACE_FLAG)
5953 orig_len = STRLEN(ml_get_cursor());
5954# endif
5955 replace_push(cc);
5956 }
5957 else
5958#endif
5959 {
5960 pchar_cursor(cc);
5961#ifdef FEAT_VREPLACE
5962 if (State & VREPLACE_FLAG)
5963 orig_len = STRLEN(ml_get_cursor()) - 1;
5964#endif
5965 }
5966 replace_pop_ins();
5967
5968#ifdef FEAT_VREPLACE
5969 if (State & VREPLACE_FLAG)
5970 {
5971 /* Get the number of screen cells used by the inserted characters */
5972 p = ml_get_cursor();
5973 ins_len = STRLEN(p) - orig_len;
5974 vcol = start_vcol;
5975 for (i = 0; i < ins_len; ++i)
5976 {
5977 vcol += chartabsize(p + i, vcol);
5978#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005979 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980#endif
5981 }
5982 vcol -= start_vcol;
5983
5984 /* Delete spaces that were inserted after the cursor to keep the
5985 * text aligned. */
5986 curwin->w_cursor.col += ins_len;
5987 while (vcol > orig_vcols && gchar_cursor() == ' ')
5988 {
5989 del_char(FALSE);
5990 ++orig_vcols;
5991 }
5992 curwin->w_cursor.col -= ins_len;
5993 }
5994#endif
5995
5996 /* mark the buffer as changed and prepare for displaying */
5997 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
5998 }
5999 else if (cc == 0)
6000 (void)del_char(FALSE);
6001}
6002
6003#ifdef FEAT_CINDENT
6004/*
6005 * Return TRUE if C-indenting is on.
6006 */
6007 static int
6008cindent_on()
6009{
6010 return (!p_paste && (curbuf->b_p_cin
6011# ifdef FEAT_EVAL
6012 || *curbuf->b_p_inde != NUL
6013# endif
6014 ));
6015}
6016#endif
6017
6018#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6019/*
6020 * Re-indent the current line, based on the current contents of it and the
6021 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6022 * confused what all the part that handles Control-T is doing that I'm not.
6023 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6024 */
6025
6026 void
6027fixthisline(get_the_indent)
6028 int (*get_the_indent) __ARGS((void));
6029{
6030 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6031 if (linewhite(curwin->w_cursor.lnum))
6032 did_ai = TRUE; /* delete the indent if the line stays empty */
6033}
6034
6035 void
6036fix_indent()
6037{
6038 if (p_paste)
6039 return;
6040# ifdef FEAT_LISP
6041 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6042 fixthisline(get_lisp_indent);
6043# endif
6044# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6045 else
6046# endif
6047# ifdef FEAT_CINDENT
6048 if (cindent_on())
6049 do_c_expr_indent();
6050# endif
6051}
6052
6053#endif
6054
6055#ifdef FEAT_CINDENT
6056/*
6057 * return TRUE if 'cinkeys' contains the key "keytyped",
6058 * when == '*': Only if key is preceded with '*' (indent before insert)
6059 * when == '!': Only if key is prededed with '!' (don't insert)
6060 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6061 *
6062 * "keytyped" can have a few special values:
6063 * KEY_OPEN_FORW
6064 * KEY_OPEN_BACK
6065 * KEY_COMPLETE just finished completion.
6066 *
6067 * If line_is_empty is TRUE accept keys with '0' before them.
6068 */
6069 int
6070in_cinkeys(keytyped, when, line_is_empty)
6071 int keytyped;
6072 int when;
6073 int line_is_empty;
6074{
6075 char_u *look;
6076 int try_match;
6077 int try_match_word;
6078 char_u *p;
6079 char_u *line;
6080 int icase;
6081 int i;
6082
6083#ifdef FEAT_EVAL
6084 if (*curbuf->b_p_inde != NUL)
6085 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6086 else
6087#endif
6088 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6089 while (*look)
6090 {
6091 /*
6092 * Find out if we want to try a match with this key, depending on
6093 * 'when' and a '*' or '!' before the key.
6094 */
6095 switch (when)
6096 {
6097 case '*': try_match = (*look == '*'); break;
6098 case '!': try_match = (*look == '!'); break;
6099 default: try_match = (*look != '*'); break;
6100 }
6101 if (*look == '*' || *look == '!')
6102 ++look;
6103
6104 /*
6105 * If there is a '0', only accept a match if the line is empty.
6106 * But may still match when typing last char of a word.
6107 */
6108 if (*look == '0')
6109 {
6110 try_match_word = try_match;
6111 if (!line_is_empty)
6112 try_match = FALSE;
6113 ++look;
6114 }
6115 else
6116 try_match_word = FALSE;
6117
6118 /*
6119 * does it look like a control character?
6120 */
6121 if (*look == '^'
6122#ifdef EBCDIC
6123 && (Ctrl_chr(look[1]) != 0)
6124#else
6125 && look[1] >= '?' && look[1] <= '_'
6126#endif
6127 )
6128 {
6129 if (try_match && keytyped == Ctrl_chr(look[1]))
6130 return TRUE;
6131 look += 2;
6132 }
6133 /*
6134 * 'o' means "o" command, open forward.
6135 * 'O' means "O" command, open backward.
6136 */
6137 else if (*look == 'o')
6138 {
6139 if (try_match && keytyped == KEY_OPEN_FORW)
6140 return TRUE;
6141 ++look;
6142 }
6143 else if (*look == 'O')
6144 {
6145 if (try_match && keytyped == KEY_OPEN_BACK)
6146 return TRUE;
6147 ++look;
6148 }
6149
6150 /*
6151 * 'e' means to check for "else" at start of line and just before the
6152 * cursor.
6153 */
6154 else if (*look == 'e')
6155 {
6156 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6157 {
6158 p = ml_get_curline();
6159 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6160 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6161 return TRUE;
6162 }
6163 ++look;
6164 }
6165
6166 /*
6167 * ':' only causes an indent if it is at the end of a label or case
6168 * statement, or when it was before typing the ':' (to fix
6169 * class::method for C++).
6170 */
6171 else if (*look == ':')
6172 {
6173 if (try_match && keytyped == ':')
6174 {
6175 p = ml_get_curline();
6176 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6177 return TRUE;
6178 if (curwin->w_cursor.col > 2
6179 && p[curwin->w_cursor.col - 1] == ':'
6180 && p[curwin->w_cursor.col - 2] == ':')
6181 {
6182 p[curwin->w_cursor.col - 1] = ' ';
6183 i = (cin_iscase(p) || cin_isscopedecl(p)
6184 || cin_islabel(30));
6185 p = ml_get_curline();
6186 p[curwin->w_cursor.col - 1] = ':';
6187 if (i)
6188 return TRUE;
6189 }
6190 }
6191 ++look;
6192 }
6193
6194
6195 /*
6196 * Is it a key in <>, maybe?
6197 */
6198 else if (*look == '<')
6199 {
6200 if (try_match)
6201 {
6202 /*
6203 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6204 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6205 * >, *, : and ! keys if they really really want to.
6206 */
6207 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6208 && keytyped == look[1])
6209 return TRUE;
6210
6211 if (keytyped == get_special_key_code(look + 1))
6212 return TRUE;
6213 }
6214 while (*look && *look != '>')
6215 look++;
6216 while (*look == '>')
6217 look++;
6218 }
6219
6220 /*
6221 * Is it a word: "=word"?
6222 */
6223 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6224 {
6225 ++look;
6226 if (*look == '~')
6227 {
6228 icase = TRUE;
6229 ++look;
6230 }
6231 else
6232 icase = FALSE;
6233 p = vim_strchr(look, ',');
6234 if (p == NULL)
6235 p = look + STRLEN(look);
6236 if ((try_match || try_match_word)
6237 && curwin->w_cursor.col >= (colnr_T)(p - look))
6238 {
6239 int match = FALSE;
6240
6241#ifdef FEAT_INS_EXPAND
6242 if (keytyped == KEY_COMPLETE)
6243 {
6244 char_u *s;
6245
6246 /* Just completed a word, check if it starts with "look".
6247 * search back for the start of a word. */
6248 line = ml_get_curline();
6249# ifdef FEAT_MBYTE
6250 if (has_mbyte)
6251 {
6252 char_u *n;
6253
6254 for (s = line + curwin->w_cursor.col; s > line; s = n)
6255 {
6256 n = mb_prevptr(line, s);
6257 if (!vim_iswordp(n))
6258 break;
6259 }
6260 }
6261 else
6262# endif
6263 for (s = line + curwin->w_cursor.col; s > line; --s)
6264 if (!vim_iswordc(s[-1]))
6265 break;
6266 if (s + (p - look) <= line + curwin->w_cursor.col
6267 && (icase
6268 ? MB_STRNICMP(s, look, p - look)
6269 : STRNCMP(s, look, p - look)) == 0)
6270 match = TRUE;
6271 }
6272 else
6273#endif
6274 /* TODO: multi-byte */
6275 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6276 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6277 {
6278 line = ml_get_cursor();
6279 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6280 || !vim_iswordc(line[-(p - look) - 1]))
6281 && (icase
6282 ? MB_STRNICMP(line - (p - look), look, p - look)
6283 : STRNCMP(line - (p - look), look, p - look))
6284 == 0)
6285 match = TRUE;
6286 }
6287 if (match && try_match_word && !try_match)
6288 {
6289 /* "0=word": Check if there are only blanks before the
6290 * word. */
6291 line = ml_get_curline();
6292 if ((int)(skipwhite(line) - line) !=
6293 (int)(curwin->w_cursor.col - (p - look)))
6294 match = FALSE;
6295 }
6296 if (match)
6297 return TRUE;
6298 }
6299 look = p;
6300 }
6301
6302 /*
6303 * ok, it's a boring generic character.
6304 */
6305 else
6306 {
6307 if (try_match && *look == keytyped)
6308 return TRUE;
6309 ++look;
6310 }
6311
6312 /*
6313 * Skip over ", ".
6314 */
6315 look = skip_to_option_part(look);
6316 }
6317 return FALSE;
6318}
6319#endif /* FEAT_CINDENT */
6320
6321#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6322/*
6323 * Map Hebrew keyboard when in hkmap mode.
6324 */
6325 int
6326hkmap(c)
6327 int c;
6328{
6329 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6330 {
6331 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6332 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6333 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6334 static char_u map[26] =
6335 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6336 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6337 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6338 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6339 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6340 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6341 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6342 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6343 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6344
6345 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6346 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6347 /* '-1'='sofit' */
6348 else if (c == 'x')
6349 return 'X';
6350 else if (c == 'q')
6351 return '\''; /* {geresh}={'} */
6352 else if (c == 246)
6353 return ' '; /* \"o --> ' ' for a german keyboard */
6354 else if (c == 228)
6355 return ' '; /* \"a --> ' ' -- / -- */
6356 else if (c == 252)
6357 return ' '; /* \"u --> ' ' -- / -- */
6358#ifdef EBCDIC
6359 else if (islower(c))
6360#else
6361 /* NOTE: islower() does not do the right thing for us on Linux so we
6362 * do this the same was as 5.7 and previous, so it works correctly on
6363 * all systems. Specifically, the e.g. Delete and Arrow keys are
6364 * munged and won't work if e.g. searching for Hebrew text.
6365 */
6366 else if (c >= 'a' && c <= 'z')
6367#endif
6368 return (int)(map[CharOrdLow(c)] + p_aleph);
6369 else
6370 return c;
6371 }
6372 else
6373 {
6374 switch (c)
6375 {
6376 case '`': return ';';
6377 case '/': return '.';
6378 case '\'': return ',';
6379 case 'q': return '/';
6380 case 'w': return '\'';
6381
6382 /* Hebrew letters - set offset from 'a' */
6383 case ',': c = '{'; break;
6384 case '.': c = 'v'; break;
6385 case ';': c = 't'; break;
6386 default: {
6387 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6388
6389#ifdef EBCDIC
6390 /* see note about islower() above */
6391 if (!islower(c))
6392#else
6393 if (c < 'a' || c > 'z')
6394#endif
6395 return c;
6396 c = str[CharOrdLow(c)];
6397 break;
6398 }
6399 }
6400
6401 return (int)(CharOrdLow(c) + p_aleph);
6402 }
6403}
6404#endif
6405
6406 static void
6407ins_reg()
6408{
6409 int need_redraw = FALSE;
6410 int regname;
6411 int literally = 0;
6412
6413 /*
6414 * If we are going to wait for a character, show a '"'.
6415 */
6416 pc_status = PC_STATUS_UNSET;
6417 if (redrawing() && !char_avail())
6418 {
6419 /* may need to redraw when no more chars available now */
6420 ins_redraw();
6421
6422 edit_putchar('"', TRUE);
6423#ifdef FEAT_CMDL_INFO
6424 add_to_showcmd_c(Ctrl_R);
6425#endif
6426 }
6427
6428#ifdef USE_ON_FLY_SCROLL
6429 dont_scroll = TRUE; /* disallow scrolling here */
6430#endif
6431
6432 /*
6433 * Don't map the register name. This also prevents the mode message to be
6434 * deleted when ESC is hit.
6435 */
6436 ++no_mapping;
6437 regname = safe_vgetc();
6438#ifdef FEAT_LANGMAP
6439 LANGMAP_ADJUST(regname, TRUE);
6440#endif
6441 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6442 {
6443 /* Get a third key for literal register insertion */
6444 literally = regname;
6445#ifdef FEAT_CMDL_INFO
6446 add_to_showcmd_c(literally);
6447#endif
6448 regname = safe_vgetc();
6449#ifdef FEAT_LANGMAP
6450 LANGMAP_ADJUST(regname, TRUE);
6451#endif
6452 }
6453 --no_mapping;
6454
6455#ifdef FEAT_EVAL
6456 /*
6457 * Don't call u_sync() while getting the expression,
6458 * evaluating it or giving an error message for it!
6459 */
6460 ++no_u_sync;
6461 if (regname == '=')
6462 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006463# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006465# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006467# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 /* Restore the Input Method. */
6469 if (im_on)
6470 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006473 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6474 {
6475 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 else
6479 {
6480#endif
6481 if (literally == Ctrl_O || literally == Ctrl_P)
6482 {
6483 /* Append the command to the redo buffer. */
6484 AppendCharToRedobuff(Ctrl_R);
6485 AppendCharToRedobuff(literally);
6486 AppendCharToRedobuff(regname);
6487
6488 do_put(regname, BACKWARD, 1L,
6489 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6490 }
6491 else if (insert_reg(regname, literally) == FAIL)
6492 {
6493 vim_beep();
6494 need_redraw = TRUE; /* remove the '"' */
6495 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006496 else if (stop_insert_mode)
6497 /* When the '=' register was used and a function was invoked that
6498 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
6499 * insert anything, need to remove the '"' */
6500 need_redraw = TRUE;
6501
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502#ifdef FEAT_EVAL
6503 }
6504 --no_u_sync;
6505#endif
6506#ifdef FEAT_CMDL_INFO
6507 clear_showcmd();
6508#endif
6509
6510 /* If the inserted register is empty, we need to remove the '"' */
6511 if (need_redraw || stuff_empty())
6512 edit_unputchar();
6513}
6514
6515/*
6516 * CTRL-G commands in Insert mode.
6517 */
6518 static void
6519ins_ctrl_g()
6520{
6521 int c;
6522
6523#ifdef FEAT_INS_EXPAND
6524 /* Right after CTRL-X the cursor will be after the ruler. */
6525 setcursor();
6526#endif
6527
6528 /*
6529 * Don't map the second key. This also prevents the mode message to be
6530 * deleted when ESC is hit.
6531 */
6532 ++no_mapping;
6533 c = safe_vgetc();
6534 --no_mapping;
6535 switch (c)
6536 {
6537 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
6538 case K_UP:
6539 case Ctrl_K:
6540 case 'k': ins_up(TRUE);
6541 break;
6542
6543 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
6544 case K_DOWN:
6545 case Ctrl_J:
6546 case 'j': ins_down(TRUE);
6547 break;
6548
6549 /* CTRL-G u: start new undoable edit */
6550 case 'u': u_sync();
6551 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006552
6553 /* Need to reset Insstart, esp. because a BS that joins
6554 * aline to the previous one must save for undo. */
6555 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 break;
6557
6558 /* Unknown CTRL-G command, reserved for future expansion. */
6559 default: vim_beep();
6560 }
6561}
6562
6563/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006564 * CTRL-^ in Insert mode.
6565 */
6566 static void
6567ins_ctrl_hat()
6568{
6569 if (map_to_exists_mode((char_u *)"", LANGMAP))
6570 {
6571 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
6572 if (State & LANGMAP)
6573 {
6574 curbuf->b_p_iminsert = B_IMODE_NONE;
6575 State &= ~LANGMAP;
6576 }
6577 else
6578 {
6579 curbuf->b_p_iminsert = B_IMODE_LMAP;
6580 State |= LANGMAP;
6581#ifdef USE_IM_CONTROL
6582 im_set_active(FALSE);
6583#endif
6584 }
6585 }
6586#ifdef USE_IM_CONTROL
6587 else
6588 {
6589 /* There are no ":lmap" mappings, toggle IM */
6590 if (im_get_status())
6591 {
6592 curbuf->b_p_iminsert = B_IMODE_NONE;
6593 im_set_active(FALSE);
6594 }
6595 else
6596 {
6597 curbuf->b_p_iminsert = B_IMODE_IM;
6598 State &= ~LANGMAP;
6599 im_set_active(TRUE);
6600 }
6601 }
6602#endif
6603 set_iminsert_global();
6604 showmode();
6605#ifdef FEAT_GUI
6606 /* may show different cursor shape or color */
6607 if (gui.in_use)
6608 gui_update_cursor(TRUE, FALSE);
6609#endif
6610#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6611 /* Show/unshow value of 'keymap' in status lines. */
6612 status_redraw_curbuf();
6613#endif
6614}
6615
6616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 * Handle ESC in insert mode.
6618 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
6619 * insert.
6620 */
6621 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00006622ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 long *count;
6624 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00006625 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626{
6627 int temp;
6628 static int disabled_redraw = FALSE;
6629
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006630#ifdef FEAT_SYN_HL
6631 check_spell_redraw();
6632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633#if defined(FEAT_HANGULIN)
6634# if defined(ESC_CHG_TO_ENG_MODE)
6635 hangul_input_state_set(0);
6636# endif
6637 if (composing_hangul)
6638 {
6639 push_raw_key(composing_hangul_buffer, 2);
6640 composing_hangul = 0;
6641 }
6642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643
6644 temp = curwin->w_cursor.col;
6645 if (disabled_redraw)
6646 {
6647 --RedrawingDisabled;
6648 disabled_redraw = FALSE;
6649 }
6650 if (!arrow_used)
6651 {
6652 /*
6653 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00006654 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
6655 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 */
6657 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00006658 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659
6660 /*
6661 * Repeating insert may take a long time. Check for
6662 * interrupt now and then.
6663 */
6664 if (*count > 0)
6665 {
6666 line_breakcheck();
6667 if (got_int)
6668 *count = 0;
6669 }
6670
6671 if (--*count > 0) /* repeat what was typed */
6672 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006673 /* Vi repeats the insert without replacing characters. */
6674 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
6675 State &= ~REPLACE_FLAG;
6676
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 (void)start_redo_ins();
6678 if (cmdchar == 'r' || cmdchar == 'v')
6679 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
6680 ++RedrawingDisabled;
6681 disabled_redraw = TRUE;
6682 return FALSE; /* repeat the insert */
6683 }
6684 stop_insert(&curwin->w_cursor, TRUE);
6685 undisplay_dollar();
6686 }
6687
6688 /* When an autoindent was removed, curswant stays after the
6689 * indent */
6690 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
6691 curwin->w_set_curswant = TRUE;
6692
6693 /* Remember the last Insert position in the '^ mark. */
6694 if (!cmdmod.keepjumps)
6695 curbuf->b_last_insert = curwin->w_cursor;
6696
6697 /*
6698 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00006699 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00006701 if (!nomove
6702 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703#ifdef FEAT_VIRTUALEDIT
6704 || curwin->w_cursor.coladd > 0
6705#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006706 )
6707 && (restart_edit == NUL
6708 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00006710 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006712 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713#ifdef FEAT_RIGHTLEFT
6714 && !revins_on
6715#endif
6716 )
6717 {
6718#ifdef FEAT_VIRTUALEDIT
6719 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
6720 {
6721 oneleft();
6722 if (restart_edit != NUL)
6723 ++curwin->w_cursor.coladd;
6724 }
6725 else
6726#endif
6727 {
6728 --curwin->w_cursor.col;
6729#ifdef FEAT_MBYTE
6730 /* Correct cursor for multi-byte character. */
6731 if (has_mbyte)
6732 mb_adjust_cursor();
6733#endif
6734 }
6735 }
6736
6737#ifdef USE_IM_CONTROL
6738 /* Disable IM to allow typing English directly for Normal mode commands.
6739 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
6740 * well). */
6741 if (!(State & LANGMAP))
6742 im_save_status(&curbuf->b_p_iminsert);
6743 im_set_active(FALSE);
6744#endif
6745
6746 State = NORMAL;
6747 /* need to position cursor again (e.g. when on a TAB ) */
6748 changed_cline_bef_curs();
6749
6750#ifdef FEAT_MOUSE
6751 setmouse();
6752#endif
6753#ifdef CURSOR_SHAPE
6754 ui_cursor_shape(); /* may show different cursor shape */
6755#endif
6756
6757 /*
6758 * When recording or for CTRL-O, need to display the new mode.
6759 * Otherwise remove the mode message.
6760 */
6761 if (Recording || restart_edit != NUL)
6762 showmode();
6763 else if (p_smd)
6764 MSG("");
6765
6766 return TRUE; /* exit Insert mode */
6767}
6768
6769#ifdef FEAT_RIGHTLEFT
6770/*
6771 * Toggle language: hkmap and revins_on.
6772 * Move to end of reverse inserted text.
6773 */
6774 static void
6775ins_ctrl_()
6776{
6777 if (revins_on && revins_chars && revins_scol >= 0)
6778 {
6779 while (gchar_cursor() != NUL && revins_chars--)
6780 ++curwin->w_cursor.col;
6781 }
6782 p_ri = !p_ri;
6783 revins_on = (State == INSERT && p_ri);
6784 if (revins_on)
6785 {
6786 revins_scol = curwin->w_cursor.col;
6787 revins_legal++;
6788 revins_chars = 0;
6789 undisplay_dollar();
6790 }
6791 else
6792 revins_scol = -1;
6793#ifdef FEAT_FKMAP
6794 if (p_altkeymap)
6795 {
6796 /*
6797 * to be consistent also for redo command, using '.'
6798 * set arrow_used to true and stop it - causing to redo
6799 * characters entered in one mode (normal/reverse insert).
6800 */
6801 arrow_used = TRUE;
6802 (void)stop_arrow();
6803 p_fkmap = curwin->w_p_rl ^ p_ri;
6804 if (p_fkmap && p_ri)
6805 State = INSERT;
6806 }
6807 else
6808#endif
6809 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
6810 showmode();
6811}
6812#endif
6813
6814#ifdef FEAT_VISUAL
6815/*
6816 * If 'keymodel' contains "startsel", may start selection.
6817 * Returns TRUE when a CTRL-O and other keys stuffed.
6818 */
6819 static int
6820ins_start_select(c)
6821 int c;
6822{
6823 if (km_startsel)
6824 switch (c)
6825 {
6826 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 case K_PAGEUP:
6829 case K_KPAGEUP:
6830 case K_PAGEDOWN:
6831 case K_KPAGEDOWN:
6832# ifdef MACOS
6833 case K_LEFT:
6834 case K_RIGHT:
6835 case K_UP:
6836 case K_DOWN:
6837 case K_END:
6838 case K_HOME:
6839# endif
6840 if (!(mod_mask & MOD_MASK_SHIFT))
6841 break;
6842 /* FALLTHROUGH */
6843 case K_S_LEFT:
6844 case K_S_RIGHT:
6845 case K_S_UP:
6846 case K_S_DOWN:
6847 case K_S_END:
6848 case K_S_HOME:
6849 /* Start selection right away, the cursor can move with
6850 * CTRL-O when beyond the end of the line. */
6851 start_selection();
6852
6853 /* Execute the key in (insert) Select mode. */
6854 stuffcharReadbuff(Ctrl_O);
6855 if (mod_mask)
6856 {
6857 char_u buf[4];
6858
6859 buf[0] = K_SPECIAL;
6860 buf[1] = KS_MODIFIER;
6861 buf[2] = mod_mask;
6862 buf[3] = NUL;
6863 stuffReadbuff(buf);
6864 }
6865 stuffcharReadbuff(c);
6866 return TRUE;
6867 }
6868 return FALSE;
6869}
6870#endif
6871
6872/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006873 * <Insert> key in Insert mode: toggle insert/remplace mode.
6874 */
6875 static void
6876ins_insert(replaceState)
6877 int replaceState;
6878{
6879#ifdef FEAT_FKMAP
6880 if (p_fkmap && p_ri)
6881 {
6882 beep_flush();
6883 EMSG(farsi_text_3); /* encoded in Farsi */
6884 return;
6885 }
6886#endif
6887
6888#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00006889# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006890 set_vim_var_string(VV_INSERTMODE,
6891 (char_u *)((State & REPLACE_FLAG) ? "i" :
6892 replaceState == VREPLACE ? "v" : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00006893# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006894 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
6895#endif
6896 if (State & REPLACE_FLAG)
6897 State = INSERT | (State & LANGMAP);
6898 else
6899 State = replaceState | (State & LANGMAP);
6900 AppendCharToRedobuff(K_INS);
6901 showmode();
6902#ifdef CURSOR_SHAPE
6903 ui_cursor_shape(); /* may show different cursor shape */
6904#endif
6905}
6906
6907/*
6908 * Pressed CTRL-O in Insert mode.
6909 */
6910 static void
6911ins_ctrl_o()
6912{
6913#ifdef FEAT_VREPLACE
6914 if (State & VREPLACE_FLAG)
6915 restart_edit = 'V';
6916 else
6917#endif
6918 if (State & REPLACE_FLAG)
6919 restart_edit = 'R';
6920 else
6921 restart_edit = 'I';
6922#ifdef FEAT_VIRTUALEDIT
6923 if (virtual_active())
6924 ins_at_eol = FALSE; /* cursor always keeps its column */
6925 else
6926#endif
6927 ins_at_eol = (gchar_cursor() == NUL);
6928}
6929
6930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 * If the cursor is on an indent, ^T/^D insert/delete one
6932 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
6933 * Always round the indent to 'shiftwith', this is compatible
6934 * with vi. But vi only supports ^T and ^D after an
6935 * autoindent, we support it everywhere.
6936 */
6937 static void
6938ins_shift(c, lastc)
6939 int c;
6940 int lastc;
6941{
6942 if (stop_arrow() == FAIL)
6943 return;
6944 AppendCharToRedobuff(c);
6945
6946 /*
6947 * 0^D and ^^D: remove all indent.
6948 */
6949 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
6950 {
6951 --curwin->w_cursor.col;
6952 (void)del_char(FALSE); /* delete the '^' or '0' */
6953 /* In Replace mode, restore the characters that '^' or '0' replaced. */
6954 if (State & REPLACE_FLAG)
6955 replace_pop_ins();
6956 if (lastc == '^')
6957 old_indent = get_indent(); /* remember curr. indent */
6958 change_indent(INDENT_SET, 0, TRUE, 0);
6959 }
6960 else
6961 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
6962
6963 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
6964 did_ai = FALSE;
6965#ifdef FEAT_SMARTINDENT
6966 did_si = FALSE;
6967 can_si = FALSE;
6968 can_si_back = FALSE;
6969#endif
6970#ifdef FEAT_CINDENT
6971 can_cindent = FALSE; /* no cindenting after ^D or ^T */
6972#endif
6973}
6974
6975 static void
6976ins_del()
6977{
6978 int temp;
6979
6980 if (stop_arrow() == FAIL)
6981 return;
6982 if (gchar_cursor() == NUL) /* delete newline */
6983 {
6984 temp = curwin->w_cursor.col;
6985 if (!can_bs(BS_EOL) /* only if "eol" included */
6986 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
6987 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
6988 || do_join(FALSE) == FAIL)
6989 vim_beep();
6990 else
6991 curwin->w_cursor.col = temp;
6992 }
6993 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
6994 vim_beep();
6995 did_ai = FALSE;
6996#ifdef FEAT_SMARTINDENT
6997 did_si = FALSE;
6998 can_si = FALSE;
6999 can_si_back = FALSE;
7000#endif
7001 AppendCharToRedobuff(K_DEL);
7002}
7003
7004/*
7005 * Handle Backspace, delete-word and delete-line in Insert mode.
7006 * Return TRUE when backspace was actually used.
7007 */
7008 static int
7009ins_bs(c, mode, inserted_space_p)
7010 int c;
7011 int mode;
7012 int *inserted_space_p;
7013{
7014 linenr_T lnum;
7015 int cc;
7016 int temp = 0; /* init for GCC */
7017 colnr_T mincol;
7018 int did_backspace = FALSE;
7019 int in_indent;
7020 int oldState;
7021#ifdef FEAT_MBYTE
7022 int p1, p2;
7023#endif
7024
7025 /*
7026 * can't delete anything in an empty file
7027 * can't backup past first character in buffer
7028 * can't backup past starting point unless 'backspace' > 1
7029 * can backup to a previous line if 'backspace' == 0
7030 */
7031 if ( bufempty()
7032 || (
7033#ifdef FEAT_RIGHTLEFT
7034 !revins_on &&
7035#endif
7036 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7037 || (!can_bs(BS_START)
7038 && (arrow_used
7039 || (curwin->w_cursor.lnum == Insstart.lnum
7040 && curwin->w_cursor.col <= Insstart.col)))
7041 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7042 && curwin->w_cursor.col <= ai_col)
7043 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7044 {
7045 vim_beep();
7046 return FALSE;
7047 }
7048
7049 if (stop_arrow() == FAIL)
7050 return FALSE;
7051 in_indent = inindent(0);
7052#ifdef FEAT_CINDENT
7053 if (in_indent)
7054 can_cindent = FALSE;
7055#endif
7056#ifdef FEAT_COMMENTS
7057 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7058#endif
7059#ifdef FEAT_RIGHTLEFT
7060 if (revins_on) /* put cursor after last inserted char */
7061 inc_cursor();
7062#endif
7063
7064#ifdef FEAT_VIRTUALEDIT
7065 /* Virtualedit:
7066 * BACKSPACE_CHAR eats a virtual space
7067 * BACKSPACE_WORD eats all coladd
7068 * BACKSPACE_LINE eats all coladd and keeps going
7069 */
7070 if (curwin->w_cursor.coladd > 0)
7071 {
7072 if (mode == BACKSPACE_CHAR)
7073 {
7074 --curwin->w_cursor.coladd;
7075 return TRUE;
7076 }
7077 if (mode == BACKSPACE_WORD)
7078 {
7079 curwin->w_cursor.coladd = 0;
7080 return TRUE;
7081 }
7082 curwin->w_cursor.coladd = 0;
7083 }
7084#endif
7085
7086 /*
7087 * delete newline!
7088 */
7089 if (curwin->w_cursor.col == 0)
7090 {
7091 lnum = Insstart.lnum;
7092 if (curwin->w_cursor.lnum == Insstart.lnum
7093#ifdef FEAT_RIGHTLEFT
7094 || revins_on
7095#endif
7096 )
7097 {
7098 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7099 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7100 return FALSE;
7101 --Insstart.lnum;
7102 Insstart.col = MAXCOL;
7103 }
7104 /*
7105 * In replace mode:
7106 * cc < 0: NL was inserted, delete it
7107 * cc >= 0: NL was replaced, put original characters back
7108 */
7109 cc = -1;
7110 if (State & REPLACE_FLAG)
7111 cc = replace_pop(); /* returns -1 if NL was inserted */
7112 /*
7113 * In replace mode, in the line we started replacing, we only move the
7114 * cursor.
7115 */
7116 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7117 {
7118 dec_cursor();
7119 }
7120 else
7121 {
7122#ifdef FEAT_VREPLACE
7123 if (!(State & VREPLACE_FLAG)
7124 || curwin->w_cursor.lnum > orig_line_count)
7125#endif
7126 {
7127 temp = gchar_cursor(); /* remember current char */
7128 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007129
7130 /* When "aw" is in 'formatoptions' we must delete the space at
7131 * the end of the line, otherwise the line will be broken
7132 * again when auto-formatting. */
7133 if (has_format_option(FO_AUTO)
7134 && has_format_option(FO_WHITE_PAR))
7135 {
7136 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7137 TRUE);
7138 int len;
7139
7140 len = STRLEN(ptr);
7141 if (len > 0 && ptr[len - 1] == ' ')
7142 ptr[len - 1] = NUL;
7143 }
7144
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 (void)do_join(FALSE);
7146 if (temp == NUL && gchar_cursor() != NUL)
7147 inc_cursor();
7148 }
7149#ifdef FEAT_VREPLACE
7150 else
7151 dec_cursor();
7152#endif
7153
7154 /*
7155 * In REPLACE mode we have to put back the text that was replaced
7156 * by the NL. On the replace stack is first a NUL-terminated
7157 * sequence of characters that were deleted and then the
7158 * characters that NL replaced.
7159 */
7160 if (State & REPLACE_FLAG)
7161 {
7162 /*
7163 * Do the next ins_char() in NORMAL state, to
7164 * prevent ins_char() from replacing characters and
7165 * avoiding showmatch().
7166 */
7167 oldState = State;
7168 State = NORMAL;
7169 /*
7170 * restore characters (blanks) deleted after cursor
7171 */
7172 while (cc > 0)
7173 {
7174 temp = curwin->w_cursor.col;
7175#ifdef FEAT_MBYTE
7176 mb_replace_pop_ins(cc);
7177#else
7178 ins_char(cc);
7179#endif
7180 curwin->w_cursor.col = temp;
7181 cc = replace_pop();
7182 }
7183 /* restore the characters that NL replaced */
7184 replace_pop_ins();
7185 State = oldState;
7186 }
7187 }
7188 did_ai = FALSE;
7189 }
7190 else
7191 {
7192 /*
7193 * Delete character(s) before the cursor.
7194 */
7195#ifdef FEAT_RIGHTLEFT
7196 if (revins_on) /* put cursor on last inserted char */
7197 dec_cursor();
7198#endif
7199 mincol = 0;
7200 /* keep indent */
7201 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7202#ifdef FEAT_RIGHTLEFT
7203 && !revins_on
7204#endif
7205 )
7206 {
7207 temp = curwin->w_cursor.col;
7208 beginline(BL_WHITE);
7209 if (curwin->w_cursor.col < (colnr_T)temp)
7210 mincol = curwin->w_cursor.col;
7211 curwin->w_cursor.col = temp;
7212 }
7213
7214 /*
7215 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7216 */
7217 if ( mode == BACKSPACE_CHAR
7218 && ((p_sta && in_indent)
7219 || (curbuf->b_p_sts
7220 && (*(ml_get_cursor() - 1) == TAB
7221 || (*(ml_get_cursor() - 1) == ' '
7222 && (!*inserted_space_p
7223 || arrow_used))))))
7224 {
7225 int ts;
7226 colnr_T vcol;
7227 colnr_T want_vcol;
7228 int extra = 0;
7229
7230 *inserted_space_p = FALSE;
7231 if (p_sta)
7232 ts = curbuf->b_p_sw;
7233 else
7234 ts = curbuf->b_p_sts;
7235 /* Compute the virtual column where we want to be. Since
7236 * 'showbreak' may get in the way, need to get the last column of
7237 * the previous character. */
7238 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7239 dec_cursor();
7240 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7241 inc_cursor();
7242 want_vcol = (want_vcol / ts) * ts;
7243
7244 /* delete characters until we are at or before want_vcol */
7245 while (vcol > want_vcol
7246 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7247 {
7248 dec_cursor();
7249 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7250 if (State & REPLACE_FLAG)
7251 {
7252 /* Don't delete characters before the insert point when in
7253 * Replace mode */
7254 if (curwin->w_cursor.lnum != Insstart.lnum
7255 || curwin->w_cursor.col >= Insstart.col)
7256 {
7257#if 0 /* what was this for? It causes problems when sw != ts. */
7258 if (State == REPLACE && (int)vcol < want_vcol)
7259 {
7260 (void)del_char(FALSE);
7261 extra = 2; /* don't pop too much */
7262 }
7263 else
7264#endif
7265 replace_do_bs();
7266 }
7267 }
7268 else
7269 (void)del_char(FALSE);
7270 }
7271
7272 /* insert extra spaces until we are at want_vcol */
7273 while (vcol < want_vcol)
7274 {
7275 /* Remember the first char we inserted */
7276 if (curwin->w_cursor.lnum == Insstart.lnum
7277 && curwin->w_cursor.col < Insstart.col)
7278 Insstart.col = curwin->w_cursor.col;
7279
7280#ifdef FEAT_VREPLACE
7281 if (State & VREPLACE_FLAG)
7282 ins_char(' ');
7283 else
7284#endif
7285 {
7286 ins_str((char_u *)" ");
7287 if ((State & REPLACE_FLAG) && extra <= 1)
7288 {
7289 if (extra)
7290 replace_push_off(NUL);
7291 else
7292 replace_push(NUL);
7293 }
7294 if (extra == 2)
7295 extra = 1;
7296 }
7297 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7298 }
7299 }
7300
7301 /*
7302 * Delete upto starting point, start of line or previous word.
7303 */
7304 else do
7305 {
7306#ifdef FEAT_RIGHTLEFT
7307 if (!revins_on) /* put cursor on char to be deleted */
7308#endif
7309 dec_cursor();
7310
7311 /* start of word? */
7312 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7313 {
7314 mode = BACKSPACE_WORD_NOT_SPACE;
7315 temp = vim_iswordc(gchar_cursor());
7316 }
7317 /* end of word? */
7318 else if (mode == BACKSPACE_WORD_NOT_SPACE
7319 && (vim_isspace(cc = gchar_cursor())
7320 || vim_iswordc(cc) != temp))
7321 {
7322#ifdef FEAT_RIGHTLEFT
7323 if (!revins_on)
7324#endif
7325 inc_cursor();
7326#ifdef FEAT_RIGHTLEFT
7327 else if (State & REPLACE_FLAG)
7328 dec_cursor();
7329#endif
7330 break;
7331 }
7332 if (State & REPLACE_FLAG)
7333 replace_do_bs();
7334 else
7335 {
7336#ifdef FEAT_MBYTE
7337 if (enc_utf8 && p_deco)
7338 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7339#endif
7340 (void)del_char(FALSE);
7341#ifdef FEAT_MBYTE
7342 /*
7343 * If p1 or p2 is non-zero, there are combining characters we
7344 * need to take account of. Don't back up before the base
7345 * character.
7346 */
7347 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7348 inc_cursor();
7349#endif
7350#ifdef FEAT_RIGHTLEFT
7351 if (revins_chars)
7352 {
7353 revins_chars--;
7354 revins_legal++;
7355 }
7356 if (revins_on && gchar_cursor() == NUL)
7357 break;
7358#endif
7359 }
7360 /* Just a single backspace?: */
7361 if (mode == BACKSPACE_CHAR)
7362 break;
7363 } while (
7364#ifdef FEAT_RIGHTLEFT
7365 revins_on ||
7366#endif
7367 (curwin->w_cursor.col > mincol
7368 && (curwin->w_cursor.lnum != Insstart.lnum
7369 || curwin->w_cursor.col != Insstart.col)));
7370 did_backspace = TRUE;
7371 }
7372#ifdef FEAT_SMARTINDENT
7373 did_si = FALSE;
7374 can_si = FALSE;
7375 can_si_back = FALSE;
7376#endif
7377 if (curwin->w_cursor.col <= 1)
7378 did_ai = FALSE;
7379 /*
7380 * It's a little strange to put backspaces into the redo
7381 * buffer, but it makes auto-indent a lot easier to deal
7382 * with.
7383 */
7384 AppendCharToRedobuff(c);
7385
7386 /* If deleted before the insertion point, adjust it */
7387 if (curwin->w_cursor.lnum == Insstart.lnum
7388 && curwin->w_cursor.col < Insstart.col)
7389 Insstart.col = curwin->w_cursor.col;
7390
7391 /* vi behaviour: the cursor moves backward but the character that
7392 * was there remains visible
7393 * Vim behaviour: the cursor moves backward and the character that
7394 * was there is erased from the screen.
7395 * We can emulate the vi behaviour by pretending there is a dollar
7396 * displayed even when there isn't.
7397 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7398 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7399 dollar_vcol = curwin->w_virtcol;
7400
7401 return did_backspace;
7402}
7403
7404#ifdef FEAT_MOUSE
7405 static void
7406ins_mouse(c)
7407 int c;
7408{
7409 pos_T tpos;
7410
7411# ifdef FEAT_GUI
7412 /* When GUI is active, also move/paste when 'mouse' is empty */
7413 if (!gui.in_use)
7414# endif
7415 if (!mouse_has(MOUSE_INSERT))
7416 return;
7417
7418 undisplay_dollar();
7419 tpos = curwin->w_cursor;
7420 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7421 {
7422 start_arrow(&tpos);
7423# ifdef FEAT_CINDENT
7424 can_cindent = TRUE;
7425# endif
7426 }
7427
7428#ifdef FEAT_WINDOWS
7429 /* redraw status lines (in case another window became active) */
7430 redraw_statuslines();
7431#endif
7432}
7433
7434 static void
7435ins_mousescroll(up)
7436 int up;
7437{
7438 pos_T tpos;
7439# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7440 win_T *old_curwin;
7441# endif
7442
7443 tpos = curwin->w_cursor;
7444
7445# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7446 old_curwin = curwin;
7447
7448 /* Currently the mouse coordinates are only known in the GUI. */
7449 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7450 {
7451 int row, col;
7452
7453 row = mouse_row;
7454 col = mouse_col;
7455
7456 /* find the window at the pointer coordinates */
7457 curwin = mouse_find_win(&row, &col);
7458 curbuf = curwin->w_buffer;
7459 }
7460 if (curwin == old_curwin)
7461# endif
7462 undisplay_dollar();
7463
7464 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7465 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7466 else
7467 scroll_redraw(up, 3L);
7468
7469# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7470 curwin->w_redr_status = TRUE;
7471
7472 curwin = old_curwin;
7473 curbuf = curwin->w_buffer;
7474# endif
7475
7476 if (!equalpos(curwin->w_cursor, tpos))
7477 {
7478 start_arrow(&tpos);
7479# ifdef FEAT_CINDENT
7480 can_cindent = TRUE;
7481# endif
7482 }
7483}
7484#endif
7485
7486#ifdef FEAT_GUI
7487 void
7488ins_scroll()
7489{
7490 pos_T tpos;
7491
7492 undisplay_dollar();
7493 tpos = curwin->w_cursor;
7494 if (gui_do_scroll())
7495 {
7496 start_arrow(&tpos);
7497# ifdef FEAT_CINDENT
7498 can_cindent = TRUE;
7499# endif
7500 }
7501}
7502
7503 void
7504ins_horscroll()
7505{
7506 pos_T tpos;
7507
7508 undisplay_dollar();
7509 tpos = curwin->w_cursor;
7510 if (gui_do_horiz_scroll())
7511 {
7512 start_arrow(&tpos);
7513# ifdef FEAT_CINDENT
7514 can_cindent = TRUE;
7515# endif
7516 }
7517}
7518#endif
7519
7520 static void
7521ins_left()
7522{
7523 pos_T tpos;
7524
7525#ifdef FEAT_FOLDING
7526 if ((fdo_flags & FDO_HOR) && KeyTyped)
7527 foldOpenCursor();
7528#endif
7529 undisplay_dollar();
7530 tpos = curwin->w_cursor;
7531 if (oneleft() == OK)
7532 {
7533 start_arrow(&tpos);
7534#ifdef FEAT_RIGHTLEFT
7535 /* If exit reversed string, position is fixed */
7536 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
7537 revins_legal++;
7538 revins_chars++;
7539#endif
7540 }
7541
7542 /*
7543 * if 'whichwrap' set for cursor in insert mode may go to
7544 * previous line
7545 */
7546 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
7547 {
7548 start_arrow(&tpos);
7549 --(curwin->w_cursor.lnum);
7550 coladvance((colnr_T)MAXCOL);
7551 curwin->w_set_curswant = TRUE; /* so we stay at the end */
7552 }
7553 else
7554 vim_beep();
7555}
7556
7557 static void
7558ins_home(c)
7559 int c;
7560{
7561 pos_T tpos;
7562
7563#ifdef FEAT_FOLDING
7564 if ((fdo_flags & FDO_HOR) && KeyTyped)
7565 foldOpenCursor();
7566#endif
7567 undisplay_dollar();
7568 tpos = curwin->w_cursor;
7569 if (c == K_C_HOME)
7570 curwin->w_cursor.lnum = 1;
7571 curwin->w_cursor.col = 0;
7572#ifdef FEAT_VIRTUALEDIT
7573 curwin->w_cursor.coladd = 0;
7574#endif
7575 curwin->w_curswant = 0;
7576 start_arrow(&tpos);
7577}
7578
7579 static void
7580ins_end(c)
7581 int c;
7582{
7583 pos_T tpos;
7584
7585#ifdef FEAT_FOLDING
7586 if ((fdo_flags & FDO_HOR) && KeyTyped)
7587 foldOpenCursor();
7588#endif
7589 undisplay_dollar();
7590 tpos = curwin->w_cursor;
7591 if (c == K_C_END)
7592 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7593 coladvance((colnr_T)MAXCOL);
7594 curwin->w_curswant = MAXCOL;
7595
7596 start_arrow(&tpos);
7597}
7598
7599 static void
7600ins_s_left()
7601{
7602#ifdef FEAT_FOLDING
7603 if ((fdo_flags & FDO_HOR) && KeyTyped)
7604 foldOpenCursor();
7605#endif
7606 undisplay_dollar();
7607 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
7608 {
7609 start_arrow(&curwin->w_cursor);
7610 (void)bck_word(1L, FALSE, FALSE);
7611 curwin->w_set_curswant = TRUE;
7612 }
7613 else
7614 vim_beep();
7615}
7616
7617 static void
7618ins_right()
7619{
7620#ifdef FEAT_FOLDING
7621 if ((fdo_flags & FDO_HOR) && KeyTyped)
7622 foldOpenCursor();
7623#endif
7624 undisplay_dollar();
7625 if (gchar_cursor() != NUL || virtual_active()
7626 )
7627 {
7628 start_arrow(&curwin->w_cursor);
7629 curwin->w_set_curswant = TRUE;
7630#ifdef FEAT_VIRTUALEDIT
7631 if (virtual_active())
7632 oneright();
7633 else
7634#endif
7635 {
7636#ifdef FEAT_MBYTE
7637 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007638 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 else
7640#endif
7641 ++curwin->w_cursor.col;
7642 }
7643
7644#ifdef FEAT_RIGHTLEFT
7645 revins_legal++;
7646 if (revins_chars)
7647 revins_chars--;
7648#endif
7649 }
7650 /* if 'whichwrap' set for cursor in insert mode, may move the
7651 * cursor to the next line */
7652 else if (vim_strchr(p_ww, ']') != NULL
7653 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7654 {
7655 start_arrow(&curwin->w_cursor);
7656 curwin->w_set_curswant = TRUE;
7657 ++curwin->w_cursor.lnum;
7658 curwin->w_cursor.col = 0;
7659 }
7660 else
7661 vim_beep();
7662}
7663
7664 static void
7665ins_s_right()
7666{
7667#ifdef FEAT_FOLDING
7668 if ((fdo_flags & FDO_HOR) && KeyTyped)
7669 foldOpenCursor();
7670#endif
7671 undisplay_dollar();
7672 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
7673 || gchar_cursor() != NUL)
7674 {
7675 start_arrow(&curwin->w_cursor);
7676 (void)fwd_word(1L, FALSE, 0);
7677 curwin->w_set_curswant = TRUE;
7678 }
7679 else
7680 vim_beep();
7681}
7682
7683 static void
7684ins_up(startcol)
7685 int startcol; /* when TRUE move to Insstart.col */
7686{
7687 pos_T tpos;
7688 linenr_T old_topline = curwin->w_topline;
7689#ifdef FEAT_DIFF
7690 int old_topfill = curwin->w_topfill;
7691#endif
7692
7693 undisplay_dollar();
7694 tpos = curwin->w_cursor;
7695 if (cursor_up(1L, TRUE) == OK)
7696 {
7697 if (startcol)
7698 coladvance(getvcol_nolist(&Insstart));
7699 if (old_topline != curwin->w_topline
7700#ifdef FEAT_DIFF
7701 || old_topfill != curwin->w_topfill
7702#endif
7703 )
7704 redraw_later(VALID);
7705 start_arrow(&tpos);
7706#ifdef FEAT_CINDENT
7707 can_cindent = TRUE;
7708#endif
7709 }
7710 else
7711 vim_beep();
7712}
7713
7714 static void
7715ins_pageup()
7716{
7717 pos_T tpos;
7718
7719 undisplay_dollar();
7720 tpos = curwin->w_cursor;
7721 if (onepage(BACKWARD, 1L) == OK)
7722 {
7723 start_arrow(&tpos);
7724#ifdef FEAT_CINDENT
7725 can_cindent = TRUE;
7726#endif
7727 }
7728 else
7729 vim_beep();
7730}
7731
7732 static void
7733ins_down(startcol)
7734 int startcol; /* when TRUE move to Insstart.col */
7735{
7736 pos_T tpos;
7737 linenr_T old_topline = curwin->w_topline;
7738#ifdef FEAT_DIFF
7739 int old_topfill = curwin->w_topfill;
7740#endif
7741
7742 undisplay_dollar();
7743 tpos = curwin->w_cursor;
7744 if (cursor_down(1L, TRUE) == OK)
7745 {
7746 if (startcol)
7747 coladvance(getvcol_nolist(&Insstart));
7748 if (old_topline != curwin->w_topline
7749#ifdef FEAT_DIFF
7750 || old_topfill != curwin->w_topfill
7751#endif
7752 )
7753 redraw_later(VALID);
7754 start_arrow(&tpos);
7755#ifdef FEAT_CINDENT
7756 can_cindent = TRUE;
7757#endif
7758 }
7759 else
7760 vim_beep();
7761}
7762
7763 static void
7764ins_pagedown()
7765{
7766 pos_T tpos;
7767
7768 undisplay_dollar();
7769 tpos = curwin->w_cursor;
7770 if (onepage(FORWARD, 1L) == OK)
7771 {
7772 start_arrow(&tpos);
7773#ifdef FEAT_CINDENT
7774 can_cindent = TRUE;
7775#endif
7776 }
7777 else
7778 vim_beep();
7779}
7780
7781#ifdef FEAT_DND
7782 static void
7783ins_drop()
7784{
7785 do_put('~', BACKWARD, 1L, PUT_CURSEND);
7786}
7787#endif
7788
7789/*
7790 * Handle TAB in Insert or Replace mode.
7791 * Return TRUE when the TAB needs to be inserted like a normal character.
7792 */
7793 static int
7794ins_tab()
7795{
7796 int ind;
7797 int i;
7798 int temp;
7799
7800 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
7801 Insstart_blank_vcol = get_nolist_virtcol();
7802 if (echeck_abbr(TAB + ABBR_OFF))
7803 return FALSE;
7804
7805 ind = inindent(0);
7806#ifdef FEAT_CINDENT
7807 if (ind)
7808 can_cindent = FALSE;
7809#endif
7810
7811 /*
7812 * When nothing special, insert TAB like a normal character
7813 */
7814 if (!curbuf->b_p_et
7815 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
7816 && curbuf->b_p_sts == 0)
7817 return TRUE;
7818
7819 if (stop_arrow() == FAIL)
7820 return TRUE;
7821
7822 did_ai = FALSE;
7823#ifdef FEAT_SMARTINDENT
7824 did_si = FALSE;
7825 can_si = FALSE;
7826 can_si_back = FALSE;
7827#endif
7828 AppendToRedobuff((char_u *)"\t");
7829
7830 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
7831 temp = (int)curbuf->b_p_sw;
7832 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
7833 temp = (int)curbuf->b_p_sts;
7834 else /* otherwise use 'tabstop' */
7835 temp = (int)curbuf->b_p_ts;
7836 temp -= get_nolist_virtcol() % temp;
7837
7838 /*
7839 * Insert the first space with ins_char(). It will delete one char in
7840 * replace mode. Insert the rest with ins_str(); it will not delete any
7841 * chars. For VREPLACE mode, we use ins_char() for all characters.
7842 */
7843 ins_char(' ');
7844 while (--temp > 0)
7845 {
7846#ifdef FEAT_VREPLACE
7847 if (State & VREPLACE_FLAG)
7848 ins_char(' ');
7849 else
7850#endif
7851 {
7852 ins_str((char_u *)" ");
7853 if (State & REPLACE_FLAG) /* no char replaced */
7854 replace_push(NUL);
7855 }
7856 }
7857
7858 /*
7859 * When 'expandtab' not set: Replace spaces by TABs where possible.
7860 */
7861 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
7862 {
7863 char_u *ptr;
7864#ifdef FEAT_VREPLACE
7865 char_u *saved_line = NULL; /* init for GCC */
7866 pos_T pos;
7867#endif
7868 pos_T fpos;
7869 pos_T *cursor;
7870 colnr_T want_vcol, vcol;
7871 int change_col = -1;
7872 int save_list = curwin->w_p_list;
7873
7874 /*
7875 * Get the current line. For VREPLACE mode, don't make real changes
7876 * yet, just work on a copy of the line.
7877 */
7878#ifdef FEAT_VREPLACE
7879 if (State & VREPLACE_FLAG)
7880 {
7881 pos = curwin->w_cursor;
7882 cursor = &pos;
7883 saved_line = vim_strsave(ml_get_curline());
7884 if (saved_line == NULL)
7885 return FALSE;
7886 ptr = saved_line + pos.col;
7887 }
7888 else
7889#endif
7890 {
7891 ptr = ml_get_cursor();
7892 cursor = &curwin->w_cursor;
7893 }
7894
7895 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
7896 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
7897 curwin->w_p_list = FALSE;
7898
7899 /* Find first white before the cursor */
7900 fpos = curwin->w_cursor;
7901 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
7902 {
7903 --fpos.col;
7904 --ptr;
7905 }
7906
7907 /* In Replace mode, don't change characters before the insert point. */
7908 if ((State & REPLACE_FLAG)
7909 && fpos.lnum == Insstart.lnum
7910 && fpos.col < Insstart.col)
7911 {
7912 ptr += Insstart.col - fpos.col;
7913 fpos.col = Insstart.col;
7914 }
7915
7916 /* compute virtual column numbers of first white and cursor */
7917 getvcol(curwin, &fpos, &vcol, NULL, NULL);
7918 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
7919
7920 /* Use as many TABs as possible. Beware of 'showbreak' and
7921 * 'linebreak' adding extra virtual columns. */
7922 while (vim_iswhite(*ptr))
7923 {
7924 i = lbr_chartabsize((char_u *)"\t", vcol);
7925 if (vcol + i > want_vcol)
7926 break;
7927 if (*ptr != TAB)
7928 {
7929 *ptr = TAB;
7930 if (change_col < 0)
7931 {
7932 change_col = fpos.col; /* Column of first change */
7933 /* May have to adjust Insstart */
7934 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
7935 Insstart.col = fpos.col;
7936 }
7937 }
7938 ++fpos.col;
7939 ++ptr;
7940 vcol += i;
7941 }
7942
7943 if (change_col >= 0)
7944 {
7945 int repl_off = 0;
7946
7947 /* Skip over the spaces we need. */
7948 while (vcol < want_vcol && *ptr == ' ')
7949 {
7950 vcol += lbr_chartabsize(ptr, vcol);
7951 ++ptr;
7952 ++repl_off;
7953 }
7954 if (vcol > want_vcol)
7955 {
7956 /* Must have a char with 'showbreak' just before it. */
7957 --ptr;
7958 --repl_off;
7959 }
7960 fpos.col += repl_off;
7961
7962 /* Delete following spaces. */
7963 i = cursor->col - fpos.col;
7964 if (i > 0)
7965 {
7966 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
7967 /* correct replace stack. */
7968 if ((State & REPLACE_FLAG)
7969#ifdef FEAT_VREPLACE
7970 && !(State & VREPLACE_FLAG)
7971#endif
7972 )
7973 for (temp = i; --temp >= 0; )
7974 replace_join(repl_off);
7975 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00007976#ifdef FEAT_NETBEANS_INTG
7977 if (usingNetbeans)
7978 {
7979 netbeans_removed(curbuf, fpos.lnum, cursor->col,
7980 (long)(i + 1));
7981 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
7982 (char_u *)"\t", 1);
7983 }
7984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 cursor->col -= i;
7986
7987#ifdef FEAT_VREPLACE
7988 /*
7989 * In VREPLACE mode, we haven't changed anything yet. Do it now by
7990 * backspacing over the changed spacing and then inserting the new
7991 * spacing.
7992 */
7993 if (State & VREPLACE_FLAG)
7994 {
7995 /* Backspace from real cursor to change_col */
7996 backspace_until_column(change_col);
7997
7998 /* Insert each char in saved_line from changed_col to
7999 * ptr-cursor */
8000 ins_bytes_len(saved_line + change_col,
8001 cursor->col - change_col);
8002 }
8003#endif
8004 }
8005
8006#ifdef FEAT_VREPLACE
8007 if (State & VREPLACE_FLAG)
8008 vim_free(saved_line);
8009#endif
8010 curwin->w_p_list = save_list;
8011 }
8012
8013 return FALSE;
8014}
8015
8016/*
8017 * Handle CR or NL in insert mode.
8018 * Return TRUE when out of memory or can't undo.
8019 */
8020 static int
8021ins_eol(c)
8022 int c;
8023{
8024 int i;
8025
8026 if (echeck_abbr(c + ABBR_OFF))
8027 return FALSE;
8028 if (stop_arrow() == FAIL)
8029 return TRUE;
8030 undisplay_dollar();
8031
8032 /*
8033 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8034 * character under the cursor. Only push a NUL on the replace stack,
8035 * nothing to put back when the NL is deleted.
8036 */
8037 if ((State & REPLACE_FLAG)
8038#ifdef FEAT_VREPLACE
8039 && !(State & VREPLACE_FLAG)
8040#endif
8041 )
8042 replace_push(NUL);
8043
8044 /*
8045 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8046 * replacing the next line, so we push all of the characters left on the
8047 * line onto the replace stack. This is not done here though, it is done
8048 * in open_line().
8049 */
8050
8051#ifdef FEAT_RIGHTLEFT
8052# ifdef FEAT_FKMAP
8053 if (p_altkeymap && p_fkmap)
8054 fkmap(NL);
8055# endif
8056 /* NL in reverse insert will always start in the end of
8057 * current line. */
8058 if (revins_on)
8059 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8060#endif
8061
8062 AppendToRedobuff(NL_STR);
8063 i = open_line(FORWARD,
8064#ifdef FEAT_COMMENTS
8065 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8066#endif
8067 0, old_indent);
8068 old_indent = 0;
8069#ifdef FEAT_CINDENT
8070 can_cindent = TRUE;
8071#endif
8072
8073 return (!i);
8074}
8075
8076#ifdef FEAT_DIGRAPHS
8077/*
8078 * Handle digraph in insert mode.
8079 * Returns character still to be inserted, or NUL when nothing remaining to be
8080 * done.
8081 */
8082 static int
8083ins_digraph()
8084{
8085 int c;
8086 int cc;
8087
8088 pc_status = PC_STATUS_UNSET;
8089 if (redrawing() && !char_avail())
8090 {
8091 /* may need to redraw when no more chars available now */
8092 ins_redraw();
8093
8094 edit_putchar('?', TRUE);
8095#ifdef FEAT_CMDL_INFO
8096 add_to_showcmd_c(Ctrl_K);
8097#endif
8098 }
8099
8100#ifdef USE_ON_FLY_SCROLL
8101 dont_scroll = TRUE; /* disallow scrolling here */
8102#endif
8103
8104 /* don't map the digraph chars. This also prevents the
8105 * mode message to be deleted when ESC is hit */
8106 ++no_mapping;
8107 ++allow_keys;
8108 c = safe_vgetc();
8109 --no_mapping;
8110 --allow_keys;
8111 if (IS_SPECIAL(c) || mod_mask) /* special key */
8112 {
8113#ifdef FEAT_CMDL_INFO
8114 clear_showcmd();
8115#endif
8116 insert_special(c, TRUE, FALSE);
8117 return NUL;
8118 }
8119 if (c != ESC)
8120 {
8121 if (redrawing() && !char_avail())
8122 {
8123 /* may need to redraw when no more chars available now */
8124 ins_redraw();
8125
8126 if (char2cells(c) == 1)
8127 {
8128 /* first remove the '?', otherwise it's restored when typing
8129 * an ESC next */
8130 edit_unputchar();
8131 ins_redraw();
8132 edit_putchar(c, TRUE);
8133 }
8134#ifdef FEAT_CMDL_INFO
8135 add_to_showcmd_c(c);
8136#endif
8137 }
8138 ++no_mapping;
8139 ++allow_keys;
8140 cc = safe_vgetc();
8141 --no_mapping;
8142 --allow_keys;
8143 if (cc != ESC)
8144 {
8145 AppendToRedobuff((char_u *)CTRL_V_STR);
8146 c = getdigraph(c, cc, TRUE);
8147#ifdef FEAT_CMDL_INFO
8148 clear_showcmd();
8149#endif
8150 return c;
8151 }
8152 }
8153 edit_unputchar();
8154#ifdef FEAT_CMDL_INFO
8155 clear_showcmd();
8156#endif
8157 return NUL;
8158}
8159#endif
8160
8161/*
8162 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8163 * Returns the char to be inserted, or NUL if none found.
8164 */
8165 static int
8166ins_copychar(lnum)
8167 linenr_T lnum;
8168{
8169 int c;
8170 int temp;
8171 char_u *ptr, *prev_ptr;
8172
8173 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8174 {
8175 vim_beep();
8176 return NUL;
8177 }
8178
8179 /* try to advance to the cursor column */
8180 temp = 0;
8181 ptr = ml_get(lnum);
8182 prev_ptr = ptr;
8183 validate_virtcol();
8184 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8185 {
8186 prev_ptr = ptr;
8187 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8188 }
8189 if ((colnr_T)temp > curwin->w_virtcol)
8190 ptr = prev_ptr;
8191
8192#ifdef FEAT_MBYTE
8193 c = (*mb_ptr2char)(ptr);
8194#else
8195 c = *ptr;
8196#endif
8197 if (c == NUL)
8198 vim_beep();
8199 return c;
8200}
8201
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008202/*
8203 * CTRL-Y or CTRL-E typed in Insert mode.
8204 */
8205 static int
8206ins_ctrl_ey(tc)
8207 int tc;
8208{
8209 int c = tc;
8210
8211#ifdef FEAT_INS_EXPAND
8212 if (ctrl_x_mode == CTRL_X_SCROLL)
8213 {
8214 if (c == Ctrl_Y)
8215 scrolldown_clamp();
8216 else
8217 scrollup_clamp();
8218 redraw_later(VALID);
8219 }
8220 else
8221#endif
8222 {
8223 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8224 if (c != NUL)
8225 {
8226 long tw_save;
8227
8228 /* The character must be taken literally, insert like it
8229 * was typed after a CTRL-V, and pretend 'textwidth'
8230 * wasn't set. Digits, 'o' and 'x' are special after a
8231 * CTRL-V, don't use it for these. */
8232 if (c < 256 && !isalnum(c))
8233 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8234 tw_save = curbuf->b_p_tw;
8235 curbuf->b_p_tw = -1;
8236 insert_special(c, TRUE, FALSE);
8237 curbuf->b_p_tw = tw_save;
8238#ifdef FEAT_RIGHTLEFT
8239 revins_chars++;
8240 revins_legal++;
8241#endif
8242 c = Ctrl_V; /* pretend CTRL-V is last character */
8243 auto_format(FALSE, TRUE);
8244 }
8245 }
8246 return c;
8247}
8248
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249#ifdef FEAT_SMARTINDENT
8250/*
8251 * Try to do some very smart auto-indenting.
8252 * Used when inserting a "normal" character.
8253 */
8254 static void
8255ins_try_si(c)
8256 int c;
8257{
8258 pos_T *pos, old_pos;
8259 char_u *ptr;
8260 int i;
8261 int temp;
8262
8263 /*
8264 * do some very smart indenting when entering '{' or '}'
8265 */
8266 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8267 {
8268 /*
8269 * for '}' set indent equal to indent of line containing matching '{'
8270 */
8271 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8272 {
8273 old_pos = curwin->w_cursor;
8274 /*
8275 * If the matching '{' has a ')' immediately before it (ignoring
8276 * white-space), then line up with the start of the line
8277 * containing the matching '(' if there is one. This handles the
8278 * case where an "if (..\n..) {" statement continues over multiple
8279 * lines -- webb
8280 */
8281 ptr = ml_get(pos->lnum);
8282 i = pos->col;
8283 if (i > 0) /* skip blanks before '{' */
8284 while (--i > 0 && vim_iswhite(ptr[i]))
8285 ;
8286 curwin->w_cursor.lnum = pos->lnum;
8287 curwin->w_cursor.col = i;
8288 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8289 curwin->w_cursor = *pos;
8290 i = get_indent();
8291 curwin->w_cursor = old_pos;
8292#ifdef FEAT_VREPLACE
8293 if (State & VREPLACE_FLAG)
8294 change_indent(INDENT_SET, i, FALSE, NUL);
8295 else
8296#endif
8297 (void)set_indent(i, SIN_CHANGED);
8298 }
8299 else if (curwin->w_cursor.col > 0)
8300 {
8301 /*
8302 * when inserting '{' after "O" reduce indent, but not
8303 * more than indent of previous line
8304 */
8305 temp = TRUE;
8306 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8307 {
8308 old_pos = curwin->w_cursor;
8309 i = get_indent();
8310 while (curwin->w_cursor.lnum > 1)
8311 {
8312 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8313
8314 /* ignore empty lines and lines starting with '#'. */
8315 if (*ptr != '#' && *ptr != NUL)
8316 break;
8317 }
8318 if (get_indent() >= i)
8319 temp = FALSE;
8320 curwin->w_cursor = old_pos;
8321 }
8322 if (temp)
8323 shift_line(TRUE, FALSE, 1);
8324 }
8325 }
8326
8327 /*
8328 * set indent of '#' always to 0
8329 */
8330 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8331 {
8332 /* remember current indent for next line */
8333 old_indent = get_indent();
8334 (void)set_indent(0, SIN_CHANGED);
8335 }
8336
8337 /* Adjust ai_col, the char at this position can be deleted. */
8338 if (ai_col > curwin->w_cursor.col)
8339 ai_col = curwin->w_cursor.col;
8340}
8341#endif
8342
8343/*
8344 * Get the value that w_virtcol would have when 'list' is off.
8345 * Unless 'cpo' contains the 'L' flag.
8346 */
8347 static colnr_T
8348get_nolist_virtcol()
8349{
8350 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8351 return getvcol_nolist(&curwin->w_cursor);
8352 validate_virtcol();
8353 return curwin->w_virtcol;
8354}