blob: f3842292630b61e172fb8a142bb2f94a9b667d5c [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));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000114static int pum_wanted __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int dir, int flags, int thesaurus));
116static void ins_compl_free __ARGS((void));
117static void ins_compl_clear __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000118static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
120static int ins_compl_get_exp __ARGS((pos_T *ini, int dir));
121static void ins_compl_delete __ARGS((void));
122static void ins_compl_insert __ARGS((void));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000123static int ins_compl_next __ARGS((int allow_get_expansion, int count));
124static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000125static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000126static int ins_compl_key2count __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127static int ins_complete __ARGS((int c));
128static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
129#endif /* FEAT_INS_EXPAND */
130
131#define BACKSPACE_CHAR 1
132#define BACKSPACE_WORD 2
133#define BACKSPACE_WORD_NOT_SPACE 3
134#define BACKSPACE_LINE 4
135
136static void ins_redraw __ARGS((void));
137static void ins_ctrl_v __ARGS((void));
138static void undisplay_dollar __ARGS((void));
139static void insert_special __ARGS((int, int, int));
140static void check_auto_format __ARGS((int));
141static void redo_literal __ARGS((int c));
142static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000143#ifdef FEAT_SYN_HL
144static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000145static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000146static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
149static int echeck_abbr __ARGS((int));
150static void replace_push_off __ARGS((int c));
151static int replace_pop __ARGS((void));
152static void replace_join __ARGS((int off));
153static void replace_pop_ins __ARGS((void));
154#ifdef FEAT_MBYTE
155static void mb_replace_pop_ins __ARGS((int cc));
156#endif
157static void replace_flush __ARGS((void));
158static void replace_do_bs __ARGS((void));
159#ifdef FEAT_CINDENT
160static int cindent_on __ARGS((void));
161#endif
162static void ins_reg __ARGS((void));
163static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000164static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000165static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#ifdef FEAT_RIGHTLEFT
167static void ins_ctrl_ __ARGS((void));
168#endif
169#ifdef FEAT_VISUAL
170static int ins_start_select __ARGS((int c));
171#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000172static void ins_insert __ARGS((int replaceState));
173static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174static void ins_shift __ARGS((int c, int lastc));
175static void ins_del __ARGS((void));
176static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
177#ifdef FEAT_MOUSE
178static void ins_mouse __ARGS((int c));
179static void ins_mousescroll __ARGS((int up));
180#endif
181static void ins_left __ARGS((void));
182static void ins_home __ARGS((int c));
183static void ins_end __ARGS((int c));
184static void ins_s_left __ARGS((void));
185static void ins_right __ARGS((void));
186static void ins_s_right __ARGS((void));
187static void ins_up __ARGS((int startcol));
188static void ins_pageup __ARGS((void));
189static void ins_down __ARGS((int startcol));
190static void ins_pagedown __ARGS((void));
191#ifdef FEAT_DND
192static void ins_drop __ARGS((void));
193#endif
194static int ins_tab __ARGS((void));
195static int ins_eol __ARGS((int c));
196#ifdef FEAT_DIGRAPHS
197static int ins_digraph __ARGS((void));
198#endif
199static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000200static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifdef FEAT_SMARTINDENT
202static void ins_try_si __ARGS((int c));
203#endif
204static colnr_T get_nolist_virtcol __ARGS((void));
205
206static colnr_T Insstart_textlen; /* length of line when insert started */
207static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
208
209static char_u *last_insert = NULL; /* the text of the previous insert,
210 K_SPECIAL and CSI are escaped */
211static int last_insert_skip; /* nr of chars in front of previous insert */
212static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000213static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
215#ifdef FEAT_CINDENT
216static int can_cindent; /* may do cindenting on this line */
217#endif
218
219static int old_indent = 0; /* for ^^D command in insert mode */
220
221#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000222static int revins_on; /* reverse insert mode on */
223static int revins_chars; /* how much to skip after edit */
224static int revins_legal; /* was the last char 'legal'? */
225static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
227
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228static int ins_need_undo; /* call u_save() before inserting a
229 char. Set when edit() is called.
230 after that arrow_used is used. */
231
232static int did_add_space = FALSE; /* auto_format() added an extra space
233 under the cursor */
234
235/*
236 * edit(): Start inserting text.
237 *
238 * "cmdchar" can be:
239 * 'i' normal insert command
240 * 'a' normal append command
241 * 'R' replace command
242 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
243 * but still only one <CR> is inserted. The <Esc> is not used for redo.
244 * 'g' "gI" command.
245 * 'V' "gR" command for Virtual Replace mode.
246 * 'v' "gr" command for single character Virtual Replace mode.
247 *
248 * This function is not called recursively. For CTRL-O commands, it returns
249 * and lets the caller handle the Normal-mode command.
250 *
251 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
252 */
253 int
254edit(cmdchar, startln, count)
255 int cmdchar;
256 int startln; /* if set, insert at start of line */
257 long count;
258{
259 int c = 0;
260 char_u *ptr;
261 int lastc;
262 colnr_T mincol;
263 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 int i;
265 int did_backspace = TRUE; /* previous char was backspace */
266#ifdef FEAT_CINDENT
267 int line_is_white = FALSE; /* line is empty before insert */
268#endif
269 linenr_T old_topline = 0; /* topline before insertion */
270#ifdef FEAT_DIFF
271 int old_topfill = -1;
272#endif
273 int inserted_space = FALSE; /* just inserted a space */
274 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000275 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000277 /* Remember whether editing was restarted after CTRL-O. */
278 did_restart_edit = restart_edit;
279
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 /* sleep before redrawing, needed for "CTRL-O :" that results in an
281 * error message */
282 check_for_delay(TRUE);
283
284#ifdef HAVE_SANDBOX
285 /* Don't allow inserting in the sandbox. */
286 if (sandbox != 0)
287 {
288 EMSG(_(e_sandbox));
289 return FALSE;
290 }
291#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000292 /* Don't allow changes in the buffer while editing the cmdline. The
293 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000294 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000295 {
296 EMSG(_(e_secure));
297 return FALSE;
298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299
300#ifdef FEAT_INS_EXPAND
301 ins_compl_clear(); /* clear stuff for CTRL-X mode */
302#endif
303
Bram Moolenaar843ee412004-06-30 16:16:41 +0000304#ifdef FEAT_AUTOCMD
305 /*
306 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
307 */
308 if (cmdchar != 'r' && cmdchar != 'v')
309 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000310# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000311 if (cmdchar == 'R')
312 ptr = (char_u *)"r";
313 else if (cmdchar == 'V')
314 ptr = (char_u *)"v";
315 else
316 ptr = (char_u *)"i";
317 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000318# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000319 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
320 }
321#endif
322
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#ifdef FEAT_MOUSE
324 /*
325 * When doing a paste with the middle mouse button, Insstart is set to
326 * where the paste started.
327 */
328 if (where_paste_started.lnum != 0)
329 Insstart = where_paste_started;
330 else
331#endif
332 {
333 Insstart = curwin->w_cursor;
334 if (startln)
335 Insstart.col = 0;
336 }
337 Insstart_textlen = linetabsize(ml_get_curline());
338 Insstart_blank_vcol = MAXCOL;
339 if (!did_ai)
340 ai_col = 0;
341
342 if (cmdchar != NUL && restart_edit == 0)
343 {
344 ResetRedobuff();
345 AppendNumberToRedobuff(count);
346#ifdef FEAT_VREPLACE
347 if (cmdchar == 'V' || cmdchar == 'v')
348 {
349 /* "gR" or "gr" command */
350 AppendCharToRedobuff('g');
351 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
352 }
353 else
354#endif
355 {
356 AppendCharToRedobuff(cmdchar);
357 if (cmdchar == 'g') /* "gI" command */
358 AppendCharToRedobuff('I');
359 else if (cmdchar == 'r') /* "r<CR>" command */
360 count = 1; /* insert only one <CR> */
361 }
362 }
363
364 if (cmdchar == 'R')
365 {
366#ifdef FEAT_FKMAP
367 if (p_fkmap && p_ri)
368 {
369 beep_flush();
370 EMSG(farsi_text_3); /* encoded in Farsi */
371 State = INSERT;
372 }
373 else
374#endif
375 State = REPLACE;
376 }
377#ifdef FEAT_VREPLACE
378 else if (cmdchar == 'V' || cmdchar == 'v')
379 {
380 State = VREPLACE;
381 replaceState = VREPLACE;
382 orig_line_count = curbuf->b_ml.ml_line_count;
383 vr_lines_changed = 1;
384 }
385#endif
386 else
387 State = INSERT;
388
389 stop_insert_mode = FALSE;
390
391 /*
392 * Need to recompute the cursor position, it might move when the cursor is
393 * on a TAB or special character.
394 */
395 curs_columns(TRUE);
396
397 /*
398 * Enable langmap or IME, indicated by 'iminsert'.
399 * Note that IME may enabled/disabled without us noticing here, thus the
400 * 'iminsert' value may not reflect what is actually used. It is updated
401 * when hitting <Esc>.
402 */
403 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
404 State |= LANGMAP;
405#ifdef USE_IM_CONTROL
406 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
407#endif
408
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#ifdef FEAT_MOUSE
410 setmouse();
411#endif
412#ifdef FEAT_CMDL_INFO
413 clear_showcmd();
414#endif
415#ifdef FEAT_RIGHTLEFT
416 /* there is no reverse replace mode */
417 revins_on = (State == INSERT && p_ri);
418 if (revins_on)
419 undisplay_dollar();
420 revins_chars = 0;
421 revins_legal = 0;
422 revins_scol = -1;
423#endif
424
425 /*
426 * Handle restarting Insert mode.
427 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
428 * restart_edit non-zero, and something in the stuff buffer.
429 */
430 if (restart_edit != 0 && stuff_empty())
431 {
432#ifdef FEAT_MOUSE
433 /*
434 * After a paste we consider text typed to be part of the insert for
435 * the pasted text. You can backspace over the pasted text too.
436 */
437 if (where_paste_started.lnum)
438 arrow_used = FALSE;
439 else
440#endif
441 arrow_used = TRUE;
442 restart_edit = 0;
443
444 /*
445 * If the cursor was after the end-of-line before the CTRL-O and it is
446 * now at the end-of-line, put it after the end-of-line (this is not
447 * correct in very rare cases).
448 * Also do this if curswant is greater than the current virtual
449 * column. Eg after "^O$" or "^O80|".
450 */
451 validate_virtcol();
452 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000453 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 || curwin->w_curswant > curwin->w_virtcol)
455 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
456 {
457 if (ptr[1] == NUL)
458 ++curwin->w_cursor.col;
459#ifdef FEAT_MBYTE
460 else if (has_mbyte)
461 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000462 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 if (ptr[i] == NUL)
464 curwin->w_cursor.col += i;
465 }
466#endif
467 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000468 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 }
470 else
471 arrow_used = FALSE;
472
473 /* we are in insert mode now, don't need to start it anymore */
474 need_start_insertmode = FALSE;
475
476 /* Need to save the line for undo before inserting the first char. */
477 ins_need_undo = TRUE;
478
479#ifdef FEAT_MOUSE
480 where_paste_started.lnum = 0;
481#endif
482#ifdef FEAT_CINDENT
483 can_cindent = TRUE;
484#endif
485#ifdef FEAT_FOLDING
486 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
487 * restarting. */
488 if (!p_im && did_restart_edit == 0)
489 foldOpenCursor();
490#endif
491
492 /*
493 * If 'showmode' is set, show the current (insert/replace/..) mode.
494 * A warning message for changing a readonly file is given here, before
495 * actually changing anything. It's put after the mode, if any.
496 */
497 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000498 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 i = showmode();
500
501 if (!p_im && did_restart_edit == 0)
502 change_warning(i + 1);
503
504#ifdef CURSOR_SHAPE
505 ui_cursor_shape(); /* may show different cursor shape */
506#endif
507#ifdef FEAT_DIGRAPHS
508 do_digraph(-1); /* clear digraphs */
509#endif
510
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000511 /*
512 * Get the current length of the redo buffer, those characters have to be
513 * skipped if we want to get to the inserted characters.
514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 ptr = get_inserted();
516 if (ptr == NULL)
517 new_insert_skip = 0;
518 else
519 {
520 new_insert_skip = (int)STRLEN(ptr);
521 vim_free(ptr);
522 }
523
524 old_indent = 0;
525
526 /*
527 * Main loop in Insert mode: repeat until Insert mode is left.
528 */
529 for (;;)
530 {
531#ifdef FEAT_RIGHTLEFT
532 if (!revins_legal)
533 revins_scol = -1; /* reset on illegal motions */
534 else
535 revins_legal = 0;
536#endif
537 if (arrow_used) /* don't repeat insert when arrow key used */
538 count = 0;
539
540 if (stop_insert_mode)
541 {
542 /* ":stopinsert" used or 'insertmode' reset */
543 count = 0;
544 goto doESCkey;
545 }
546
547 /* set curwin->w_curswant for next K_DOWN or K_UP */
548 if (!arrow_used)
549 curwin->w_set_curswant = TRUE;
550
551 /* If there is no typeahead may check for timestamps (e.g., for when a
552 * menu invoked a shell command). */
553 if (stuff_empty())
554 {
555 did_check_timestamps = FALSE;
556 if (need_check_timestamps)
557 check_timestamps(FALSE);
558 }
559
560 /*
561 * When emsg() was called msg_scroll will have been set.
562 */
563 msg_scroll = FALSE;
564
565#ifdef FEAT_GUI
566 /* When 'mousefocus' is set a mouse movement may have taken us to
567 * another window. "need_mouse_correct" may then be set because of an
568 * autocommand. */
569 if (need_mouse_correct)
570 gui_mouse_correct();
571#endif
572
573#ifdef FEAT_FOLDING
574 /* Open fold at the cursor line, according to 'foldopen'. */
575 if (fdo_flags & FDO_INSERT)
576 foldOpenCursor();
577 /* Close folds where the cursor isn't, according to 'foldclose' */
578 if (!char_avail())
579 foldCheckClose();
580#endif
581
582 /*
583 * If we inserted a character at the last position of the last line in
584 * the window, scroll the window one line up. This avoids an extra
585 * redraw.
586 * This is detected when the cursor column is smaller after inserting
587 * something.
588 * Don't do this when the topline changed already, it has
589 * already been adjusted (by insertchar() calling open_line())).
590 */
591 if (curbuf->b_mod_set
592 && curwin->w_p_wrap
593 && !did_backspace
594 && curwin->w_topline == old_topline
595#ifdef FEAT_DIFF
596 && curwin->w_topfill == old_topfill
597#endif
598 )
599 {
600 mincol = curwin->w_wcol;
601 validate_cursor_col();
602
603 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
604 && curwin->w_wrow == W_WINROW(curwin)
605 + curwin->w_height - 1 - p_so
606 && (curwin->w_cursor.lnum != curwin->w_topline
607#ifdef FEAT_DIFF
608 || curwin->w_topfill > 0
609#endif
610 ))
611 {
612#ifdef FEAT_DIFF
613 if (curwin->w_topfill > 0)
614 --curwin->w_topfill;
615 else
616#endif
617#ifdef FEAT_FOLDING
618 if (hasFolding(curwin->w_topline, NULL, &old_topline))
619 set_topline(curwin, old_topline + 1);
620 else
621#endif
622 set_topline(curwin, curwin->w_topline + 1);
623 }
624 }
625
626 /* May need to adjust w_topline to show the cursor. */
627 update_topline();
628
629 did_backspace = FALSE;
630
631 validate_cursor(); /* may set must_redraw */
632
633 /*
634 * Redraw the display when no characters are waiting.
635 * Also shows mode, ruler and positions cursor.
636 */
637 ins_redraw();
638
639#ifdef FEAT_SCROLLBIND
640 if (curwin->w_p_scb)
641 do_check_scrollbind(TRUE);
642#endif
643
644 update_curswant();
645 old_topline = curwin->w_topline;
646#ifdef FEAT_DIFF
647 old_topfill = curwin->w_topfill;
648#endif
649
650#ifdef USE_ON_FLY_SCROLL
651 dont_scroll = FALSE; /* allow scrolling here */
652#endif
653
654 /*
655 * Get a character for Insert mode.
656 */
657 lastc = c; /* remember previous char for CTRL-D */
658 c = safe_vgetc();
659
660#ifdef FEAT_RIGHTLEFT
661 if (p_hkmap && KeyTyped)
662 c = hkmap(c); /* Hebrew mode mapping */
663#endif
664#ifdef FEAT_FKMAP
665 if (p_fkmap && KeyTyped)
666 c = fkmap(c); /* Farsi mode mapping */
667#endif
668
669#ifdef FEAT_INS_EXPAND
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000670 /* When the popup menu is visible cursor keys change the selection. */
671 if (c == K_UP && pum_visible())
672 c = Ctrl_P;
673 if (c == K_DOWN && pum_visible())
674 c = Ctrl_N;
675
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
677 * it does fix up the text when finishing completion. */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000678 if (c != K_IGNORE)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000679 {
680 if (ins_compl_prep(c))
681 continue;
682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683#endif
684
Bram Moolenaar488c6512005-08-11 20:09:58 +0000685 /* CTRL-\ CTRL-N goes to Normal mode,
686 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
687 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 if (c == Ctrl_BSL)
689 {
690 /* may need to redraw when no more chars available now */
691 ins_redraw();
692 ++no_mapping;
693 ++allow_keys;
694 c = safe_vgetc();
695 --no_mapping;
696 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000697 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000699 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 vungetc(c);
701 c = Ctrl_BSL;
702 }
703 else if (c == Ctrl_G && p_im)
704 continue;
705 else
706 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000707 if (c == Ctrl_O)
708 {
709 ins_ctrl_o();
710 ins_at_eol = FALSE; /* cursor keeps its column */
711 nomove = TRUE;
712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 count = 0;
714 goto doESCkey;
715 }
716 }
717
718#ifdef FEAT_DIGRAPHS
719 c = do_digraph(c);
720#endif
721
722#ifdef FEAT_INS_EXPAND
723 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
724 goto docomplete;
725#endif
726 if (c == Ctrl_V || c == Ctrl_Q)
727 {
728 ins_ctrl_v();
729 c = Ctrl_V; /* pretend CTRL-V is last typed character */
730 continue;
731 }
732
733#ifdef FEAT_CINDENT
734 if (cindent_on()
735# ifdef FEAT_INS_EXPAND
736 && ctrl_x_mode == 0
737# endif
738 )
739 {
740 /* A key name preceded by a bang means this key is not to be
741 * inserted. Skip ahead to the re-indenting below.
742 * A key name preceded by a star means that indenting has to be
743 * done before inserting the key. */
744 line_is_white = inindent(0);
745 if (in_cinkeys(c, '!', line_is_white))
746 goto force_cindent;
747 if (can_cindent && in_cinkeys(c, '*', line_is_white)
748 && stop_arrow() == OK)
749 do_c_expr_indent();
750 }
751#endif
752
753#ifdef FEAT_RIGHTLEFT
754 if (curwin->w_p_rl)
755 switch (c)
756 {
757 case K_LEFT: c = K_RIGHT; break;
758 case K_S_LEFT: c = K_S_RIGHT; break;
759 case K_C_LEFT: c = K_C_RIGHT; break;
760 case K_RIGHT: c = K_LEFT; break;
761 case K_S_RIGHT: c = K_S_LEFT; break;
762 case K_C_RIGHT: c = K_C_LEFT; break;
763 }
764#endif
765
766#ifdef FEAT_VISUAL
767 /*
768 * If 'keymodel' contains "startsel", may start selection. If it
769 * does, a CTRL-O and c will be stuffed, we need to get these
770 * characters.
771 */
772 if (ins_start_select(c))
773 continue;
774#endif
775
776 /*
777 * The big switch to handle a character in insert mode.
778 */
779 switch (c)
780 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000781 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 if (echeck_abbr(ESC + ABBR_OFF))
783 break;
784 /*FALLTHROUGH*/
785
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000786 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787#ifdef FEAT_CMDWIN
788 if (c == Ctrl_C && cmdwin_type != 0)
789 {
790 /* Close the cmdline window. */
791 cmdwin_result = K_IGNORE;
792 got_int = FALSE; /* don't stop executing autocommands et al. */
793 goto doESCkey;
794 }
795#endif
796
797#ifdef UNIX
798do_intr:
799#endif
800 /* when 'insertmode' set, and not halfway a mapping, don't leave
801 * Insert mode */
802 if (goto_im())
803 {
804 if (got_int)
805 {
806 (void)vgetc(); /* flush all buffers */
807 got_int = FALSE;
808 }
809 else
810 vim_beep();
811 break;
812 }
813doESCkey:
814 /*
815 * This is the ONLY return from edit()!
816 */
817 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
818 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000819 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 o_lnum = curwin->w_cursor.lnum;
821
Bram Moolenaar488c6512005-08-11 20:09:58 +0000822 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000823 {
824#ifdef FEAT_AUTOCMD
825 if (cmdchar != 'r' && cmdchar != 'v')
826 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
827 FALSE, curbuf);
828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 continue;
832
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000833 case Ctrl_Z: /* suspend when 'insertmode' set */
834 if (!p_im)
835 goto normalchar; /* insert CTRL-Z as normal char */
836 stuffReadbuff((char_u *)":st\r");
837 c = Ctrl_O;
838 /*FALLTHROUGH*/
839
840 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000841#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000842 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000843 goto docomplete;
844#endif
845 if (echeck_abbr(Ctrl_O + ABBR_OFF))
846 break;
847 ins_ctrl_o();
848 count = 0;
849 goto doESCkey;
850
Bram Moolenaar572cb562005-08-05 21:35:02 +0000851 case K_INS: /* toggle insert/replace mode */
852 case K_KINS:
853 ins_insert(replaceState);
854 break;
855
856 case K_SELECT: /* end of Select mode mapping - ignore */
857 break;
858
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000859#ifdef FEAT_SNIFF
860 case K_SNIFF: /* Sniff command received */
861 stuffcharReadbuff(K_SNIFF);
862 goto doESCkey;
863#endif
864
865 case K_HELP: /* Help key works like <ESC> <Help> */
866 case K_F1:
867 case K_XF1:
868 stuffcharReadbuff(K_HELP);
869 if (p_im)
870 need_start_insertmode = TRUE;
871 goto doESCkey;
872
873#ifdef FEAT_NETBEANS_INTG
874 case K_F21: /* NetBeans command */
875 ++no_mapping; /* don't map the next key hits */
876 i = safe_vgetc();
877 --no_mapping;
878 netbeans_keycommand(i);
879 break;
880#endif
881
882 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 case NUL:
884 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000885 /* For ^@ the trailing ESC will end the insert, unless there is an
886 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
888 && c != Ctrl_A && !p_im)
889 goto doESCkey; /* quit insert mode */
890 inserted_space = FALSE;
891 break;
892
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000893 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 ins_reg();
895 auto_format(FALSE, TRUE);
896 inserted_space = FALSE;
897 break;
898
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000899 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 ins_ctrl_g();
901 break;
902
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000903 case Ctrl_HAT: /* switch input mode and/or langmap */
904 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 break;
906
907#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000908 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 if (!p_ari)
910 goto normalchar;
911 ins_ctrl_();
912 break;
913#endif
914
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000915 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
917 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
918 goto docomplete;
919#endif
920 /* FALLTHROUGH */
921
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000922 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923# ifdef FEAT_INS_EXPAND
924 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
925 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000926 if (has_compl_option(FALSE))
927 goto docomplete;
928 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 }
930# endif
931 ins_shift(c, lastc);
932 auto_format(FALSE, TRUE);
933 inserted_space = FALSE;
934 break;
935
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000936 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 case K_KDEL:
938 ins_del();
939 auto_format(FALSE, TRUE);
940 break;
941
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000942 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 case Ctrl_H:
944 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
945 auto_format(FALSE, TRUE);
946 break;
947
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000948 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
950 auto_format(FALSE, TRUE);
951 break;
952
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000953 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000954# ifdef FEAT_COMPL_FUNC
955 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000956 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000957 goto docomplete;
958# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
960 auto_format(FALSE, TRUE);
961 inserted_space = FALSE;
962 break;
963
964#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000965 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 case K_LEFTMOUSE_NM:
967 case K_LEFTDRAG:
968 case K_LEFTRELEASE:
969 case K_LEFTRELEASE_NM:
970 case K_MIDDLEMOUSE:
971 case K_MIDDLEDRAG:
972 case K_MIDDLERELEASE:
973 case K_RIGHTMOUSE:
974 case K_RIGHTDRAG:
975 case K_RIGHTRELEASE:
976 case K_X1MOUSE:
977 case K_X1DRAG:
978 case K_X1RELEASE:
979 case K_X2MOUSE:
980 case K_X2DRAG:
981 case K_X2RELEASE:
982 ins_mouse(c);
983 break;
984
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000985 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 ins_mousescroll(FALSE);
987 break;
988
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000989 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 ins_mousescroll(TRUE);
991 break;
992#endif
993
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000994 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 break;
996
Bram Moolenaar4770d092006-01-12 23:22:24 +0000997#ifdef FEAT_GUI_W32
998 /* On Win32 ignore <M-F4>, we get it when closing the window was
999 * cancelled. */
1000 case K_F4:
1001 if (mod_mask != MOD_MASK_ALT)
1002 goto normalchar;
1003 break;
1004#endif
1005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006#ifdef FEAT_GUI
1007 case K_VER_SCROLLBAR:
1008 ins_scroll();
1009 break;
1010
1011 case K_HOR_SCROLLBAR:
1012 ins_horscroll();
1013 break;
1014#endif
1015
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001016 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 case K_S_HOME:
1019 case K_C_HOME:
1020 ins_home(c);
1021 break;
1022
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001023 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 case K_S_END:
1026 case K_C_END:
1027 ins_end(c);
1028 break;
1029
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001030 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001031 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1032 ins_s_left();
1033 else
1034 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 break;
1036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 case K_C_LEFT:
1039 ins_s_left();
1040 break;
1041
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001042 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001043 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1044 ins_s_right();
1045 else
1046 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 break;
1048
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 case K_C_RIGHT:
1051 ins_s_right();
1052 break;
1053
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001054 case K_UP: /* <Up> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001055 if (mod_mask & MOD_MASK_SHIFT)
1056 ins_pageup();
1057 else
1058 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 break;
1060
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 case K_PAGEUP:
1063 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001064#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001065 if (pum_visible())
1066 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 ins_pageup();
1069 break;
1070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001071 case K_DOWN: /* <Down> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001072 if (mod_mask & MOD_MASK_SHIFT)
1073 ins_pagedown();
1074 else
1075 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 break;
1077
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001078 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 case K_PAGEDOWN:
1080 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001081#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001082 if (pum_visible())
1083 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 ins_pagedown();
1086 break;
1087
1088#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001089 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 ins_drop();
1091 break;
1092#endif
1093
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001094 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 c = TAB;
1096 /* FALLTHROUGH */
1097
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001098 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1100 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1101 goto docomplete;
1102#endif
1103 inserted_space = FALSE;
1104 if (ins_tab())
1105 goto normalchar; /* insert TAB as a normal char */
1106 auto_format(FALSE, TRUE);
1107 break;
1108
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001109 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 c = CAR;
1111 /* FALLTHROUGH */
1112 case CAR:
1113 case NL:
1114#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1115 /* In a quickfix window a <CR> jumps to the error under the
1116 * cursor. */
1117 if (bt_quickfix(curbuf) && c == CAR)
1118 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001119 if (curwin->w_llist_ref == NULL) /* quickfix window */
1120 do_cmdline_cmd((char_u *)".cc");
1121 else /* location list window */
1122 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 break;
1124 }
1125#endif
1126#ifdef FEAT_CMDWIN
1127 if (cmdwin_type != 0)
1128 {
1129 /* Execute the command in the cmdline window. */
1130 cmdwin_result = CAR;
1131 goto doESCkey;
1132 }
1133#endif
1134 if (ins_eol(c) && !p_im)
1135 goto doESCkey; /* out of memory */
1136 auto_format(FALSE, FALSE);
1137 inserted_space = FALSE;
1138 break;
1139
1140#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001141 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142# ifdef FEAT_INS_EXPAND
1143 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1144 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001145 if (has_compl_option(TRUE))
1146 goto docomplete;
1147 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 }
1149# endif
1150# ifdef FEAT_DIGRAPHS
1151 c = ins_digraph();
1152 if (c == NUL)
1153 break;
1154# endif
1155 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
1158#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001159 case Ctrl_X: /* Enter CTRL-X mode */
1160 ins_ctrl_x();
1161 break;
1162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 if (ctrl_x_mode != CTRL_X_TAGS)
1165 goto normalchar;
1166 goto docomplete;
1167
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001168 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 if (ctrl_x_mode != CTRL_X_FILES)
1170 goto normalchar;
1171 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001172
1173 case 's': /* Spelling completion after ^X */
1174 case Ctrl_S:
1175 if (ctrl_x_mode != CTRL_X_SPELL)
1176 goto normalchar;
1177 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#endif
1179
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001180 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181#ifdef FEAT_INS_EXPAND
1182 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1183#endif
1184 {
1185 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1186 if (p_im)
1187 {
1188 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1189 break;
1190 goto doESCkey;
1191 }
1192 goto normalchar;
1193 }
1194#ifdef FEAT_INS_EXPAND
1195 /* FALLTHROUGH */
1196
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001197 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 case Ctrl_N:
1199 /* if 'complete' is empty then plain ^P is no longer special,
1200 * but it is under other ^X modes */
1201 if (*curbuf->b_p_cpt == NUL
1202 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001203 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 goto normalchar;
1205
1206docomplete:
1207 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001208 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 break;
1210#endif /* FEAT_INS_EXPAND */
1211
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001212 case Ctrl_Y: /* copy from previous line or scroll down */
1213 case Ctrl_E: /* copy from next line or scroll up */
1214 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 break;
1216
1217 default:
1218#ifdef UNIX
1219 if (c == intr_char) /* special interrupt char */
1220 goto do_intr;
1221#endif
1222
1223 /*
1224 * Insert a nomal character.
1225 */
1226normalchar:
1227#ifdef FEAT_SMARTINDENT
1228 /* Try to perform smart-indenting. */
1229 ins_try_si(c);
1230#endif
1231
1232 if (c == ' ')
1233 {
1234 inserted_space = TRUE;
1235#ifdef FEAT_CINDENT
1236 if (inindent(0))
1237 can_cindent = FALSE;
1238#endif
1239 if (Insstart_blank_vcol == MAXCOL
1240 && curwin->w_cursor.lnum == Insstart.lnum)
1241 Insstart_blank_vcol = get_nolist_virtcol();
1242 }
1243
1244 if (vim_iswordc(c) || !echeck_abbr(
1245#ifdef FEAT_MBYTE
1246 /* Add ABBR_OFF for characters above 0x100, this is
1247 * what check_abbr() expects. */
1248 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1249#endif
1250 c))
1251 {
1252 insert_special(c, FALSE, FALSE);
1253#ifdef FEAT_RIGHTLEFT
1254 revins_legal++;
1255 revins_chars++;
1256#endif
1257 }
1258
1259 auto_format(FALSE, TRUE);
1260
1261#ifdef FEAT_FOLDING
1262 /* When inserting a character the cursor line must never be in a
1263 * closed fold. */
1264 foldOpenCursor();
1265#endif
1266 break;
1267 } /* end of switch (c) */
1268
1269 /* If the cursor was moved we didn't just insert a space */
1270 if (arrow_used)
1271 inserted_space = FALSE;
1272
1273#ifdef FEAT_CINDENT
1274 if (can_cindent && cindent_on()
1275# ifdef FEAT_INS_EXPAND
1276 && ctrl_x_mode == 0
1277# endif
1278 )
1279 {
1280force_cindent:
1281 /*
1282 * Indent now if a key was typed that is in 'cinkeys'.
1283 */
1284 if (in_cinkeys(c, ' ', line_is_white))
1285 {
1286 if (stop_arrow() == OK)
1287 /* re-indent the current line */
1288 do_c_expr_indent();
1289 }
1290 }
1291#endif /* FEAT_CINDENT */
1292
1293 } /* for (;;) */
1294 /* NOTREACHED */
1295}
1296
1297/*
1298 * Redraw for Insert mode.
1299 * This is postponed until getting the next character to make '$' in the 'cpo'
1300 * option work correctly.
1301 * Only redraw when there are no characters available. This speeds up
1302 * inserting sequences of characters (e.g., for CTRL-R).
1303 */
1304 static void
1305ins_redraw()
1306{
1307 if (!char_avail())
1308 {
1309 if (must_redraw)
1310 update_screen(0);
1311 else if (clear_cmdline || redraw_cmdline)
1312 showmode(); /* clear cmdline and show mode */
1313 showruler(FALSE);
1314 setcursor();
1315 emsg_on_display = FALSE; /* may remove error message now */
1316 }
1317}
1318
1319/*
1320 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1321 */
1322 static void
1323ins_ctrl_v()
1324{
1325 int c;
1326
1327 /* may need to redraw when no more chars available now */
1328 ins_redraw();
1329
1330 if (redrawing() && !char_avail())
1331 edit_putchar('^', TRUE);
1332 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1333
1334#ifdef FEAT_CMDL_INFO
1335 add_to_showcmd_c(Ctrl_V);
1336#endif
1337
1338 c = get_literal();
1339#ifdef FEAT_CMDL_INFO
1340 clear_showcmd();
1341#endif
1342 insert_special(c, FALSE, TRUE);
1343#ifdef FEAT_RIGHTLEFT
1344 revins_chars++;
1345 revins_legal++;
1346#endif
1347}
1348
1349/*
1350 * Put a character directly onto the screen. It's not stored in a buffer.
1351 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1352 */
1353static int pc_status;
1354#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1355#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1356#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1357#define PC_STATUS_SET 3 /* pc_bytes was filled */
1358#ifdef FEAT_MBYTE
1359static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1360#else
1361static char_u pc_bytes[2]; /* saved bytes */
1362#endif
1363static int pc_attr;
1364static int pc_row;
1365static int pc_col;
1366
1367 void
1368edit_putchar(c, highlight)
1369 int c;
1370 int highlight;
1371{
1372 int attr;
1373
1374 if (ScreenLines != NULL)
1375 {
1376 update_topline(); /* just in case w_topline isn't valid */
1377 validate_cursor();
1378 if (highlight)
1379 attr = hl_attr(HLF_8);
1380 else
1381 attr = 0;
1382 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1383 pc_col = W_WINCOL(curwin);
1384#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1385 pc_status = PC_STATUS_UNSET;
1386#endif
1387#ifdef FEAT_RIGHTLEFT
1388 if (curwin->w_p_rl)
1389 {
1390 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1391# ifdef FEAT_MBYTE
1392 if (has_mbyte)
1393 {
1394 int fix_col = mb_fix_col(pc_col, pc_row);
1395
1396 if (fix_col != pc_col)
1397 {
1398 screen_putchar(' ', pc_row, fix_col, attr);
1399 --curwin->w_wcol;
1400 pc_status = PC_STATUS_RIGHT;
1401 }
1402 }
1403# endif
1404 }
1405 else
1406#endif
1407 {
1408 pc_col += curwin->w_wcol;
1409#ifdef FEAT_MBYTE
1410 if (mb_lefthalve(pc_row, pc_col))
1411 pc_status = PC_STATUS_LEFT;
1412#endif
1413 }
1414
1415 /* save the character to be able to put it back */
1416#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1417 if (pc_status == PC_STATUS_UNSET)
1418#endif
1419 {
1420 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1421 pc_status = PC_STATUS_SET;
1422 }
1423 screen_putchar(c, pc_row, pc_col, attr);
1424 }
1425}
1426
1427/*
1428 * Undo the previous edit_putchar().
1429 */
1430 void
1431edit_unputchar()
1432{
1433 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1434 {
1435#if defined(FEAT_MBYTE)
1436 if (pc_status == PC_STATUS_RIGHT)
1437 ++curwin->w_wcol;
1438 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1439 redrawWinline(curwin->w_cursor.lnum, FALSE);
1440 else
1441#endif
1442 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1443 }
1444}
1445
1446/*
1447 * Called when p_dollar is set: display a '$' at the end of the changed text
1448 * Only works when cursor is in the line that changes.
1449 */
1450 void
1451display_dollar(col)
1452 colnr_T col;
1453{
1454 colnr_T save_col;
1455
1456 if (!redrawing())
1457 return;
1458
1459 cursor_off();
1460 save_col = curwin->w_cursor.col;
1461 curwin->w_cursor.col = col;
1462#ifdef FEAT_MBYTE
1463 if (has_mbyte)
1464 {
1465 char_u *p;
1466
1467 /* If on the last byte of a multi-byte move to the first byte. */
1468 p = ml_get_curline();
1469 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1470 }
1471#endif
1472 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1473 if (curwin->w_wcol < W_WIDTH(curwin))
1474 {
1475 edit_putchar('$', FALSE);
1476 dollar_vcol = curwin->w_virtcol;
1477 }
1478 curwin->w_cursor.col = save_col;
1479}
1480
1481/*
1482 * Call this function before moving the cursor from the normal insert position
1483 * in insert mode.
1484 */
1485 static void
1486undisplay_dollar()
1487{
1488 if (dollar_vcol)
1489 {
1490 dollar_vcol = 0;
1491 redrawWinline(curwin->w_cursor.lnum, FALSE);
1492 }
1493}
1494
1495/*
1496 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1497 * Keep the cursor on the same character.
1498 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1499 * type == INDENT_DEC decrease indent (for CTRL-D)
1500 * type == INDENT_SET set indent to "amount"
1501 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1502 */
1503 void
1504change_indent(type, amount, round, replaced)
1505 int type;
1506 int amount;
1507 int round;
1508 int replaced; /* replaced character, put on replace stack */
1509{
1510 int vcol;
1511 int last_vcol;
1512 int insstart_less; /* reduction for Insstart.col */
1513 int new_cursor_col;
1514 int i;
1515 char_u *ptr;
1516 int save_p_list;
1517 int start_col;
1518 colnr_T vc;
1519#ifdef FEAT_VREPLACE
1520 colnr_T orig_col = 0; /* init for GCC */
1521 char_u *new_line, *orig_line = NULL; /* init for GCC */
1522
1523 /* VREPLACE mode needs to know what the line was like before changing */
1524 if (State & VREPLACE_FLAG)
1525 {
1526 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1527 orig_col = curwin->w_cursor.col;
1528 }
1529#endif
1530
1531 /* for the following tricks we don't want list mode */
1532 save_p_list = curwin->w_p_list;
1533 curwin->w_p_list = FALSE;
1534 vc = getvcol_nolist(&curwin->w_cursor);
1535 vcol = vc;
1536
1537 /*
1538 * For Replace mode we need to fix the replace stack later, which is only
1539 * possible when the cursor is in the indent. Remember the number of
1540 * characters before the cursor if it's possible.
1541 */
1542 start_col = curwin->w_cursor.col;
1543
1544 /* determine offset from first non-blank */
1545 new_cursor_col = curwin->w_cursor.col;
1546 beginline(BL_WHITE);
1547 new_cursor_col -= curwin->w_cursor.col;
1548
1549 insstart_less = curwin->w_cursor.col;
1550
1551 /*
1552 * If the cursor is in the indent, compute how many screen columns the
1553 * cursor is to the left of the first non-blank.
1554 */
1555 if (new_cursor_col < 0)
1556 vcol = get_indent() - vcol;
1557
1558 if (new_cursor_col > 0) /* can't fix replace stack */
1559 start_col = -1;
1560
1561 /*
1562 * Set the new indent. The cursor will be put on the first non-blank.
1563 */
1564 if (type == INDENT_SET)
1565 (void)set_indent(amount, SIN_CHANGED);
1566 else
1567 {
1568#ifdef FEAT_VREPLACE
1569 int save_State = State;
1570
1571 /* Avoid being called recursively. */
1572 if (State & VREPLACE_FLAG)
1573 State = INSERT;
1574#endif
1575 shift_line(type == INDENT_DEC, round, 1);
1576#ifdef FEAT_VREPLACE
1577 State = save_State;
1578#endif
1579 }
1580 insstart_less -= curwin->w_cursor.col;
1581
1582 /*
1583 * Try to put cursor on same character.
1584 * If the cursor is at or after the first non-blank in the line,
1585 * compute the cursor column relative to the column of the first
1586 * non-blank character.
1587 * If we are not in insert mode, leave the cursor on the first non-blank.
1588 * If the cursor is before the first non-blank, position it relative
1589 * to the first non-blank, counted in screen columns.
1590 */
1591 if (new_cursor_col >= 0)
1592 {
1593 /*
1594 * When changing the indent while the cursor is touching it, reset
1595 * Insstart_col to 0.
1596 */
1597 if (new_cursor_col == 0)
1598 insstart_less = MAXCOL;
1599 new_cursor_col += curwin->w_cursor.col;
1600 }
1601 else if (!(State & INSERT))
1602 new_cursor_col = curwin->w_cursor.col;
1603 else
1604 {
1605 /*
1606 * Compute the screen column where the cursor should be.
1607 */
1608 vcol = get_indent() - vcol;
1609 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1610
1611 /*
1612 * Advance the cursor until we reach the right screen column.
1613 */
1614 vcol = last_vcol = 0;
1615 new_cursor_col = -1;
1616 ptr = ml_get_curline();
1617 while (vcol <= (int)curwin->w_virtcol)
1618 {
1619 last_vcol = vcol;
1620#ifdef FEAT_MBYTE
1621 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001622 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 else
1624#endif
1625 ++new_cursor_col;
1626 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1627 }
1628 vcol = last_vcol;
1629
1630 /*
1631 * May need to insert spaces to be able to position the cursor on
1632 * the right screen column.
1633 */
1634 if (vcol != (int)curwin->w_virtcol)
1635 {
1636 curwin->w_cursor.col = new_cursor_col;
1637 i = (int)curwin->w_virtcol - vcol;
1638 ptr = alloc(i + 1);
1639 if (ptr != NULL)
1640 {
1641 new_cursor_col += i;
1642 ptr[i] = NUL;
1643 while (--i >= 0)
1644 ptr[i] = ' ';
1645 ins_str(ptr);
1646 vim_free(ptr);
1647 }
1648 }
1649
1650 /*
1651 * When changing the indent while the cursor is in it, reset
1652 * Insstart_col to 0.
1653 */
1654 insstart_less = MAXCOL;
1655 }
1656
1657 curwin->w_p_list = save_p_list;
1658
1659 if (new_cursor_col <= 0)
1660 curwin->w_cursor.col = 0;
1661 else
1662 curwin->w_cursor.col = new_cursor_col;
1663 curwin->w_set_curswant = TRUE;
1664 changed_cline_bef_curs();
1665
1666 /*
1667 * May have to adjust the start of the insert.
1668 */
1669 if (State & INSERT)
1670 {
1671 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1672 {
1673 if ((int)Insstart.col <= insstart_less)
1674 Insstart.col = 0;
1675 else
1676 Insstart.col -= insstart_less;
1677 }
1678 if ((int)ai_col <= insstart_less)
1679 ai_col = 0;
1680 else
1681 ai_col -= insstart_less;
1682 }
1683
1684 /*
1685 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1686 * If the number of characters before the cursor decreased, need to pop a
1687 * few characters from the replace stack.
1688 * If the number of characters before the cursor increased, need to push a
1689 * few NULs onto the replace stack.
1690 */
1691 if (REPLACE_NORMAL(State) && start_col >= 0)
1692 {
1693 while (start_col > (int)curwin->w_cursor.col)
1694 {
1695 replace_join(0); /* remove a NUL from the replace stack */
1696 --start_col;
1697 }
1698 while (start_col < (int)curwin->w_cursor.col || replaced)
1699 {
1700 replace_push(NUL);
1701 if (replaced)
1702 {
1703 replace_push(replaced);
1704 replaced = NUL;
1705 }
1706 ++start_col;
1707 }
1708 }
1709
1710#ifdef FEAT_VREPLACE
1711 /*
1712 * For VREPLACE mode, we also have to fix the replace stack. In this case
1713 * it is always possible because we backspace over the whole line and then
1714 * put it back again the way we wanted it.
1715 */
1716 if (State & VREPLACE_FLAG)
1717 {
1718 /* If orig_line didn't allocate, just return. At least we did the job,
1719 * even if you can't backspace. */
1720 if (orig_line == NULL)
1721 return;
1722
1723 /* Save new line */
1724 new_line = vim_strsave(ml_get_curline());
1725 if (new_line == NULL)
1726 return;
1727
1728 /* We only put back the new line up to the cursor */
1729 new_line[curwin->w_cursor.col] = NUL;
1730
1731 /* Put back original line */
1732 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1733 curwin->w_cursor.col = orig_col;
1734
1735 /* Backspace from cursor to start of line */
1736 backspace_until_column(0);
1737
1738 /* Insert new stuff into line again */
1739 ins_bytes(new_line);
1740
1741 vim_free(new_line);
1742 }
1743#endif
1744}
1745
1746/*
1747 * Truncate the space at the end of a line. This is to be used only in an
1748 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1749 * modes.
1750 */
1751 void
1752truncate_spaces(line)
1753 char_u *line;
1754{
1755 int i;
1756
1757 /* find start of trailing white space */
1758 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1759 {
1760 if (State & REPLACE_FLAG)
1761 replace_join(0); /* remove a NUL from the replace stack */
1762 }
1763 line[i + 1] = NUL;
1764}
1765
1766#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1767 || defined(FEAT_COMMENTS) || defined(PROTO)
1768/*
1769 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1770 * modes correctly. May also be used when not in insert mode at all.
1771 */
1772 void
1773backspace_until_column(col)
1774 int col;
1775{
1776 while ((int)curwin->w_cursor.col > col)
1777 {
1778 curwin->w_cursor.col--;
1779 if (State & REPLACE_FLAG)
1780 replace_do_bs();
1781 else
1782 (void)del_char(FALSE);
1783 }
1784}
1785#endif
1786
1787#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1788/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001789 * CTRL-X pressed in Insert mode.
1790 */
1791 static void
1792ins_ctrl_x()
1793{
1794 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1795 * CTRL-V works like CTRL-N */
1796 if (ctrl_x_mode != CTRL_X_CMDLINE)
1797 {
1798 /* if the next ^X<> won't ADD nothing, then reset
1799 * compl_cont_status */
1800 if (compl_cont_status & CONT_N_ADDS)
1801 compl_cont_status = (compl_cont_status | CONT_INTRPT);
1802 else
1803 compl_cont_status = 0;
1804 /* We're not sure which CTRL-X mode it will be yet */
1805 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1806 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1807 edit_submode_pre = NULL;
1808 showmode();
1809 }
1810}
1811
1812/*
1813 * Return TRUE if the 'dict' or 'tsr' option can be used.
1814 */
1815 static int
1816has_compl_option(dict_opt)
1817 int dict_opt;
1818{
1819 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1820 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1821 {
1822 ctrl_x_mode = 0;
1823 edit_submode = NULL;
1824 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1825 : (char_u *)_("'thesaurus' option is empty"),
1826 hl_attr(HLF_E));
1827 if (emsg_silent == 0)
1828 {
1829 vim_beep();
1830 setcursor();
1831 out_flush();
1832 ui_delay(2000L, FALSE);
1833 }
1834 return FALSE;
1835 }
1836 return TRUE;
1837}
1838
1839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1841 * This depends on the current mode.
1842 */
1843 int
1844vim_is_ctrl_x_key(c)
1845 int c;
1846{
1847 /* Always allow ^R - let it's results then be checked */
1848 if (c == Ctrl_R)
1849 return TRUE;
1850
Bram Moolenaare3226be2005-12-18 22:10:00 +00001851 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001852 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001853 return TRUE;
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 switch (ctrl_x_mode)
1856 {
1857 case 0: /* Not in any CTRL-X mode */
1858 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1859 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001860 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1862 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1863 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001864 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1865 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 case CTRL_X_SCROLL:
1867 return (c == Ctrl_Y || c == Ctrl_E);
1868 case CTRL_X_WHOLE_LINE:
1869 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1870 case CTRL_X_FILES:
1871 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1872 case CTRL_X_DICTIONARY:
1873 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1874 case CTRL_X_THESAURUS:
1875 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1876 case CTRL_X_TAGS:
1877 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1878#ifdef FEAT_FIND_ID
1879 case CTRL_X_PATH_PATTERNS:
1880 return (c == Ctrl_P || c == Ctrl_N);
1881 case CTRL_X_PATH_DEFINES:
1882 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1883#endif
1884 case CTRL_X_CMDLINE:
1885 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1886 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001887#ifdef FEAT_COMPL_FUNC
1888 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001889 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001890 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001891 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001892#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001893 case CTRL_X_SPELL:
1894 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 }
1896 EMSG(_(e_internal));
1897 return FALSE;
1898}
1899
1900/*
1901 * This is like ins_compl_add(), but if ic and inf are set, then the
1902 * case of the originally typed text is used, and the case of the completed
1903 * text is infered, ie this tries to work out what case you probably wanted
1904 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001905 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 */
1907 int
Bram Moolenaar572cb562005-08-05 21:35:02 +00001908ins_compl_add_infercase(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 char_u *str;
1910 int len;
1911 char_u *fname;
1912 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001913 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914{
1915 int has_lower = FALSE;
1916 int was_letter = FALSE;
1917 int idx;
1918
1919 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
1920 {
1921 /* Infer case of completed part -- webb */
1922 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001923 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924
1925 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001926 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001928 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 {
1930 has_lower = TRUE;
1931 if (isupper(IObuff[idx]))
1932 {
1933 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001934 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
1936 break;
1937 }
1938 }
1939 }
1940
1941 /*
1942 * Rule 2: No lower case, 2nd consecutive letter converted to
1943 * upper case.
1944 */
1945 if (!has_lower)
1946 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001947 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001949 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 && islower(IObuff[idx]))
1951 {
1952 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001953 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
1955 break;
1956 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001957 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 }
1959 }
1960
1961 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001962 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963
Bram Moolenaar572cb562005-08-05 21:35:02 +00001964 return ins_compl_add(IObuff, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00001966 return ins_compl_add(str, len, fname, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967}
1968
1969/*
1970 * Add a match to the list of matches.
1971 * If the given string is already in the list of completions, then return
1972 * FAIL, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaar572cb562005-08-05 21:35:02 +00001973 * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb.
1974 *
1975 * New:
1976 * If the given string is already in the list of completions, then return
1977 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
1978 * maybe because alloc() returns NULL, then FAIL is returned -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00001980 int
1981ins_compl_add(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 char_u *str;
1983 int len;
1984 char_u *fname;
1985 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001986 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
Bram Moolenaar572cb562005-08-05 21:35:02 +00001988 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
1990 ui_breakcheck();
1991 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00001992 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 if (len < 0)
1994 len = (int)STRLEN(str);
1995
1996 /*
1997 * If the same match is already present, don't add it.
1998 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001999 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002001 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 do
2003 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002004 if ( !(match->cp_flags & ORIGINAL_TEXT)
2005 && STRNCMP(match->cp_str, str, (size_t)len) == 0
2006 && match->cp_str[len] == NUL)
2007 return NOTDONE;
2008 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002009 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 }
2011
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002012 /* Remove any popup menu before changing the list of matches. */
2013 ins_compl_del_pum();
2014
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 /*
2016 * Allocate a new match structure.
2017 * Copy the values to the new match structure.
2018 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002019 match = (compl_T *)alloc((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002021 return FAIL;
2022 match->cp_number = -1;
2023 if (flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002025 match->cp_number = 0;
2026 match->cp_str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002028 else if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
2030 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002031 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 }
2033 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002034 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2036 * - NULL otherwise. --Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002037 if (fname && compl_curr_match && compl_curr_match->cp_fname
2038 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
2039 match->cp_fname = compl_curr_match->cp_fname;
2040 else if (fname && (match->cp_fname = vim_strsave(fname)) != NULL)
2041 flags |= FREE_FNAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002043 match->cp_fname = NULL;
2044 match->cp_flags = flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045
2046 /*
2047 * Link the new match structure in the list of matches.
2048 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002049 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002050 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 else if (dir == FORWARD)
2052 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002053 match->cp_next = compl_curr_match->cp_next;
2054 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056 else /* BACKWARD */
2057 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002058 match->cp_next = compl_curr_match;
2059 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002061 if (match->cp_next)
2062 match->cp_next->cp_prev = match;
2063 if (match->cp_prev)
2064 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002066 compl_first_match = match;
2067 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068
2069 return OK;
2070}
2071
2072/*
2073 * Add an array of matches to the list of matches.
2074 * Frees matches[].
2075 */
2076 static void
2077ins_compl_add_matches(num_matches, matches, dir)
2078 int num_matches;
2079 char_u **matches;
2080 int dir;
2081{
2082 int i;
2083 int add_r = OK;
2084 int ldir = dir;
2085
Bram Moolenaar572cb562005-08-05 21:35:02 +00002086 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 if ((add_r = ins_compl_add(matches[i], -1, NULL, ldir, 0)) == OK)
2088 /* if dir was BACKWARD then honor it just once */
2089 ldir = FORWARD;
2090 FreeWild(num_matches, matches);
2091}
2092
2093/* Make the completion list cyclic.
2094 * Return the number of matches (excluding the original).
2095 */
2096 static int
2097ins_compl_make_cyclic()
2098{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002099 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 int count = 0;
2101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002102 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
2104 /*
2105 * Find the end of the list.
2106 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002107 match = compl_first_match;
2108 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002109 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002111 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 ++count;
2113 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002114 match->cp_next = compl_first_match;
2115 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 }
2117 return count;
2118}
2119
Bram Moolenaar9372a112005-12-06 19:59:18 +00002120/* "compl_match_array" points the currently displayed list of entries in the
2121 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002122static char_u **compl_match_array = NULL;
2123static int compl_match_arraysize;
2124
2125/*
2126 * Update the screen and when there is any scrolling remove the popup menu.
2127 */
2128 static void
2129ins_compl_upd_pum()
2130{
2131 int h;
2132
2133 if (compl_match_array != NULL)
2134 {
2135 h = curwin->w_cline_height;
2136 update_screen(0);
2137 if (h != curwin->w_cline_height)
2138 ins_compl_del_pum();
2139 }
2140}
2141
2142/*
2143 * Remove any popup menu.
2144 */
2145 static void
2146ins_compl_del_pum()
2147{
2148 if (compl_match_array != NULL)
2149 {
2150 pum_undisplay();
2151 vim_free(compl_match_array);
2152 compl_match_array = NULL;
2153 }
2154}
2155
2156/*
2157 * Return TRUE if the popup menu should be displayed.
2158 */
2159 static int
2160pum_wanted()
2161{
2162 compl_T *compl;
2163 int i;
2164
2165 /* 'completeopt' must contain "menu" */
2166 if (*p_cot == NUL)
2167 return FALSE;
2168
2169 /* The display looks bad on a B&W display. */
2170 if (t_colors < 8
2171#ifdef FEAT_GUI
2172 && !gui.in_use
2173#endif
2174 )
2175 return FALSE;
2176
2177 /* Don't display the popup menu if there are no matches or there is only
2178 * one (ignoring the original text). */
2179 compl = compl_first_match;
2180 i = 0;
2181 do
2182 {
2183 if (compl == NULL
2184 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2185 break;
2186 compl = compl->cp_next;
2187 } while (compl != compl_first_match);
2188
2189 return (i >= 2);
2190}
2191
2192/*
2193 * Show the popup menu for the list of matches.
2194 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002195 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002196ins_compl_show_pum()
2197{
2198 compl_T *compl;
2199 int i;
2200 int cur = -1;
2201 colnr_T col;
2202
2203 if (!pum_wanted())
2204 return;
2205
2206 /* Update the screen before drawing the popup menu over it. */
2207 update_screen(0);
2208
2209 if (compl_match_array == NULL)
2210 {
2211 /* Need to build the popup menu list. */
2212 compl_match_arraysize = 0;
2213 compl = compl_first_match;
2214 do
2215 {
2216 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2217 ++compl_match_arraysize;
2218 compl = compl->cp_next;
2219 } while (compl != NULL && compl != compl_first_match);
2220 compl_match_array = (char_u **)alloc((unsigned)(sizeof(char_u **)
2221 * compl_match_arraysize));
2222 if (compl_match_array != NULL)
2223 {
2224 i = 0;
2225 compl = compl_first_match;
2226 do
2227 {
2228 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2229 {
2230 if (compl == compl_shown_match)
2231 cur = i;
2232 compl_match_array[i++] = compl->cp_str;
2233 }
2234 compl = compl->cp_next;
2235 } while (compl != NULL && compl != compl_first_match);
2236 }
2237 }
2238 else
2239 {
2240 /* popup menu already exists, only need to find the current item.*/
2241 i = 0;
2242 compl = compl_first_match;
2243 do
2244 {
2245 if ((compl->cp_flags & ORIGINAL_TEXT) == 0)
2246 {
2247 if (compl == compl_shown_match)
2248 {
2249 cur = i;
2250 break;
2251 }
2252 ++i;
2253 }
2254 compl = compl->cp_next;
2255 } while (compl != NULL && compl != compl_first_match);
2256 }
2257
2258 if (compl_match_array != NULL)
2259 {
2260 /* Compute the screen column of the start of the completed text.
2261 * Use the cursor to get all wrapping and other settings right. */
2262 col = curwin->w_cursor.col;
2263 curwin->w_cursor.col = compl_col;
2264 validate_cursor_col();
2265 pum_display(compl_match_array, compl_match_arraysize, cur,
2266 curwin->w_cline_row + W_WINROW(curwin),
2267 curwin->w_cline_height,
Bram Moolenaar280f1262006-01-30 00:14:18 +00002268 curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002269 curwin->w_cursor.col = col;
2270 }
2271}
2272
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273#define DICT_FIRST (1) /* use just first element in "dict" */
2274#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276/*
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 Moolenaar280f1262006-01-30 00:14:18 +00002845 else if (did_emsg)
2846 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002847 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00002848
2849 list_unref(matchlist);
2850 *matches = (char_u **)ga.ga_data;
2851 return ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002852}
2853#endif /* FEAT_COMPL_FUNC */
2854
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002855/*
2856 * Get the next expansion(s), using "compl_pattern".
2857 * The search starts at position "ini" in curbuf and in the direction dir.
2858 * When "compl_started" is FALSE start at that position, otherwise
2859 * continue where we stopped searching before.
2860 * This may return before finding all the matches.
2861 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 */
2863 static int
2864ins_compl_get_exp(ini, dir)
2865 pos_T *ini;
2866 int dir;
2867{
2868 static pos_T first_match_pos;
2869 static pos_T last_match_pos;
2870 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002871 static int found_all = FALSE; /* Found all matches of a
2872 certain type. */
2873 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874
Bram Moolenaar572cb562005-08-05 21:35:02 +00002875 pos_T *pos;
2876 char_u **matches;
2877 int save_p_scs;
2878 int save_p_ws;
2879 int save_p_ic;
2880 int i;
2881 int num_matches;
2882 int len;
2883 int found_new_match;
2884 int type = ctrl_x_mode;
2885 char_u *ptr;
2886 char_u *dict = NULL;
2887 int dict_f = 0;
2888 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002890 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 {
2892 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
2893 ins_buf->b_scanned = 0;
2894 found_all = FALSE;
2895 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002896 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002897 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 last_match_pos = first_match_pos = *ini;
2899 }
2900
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002901 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 pos = (dir == FORWARD) ? &last_match_pos : &first_match_pos;
2903 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
2904 for (;;)
2905 {
2906 found_new_match = FAIL;
2907
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002908 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 * or if found_all says this entry is done. For ^X^L only use the
2910 * entries from 'complete' that look in loaded buffers. */
2911 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002912 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 {
2914 found_all = FALSE;
2915 while (*e_cpt == ',' || *e_cpt == ' ')
2916 e_cpt++;
2917 if (*e_cpt == '.' && !curbuf->b_scanned)
2918 {
2919 ins_buf = curbuf;
2920 first_match_pos = *ini;
2921 /* So that ^N can match word immediately after cursor */
2922 if (ctrl_x_mode == 0)
2923 dec(&first_match_pos);
2924 last_match_pos = first_match_pos;
2925 type = 0;
2926 }
2927 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2928 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2929 {
2930 /* Scan a buffer, but not the current one. */
2931 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
2932 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002933 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 first_match_pos.col = last_match_pos.col = 0;
2935 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2936 last_match_pos.lnum = 0;
2937 type = 0;
2938 }
2939 else /* unloaded buffer, scan like dictionary */
2940 {
2941 found_all = TRUE;
2942 if (ins_buf->b_fname == NULL)
2943 continue;
2944 type = CTRL_X_DICTIONARY;
2945 dict = ins_buf->b_fname;
2946 dict_f = DICT_EXACT;
2947 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00002948 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 ins_buf->b_fname == NULL
2950 ? buf_spname(ins_buf)
2951 : ins_buf->b_sfname == NULL
2952 ? (char *)ins_buf->b_fname
2953 : (char *)ins_buf->b_sfname);
2954 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2955 }
2956 else if (*e_cpt == NUL)
2957 break;
2958 else
2959 {
2960 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2961 type = -1;
2962 else if (*e_cpt == 'k' || *e_cpt == 's')
2963 {
2964 if (*e_cpt == 'k')
2965 type = CTRL_X_DICTIONARY;
2966 else
2967 type = CTRL_X_THESAURUS;
2968 if (*++e_cpt != ',' && *e_cpt != NUL)
2969 {
2970 dict = e_cpt;
2971 dict_f = DICT_FIRST;
2972 }
2973 }
2974#ifdef FEAT_FIND_ID
2975 else if (*e_cpt == 'i')
2976 type = CTRL_X_PATH_PATTERNS;
2977 else if (*e_cpt == 'd')
2978 type = CTRL_X_PATH_DEFINES;
2979#endif
2980 else if (*e_cpt == ']' || *e_cpt == 't')
2981 {
2982 type = CTRL_X_TAGS;
2983 sprintf((char*)IObuff, _("Scanning tags."));
2984 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2985 }
2986 else
2987 type = -1;
2988
2989 /* in any case e_cpt is advanced to the next entry */
2990 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2991
2992 found_all = TRUE;
2993 if (type == -1)
2994 continue;
2995 }
2996 }
2997
2998 switch (type)
2999 {
3000 case -1:
3001 break;
3002#ifdef FEAT_FIND_ID
3003 case CTRL_X_PATH_PATTERNS:
3004 case CTRL_X_PATH_DEFINES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003005 find_pattern_in_path(compl_pattern, dir,
3006 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003008 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3010 (linenr_T)1, (linenr_T)MAXLNUM);
3011 break;
3012#endif
3013
3014 case CTRL_X_DICTIONARY:
3015 case CTRL_X_THESAURUS:
3016 ins_compl_dictionaries(
3017 dict ? dict
3018 : (type == CTRL_X_THESAURUS
3019 ? (*curbuf->b_p_tsr == NUL
3020 ? p_tsr
3021 : curbuf->b_p_tsr)
3022 : (*curbuf->b_p_dict == NUL
3023 ? p_dict
3024 : curbuf->b_p_dict)),
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003025 compl_pattern, dir,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
3027 dict = NULL;
3028 break;
3029
3030 case CTRL_X_TAGS:
3031 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3032 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003033 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034
3035 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003036 * of matches is found when compl_pattern is empty */
3037 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3039 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3040 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3041 {
3042 ins_compl_add_matches(num_matches, matches, dir);
3043 }
3044 p_ic = save_p_ic;
3045 break;
3046
3047 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003048 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3050 {
3051
3052 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003053 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 ins_compl_add_matches(num_matches, matches, dir);
3055 }
3056 break;
3057
3058 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003059 if (expand_cmdline(&compl_xp, compl_pattern,
3060 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 &num_matches, &matches) == EXPAND_OK)
3062 ins_compl_add_matches(num_matches, matches, dir);
3063 break;
3064
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003065#ifdef FEAT_COMPL_FUNC
3066 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003067 case CTRL_X_OMNI:
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003068 num_matches = expand_by_function(type, compl_pattern, &matches);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003069 if (num_matches > 0)
3070 ins_compl_add_matches(num_matches, matches, dir);
3071 break;
3072#endif
3073
Bram Moolenaar488c6512005-08-11 20:09:58 +00003074 case CTRL_X_SPELL:
3075#ifdef FEAT_SYN_HL
3076 num_matches = expand_spelling(first_match_pos.lnum,
3077 first_match_pos.col, compl_pattern, &matches);
3078 if (num_matches > 0)
3079 ins_compl_add_matches(num_matches, matches, dir);
3080#endif
3081 break;
3082
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 default: /* normal ^P/^N and ^X^L */
3084 /*
3085 * If 'infercase' is set, don't use 'smartcase' here
3086 */
3087 save_p_scs = p_scs;
3088 if (ins_buf->b_p_inf)
3089 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003090
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 /* buffers other than curbuf are scanned from the beginning or the
3092 * end but never from the middle, thus setting nowrapscan in this
3093 * buffers is a good idea, on the other hand, we always set
3094 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3095 save_p_ws = p_ws;
3096 if (ins_buf != curbuf)
3097 p_ws = FALSE;
3098 else if (*e_cpt == '.')
3099 p_ws = TRUE;
3100 for (;;)
3101 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003102 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003104 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3105 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003107 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003109 dir, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 else
3111 found_new_match = searchit(NULL, ins_buf, pos, dir,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003112 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 RE_LAST);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003114 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003116 /* set compl_started even on fail */
3117 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 first_match_pos = *pos;
3119 last_match_pos = *pos;
3120 }
3121 else if (first_match_pos.lnum == last_match_pos.lnum
3122 && first_match_pos.col == last_match_pos.col)
3123 found_new_match = FAIL;
3124 if (found_new_match == FAIL)
3125 {
3126 if (ins_buf == curbuf)
3127 found_all = TRUE;
3128 break;
3129 }
3130
3131 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003132 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 && ini->lnum == pos->lnum
3134 && ini->col == pos->col)
3135 continue;
3136 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3137 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3138 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003139 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 {
3141 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3142 continue;
3143 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3144 if (!p_paste)
3145 ptr = skipwhite(ptr);
3146 }
3147 len = (int)STRLEN(ptr);
3148 }
3149 else
3150 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003151 char_u *tmp_ptr = ptr;
3152
3153 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003155 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 /* Skip if already inside a word. */
3157 if (vim_iswordp(tmp_ptr))
3158 continue;
3159 /* Find start of next word. */
3160 tmp_ptr = find_word_start(tmp_ptr);
3161 }
3162 /* Find end of this word. */
3163 tmp_ptr = find_word_end(tmp_ptr);
3164 len = (int)(tmp_ptr - ptr);
3165
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003166 if ((compl_cont_status & CONT_ADDING)
3167 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 {
3169 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3170 {
3171 /* Try next line, if any. the new word will be
3172 * "join" as if the normal command "J" was used.
3173 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003174 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 * works -- Acevedo */
3176 STRNCPY(IObuff, ptr, len);
3177 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3178 tmp_ptr = ptr = skipwhite(ptr);
3179 /* Find start of next word. */
3180 tmp_ptr = find_word_start(tmp_ptr);
3181 /* Find end of next word. */
3182 tmp_ptr = find_word_end(tmp_ptr);
3183 if (tmp_ptr > ptr)
3184 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003185 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003187 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 IObuff[len++] = ' ';
3189 /* IObuf =~ "\k.* ", thus len >= 2 */
3190 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003191 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 || (vim_strchr(p_cpo, CPO_JOINSP)
3193 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003194 && (IObuff[len - 2] == '?'
3195 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 IObuff[len++] = ' ';
3197 }
3198 /* copy as much as posible of the new word */
3199 if (tmp_ptr - ptr >= IOSIZE - len)
3200 tmp_ptr = ptr + IOSIZE - len - 1;
3201 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3202 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003203 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 }
3205 IObuff[len] = NUL;
3206 ptr = IObuff;
3207 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003208 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 continue;
3210 }
3211 }
3212 if (ins_compl_add_infercase(ptr, len,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003213 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar572cb562005-08-05 21:35:02 +00003214 dir, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
3216 found_new_match = OK;
3217 break;
3218 }
3219 }
3220 p_scs = save_p_scs;
3221 p_ws = save_p_ws;
3222 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003223
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003224 /* check if compl_curr_match has changed, (e.g. other type of
3225 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003226 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 found_new_match = OK;
3228
3229 /* break the loop for specialized modes (use 'complete' just for the
3230 * generic ctrl_x_mode == 0) or when we've found a new match */
3231 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003232 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003233 {
3234 if (got_int)
3235 break;
3236 if (pum_wanted() && type != -1)
3237 /* Fill the popup menu as soon as possible. */
3238 ins_compl_check_keys(0);
3239 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3240 || compl_interrupted)
3241 break;
3242 compl_started = TRUE;
3243 }
3244 else
3245 {
3246 /* Mark a buffer scanned when it has been scanned completely */
3247 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3248 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003250 compl_started = FALSE;
3251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003253 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
3255 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3256 && *e_cpt == NUL) /* Got to end of 'complete' */
3257 found_new_match = FAIL;
3258
3259 i = -1; /* total of matches, unknown */
3260 if (found_new_match == FAIL
3261 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3262 i = ins_compl_make_cyclic();
3263
3264 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003265 * just been made cyclic then we have to move compl_curr_match to the next
3266 * or previous entry (if any) -- Acevedo */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003267 compl_curr_match = dir == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003268 if (compl_curr_match == NULL)
3269 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 return i;
3271}
3272
3273/* Delete the old text being completed. */
3274 static void
3275ins_compl_delete()
3276{
3277 int i;
3278
3279 /*
3280 * In insert mode: Delete the typed part.
3281 * In replace mode: Put the old characters back, if any.
3282 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003283 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 backspace_until_column(i);
3285 changed_cline_bef_curs();
3286}
3287
3288/* Insert the new text being completed. */
3289 static void
3290ins_compl_insert()
3291{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003292 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293}
3294
3295/*
3296 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003297 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3298 * get more completions. If it is FALSE, then we just do nothing when there
3299 * are no more completions in a given direction. The latter case is used when
3300 * we are still in the middle of finding completions, to allow browsing
3301 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 * Return the total number of matches, or -1 if still unknown -- webb.
3303 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003304 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3305 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 *
3307 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003308 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3309 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 */
3311 static int
Bram Moolenaare3226be2005-12-18 22:10:00 +00003312ins_compl_next(allow_get_expansion, count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003314 int count; /* repeat completion this many times; should
3315 be at least 1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 int num_matches = -1;
3318 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003319 int todo = count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
3321 if (allow_get_expansion)
3322 {
3323 /* Delete old text to be replaced */
3324 ins_compl_delete();
3325 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003326 compl_pending = FALSE;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003327
3328 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3329 * around. */
3330 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003332 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003334 compl_shown_match = compl_shown_match->cp_next;
3335 if (compl_shown_match->cp_next != NULL
3336 && compl_shown_match->cp_next == compl_first_match)
3337 break;
3338 }
3339 else if (compl_shows_dir == BACKWARD
3340 && compl_shown_match->cp_prev != NULL)
3341 {
3342 compl_shown_match = compl_shown_match->cp_prev;
3343 if (compl_shown_match == compl_first_match)
3344 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 }
3346 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003347 {
3348 compl_pending = TRUE;
3349 if (allow_get_expansion)
3350 {
3351 num_matches = ins_compl_get_exp(&compl_startpos,
3352 compl_direction);
3353 if (compl_pending)
3354 {
3355 if (compl_direction == compl_shows_dir)
3356 compl_shown_match = compl_curr_match;
3357 }
3358 }
3359 else
3360 return -1;
3361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
3363
3364 /* Insert the text of the new completion */
3365 ins_compl_insert();
3366
3367 if (!allow_get_expansion)
3368 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003369 /* may undisplay the popup menu first */
3370 ins_compl_upd_pum();
3371
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003372 /* display the updated popup menu */
3373 ins_compl_show_pum();
3374
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 /* Delete old text to be replaced, since we're still searching and
3376 * don't want to match ourselves! */
3377 ins_compl_delete();
3378 }
3379
3380 /*
3381 * Show the file name for the match (if any)
3382 * Truncate the file name to avoid a wait for return.
3383 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003384 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
3386 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003387 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 if (i <= 0)
3389 i = 0;
3390 else
3391 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003392 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 msg(IObuff);
3394 redraw_cmdline = FALSE; /* don't overwrite! */
3395 }
3396
3397 return num_matches;
3398}
3399
3400/*
3401 * Call this while finding completions, to check whether the user has hit a key
3402 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003403 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003405 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 */
3407 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003408ins_compl_check_keys(frequency)
3409 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410{
3411 static int count = 0;
3412
3413 int c;
3414
3415 /* Don't check when reading keys from a script. That would break the test
3416 * scripts */
3417 if (using_script())
3418 return;
3419
3420 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003421 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 return;
3423 count = 0;
3424
3425 ++no_mapping;
3426 c = vpeekc_any();
3427 --no_mapping;
3428 if (c != NUL)
3429 {
3430 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3431 {
3432 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003433 compl_shows_dir = ins_compl_key2dir(c);
3434 (void)ins_compl_next(FALSE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
3436 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003437 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003439 if (compl_pending && !got_int)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003440 (void)ins_compl_next(FALSE, 1);
3441}
3442
3443/*
3444 * Decide the direction of Insert mode complete from the key typed.
3445 * Returns BACKWARD or FORWARD.
3446 */
3447 static int
3448ins_compl_key2dir(c)
3449 int c;
3450{
3451 if (c == Ctrl_P || c == Ctrl_L || (pum_visible()
3452 && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP)))
3453 return BACKWARD;
3454 return FORWARD;
3455}
3456
3457/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003458 * Return TRUE for keys that are used for completion only when the popup menu
3459 * is visible.
3460 */
3461 static int
3462ins_compl_pum_key(c)
3463 int c;
3464{
3465 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
3466 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN);
3467}
3468
3469/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00003470 * Decide the number of completions to move forward.
3471 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3472 */
3473 static int
3474ins_compl_key2count(c)
3475 int c;
3476{
3477 int h;
3478
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003479 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00003480 {
3481 h = pum_get_height();
3482 if (h > 3)
3483 h -= 2; /* keep some context */
3484 return h;
3485 }
3486 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487}
3488
3489/*
3490 * Do Insert mode completion.
3491 * Called when character "c" was typed, which has a meaning for completion.
3492 * Returns OK if completion was done, FAIL if something failed (out of mem).
3493 */
3494 static int
3495ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003496 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003498 char_u *line;
3499 int startcol = 0; /* column where searched text starts */
3500 colnr_T curs_col; /* cursor column */
3501 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
Bram Moolenaare3226be2005-12-18 22:10:00 +00003503 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003504 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 /* First time we hit ^N or ^P (in a row, I mean) */
3507
3508 /* Turn off 'sm' so we don't show matches with ^X^L */
3509 save_sm = p_sm;
3510 p_sm = FALSE;
3511
3512 did_ai = FALSE;
3513#ifdef FEAT_SMARTINDENT
3514 did_si = FALSE;
3515 can_si = FALSE;
3516 can_si_back = FALSE;
3517#endif
3518 if (stop_arrow() == FAIL)
3519 return FAIL;
3520
3521 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003522 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
3524 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003525 * "compl_startpos" to the cursor as a pattern to add a new word
3526 * instead of expand the one before the cursor, in word-wise if
3527 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 * is not in the same line as the cursor then fix it (the line has
3529 * been split because it was longer than 'tw'). if SOL is set then
3530 * skip the previous pattern, a word at the beginning of the line has
3531 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003532 if ((compl_cont_status & CONT_INTRPT) && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 {
3534 /*
3535 * it is a continued search
3536 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003537 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3539 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3540 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003541 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003543 /* line (probably) wrapped, set compl_startpos to the
3544 * first non_blank in the line, if it is not a wordchar
3545 * include it to get a better pattern, but then we don't
3546 * want the "\\<" prefix, check it bellow */
3547 compl_col = (colnr_T)(skipwhite(line) - line);
3548 compl_startpos.col = compl_col;
3549 compl_startpos.lnum = curwin->w_cursor.lnum;
3550 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552 else
3553 {
3554 /* S_IPOS was set when we inserted a word that was at the
3555 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003556 * mode but first we need to redefine compl_startpos */
3557 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003559 compl_cont_status |= CONT_SOL;
3560 compl_startpos.col = (colnr_T)(skipwhite(
3561 line + compl_length
3562 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003564 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003566 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003567 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 * have enough space? just being paranoic */
3569#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003570 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003572 compl_cont_status &= ~CONT_SOL;
3573 compl_length = (IOSIZE - MIN_SPACE);
3574 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003576 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3577 if (compl_length < 1)
3578 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
3580 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003581 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003583 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
3585 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003586 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003588 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003590 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003592 compl_cont_status = 0;
3593 compl_cont_status |= CONT_N_ADDS;
3594 compl_startpos = curwin->w_cursor;
3595 startcol = (int)curs_col;
3596 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598
3599 /* Work out completion pattern and original text -- webb */
3600 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3601 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003602 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3604 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003605 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003607 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003609 compl_col += ++startcol;
3610 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003613 compl_pattern = str_foldcase(line + compl_col,
3614 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003616 compl_pattern = vim_strnsave(line + compl_col,
3617 compl_length);
3618 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 return FAIL;
3620 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003621 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
3623 char_u *prefix = (char_u *)"\\<";
3624
3625 /* we need 3 extra chars, 1 for the NUL and
3626 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003627 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3628 compl_length) + 3);
3629 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003631 if (!vim_iswordp(line + compl_col)
3632 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 && (
3634#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003635 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003637 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638#endif
3639 )))
3640 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003641 STRCPY((char *)compl_pattern, prefix);
3642 (void)quote_meta(compl_pattern + STRLEN(prefix),
3643 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003645 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003647 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003649 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650#endif
3651 )
3652 {
3653 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003654 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3655 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003657 compl_col += curs_col;
3658 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
3660 else
3661 {
3662#ifdef FEAT_MBYTE
3663 /* Search the point of change class of multibyte character
3664 * or not a word single byte character backward. */
3665 if (has_mbyte)
3666 {
3667 int base_class;
3668 int head_off;
3669
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003670 startcol -= (*mb_head_off)(line, line + startcol);
3671 base_class = mb_get_class(line + startcol);
3672 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003674 head_off = (*mb_head_off)(line, line + startcol);
3675 if (base_class != mb_get_class(line + startcol
3676 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003678 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680 }
3681 else
3682#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003683 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003685 compl_col += ++startcol;
3686 compl_length = (int)curs_col - startcol;
3687 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
3689 /* Only match word with at least two chars -- webb
3690 * there's no need to call quote_meta,
3691 * alloc(7) is enough -- Acevedo
3692 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003693 compl_pattern = alloc(7);
3694 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003696 STRCPY((char *)compl_pattern, "\\<");
3697 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3698 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
3700 else
3701 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003702 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3703 compl_length) + 3);
3704 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003706 STRCPY((char *)compl_pattern, "\\<");
3707 (void)quote_meta(compl_pattern + 2, line + compl_col,
3708 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 }
3710 }
3711 }
3712 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3713 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003714 compl_col = skipwhite(line) - line;
3715 compl_length = (int)curs_col - (int)compl_col;
3716 if (compl_length < 0) /* cursor in indent: empty pattern */
3717 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003719 compl_pattern = str_foldcase(line + compl_col, compl_length,
3720 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003722 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3723 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 return FAIL;
3725 }
3726 else if (ctrl_x_mode == CTRL_X_FILES)
3727 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003728 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003730 compl_col += ++startcol;
3731 compl_length = (int)curs_col - startcol;
3732 compl_pattern = addstar(line + compl_col, compl_length,
3733 EXPAND_FILES);
3734 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 return FAIL;
3736 }
3737 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3738 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003739 compl_pattern = vim_strnsave(line, curs_col);
3740 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003742 set_cmd_context(&compl_xp, compl_pattern,
3743 (int)STRLEN(compl_pattern), curs_col);
3744 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3745 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003747 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
3748 compl_col = startcol;
3749 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003751 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003752 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00003753#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003754 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00003755 * Call user defined function 'completefunc' with "a:findstart"
3756 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003757 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00003758 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003759 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003760 char_u *funcname;
3761 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003762
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003763 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00003764 * string */
3765 funcname = ctrl_x_mode == CTRL_X_FUNCTION
3766 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3767 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003768 {
3769 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
3770 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003771 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003772 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003773
3774 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003775 args[1] = NULL;
3776 pos = curwin->w_cursor;
3777 col = call_func_retnr(funcname, 2, args, FALSE);
3778 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003779
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003780 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003781 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003782 compl_col = col;
3783 if ((colnr_T)compl_col > curs_col)
3784 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003785
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003786 /* Setup variables for completion. Need to obtain "line" again,
3787 * it may have become invalid. */
3788 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003789 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003790 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3791 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003792#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003793 return FAIL;
3794 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00003795 else if (ctrl_x_mode == CTRL_X_SPELL)
3796 {
3797#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00003798 if (spell_bad_len > 0)
3799 compl_col = curs_col - spell_bad_len;
3800 else
3801 compl_col = spell_word_start(startcol);
3802 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00003803 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00003804 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003805 compl_length = (int)curs_col - compl_col;
3806 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3807 if (compl_pattern == NULL)
3808#endif
3809 return FAIL;
3810 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003811 else
3812 {
3813 EMSG2(_(e_intern2), "ins_complete()");
3814 return FAIL;
3815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003817 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 {
3819 edit_submode_pre = (char_u *)_(" Adding");
3820 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3821 {
3822 /* Insert a new line, keep indentation but ignore 'comments' */
3823#ifdef FEAT_COMMENTS
3824 char_u *old = curbuf->b_p_com;
3825
3826 curbuf->b_p_com = (char_u *)"";
3827#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003828 compl_startpos.lnum = curwin->w_cursor.lnum;
3829 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 ins_eol('\r');
3831#ifdef FEAT_COMMENTS
3832 curbuf->b_p_com = old;
3833#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003834 compl_length = 0;
3835 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837 }
3838 else
3839 {
3840 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003841 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003844 if (compl_cont_status & CONT_LOCAL)
3845 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 else
3847 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
3848
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003850 * "compl_orig_text" itself (not a copy) is added, it will be freed
3851 * when the list of matches is freed. */
3852 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
3853 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
3854 -1, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003856 vim_free(compl_pattern);
3857 compl_pattern = NULL;
3858 vim_free(compl_orig_text);
3859 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 return FAIL;
3861 }
3862
3863 /* showmode might reset the internal line pointers, so it must
3864 * be called before line = ml_get(), or when this address is no
3865 * longer needed. -- Acevedo.
3866 */
3867 edit_submode_extra = (char_u *)_("-- Searching...");
3868 edit_submode_highl = HLF_COUNT;
3869 showmode();
3870 edit_submode_extra = NULL;
3871 out_flush();
3872 }
3873
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003874 compl_shown_match = compl_curr_match;
3875 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
3877 /*
3878 * Find next match.
3879 */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003880 n = ins_compl_next(TRUE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003882 /* may undisplay the popup menu */
3883 ins_compl_upd_pum();
3884
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003885 if (n > 1) /* all matches have been found */
3886 compl_matches = n;
3887 compl_curr_match = compl_shown_match;
3888 compl_direction = compl_shows_dir;
3889 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
3891 /* eat the ESC to avoid leaving insert mode */
3892 if (got_int && !global_busy)
3893 {
3894 (void)vgetc();
3895 got_int = FALSE;
3896 }
3897
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003898 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003899 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003901 edit_submode_extra = (compl_cont_status & CONT_ADDING)
3902 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
3904 edit_submode_highl = HLF_E;
3905 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
3906 * because we couldn't expand anything at first place, but if we used
3907 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
3908 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003909 if ( compl_length > 1
3910 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 || (ctrl_x_mode != 0
3912 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
3913 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003914 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916
Bram Moolenaar572cb562005-08-05 21:35:02 +00003917 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003918 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003920 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
3922 if (edit_submode_extra == NULL)
3923 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003924 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 {
3926 edit_submode_extra = (char_u *)_("Back at original");
3927 edit_submode_highl = HLF_W;
3928 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003929 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 {
3931 edit_submode_extra = (char_u *)_("Word from other line");
3932 edit_submode_highl = HLF_COUNT;
3933 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00003934 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
3936 edit_submode_extra = (char_u *)_("The only match");
3937 edit_submode_highl = HLF_COUNT;
3938 }
3939 else
3940 {
3941 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003942 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003944 int number = 0;
3945 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003947 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
3949 /* search backwards for the first valid (!= -1) number.
3950 * This should normally succeed already at the first loop
3951 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003952 for (match = compl_curr_match->cp_prev; match != NULL
3953 && match != compl_first_match;
3954 match = match->cp_prev)
3955 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003957 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 break;
3959 }
3960 if (match != NULL)
3961 /* go up and assign all numbers which are not assigned
3962 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003963 for (match = match->cp_next;
3964 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003965 match = match->cp_next)
3966 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 }
3968 else /* BACKWARD */
3969 {
3970 /* search forwards (upwards) for the first valid (!= -1)
3971 * number. This should normally succeed already at the
3972 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003973 for (match = compl_curr_match->cp_next; match != NULL
3974 && match != compl_first_match;
3975 match = match->cp_next)
3976 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003978 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 break;
3980 }
3981 if (match != NULL)
3982 /* go down and assign all numbers which are not
3983 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003984 for (match = match->cp_prev; match
3985 && match->cp_number == -1;
3986 match = match->cp_prev)
3987 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
3989 }
3990
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003991 /* The match should always have a sequence number now, this is
3992 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003993 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 {
3995 /* Space for 10 text chars. + 2x10-digit no.s */
3996 static char_u match_ref[31];
3997
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003998 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004000 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004002 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004003 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004004 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 edit_submode_extra = match_ref;
4006 edit_submode_highl = HLF_R;
4007 if (dollar_vcol)
4008 curs_columns(FALSE);
4009 }
4010 }
4011 }
4012
4013 /* Show a message about what (completion) mode we're in. */
4014 showmode();
4015 if (edit_submode_extra != NULL)
4016 {
4017 if (!p_smd)
4018 msg_attr(edit_submode_extra,
4019 edit_submode_highl < HLF_COUNT
4020 ? hl_attr(edit_submode_highl) : 0);
4021 }
4022 else
4023 msg_clr_cmdline(); /* necessary for "noshowmode" */
4024
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004025 ins_compl_show_pum();
4026
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 return OK;
4028}
4029
4030/*
4031 * Looks in the first "len" chars. of "src" for search-metachars.
4032 * If dest is not NULL the chars. are copied there quoting (with
4033 * a backslash) the metachars, and dest would be NUL terminated.
4034 * Returns the length (needed) of dest
4035 */
4036 static int
4037quote_meta(dest, src, len)
4038 char_u *dest;
4039 char_u *src;
4040 int len;
4041{
4042 int m;
4043
4044 for (m = len; --len >= 0; src++)
4045 {
4046 switch (*src)
4047 {
4048 case '.':
4049 case '*':
4050 case '[':
4051 if (ctrl_x_mode == CTRL_X_DICTIONARY
4052 || ctrl_x_mode == CTRL_X_THESAURUS)
4053 break;
4054 case '~':
4055 if (!p_magic) /* quote these only if magic is set */
4056 break;
4057 case '\\':
4058 if (ctrl_x_mode == CTRL_X_DICTIONARY
4059 || ctrl_x_mode == CTRL_X_THESAURUS)
4060 break;
4061 case '^': /* currently it's not needed. */
4062 case '$':
4063 m++;
4064 if (dest != NULL)
4065 *dest++ = '\\';
4066 break;
4067 }
4068 if (dest != NULL)
4069 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004070# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 /* Copy remaining bytes of a multibyte character. */
4072 if (has_mbyte)
4073 {
4074 int i, mb_len;
4075
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004076 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 if (mb_len > 0 && len >= mb_len)
4078 for (i = 0; i < mb_len; ++i)
4079 {
4080 --len;
4081 ++src;
4082 if (dest != NULL)
4083 *dest++ = *src;
4084 }
4085 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004086# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088 if (dest != NULL)
4089 *dest = NUL;
4090
4091 return m;
4092}
4093#endif /* FEAT_INS_EXPAND */
4094
4095/*
4096 * Next character is interpreted literally.
4097 * A one, two or three digit decimal number is interpreted as its byte value.
4098 * If one or two digits are entered, the next character is given to vungetc().
4099 * For Unicode a character > 255 may be returned.
4100 */
4101 int
4102get_literal()
4103{
4104 int cc;
4105 int nc;
4106 int i;
4107 int hex = FALSE;
4108 int octal = FALSE;
4109#ifdef FEAT_MBYTE
4110 int unicode = 0;
4111#endif
4112
4113 if (got_int)
4114 return Ctrl_C;
4115
4116#ifdef FEAT_GUI
4117 /*
4118 * In GUI there is no point inserting the internal code for a special key.
4119 * It is more useful to insert the string "<KEY>" instead. This would
4120 * probably be useful in a text window too, but it would not be
4121 * vi-compatible (maybe there should be an option for it?) -- webb
4122 */
4123 if (gui.in_use)
4124 ++allow_keys;
4125#endif
4126#ifdef USE_ON_FLY_SCROLL
4127 dont_scroll = TRUE; /* disallow scrolling here */
4128#endif
4129 ++no_mapping; /* don't map the next key hits */
4130 cc = 0;
4131 i = 0;
4132 for (;;)
4133 {
4134 do
4135 nc = safe_vgetc();
4136 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4137 || nc == K_HOR_SCROLLBAR);
4138#ifdef FEAT_CMDL_INFO
4139 if (!(State & CMDLINE)
4140# ifdef FEAT_MBYTE
4141 && MB_BYTE2LEN_CHECK(nc) == 1
4142# endif
4143 )
4144 add_to_showcmd(nc);
4145#endif
4146 if (nc == 'x' || nc == 'X')
4147 hex = TRUE;
4148 else if (nc == 'o' || nc == 'O')
4149 octal = TRUE;
4150#ifdef FEAT_MBYTE
4151 else if (nc == 'u' || nc == 'U')
4152 unicode = nc;
4153#endif
4154 else
4155 {
4156 if (hex
4157#ifdef FEAT_MBYTE
4158 || unicode != 0
4159#endif
4160 )
4161 {
4162 if (!vim_isxdigit(nc))
4163 break;
4164 cc = cc * 16 + hex2nr(nc);
4165 }
4166 else if (octal)
4167 {
4168 if (nc < '0' || nc > '7')
4169 break;
4170 cc = cc * 8 + nc - '0';
4171 }
4172 else
4173 {
4174 if (!VIM_ISDIGIT(nc))
4175 break;
4176 cc = cc * 10 + nc - '0';
4177 }
4178
4179 ++i;
4180 }
4181
4182 if (cc > 255
4183#ifdef FEAT_MBYTE
4184 && unicode == 0
4185#endif
4186 )
4187 cc = 255; /* limit range to 0-255 */
4188 nc = 0;
4189
4190 if (hex) /* hex: up to two chars */
4191 {
4192 if (i >= 2)
4193 break;
4194 }
4195#ifdef FEAT_MBYTE
4196 else if (unicode) /* Unicode: up to four or eight chars */
4197 {
4198 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4199 break;
4200 }
4201#endif
4202 else if (i >= 3) /* decimal or octal: up to three chars */
4203 break;
4204 }
4205 if (i == 0) /* no number entered */
4206 {
4207 if (nc == K_ZERO) /* NUL is stored as NL */
4208 {
4209 cc = '\n';
4210 nc = 0;
4211 }
4212 else
4213 {
4214 cc = nc;
4215 nc = 0;
4216 }
4217 }
4218
4219 if (cc == 0) /* NUL is stored as NL */
4220 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004221#ifdef FEAT_MBYTE
4222 if (enc_dbcs && (cc & 0xff) == 0)
4223 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4224 second byte will cause trouble! */
4225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226
4227 --no_mapping;
4228#ifdef FEAT_GUI
4229 if (gui.in_use)
4230 --allow_keys;
4231#endif
4232 if (nc)
4233 vungetc(nc);
4234 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4235 return cc;
4236}
4237
4238/*
4239 * Insert character, taking care of special keys and mod_mask
4240 */
4241 static void
4242insert_special(c, allow_modmask, ctrlv)
4243 int c;
4244 int allow_modmask;
4245 int ctrlv; /* c was typed after CTRL-V */
4246{
4247 char_u *p;
4248 int len;
4249
4250 /*
4251 * Special function key, translate into "<Key>". Up to the last '>' is
4252 * inserted with ins_str(), so as not to replace characters in replace
4253 * mode.
4254 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4255 * unless 'allow_modmask' is TRUE.
4256 */
4257#ifdef MACOS
4258 /* Command-key never produces a normal key */
4259 if (mod_mask & MOD_MASK_CMD)
4260 allow_modmask = TRUE;
4261#endif
4262 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4263 {
4264 p = get_special_key_name(c, mod_mask);
4265 len = (int)STRLEN(p);
4266 c = p[len - 1];
4267 if (len > 2)
4268 {
4269 if (stop_arrow() == FAIL)
4270 return;
4271 p[len - 1] = NUL;
4272 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004273 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 ctrlv = FALSE;
4275 }
4276 }
4277 if (stop_arrow() == OK)
4278 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4279}
4280
4281/*
4282 * Special characters in this context are those that need processing other
4283 * than the simple insertion that can be performed here. This includes ESC
4284 * which terminates the insert, and CR/NL which need special processing to
4285 * open up a new line. This routine tries to optimize insertions performed by
4286 * the "redo", "undo" or "put" commands, so it needs to know when it should
4287 * stop and defer processing to the "normal" mechanism.
4288 * '0' and '^' are special, because they can be followed by CTRL-D.
4289 */
4290#ifdef EBCDIC
4291# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4292#else
4293# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4294#endif
4295
4296#ifdef FEAT_MBYTE
4297# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4298#else
4299# define WHITECHAR(cc) vim_iswhite(cc)
4300#endif
4301
4302 void
4303insertchar(c, flags, second_indent)
4304 int c; /* character to insert or NUL */
4305 int flags; /* INSCHAR_FORMAT, etc. */
4306 int second_indent; /* indent for second line if >= 0 */
4307{
4308 int haveto_redraw = FALSE;
4309 int textwidth;
4310#ifdef FEAT_COMMENTS
4311 colnr_T leader_len;
4312 char_u *p;
4313 int no_leader = FALSE;
4314 int do_comments = (flags & INSCHAR_DO_COM);
4315#endif
4316 int fo_white_par;
4317 int first_line = TRUE;
4318 int fo_ins_blank;
4319#ifdef FEAT_MBYTE
4320 int fo_multibyte;
4321#endif
4322 int save_char = NUL;
4323 int cc;
4324
4325 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4326 fo_ins_blank = has_format_option(FO_INS_BLANK);
4327#ifdef FEAT_MBYTE
4328 fo_multibyte = has_format_option(FO_MBYTE_BREAK);
4329#endif
4330 fo_white_par = has_format_option(FO_WHITE_PAR);
4331
4332 /*
4333 * Try to break the line in two or more pieces when:
4334 * - Always do this if we have been called to do formatting only.
4335 * - Always do this when 'formatoptions' has the 'a' flag and the line
4336 * ends in white space.
4337 * - Otherwise:
4338 * - Don't do this if inserting a blank
4339 * - Don't do this if an existing character is being replaced, unless
4340 * we're in VREPLACE mode.
4341 * - Do this if the cursor is not on the line where insert started
4342 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4343 * before the insert.
4344 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4345 * before 'textwidth'
4346 */
4347 if (textwidth
4348 && ((flags & INSCHAR_FORMAT)
4349 || (!vim_iswhite(c)
4350 && !((State & REPLACE_FLAG)
4351#ifdef FEAT_VREPLACE
4352 && !(State & VREPLACE_FLAG)
4353#endif
4354 && *ml_get_cursor() != NUL)
4355 && (curwin->w_cursor.lnum != Insstart.lnum
4356 || ((!has_format_option(FO_INS_LONG)
4357 || Insstart_textlen <= (colnr_T)textwidth)
4358 && (!fo_ins_blank
4359 || Insstart_blank_vcol <= (colnr_T)textwidth
4360 ))))))
4361 {
4362 /*
4363 * When 'ai' is off we don't want a space under the cursor to be
4364 * deleted. Replace it with an 'x' temporarily.
4365 */
4366 if (!curbuf->b_p_ai)
4367 {
4368 cc = gchar_cursor();
4369 if (vim_iswhite(cc))
4370 {
4371 save_char = cc;
4372 pchar_cursor('x');
4373 }
4374 }
4375
4376 /*
4377 * Repeat breaking lines, until the current line is not too long.
4378 */
4379 while (!got_int)
4380 {
4381 int startcol; /* Cursor column at entry */
4382 int wantcol; /* column at textwidth border */
4383 int foundcol; /* column for start of spaces */
4384 int end_foundcol = 0; /* column for start of word */
4385 colnr_T len;
4386 colnr_T virtcol;
4387#ifdef FEAT_VREPLACE
4388 int orig_col = 0;
4389 char_u *saved_text = NULL;
4390#endif
4391 colnr_T col;
4392
4393 virtcol = get_nolist_virtcol();
4394 if (virtcol < (colnr_T)textwidth)
4395 break;
4396
4397#ifdef FEAT_COMMENTS
4398 if (no_leader)
4399 do_comments = FALSE;
4400 else if (!(flags & INSCHAR_FORMAT)
4401 && has_format_option(FO_WRAP_COMS))
4402 do_comments = TRUE;
4403
4404 /* Don't break until after the comment leader */
4405 if (do_comments)
4406 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
4407 else
4408 leader_len = 0;
4409
4410 /* If the line doesn't start with a comment leader, then don't
4411 * start one in a following broken line. Avoids that a %word
4412 * moved to the start of the next line causes all following lines
4413 * to start with %. */
4414 if (leader_len == 0)
4415 no_leader = TRUE;
4416#endif
4417 if (!(flags & INSCHAR_FORMAT)
4418#ifdef FEAT_COMMENTS
4419 && leader_len == 0
4420#endif
4421 && !has_format_option(FO_WRAP))
4422
4423 {
4424 textwidth = 0;
4425 break;
4426 }
4427 if ((startcol = curwin->w_cursor.col) == 0)
4428 break;
4429
4430 /* find column of textwidth border */
4431 coladvance((colnr_T)textwidth);
4432 wantcol = curwin->w_cursor.col;
4433
4434 curwin->w_cursor.col = startcol - 1;
4435#ifdef FEAT_MBYTE
4436 /* Correct cursor for multi-byte character. */
4437 if (has_mbyte)
4438 mb_adjust_cursor();
4439#endif
4440 foundcol = 0;
4441
4442 /*
4443 * Find position to break at.
4444 * Stop at first entered white when 'formatoptions' has 'v'
4445 */
4446 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
4447 || curwin->w_cursor.lnum != Insstart.lnum
4448 || curwin->w_cursor.col >= Insstart.col)
4449 {
4450 cc = gchar_cursor();
4451 if (WHITECHAR(cc))
4452 {
4453 /* remember position of blank just before text */
4454 end_foundcol = curwin->w_cursor.col;
4455
4456 /* find start of sequence of blanks */
4457 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
4458 {
4459 dec_cursor();
4460 cc = gchar_cursor();
4461 }
4462 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
4463 break; /* only spaces in front of text */
4464#ifdef FEAT_COMMENTS
4465 /* Don't break until after the comment leader */
4466 if (curwin->w_cursor.col < leader_len)
4467 break;
4468#endif
4469 if (has_format_option(FO_ONE_LETTER))
4470 {
4471 /* do not break after one-letter words */
4472 if (curwin->w_cursor.col == 0)
4473 break; /* one-letter word at begin */
4474
4475 col = curwin->w_cursor.col;
4476 dec_cursor();
4477 cc = gchar_cursor();
4478
4479 if (WHITECHAR(cc))
4480 continue; /* one-letter, continue */
4481 curwin->w_cursor.col = col;
4482 }
4483#ifdef FEAT_MBYTE
4484 if (has_mbyte)
4485 foundcol = curwin->w_cursor.col
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004486 + (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 else
4488#endif
4489 foundcol = curwin->w_cursor.col + 1;
4490 if (curwin->w_cursor.col < (colnr_T)wantcol)
4491 break;
4492 }
4493#ifdef FEAT_MBYTE
4494 else if (cc >= 0x100 && fo_multibyte
4495 && curwin->w_cursor.col <= (colnr_T)wantcol)
4496 {
4497 /* Break after or before a multi-byte character. */
4498 foundcol = curwin->w_cursor.col;
4499 if (curwin->w_cursor.col < (colnr_T)wantcol)
4500 foundcol += (*mb_char2len)(cc);
4501 end_foundcol = foundcol;
4502 break;
4503 }
4504#endif
4505 if (curwin->w_cursor.col == 0)
4506 break;
4507 dec_cursor();
4508 }
4509
4510 if (foundcol == 0) /* no spaces, cannot break line */
4511 {
4512 curwin->w_cursor.col = startcol;
4513 break;
4514 }
4515
4516 /* Going to break the line, remove any "$" now. */
4517 undisplay_dollar();
4518
4519 /*
4520 * Offset between cursor position and line break is used by replace
4521 * stack functions. VREPLACE does not use this, and backspaces
4522 * over the text instead.
4523 */
4524#ifdef FEAT_VREPLACE
4525 if (State & VREPLACE_FLAG)
4526 orig_col = startcol; /* Will start backspacing from here */
4527 else
4528#endif
4529 replace_offset = startcol - end_foundcol - 1;
4530
4531 /*
4532 * adjust startcol for spaces that will be deleted and
4533 * characters that will remain on top line
4534 */
4535 curwin->w_cursor.col = foundcol;
4536 while (cc = gchar_cursor(), WHITECHAR(cc))
4537 inc_cursor();
4538 startcol -= curwin->w_cursor.col;
4539 if (startcol < 0)
4540 startcol = 0;
4541
4542#ifdef FEAT_VREPLACE
4543 if (State & VREPLACE_FLAG)
4544 {
4545 /*
4546 * In VREPLACE mode, we will backspace over the text to be
4547 * wrapped, so save a copy now to put on the next line.
4548 */
4549 saved_text = vim_strsave(ml_get_cursor());
4550 curwin->w_cursor.col = orig_col;
4551 if (saved_text == NULL)
4552 break; /* Can't do it, out of memory */
4553 saved_text[startcol] = NUL;
4554
4555 /* Backspace over characters that will move to the next line */
4556 if (!fo_white_par)
4557 backspace_until_column(foundcol);
4558 }
4559 else
4560#endif
4561 {
4562 /* put cursor after pos. to break line */
4563 if (!fo_white_par)
4564 curwin->w_cursor.col = foundcol;
4565 }
4566
4567 /*
4568 * Split the line just before the margin.
4569 * Only insert/delete lines, but don't really redraw the window.
4570 */
4571 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
4572 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
4573#ifdef FEAT_COMMENTS
4574 + (do_comments ? OPENLINE_DO_COM : 0)
4575#endif
4576 , old_indent);
4577 old_indent = 0;
4578
4579 replace_offset = 0;
4580 if (first_line)
4581 {
4582 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
4583 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
4584 if (second_indent >= 0)
4585 {
4586#ifdef FEAT_VREPLACE
4587 if (State & VREPLACE_FLAG)
4588 change_indent(INDENT_SET, second_indent, FALSE, NUL);
4589 else
4590#endif
4591 (void)set_indent(second_indent, SIN_CHANGED);
4592 }
4593 first_line = FALSE;
4594 }
4595
4596#ifdef FEAT_VREPLACE
4597 if (State & VREPLACE_FLAG)
4598 {
4599 /*
4600 * In VREPLACE mode we have backspaced over the text to be
4601 * moved, now we re-insert it into the new line.
4602 */
4603 ins_bytes(saved_text);
4604 vim_free(saved_text);
4605 }
4606 else
4607#endif
4608 {
4609 /*
4610 * Check if cursor is not past the NUL off the line, cindent
4611 * may have added or removed indent.
4612 */
4613 curwin->w_cursor.col += startcol;
4614 len = (colnr_T)STRLEN(ml_get_curline());
4615 if (curwin->w_cursor.col > len)
4616 curwin->w_cursor.col = len;
4617 }
4618
4619 haveto_redraw = TRUE;
4620#ifdef FEAT_CINDENT
4621 can_cindent = TRUE;
4622#endif
4623 /* moved the cursor, don't autoindent or cindent now */
4624 did_ai = FALSE;
4625#ifdef FEAT_SMARTINDENT
4626 did_si = FALSE;
4627 can_si = FALSE;
4628 can_si_back = FALSE;
4629#endif
4630 line_breakcheck();
4631 }
4632
4633 if (save_char) /* put back space after cursor */
4634 pchar_cursor(save_char);
4635
4636 if (c == NUL) /* formatting only */
4637 return;
4638 if (haveto_redraw)
4639 {
4640 update_topline();
4641 redraw_curbuf_later(VALID);
4642 }
4643 }
4644 if (c == NUL) /* only formatting was wanted */
4645 return;
4646
4647#ifdef FEAT_COMMENTS
4648 /* Check whether this character should end a comment. */
4649 if (did_ai && (int)c == end_comment_pending)
4650 {
4651 char_u *line;
4652 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4653 int middle_len, end_len;
4654 int i;
4655
4656 /*
4657 * Need to remove existing (middle) comment leader and insert end
4658 * comment leader. First, check what comment leader we can find.
4659 */
4660 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4661 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4662 {
4663 /* Skip middle-comment string */
4664 while (*p && p[-1] != ':') /* find end of middle flags */
4665 ++p;
4666 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4667 /* Don't count trailing white space for middle_len */
4668 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4669 --middle_len;
4670
4671 /* Find the end-comment string */
4672 while (*p && p[-1] != ':') /* find end of end flags */
4673 ++p;
4674 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4675
4676 /* Skip white space before the cursor */
4677 i = curwin->w_cursor.col;
4678 while (--i >= 0 && vim_iswhite(line[i]))
4679 ;
4680 i++;
4681
4682 /* Skip to before the middle leader */
4683 i -= middle_len;
4684
4685 /* Check some expected things before we go on */
4686 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4687 {
4688 /* Backspace over all the stuff we want to replace */
4689 backspace_until_column(i);
4690
4691 /*
4692 * Insert the end-comment string, except for the last
4693 * character, which will get inserted as normal later.
4694 */
4695 ins_bytes_len(lead_end, end_len - 1);
4696 }
4697 }
4698 }
4699 end_comment_pending = NUL;
4700#endif
4701
4702 did_ai = FALSE;
4703#ifdef FEAT_SMARTINDENT
4704 did_si = FALSE;
4705 can_si = FALSE;
4706 can_si_back = FALSE;
4707#endif
4708
4709 /*
4710 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4711 * This speeds up normal text input considerably.
4712 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4713 * need to re-indent at a ':', or any other character (but not what
4714 * 'paste' is set)..
4715 */
4716#ifdef USE_ON_FLY_SCROLL
4717 dont_scroll = FALSE; /* allow scrolling here */
4718#endif
4719
4720 if ( !ISSPECIAL(c)
4721#ifdef FEAT_MBYTE
4722 && (!has_mbyte || (*mb_char2len)(c) == 1)
4723#endif
4724 && vpeekc() != NUL
4725 && !(State & REPLACE_FLAG)
4726#ifdef FEAT_CINDENT
4727 && !cindent_on()
4728#endif
4729#ifdef FEAT_RIGHTLEFT
4730 && !p_ri
4731#endif
4732 )
4733 {
4734#define INPUT_BUFLEN 100
4735 char_u buf[INPUT_BUFLEN + 1];
4736 int i;
4737 colnr_T virtcol = 0;
4738
4739 buf[0] = c;
4740 i = 1;
4741 if (textwidth)
4742 virtcol = get_nolist_virtcol();
4743 /*
4744 * Stop the string when:
4745 * - no more chars available
4746 * - finding a special character (command key)
4747 * - buffer is full
4748 * - running into the 'textwidth' boundary
4749 * - need to check for abbreviation: A non-word char after a word-char
4750 */
4751 while ( (c = vpeekc()) != NUL
4752 && !ISSPECIAL(c)
4753#ifdef FEAT_MBYTE
4754 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4755#endif
4756 && i < INPUT_BUFLEN
4757 && (textwidth == 0
4758 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4759 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4760 {
4761#ifdef FEAT_RIGHTLEFT
4762 c = vgetc();
4763 if (p_hkmap && KeyTyped)
4764 c = hkmap(c); /* Hebrew mode mapping */
4765# ifdef FEAT_FKMAP
4766 if (p_fkmap && KeyTyped)
4767 c = fkmap(c); /* Farsi mode mapping */
4768# endif
4769 buf[i++] = c;
4770#else
4771 buf[i++] = vgetc();
4772#endif
4773 }
4774
4775#ifdef FEAT_DIGRAPHS
4776 do_digraph(-1); /* clear digraphs */
4777 do_digraph(buf[i-1]); /* may be the start of a digraph */
4778#endif
4779 buf[i] = NUL;
4780 ins_str(buf);
4781 if (flags & INSCHAR_CTRLV)
4782 {
4783 redo_literal(*buf);
4784 i = 1;
4785 }
4786 else
4787 i = 0;
4788 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004789 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 }
4791 else
4792 {
4793#ifdef FEAT_MBYTE
4794 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
4795 {
4796 char_u buf[MB_MAXBYTES + 1];
4797
4798 (*mb_char2bytes)(c, buf);
4799 buf[cc] = NUL;
4800 ins_char_bytes(buf, cc);
4801 AppendCharToRedobuff(c);
4802 }
4803 else
4804#endif
4805 {
4806 ins_char(c);
4807 if (flags & INSCHAR_CTRLV)
4808 redo_literal(c);
4809 else
4810 AppendCharToRedobuff(c);
4811 }
4812 }
4813}
4814
4815/*
4816 * Called after inserting or deleting text: When 'formatoptions' includes the
4817 * 'a' flag format from the current line until the end of the paragraph.
4818 * Keep the cursor at the same position relative to the text.
4819 * The caller must have saved the cursor line for undo, following ones will be
4820 * saved here.
4821 */
4822 void
4823auto_format(trailblank, prev_line)
4824 int trailblank; /* when TRUE also format with trailing blank */
4825 int prev_line; /* may start in previous line */
4826{
4827 pos_T pos;
4828 colnr_T len;
4829 char_u *old;
4830 char_u *new, *pnew;
4831 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004832 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833
4834 if (!has_format_option(FO_AUTO))
4835 return;
4836
4837 pos = curwin->w_cursor;
4838 old = ml_get_curline();
4839
4840 /* may remove added space */
4841 check_auto_format(FALSE);
4842
4843 /* Don't format in Insert mode when the cursor is on a trailing blank, the
4844 * user might insert normal text next. Also skip formatting when "1" is
4845 * in 'formatoptions' and there is a single character before the cursor.
4846 * Otherwise the line would be broken and when typing another non-white
4847 * next they are not joined back together. */
4848 wasatend = (pos.col == STRLEN(old));
4849 if (*old != NUL && !trailblank && wasatend)
4850 {
4851 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004852 cc = gchar_cursor();
4853 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
4854 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004856 cc = gchar_cursor();
4857 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 {
4859 curwin->w_cursor = pos;
4860 return;
4861 }
4862 curwin->w_cursor = pos;
4863 }
4864
4865#ifdef FEAT_COMMENTS
4866 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
4867 * comments. */
4868 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
4869 && get_leader_len(old, NULL, FALSE) == 0)
4870 return;
4871#endif
4872
4873 /*
4874 * May start formatting in a previous line, so that after "x" a word is
4875 * moved to the previous line if it fits there now. Only when this is not
4876 * the start of a paragraph.
4877 */
4878 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
4879 {
4880 --curwin->w_cursor.lnum;
4881 if (u_save_cursor() == FAIL)
4882 return;
4883 }
4884
4885 /*
4886 * Do the formatting and restore the cursor position. "saved_cursor" will
4887 * be adjusted for the text formatting.
4888 */
4889 saved_cursor = pos;
4890 format_lines((linenr_T)-1);
4891 curwin->w_cursor = saved_cursor;
4892 saved_cursor.lnum = 0;
4893
4894 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4895 {
4896 /* "cannot happen" */
4897 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4898 coladvance((colnr_T)MAXCOL);
4899 }
4900 else
4901 check_cursor_col();
4902
4903 /* Insert mode: If the cursor is now after the end of the line while it
4904 * previously wasn't, the line was broken. Because of the rule above we
4905 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
4906 * formatted. */
4907 if (!wasatend && has_format_option(FO_WHITE_PAR))
4908 {
4909 new = ml_get_curline();
4910 len = STRLEN(new);
4911 if (curwin->w_cursor.col == len)
4912 {
4913 pnew = vim_strnsave(new, len + 2);
4914 pnew[len] = ' ';
4915 pnew[len + 1] = NUL;
4916 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
4917 /* remove the space later */
4918 did_add_space = TRUE;
4919 }
4920 else
4921 /* may remove added space */
4922 check_auto_format(FALSE);
4923 }
4924
4925 check_cursor();
4926}
4927
4928/*
4929 * When an extra space was added to continue a paragraph for auto-formatting,
4930 * delete it now. The space must be under the cursor, just after the insert
4931 * position.
4932 */
4933 static void
4934check_auto_format(end_insert)
4935 int end_insert; /* TRUE when ending Insert mode */
4936{
4937 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004938 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
4940 if (did_add_space)
4941 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004942 cc = gchar_cursor();
4943 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 /* Somehow the space was removed already. */
4945 did_add_space = FALSE;
4946 else
4947 {
4948 if (!end_insert)
4949 {
4950 inc_cursor();
4951 c = gchar_cursor();
4952 dec_cursor();
4953 }
4954 if (c != NUL)
4955 {
4956 /* The space is no longer at the end of the line, delete it. */
4957 del_char(FALSE);
4958 did_add_space = FALSE;
4959 }
4960 }
4961 }
4962}
4963
4964/*
4965 * Find out textwidth to be used for formatting:
4966 * if 'textwidth' option is set, use it
4967 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
4968 * if invalid value, use 0.
4969 * Set default to window width (maximum 79) for "gq" operator.
4970 */
4971 int
4972comp_textwidth(ff)
4973 int ff; /* force formatting (for "Q" command) */
4974{
4975 int textwidth;
4976
4977 textwidth = curbuf->b_p_tw;
4978 if (textwidth == 0 && curbuf->b_p_wm)
4979 {
4980 /* The width is the window width minus 'wrapmargin' minus all the
4981 * things that add to the margin. */
4982 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
4983#ifdef FEAT_CMDWIN
4984 if (cmdwin_type != 0)
4985 textwidth -= 1;
4986#endif
4987#ifdef FEAT_FOLDING
4988 textwidth -= curwin->w_p_fdc;
4989#endif
4990#ifdef FEAT_SIGNS
4991 if (curwin->w_buffer->b_signlist != NULL
4992# ifdef FEAT_NETBEANS_INTG
4993 || usingNetbeans
4994# endif
4995 )
4996 textwidth -= 1;
4997#endif
4998 if (curwin->w_p_nu)
4999 textwidth -= 8;
5000 }
5001 if (textwidth < 0)
5002 textwidth = 0;
5003 if (ff && textwidth == 0)
5004 {
5005 textwidth = W_WIDTH(curwin) - 1;
5006 if (textwidth > 79)
5007 textwidth = 79;
5008 }
5009 return textwidth;
5010}
5011
5012/*
5013 * Put a character in the redo buffer, for when just after a CTRL-V.
5014 */
5015 static void
5016redo_literal(c)
5017 int c;
5018{
5019 char_u buf[10];
5020
5021 /* Only digits need special treatment. Translate them into a string of
5022 * three digits. */
5023 if (VIM_ISDIGIT(c))
5024 {
5025 sprintf((char *)buf, "%03d", c);
5026 AppendToRedobuff(buf);
5027 }
5028 else
5029 AppendCharToRedobuff(c);
5030}
5031
5032/*
5033 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005034 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 */
5036 static void
5037start_arrow(end_insert_pos)
5038 pos_T *end_insert_pos;
5039{
5040 if (!arrow_used) /* something has been inserted */
5041 {
5042 AppendToRedobuff(ESC_STR);
5043 stop_insert(end_insert_pos, FALSE);
5044 arrow_used = TRUE; /* this means we stopped the current insert */
5045 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005046#ifdef FEAT_SYN_HL
5047 check_spell_redraw();
5048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049}
5050
Bram Moolenaar217ad922005-03-20 22:37:15 +00005051#ifdef FEAT_SYN_HL
5052/*
5053 * If we skipped highlighting word at cursor, do it now.
5054 * It may be skipped again, thus reset spell_redraw_lnum first.
5055 */
5056 static void
5057check_spell_redraw()
5058{
5059 if (spell_redraw_lnum != 0)
5060 {
5061 linenr_T lnum = spell_redraw_lnum;
5062
5063 spell_redraw_lnum = 0;
5064 redrawWinline(lnum, FALSE);
5065 }
5066}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005067
5068/*
5069 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5070 * spelled word, if there is one.
5071 */
5072 static void
5073spell_back_to_badword()
5074{
5075 pos_T tpos = curwin->w_cursor;
5076
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005077 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005078 if (curwin->w_cursor.col != tpos.col)
5079 start_arrow(&tpos);
5080}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005081#endif
5082
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083/*
5084 * stop_arrow() is called before a change is made in insert mode.
5085 * If an arrow key has been used, start a new insertion.
5086 * Returns FAIL if undo is impossible, shouldn't insert then.
5087 */
5088 int
5089stop_arrow()
5090{
5091 if (arrow_used)
5092 {
5093 if (u_save_cursor() == OK)
5094 {
5095 arrow_used = FALSE;
5096 ins_need_undo = FALSE;
5097 }
5098 Insstart = curwin->w_cursor; /* new insertion starts here */
5099 Insstart_textlen = linetabsize(ml_get_curline());
5100 ai_col = 0;
5101#ifdef FEAT_VREPLACE
5102 if (State & VREPLACE_FLAG)
5103 {
5104 orig_line_count = curbuf->b_ml.ml_line_count;
5105 vr_lines_changed = 1;
5106 }
5107#endif
5108 ResetRedobuff();
5109 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005110 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
5112 else if (ins_need_undo)
5113 {
5114 if (u_save_cursor() == OK)
5115 ins_need_undo = FALSE;
5116 }
5117
5118#ifdef FEAT_FOLDING
5119 /* Always open fold at the cursor line when inserting something. */
5120 foldOpenCursor();
5121#endif
5122
5123 return (arrow_used || ins_need_undo ? FAIL : OK);
5124}
5125
5126/*
5127 * do a few things to stop inserting
5128 */
5129 static void
5130stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005131 pos_T *end_insert_pos; /* where insert ended */
5132 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005134 int cc;
5135 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136
5137 stop_redo_ins();
5138 replace_flush(); /* abandon replace stack */
5139
5140 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005141 * Save the inserted text for later redo with ^@ and CTRL-A.
5142 * Don't do it when "restart_edit" was set and nothing was inserted,
5143 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005145 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005146 if (did_restart_edit == 0 || (ptr != NULL
5147 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005148 {
5149 vim_free(last_insert);
5150 last_insert = ptr;
5151 last_insert_skip = new_insert_skip;
5152 }
5153 else
5154 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155
5156 if (!arrow_used)
5157 {
5158 /* Auto-format now. It may seem strange to do this when stopping an
5159 * insertion (or moving the cursor), but it's required when appending
5160 * a line and having it end in a space. But only do it when something
5161 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005162 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005164 pos_T tpos = curwin->w_cursor;
5165
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 /* When the cursor is at the end of the line after a space the
5167 * formatting will move it to the following word. Avoid that by
5168 * moving the cursor onto the space. */
5169 cc = 'x';
5170 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5171 {
5172 dec_cursor();
5173 cc = gchar_cursor();
5174 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005175 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
5177
5178 auto_format(TRUE, FALSE);
5179
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005180 if (vim_iswhite(cc))
5181 {
5182 if (gchar_cursor() != NUL)
5183 inc_cursor();
5184#ifdef FEAT_VIRTUALEDIT
5185 /* If the cursor is still at the same character, also keep
5186 * the "coladd". */
5187 if (gchar_cursor() == NUL
5188 && curwin->w_cursor.lnum == tpos.lnum
5189 && curwin->w_cursor.col == tpos.col)
5190 curwin->w_cursor.coladd = tpos.coladd;
5191#endif
5192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194
5195 /* If a space was inserted for auto-formatting, remove it now. */
5196 check_auto_format(TRUE);
5197
5198 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005199 * of the line, and put the cursor back.
5200 * Do this when ESC was used or moving the cursor up/down. */
5201 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5202 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005204 pos_T tpos = curwin->w_cursor;
5205
5206 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5208 --curwin->w_cursor.col;
5209 while (cc = gchar_cursor(), vim_iswhite(cc))
5210 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005211 if (curwin->w_cursor.lnum != tpos.lnum)
5212 curwin->w_cursor = tpos;
5213 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5215
5216#ifdef FEAT_VISUAL
5217 /* <C-S-Right> may have started Visual mode, adjust the position for
5218 * deleted characters. */
5219 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5220 {
5221 cc = STRLEN(ml_get_curline());
5222 if (VIsual.col > (colnr_T)cc)
5223 {
5224 VIsual.col = cc;
5225# ifdef FEAT_VIRTUALEDIT
5226 VIsual.coladd = 0;
5227# endif
5228 }
5229 }
5230#endif
5231 }
5232 }
5233 did_ai = FALSE;
5234#ifdef FEAT_SMARTINDENT
5235 did_si = FALSE;
5236 can_si = FALSE;
5237 can_si_back = FALSE;
5238#endif
5239
5240 /* set '[ and '] to the inserted text */
5241 curbuf->b_op_start = Insstart;
5242 curbuf->b_op_end = *end_insert_pos;
5243}
5244
5245/*
5246 * Set the last inserted text to a single character.
5247 * Used for the replace command.
5248 */
5249 void
5250set_last_insert(c)
5251 int c;
5252{
5253 char_u *s;
5254
5255 vim_free(last_insert);
5256#ifdef FEAT_MBYTE
5257 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5258#else
5259 last_insert = alloc(6);
5260#endif
5261 if (last_insert != NULL)
5262 {
5263 s = last_insert;
5264 /* Use the CTRL-V only when entering a special char */
5265 if (c < ' ' || c == DEL)
5266 *s++ = Ctrl_V;
5267 s = add_char2buf(c, s);
5268 *s++ = ESC;
5269 *s++ = NUL;
5270 last_insert_skip = 0;
5271 }
5272}
5273
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005274#if defined(EXITFREE) || defined(PROTO)
5275 void
5276free_last_insert()
5277{
5278 vim_free(last_insert);
5279 last_insert = NULL;
5280}
5281#endif
5282
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283/*
5284 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5285 * and CSI. Handle multi-byte characters.
5286 * Returns a pointer to after the added bytes.
5287 */
5288 char_u *
5289add_char2buf(c, s)
5290 int c;
5291 char_u *s;
5292{
5293#ifdef FEAT_MBYTE
5294 char_u temp[MB_MAXBYTES];
5295 int i;
5296 int len;
5297
5298 len = (*mb_char2bytes)(c, temp);
5299 for (i = 0; i < len; ++i)
5300 {
5301 c = temp[i];
5302#endif
5303 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5304 if (c == K_SPECIAL)
5305 {
5306 *s++ = K_SPECIAL;
5307 *s++ = KS_SPECIAL;
5308 *s++ = KE_FILLER;
5309 }
5310#ifdef FEAT_GUI
5311 else if (c == CSI)
5312 {
5313 *s++ = CSI;
5314 *s++ = KS_EXTRA;
5315 *s++ = (int)KE_CSI;
5316 }
5317#endif
5318 else
5319 *s++ = c;
5320#ifdef FEAT_MBYTE
5321 }
5322#endif
5323 return s;
5324}
5325
5326/*
5327 * move cursor to start of line
5328 * if flags & BL_WHITE move to first non-white
5329 * if flags & BL_SOL move to first non-white if startofline is set,
5330 * otherwise keep "curswant" column
5331 * if flags & BL_FIX don't leave the cursor on a NUL.
5332 */
5333 void
5334beginline(flags)
5335 int flags;
5336{
5337 if ((flags & BL_SOL) && !p_sol)
5338 coladvance(curwin->w_curswant);
5339 else
5340 {
5341 curwin->w_cursor.col = 0;
5342#ifdef FEAT_VIRTUALEDIT
5343 curwin->w_cursor.coladd = 0;
5344#endif
5345
5346 if (flags & (BL_WHITE | BL_SOL))
5347 {
5348 char_u *ptr;
5349
5350 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5351 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5352 ++curwin->w_cursor.col;
5353 }
5354 curwin->w_set_curswant = TRUE;
5355 }
5356}
5357
5358/*
5359 * oneright oneleft cursor_down cursor_up
5360 *
5361 * Move one char {right,left,down,up}.
5362 * Doesn't move onto the NUL past the end of the line.
5363 * Return OK when successful, FAIL when we hit a line of file boundary.
5364 */
5365
5366 int
5367oneright()
5368{
5369 char_u *ptr;
5370#ifdef FEAT_MBYTE
5371 int l;
5372#endif
5373
5374#ifdef FEAT_VIRTUALEDIT
5375 if (virtual_active())
5376 {
5377 pos_T prevpos = curwin->w_cursor;
5378
5379 /* Adjust for multi-wide char (excluding TAB) */
5380 ptr = ml_get_cursor();
5381 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5382#ifdef FEAT_MBYTE
5383 (*mb_ptr2char)(ptr)
5384#else
5385 *ptr
5386#endif
5387 ))
5388 ? ptr2cells(ptr) : 1));
5389 curwin->w_set_curswant = TRUE;
5390 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5391 return (prevpos.col != curwin->w_cursor.col
5392 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5393 }
5394#endif
5395
5396 ptr = ml_get_cursor();
5397#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005398 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 {
5400 /* The character under the cursor is a multi-byte character, move
5401 * several bytes right, but don't end up on the NUL. */
5402 if (ptr[l] == NUL)
5403 return FAIL;
5404 curwin->w_cursor.col += l;
5405 }
5406 else
5407#endif
5408 {
5409 if (*ptr++ == NUL || *ptr == NUL)
5410 return FAIL;
5411 ++curwin->w_cursor.col;
5412 }
5413
5414 curwin->w_set_curswant = TRUE;
5415 return OK;
5416}
5417
5418 int
5419oneleft()
5420{
5421#ifdef FEAT_VIRTUALEDIT
5422 if (virtual_active())
5423 {
5424 int width;
5425 int v = getviscol();
5426
5427 if (v == 0)
5428 return FAIL;
5429
5430# ifdef FEAT_LINEBREAK
5431 /* We might get stuck on 'showbreak', skip over it. */
5432 width = 1;
5433 for (;;)
5434 {
5435 coladvance(v - width);
5436 /* getviscol() is slow, skip it when 'showbreak' is empty and
5437 * there are no multi-byte characters */
5438 if ((*p_sbr == NUL
5439# ifdef FEAT_MBYTE
5440 && !has_mbyte
5441# endif
5442 ) || getviscol() < v)
5443 break;
5444 ++width;
5445 }
5446# else
5447 coladvance(v - 1);
5448# endif
5449
5450 if (curwin->w_cursor.coladd == 1)
5451 {
5452 char_u *ptr;
5453
5454 /* Adjust for multi-wide char (not a TAB) */
5455 ptr = ml_get_cursor();
5456 if (*ptr != TAB && vim_isprintc(
5457# ifdef FEAT_MBYTE
5458 (*mb_ptr2char)(ptr)
5459# else
5460 *ptr
5461# endif
5462 ) && ptr2cells(ptr) > 1)
5463 curwin->w_cursor.coladd = 0;
5464 }
5465
5466 curwin->w_set_curswant = TRUE;
5467 return OK;
5468 }
5469#endif
5470
5471 if (curwin->w_cursor.col == 0)
5472 return FAIL;
5473
5474 curwin->w_set_curswant = TRUE;
5475 --curwin->w_cursor.col;
5476
5477#ifdef FEAT_MBYTE
5478 /* if the character on the left of the current cursor is a multi-byte
5479 * character, move to its first byte */
5480 if (has_mbyte)
5481 mb_adjust_cursor();
5482#endif
5483 return OK;
5484}
5485
5486 int
5487cursor_up(n, upd_topline)
5488 long n;
5489 int upd_topline; /* When TRUE: update topline */
5490{
5491 linenr_T lnum;
5492
5493 if (n > 0)
5494 {
5495 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005496 /* This fails if the cursor is already in the first line or the count
5497 * is larger than the line number and '-' is in 'cpoptions' */
5498 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 return FAIL;
5500 if (n >= lnum)
5501 lnum = 1;
5502 else
5503#ifdef FEAT_FOLDING
5504 if (hasAnyFolding(curwin))
5505 {
5506 /*
5507 * Count each sequence of folded lines as one logical line.
5508 */
5509 /* go to the the start of the current fold */
5510 (void)hasFolding(lnum, &lnum, NULL);
5511
5512 while (n--)
5513 {
5514 /* move up one line */
5515 --lnum;
5516 if (lnum <= 1)
5517 break;
5518 /* If we entered a fold, move to the beginning, unless in
5519 * Insert mode or when 'foldopen' contains "all": it will open
5520 * in a moment. */
5521 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
5522 (void)hasFolding(lnum, &lnum, NULL);
5523 }
5524 if (lnum < 1)
5525 lnum = 1;
5526 }
5527 else
5528#endif
5529 lnum -= n;
5530 curwin->w_cursor.lnum = lnum;
5531 }
5532
5533 /* try to advance to the column we want to be at */
5534 coladvance(curwin->w_curswant);
5535
5536 if (upd_topline)
5537 update_topline(); /* make sure curwin->w_topline is valid */
5538
5539 return OK;
5540}
5541
5542/*
5543 * Cursor down a number of logical lines.
5544 */
5545 int
5546cursor_down(n, upd_topline)
5547 long n;
5548 int upd_topline; /* When TRUE: update topline */
5549{
5550 linenr_T lnum;
5551
5552 if (n > 0)
5553 {
5554 lnum = curwin->w_cursor.lnum;
5555#ifdef FEAT_FOLDING
5556 /* Move to last line of fold, will fail if it's the end-of-file. */
5557 (void)hasFolding(lnum, NULL, &lnum);
5558#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00005559 /* This fails if the cursor is already in the last line or would move
5560 * beyound the last line and '-' is in 'cpoptions' */
5561 if (lnum >= curbuf->b_ml.ml_line_count
5562 || (lnum + n > curbuf->b_ml.ml_line_count
5563 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 return FAIL;
5565 if (lnum + n >= curbuf->b_ml.ml_line_count)
5566 lnum = curbuf->b_ml.ml_line_count;
5567 else
5568#ifdef FEAT_FOLDING
5569 if (hasAnyFolding(curwin))
5570 {
5571 linenr_T last;
5572
5573 /* count each sequence of folded lines as one logical line */
5574 while (n--)
5575 {
5576 if (hasFolding(lnum, NULL, &last))
5577 lnum = last + 1;
5578 else
5579 ++lnum;
5580 if (lnum >= curbuf->b_ml.ml_line_count)
5581 break;
5582 }
5583 if (lnum > curbuf->b_ml.ml_line_count)
5584 lnum = curbuf->b_ml.ml_line_count;
5585 }
5586 else
5587#endif
5588 lnum += n;
5589 curwin->w_cursor.lnum = lnum;
5590 }
5591
5592 /* try to advance to the column we want to be at */
5593 coladvance(curwin->w_curswant);
5594
5595 if (upd_topline)
5596 update_topline(); /* make sure curwin->w_topline is valid */
5597
5598 return OK;
5599}
5600
5601/*
5602 * Stuff the last inserted text in the read buffer.
5603 * Last_insert actually is a copy of the redo buffer, so we
5604 * first have to remove the command.
5605 */
5606 int
5607stuff_inserted(c, count, no_esc)
5608 int c; /* Command character to be inserted */
5609 long count; /* Repeat this many times */
5610 int no_esc; /* Don't add an ESC at the end */
5611{
5612 char_u *esc_ptr;
5613 char_u *ptr;
5614 char_u *last_ptr;
5615 char_u last = NUL;
5616
5617 ptr = get_last_insert();
5618 if (ptr == NULL)
5619 {
5620 EMSG(_(e_noinstext));
5621 return FAIL;
5622 }
5623
5624 /* may want to stuff the command character, to start Insert mode */
5625 if (c != NUL)
5626 stuffcharReadbuff(c);
5627 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
5628 *esc_ptr = NUL; /* remove the ESC */
5629
5630 /* when the last char is either "0" or "^" it will be quoted if no ESC
5631 * comes after it OR if it will inserted more than once and "ptr"
5632 * starts with ^D. -- Acevedo
5633 */
5634 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
5635 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
5636 && (no_esc || (*ptr == Ctrl_D && count > 1)))
5637 {
5638 last = *last_ptr;
5639 *last_ptr = NUL;
5640 }
5641
5642 do
5643 {
5644 stuffReadbuff(ptr);
5645 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
5646 if (last)
5647 stuffReadbuff((char_u *)(last == '0'
5648 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
5649 : IF_EB("\026^", CTRL_V_STR "^")));
5650 }
5651 while (--count > 0);
5652
5653 if (last)
5654 *last_ptr = last;
5655
5656 if (esc_ptr != NULL)
5657 *esc_ptr = ESC; /* put the ESC back */
5658
5659 /* may want to stuff a trailing ESC, to get out of Insert mode */
5660 if (!no_esc)
5661 stuffcharReadbuff(ESC);
5662
5663 return OK;
5664}
5665
5666 char_u *
5667get_last_insert()
5668{
5669 if (last_insert == NULL)
5670 return NULL;
5671 return last_insert + last_insert_skip;
5672}
5673
5674/*
5675 * Get last inserted string, and remove trailing <Esc>.
5676 * Returns pointer to allocated memory (must be freed) or NULL.
5677 */
5678 char_u *
5679get_last_insert_save()
5680{
5681 char_u *s;
5682 int len;
5683
5684 if (last_insert == NULL)
5685 return NULL;
5686 s = vim_strsave(last_insert + last_insert_skip);
5687 if (s != NULL)
5688 {
5689 len = (int)STRLEN(s);
5690 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
5691 s[len - 1] = NUL;
5692 }
5693 return s;
5694}
5695
5696/*
5697 * Check the word in front of the cursor for an abbreviation.
5698 * Called when the non-id character "c" has been entered.
5699 * When an abbreviation is recognized it is removed from the text and
5700 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
5701 */
5702 static int
5703echeck_abbr(c)
5704 int c;
5705{
5706 /* Don't check for abbreviation in paste mode, when disabled and just
5707 * after moving around with cursor keys. */
5708 if (p_paste || no_abbr || arrow_used)
5709 return FALSE;
5710
5711 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
5712 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
5713}
5714
5715/*
5716 * replace-stack functions
5717 *
5718 * When replacing characters, the replaced characters are remembered for each
5719 * new character. This is used to re-insert the old text when backspacing.
5720 *
5721 * There is a NUL headed list of characters for each character that is
5722 * currently in the file after the insertion point. When BS is used, one NUL
5723 * headed list is put back for the deleted character.
5724 *
5725 * For a newline, there are two NUL headed lists. One contains the characters
5726 * that the NL replaced. The extra one stores the characters after the cursor
5727 * that were deleted (always white space).
5728 *
5729 * Replace_offset is normally 0, in which case replace_push will add a new
5730 * character at the end of the stack. If replace_offset is not 0, that many
5731 * characters will be left on the stack above the newly inserted character.
5732 */
5733
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00005734static char_u *replace_stack = NULL;
5735static long replace_stack_nr = 0; /* next entry in replace stack */
5736static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737
5738 void
5739replace_push(c)
5740 int c; /* character that is replaced (NUL is none) */
5741{
5742 char_u *p;
5743
5744 if (replace_stack_nr < replace_offset) /* nothing to do */
5745 return;
5746 if (replace_stack_len <= replace_stack_nr)
5747 {
5748 replace_stack_len += 50;
5749 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
5750 if (p == NULL) /* out of memory */
5751 {
5752 replace_stack_len -= 50;
5753 return;
5754 }
5755 if (replace_stack != NULL)
5756 {
5757 mch_memmove(p, replace_stack,
5758 (size_t)(replace_stack_nr * sizeof(char_u)));
5759 vim_free(replace_stack);
5760 }
5761 replace_stack = p;
5762 }
5763 p = replace_stack + replace_stack_nr - replace_offset;
5764 if (replace_offset)
5765 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
5766 *p = c;
5767 ++replace_stack_nr;
5768}
5769
5770/*
5771 * call replace_push(c) with replace_offset set to the first NUL.
5772 */
5773 static void
5774replace_push_off(c)
5775 int c;
5776{
5777 char_u *p;
5778
5779 p = replace_stack + replace_stack_nr;
5780 for (replace_offset = 1; replace_offset < replace_stack_nr;
5781 ++replace_offset)
5782 if (*--p == NUL)
5783 break;
5784 replace_push(c);
5785 replace_offset = 0;
5786}
5787
5788/*
5789 * Pop one item from the replace stack.
5790 * return -1 if stack empty
5791 * return replaced character or NUL otherwise
5792 */
5793 static int
5794replace_pop()
5795{
5796 if (replace_stack_nr == 0)
5797 return -1;
5798 return (int)replace_stack[--replace_stack_nr];
5799}
5800
5801/*
5802 * Join the top two items on the replace stack. This removes to "off"'th NUL
5803 * encountered.
5804 */
5805 static void
5806replace_join(off)
5807 int off; /* offset for which NUL to remove */
5808{
5809 int i;
5810
5811 for (i = replace_stack_nr; --i >= 0; )
5812 if (replace_stack[i] == NUL && off-- <= 0)
5813 {
5814 --replace_stack_nr;
5815 mch_memmove(replace_stack + i, replace_stack + i + 1,
5816 (size_t)(replace_stack_nr - i));
5817 return;
5818 }
5819}
5820
5821/*
5822 * Pop bytes from the replace stack until a NUL is found, and insert them
5823 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
5824 */
5825 static void
5826replace_pop_ins()
5827{
5828 int cc;
5829 int oldState = State;
5830
5831 State = NORMAL; /* don't want REPLACE here */
5832 while ((cc = replace_pop()) > 0)
5833 {
5834#ifdef FEAT_MBYTE
5835 mb_replace_pop_ins(cc);
5836#else
5837 ins_char(cc);
5838#endif
5839 dec_cursor();
5840 }
5841 State = oldState;
5842}
5843
5844#ifdef FEAT_MBYTE
5845/*
5846 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
5847 * indicates a multi-byte char, pop the other bytes too.
5848 */
5849 static void
5850mb_replace_pop_ins(cc)
5851 int cc;
5852{
5853 int n;
5854 char_u buf[MB_MAXBYTES];
5855 int i;
5856 int c;
5857
5858 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
5859 {
5860 buf[0] = cc;
5861 for (i = 1; i < n; ++i)
5862 buf[i] = replace_pop();
5863 ins_bytes_len(buf, n);
5864 }
5865 else
5866 ins_char(cc);
5867
5868 if (enc_utf8)
5869 /* Handle composing chars. */
5870 for (;;)
5871 {
5872 c = replace_pop();
5873 if (c == -1) /* stack empty */
5874 break;
5875 if ((n = MB_BYTE2LEN(c)) == 1)
5876 {
5877 /* Not a multi-byte char, put it back. */
5878 replace_push(c);
5879 break;
5880 }
5881 else
5882 {
5883 buf[0] = c;
5884 for (i = 1; i < n; ++i)
5885 buf[i] = replace_pop();
5886 if (utf_iscomposing(utf_ptr2char(buf)))
5887 ins_bytes_len(buf, n);
5888 else
5889 {
5890 /* Not a composing char, put it back. */
5891 for (i = n - 1; i >= 0; --i)
5892 replace_push(buf[i]);
5893 break;
5894 }
5895 }
5896 }
5897}
5898#endif
5899
5900/*
5901 * make the replace stack empty
5902 * (called when exiting replace mode)
5903 */
5904 static void
5905replace_flush()
5906{
5907 vim_free(replace_stack);
5908 replace_stack = NULL;
5909 replace_stack_len = 0;
5910 replace_stack_nr = 0;
5911}
5912
5913/*
5914 * Handle doing a BS for one character.
5915 * cc < 0: replace stack empty, just move cursor
5916 * cc == 0: character was inserted, delete it
5917 * cc > 0: character was replaced, put cc (first byte of original char) back
5918 * and check for more characters to be put back
5919 */
5920 static void
5921replace_do_bs()
5922{
5923 int cc;
5924#ifdef FEAT_VREPLACE
5925 int orig_len = 0;
5926 int ins_len;
5927 int orig_vcols = 0;
5928 colnr_T start_vcol;
5929 char_u *p;
5930 int i;
5931 int vcol;
5932#endif
5933
5934 cc = replace_pop();
5935 if (cc > 0)
5936 {
5937#ifdef FEAT_VREPLACE
5938 if (State & VREPLACE_FLAG)
5939 {
5940 /* Get the number of screen cells used by the character we are
5941 * going to delete. */
5942 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
5943 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
5944 }
5945#endif
5946#ifdef FEAT_MBYTE
5947 if (has_mbyte)
5948 {
5949 del_char(FALSE);
5950# ifdef FEAT_VREPLACE
5951 if (State & VREPLACE_FLAG)
5952 orig_len = STRLEN(ml_get_cursor());
5953# endif
5954 replace_push(cc);
5955 }
5956 else
5957#endif
5958 {
5959 pchar_cursor(cc);
5960#ifdef FEAT_VREPLACE
5961 if (State & VREPLACE_FLAG)
5962 orig_len = STRLEN(ml_get_cursor()) - 1;
5963#endif
5964 }
5965 replace_pop_ins();
5966
5967#ifdef FEAT_VREPLACE
5968 if (State & VREPLACE_FLAG)
5969 {
5970 /* Get the number of screen cells used by the inserted characters */
5971 p = ml_get_cursor();
5972 ins_len = STRLEN(p) - orig_len;
5973 vcol = start_vcol;
5974 for (i = 0; i < ins_len; ++i)
5975 {
5976 vcol += chartabsize(p + i, vcol);
5977#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005978 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979#endif
5980 }
5981 vcol -= start_vcol;
5982
5983 /* Delete spaces that were inserted after the cursor to keep the
5984 * text aligned. */
5985 curwin->w_cursor.col += ins_len;
5986 while (vcol > orig_vcols && gchar_cursor() == ' ')
5987 {
5988 del_char(FALSE);
5989 ++orig_vcols;
5990 }
5991 curwin->w_cursor.col -= ins_len;
5992 }
5993#endif
5994
5995 /* mark the buffer as changed and prepare for displaying */
5996 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
5997 }
5998 else if (cc == 0)
5999 (void)del_char(FALSE);
6000}
6001
6002#ifdef FEAT_CINDENT
6003/*
6004 * Return TRUE if C-indenting is on.
6005 */
6006 static int
6007cindent_on()
6008{
6009 return (!p_paste && (curbuf->b_p_cin
6010# ifdef FEAT_EVAL
6011 || *curbuf->b_p_inde != NUL
6012# endif
6013 ));
6014}
6015#endif
6016
6017#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6018/*
6019 * Re-indent the current line, based on the current contents of it and the
6020 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6021 * confused what all the part that handles Control-T is doing that I'm not.
6022 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6023 */
6024
6025 void
6026fixthisline(get_the_indent)
6027 int (*get_the_indent) __ARGS((void));
6028{
6029 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6030 if (linewhite(curwin->w_cursor.lnum))
6031 did_ai = TRUE; /* delete the indent if the line stays empty */
6032}
6033
6034 void
6035fix_indent()
6036{
6037 if (p_paste)
6038 return;
6039# ifdef FEAT_LISP
6040 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6041 fixthisline(get_lisp_indent);
6042# endif
6043# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6044 else
6045# endif
6046# ifdef FEAT_CINDENT
6047 if (cindent_on())
6048 do_c_expr_indent();
6049# endif
6050}
6051
6052#endif
6053
6054#ifdef FEAT_CINDENT
6055/*
6056 * return TRUE if 'cinkeys' contains the key "keytyped",
6057 * when == '*': Only if key is preceded with '*' (indent before insert)
6058 * when == '!': Only if key is prededed with '!' (don't insert)
6059 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6060 *
6061 * "keytyped" can have a few special values:
6062 * KEY_OPEN_FORW
6063 * KEY_OPEN_BACK
6064 * KEY_COMPLETE just finished completion.
6065 *
6066 * If line_is_empty is TRUE accept keys with '0' before them.
6067 */
6068 int
6069in_cinkeys(keytyped, when, line_is_empty)
6070 int keytyped;
6071 int when;
6072 int line_is_empty;
6073{
6074 char_u *look;
6075 int try_match;
6076 int try_match_word;
6077 char_u *p;
6078 char_u *line;
6079 int icase;
6080 int i;
6081
6082#ifdef FEAT_EVAL
6083 if (*curbuf->b_p_inde != NUL)
6084 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6085 else
6086#endif
6087 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6088 while (*look)
6089 {
6090 /*
6091 * Find out if we want to try a match with this key, depending on
6092 * 'when' and a '*' or '!' before the key.
6093 */
6094 switch (when)
6095 {
6096 case '*': try_match = (*look == '*'); break;
6097 case '!': try_match = (*look == '!'); break;
6098 default: try_match = (*look != '*'); break;
6099 }
6100 if (*look == '*' || *look == '!')
6101 ++look;
6102
6103 /*
6104 * If there is a '0', only accept a match if the line is empty.
6105 * But may still match when typing last char of a word.
6106 */
6107 if (*look == '0')
6108 {
6109 try_match_word = try_match;
6110 if (!line_is_empty)
6111 try_match = FALSE;
6112 ++look;
6113 }
6114 else
6115 try_match_word = FALSE;
6116
6117 /*
6118 * does it look like a control character?
6119 */
6120 if (*look == '^'
6121#ifdef EBCDIC
6122 && (Ctrl_chr(look[1]) != 0)
6123#else
6124 && look[1] >= '?' && look[1] <= '_'
6125#endif
6126 )
6127 {
6128 if (try_match && keytyped == Ctrl_chr(look[1]))
6129 return TRUE;
6130 look += 2;
6131 }
6132 /*
6133 * 'o' means "o" command, open forward.
6134 * 'O' means "O" command, open backward.
6135 */
6136 else if (*look == 'o')
6137 {
6138 if (try_match && keytyped == KEY_OPEN_FORW)
6139 return TRUE;
6140 ++look;
6141 }
6142 else if (*look == 'O')
6143 {
6144 if (try_match && keytyped == KEY_OPEN_BACK)
6145 return TRUE;
6146 ++look;
6147 }
6148
6149 /*
6150 * 'e' means to check for "else" at start of line and just before the
6151 * cursor.
6152 */
6153 else if (*look == 'e')
6154 {
6155 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6156 {
6157 p = ml_get_curline();
6158 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6159 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6160 return TRUE;
6161 }
6162 ++look;
6163 }
6164
6165 /*
6166 * ':' only causes an indent if it is at the end of a label or case
6167 * statement, or when it was before typing the ':' (to fix
6168 * class::method for C++).
6169 */
6170 else if (*look == ':')
6171 {
6172 if (try_match && keytyped == ':')
6173 {
6174 p = ml_get_curline();
6175 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6176 return TRUE;
6177 if (curwin->w_cursor.col > 2
6178 && p[curwin->w_cursor.col - 1] == ':'
6179 && p[curwin->w_cursor.col - 2] == ':')
6180 {
6181 p[curwin->w_cursor.col - 1] = ' ';
6182 i = (cin_iscase(p) || cin_isscopedecl(p)
6183 || cin_islabel(30));
6184 p = ml_get_curline();
6185 p[curwin->w_cursor.col - 1] = ':';
6186 if (i)
6187 return TRUE;
6188 }
6189 }
6190 ++look;
6191 }
6192
6193
6194 /*
6195 * Is it a key in <>, maybe?
6196 */
6197 else if (*look == '<')
6198 {
6199 if (try_match)
6200 {
6201 /*
6202 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6203 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6204 * >, *, : and ! keys if they really really want to.
6205 */
6206 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6207 && keytyped == look[1])
6208 return TRUE;
6209
6210 if (keytyped == get_special_key_code(look + 1))
6211 return TRUE;
6212 }
6213 while (*look && *look != '>')
6214 look++;
6215 while (*look == '>')
6216 look++;
6217 }
6218
6219 /*
6220 * Is it a word: "=word"?
6221 */
6222 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6223 {
6224 ++look;
6225 if (*look == '~')
6226 {
6227 icase = TRUE;
6228 ++look;
6229 }
6230 else
6231 icase = FALSE;
6232 p = vim_strchr(look, ',');
6233 if (p == NULL)
6234 p = look + STRLEN(look);
6235 if ((try_match || try_match_word)
6236 && curwin->w_cursor.col >= (colnr_T)(p - look))
6237 {
6238 int match = FALSE;
6239
6240#ifdef FEAT_INS_EXPAND
6241 if (keytyped == KEY_COMPLETE)
6242 {
6243 char_u *s;
6244
6245 /* Just completed a word, check if it starts with "look".
6246 * search back for the start of a word. */
6247 line = ml_get_curline();
6248# ifdef FEAT_MBYTE
6249 if (has_mbyte)
6250 {
6251 char_u *n;
6252
6253 for (s = line + curwin->w_cursor.col; s > line; s = n)
6254 {
6255 n = mb_prevptr(line, s);
6256 if (!vim_iswordp(n))
6257 break;
6258 }
6259 }
6260 else
6261# endif
6262 for (s = line + curwin->w_cursor.col; s > line; --s)
6263 if (!vim_iswordc(s[-1]))
6264 break;
6265 if (s + (p - look) <= line + curwin->w_cursor.col
6266 && (icase
6267 ? MB_STRNICMP(s, look, p - look)
6268 : STRNCMP(s, look, p - look)) == 0)
6269 match = TRUE;
6270 }
6271 else
6272#endif
6273 /* TODO: multi-byte */
6274 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6275 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6276 {
6277 line = ml_get_cursor();
6278 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6279 || !vim_iswordc(line[-(p - look) - 1]))
6280 && (icase
6281 ? MB_STRNICMP(line - (p - look), look, p - look)
6282 : STRNCMP(line - (p - look), look, p - look))
6283 == 0)
6284 match = TRUE;
6285 }
6286 if (match && try_match_word && !try_match)
6287 {
6288 /* "0=word": Check if there are only blanks before the
6289 * word. */
6290 line = ml_get_curline();
6291 if ((int)(skipwhite(line) - line) !=
6292 (int)(curwin->w_cursor.col - (p - look)))
6293 match = FALSE;
6294 }
6295 if (match)
6296 return TRUE;
6297 }
6298 look = p;
6299 }
6300
6301 /*
6302 * ok, it's a boring generic character.
6303 */
6304 else
6305 {
6306 if (try_match && *look == keytyped)
6307 return TRUE;
6308 ++look;
6309 }
6310
6311 /*
6312 * Skip over ", ".
6313 */
6314 look = skip_to_option_part(look);
6315 }
6316 return FALSE;
6317}
6318#endif /* FEAT_CINDENT */
6319
6320#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6321/*
6322 * Map Hebrew keyboard when in hkmap mode.
6323 */
6324 int
6325hkmap(c)
6326 int c;
6327{
6328 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6329 {
6330 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6331 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6332 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6333 static char_u map[26] =
6334 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6335 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6336 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6337 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6338 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6339 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6340 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6341 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6342 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6343
6344 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6345 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6346 /* '-1'='sofit' */
6347 else if (c == 'x')
6348 return 'X';
6349 else if (c == 'q')
6350 return '\''; /* {geresh}={'} */
6351 else if (c == 246)
6352 return ' '; /* \"o --> ' ' for a german keyboard */
6353 else if (c == 228)
6354 return ' '; /* \"a --> ' ' -- / -- */
6355 else if (c == 252)
6356 return ' '; /* \"u --> ' ' -- / -- */
6357#ifdef EBCDIC
6358 else if (islower(c))
6359#else
6360 /* NOTE: islower() does not do the right thing for us on Linux so we
6361 * do this the same was as 5.7 and previous, so it works correctly on
6362 * all systems. Specifically, the e.g. Delete and Arrow keys are
6363 * munged and won't work if e.g. searching for Hebrew text.
6364 */
6365 else if (c >= 'a' && c <= 'z')
6366#endif
6367 return (int)(map[CharOrdLow(c)] + p_aleph);
6368 else
6369 return c;
6370 }
6371 else
6372 {
6373 switch (c)
6374 {
6375 case '`': return ';';
6376 case '/': return '.';
6377 case '\'': return ',';
6378 case 'q': return '/';
6379 case 'w': return '\'';
6380
6381 /* Hebrew letters - set offset from 'a' */
6382 case ',': c = '{'; break;
6383 case '.': c = 'v'; break;
6384 case ';': c = 't'; break;
6385 default: {
6386 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6387
6388#ifdef EBCDIC
6389 /* see note about islower() above */
6390 if (!islower(c))
6391#else
6392 if (c < 'a' || c > 'z')
6393#endif
6394 return c;
6395 c = str[CharOrdLow(c)];
6396 break;
6397 }
6398 }
6399
6400 return (int)(CharOrdLow(c) + p_aleph);
6401 }
6402}
6403#endif
6404
6405 static void
6406ins_reg()
6407{
6408 int need_redraw = FALSE;
6409 int regname;
6410 int literally = 0;
6411
6412 /*
6413 * If we are going to wait for a character, show a '"'.
6414 */
6415 pc_status = PC_STATUS_UNSET;
6416 if (redrawing() && !char_avail())
6417 {
6418 /* may need to redraw when no more chars available now */
6419 ins_redraw();
6420
6421 edit_putchar('"', TRUE);
6422#ifdef FEAT_CMDL_INFO
6423 add_to_showcmd_c(Ctrl_R);
6424#endif
6425 }
6426
6427#ifdef USE_ON_FLY_SCROLL
6428 dont_scroll = TRUE; /* disallow scrolling here */
6429#endif
6430
6431 /*
6432 * Don't map the register name. This also prevents the mode message to be
6433 * deleted when ESC is hit.
6434 */
6435 ++no_mapping;
6436 regname = safe_vgetc();
6437#ifdef FEAT_LANGMAP
6438 LANGMAP_ADJUST(regname, TRUE);
6439#endif
6440 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6441 {
6442 /* Get a third key for literal register insertion */
6443 literally = regname;
6444#ifdef FEAT_CMDL_INFO
6445 add_to_showcmd_c(literally);
6446#endif
6447 regname = safe_vgetc();
6448#ifdef FEAT_LANGMAP
6449 LANGMAP_ADJUST(regname, TRUE);
6450#endif
6451 }
6452 --no_mapping;
6453
6454#ifdef FEAT_EVAL
6455 /*
6456 * Don't call u_sync() while getting the expression,
6457 * evaluating it or giving an error message for it!
6458 */
6459 ++no_u_sync;
6460 if (regname == '=')
6461 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006462# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006464# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006466# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 /* Restore the Input Method. */
6468 if (im_on)
6469 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006470# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006472 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6473 {
6474 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 else
6478 {
6479#endif
6480 if (literally == Ctrl_O || literally == Ctrl_P)
6481 {
6482 /* Append the command to the redo buffer. */
6483 AppendCharToRedobuff(Ctrl_R);
6484 AppendCharToRedobuff(literally);
6485 AppendCharToRedobuff(regname);
6486
6487 do_put(regname, BACKWARD, 1L,
6488 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6489 }
6490 else if (insert_reg(regname, literally) == FAIL)
6491 {
6492 vim_beep();
6493 need_redraw = TRUE; /* remove the '"' */
6494 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006495 else if (stop_insert_mode)
6496 /* When the '=' register was used and a function was invoked that
6497 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
6498 * insert anything, need to remove the '"' */
6499 need_redraw = TRUE;
6500
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501#ifdef FEAT_EVAL
6502 }
6503 --no_u_sync;
6504#endif
6505#ifdef FEAT_CMDL_INFO
6506 clear_showcmd();
6507#endif
6508
6509 /* If the inserted register is empty, we need to remove the '"' */
6510 if (need_redraw || stuff_empty())
6511 edit_unputchar();
6512}
6513
6514/*
6515 * CTRL-G commands in Insert mode.
6516 */
6517 static void
6518ins_ctrl_g()
6519{
6520 int c;
6521
6522#ifdef FEAT_INS_EXPAND
6523 /* Right after CTRL-X the cursor will be after the ruler. */
6524 setcursor();
6525#endif
6526
6527 /*
6528 * Don't map the second key. This also prevents the mode message to be
6529 * deleted when ESC is hit.
6530 */
6531 ++no_mapping;
6532 c = safe_vgetc();
6533 --no_mapping;
6534 switch (c)
6535 {
6536 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
6537 case K_UP:
6538 case Ctrl_K:
6539 case 'k': ins_up(TRUE);
6540 break;
6541
6542 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
6543 case K_DOWN:
6544 case Ctrl_J:
6545 case 'j': ins_down(TRUE);
6546 break;
6547
6548 /* CTRL-G u: start new undoable edit */
6549 case 'u': u_sync();
6550 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006551
6552 /* Need to reset Insstart, esp. because a BS that joins
6553 * aline to the previous one must save for undo. */
6554 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 break;
6556
6557 /* Unknown CTRL-G command, reserved for future expansion. */
6558 default: vim_beep();
6559 }
6560}
6561
6562/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006563 * CTRL-^ in Insert mode.
6564 */
6565 static void
6566ins_ctrl_hat()
6567{
6568 if (map_to_exists_mode((char_u *)"", LANGMAP))
6569 {
6570 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
6571 if (State & LANGMAP)
6572 {
6573 curbuf->b_p_iminsert = B_IMODE_NONE;
6574 State &= ~LANGMAP;
6575 }
6576 else
6577 {
6578 curbuf->b_p_iminsert = B_IMODE_LMAP;
6579 State |= LANGMAP;
6580#ifdef USE_IM_CONTROL
6581 im_set_active(FALSE);
6582#endif
6583 }
6584 }
6585#ifdef USE_IM_CONTROL
6586 else
6587 {
6588 /* There are no ":lmap" mappings, toggle IM */
6589 if (im_get_status())
6590 {
6591 curbuf->b_p_iminsert = B_IMODE_NONE;
6592 im_set_active(FALSE);
6593 }
6594 else
6595 {
6596 curbuf->b_p_iminsert = B_IMODE_IM;
6597 State &= ~LANGMAP;
6598 im_set_active(TRUE);
6599 }
6600 }
6601#endif
6602 set_iminsert_global();
6603 showmode();
6604#ifdef FEAT_GUI
6605 /* may show different cursor shape or color */
6606 if (gui.in_use)
6607 gui_update_cursor(TRUE, FALSE);
6608#endif
6609#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6610 /* Show/unshow value of 'keymap' in status lines. */
6611 status_redraw_curbuf();
6612#endif
6613}
6614
6615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 * Handle ESC in insert mode.
6617 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
6618 * insert.
6619 */
6620 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00006621ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 long *count;
6623 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00006624 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 int temp;
6627 static int disabled_redraw = FALSE;
6628
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006629#ifdef FEAT_SYN_HL
6630 check_spell_redraw();
6631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632#if defined(FEAT_HANGULIN)
6633# if defined(ESC_CHG_TO_ENG_MODE)
6634 hangul_input_state_set(0);
6635# endif
6636 if (composing_hangul)
6637 {
6638 push_raw_key(composing_hangul_buffer, 2);
6639 composing_hangul = 0;
6640 }
6641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642
6643 temp = curwin->w_cursor.col;
6644 if (disabled_redraw)
6645 {
6646 --RedrawingDisabled;
6647 disabled_redraw = FALSE;
6648 }
6649 if (!arrow_used)
6650 {
6651 /*
6652 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00006653 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
6654 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 */
6656 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00006657 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658
6659 /*
6660 * Repeating insert may take a long time. Check for
6661 * interrupt now and then.
6662 */
6663 if (*count > 0)
6664 {
6665 line_breakcheck();
6666 if (got_int)
6667 *count = 0;
6668 }
6669
6670 if (--*count > 0) /* repeat what was typed */
6671 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006672 /* Vi repeats the insert without replacing characters. */
6673 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
6674 State &= ~REPLACE_FLAG;
6675
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 (void)start_redo_ins();
6677 if (cmdchar == 'r' || cmdchar == 'v')
6678 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
6679 ++RedrawingDisabled;
6680 disabled_redraw = TRUE;
6681 return FALSE; /* repeat the insert */
6682 }
6683 stop_insert(&curwin->w_cursor, TRUE);
6684 undisplay_dollar();
6685 }
6686
6687 /* When an autoindent was removed, curswant stays after the
6688 * indent */
6689 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
6690 curwin->w_set_curswant = TRUE;
6691
6692 /* Remember the last Insert position in the '^ mark. */
6693 if (!cmdmod.keepjumps)
6694 curbuf->b_last_insert = curwin->w_cursor;
6695
6696 /*
6697 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00006698 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00006700 if (!nomove
6701 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702#ifdef FEAT_VIRTUALEDIT
6703 || curwin->w_cursor.coladd > 0
6704#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006705 )
6706 && (restart_edit == NUL
6707 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00006709 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006711 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712#ifdef FEAT_RIGHTLEFT
6713 && !revins_on
6714#endif
6715 )
6716 {
6717#ifdef FEAT_VIRTUALEDIT
6718 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
6719 {
6720 oneleft();
6721 if (restart_edit != NUL)
6722 ++curwin->w_cursor.coladd;
6723 }
6724 else
6725#endif
6726 {
6727 --curwin->w_cursor.col;
6728#ifdef FEAT_MBYTE
6729 /* Correct cursor for multi-byte character. */
6730 if (has_mbyte)
6731 mb_adjust_cursor();
6732#endif
6733 }
6734 }
6735
6736#ifdef USE_IM_CONTROL
6737 /* Disable IM to allow typing English directly for Normal mode commands.
6738 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
6739 * well). */
6740 if (!(State & LANGMAP))
6741 im_save_status(&curbuf->b_p_iminsert);
6742 im_set_active(FALSE);
6743#endif
6744
6745 State = NORMAL;
6746 /* need to position cursor again (e.g. when on a TAB ) */
6747 changed_cline_bef_curs();
6748
6749#ifdef FEAT_MOUSE
6750 setmouse();
6751#endif
6752#ifdef CURSOR_SHAPE
6753 ui_cursor_shape(); /* may show different cursor shape */
6754#endif
6755
6756 /*
6757 * When recording or for CTRL-O, need to display the new mode.
6758 * Otherwise remove the mode message.
6759 */
6760 if (Recording || restart_edit != NUL)
6761 showmode();
6762 else if (p_smd)
6763 MSG("");
6764
6765 return TRUE; /* exit Insert mode */
6766}
6767
6768#ifdef FEAT_RIGHTLEFT
6769/*
6770 * Toggle language: hkmap and revins_on.
6771 * Move to end of reverse inserted text.
6772 */
6773 static void
6774ins_ctrl_()
6775{
6776 if (revins_on && revins_chars && revins_scol >= 0)
6777 {
6778 while (gchar_cursor() != NUL && revins_chars--)
6779 ++curwin->w_cursor.col;
6780 }
6781 p_ri = !p_ri;
6782 revins_on = (State == INSERT && p_ri);
6783 if (revins_on)
6784 {
6785 revins_scol = curwin->w_cursor.col;
6786 revins_legal++;
6787 revins_chars = 0;
6788 undisplay_dollar();
6789 }
6790 else
6791 revins_scol = -1;
6792#ifdef FEAT_FKMAP
6793 if (p_altkeymap)
6794 {
6795 /*
6796 * to be consistent also for redo command, using '.'
6797 * set arrow_used to true and stop it - causing to redo
6798 * characters entered in one mode (normal/reverse insert).
6799 */
6800 arrow_used = TRUE;
6801 (void)stop_arrow();
6802 p_fkmap = curwin->w_p_rl ^ p_ri;
6803 if (p_fkmap && p_ri)
6804 State = INSERT;
6805 }
6806 else
6807#endif
6808 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
6809 showmode();
6810}
6811#endif
6812
6813#ifdef FEAT_VISUAL
6814/*
6815 * If 'keymodel' contains "startsel", may start selection.
6816 * Returns TRUE when a CTRL-O and other keys stuffed.
6817 */
6818 static int
6819ins_start_select(c)
6820 int c;
6821{
6822 if (km_startsel)
6823 switch (c)
6824 {
6825 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 case K_PAGEUP:
6828 case K_KPAGEUP:
6829 case K_PAGEDOWN:
6830 case K_KPAGEDOWN:
6831# ifdef MACOS
6832 case K_LEFT:
6833 case K_RIGHT:
6834 case K_UP:
6835 case K_DOWN:
6836 case K_END:
6837 case K_HOME:
6838# endif
6839 if (!(mod_mask & MOD_MASK_SHIFT))
6840 break;
6841 /* FALLTHROUGH */
6842 case K_S_LEFT:
6843 case K_S_RIGHT:
6844 case K_S_UP:
6845 case K_S_DOWN:
6846 case K_S_END:
6847 case K_S_HOME:
6848 /* Start selection right away, the cursor can move with
6849 * CTRL-O when beyond the end of the line. */
6850 start_selection();
6851
6852 /* Execute the key in (insert) Select mode. */
6853 stuffcharReadbuff(Ctrl_O);
6854 if (mod_mask)
6855 {
6856 char_u buf[4];
6857
6858 buf[0] = K_SPECIAL;
6859 buf[1] = KS_MODIFIER;
6860 buf[2] = mod_mask;
6861 buf[3] = NUL;
6862 stuffReadbuff(buf);
6863 }
6864 stuffcharReadbuff(c);
6865 return TRUE;
6866 }
6867 return FALSE;
6868}
6869#endif
6870
6871/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006872 * <Insert> key in Insert mode: toggle insert/remplace mode.
6873 */
6874 static void
6875ins_insert(replaceState)
6876 int replaceState;
6877{
6878#ifdef FEAT_FKMAP
6879 if (p_fkmap && p_ri)
6880 {
6881 beep_flush();
6882 EMSG(farsi_text_3); /* encoded in Farsi */
6883 return;
6884 }
6885#endif
6886
6887#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00006888# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006889 set_vim_var_string(VV_INSERTMODE,
6890 (char_u *)((State & REPLACE_FLAG) ? "i" :
6891 replaceState == VREPLACE ? "v" : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00006892# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006893 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
6894#endif
6895 if (State & REPLACE_FLAG)
6896 State = INSERT | (State & LANGMAP);
6897 else
6898 State = replaceState | (State & LANGMAP);
6899 AppendCharToRedobuff(K_INS);
6900 showmode();
6901#ifdef CURSOR_SHAPE
6902 ui_cursor_shape(); /* may show different cursor shape */
6903#endif
6904}
6905
6906/*
6907 * Pressed CTRL-O in Insert mode.
6908 */
6909 static void
6910ins_ctrl_o()
6911{
6912#ifdef FEAT_VREPLACE
6913 if (State & VREPLACE_FLAG)
6914 restart_edit = 'V';
6915 else
6916#endif
6917 if (State & REPLACE_FLAG)
6918 restart_edit = 'R';
6919 else
6920 restart_edit = 'I';
6921#ifdef FEAT_VIRTUALEDIT
6922 if (virtual_active())
6923 ins_at_eol = FALSE; /* cursor always keeps its column */
6924 else
6925#endif
6926 ins_at_eol = (gchar_cursor() == NUL);
6927}
6928
6929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 * If the cursor is on an indent, ^T/^D insert/delete one
6931 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
6932 * Always round the indent to 'shiftwith', this is compatible
6933 * with vi. But vi only supports ^T and ^D after an
6934 * autoindent, we support it everywhere.
6935 */
6936 static void
6937ins_shift(c, lastc)
6938 int c;
6939 int lastc;
6940{
6941 if (stop_arrow() == FAIL)
6942 return;
6943 AppendCharToRedobuff(c);
6944
6945 /*
6946 * 0^D and ^^D: remove all indent.
6947 */
6948 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
6949 {
6950 --curwin->w_cursor.col;
6951 (void)del_char(FALSE); /* delete the '^' or '0' */
6952 /* In Replace mode, restore the characters that '^' or '0' replaced. */
6953 if (State & REPLACE_FLAG)
6954 replace_pop_ins();
6955 if (lastc == '^')
6956 old_indent = get_indent(); /* remember curr. indent */
6957 change_indent(INDENT_SET, 0, TRUE, 0);
6958 }
6959 else
6960 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
6961
6962 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
6963 did_ai = FALSE;
6964#ifdef FEAT_SMARTINDENT
6965 did_si = FALSE;
6966 can_si = FALSE;
6967 can_si_back = FALSE;
6968#endif
6969#ifdef FEAT_CINDENT
6970 can_cindent = FALSE; /* no cindenting after ^D or ^T */
6971#endif
6972}
6973
6974 static void
6975ins_del()
6976{
6977 int temp;
6978
6979 if (stop_arrow() == FAIL)
6980 return;
6981 if (gchar_cursor() == NUL) /* delete newline */
6982 {
6983 temp = curwin->w_cursor.col;
6984 if (!can_bs(BS_EOL) /* only if "eol" included */
6985 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
6986 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
6987 || do_join(FALSE) == FAIL)
6988 vim_beep();
6989 else
6990 curwin->w_cursor.col = temp;
6991 }
6992 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
6993 vim_beep();
6994 did_ai = FALSE;
6995#ifdef FEAT_SMARTINDENT
6996 did_si = FALSE;
6997 can_si = FALSE;
6998 can_si_back = FALSE;
6999#endif
7000 AppendCharToRedobuff(K_DEL);
7001}
7002
7003/*
7004 * Handle Backspace, delete-word and delete-line in Insert mode.
7005 * Return TRUE when backspace was actually used.
7006 */
7007 static int
7008ins_bs(c, mode, inserted_space_p)
7009 int c;
7010 int mode;
7011 int *inserted_space_p;
7012{
7013 linenr_T lnum;
7014 int cc;
7015 int temp = 0; /* init for GCC */
7016 colnr_T mincol;
7017 int did_backspace = FALSE;
7018 int in_indent;
7019 int oldState;
7020#ifdef FEAT_MBYTE
7021 int p1, p2;
7022#endif
7023
7024 /*
7025 * can't delete anything in an empty file
7026 * can't backup past first character in buffer
7027 * can't backup past starting point unless 'backspace' > 1
7028 * can backup to a previous line if 'backspace' == 0
7029 */
7030 if ( bufempty()
7031 || (
7032#ifdef FEAT_RIGHTLEFT
7033 !revins_on &&
7034#endif
7035 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7036 || (!can_bs(BS_START)
7037 && (arrow_used
7038 || (curwin->w_cursor.lnum == Insstart.lnum
7039 && curwin->w_cursor.col <= Insstart.col)))
7040 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7041 && curwin->w_cursor.col <= ai_col)
7042 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7043 {
7044 vim_beep();
7045 return FALSE;
7046 }
7047
7048 if (stop_arrow() == FAIL)
7049 return FALSE;
7050 in_indent = inindent(0);
7051#ifdef FEAT_CINDENT
7052 if (in_indent)
7053 can_cindent = FALSE;
7054#endif
7055#ifdef FEAT_COMMENTS
7056 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7057#endif
7058#ifdef FEAT_RIGHTLEFT
7059 if (revins_on) /* put cursor after last inserted char */
7060 inc_cursor();
7061#endif
7062
7063#ifdef FEAT_VIRTUALEDIT
7064 /* Virtualedit:
7065 * BACKSPACE_CHAR eats a virtual space
7066 * BACKSPACE_WORD eats all coladd
7067 * BACKSPACE_LINE eats all coladd and keeps going
7068 */
7069 if (curwin->w_cursor.coladd > 0)
7070 {
7071 if (mode == BACKSPACE_CHAR)
7072 {
7073 --curwin->w_cursor.coladd;
7074 return TRUE;
7075 }
7076 if (mode == BACKSPACE_WORD)
7077 {
7078 curwin->w_cursor.coladd = 0;
7079 return TRUE;
7080 }
7081 curwin->w_cursor.coladd = 0;
7082 }
7083#endif
7084
7085 /*
7086 * delete newline!
7087 */
7088 if (curwin->w_cursor.col == 0)
7089 {
7090 lnum = Insstart.lnum;
7091 if (curwin->w_cursor.lnum == Insstart.lnum
7092#ifdef FEAT_RIGHTLEFT
7093 || revins_on
7094#endif
7095 )
7096 {
7097 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7098 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7099 return FALSE;
7100 --Insstart.lnum;
7101 Insstart.col = MAXCOL;
7102 }
7103 /*
7104 * In replace mode:
7105 * cc < 0: NL was inserted, delete it
7106 * cc >= 0: NL was replaced, put original characters back
7107 */
7108 cc = -1;
7109 if (State & REPLACE_FLAG)
7110 cc = replace_pop(); /* returns -1 if NL was inserted */
7111 /*
7112 * In replace mode, in the line we started replacing, we only move the
7113 * cursor.
7114 */
7115 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7116 {
7117 dec_cursor();
7118 }
7119 else
7120 {
7121#ifdef FEAT_VREPLACE
7122 if (!(State & VREPLACE_FLAG)
7123 || curwin->w_cursor.lnum > orig_line_count)
7124#endif
7125 {
7126 temp = gchar_cursor(); /* remember current char */
7127 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007128
7129 /* When "aw" is in 'formatoptions' we must delete the space at
7130 * the end of the line, otherwise the line will be broken
7131 * again when auto-formatting. */
7132 if (has_format_option(FO_AUTO)
7133 && has_format_option(FO_WHITE_PAR))
7134 {
7135 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7136 TRUE);
7137 int len;
7138
7139 len = STRLEN(ptr);
7140 if (len > 0 && ptr[len - 1] == ' ')
7141 ptr[len - 1] = NUL;
7142 }
7143
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 (void)do_join(FALSE);
7145 if (temp == NUL && gchar_cursor() != NUL)
7146 inc_cursor();
7147 }
7148#ifdef FEAT_VREPLACE
7149 else
7150 dec_cursor();
7151#endif
7152
7153 /*
7154 * In REPLACE mode we have to put back the text that was replaced
7155 * by the NL. On the replace stack is first a NUL-terminated
7156 * sequence of characters that were deleted and then the
7157 * characters that NL replaced.
7158 */
7159 if (State & REPLACE_FLAG)
7160 {
7161 /*
7162 * Do the next ins_char() in NORMAL state, to
7163 * prevent ins_char() from replacing characters and
7164 * avoiding showmatch().
7165 */
7166 oldState = State;
7167 State = NORMAL;
7168 /*
7169 * restore characters (blanks) deleted after cursor
7170 */
7171 while (cc > 0)
7172 {
7173 temp = curwin->w_cursor.col;
7174#ifdef FEAT_MBYTE
7175 mb_replace_pop_ins(cc);
7176#else
7177 ins_char(cc);
7178#endif
7179 curwin->w_cursor.col = temp;
7180 cc = replace_pop();
7181 }
7182 /* restore the characters that NL replaced */
7183 replace_pop_ins();
7184 State = oldState;
7185 }
7186 }
7187 did_ai = FALSE;
7188 }
7189 else
7190 {
7191 /*
7192 * Delete character(s) before the cursor.
7193 */
7194#ifdef FEAT_RIGHTLEFT
7195 if (revins_on) /* put cursor on last inserted char */
7196 dec_cursor();
7197#endif
7198 mincol = 0;
7199 /* keep indent */
7200 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7201#ifdef FEAT_RIGHTLEFT
7202 && !revins_on
7203#endif
7204 )
7205 {
7206 temp = curwin->w_cursor.col;
7207 beginline(BL_WHITE);
7208 if (curwin->w_cursor.col < (colnr_T)temp)
7209 mincol = curwin->w_cursor.col;
7210 curwin->w_cursor.col = temp;
7211 }
7212
7213 /*
7214 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7215 */
7216 if ( mode == BACKSPACE_CHAR
7217 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007218 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 && (*(ml_get_cursor() - 1) == TAB
7220 || (*(ml_get_cursor() - 1) == ' '
7221 && (!*inserted_space_p
7222 || arrow_used))))))
7223 {
7224 int ts;
7225 colnr_T vcol;
7226 colnr_T want_vcol;
7227 int extra = 0;
7228
7229 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007230 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 ts = curbuf->b_p_sw;
7232 else
7233 ts = curbuf->b_p_sts;
7234 /* Compute the virtual column where we want to be. Since
7235 * 'showbreak' may get in the way, need to get the last column of
7236 * the previous character. */
7237 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7238 dec_cursor();
7239 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7240 inc_cursor();
7241 want_vcol = (want_vcol / ts) * ts;
7242
7243 /* delete characters until we are at or before want_vcol */
7244 while (vcol > want_vcol
7245 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7246 {
7247 dec_cursor();
7248 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7249 if (State & REPLACE_FLAG)
7250 {
7251 /* Don't delete characters before the insert point when in
7252 * Replace mode */
7253 if (curwin->w_cursor.lnum != Insstart.lnum
7254 || curwin->w_cursor.col >= Insstart.col)
7255 {
7256#if 0 /* what was this for? It causes problems when sw != ts. */
7257 if (State == REPLACE && (int)vcol < want_vcol)
7258 {
7259 (void)del_char(FALSE);
7260 extra = 2; /* don't pop too much */
7261 }
7262 else
7263#endif
7264 replace_do_bs();
7265 }
7266 }
7267 else
7268 (void)del_char(FALSE);
7269 }
7270
7271 /* insert extra spaces until we are at want_vcol */
7272 while (vcol < want_vcol)
7273 {
7274 /* Remember the first char we inserted */
7275 if (curwin->w_cursor.lnum == Insstart.lnum
7276 && curwin->w_cursor.col < Insstart.col)
7277 Insstart.col = curwin->w_cursor.col;
7278
7279#ifdef FEAT_VREPLACE
7280 if (State & VREPLACE_FLAG)
7281 ins_char(' ');
7282 else
7283#endif
7284 {
7285 ins_str((char_u *)" ");
7286 if ((State & REPLACE_FLAG) && extra <= 1)
7287 {
7288 if (extra)
7289 replace_push_off(NUL);
7290 else
7291 replace_push(NUL);
7292 }
7293 if (extra == 2)
7294 extra = 1;
7295 }
7296 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7297 }
7298 }
7299
7300 /*
7301 * Delete upto starting point, start of line or previous word.
7302 */
7303 else do
7304 {
7305#ifdef FEAT_RIGHTLEFT
7306 if (!revins_on) /* put cursor on char to be deleted */
7307#endif
7308 dec_cursor();
7309
7310 /* start of word? */
7311 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7312 {
7313 mode = BACKSPACE_WORD_NOT_SPACE;
7314 temp = vim_iswordc(gchar_cursor());
7315 }
7316 /* end of word? */
7317 else if (mode == BACKSPACE_WORD_NOT_SPACE
7318 && (vim_isspace(cc = gchar_cursor())
7319 || vim_iswordc(cc) != temp))
7320 {
7321#ifdef FEAT_RIGHTLEFT
7322 if (!revins_on)
7323#endif
7324 inc_cursor();
7325#ifdef FEAT_RIGHTLEFT
7326 else if (State & REPLACE_FLAG)
7327 dec_cursor();
7328#endif
7329 break;
7330 }
7331 if (State & REPLACE_FLAG)
7332 replace_do_bs();
7333 else
7334 {
7335#ifdef FEAT_MBYTE
7336 if (enc_utf8 && p_deco)
7337 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7338#endif
7339 (void)del_char(FALSE);
7340#ifdef FEAT_MBYTE
7341 /*
7342 * If p1 or p2 is non-zero, there are combining characters we
7343 * need to take account of. Don't back up before the base
7344 * character.
7345 */
7346 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7347 inc_cursor();
7348#endif
7349#ifdef FEAT_RIGHTLEFT
7350 if (revins_chars)
7351 {
7352 revins_chars--;
7353 revins_legal++;
7354 }
7355 if (revins_on && gchar_cursor() == NUL)
7356 break;
7357#endif
7358 }
7359 /* Just a single backspace?: */
7360 if (mode == BACKSPACE_CHAR)
7361 break;
7362 } while (
7363#ifdef FEAT_RIGHTLEFT
7364 revins_on ||
7365#endif
7366 (curwin->w_cursor.col > mincol
7367 && (curwin->w_cursor.lnum != Insstart.lnum
7368 || curwin->w_cursor.col != Insstart.col)));
7369 did_backspace = TRUE;
7370 }
7371#ifdef FEAT_SMARTINDENT
7372 did_si = FALSE;
7373 can_si = FALSE;
7374 can_si_back = FALSE;
7375#endif
7376 if (curwin->w_cursor.col <= 1)
7377 did_ai = FALSE;
7378 /*
7379 * It's a little strange to put backspaces into the redo
7380 * buffer, but it makes auto-indent a lot easier to deal
7381 * with.
7382 */
7383 AppendCharToRedobuff(c);
7384
7385 /* If deleted before the insertion point, adjust it */
7386 if (curwin->w_cursor.lnum == Insstart.lnum
7387 && curwin->w_cursor.col < Insstart.col)
7388 Insstart.col = curwin->w_cursor.col;
7389
7390 /* vi behaviour: the cursor moves backward but the character that
7391 * was there remains visible
7392 * Vim behaviour: the cursor moves backward and the character that
7393 * was there is erased from the screen.
7394 * We can emulate the vi behaviour by pretending there is a dollar
7395 * displayed even when there isn't.
7396 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7397 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7398 dollar_vcol = curwin->w_virtcol;
7399
7400 return did_backspace;
7401}
7402
7403#ifdef FEAT_MOUSE
7404 static void
7405ins_mouse(c)
7406 int c;
7407{
7408 pos_T tpos;
7409
7410# ifdef FEAT_GUI
7411 /* When GUI is active, also move/paste when 'mouse' is empty */
7412 if (!gui.in_use)
7413# endif
7414 if (!mouse_has(MOUSE_INSERT))
7415 return;
7416
7417 undisplay_dollar();
7418 tpos = curwin->w_cursor;
7419 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7420 {
7421 start_arrow(&tpos);
7422# ifdef FEAT_CINDENT
7423 can_cindent = TRUE;
7424# endif
7425 }
7426
7427#ifdef FEAT_WINDOWS
7428 /* redraw status lines (in case another window became active) */
7429 redraw_statuslines();
7430#endif
7431}
7432
7433 static void
7434ins_mousescroll(up)
7435 int up;
7436{
7437 pos_T tpos;
7438# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7439 win_T *old_curwin;
7440# endif
7441
7442 tpos = curwin->w_cursor;
7443
7444# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7445 old_curwin = curwin;
7446
7447 /* Currently the mouse coordinates are only known in the GUI. */
7448 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7449 {
7450 int row, col;
7451
7452 row = mouse_row;
7453 col = mouse_col;
7454
7455 /* find the window at the pointer coordinates */
7456 curwin = mouse_find_win(&row, &col);
7457 curbuf = curwin->w_buffer;
7458 }
7459 if (curwin == old_curwin)
7460# endif
7461 undisplay_dollar();
7462
7463 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7464 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7465 else
7466 scroll_redraw(up, 3L);
7467
7468# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7469 curwin->w_redr_status = TRUE;
7470
7471 curwin = old_curwin;
7472 curbuf = curwin->w_buffer;
7473# endif
7474
7475 if (!equalpos(curwin->w_cursor, tpos))
7476 {
7477 start_arrow(&tpos);
7478# ifdef FEAT_CINDENT
7479 can_cindent = TRUE;
7480# endif
7481 }
7482}
7483#endif
7484
7485#ifdef FEAT_GUI
7486 void
7487ins_scroll()
7488{
7489 pos_T tpos;
7490
7491 undisplay_dollar();
7492 tpos = curwin->w_cursor;
7493 if (gui_do_scroll())
7494 {
7495 start_arrow(&tpos);
7496# ifdef FEAT_CINDENT
7497 can_cindent = TRUE;
7498# endif
7499 }
7500}
7501
7502 void
7503ins_horscroll()
7504{
7505 pos_T tpos;
7506
7507 undisplay_dollar();
7508 tpos = curwin->w_cursor;
7509 if (gui_do_horiz_scroll())
7510 {
7511 start_arrow(&tpos);
7512# ifdef FEAT_CINDENT
7513 can_cindent = TRUE;
7514# endif
7515 }
7516}
7517#endif
7518
7519 static void
7520ins_left()
7521{
7522 pos_T tpos;
7523
7524#ifdef FEAT_FOLDING
7525 if ((fdo_flags & FDO_HOR) && KeyTyped)
7526 foldOpenCursor();
7527#endif
7528 undisplay_dollar();
7529 tpos = curwin->w_cursor;
7530 if (oneleft() == OK)
7531 {
7532 start_arrow(&tpos);
7533#ifdef FEAT_RIGHTLEFT
7534 /* If exit reversed string, position is fixed */
7535 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
7536 revins_legal++;
7537 revins_chars++;
7538#endif
7539 }
7540
7541 /*
7542 * if 'whichwrap' set for cursor in insert mode may go to
7543 * previous line
7544 */
7545 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
7546 {
7547 start_arrow(&tpos);
7548 --(curwin->w_cursor.lnum);
7549 coladvance((colnr_T)MAXCOL);
7550 curwin->w_set_curswant = TRUE; /* so we stay at the end */
7551 }
7552 else
7553 vim_beep();
7554}
7555
7556 static void
7557ins_home(c)
7558 int c;
7559{
7560 pos_T tpos;
7561
7562#ifdef FEAT_FOLDING
7563 if ((fdo_flags & FDO_HOR) && KeyTyped)
7564 foldOpenCursor();
7565#endif
7566 undisplay_dollar();
7567 tpos = curwin->w_cursor;
7568 if (c == K_C_HOME)
7569 curwin->w_cursor.lnum = 1;
7570 curwin->w_cursor.col = 0;
7571#ifdef FEAT_VIRTUALEDIT
7572 curwin->w_cursor.coladd = 0;
7573#endif
7574 curwin->w_curswant = 0;
7575 start_arrow(&tpos);
7576}
7577
7578 static void
7579ins_end(c)
7580 int c;
7581{
7582 pos_T tpos;
7583
7584#ifdef FEAT_FOLDING
7585 if ((fdo_flags & FDO_HOR) && KeyTyped)
7586 foldOpenCursor();
7587#endif
7588 undisplay_dollar();
7589 tpos = curwin->w_cursor;
7590 if (c == K_C_END)
7591 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7592 coladvance((colnr_T)MAXCOL);
7593 curwin->w_curswant = MAXCOL;
7594
7595 start_arrow(&tpos);
7596}
7597
7598 static void
7599ins_s_left()
7600{
7601#ifdef FEAT_FOLDING
7602 if ((fdo_flags & FDO_HOR) && KeyTyped)
7603 foldOpenCursor();
7604#endif
7605 undisplay_dollar();
7606 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
7607 {
7608 start_arrow(&curwin->w_cursor);
7609 (void)bck_word(1L, FALSE, FALSE);
7610 curwin->w_set_curswant = TRUE;
7611 }
7612 else
7613 vim_beep();
7614}
7615
7616 static void
7617ins_right()
7618{
7619#ifdef FEAT_FOLDING
7620 if ((fdo_flags & FDO_HOR) && KeyTyped)
7621 foldOpenCursor();
7622#endif
7623 undisplay_dollar();
7624 if (gchar_cursor() != NUL || virtual_active()
7625 )
7626 {
7627 start_arrow(&curwin->w_cursor);
7628 curwin->w_set_curswant = TRUE;
7629#ifdef FEAT_VIRTUALEDIT
7630 if (virtual_active())
7631 oneright();
7632 else
7633#endif
7634 {
7635#ifdef FEAT_MBYTE
7636 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007637 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 else
7639#endif
7640 ++curwin->w_cursor.col;
7641 }
7642
7643#ifdef FEAT_RIGHTLEFT
7644 revins_legal++;
7645 if (revins_chars)
7646 revins_chars--;
7647#endif
7648 }
7649 /* if 'whichwrap' set for cursor in insert mode, may move the
7650 * cursor to the next line */
7651 else if (vim_strchr(p_ww, ']') != NULL
7652 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7653 {
7654 start_arrow(&curwin->w_cursor);
7655 curwin->w_set_curswant = TRUE;
7656 ++curwin->w_cursor.lnum;
7657 curwin->w_cursor.col = 0;
7658 }
7659 else
7660 vim_beep();
7661}
7662
7663 static void
7664ins_s_right()
7665{
7666#ifdef FEAT_FOLDING
7667 if ((fdo_flags & FDO_HOR) && KeyTyped)
7668 foldOpenCursor();
7669#endif
7670 undisplay_dollar();
7671 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
7672 || gchar_cursor() != NUL)
7673 {
7674 start_arrow(&curwin->w_cursor);
7675 (void)fwd_word(1L, FALSE, 0);
7676 curwin->w_set_curswant = TRUE;
7677 }
7678 else
7679 vim_beep();
7680}
7681
7682 static void
7683ins_up(startcol)
7684 int startcol; /* when TRUE move to Insstart.col */
7685{
7686 pos_T tpos;
7687 linenr_T old_topline = curwin->w_topline;
7688#ifdef FEAT_DIFF
7689 int old_topfill = curwin->w_topfill;
7690#endif
7691
7692 undisplay_dollar();
7693 tpos = curwin->w_cursor;
7694 if (cursor_up(1L, TRUE) == OK)
7695 {
7696 if (startcol)
7697 coladvance(getvcol_nolist(&Insstart));
7698 if (old_topline != curwin->w_topline
7699#ifdef FEAT_DIFF
7700 || old_topfill != curwin->w_topfill
7701#endif
7702 )
7703 redraw_later(VALID);
7704 start_arrow(&tpos);
7705#ifdef FEAT_CINDENT
7706 can_cindent = TRUE;
7707#endif
7708 }
7709 else
7710 vim_beep();
7711}
7712
7713 static void
7714ins_pageup()
7715{
7716 pos_T tpos;
7717
7718 undisplay_dollar();
7719 tpos = curwin->w_cursor;
7720 if (onepage(BACKWARD, 1L) == OK)
7721 {
7722 start_arrow(&tpos);
7723#ifdef FEAT_CINDENT
7724 can_cindent = TRUE;
7725#endif
7726 }
7727 else
7728 vim_beep();
7729}
7730
7731 static void
7732ins_down(startcol)
7733 int startcol; /* when TRUE move to Insstart.col */
7734{
7735 pos_T tpos;
7736 linenr_T old_topline = curwin->w_topline;
7737#ifdef FEAT_DIFF
7738 int old_topfill = curwin->w_topfill;
7739#endif
7740
7741 undisplay_dollar();
7742 tpos = curwin->w_cursor;
7743 if (cursor_down(1L, TRUE) == OK)
7744 {
7745 if (startcol)
7746 coladvance(getvcol_nolist(&Insstart));
7747 if (old_topline != curwin->w_topline
7748#ifdef FEAT_DIFF
7749 || old_topfill != curwin->w_topfill
7750#endif
7751 )
7752 redraw_later(VALID);
7753 start_arrow(&tpos);
7754#ifdef FEAT_CINDENT
7755 can_cindent = TRUE;
7756#endif
7757 }
7758 else
7759 vim_beep();
7760}
7761
7762 static void
7763ins_pagedown()
7764{
7765 pos_T tpos;
7766
7767 undisplay_dollar();
7768 tpos = curwin->w_cursor;
7769 if (onepage(FORWARD, 1L) == OK)
7770 {
7771 start_arrow(&tpos);
7772#ifdef FEAT_CINDENT
7773 can_cindent = TRUE;
7774#endif
7775 }
7776 else
7777 vim_beep();
7778}
7779
7780#ifdef FEAT_DND
7781 static void
7782ins_drop()
7783{
7784 do_put('~', BACKWARD, 1L, PUT_CURSEND);
7785}
7786#endif
7787
7788/*
7789 * Handle TAB in Insert or Replace mode.
7790 * Return TRUE when the TAB needs to be inserted like a normal character.
7791 */
7792 static int
7793ins_tab()
7794{
7795 int ind;
7796 int i;
7797 int temp;
7798
7799 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
7800 Insstart_blank_vcol = get_nolist_virtcol();
7801 if (echeck_abbr(TAB + ABBR_OFF))
7802 return FALSE;
7803
7804 ind = inindent(0);
7805#ifdef FEAT_CINDENT
7806 if (ind)
7807 can_cindent = FALSE;
7808#endif
7809
7810 /*
7811 * When nothing special, insert TAB like a normal character
7812 */
7813 if (!curbuf->b_p_et
7814 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
7815 && curbuf->b_p_sts == 0)
7816 return TRUE;
7817
7818 if (stop_arrow() == FAIL)
7819 return TRUE;
7820
7821 did_ai = FALSE;
7822#ifdef FEAT_SMARTINDENT
7823 did_si = FALSE;
7824 can_si = FALSE;
7825 can_si_back = FALSE;
7826#endif
7827 AppendToRedobuff((char_u *)"\t");
7828
7829 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
7830 temp = (int)curbuf->b_p_sw;
7831 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
7832 temp = (int)curbuf->b_p_sts;
7833 else /* otherwise use 'tabstop' */
7834 temp = (int)curbuf->b_p_ts;
7835 temp -= get_nolist_virtcol() % temp;
7836
7837 /*
7838 * Insert the first space with ins_char(). It will delete one char in
7839 * replace mode. Insert the rest with ins_str(); it will not delete any
7840 * chars. For VREPLACE mode, we use ins_char() for all characters.
7841 */
7842 ins_char(' ');
7843 while (--temp > 0)
7844 {
7845#ifdef FEAT_VREPLACE
7846 if (State & VREPLACE_FLAG)
7847 ins_char(' ');
7848 else
7849#endif
7850 {
7851 ins_str((char_u *)" ");
7852 if (State & REPLACE_FLAG) /* no char replaced */
7853 replace_push(NUL);
7854 }
7855 }
7856
7857 /*
7858 * When 'expandtab' not set: Replace spaces by TABs where possible.
7859 */
7860 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
7861 {
7862 char_u *ptr;
7863#ifdef FEAT_VREPLACE
7864 char_u *saved_line = NULL; /* init for GCC */
7865 pos_T pos;
7866#endif
7867 pos_T fpos;
7868 pos_T *cursor;
7869 colnr_T want_vcol, vcol;
7870 int change_col = -1;
7871 int save_list = curwin->w_p_list;
7872
7873 /*
7874 * Get the current line. For VREPLACE mode, don't make real changes
7875 * yet, just work on a copy of the line.
7876 */
7877#ifdef FEAT_VREPLACE
7878 if (State & VREPLACE_FLAG)
7879 {
7880 pos = curwin->w_cursor;
7881 cursor = &pos;
7882 saved_line = vim_strsave(ml_get_curline());
7883 if (saved_line == NULL)
7884 return FALSE;
7885 ptr = saved_line + pos.col;
7886 }
7887 else
7888#endif
7889 {
7890 ptr = ml_get_cursor();
7891 cursor = &curwin->w_cursor;
7892 }
7893
7894 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
7895 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
7896 curwin->w_p_list = FALSE;
7897
7898 /* Find first white before the cursor */
7899 fpos = curwin->w_cursor;
7900 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
7901 {
7902 --fpos.col;
7903 --ptr;
7904 }
7905
7906 /* In Replace mode, don't change characters before the insert point. */
7907 if ((State & REPLACE_FLAG)
7908 && fpos.lnum == Insstart.lnum
7909 && fpos.col < Insstart.col)
7910 {
7911 ptr += Insstart.col - fpos.col;
7912 fpos.col = Insstart.col;
7913 }
7914
7915 /* compute virtual column numbers of first white and cursor */
7916 getvcol(curwin, &fpos, &vcol, NULL, NULL);
7917 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
7918
7919 /* Use as many TABs as possible. Beware of 'showbreak' and
7920 * 'linebreak' adding extra virtual columns. */
7921 while (vim_iswhite(*ptr))
7922 {
7923 i = lbr_chartabsize((char_u *)"\t", vcol);
7924 if (vcol + i > want_vcol)
7925 break;
7926 if (*ptr != TAB)
7927 {
7928 *ptr = TAB;
7929 if (change_col < 0)
7930 {
7931 change_col = fpos.col; /* Column of first change */
7932 /* May have to adjust Insstart */
7933 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
7934 Insstart.col = fpos.col;
7935 }
7936 }
7937 ++fpos.col;
7938 ++ptr;
7939 vcol += i;
7940 }
7941
7942 if (change_col >= 0)
7943 {
7944 int repl_off = 0;
7945
7946 /* Skip over the spaces we need. */
7947 while (vcol < want_vcol && *ptr == ' ')
7948 {
7949 vcol += lbr_chartabsize(ptr, vcol);
7950 ++ptr;
7951 ++repl_off;
7952 }
7953 if (vcol > want_vcol)
7954 {
7955 /* Must have a char with 'showbreak' just before it. */
7956 --ptr;
7957 --repl_off;
7958 }
7959 fpos.col += repl_off;
7960
7961 /* Delete following spaces. */
7962 i = cursor->col - fpos.col;
7963 if (i > 0)
7964 {
7965 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
7966 /* correct replace stack. */
7967 if ((State & REPLACE_FLAG)
7968#ifdef FEAT_VREPLACE
7969 && !(State & VREPLACE_FLAG)
7970#endif
7971 )
7972 for (temp = i; --temp >= 0; )
7973 replace_join(repl_off);
7974 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00007975#ifdef FEAT_NETBEANS_INTG
7976 if (usingNetbeans)
7977 {
7978 netbeans_removed(curbuf, fpos.lnum, cursor->col,
7979 (long)(i + 1));
7980 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
7981 (char_u *)"\t", 1);
7982 }
7983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 cursor->col -= i;
7985
7986#ifdef FEAT_VREPLACE
7987 /*
7988 * In VREPLACE mode, we haven't changed anything yet. Do it now by
7989 * backspacing over the changed spacing and then inserting the new
7990 * spacing.
7991 */
7992 if (State & VREPLACE_FLAG)
7993 {
7994 /* Backspace from real cursor to change_col */
7995 backspace_until_column(change_col);
7996
7997 /* Insert each char in saved_line from changed_col to
7998 * ptr-cursor */
7999 ins_bytes_len(saved_line + change_col,
8000 cursor->col - change_col);
8001 }
8002#endif
8003 }
8004
8005#ifdef FEAT_VREPLACE
8006 if (State & VREPLACE_FLAG)
8007 vim_free(saved_line);
8008#endif
8009 curwin->w_p_list = save_list;
8010 }
8011
8012 return FALSE;
8013}
8014
8015/*
8016 * Handle CR or NL in insert mode.
8017 * Return TRUE when out of memory or can't undo.
8018 */
8019 static int
8020ins_eol(c)
8021 int c;
8022{
8023 int i;
8024
8025 if (echeck_abbr(c + ABBR_OFF))
8026 return FALSE;
8027 if (stop_arrow() == FAIL)
8028 return TRUE;
8029 undisplay_dollar();
8030
8031 /*
8032 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8033 * character under the cursor. Only push a NUL on the replace stack,
8034 * nothing to put back when the NL is deleted.
8035 */
8036 if ((State & REPLACE_FLAG)
8037#ifdef FEAT_VREPLACE
8038 && !(State & VREPLACE_FLAG)
8039#endif
8040 )
8041 replace_push(NUL);
8042
8043 /*
8044 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8045 * replacing the next line, so we push all of the characters left on the
8046 * line onto the replace stack. This is not done here though, it is done
8047 * in open_line().
8048 */
8049
8050#ifdef FEAT_RIGHTLEFT
8051# ifdef FEAT_FKMAP
8052 if (p_altkeymap && p_fkmap)
8053 fkmap(NL);
8054# endif
8055 /* NL in reverse insert will always start in the end of
8056 * current line. */
8057 if (revins_on)
8058 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8059#endif
8060
8061 AppendToRedobuff(NL_STR);
8062 i = open_line(FORWARD,
8063#ifdef FEAT_COMMENTS
8064 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8065#endif
8066 0, old_indent);
8067 old_indent = 0;
8068#ifdef FEAT_CINDENT
8069 can_cindent = TRUE;
8070#endif
8071
8072 return (!i);
8073}
8074
8075#ifdef FEAT_DIGRAPHS
8076/*
8077 * Handle digraph in insert mode.
8078 * Returns character still to be inserted, or NUL when nothing remaining to be
8079 * done.
8080 */
8081 static int
8082ins_digraph()
8083{
8084 int c;
8085 int cc;
8086
8087 pc_status = PC_STATUS_UNSET;
8088 if (redrawing() && !char_avail())
8089 {
8090 /* may need to redraw when no more chars available now */
8091 ins_redraw();
8092
8093 edit_putchar('?', TRUE);
8094#ifdef FEAT_CMDL_INFO
8095 add_to_showcmd_c(Ctrl_K);
8096#endif
8097 }
8098
8099#ifdef USE_ON_FLY_SCROLL
8100 dont_scroll = TRUE; /* disallow scrolling here */
8101#endif
8102
8103 /* don't map the digraph chars. This also prevents the
8104 * mode message to be deleted when ESC is hit */
8105 ++no_mapping;
8106 ++allow_keys;
8107 c = safe_vgetc();
8108 --no_mapping;
8109 --allow_keys;
8110 if (IS_SPECIAL(c) || mod_mask) /* special key */
8111 {
8112#ifdef FEAT_CMDL_INFO
8113 clear_showcmd();
8114#endif
8115 insert_special(c, TRUE, FALSE);
8116 return NUL;
8117 }
8118 if (c != ESC)
8119 {
8120 if (redrawing() && !char_avail())
8121 {
8122 /* may need to redraw when no more chars available now */
8123 ins_redraw();
8124
8125 if (char2cells(c) == 1)
8126 {
8127 /* first remove the '?', otherwise it's restored when typing
8128 * an ESC next */
8129 edit_unputchar();
8130 ins_redraw();
8131 edit_putchar(c, TRUE);
8132 }
8133#ifdef FEAT_CMDL_INFO
8134 add_to_showcmd_c(c);
8135#endif
8136 }
8137 ++no_mapping;
8138 ++allow_keys;
8139 cc = safe_vgetc();
8140 --no_mapping;
8141 --allow_keys;
8142 if (cc != ESC)
8143 {
8144 AppendToRedobuff((char_u *)CTRL_V_STR);
8145 c = getdigraph(c, cc, TRUE);
8146#ifdef FEAT_CMDL_INFO
8147 clear_showcmd();
8148#endif
8149 return c;
8150 }
8151 }
8152 edit_unputchar();
8153#ifdef FEAT_CMDL_INFO
8154 clear_showcmd();
8155#endif
8156 return NUL;
8157}
8158#endif
8159
8160/*
8161 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8162 * Returns the char to be inserted, or NUL if none found.
8163 */
8164 static int
8165ins_copychar(lnum)
8166 linenr_T lnum;
8167{
8168 int c;
8169 int temp;
8170 char_u *ptr, *prev_ptr;
8171
8172 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8173 {
8174 vim_beep();
8175 return NUL;
8176 }
8177
8178 /* try to advance to the cursor column */
8179 temp = 0;
8180 ptr = ml_get(lnum);
8181 prev_ptr = ptr;
8182 validate_virtcol();
8183 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8184 {
8185 prev_ptr = ptr;
8186 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8187 }
8188 if ((colnr_T)temp > curwin->w_virtcol)
8189 ptr = prev_ptr;
8190
8191#ifdef FEAT_MBYTE
8192 c = (*mb_ptr2char)(ptr);
8193#else
8194 c = *ptr;
8195#endif
8196 if (c == NUL)
8197 vim_beep();
8198 return c;
8199}
8200
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008201/*
8202 * CTRL-Y or CTRL-E typed in Insert mode.
8203 */
8204 static int
8205ins_ctrl_ey(tc)
8206 int tc;
8207{
8208 int c = tc;
8209
8210#ifdef FEAT_INS_EXPAND
8211 if (ctrl_x_mode == CTRL_X_SCROLL)
8212 {
8213 if (c == Ctrl_Y)
8214 scrolldown_clamp();
8215 else
8216 scrollup_clamp();
8217 redraw_later(VALID);
8218 }
8219 else
8220#endif
8221 {
8222 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8223 if (c != NUL)
8224 {
8225 long tw_save;
8226
8227 /* The character must be taken literally, insert like it
8228 * was typed after a CTRL-V, and pretend 'textwidth'
8229 * wasn't set. Digits, 'o' and 'x' are special after a
8230 * CTRL-V, don't use it for these. */
8231 if (c < 256 && !isalnum(c))
8232 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8233 tw_save = curbuf->b_p_tw;
8234 curbuf->b_p_tw = -1;
8235 insert_special(c, TRUE, FALSE);
8236 curbuf->b_p_tw = tw_save;
8237#ifdef FEAT_RIGHTLEFT
8238 revins_chars++;
8239 revins_legal++;
8240#endif
8241 c = Ctrl_V; /* pretend CTRL-V is last character */
8242 auto_format(FALSE, TRUE);
8243 }
8244 }
8245 return c;
8246}
8247
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248#ifdef FEAT_SMARTINDENT
8249/*
8250 * Try to do some very smart auto-indenting.
8251 * Used when inserting a "normal" character.
8252 */
8253 static void
8254ins_try_si(c)
8255 int c;
8256{
8257 pos_T *pos, old_pos;
8258 char_u *ptr;
8259 int i;
8260 int temp;
8261
8262 /*
8263 * do some very smart indenting when entering '{' or '}'
8264 */
8265 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8266 {
8267 /*
8268 * for '}' set indent equal to indent of line containing matching '{'
8269 */
8270 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8271 {
8272 old_pos = curwin->w_cursor;
8273 /*
8274 * If the matching '{' has a ')' immediately before it (ignoring
8275 * white-space), then line up with the start of the line
8276 * containing the matching '(' if there is one. This handles the
8277 * case where an "if (..\n..) {" statement continues over multiple
8278 * lines -- webb
8279 */
8280 ptr = ml_get(pos->lnum);
8281 i = pos->col;
8282 if (i > 0) /* skip blanks before '{' */
8283 while (--i > 0 && vim_iswhite(ptr[i]))
8284 ;
8285 curwin->w_cursor.lnum = pos->lnum;
8286 curwin->w_cursor.col = i;
8287 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8288 curwin->w_cursor = *pos;
8289 i = get_indent();
8290 curwin->w_cursor = old_pos;
8291#ifdef FEAT_VREPLACE
8292 if (State & VREPLACE_FLAG)
8293 change_indent(INDENT_SET, i, FALSE, NUL);
8294 else
8295#endif
8296 (void)set_indent(i, SIN_CHANGED);
8297 }
8298 else if (curwin->w_cursor.col > 0)
8299 {
8300 /*
8301 * when inserting '{' after "O" reduce indent, but not
8302 * more than indent of previous line
8303 */
8304 temp = TRUE;
8305 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8306 {
8307 old_pos = curwin->w_cursor;
8308 i = get_indent();
8309 while (curwin->w_cursor.lnum > 1)
8310 {
8311 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8312
8313 /* ignore empty lines and lines starting with '#'. */
8314 if (*ptr != '#' && *ptr != NUL)
8315 break;
8316 }
8317 if (get_indent() >= i)
8318 temp = FALSE;
8319 curwin->w_cursor = old_pos;
8320 }
8321 if (temp)
8322 shift_line(TRUE, FALSE, 1);
8323 }
8324 }
8325
8326 /*
8327 * set indent of '#' always to 0
8328 */
8329 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8330 {
8331 /* remember current indent for next line */
8332 old_indent = get_indent();
8333 (void)set_indent(0, SIN_CHANGED);
8334 }
8335
8336 /* Adjust ai_col, the char at this position can be deleted. */
8337 if (ai_col > curwin->w_cursor.col)
8338 ai_col = curwin->w_cursor.col;
8339}
8340#endif
8341
8342/*
8343 * Get the value that w_virtcol would have when 'list' is off.
8344 * Unless 'cpo' contains the 'L' flag.
8345 */
8346 static colnr_T
8347get_nolist_virtcol()
8348{
8349 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8350 return getvcol_nolist(&curwin->w_cursor);
8351 validate_virtcol();
8352 return curwin->w_virtcol;
8353}