blob: b7e416034cf3a45d621a89dd7a884609e30614be [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 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000071 char_u *cp_extra; /* extra menu text (allocated, can be NULL) */
72 char_u *cp_info; /* verbose info (can be NULL) */
73 char_u cp_kind; /* kind of match, single letter, or NUL */
74 char_u *cp_fname; /* file containing the match, allocated when
75 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000076 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
77 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078};
79
Bram Moolenaar572cb562005-08-05 21:35:02 +000080#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#define FREE_FNAME (2)
82
83/*
84 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000085 * "compl_first_match" points to the start of the list.
86 * "compl_curr_match" points to the currently selected entry.
87 * "compl_shown_match" is different from compl_curr_match during
88 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000090static compl_T *compl_first_match = NULL;
91static compl_T *compl_curr_match = NULL;
92static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
Bram Moolenaara6557602006-02-04 22:43:20 +000094/* When "compl_leader" is not NULL only matches that start with this string
95 * are used. */
96static char_u *compl_leader = NULL;
97
98static int compl_used_match; /* Selected one of the matches. When
99 FALSE the match was edited or using
100 the longest common string. */
101
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000102/* When the first completion is done "compl_started" is set. When it's
103 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000104static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000105
Bram Moolenaar572cb562005-08-05 21:35:02 +0000106static int compl_matches = 0;
107static char_u *compl_pattern = NULL;
108static int compl_direction = FORWARD;
109static int compl_shows_dir = FORWARD;
110static int compl_pending = FALSE;
111static pos_T compl_startpos;
112static colnr_T compl_col = 0; /* column where the text starts
113 * that is being completed */
114static int save_sm = -1;
115static char_u *compl_orig_text = NULL; /* text as it was before
116 * completion started */
117static int compl_cont_mode = 0;
118static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000120static void ins_ctrl_x __ARGS((void));
121static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000122static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000124static void ins_compl_upd_pum __ARGS((void));
125static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000126static int pum_wanted __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000127static int pum_two_or_more __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000128static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129static void ins_compl_free __ARGS((void));
130static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000131static int ins_compl_bs __ARGS((void));
132static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000133static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000134static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000136static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137static void ins_compl_delete __ARGS((void));
138static void ins_compl_insert __ARGS((void));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000139static int ins_compl_next __ARGS((int allow_get_expansion, int count));
140static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000141static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000142static int ins_compl_key2count __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static int ins_complete __ARGS((int c));
144static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
145#endif /* FEAT_INS_EXPAND */
146
147#define BACKSPACE_CHAR 1
148#define BACKSPACE_WORD 2
149#define BACKSPACE_WORD_NOT_SPACE 3
150#define BACKSPACE_LINE 4
151
Bram Moolenaar754b5602006-02-09 23:53:20 +0000152static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static void ins_ctrl_v __ARGS((void));
154static void undisplay_dollar __ARGS((void));
155static void insert_special __ARGS((int, int, int));
156static void check_auto_format __ARGS((int));
157static void redo_literal __ARGS((int c));
158static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000159#ifdef FEAT_SYN_HL
160static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000161static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000162static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
165static int echeck_abbr __ARGS((int));
166static void replace_push_off __ARGS((int c));
167static int replace_pop __ARGS((void));
168static void replace_join __ARGS((int off));
169static void replace_pop_ins __ARGS((void));
170#ifdef FEAT_MBYTE
171static void mb_replace_pop_ins __ARGS((int cc));
172#endif
173static void replace_flush __ARGS((void));
174static void replace_do_bs __ARGS((void));
175#ifdef FEAT_CINDENT
176static int cindent_on __ARGS((void));
177#endif
178static void ins_reg __ARGS((void));
179static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000180static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000181static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#ifdef FEAT_RIGHTLEFT
183static void ins_ctrl_ __ARGS((void));
184#endif
185#ifdef FEAT_VISUAL
186static int ins_start_select __ARGS((int c));
187#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000188static void ins_insert __ARGS((int replaceState));
189static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190static void ins_shift __ARGS((int c, int lastc));
191static void ins_del __ARGS((void));
192static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
193#ifdef FEAT_MOUSE
194static void ins_mouse __ARGS((int c));
195static void ins_mousescroll __ARGS((int up));
196#endif
197static void ins_left __ARGS((void));
198static void ins_home __ARGS((int c));
199static void ins_end __ARGS((int c));
200static void ins_s_left __ARGS((void));
201static void ins_right __ARGS((void));
202static void ins_s_right __ARGS((void));
203static void ins_up __ARGS((int startcol));
204static void ins_pageup __ARGS((void));
205static void ins_down __ARGS((int startcol));
206static void ins_pagedown __ARGS((void));
207#ifdef FEAT_DND
208static void ins_drop __ARGS((void));
209#endif
210static int ins_tab __ARGS((void));
211static int ins_eol __ARGS((int c));
212#ifdef FEAT_DIGRAPHS
213static int ins_digraph __ARGS((void));
214#endif
215static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000216static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217#ifdef FEAT_SMARTINDENT
218static void ins_try_si __ARGS((int c));
219#endif
220static colnr_T get_nolist_virtcol __ARGS((void));
221
222static colnr_T Insstart_textlen; /* length of line when insert started */
223static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
224
225static char_u *last_insert = NULL; /* the text of the previous insert,
226 K_SPECIAL and CSI are escaped */
227static int last_insert_skip; /* nr of chars in front of previous insert */
228static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000229static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
231#ifdef FEAT_CINDENT
232static int can_cindent; /* may do cindenting on this line */
233#endif
234
235static int old_indent = 0; /* for ^^D command in insert mode */
236
237#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000238static int revins_on; /* reverse insert mode on */
239static int revins_chars; /* how much to skip after edit */
240static int revins_legal; /* was the last char 'legal'? */
241static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242#endif
243
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244static int ins_need_undo; /* call u_save() before inserting a
245 char. Set when edit() is called.
246 after that arrow_used is used. */
247
248static int did_add_space = FALSE; /* auto_format() added an extra space
249 under the cursor */
250
251/*
252 * edit(): Start inserting text.
253 *
254 * "cmdchar" can be:
255 * 'i' normal insert command
256 * 'a' normal append command
257 * 'R' replace command
258 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
259 * but still only one <CR> is inserted. The <Esc> is not used for redo.
260 * 'g' "gI" command.
261 * 'V' "gR" command for Virtual Replace mode.
262 * 'v' "gr" command for single character Virtual Replace mode.
263 *
264 * This function is not called recursively. For CTRL-O commands, it returns
265 * and lets the caller handle the Normal-mode command.
266 *
267 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
268 */
269 int
270edit(cmdchar, startln, count)
271 int cmdchar;
272 int startln; /* if set, insert at start of line */
273 long count;
274{
275 int c = 0;
276 char_u *ptr;
277 int lastc;
278 colnr_T mincol;
279 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 int i;
281 int did_backspace = TRUE; /* previous char was backspace */
282#ifdef FEAT_CINDENT
283 int line_is_white = FALSE; /* line is empty before insert */
284#endif
285 linenr_T old_topline = 0; /* topline before insertion */
286#ifdef FEAT_DIFF
287 int old_topfill = -1;
288#endif
289 int inserted_space = FALSE; /* just inserted a space */
290 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000291 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000293 /* Remember whether editing was restarted after CTRL-O. */
294 did_restart_edit = restart_edit;
295
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 /* sleep before redrawing, needed for "CTRL-O :" that results in an
297 * error message */
298 check_for_delay(TRUE);
299
300#ifdef HAVE_SANDBOX
301 /* Don't allow inserting in the sandbox. */
302 if (sandbox != 0)
303 {
304 EMSG(_(e_sandbox));
305 return FALSE;
306 }
307#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000308 /* Don't allow changes in the buffer while editing the cmdline. The
309 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000310 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000311 {
312 EMSG(_(e_secure));
313 return FALSE;
314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315
316#ifdef FEAT_INS_EXPAND
317 ins_compl_clear(); /* clear stuff for CTRL-X mode */
318#endif
319
Bram Moolenaar843ee412004-06-30 16:16:41 +0000320#ifdef FEAT_AUTOCMD
321 /*
322 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
323 */
324 if (cmdchar != 'r' && cmdchar != 'v')
325 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000326# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000327 if (cmdchar == 'R')
328 ptr = (char_u *)"r";
329 else if (cmdchar == 'V')
330 ptr = (char_u *)"v";
331 else
332 ptr = (char_u *)"i";
333 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000334# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000335 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
336 }
337#endif
338
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339#ifdef FEAT_MOUSE
340 /*
341 * When doing a paste with the middle mouse button, Insstart is set to
342 * where the paste started.
343 */
344 if (where_paste_started.lnum != 0)
345 Insstart = where_paste_started;
346 else
347#endif
348 {
349 Insstart = curwin->w_cursor;
350 if (startln)
351 Insstart.col = 0;
352 }
353 Insstart_textlen = linetabsize(ml_get_curline());
354 Insstart_blank_vcol = MAXCOL;
355 if (!did_ai)
356 ai_col = 0;
357
358 if (cmdchar != NUL && restart_edit == 0)
359 {
360 ResetRedobuff();
361 AppendNumberToRedobuff(count);
362#ifdef FEAT_VREPLACE
363 if (cmdchar == 'V' || cmdchar == 'v')
364 {
365 /* "gR" or "gr" command */
366 AppendCharToRedobuff('g');
367 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
368 }
369 else
370#endif
371 {
372 AppendCharToRedobuff(cmdchar);
373 if (cmdchar == 'g') /* "gI" command */
374 AppendCharToRedobuff('I');
375 else if (cmdchar == 'r') /* "r<CR>" command */
376 count = 1; /* insert only one <CR> */
377 }
378 }
379
380 if (cmdchar == 'R')
381 {
382#ifdef FEAT_FKMAP
383 if (p_fkmap && p_ri)
384 {
385 beep_flush();
386 EMSG(farsi_text_3); /* encoded in Farsi */
387 State = INSERT;
388 }
389 else
390#endif
391 State = REPLACE;
392 }
393#ifdef FEAT_VREPLACE
394 else if (cmdchar == 'V' || cmdchar == 'v')
395 {
396 State = VREPLACE;
397 replaceState = VREPLACE;
398 orig_line_count = curbuf->b_ml.ml_line_count;
399 vr_lines_changed = 1;
400 }
401#endif
402 else
403 State = INSERT;
404
405 stop_insert_mode = FALSE;
406
407 /*
408 * Need to recompute the cursor position, it might move when the cursor is
409 * on a TAB or special character.
410 */
411 curs_columns(TRUE);
412
413 /*
414 * Enable langmap or IME, indicated by 'iminsert'.
415 * Note that IME may enabled/disabled without us noticing here, thus the
416 * 'iminsert' value may not reflect what is actually used. It is updated
417 * when hitting <Esc>.
418 */
419 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
420 State |= LANGMAP;
421#ifdef USE_IM_CONTROL
422 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
423#endif
424
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425#ifdef FEAT_MOUSE
426 setmouse();
427#endif
428#ifdef FEAT_CMDL_INFO
429 clear_showcmd();
430#endif
431#ifdef FEAT_RIGHTLEFT
432 /* there is no reverse replace mode */
433 revins_on = (State == INSERT && p_ri);
434 if (revins_on)
435 undisplay_dollar();
436 revins_chars = 0;
437 revins_legal = 0;
438 revins_scol = -1;
439#endif
440
441 /*
442 * Handle restarting Insert mode.
443 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
444 * restart_edit non-zero, and something in the stuff buffer.
445 */
446 if (restart_edit != 0 && stuff_empty())
447 {
448#ifdef FEAT_MOUSE
449 /*
450 * After a paste we consider text typed to be part of the insert for
451 * the pasted text. You can backspace over the pasted text too.
452 */
453 if (where_paste_started.lnum)
454 arrow_used = FALSE;
455 else
456#endif
457 arrow_used = TRUE;
458 restart_edit = 0;
459
460 /*
461 * If the cursor was after the end-of-line before the CTRL-O and it is
462 * now at the end-of-line, put it after the end-of-line (this is not
463 * correct in very rare cases).
464 * Also do this if curswant is greater than the current virtual
465 * column. Eg after "^O$" or "^O80|".
466 */
467 validate_virtcol();
468 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000469 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 || curwin->w_curswant > curwin->w_virtcol)
471 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
472 {
473 if (ptr[1] == NUL)
474 ++curwin->w_cursor.col;
475#ifdef FEAT_MBYTE
476 else if (has_mbyte)
477 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000478 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 if (ptr[i] == NUL)
480 curwin->w_cursor.col += i;
481 }
482#endif
483 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000484 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 }
486 else
487 arrow_used = FALSE;
488
489 /* we are in insert mode now, don't need to start it anymore */
490 need_start_insertmode = FALSE;
491
492 /* Need to save the line for undo before inserting the first char. */
493 ins_need_undo = TRUE;
494
495#ifdef FEAT_MOUSE
496 where_paste_started.lnum = 0;
497#endif
498#ifdef FEAT_CINDENT
499 can_cindent = TRUE;
500#endif
501#ifdef FEAT_FOLDING
502 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
503 * restarting. */
504 if (!p_im && did_restart_edit == 0)
505 foldOpenCursor();
506#endif
507
508 /*
509 * If 'showmode' is set, show the current (insert/replace/..) mode.
510 * A warning message for changing a readonly file is given here, before
511 * actually changing anything. It's put after the mode, if any.
512 */
513 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000514 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 i = showmode();
516
517 if (!p_im && did_restart_edit == 0)
518 change_warning(i + 1);
519
520#ifdef CURSOR_SHAPE
521 ui_cursor_shape(); /* may show different cursor shape */
522#endif
523#ifdef FEAT_DIGRAPHS
524 do_digraph(-1); /* clear digraphs */
525#endif
526
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000527 /*
528 * Get the current length of the redo buffer, those characters have to be
529 * skipped if we want to get to the inserted characters.
530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 ptr = get_inserted();
532 if (ptr == NULL)
533 new_insert_skip = 0;
534 else
535 {
536 new_insert_skip = (int)STRLEN(ptr);
537 vim_free(ptr);
538 }
539
540 old_indent = 0;
541
542 /*
543 * Main loop in Insert mode: repeat until Insert mode is left.
544 */
545 for (;;)
546 {
547#ifdef FEAT_RIGHTLEFT
548 if (!revins_legal)
549 revins_scol = -1; /* reset on illegal motions */
550 else
551 revins_legal = 0;
552#endif
553 if (arrow_used) /* don't repeat insert when arrow key used */
554 count = 0;
555
556 if (stop_insert_mode)
557 {
558 /* ":stopinsert" used or 'insertmode' reset */
559 count = 0;
560 goto doESCkey;
561 }
562
563 /* set curwin->w_curswant for next K_DOWN or K_UP */
564 if (!arrow_used)
565 curwin->w_set_curswant = TRUE;
566
567 /* If there is no typeahead may check for timestamps (e.g., for when a
568 * menu invoked a shell command). */
569 if (stuff_empty())
570 {
571 did_check_timestamps = FALSE;
572 if (need_check_timestamps)
573 check_timestamps(FALSE);
574 }
575
576 /*
577 * When emsg() was called msg_scroll will have been set.
578 */
579 msg_scroll = FALSE;
580
581#ifdef FEAT_GUI
582 /* When 'mousefocus' is set a mouse movement may have taken us to
583 * another window. "need_mouse_correct" may then be set because of an
584 * autocommand. */
585 if (need_mouse_correct)
586 gui_mouse_correct();
587#endif
588
589#ifdef FEAT_FOLDING
590 /* Open fold at the cursor line, according to 'foldopen'. */
591 if (fdo_flags & FDO_INSERT)
592 foldOpenCursor();
593 /* Close folds where the cursor isn't, according to 'foldclose' */
594 if (!char_avail())
595 foldCheckClose();
596#endif
597
598 /*
599 * If we inserted a character at the last position of the last line in
600 * the window, scroll the window one line up. This avoids an extra
601 * redraw.
602 * This is detected when the cursor column is smaller after inserting
603 * something.
604 * Don't do this when the topline changed already, it has
605 * already been adjusted (by insertchar() calling open_line())).
606 */
607 if (curbuf->b_mod_set
608 && curwin->w_p_wrap
609 && !did_backspace
610 && curwin->w_topline == old_topline
611#ifdef FEAT_DIFF
612 && curwin->w_topfill == old_topfill
613#endif
614 )
615 {
616 mincol = curwin->w_wcol;
617 validate_cursor_col();
618
619 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
620 && curwin->w_wrow == W_WINROW(curwin)
621 + curwin->w_height - 1 - p_so
622 && (curwin->w_cursor.lnum != curwin->w_topline
623#ifdef FEAT_DIFF
624 || curwin->w_topfill > 0
625#endif
626 ))
627 {
628#ifdef FEAT_DIFF
629 if (curwin->w_topfill > 0)
630 --curwin->w_topfill;
631 else
632#endif
633#ifdef FEAT_FOLDING
634 if (hasFolding(curwin->w_topline, NULL, &old_topline))
635 set_topline(curwin, old_topline + 1);
636 else
637#endif
638 set_topline(curwin, curwin->w_topline + 1);
639 }
640 }
641
642 /* May need to adjust w_topline to show the cursor. */
643 update_topline();
644
645 did_backspace = FALSE;
646
647 validate_cursor(); /* may set must_redraw */
648
649 /*
650 * Redraw the display when no characters are waiting.
651 * Also shows mode, ruler and positions cursor.
652 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000653 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
655#ifdef FEAT_SCROLLBIND
656 if (curwin->w_p_scb)
657 do_check_scrollbind(TRUE);
658#endif
659
660 update_curswant();
661 old_topline = curwin->w_topline;
662#ifdef FEAT_DIFF
663 old_topfill = curwin->w_topfill;
664#endif
665
666#ifdef USE_ON_FLY_SCROLL
667 dont_scroll = FALSE; /* allow scrolling here */
668#endif
669
670 /*
671 * Get a character for Insert mode.
672 */
673 lastc = c; /* remember previous char for CTRL-D */
674 c = safe_vgetc();
675
676#ifdef FEAT_RIGHTLEFT
677 if (p_hkmap && KeyTyped)
678 c = hkmap(c); /* Hebrew mode mapping */
679#endif
680#ifdef FEAT_FKMAP
681 if (p_fkmap && KeyTyped)
682 c = fkmap(c); /* Farsi mode mapping */
683#endif
684
685#ifdef FEAT_INS_EXPAND
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000686 /* When the popup menu is visible cursor keys change the selection. */
687 if (c == K_UP && pum_visible())
688 c = Ctrl_P;
689 if (c == K_DOWN && pum_visible())
690 c = Ctrl_N;
691
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000692 /*
693 * Special handling of keys while the popup menu is visible or wanted
694 * and the cursor is still in the completed word.
695 */
696 if (compl_started && pum_wanted() && curwin->w_cursor.col >= compl_col)
Bram Moolenaara6557602006-02-04 22:43:20 +0000697 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000698 /* BS: Delete one character from "compl_leader". */
699 if ((c == K_BS || c == Ctrl_H)
700 && curwin->w_cursor.col > compl_col && ins_compl_bs())
Bram Moolenaara6557602006-02-04 22:43:20 +0000701 continue;
702
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000703 /* When no match was selected or it was edited. */
704 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000705 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000706 /* CTRL-L: Add one character from the current match to
707 * "compl_leader". */
708 if (c == Ctrl_L)
709 {
710 ins_compl_addfrommatch();
711 continue;
712 }
713
714 /* A printable character: Add it to "compl_leader". */
715 if (vim_isprintc(c))
716 {
717 ins_compl_addleader(c);
718 continue;
719 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000720 }
721 }
722
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
724 * it does fix up the text when finishing completion. */
Bram Moolenaara6557602006-02-04 22:43:20 +0000725 if (c != K_IGNORE && ins_compl_prep(c))
726 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727#endif
728
Bram Moolenaar488c6512005-08-11 20:09:58 +0000729 /* CTRL-\ CTRL-N goes to Normal mode,
730 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
731 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 if (c == Ctrl_BSL)
733 {
734 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000735 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 ++no_mapping;
737 ++allow_keys;
738 c = safe_vgetc();
739 --no_mapping;
740 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000741 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000743 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 vungetc(c);
745 c = Ctrl_BSL;
746 }
747 else if (c == Ctrl_G && p_im)
748 continue;
749 else
750 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000751 if (c == Ctrl_O)
752 {
753 ins_ctrl_o();
754 ins_at_eol = FALSE; /* cursor keeps its column */
755 nomove = TRUE;
756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 count = 0;
758 goto doESCkey;
759 }
760 }
761
762#ifdef FEAT_DIGRAPHS
763 c = do_digraph(c);
764#endif
765
766#ifdef FEAT_INS_EXPAND
767 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
768 goto docomplete;
769#endif
770 if (c == Ctrl_V || c == Ctrl_Q)
771 {
772 ins_ctrl_v();
773 c = Ctrl_V; /* pretend CTRL-V is last typed character */
774 continue;
775 }
776
777#ifdef FEAT_CINDENT
778 if (cindent_on()
779# ifdef FEAT_INS_EXPAND
780 && ctrl_x_mode == 0
781# endif
782 )
783 {
784 /* A key name preceded by a bang means this key is not to be
785 * inserted. Skip ahead to the re-indenting below.
786 * A key name preceded by a star means that indenting has to be
787 * done before inserting the key. */
788 line_is_white = inindent(0);
789 if (in_cinkeys(c, '!', line_is_white))
790 goto force_cindent;
791 if (can_cindent && in_cinkeys(c, '*', line_is_white)
792 && stop_arrow() == OK)
793 do_c_expr_indent();
794 }
795#endif
796
797#ifdef FEAT_RIGHTLEFT
798 if (curwin->w_p_rl)
799 switch (c)
800 {
801 case K_LEFT: c = K_RIGHT; break;
802 case K_S_LEFT: c = K_S_RIGHT; break;
803 case K_C_LEFT: c = K_C_RIGHT; break;
804 case K_RIGHT: c = K_LEFT; break;
805 case K_S_RIGHT: c = K_S_LEFT; break;
806 case K_C_RIGHT: c = K_C_LEFT; break;
807 }
808#endif
809
810#ifdef FEAT_VISUAL
811 /*
812 * If 'keymodel' contains "startsel", may start selection. If it
813 * does, a CTRL-O and c will be stuffed, we need to get these
814 * characters.
815 */
816 if (ins_start_select(c))
817 continue;
818#endif
819
820 /*
821 * The big switch to handle a character in insert mode.
822 */
823 switch (c)
824 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000825 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 if (echeck_abbr(ESC + ABBR_OFF))
827 break;
828 /*FALLTHROUGH*/
829
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000830 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831#ifdef FEAT_CMDWIN
832 if (c == Ctrl_C && cmdwin_type != 0)
833 {
834 /* Close the cmdline window. */
835 cmdwin_result = K_IGNORE;
836 got_int = FALSE; /* don't stop executing autocommands et al. */
837 goto doESCkey;
838 }
839#endif
840
841#ifdef UNIX
842do_intr:
843#endif
844 /* when 'insertmode' set, and not halfway a mapping, don't leave
845 * Insert mode */
846 if (goto_im())
847 {
848 if (got_int)
849 {
850 (void)vgetc(); /* flush all buffers */
851 got_int = FALSE;
852 }
853 else
854 vim_beep();
855 break;
856 }
857doESCkey:
858 /*
859 * This is the ONLY return from edit()!
860 */
861 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
862 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000863 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 o_lnum = curwin->w_cursor.lnum;
865
Bram Moolenaar488c6512005-08-11 20:09:58 +0000866 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000867 {
868#ifdef FEAT_AUTOCMD
869 if (cmdchar != 'r' && cmdchar != 'v')
870 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
871 FALSE, curbuf);
872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 continue;
876
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000877 case Ctrl_Z: /* suspend when 'insertmode' set */
878 if (!p_im)
879 goto normalchar; /* insert CTRL-Z as normal char */
880 stuffReadbuff((char_u *)":st\r");
881 c = Ctrl_O;
882 /*FALLTHROUGH*/
883
884 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000885#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000886 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000887 goto docomplete;
888#endif
889 if (echeck_abbr(Ctrl_O + ABBR_OFF))
890 break;
891 ins_ctrl_o();
892 count = 0;
893 goto doESCkey;
894
Bram Moolenaar572cb562005-08-05 21:35:02 +0000895 case K_INS: /* toggle insert/replace mode */
896 case K_KINS:
897 ins_insert(replaceState);
898 break;
899
900 case K_SELECT: /* end of Select mode mapping - ignore */
901 break;
902
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000903#ifdef FEAT_SNIFF
904 case K_SNIFF: /* Sniff command received */
905 stuffcharReadbuff(K_SNIFF);
906 goto doESCkey;
907#endif
908
909 case K_HELP: /* Help key works like <ESC> <Help> */
910 case K_F1:
911 case K_XF1:
912 stuffcharReadbuff(K_HELP);
913 if (p_im)
914 need_start_insertmode = TRUE;
915 goto doESCkey;
916
917#ifdef FEAT_NETBEANS_INTG
918 case K_F21: /* NetBeans command */
919 ++no_mapping; /* don't map the next key hits */
920 i = safe_vgetc();
921 --no_mapping;
922 netbeans_keycommand(i);
923 break;
924#endif
925
926 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 case NUL:
928 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000929 /* For ^@ the trailing ESC will end the insert, unless there is an
930 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
932 && c != Ctrl_A && !p_im)
933 goto doESCkey; /* quit insert mode */
934 inserted_space = FALSE;
935 break;
936
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000937 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 ins_reg();
939 auto_format(FALSE, TRUE);
940 inserted_space = FALSE;
941 break;
942
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000943 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 ins_ctrl_g();
945 break;
946
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000947 case Ctrl_HAT: /* switch input mode and/or langmap */
948 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 break;
950
951#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000952 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (!p_ari)
954 goto normalchar;
955 ins_ctrl_();
956 break;
957#endif
958
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000959 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
961 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
962 goto docomplete;
963#endif
964 /* FALLTHROUGH */
965
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000966 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967# ifdef FEAT_INS_EXPAND
968 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
969 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000970 if (has_compl_option(FALSE))
971 goto docomplete;
972 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 }
974# endif
975 ins_shift(c, lastc);
976 auto_format(FALSE, TRUE);
977 inserted_space = FALSE;
978 break;
979
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000980 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 case K_KDEL:
982 ins_del();
983 auto_format(FALSE, TRUE);
984 break;
985
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000986 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 case Ctrl_H:
988 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
989 auto_format(FALSE, TRUE);
990 break;
991
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000992 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
994 auto_format(FALSE, TRUE);
995 break;
996
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000997 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000998# ifdef FEAT_COMPL_FUNC
999 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001000 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001001 goto docomplete;
1002# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1004 auto_format(FALSE, TRUE);
1005 inserted_space = FALSE;
1006 break;
1007
1008#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001009 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 case K_LEFTMOUSE_NM:
1011 case K_LEFTDRAG:
1012 case K_LEFTRELEASE:
1013 case K_LEFTRELEASE_NM:
1014 case K_MIDDLEMOUSE:
1015 case K_MIDDLEDRAG:
1016 case K_MIDDLERELEASE:
1017 case K_RIGHTMOUSE:
1018 case K_RIGHTDRAG:
1019 case K_RIGHTRELEASE:
1020 case K_X1MOUSE:
1021 case K_X1DRAG:
1022 case K_X1RELEASE:
1023 case K_X2MOUSE:
1024 case K_X2DRAG:
1025 case K_X2RELEASE:
1026 ins_mouse(c);
1027 break;
1028
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001029 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 ins_mousescroll(FALSE);
1031 break;
1032
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001033 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 ins_mousescroll(TRUE);
1035 break;
1036#endif
1037
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001038 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 break;
1040
Bram Moolenaar754b5602006-02-09 23:53:20 +00001041#ifdef FEAT_AUTOCMD
1042 case K_CURSORHOLD: /* Didn't type something for a while. */
1043 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1044 did_cursorhold = TRUE;
1045 break;
1046#endif
1047
Bram Moolenaar4770d092006-01-12 23:22:24 +00001048#ifdef FEAT_GUI_W32
1049 /* On Win32 ignore <M-F4>, we get it when closing the window was
1050 * cancelled. */
1051 case K_F4:
1052 if (mod_mask != MOD_MASK_ALT)
1053 goto normalchar;
1054 break;
1055#endif
1056
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057#ifdef FEAT_GUI
1058 case K_VER_SCROLLBAR:
1059 ins_scroll();
1060 break;
1061
1062 case K_HOR_SCROLLBAR:
1063 ins_horscroll();
1064 break;
1065#endif
1066
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001067 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 case K_S_HOME:
1070 case K_C_HOME:
1071 ins_home(c);
1072 break;
1073
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001074 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 case K_S_END:
1077 case K_C_END:
1078 ins_end(c);
1079 break;
1080
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001081 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001082 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1083 ins_s_left();
1084 else
1085 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 break;
1087
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001088 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 case K_C_LEFT:
1090 ins_s_left();
1091 break;
1092
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001093 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001094 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1095 ins_s_right();
1096 else
1097 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 break;
1099
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001100 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 case K_C_RIGHT:
1102 ins_s_right();
1103 break;
1104
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001105 case K_UP: /* <Up> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001106 if (mod_mask & MOD_MASK_SHIFT)
1107 ins_pageup();
1108 else
1109 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 break;
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 case K_PAGEUP:
1114 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001115#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001116 if (pum_visible())
1117 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 ins_pageup();
1120 break;
1121
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001122 case K_DOWN: /* <Down> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001123 if (mod_mask & MOD_MASK_SHIFT)
1124 ins_pagedown();
1125 else
1126 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 break;
1128
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001129 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 case K_PAGEDOWN:
1131 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001132#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001133 if (pum_visible())
1134 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 ins_pagedown();
1137 break;
1138
1139#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001140 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 ins_drop();
1142 break;
1143#endif
1144
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001145 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 c = TAB;
1147 /* FALLTHROUGH */
1148
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001149 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1151 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1152 goto docomplete;
1153#endif
1154 inserted_space = FALSE;
1155 if (ins_tab())
1156 goto normalchar; /* insert TAB as a normal char */
1157 auto_format(FALSE, TRUE);
1158 break;
1159
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001160 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 c = CAR;
1162 /* FALLTHROUGH */
1163 case CAR:
1164 case NL:
1165#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1166 /* In a quickfix window a <CR> jumps to the error under the
1167 * cursor. */
1168 if (bt_quickfix(curbuf) && c == CAR)
1169 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001170 if (curwin->w_llist_ref == NULL) /* quickfix window */
1171 do_cmdline_cmd((char_u *)".cc");
1172 else /* location list window */
1173 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 break;
1175 }
1176#endif
1177#ifdef FEAT_CMDWIN
1178 if (cmdwin_type != 0)
1179 {
1180 /* Execute the command in the cmdline window. */
1181 cmdwin_result = CAR;
1182 goto doESCkey;
1183 }
1184#endif
1185 if (ins_eol(c) && !p_im)
1186 goto doESCkey; /* out of memory */
1187 auto_format(FALSE, FALSE);
1188 inserted_space = FALSE;
1189 break;
1190
1191#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001192 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193# ifdef FEAT_INS_EXPAND
1194 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1195 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001196 if (has_compl_option(TRUE))
1197 goto docomplete;
1198 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 }
1200# endif
1201# ifdef FEAT_DIGRAPHS
1202 c = ins_digraph();
1203 if (c == NUL)
1204 break;
1205# endif
1206 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208
1209#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001210 case Ctrl_X: /* Enter CTRL-X mode */
1211 ins_ctrl_x();
1212 break;
1213
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001214 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (ctrl_x_mode != CTRL_X_TAGS)
1216 goto normalchar;
1217 goto docomplete;
1218
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001219 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 if (ctrl_x_mode != CTRL_X_FILES)
1221 goto normalchar;
1222 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001223
1224 case 's': /* Spelling completion after ^X */
1225 case Ctrl_S:
1226 if (ctrl_x_mode != CTRL_X_SPELL)
1227 goto normalchar;
1228 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229#endif
1230
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001231 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232#ifdef FEAT_INS_EXPAND
1233 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1234#endif
1235 {
1236 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1237 if (p_im)
1238 {
1239 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1240 break;
1241 goto doESCkey;
1242 }
1243 goto normalchar;
1244 }
1245#ifdef FEAT_INS_EXPAND
1246 /* FALLTHROUGH */
1247
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001248 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 case Ctrl_N:
1250 /* if 'complete' is empty then plain ^P is no longer special,
1251 * but it is under other ^X modes */
1252 if (*curbuf->b_p_cpt == NUL
1253 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001254 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 goto normalchar;
1256
1257docomplete:
1258 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001259 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 break;
1261#endif /* FEAT_INS_EXPAND */
1262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 case Ctrl_Y: /* copy from previous line or scroll down */
1264 case Ctrl_E: /* copy from next line or scroll up */
1265 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 break;
1267
1268 default:
1269#ifdef UNIX
1270 if (c == intr_char) /* special interrupt char */
1271 goto do_intr;
1272#endif
1273
1274 /*
1275 * Insert a nomal character.
1276 */
1277normalchar:
1278#ifdef FEAT_SMARTINDENT
1279 /* Try to perform smart-indenting. */
1280 ins_try_si(c);
1281#endif
1282
1283 if (c == ' ')
1284 {
1285 inserted_space = TRUE;
1286#ifdef FEAT_CINDENT
1287 if (inindent(0))
1288 can_cindent = FALSE;
1289#endif
1290 if (Insstart_blank_vcol == MAXCOL
1291 && curwin->w_cursor.lnum == Insstart.lnum)
1292 Insstart_blank_vcol = get_nolist_virtcol();
1293 }
1294
1295 if (vim_iswordc(c) || !echeck_abbr(
1296#ifdef FEAT_MBYTE
1297 /* Add ABBR_OFF for characters above 0x100, this is
1298 * what check_abbr() expects. */
1299 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1300#endif
1301 c))
1302 {
1303 insert_special(c, FALSE, FALSE);
1304#ifdef FEAT_RIGHTLEFT
1305 revins_legal++;
1306 revins_chars++;
1307#endif
1308 }
1309
1310 auto_format(FALSE, TRUE);
1311
1312#ifdef FEAT_FOLDING
1313 /* When inserting a character the cursor line must never be in a
1314 * closed fold. */
1315 foldOpenCursor();
1316#endif
1317 break;
1318 } /* end of switch (c) */
1319
1320 /* If the cursor was moved we didn't just insert a space */
1321 if (arrow_used)
1322 inserted_space = FALSE;
1323
1324#ifdef FEAT_CINDENT
1325 if (can_cindent && cindent_on()
1326# ifdef FEAT_INS_EXPAND
1327 && ctrl_x_mode == 0
1328# endif
1329 )
1330 {
1331force_cindent:
1332 /*
1333 * Indent now if a key was typed that is in 'cinkeys'.
1334 */
1335 if (in_cinkeys(c, ' ', line_is_white))
1336 {
1337 if (stop_arrow() == OK)
1338 /* re-indent the current line */
1339 do_c_expr_indent();
1340 }
1341 }
1342#endif /* FEAT_CINDENT */
1343
1344 } /* for (;;) */
1345 /* NOTREACHED */
1346}
1347
1348/*
1349 * Redraw for Insert mode.
1350 * This is postponed until getting the next character to make '$' in the 'cpo'
1351 * option work correctly.
1352 * Only redraw when there are no characters available. This speeds up
1353 * inserting sequences of characters (e.g., for CTRL-R).
1354 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001355/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001357ins_redraw(ready)
1358 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359{
1360 if (!char_avail())
1361 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001362#ifdef FEAT_AUTOCMD
1363 /* Trigger CursorMoved if the cursor moved. */
1364 if (ready && has_cursormovedI()
1365 && !equalpos(last_cursormoved, curwin->w_cursor))
1366 {
1367 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1368 last_cursormoved = curwin->w_cursor;
1369 }
1370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (must_redraw)
1372 update_screen(0);
1373 else if (clear_cmdline || redraw_cmdline)
1374 showmode(); /* clear cmdline and show mode */
1375 showruler(FALSE);
1376 setcursor();
1377 emsg_on_display = FALSE; /* may remove error message now */
1378 }
1379}
1380
1381/*
1382 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1383 */
1384 static void
1385ins_ctrl_v()
1386{
1387 int c;
1388
1389 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001390 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
1392 if (redrawing() && !char_avail())
1393 edit_putchar('^', TRUE);
1394 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1395
1396#ifdef FEAT_CMDL_INFO
1397 add_to_showcmd_c(Ctrl_V);
1398#endif
1399
1400 c = get_literal();
1401#ifdef FEAT_CMDL_INFO
1402 clear_showcmd();
1403#endif
1404 insert_special(c, FALSE, TRUE);
1405#ifdef FEAT_RIGHTLEFT
1406 revins_chars++;
1407 revins_legal++;
1408#endif
1409}
1410
1411/*
1412 * Put a character directly onto the screen. It's not stored in a buffer.
1413 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1414 */
1415static int pc_status;
1416#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1417#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1418#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1419#define PC_STATUS_SET 3 /* pc_bytes was filled */
1420#ifdef FEAT_MBYTE
1421static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1422#else
1423static char_u pc_bytes[2]; /* saved bytes */
1424#endif
1425static int pc_attr;
1426static int pc_row;
1427static int pc_col;
1428
1429 void
1430edit_putchar(c, highlight)
1431 int c;
1432 int highlight;
1433{
1434 int attr;
1435
1436 if (ScreenLines != NULL)
1437 {
1438 update_topline(); /* just in case w_topline isn't valid */
1439 validate_cursor();
1440 if (highlight)
1441 attr = hl_attr(HLF_8);
1442 else
1443 attr = 0;
1444 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1445 pc_col = W_WINCOL(curwin);
1446#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1447 pc_status = PC_STATUS_UNSET;
1448#endif
1449#ifdef FEAT_RIGHTLEFT
1450 if (curwin->w_p_rl)
1451 {
1452 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1453# ifdef FEAT_MBYTE
1454 if (has_mbyte)
1455 {
1456 int fix_col = mb_fix_col(pc_col, pc_row);
1457
1458 if (fix_col != pc_col)
1459 {
1460 screen_putchar(' ', pc_row, fix_col, attr);
1461 --curwin->w_wcol;
1462 pc_status = PC_STATUS_RIGHT;
1463 }
1464 }
1465# endif
1466 }
1467 else
1468#endif
1469 {
1470 pc_col += curwin->w_wcol;
1471#ifdef FEAT_MBYTE
1472 if (mb_lefthalve(pc_row, pc_col))
1473 pc_status = PC_STATUS_LEFT;
1474#endif
1475 }
1476
1477 /* save the character to be able to put it back */
1478#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1479 if (pc_status == PC_STATUS_UNSET)
1480#endif
1481 {
1482 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1483 pc_status = PC_STATUS_SET;
1484 }
1485 screen_putchar(c, pc_row, pc_col, attr);
1486 }
1487}
1488
1489/*
1490 * Undo the previous edit_putchar().
1491 */
1492 void
1493edit_unputchar()
1494{
1495 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1496 {
1497#if defined(FEAT_MBYTE)
1498 if (pc_status == PC_STATUS_RIGHT)
1499 ++curwin->w_wcol;
1500 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1501 redrawWinline(curwin->w_cursor.lnum, FALSE);
1502 else
1503#endif
1504 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1505 }
1506}
1507
1508/*
1509 * Called when p_dollar is set: display a '$' at the end of the changed text
1510 * Only works when cursor is in the line that changes.
1511 */
1512 void
1513display_dollar(col)
1514 colnr_T col;
1515{
1516 colnr_T save_col;
1517
1518 if (!redrawing())
1519 return;
1520
1521 cursor_off();
1522 save_col = curwin->w_cursor.col;
1523 curwin->w_cursor.col = col;
1524#ifdef FEAT_MBYTE
1525 if (has_mbyte)
1526 {
1527 char_u *p;
1528
1529 /* If on the last byte of a multi-byte move to the first byte. */
1530 p = ml_get_curline();
1531 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1532 }
1533#endif
1534 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1535 if (curwin->w_wcol < W_WIDTH(curwin))
1536 {
1537 edit_putchar('$', FALSE);
1538 dollar_vcol = curwin->w_virtcol;
1539 }
1540 curwin->w_cursor.col = save_col;
1541}
1542
1543/*
1544 * Call this function before moving the cursor from the normal insert position
1545 * in insert mode.
1546 */
1547 static void
1548undisplay_dollar()
1549{
1550 if (dollar_vcol)
1551 {
1552 dollar_vcol = 0;
1553 redrawWinline(curwin->w_cursor.lnum, FALSE);
1554 }
1555}
1556
1557/*
1558 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1559 * Keep the cursor on the same character.
1560 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1561 * type == INDENT_DEC decrease indent (for CTRL-D)
1562 * type == INDENT_SET set indent to "amount"
1563 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1564 */
1565 void
1566change_indent(type, amount, round, replaced)
1567 int type;
1568 int amount;
1569 int round;
1570 int replaced; /* replaced character, put on replace stack */
1571{
1572 int vcol;
1573 int last_vcol;
1574 int insstart_less; /* reduction for Insstart.col */
1575 int new_cursor_col;
1576 int i;
1577 char_u *ptr;
1578 int save_p_list;
1579 int start_col;
1580 colnr_T vc;
1581#ifdef FEAT_VREPLACE
1582 colnr_T orig_col = 0; /* init for GCC */
1583 char_u *new_line, *orig_line = NULL; /* init for GCC */
1584
1585 /* VREPLACE mode needs to know what the line was like before changing */
1586 if (State & VREPLACE_FLAG)
1587 {
1588 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1589 orig_col = curwin->w_cursor.col;
1590 }
1591#endif
1592
1593 /* for the following tricks we don't want list mode */
1594 save_p_list = curwin->w_p_list;
1595 curwin->w_p_list = FALSE;
1596 vc = getvcol_nolist(&curwin->w_cursor);
1597 vcol = vc;
1598
1599 /*
1600 * For Replace mode we need to fix the replace stack later, which is only
1601 * possible when the cursor is in the indent. Remember the number of
1602 * characters before the cursor if it's possible.
1603 */
1604 start_col = curwin->w_cursor.col;
1605
1606 /* determine offset from first non-blank */
1607 new_cursor_col = curwin->w_cursor.col;
1608 beginline(BL_WHITE);
1609 new_cursor_col -= curwin->w_cursor.col;
1610
1611 insstart_less = curwin->w_cursor.col;
1612
1613 /*
1614 * If the cursor is in the indent, compute how many screen columns the
1615 * cursor is to the left of the first non-blank.
1616 */
1617 if (new_cursor_col < 0)
1618 vcol = get_indent() - vcol;
1619
1620 if (new_cursor_col > 0) /* can't fix replace stack */
1621 start_col = -1;
1622
1623 /*
1624 * Set the new indent. The cursor will be put on the first non-blank.
1625 */
1626 if (type == INDENT_SET)
1627 (void)set_indent(amount, SIN_CHANGED);
1628 else
1629 {
1630#ifdef FEAT_VREPLACE
1631 int save_State = State;
1632
1633 /* Avoid being called recursively. */
1634 if (State & VREPLACE_FLAG)
1635 State = INSERT;
1636#endif
1637 shift_line(type == INDENT_DEC, round, 1);
1638#ifdef FEAT_VREPLACE
1639 State = save_State;
1640#endif
1641 }
1642 insstart_less -= curwin->w_cursor.col;
1643
1644 /*
1645 * Try to put cursor on same character.
1646 * If the cursor is at or after the first non-blank in the line,
1647 * compute the cursor column relative to the column of the first
1648 * non-blank character.
1649 * If we are not in insert mode, leave the cursor on the first non-blank.
1650 * If the cursor is before the first non-blank, position it relative
1651 * to the first non-blank, counted in screen columns.
1652 */
1653 if (new_cursor_col >= 0)
1654 {
1655 /*
1656 * When changing the indent while the cursor is touching it, reset
1657 * Insstart_col to 0.
1658 */
1659 if (new_cursor_col == 0)
1660 insstart_less = MAXCOL;
1661 new_cursor_col += curwin->w_cursor.col;
1662 }
1663 else if (!(State & INSERT))
1664 new_cursor_col = curwin->w_cursor.col;
1665 else
1666 {
1667 /*
1668 * Compute the screen column where the cursor should be.
1669 */
1670 vcol = get_indent() - vcol;
1671 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1672
1673 /*
1674 * Advance the cursor until we reach the right screen column.
1675 */
1676 vcol = last_vcol = 0;
1677 new_cursor_col = -1;
1678 ptr = ml_get_curline();
1679 while (vcol <= (int)curwin->w_virtcol)
1680 {
1681 last_vcol = vcol;
1682#ifdef FEAT_MBYTE
1683 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001684 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 else
1686#endif
1687 ++new_cursor_col;
1688 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1689 }
1690 vcol = last_vcol;
1691
1692 /*
1693 * May need to insert spaces to be able to position the cursor on
1694 * the right screen column.
1695 */
1696 if (vcol != (int)curwin->w_virtcol)
1697 {
1698 curwin->w_cursor.col = new_cursor_col;
1699 i = (int)curwin->w_virtcol - vcol;
1700 ptr = alloc(i + 1);
1701 if (ptr != NULL)
1702 {
1703 new_cursor_col += i;
1704 ptr[i] = NUL;
1705 while (--i >= 0)
1706 ptr[i] = ' ';
1707 ins_str(ptr);
1708 vim_free(ptr);
1709 }
1710 }
1711
1712 /*
1713 * When changing the indent while the cursor is in it, reset
1714 * Insstart_col to 0.
1715 */
1716 insstart_less = MAXCOL;
1717 }
1718
1719 curwin->w_p_list = save_p_list;
1720
1721 if (new_cursor_col <= 0)
1722 curwin->w_cursor.col = 0;
1723 else
1724 curwin->w_cursor.col = new_cursor_col;
1725 curwin->w_set_curswant = TRUE;
1726 changed_cline_bef_curs();
1727
1728 /*
1729 * May have to adjust the start of the insert.
1730 */
1731 if (State & INSERT)
1732 {
1733 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1734 {
1735 if ((int)Insstart.col <= insstart_less)
1736 Insstart.col = 0;
1737 else
1738 Insstart.col -= insstart_less;
1739 }
1740 if ((int)ai_col <= insstart_less)
1741 ai_col = 0;
1742 else
1743 ai_col -= insstart_less;
1744 }
1745
1746 /*
1747 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1748 * If the number of characters before the cursor decreased, need to pop a
1749 * few characters from the replace stack.
1750 * If the number of characters before the cursor increased, need to push a
1751 * few NULs onto the replace stack.
1752 */
1753 if (REPLACE_NORMAL(State) && start_col >= 0)
1754 {
1755 while (start_col > (int)curwin->w_cursor.col)
1756 {
1757 replace_join(0); /* remove a NUL from the replace stack */
1758 --start_col;
1759 }
1760 while (start_col < (int)curwin->w_cursor.col || replaced)
1761 {
1762 replace_push(NUL);
1763 if (replaced)
1764 {
1765 replace_push(replaced);
1766 replaced = NUL;
1767 }
1768 ++start_col;
1769 }
1770 }
1771
1772#ifdef FEAT_VREPLACE
1773 /*
1774 * For VREPLACE mode, we also have to fix the replace stack. In this case
1775 * it is always possible because we backspace over the whole line and then
1776 * put it back again the way we wanted it.
1777 */
1778 if (State & VREPLACE_FLAG)
1779 {
1780 /* If orig_line didn't allocate, just return. At least we did the job,
1781 * even if you can't backspace. */
1782 if (orig_line == NULL)
1783 return;
1784
1785 /* Save new line */
1786 new_line = vim_strsave(ml_get_curline());
1787 if (new_line == NULL)
1788 return;
1789
1790 /* We only put back the new line up to the cursor */
1791 new_line[curwin->w_cursor.col] = NUL;
1792
1793 /* Put back original line */
1794 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1795 curwin->w_cursor.col = orig_col;
1796
1797 /* Backspace from cursor to start of line */
1798 backspace_until_column(0);
1799
1800 /* Insert new stuff into line again */
1801 ins_bytes(new_line);
1802
1803 vim_free(new_line);
1804 }
1805#endif
1806}
1807
1808/*
1809 * Truncate the space at the end of a line. This is to be used only in an
1810 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1811 * modes.
1812 */
1813 void
1814truncate_spaces(line)
1815 char_u *line;
1816{
1817 int i;
1818
1819 /* find start of trailing white space */
1820 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1821 {
1822 if (State & REPLACE_FLAG)
1823 replace_join(0); /* remove a NUL from the replace stack */
1824 }
1825 line[i + 1] = NUL;
1826}
1827
1828#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1829 || defined(FEAT_COMMENTS) || defined(PROTO)
1830/*
1831 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1832 * modes correctly. May also be used when not in insert mode at all.
1833 */
1834 void
1835backspace_until_column(col)
1836 int col;
1837{
1838 while ((int)curwin->w_cursor.col > col)
1839 {
1840 curwin->w_cursor.col--;
1841 if (State & REPLACE_FLAG)
1842 replace_do_bs();
1843 else
1844 (void)del_char(FALSE);
1845 }
1846}
1847#endif
1848
1849#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1850/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001851 * CTRL-X pressed in Insert mode.
1852 */
1853 static void
1854ins_ctrl_x()
1855{
1856 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1857 * CTRL-V works like CTRL-N */
1858 if (ctrl_x_mode != CTRL_X_CMDLINE)
1859 {
1860 /* if the next ^X<> won't ADD nothing, then reset
1861 * compl_cont_status */
1862 if (compl_cont_status & CONT_N_ADDS)
1863 compl_cont_status = (compl_cont_status | CONT_INTRPT);
1864 else
1865 compl_cont_status = 0;
1866 /* We're not sure which CTRL-X mode it will be yet */
1867 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1868 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1869 edit_submode_pre = NULL;
1870 showmode();
1871 }
1872}
1873
1874/*
1875 * Return TRUE if the 'dict' or 'tsr' option can be used.
1876 */
1877 static int
1878has_compl_option(dict_opt)
1879 int dict_opt;
1880{
1881 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1882 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1883 {
1884 ctrl_x_mode = 0;
1885 edit_submode = NULL;
1886 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1887 : (char_u *)_("'thesaurus' option is empty"),
1888 hl_attr(HLF_E));
1889 if (emsg_silent == 0)
1890 {
1891 vim_beep();
1892 setcursor();
1893 out_flush();
1894 ui_delay(2000L, FALSE);
1895 }
1896 return FALSE;
1897 }
1898 return TRUE;
1899}
1900
1901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1903 * This depends on the current mode.
1904 */
1905 int
1906vim_is_ctrl_x_key(c)
1907 int c;
1908{
1909 /* Always allow ^R - let it's results then be checked */
1910 if (c == Ctrl_R)
1911 return TRUE;
1912
Bram Moolenaare3226be2005-12-18 22:10:00 +00001913 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001914 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001915 return TRUE;
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 switch (ctrl_x_mode)
1918 {
1919 case 0: /* Not in any CTRL-X mode */
1920 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1921 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001922 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1924 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1925 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001926 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1927 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 case CTRL_X_SCROLL:
1929 return (c == Ctrl_Y || c == Ctrl_E);
1930 case CTRL_X_WHOLE_LINE:
1931 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1932 case CTRL_X_FILES:
1933 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1934 case CTRL_X_DICTIONARY:
1935 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1936 case CTRL_X_THESAURUS:
1937 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1938 case CTRL_X_TAGS:
1939 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1940#ifdef FEAT_FIND_ID
1941 case CTRL_X_PATH_PATTERNS:
1942 return (c == Ctrl_P || c == Ctrl_N);
1943 case CTRL_X_PATH_DEFINES:
1944 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1945#endif
1946 case CTRL_X_CMDLINE:
1947 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1948 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001949#ifdef FEAT_COMPL_FUNC
1950 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001951 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001952 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001953 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001954#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001955 case CTRL_X_SPELL:
1956 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 }
1958 EMSG(_(e_internal));
1959 return FALSE;
1960}
1961
1962/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001963 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 * case of the originally typed text is used, and the case of the completed
1965 * text is infered, ie this tries to work out what case you probably wanted
1966 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001967 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 */
1969 int
Bram Moolenaar572cb562005-08-05 21:35:02 +00001970ins_compl_add_infercase(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 char_u *str;
1972 int len;
1973 char_u *fname;
1974 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001975 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976{
1977 int has_lower = FALSE;
1978 int was_letter = FALSE;
1979 int idx;
1980
1981 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
1982 {
1983 /* Infer case of completed part -- webb */
1984 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001985 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986
1987 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001988 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001990 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 {
1992 has_lower = TRUE;
1993 if (isupper(IObuff[idx]))
1994 {
1995 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001996 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
1998 break;
1999 }
2000 }
2001 }
2002
2003 /*
2004 * Rule 2: No lower case, 2nd consecutive letter converted to
2005 * upper case.
2006 */
2007 if (!has_lower)
2008 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002009 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002011 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 && islower(IObuff[idx]))
2013 {
2014 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002015 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2017 break;
2018 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002019 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 }
2021 }
2022
2023 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002024 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002026 return ins_compl_add(IObuff, len, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002028 return ins_compl_add(str, len, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029}
2030
2031/*
2032 * Add a match to the list of matches.
2033 * If the given string is already in the list of completions, then return
2034 * FAIL, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaar572cb562005-08-05 21:35:02 +00002035 * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb.
2036 *
2037 * New:
2038 * If the given string is already in the list of completions, then return
2039 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2040 * maybe because alloc() returns NULL, then FAIL is returned -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002042 int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002043ins_compl_add(str, len, fname, extra, cdir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 char_u *str;
2045 int len;
2046 char_u *fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002047 char_u *extra; /* extra text for popup menu or NULL */
2048 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002049 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002051 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002052 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
2054 ui_breakcheck();
2055 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002056 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (len < 0)
2058 len = (int)STRLEN(str);
2059
2060 /*
2061 * If the same match is already present, don't add it.
2062 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002063 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002065 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 do
2067 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002068 if ( !(match->cp_flags & ORIGINAL_TEXT)
2069 && STRNCMP(match->cp_str, str, (size_t)len) == 0
2070 && match->cp_str[len] == NUL)
2071 return NOTDONE;
2072 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002073 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
2075
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002076 /* Remove any popup menu before changing the list of matches. */
2077 ins_compl_del_pum();
2078
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 /*
2080 * Allocate a new match structure.
2081 * Copy the values to the new match structure.
2082 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002083 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002085 return FAIL;
2086 match->cp_number = -1;
2087 if (flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002089 match->cp_number = 0;
2090 match->cp_str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002092 else if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
2094 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002095 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002097
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002099 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2101 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002102 if (fname != NULL
2103 && compl_curr_match
2104 && compl_curr_match->cp_fname != NULL
2105 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002106 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002107 else if (fname != NULL)
2108 {
2109 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002110 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002113 match->cp_fname = NULL;
2114 match->cp_flags = flags;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002115 if (extra != NULL)
2116 match->cp_extra = vim_strsave(extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117
2118 /*
2119 * Link the new match structure in the list of matches.
2120 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002121 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002122 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 else if (dir == FORWARD)
2124 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002125 match->cp_next = compl_curr_match->cp_next;
2126 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 }
2128 else /* BACKWARD */
2129 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002130 match->cp_next = compl_curr_match;
2131 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002133 if (match->cp_next)
2134 match->cp_next->cp_prev = match;
2135 if (match->cp_prev)
2136 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002138 compl_first_match = match;
2139 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
2141 return OK;
2142}
2143
2144/*
2145 * Add an array of matches to the list of matches.
2146 * Frees matches[].
2147 */
2148 static void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002149ins_compl_add_matches(num_matches, matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 int num_matches;
2151 char_u **matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152{
2153 int i;
2154 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002155 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
Bram Moolenaar572cb562005-08-05 21:35:02 +00002157 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002158 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, dir, 0)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002160 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 FreeWild(num_matches, matches);
2162}
2163
2164/* Make the completion list cyclic.
2165 * Return the number of matches (excluding the original).
2166 */
2167 static int
2168ins_compl_make_cyclic()
2169{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002170 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 int count = 0;
2172
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002173 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
2175 /*
2176 * Find the end of the list.
2177 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002178 match = compl_first_match;
2179 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002180 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002182 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 ++count;
2184 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002185 match->cp_next = compl_first_match;
2186 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 }
2188 return count;
2189}
2190
Bram Moolenaar9372a112005-12-06 19:59:18 +00002191/* "compl_match_array" points the currently displayed list of entries in the
2192 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002193static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002194static int compl_match_arraysize;
2195
2196/*
2197 * Update the screen and when there is any scrolling remove the popup menu.
2198 */
2199 static void
2200ins_compl_upd_pum()
2201{
2202 int h;
2203
2204 if (compl_match_array != NULL)
2205 {
2206 h = curwin->w_cline_height;
2207 update_screen(0);
2208 if (h != curwin->w_cline_height)
2209 ins_compl_del_pum();
2210 }
2211}
2212
2213/*
2214 * Remove any popup menu.
2215 */
2216 static void
2217ins_compl_del_pum()
2218{
2219 if (compl_match_array != NULL)
2220 {
2221 pum_undisplay();
2222 vim_free(compl_match_array);
2223 compl_match_array = NULL;
2224 }
2225}
2226
2227/*
2228 * Return TRUE if the popup menu should be displayed.
2229 */
2230 static int
2231pum_wanted()
2232{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002233 /* 'completeopt' must contain "menu" */
2234 if (*p_cot == NUL)
2235 return FALSE;
2236
2237 /* The display looks bad on a B&W display. */
2238 if (t_colors < 8
2239#ifdef FEAT_GUI
2240 && !gui.in_use
2241#endif
2242 )
2243 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002244 return TRUE;
2245}
2246
2247/*
2248 * Return TRUE if there are two or more matches to be shown in the popup menu.
2249 */
2250 static int
2251pum_two_or_more()
2252{
2253 compl_T *compl;
2254 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002255
2256 /* Don't display the popup menu if there are no matches or there is only
2257 * one (ignoring the original text). */
2258 compl = compl_first_match;
2259 i = 0;
2260 do
2261 {
2262 if (compl == NULL
2263 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2264 break;
2265 compl = compl->cp_next;
2266 } while (compl != compl_first_match);
2267
2268 return (i >= 2);
2269}
2270
2271/*
2272 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002273 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002274 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002275 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002276ins_compl_show_pum()
2277{
2278 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002279 compl_T *shown_compl = NULL;
2280 int did_find_shown_match = FALSE;
2281 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002282 int i;
2283 int cur = -1;
2284 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002285 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002286
Bram Moolenaara6557602006-02-04 22:43:20 +00002287 if (!pum_wanted() || !pum_two_or_more())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002288 return;
2289
2290 /* Update the screen before drawing the popup menu over it. */
2291 update_screen(0);
2292
2293 if (compl_match_array == NULL)
2294 {
2295 /* Need to build the popup menu list. */
2296 compl_match_arraysize = 0;
2297 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002298 if (compl_leader != NULL)
2299 lead_len = STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002300 do
2301 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002302 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2303 && (compl_leader == NULL
2304 || STRNCMP(compl->cp_str, compl_leader, lead_len) == 0))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002305 ++compl_match_arraysize;
2306 compl = compl->cp_next;
2307 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002308 if (compl_match_arraysize == 0)
2309 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002310 compl_match_array = (pumitem_T *)alloc_clear(
2311 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002312 * compl_match_arraysize));
2313 if (compl_match_array != NULL)
2314 {
2315 i = 0;
2316 compl = compl_first_match;
2317 do
2318 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002319 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2320 && (compl_leader == NULL
2321 || STRNCMP(compl->cp_str, compl_leader,
2322 lead_len) == 0))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002323 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002324 if (!shown_match_ok)
2325 {
2326 if (compl == compl_shown_match || did_find_shown_match)
2327 {
2328 /* This item is the shown match or this is the
2329 * first displayed item after the shown match. */
2330 compl_shown_match = compl;
2331 did_find_shown_match = TRUE;
2332 shown_match_ok = TRUE;
2333 }
2334 else
2335 /* Remember this displayed match for when the
2336 * shown match is just below it. */
2337 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002338 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002339 }
2340 compl_match_array[i].pum_text = compl->cp_str;
2341 if (compl->cp_extra != NULL)
2342 compl_match_array[i++].pum_extra = compl->cp_extra;
2343 else
2344 compl_match_array[i++].pum_extra = compl->cp_fname;
2345 }
2346
2347 if (compl == compl_shown_match)
2348 {
2349 did_find_shown_match = TRUE;
2350 if (!shown_match_ok && shown_compl != NULL)
2351 {
2352 /* The shown match isn't displayed, set it to the
2353 * previously displayed match. */
2354 compl_shown_match = shown_compl;
2355 shown_match_ok = TRUE;
2356 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002357 }
2358 compl = compl->cp_next;
2359 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002360
2361 if (!shown_match_ok) /* no displayed match at all */
2362 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002363 }
2364 }
2365 else
2366 {
2367 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002368 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002369 if (compl_match_array[i].pum_text == compl_shown_match->cp_str)
Bram Moolenaara6557602006-02-04 22:43:20 +00002370 break;
2371 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002372 }
2373
2374 if (compl_match_array != NULL)
2375 {
2376 /* Compute the screen column of the start of the completed text.
2377 * Use the cursor to get all wrapping and other settings right. */
2378 col = curwin->w_cursor.col;
2379 curwin->w_cursor.col = compl_col;
2380 validate_cursor_col();
2381 pum_display(compl_match_array, compl_match_arraysize, cur,
2382 curwin->w_cline_row + W_WINROW(curwin),
2383 curwin->w_cline_height,
Bram Moolenaar280f1262006-01-30 00:14:18 +00002384 curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002385 curwin->w_cursor.col = col;
2386 }
2387}
2388
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389#define DICT_FIRST (1) /* use just first element in "dict" */
2390#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002391
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392/*
2393 * Add any identifiers that match the given pattern to the list of
2394 * completions.
2395 */
2396 static void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002397ins_compl_dictionaries(dict, pat, flags, thesaurus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 char_u *dict;
2399 char_u *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 int flags;
2401 int thesaurus;
2402{
2403 char_u *ptr;
2404 char_u *buf;
2405 FILE *fp;
2406 regmatch_T regmatch;
2407 int add_r;
2408 char_u **files;
2409 int count;
2410 int i;
2411 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002412 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
2414 buf = alloc(LSIZE);
2415 /* If 'infercase' is set, don't use 'smartcase' here */
2416 save_p_scs = p_scs;
2417 if (curbuf->b_p_inf)
2418 p_scs = FALSE;
2419 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2420 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2421 regmatch.rm_ic = ignorecase(pat);
2422 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002423 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 {
2425 /* copy one dictionary file name into buf */
2426 if (flags == DICT_EXACT)
2427 {
2428 count = 1;
2429 files = &dict;
2430 }
2431 else
2432 {
2433 /* Expand wildcards in the dictionary name, but do not allow
2434 * backticks (for security, the 'dict' option may have been set in
2435 * a modeline). */
2436 copy_option_part(&dict, buf, LSIZE, ",");
2437 if (vim_strchr(buf, '`') != NULL
2438 || expand_wildcards(1, &buf, &count, &files,
2439 EW_FILE|EW_SILENT) != OK)
2440 count = 0;
2441 }
2442
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002443 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {
2445 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2446 if (flags != DICT_EXACT)
2447 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002448 vim_snprintf((char *)IObuff, IOSIZE,
2449 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2451 }
2452
2453 if (fp != NULL)
2454 {
2455 /*
2456 * Read dictionary file line by line.
2457 * Check each line for a match.
2458 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002459 while (!got_int && !compl_interrupted
2460 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
2462 ptr = buf;
2463 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2464 {
2465 ptr = regmatch.startp[0];
2466 ptr = find_word_end(ptr);
2467 add_r = ins_compl_add_infercase(regmatch.startp[0],
2468 (int)(ptr - regmatch.startp[0]),
2469 files[i], dir, 0);
2470 if (thesaurus)
2471 {
2472 char_u *wstart;
2473
2474 /*
2475 * Add the other matches on the line
2476 */
2477 while (!got_int)
2478 {
2479 /* Find start of the next word. Skip white
2480 * space and punctuation. */
2481 ptr = find_word_start(ptr);
2482 if (*ptr == NUL || *ptr == NL)
2483 break;
2484 wstart = ptr;
2485
2486 /* Find end of the word and add it. */
2487#ifdef FEAT_MBYTE
2488 if (has_mbyte)
2489 /* Japanese words may have characters in
2490 * different classes, only separate words
2491 * with single-byte non-word characters. */
2492 while (*ptr != NUL)
2493 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002494 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
2496 if (l < 2 && !vim_iswordc(*ptr))
2497 break;
2498 ptr += l;
2499 }
2500 else
2501#endif
2502 ptr = find_word_end(ptr);
2503 add_r = ins_compl_add_infercase(wstart,
2504 (int)(ptr - wstart), files[i], dir, 0);
2505 }
2506 }
2507 if (add_r == OK)
2508 /* if dir was BACKWARD then honor it just once */
2509 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002510 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 break;
2512 /* avoid expensive call to vim_regexec() when at end
2513 * of line */
2514 if (*ptr == '\n' || got_int)
2515 break;
2516 }
2517 line_breakcheck();
Bram Moolenaar572cb562005-08-05 21:35:02 +00002518 ins_compl_check_keys(50);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 }
2520 fclose(fp);
2521 }
2522 }
2523 if (flags != DICT_EXACT)
2524 FreeWild(count, files);
2525 if (flags)
2526 break;
2527 }
2528 p_scs = save_p_scs;
2529 vim_free(regmatch.regprog);
2530 vim_free(buf);
2531}
2532
2533/*
2534 * Find the start of the next word.
2535 * Returns a pointer to the first char of the word. Also stops at a NUL.
2536 */
2537 char_u *
2538find_word_start(ptr)
2539 char_u *ptr;
2540{
2541#ifdef FEAT_MBYTE
2542 if (has_mbyte)
2543 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002544 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 else
2546#endif
2547 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2548 ++ptr;
2549 return ptr;
2550}
2551
2552/*
2553 * Find the end of the word. Assumes it starts inside a word.
2554 * Returns a pointer to just after the word.
2555 */
2556 char_u *
2557find_word_end(ptr)
2558 char_u *ptr;
2559{
2560#ifdef FEAT_MBYTE
2561 int start_class;
2562
2563 if (has_mbyte)
2564 {
2565 start_class = mb_get_class(ptr);
2566 if (start_class > 1)
2567 while (*ptr != NUL)
2568 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002569 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 if (mb_get_class(ptr) != start_class)
2571 break;
2572 }
2573 }
2574 else
2575#endif
2576 while (vim_iswordc(*ptr))
2577 ++ptr;
2578 return ptr;
2579}
2580
2581/*
2582 * Free the list of completions
2583 */
2584 static void
2585ins_compl_free()
2586{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002587 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002589 vim_free(compl_pattern);
2590 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002591 vim_free(compl_leader);
2592 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002594 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002596
2597 ins_compl_del_pum();
2598 pum_clear();
2599
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002600 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 do
2602 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002603 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002604 compl_curr_match = compl_curr_match->cp_next;
2605 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002607 if (match->cp_flags & FREE_FNAME)
2608 vim_free(match->cp_fname);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002609 vim_free(match->cp_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002611 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2612 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613}
2614
2615 static void
2616ins_compl_clear()
2617{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002618 compl_cont_status = 0;
2619 compl_started = FALSE;
2620 compl_matches = 0;
2621 vim_free(compl_pattern);
2622 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002623 vim_free(compl_leader);
2624 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 save_sm = -1;
2626 edit_submode_extra = NULL;
2627}
2628
2629/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002630 * Delete one character before the cursor and show the subset of the matches
2631 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002632 * Returns TRUE if the work is done and another char to be got from the user.
2633 */
2634 static int
2635ins_compl_bs()
2636{
2637 char_u *line;
2638 char_u *p;
2639
2640 if (curwin->w_cursor.col <= compl_col + compl_length)
2641 {
2642 /* Deleted more than what was used to find matches, need to look for
2643 * matches all over again. */
2644 ins_compl_free();
2645 compl_started = FALSE;
2646 compl_matches = 0;
2647 }
2648
2649 line = ml_get_curline();
2650 p = line + curwin->w_cursor.col;
2651 mb_ptr_back(line, p);
2652
2653 vim_free(compl_leader);
2654 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2655 if (compl_leader != NULL)
2656 {
2657 ins_compl_del_pum();
2658 ins_compl_delete();
2659 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2660
2661 if (!compl_started)
2662 {
2663 /* Matches were cleared, need to search for them now. */
2664 if (ins_complete(Ctrl_N) == FAIL)
2665 compl_cont_status = 0;
2666 else
2667 {
2668 /* Remove the completed word again. */
2669 ins_compl_delete();
2670 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2671 }
2672 }
2673
2674 /* Show the popup menu with a different set of matches. */
2675 ins_compl_show_pum();
2676 compl_used_match = FALSE;
2677
2678 return TRUE;
2679 }
2680 return FALSE;
2681}
2682
2683/*
2684 * Append one character to the match leader. May reduce the number of
2685 * matches.
2686 */
2687 static void
2688ins_compl_addleader(c)
2689 int c;
2690{
2691#ifdef FEAT_MBYTE
2692 int cc;
2693
2694 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2695 {
2696 char_u buf[MB_MAXBYTES + 1];
2697
2698 (*mb_char2bytes)(c, buf);
2699 buf[cc] = NUL;
2700 ins_char_bytes(buf, cc);
2701 }
2702 else
2703#endif
2704 ins_char(c);
2705
2706 vim_free(compl_leader);
2707 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
2708 curwin->w_cursor.col - compl_col);
2709 if (compl_leader != NULL)
2710 {
2711 /* Show the popup menu with a different set of matches. */
2712 ins_compl_del_pum();
2713 ins_compl_show_pum();
2714 compl_used_match = FALSE;
2715 }
2716}
2717
2718/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002719 * Append one character to the match leader. May reduce the number of
2720 * matches.
2721 */
2722 static void
2723ins_compl_addfrommatch()
2724{
2725 char_u *p;
2726 int len = curwin->w_cursor.col - compl_col;
2727 int c;
2728
2729 p = compl_shown_match->cp_str;
2730 if (STRLEN(p) <= len) /* the match is too short */
2731 return;
2732 p += len;
2733#ifdef FEAT_MBYTE
2734 c = mb_ptr2char(p);
2735#else
2736 c = *p;
2737#endif
2738 ins_compl_addleader(c);
2739}
2740
2741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002743 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002744 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002746 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747ins_compl_prep(c)
2748 int c;
2749{
2750 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 int temp;
2752 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002753 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
2755 /* Forget any previous 'special' messages if this is actually
2756 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2757 */
2758 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2759 edit_submode_extra = NULL;
2760
2761 /* Ignore end of Select mode mapping */
2762 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002763 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
2765 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2766 {
2767 /*
2768 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2769 * it will be yet. Now we decide.
2770 */
2771 switch (c)
2772 {
2773 case Ctrl_E:
2774 case Ctrl_Y:
2775 ctrl_x_mode = CTRL_X_SCROLL;
2776 if (!(State & REPLACE_FLAG))
2777 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2778 else
2779 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2780 edit_submode_pre = NULL;
2781 showmode();
2782 break;
2783 case Ctrl_L:
2784 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2785 break;
2786 case Ctrl_F:
2787 ctrl_x_mode = CTRL_X_FILES;
2788 break;
2789 case Ctrl_K:
2790 ctrl_x_mode = CTRL_X_DICTIONARY;
2791 break;
2792 case Ctrl_R:
2793 /* Simply allow ^R to happen without affecting ^X mode */
2794 break;
2795 case Ctrl_T:
2796 ctrl_x_mode = CTRL_X_THESAURUS;
2797 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002798#ifdef FEAT_COMPL_FUNC
2799 case Ctrl_U:
2800 ctrl_x_mode = CTRL_X_FUNCTION;
2801 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002802 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002803 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002804 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002805#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002806 case 's':
2807 case Ctrl_S:
2808 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002809#ifdef FEAT_SYN_HL
2810 spell_back_to_badword();
2811#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002812 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 case Ctrl_RSB:
2814 ctrl_x_mode = CTRL_X_TAGS;
2815 break;
2816#ifdef FEAT_FIND_ID
2817 case Ctrl_I:
2818 case K_S_TAB:
2819 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2820 break;
2821 case Ctrl_D:
2822 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2823 break;
2824#endif
2825 case Ctrl_V:
2826 case Ctrl_Q:
2827 ctrl_x_mode = CTRL_X_CMDLINE;
2828 break;
2829 case Ctrl_P:
2830 case Ctrl_N:
2831 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
2832 * just started ^X mode, or there were enough ^X's to cancel
2833 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2834 * do normal expansion when interrupting a different mode (say
2835 * ^X^F^X^P or ^P^X^X^P, see below)
2836 * nothing changes if interrupting mode 0, (eg, the flag
2837 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002838 if (!(compl_cont_status & CONT_INTRPT))
2839 compl_cont_status |= CONT_LOCAL;
2840 else if (compl_cont_mode != 0)
2841 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 /* FALLTHROUGH */
2843 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002844 /* If we have typed at least 2 ^X's... for modes != 0, we set
2845 * compl_cont_status = 0 (eg, as if we had just started ^X
2846 * mode).
2847 * For mode 0, we set "compl_cont_mode" to an impossible
2848 * value, in both cases ^X^X can be used to restart the same
2849 * mode (avoiding ADDING mode).
2850 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2851 * 'complete' and local ^P expansions respectively.
2852 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2853 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 if (c == Ctrl_X)
2855 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002856 if (compl_cont_mode != 0)
2857 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002859 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
2861 ctrl_x_mode = 0;
2862 edit_submode = NULL;
2863 showmode();
2864 break;
2865 }
2866 }
2867 else if (ctrl_x_mode != 0)
2868 {
2869 /* We're already in CTRL-X mode, do we stay in it? */
2870 if (!vim_is_ctrl_x_key(c))
2871 {
2872 if (ctrl_x_mode == CTRL_X_SCROLL)
2873 ctrl_x_mode = 0;
2874 else
2875 ctrl_x_mode = CTRL_X_FINISHED;
2876 edit_submode = NULL;
2877 }
2878 showmode();
2879 }
2880
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002881 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 {
2883 /* Show error message from attempted keyword completion (probably
2884 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002885 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002887 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
2888 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 || ctrl_x_mode == CTRL_X_FINISHED)
2890 {
2891 /* Get here when we have finished typing a sequence of ^N and
2892 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002893 * memory that was used, and make sure we can redo the insert. */
2894 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002896 char_u *p;
2897
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 /*
2899 * If any of the original typed text has been changed,
2900 * eg when ignorecase is set, we must add back-spaces to
2901 * the redo buffer. We add as few as necessary to delete
2902 * just the part of the original text that has changed.
2903 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002904 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002905 p = compl_orig_text;
2906 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002908 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 ++ptr;
2910 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002911 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002913 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915
2916#ifdef FEAT_CINDENT
2917 want_cindent = (can_cindent && cindent_on());
2918#endif
2919 /*
2920 * When completing whole lines: fix indent for 'cindent'.
2921 * Otherwise, break line if it's too long.
2922 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002923 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 {
2925#ifdef FEAT_CINDENT
2926 /* re-indent the current line */
2927 if (want_cindent)
2928 {
2929 do_c_expr_indent();
2930 want_cindent = FALSE; /* don't do it again */
2931 }
2932#endif
2933 }
2934 else
2935 {
2936 /* put the cursor on the last char, for 'tw' formatting */
2937 curwin->w_cursor.col--;
2938 if (stop_arrow() == OK)
2939 insertchar(NUL, 0, -1);
2940 curwin->w_cursor.col++;
2941 }
2942
2943 auto_format(FALSE, TRUE);
2944
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002945 /* if the popup menu is displayed hitting Enter means accepting
2946 * the selection without inserting anything. */
2947 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
2948 retval = TRUE;
2949
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002951 compl_started = FALSE;
2952 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 msg_clr_cmdline(); /* necessary for "noshowmode" */
2954 ctrl_x_mode = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002955 if (save_sm >= 0)
2956 p_sm = save_sm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 if (edit_submode != NULL)
2958 {
2959 edit_submode = NULL;
2960 showmode();
2961 }
2962
2963#ifdef FEAT_CINDENT
2964 /*
2965 * Indent now if a key was typed that is in 'cinkeys'.
2966 */
2967 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2968 do_c_expr_indent();
2969#endif
2970 }
2971 }
2972
2973 /* reset continue_* if we left expansion-mode, if we stay they'll be
2974 * (re)set properly in ins_complete() */
2975 if (!vim_is_ctrl_x_key(c))
2976 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002977 compl_cont_status = 0;
2978 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002980
2981 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982}
2983
2984/*
2985 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2986 * (depending on flag) starting from buf and looking for a non-scanned
2987 * buffer (other than curbuf). curbuf is special, if it is called with
2988 * buf=curbuf then it has to be the first call for a given flag/expansion.
2989 *
2990 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2991 */
2992 static buf_T *
2993ins_compl_next_buf(buf, flag)
2994 buf_T *buf;
2995 int flag;
2996{
2997#ifdef FEAT_WINDOWS
2998 static win_T *wp;
2999#endif
3000
3001 if (flag == 'w') /* just windows */
3002 {
3003#ifdef FEAT_WINDOWS
3004 if (buf == curbuf) /* first call for this flag/expansion */
3005 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003006 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 && wp->w_buffer->b_scanned)
3008 ;
3009 buf = wp->w_buffer;
3010#else
3011 buf = curbuf;
3012#endif
3013 }
3014 else
3015 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3016 * (unlisted buffers)
3017 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003018 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 && ((flag == 'U'
3020 ? buf->b_p_bl
3021 : (!buf->b_p_bl
3022 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
3023 || buf->b_scanned
3024 || (buf->b_ml.ml_mfp == NULL
3025 && ctrl_x_mode == CTRL_X_WHOLE_LINE)))
3026 ;
3027 return buf;
3028}
3029
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003030#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003031static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003032
3033/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003034 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003035 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003036 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003037 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003038 static void
3039expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003040 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003041 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003042{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003043 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003044 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003045 listitem_T *li;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003046 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003047 char_u *funcname;
3048 pos_T pos;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003049 int dir = compl_direction;
3050 char_u *x;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003051
Bram Moolenaare344bea2005-09-01 20:46:49 +00003052 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3053 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003054 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003055
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003056 /* Call 'completefunc' to obtain the list of matches. */
3057 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003058 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003059
Bram Moolenaare344bea2005-09-01 20:46:49 +00003060 pos = curwin->w_cursor;
3061 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3062 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003063 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003064 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003065
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003066 /* Go through the List with matches and add each of them. */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003067 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003068 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003069 if (li->li_tv.v_type == VAR_DICT && li->li_tv.vval.v_dict != NULL)
3070 {
3071 p = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"word", FALSE);
3072 x = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"menu", FALSE);
3073 }
3074 else
3075 {
3076 p = get_tv_string_chk(&li->li_tv);
3077 x = NULL;
3078 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003079 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003080 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003081 if (ins_compl_add(p, -1, NULL, x, dir, 0) == OK)
3082 /* if dir was BACKWARD then honor it just once */
3083 dir = FORWARD;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003084 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003085 else if (did_emsg)
3086 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003087 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003088
3089 list_unref(matchlist);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003090}
3091#endif /* FEAT_COMPL_FUNC */
3092
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003093/*
3094 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003095 * The search starts at position "ini" in curbuf and in the direction
3096 * compl_direction.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003097 * When "compl_started" is FALSE start at that position, otherwise
3098 * continue where we stopped searching before.
3099 * This may return before finding all the matches.
3100 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 */
3102 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003103ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106 static pos_T first_match_pos;
3107 static pos_T last_match_pos;
3108 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003109 static int found_all = FALSE; /* Found all matches of a
3110 certain type. */
3111 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112
Bram Moolenaar572cb562005-08-05 21:35:02 +00003113 pos_T *pos;
3114 char_u **matches;
3115 int save_p_scs;
3116 int save_p_ws;
3117 int save_p_ic;
3118 int i;
3119 int num_matches;
3120 int len;
3121 int found_new_match;
3122 int type = ctrl_x_mode;
3123 char_u *ptr;
3124 char_u *dict = NULL;
3125 int dict_f = 0;
3126 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003128 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 {
3130 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3131 ins_buf->b_scanned = 0;
3132 found_all = FALSE;
3133 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003134 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003135 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 last_match_pos = first_match_pos = *ini;
3137 }
3138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003139 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003140 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3142 for (;;)
3143 {
3144 found_new_match = FAIL;
3145
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003146 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 * or if found_all says this entry is done. For ^X^L only use the
3148 * entries from 'complete' that look in loaded buffers. */
3149 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003150 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 {
3152 found_all = FALSE;
3153 while (*e_cpt == ',' || *e_cpt == ' ')
3154 e_cpt++;
3155 if (*e_cpt == '.' && !curbuf->b_scanned)
3156 {
3157 ins_buf = curbuf;
3158 first_match_pos = *ini;
3159 /* So that ^N can match word immediately after cursor */
3160 if (ctrl_x_mode == 0)
3161 dec(&first_match_pos);
3162 last_match_pos = first_match_pos;
3163 type = 0;
3164 }
3165 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3166 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3167 {
3168 /* Scan a buffer, but not the current one. */
3169 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3170 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003171 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 first_match_pos.col = last_match_pos.col = 0;
3173 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3174 last_match_pos.lnum = 0;
3175 type = 0;
3176 }
3177 else /* unloaded buffer, scan like dictionary */
3178 {
3179 found_all = TRUE;
3180 if (ins_buf->b_fname == NULL)
3181 continue;
3182 type = CTRL_X_DICTIONARY;
3183 dict = ins_buf->b_fname;
3184 dict_f = DICT_EXACT;
3185 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003186 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 ins_buf->b_fname == NULL
3188 ? buf_spname(ins_buf)
3189 : ins_buf->b_sfname == NULL
3190 ? (char *)ins_buf->b_fname
3191 : (char *)ins_buf->b_sfname);
3192 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3193 }
3194 else if (*e_cpt == NUL)
3195 break;
3196 else
3197 {
3198 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3199 type = -1;
3200 else if (*e_cpt == 'k' || *e_cpt == 's')
3201 {
3202 if (*e_cpt == 'k')
3203 type = CTRL_X_DICTIONARY;
3204 else
3205 type = CTRL_X_THESAURUS;
3206 if (*++e_cpt != ',' && *e_cpt != NUL)
3207 {
3208 dict = e_cpt;
3209 dict_f = DICT_FIRST;
3210 }
3211 }
3212#ifdef FEAT_FIND_ID
3213 else if (*e_cpt == 'i')
3214 type = CTRL_X_PATH_PATTERNS;
3215 else if (*e_cpt == 'd')
3216 type = CTRL_X_PATH_DEFINES;
3217#endif
3218 else if (*e_cpt == ']' || *e_cpt == 't')
3219 {
3220 type = CTRL_X_TAGS;
3221 sprintf((char*)IObuff, _("Scanning tags."));
3222 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3223 }
3224 else
3225 type = -1;
3226
3227 /* in any case e_cpt is advanced to the next entry */
3228 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3229
3230 found_all = TRUE;
3231 if (type == -1)
3232 continue;
3233 }
3234 }
3235
3236 switch (type)
3237 {
3238 case -1:
3239 break;
3240#ifdef FEAT_FIND_ID
3241 case CTRL_X_PATH_PATTERNS:
3242 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003243 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003244 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003246 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3248 (linenr_T)1, (linenr_T)MAXLNUM);
3249 break;
3250#endif
3251
3252 case CTRL_X_DICTIONARY:
3253 case CTRL_X_THESAURUS:
3254 ins_compl_dictionaries(
3255 dict ? dict
3256 : (type == CTRL_X_THESAURUS
3257 ? (*curbuf->b_p_tsr == NUL
3258 ? p_tsr
3259 : curbuf->b_p_tsr)
3260 : (*curbuf->b_p_dict == NUL
3261 ? p_dict
3262 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003263 compl_pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
3265 dict = NULL;
3266 break;
3267
3268 case CTRL_X_TAGS:
3269 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3270 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003271 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
3273 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003274 * of matches is found when compl_pattern is empty */
3275 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3277 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3278 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3279 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003280 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
3282 p_ic = save_p_ic;
3283 break;
3284
3285 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003286 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3288 {
3289
3290 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003291 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003292 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
3294 break;
3295
3296 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003297 if (expand_cmdline(&compl_xp, compl_pattern,
3298 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003300 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 break;
3302
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003303#ifdef FEAT_COMPL_FUNC
3304 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003305 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003306 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003307 break;
3308#endif
3309
Bram Moolenaar488c6512005-08-11 20:09:58 +00003310 case CTRL_X_SPELL:
3311#ifdef FEAT_SYN_HL
3312 num_matches = expand_spelling(first_match_pos.lnum,
3313 first_match_pos.col, compl_pattern, &matches);
3314 if (num_matches > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003315 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003316#endif
3317 break;
3318
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 default: /* normal ^P/^N and ^X^L */
3320 /*
3321 * If 'infercase' is set, don't use 'smartcase' here
3322 */
3323 save_p_scs = p_scs;
3324 if (ins_buf->b_p_inf)
3325 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 /* buffers other than curbuf are scanned from the beginning or the
3328 * end but never from the middle, thus setting nowrapscan in this
3329 * buffers is a good idea, on the other hand, we always set
3330 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3331 save_p_ws = p_ws;
3332 if (ins_buf != curbuf)
3333 p_ws = FALSE;
3334 else if (*e_cpt == '.')
3335 p_ws = TRUE;
3336 for (;;)
3337 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003338 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003340 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3341 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003343 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003345 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003347 found_new_match = searchit(NULL, ins_buf, pos,
3348 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003349 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 RE_LAST);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003351 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003353 /* set compl_started even on fail */
3354 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 first_match_pos = *pos;
3356 last_match_pos = *pos;
3357 }
3358 else if (first_match_pos.lnum == last_match_pos.lnum
3359 && first_match_pos.col == last_match_pos.col)
3360 found_new_match = FAIL;
3361 if (found_new_match == FAIL)
3362 {
3363 if (ins_buf == curbuf)
3364 found_all = TRUE;
3365 break;
3366 }
3367
3368 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003369 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 && ini->lnum == pos->lnum
3371 && ini->col == pos->col)
3372 continue;
3373 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3374 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3375 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003376 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
3378 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3379 continue;
3380 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3381 if (!p_paste)
3382 ptr = skipwhite(ptr);
3383 }
3384 len = (int)STRLEN(ptr);
3385 }
3386 else
3387 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003388 char_u *tmp_ptr = ptr;
3389
3390 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003392 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 /* Skip if already inside a word. */
3394 if (vim_iswordp(tmp_ptr))
3395 continue;
3396 /* Find start of next word. */
3397 tmp_ptr = find_word_start(tmp_ptr);
3398 }
3399 /* Find end of this word. */
3400 tmp_ptr = find_word_end(tmp_ptr);
3401 len = (int)(tmp_ptr - ptr);
3402
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003403 if ((compl_cont_status & CONT_ADDING)
3404 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 {
3406 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3407 {
3408 /* Try next line, if any. the new word will be
3409 * "join" as if the normal command "J" was used.
3410 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003411 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 * works -- Acevedo */
3413 STRNCPY(IObuff, ptr, len);
3414 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3415 tmp_ptr = ptr = skipwhite(ptr);
3416 /* Find start of next word. */
3417 tmp_ptr = find_word_start(tmp_ptr);
3418 /* Find end of next word. */
3419 tmp_ptr = find_word_end(tmp_ptr);
3420 if (tmp_ptr > ptr)
3421 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003422 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003424 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 IObuff[len++] = ' ';
3426 /* IObuf =~ "\k.* ", thus len >= 2 */
3427 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003428 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 || (vim_strchr(p_cpo, CPO_JOINSP)
3430 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003431 && (IObuff[len - 2] == '?'
3432 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 IObuff[len++] = ' ';
3434 }
3435 /* copy as much as posible of the new word */
3436 if (tmp_ptr - ptr >= IOSIZE - len)
3437 tmp_ptr = ptr + IOSIZE - len - 1;
3438 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3439 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003440 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 }
3442 IObuff[len] = NUL;
3443 ptr = IObuff;
3444 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003445 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 continue;
3447 }
3448 }
3449 if (ins_compl_add_infercase(ptr, len,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003450 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003451 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
3453 found_new_match = OK;
3454 break;
3455 }
3456 }
3457 p_scs = save_p_scs;
3458 p_ws = save_p_ws;
3459 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003460
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003461 /* check if compl_curr_match has changed, (e.g. other type of
3462 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003463 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 found_new_match = OK;
3465
3466 /* break the loop for specialized modes (use 'complete' just for the
3467 * generic ctrl_x_mode == 0) or when we've found a new match */
3468 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003469 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003470 {
3471 if (got_int)
3472 break;
3473 if (pum_wanted() && type != -1)
3474 /* Fill the popup menu as soon as possible. */
3475 ins_compl_check_keys(0);
3476 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3477 || compl_interrupted)
3478 break;
3479 compl_started = TRUE;
3480 }
3481 else
3482 {
3483 /* Mark a buffer scanned when it has been scanned completely */
3484 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3485 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003487 compl_started = FALSE;
3488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003490 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
3492 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3493 && *e_cpt == NUL) /* Got to end of 'complete' */
3494 found_new_match = FAIL;
3495
3496 i = -1; /* total of matches, unknown */
3497 if (found_new_match == FAIL
3498 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3499 i = ins_compl_make_cyclic();
3500
3501 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003502 * just been made cyclic then we have to move compl_curr_match to the next
3503 * or previous entry (if any) -- Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003504 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003505 if (compl_curr_match == NULL)
3506 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 return i;
3508}
3509
3510/* Delete the old text being completed. */
3511 static void
3512ins_compl_delete()
3513{
3514 int i;
3515
3516 /*
3517 * In insert mode: Delete the typed part.
3518 * In replace mode: Put the old characters back, if any.
3519 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003520 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 backspace_until_column(i);
3522 changed_cline_bef_curs();
3523}
3524
3525/* Insert the new text being completed. */
3526 static void
3527ins_compl_insert()
3528{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003529 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003530 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531}
3532
3533/*
3534 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003535 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3536 * get more completions. If it is FALSE, then we just do nothing when there
3537 * are no more completions in a given direction. The latter case is used when
3538 * we are still in the middle of finding completions, to allow browsing
3539 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 * Return the total number of matches, or -1 if still unknown -- webb.
3541 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003542 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3543 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 *
3545 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003546 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3547 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 */
3549 static int
Bram Moolenaare3226be2005-12-18 22:10:00 +00003550ins_compl_next(allow_get_expansion, count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003552 int count; /* repeat completion this many times; should
3553 be at least 1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554{
3555 int num_matches = -1;
3556 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003557 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003558 compl_T *found_compl = NULL;
3559 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
3561 if (allow_get_expansion)
3562 {
3563 /* Delete old text to be replaced */
3564 ins_compl_delete();
3565 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003566 compl_pending = FALSE;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003567
3568 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3569 * around. */
3570 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003572 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003574 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003575 found_end = (compl_first_match != NULL
3576 && (compl_shown_match->cp_next == compl_first_match
3577 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003578 }
3579 else if (compl_shows_dir == BACKWARD
3580 && compl_shown_match->cp_prev != NULL)
3581 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003582 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003583 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003584 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
3586 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003587 {
3588 compl_pending = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003589 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003590 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003591
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003592 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara6557602006-02-04 22:43:20 +00003593 if (compl_pending && compl_direction == compl_shows_dir)
3594 compl_shown_match = compl_curr_match;
3595 found_end = FALSE;
3596 }
3597 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
3598 && compl_leader != NULL
3599 && STRNCMP(compl_shown_match->cp_str,
3600 compl_leader, STRLEN(compl_leader)) != 0)
3601 ++todo;
3602 else
3603 /* Remember a matching item. */
3604 found_compl = compl_shown_match;
3605
3606 /* Stop at the end of the list when we found a usable match. */
3607 if (found_end)
3608 {
3609 if (found_compl != NULL)
3610 {
3611 compl_shown_match = found_compl;
3612 break;
3613 }
3614 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 }
3617
3618 /* Insert the text of the new completion */
3619 ins_compl_insert();
3620
3621 if (!allow_get_expansion)
3622 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003623 /* may undisplay the popup menu first */
3624 ins_compl_upd_pum();
3625
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003626 /* display the updated popup menu */
3627 ins_compl_show_pum();
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 /* Delete old text to be replaced, since we're still searching and
3630 * don't want to match ourselves! */
3631 ins_compl_delete();
3632 }
3633
3634 /*
3635 * Show the file name for the match (if any)
3636 * Truncate the file name to avoid a wait for return.
3637 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003638 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
3640 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003641 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (i <= 0)
3643 i = 0;
3644 else
3645 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003646 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 msg(IObuff);
3648 redraw_cmdline = FALSE; /* don't overwrite! */
3649 }
3650
3651 return num_matches;
3652}
3653
3654/*
3655 * Call this while finding completions, to check whether the user has hit a key
3656 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003657 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003659 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 */
3661 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003662ins_compl_check_keys(frequency)
3663 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664{
3665 static int count = 0;
3666
3667 int c;
3668
3669 /* Don't check when reading keys from a script. That would break the test
3670 * scripts */
3671 if (using_script())
3672 return;
3673
3674 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003675 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 return;
3677 count = 0;
3678
3679 ++no_mapping;
3680 c = vpeekc_any();
3681 --no_mapping;
3682 if (c != NUL)
3683 {
3684 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3685 {
3686 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003687 compl_shows_dir = ins_compl_key2dir(c);
3688 (void)ins_compl_next(FALSE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003691 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003693 if (compl_pending && !got_int)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003694 (void)ins_compl_next(FALSE, 1);
3695}
3696
3697/*
3698 * Decide the direction of Insert mode complete from the key typed.
3699 * Returns BACKWARD or FORWARD.
3700 */
3701 static int
3702ins_compl_key2dir(c)
3703 int c;
3704{
3705 if (c == Ctrl_P || c == Ctrl_L || (pum_visible()
3706 && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP)))
3707 return BACKWARD;
3708 return FORWARD;
3709}
3710
3711/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003712 * Return TRUE for keys that are used for completion only when the popup menu
3713 * is visible.
3714 */
3715 static int
3716ins_compl_pum_key(c)
3717 int c;
3718{
3719 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
3720 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN);
3721}
3722
3723/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00003724 * Decide the number of completions to move forward.
3725 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3726 */
3727 static int
3728ins_compl_key2count(c)
3729 int c;
3730{
3731 int h;
3732
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003733 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00003734 {
3735 h = pum_get_height();
3736 if (h > 3)
3737 h -= 2; /* keep some context */
3738 return h;
3739 }
3740 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741}
3742
3743/*
3744 * Do Insert mode completion.
3745 * Called when character "c" was typed, which has a meaning for completion.
3746 * Returns OK if completion was done, FAIL if something failed (out of mem).
3747 */
3748 static int
3749ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003750 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003752 char_u *line;
3753 int startcol = 0; /* column where searched text starts */
3754 colnr_T curs_col; /* cursor column */
3755 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756
Bram Moolenaare3226be2005-12-18 22:10:00 +00003757 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003758 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 {
3760 /* First time we hit ^N or ^P (in a row, I mean) */
3761
3762 /* Turn off 'sm' so we don't show matches with ^X^L */
3763 save_sm = p_sm;
3764 p_sm = FALSE;
3765
3766 did_ai = FALSE;
3767#ifdef FEAT_SMARTINDENT
3768 did_si = FALSE;
3769 can_si = FALSE;
3770 can_si_back = FALSE;
3771#endif
3772 if (stop_arrow() == FAIL)
3773 return FAIL;
3774
3775 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003776 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777
3778 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003779 * "compl_startpos" to the cursor as a pattern to add a new word
3780 * instead of expand the one before the cursor, in word-wise if
3781 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 * is not in the same line as the cursor then fix it (the line has
3783 * been split because it was longer than 'tw'). if SOL is set then
3784 * skip the previous pattern, a word at the beginning of the line has
3785 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003786 if ((compl_cont_status & CONT_INTRPT) && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 {
3788 /*
3789 * it is a continued search
3790 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003791 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3793 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3794 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003795 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003797 /* line (probably) wrapped, set compl_startpos to the
3798 * first non_blank in the line, if it is not a wordchar
3799 * include it to get a better pattern, but then we don't
3800 * want the "\\<" prefix, check it bellow */
3801 compl_col = (colnr_T)(skipwhite(line) - line);
3802 compl_startpos.col = compl_col;
3803 compl_startpos.lnum = curwin->w_cursor.lnum;
3804 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 }
3806 else
3807 {
3808 /* S_IPOS was set when we inserted a word that was at the
3809 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003810 * mode but first we need to redefine compl_startpos */
3811 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003813 compl_cont_status |= CONT_SOL;
3814 compl_startpos.col = (colnr_T)(skipwhite(
3815 line + compl_length
3816 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003818 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003820 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003821 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 * have enough space? just being paranoic */
3823#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003824 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003826 compl_cont_status &= ~CONT_SOL;
3827 compl_length = (IOSIZE - MIN_SPACE);
3828 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003830 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3831 if (compl_length < 1)
3832 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003835 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003837 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003840 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003842 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003844 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003846 compl_cont_status = 0;
3847 compl_cont_status |= CONT_N_ADDS;
3848 compl_startpos = curwin->w_cursor;
3849 startcol = (int)curs_col;
3850 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852
3853 /* Work out completion pattern and original text -- webb */
3854 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3855 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003856 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3858 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003859 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003861 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003863 compl_col += ++startcol;
3864 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
3866 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003867 compl_pattern = str_foldcase(line + compl_col,
3868 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003870 compl_pattern = vim_strnsave(line + compl_col,
3871 compl_length);
3872 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 return FAIL;
3874 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003875 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 {
3877 char_u *prefix = (char_u *)"\\<";
3878
3879 /* we need 3 extra chars, 1 for the NUL and
3880 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003881 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3882 compl_length) + 3);
3883 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003885 if (!vim_iswordp(line + compl_col)
3886 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 && (
3888#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003889 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003891 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892#endif
3893 )))
3894 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003895 STRCPY((char *)compl_pattern, prefix);
3896 (void)quote_meta(compl_pattern + STRLEN(prefix),
3897 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003899 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003901 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003903 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904#endif
3905 )
3906 {
3907 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003908 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3909 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003911 compl_col += curs_col;
3912 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
3914 else
3915 {
3916#ifdef FEAT_MBYTE
3917 /* Search the point of change class of multibyte character
3918 * or not a word single byte character backward. */
3919 if (has_mbyte)
3920 {
3921 int base_class;
3922 int head_off;
3923
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003924 startcol -= (*mb_head_off)(line, line + startcol);
3925 base_class = mb_get_class(line + startcol);
3926 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003928 head_off = (*mb_head_off)(line, line + startcol);
3929 if (base_class != mb_get_class(line + startcol
3930 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003932 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
3934 }
3935 else
3936#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003937 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003939 compl_col += ++startcol;
3940 compl_length = (int)curs_col - startcol;
3941 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
3943 /* Only match word with at least two chars -- webb
3944 * there's no need to call quote_meta,
3945 * alloc(7) is enough -- Acevedo
3946 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003947 compl_pattern = alloc(7);
3948 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003950 STRCPY((char *)compl_pattern, "\\<");
3951 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3952 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
3954 else
3955 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003956 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3957 compl_length) + 3);
3958 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003960 STRCPY((char *)compl_pattern, "\\<");
3961 (void)quote_meta(compl_pattern + 2, line + compl_col,
3962 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964 }
3965 }
3966 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3967 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003968 compl_col = skipwhite(line) - line;
3969 compl_length = (int)curs_col - (int)compl_col;
3970 if (compl_length < 0) /* cursor in indent: empty pattern */
3971 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003973 compl_pattern = str_foldcase(line + compl_col, compl_length,
3974 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003976 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3977 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 return FAIL;
3979 }
3980 else if (ctrl_x_mode == CTRL_X_FILES)
3981 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003982 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003984 compl_col += ++startcol;
3985 compl_length = (int)curs_col - startcol;
3986 compl_pattern = addstar(line + compl_col, compl_length,
3987 EXPAND_FILES);
3988 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 return FAIL;
3990 }
3991 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3992 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003993 compl_pattern = vim_strnsave(line, curs_col);
3994 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003996 set_cmd_context(&compl_xp, compl_pattern,
3997 (int)STRLEN(compl_pattern), curs_col);
3998 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3999 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004001 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4002 compl_col = startcol;
4003 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004005 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004006 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004007#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004008 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004009 * Call user defined function 'completefunc' with "a:findstart"
4010 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004011 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004012 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004013 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004014 char_u *funcname;
4015 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004016
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004017 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004018 * string */
4019 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4020 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4021 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004022 {
4023 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4024 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004025 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004026 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004027
4028 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004029 args[1] = NULL;
4030 pos = curwin->w_cursor;
4031 col = call_func_retnr(funcname, 2, args, FALSE);
4032 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004033
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004034 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004035 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004036 compl_col = col;
4037 if ((colnr_T)compl_col > curs_col)
4038 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004039
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004040 /* Setup variables for completion. Need to obtain "line" again,
4041 * it may have become invalid. */
4042 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004043 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004044 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4045 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004046#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004047 return FAIL;
4048 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004049 else if (ctrl_x_mode == CTRL_X_SPELL)
4050 {
4051#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004052 if (spell_bad_len > 0)
4053 compl_col = curs_col - spell_bad_len;
4054 else
4055 compl_col = spell_word_start(startcol);
4056 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004057 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004058 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004059 compl_length = (int)curs_col - compl_col;
4060 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4061 if (compl_pattern == NULL)
4062#endif
4063 return FAIL;
4064 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004065 else
4066 {
4067 EMSG2(_(e_intern2), "ins_complete()");
4068 return FAIL;
4069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004071 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 {
4073 edit_submode_pre = (char_u *)_(" Adding");
4074 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4075 {
4076 /* Insert a new line, keep indentation but ignore 'comments' */
4077#ifdef FEAT_COMMENTS
4078 char_u *old = curbuf->b_p_com;
4079
4080 curbuf->b_p_com = (char_u *)"";
4081#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004082 compl_startpos.lnum = curwin->w_cursor.lnum;
4083 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 ins_eol('\r');
4085#ifdef FEAT_COMMENTS
4086 curbuf->b_p_com = old;
4087#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004088 compl_length = 0;
4089 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091 }
4092 else
4093 {
4094 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004095 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004098 if (compl_cont_status & CONT_LOCAL)
4099 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 else
4101 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4102
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004104 * "compl_orig_text" itself (not a copy) is added, it will be freed
4105 * when the list of matches is freed. */
4106 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4107 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004108 -1, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004110 vim_free(compl_pattern);
4111 compl_pattern = NULL;
4112 vim_free(compl_orig_text);
4113 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 return FAIL;
4115 }
4116
4117 /* showmode might reset the internal line pointers, so it must
4118 * be called before line = ml_get(), or when this address is no
4119 * longer needed. -- Acevedo.
4120 */
4121 edit_submode_extra = (char_u *)_("-- Searching...");
4122 edit_submode_highl = HLF_COUNT;
4123 showmode();
4124 edit_submode_extra = NULL;
4125 out_flush();
4126 }
4127
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004128 compl_shown_match = compl_curr_match;
4129 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
4131 /*
4132 * Find next match.
4133 */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004134 n = ins_compl_next(TRUE, ins_compl_key2count(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004136 /* may undisplay the popup menu */
4137 ins_compl_upd_pum();
4138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004139 if (n > 1) /* all matches have been found */
4140 compl_matches = n;
4141 compl_curr_match = compl_shown_match;
4142 compl_direction = compl_shows_dir;
4143 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 /* eat the ESC to avoid leaving insert mode */
4146 if (got_int && !global_busy)
4147 {
4148 (void)vgetc();
4149 got_int = FALSE;
4150 }
4151
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004152 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004153 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004155 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4156 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4158 edit_submode_highl = HLF_E;
4159 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4160 * because we couldn't expand anything at first place, but if we used
4161 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4162 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004163 if ( compl_length > 1
4164 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 || (ctrl_x_mode != 0
4166 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4167 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004168 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 }
4170
Bram Moolenaar572cb562005-08-05 21:35:02 +00004171 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004172 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004174 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175
4176 if (edit_submode_extra == NULL)
4177 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004178 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
4180 edit_submode_extra = (char_u *)_("Back at original");
4181 edit_submode_highl = HLF_W;
4182 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004183 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
4185 edit_submode_extra = (char_u *)_("Word from other line");
4186 edit_submode_highl = HLF_COUNT;
4187 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004188 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 {
4190 edit_submode_extra = (char_u *)_("The only match");
4191 edit_submode_highl = HLF_COUNT;
4192 }
4193 else
4194 {
4195 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004196 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004198 int number = 0;
4199 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004201 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
4203 /* search backwards for the first valid (!= -1) number.
4204 * This should normally succeed already at the first loop
4205 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004206 for (match = compl_curr_match->cp_prev; match != NULL
4207 && match != compl_first_match;
4208 match = match->cp_prev)
4209 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004211 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 break;
4213 }
4214 if (match != NULL)
4215 /* go up and assign all numbers which are not assigned
4216 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004217 for (match = match->cp_next;
4218 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004219 match = match->cp_next)
4220 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222 else /* BACKWARD */
4223 {
4224 /* search forwards (upwards) for the first valid (!= -1)
4225 * number. This should normally succeed already at the
4226 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004227 for (match = compl_curr_match->cp_next; match != NULL
4228 && match != compl_first_match;
4229 match = match->cp_next)
4230 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004232 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 break;
4234 }
4235 if (match != NULL)
4236 /* go down and assign all numbers which are not
4237 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004238 for (match = match->cp_prev; match
4239 && match->cp_number == -1;
4240 match = match->cp_prev)
4241 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 }
4244
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004245 /* The match should always have a sequence number now, this is
4246 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004247 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 {
4249 /* Space for 10 text chars. + 2x10-digit no.s */
4250 static char_u match_ref[31];
4251
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004252 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004254 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004256 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004257 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004258 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 edit_submode_extra = match_ref;
4260 edit_submode_highl = HLF_R;
4261 if (dollar_vcol)
4262 curs_columns(FALSE);
4263 }
4264 }
4265 }
4266
4267 /* Show a message about what (completion) mode we're in. */
4268 showmode();
4269 if (edit_submode_extra != NULL)
4270 {
4271 if (!p_smd)
4272 msg_attr(edit_submode_extra,
4273 edit_submode_highl < HLF_COUNT
4274 ? hl_attr(edit_submode_highl) : 0);
4275 }
4276 else
4277 msg_clr_cmdline(); /* necessary for "noshowmode" */
4278
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004279 ins_compl_show_pum();
4280
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 return OK;
4282}
4283
4284/*
4285 * Looks in the first "len" chars. of "src" for search-metachars.
4286 * If dest is not NULL the chars. are copied there quoting (with
4287 * a backslash) the metachars, and dest would be NUL terminated.
4288 * Returns the length (needed) of dest
4289 */
4290 static int
4291quote_meta(dest, src, len)
4292 char_u *dest;
4293 char_u *src;
4294 int len;
4295{
4296 int m;
4297
4298 for (m = len; --len >= 0; src++)
4299 {
4300 switch (*src)
4301 {
4302 case '.':
4303 case '*':
4304 case '[':
4305 if (ctrl_x_mode == CTRL_X_DICTIONARY
4306 || ctrl_x_mode == CTRL_X_THESAURUS)
4307 break;
4308 case '~':
4309 if (!p_magic) /* quote these only if magic is set */
4310 break;
4311 case '\\':
4312 if (ctrl_x_mode == CTRL_X_DICTIONARY
4313 || ctrl_x_mode == CTRL_X_THESAURUS)
4314 break;
4315 case '^': /* currently it's not needed. */
4316 case '$':
4317 m++;
4318 if (dest != NULL)
4319 *dest++ = '\\';
4320 break;
4321 }
4322 if (dest != NULL)
4323 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004324# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 /* Copy remaining bytes of a multibyte character. */
4326 if (has_mbyte)
4327 {
4328 int i, mb_len;
4329
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004330 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 if (mb_len > 0 && len >= mb_len)
4332 for (i = 0; i < mb_len; ++i)
4333 {
4334 --len;
4335 ++src;
4336 if (dest != NULL)
4337 *dest++ = *src;
4338 }
4339 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
4342 if (dest != NULL)
4343 *dest = NUL;
4344
4345 return m;
4346}
4347#endif /* FEAT_INS_EXPAND */
4348
4349/*
4350 * Next character is interpreted literally.
4351 * A one, two or three digit decimal number is interpreted as its byte value.
4352 * If one or two digits are entered, the next character is given to vungetc().
4353 * For Unicode a character > 255 may be returned.
4354 */
4355 int
4356get_literal()
4357{
4358 int cc;
4359 int nc;
4360 int i;
4361 int hex = FALSE;
4362 int octal = FALSE;
4363#ifdef FEAT_MBYTE
4364 int unicode = 0;
4365#endif
4366
4367 if (got_int)
4368 return Ctrl_C;
4369
4370#ifdef FEAT_GUI
4371 /*
4372 * In GUI there is no point inserting the internal code for a special key.
4373 * It is more useful to insert the string "<KEY>" instead. This would
4374 * probably be useful in a text window too, but it would not be
4375 * vi-compatible (maybe there should be an option for it?) -- webb
4376 */
4377 if (gui.in_use)
4378 ++allow_keys;
4379#endif
4380#ifdef USE_ON_FLY_SCROLL
4381 dont_scroll = TRUE; /* disallow scrolling here */
4382#endif
4383 ++no_mapping; /* don't map the next key hits */
4384 cc = 0;
4385 i = 0;
4386 for (;;)
4387 {
4388 do
4389 nc = safe_vgetc();
4390 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4391 || nc == K_HOR_SCROLLBAR);
4392#ifdef FEAT_CMDL_INFO
4393 if (!(State & CMDLINE)
4394# ifdef FEAT_MBYTE
4395 && MB_BYTE2LEN_CHECK(nc) == 1
4396# endif
4397 )
4398 add_to_showcmd(nc);
4399#endif
4400 if (nc == 'x' || nc == 'X')
4401 hex = TRUE;
4402 else if (nc == 'o' || nc == 'O')
4403 octal = TRUE;
4404#ifdef FEAT_MBYTE
4405 else if (nc == 'u' || nc == 'U')
4406 unicode = nc;
4407#endif
4408 else
4409 {
4410 if (hex
4411#ifdef FEAT_MBYTE
4412 || unicode != 0
4413#endif
4414 )
4415 {
4416 if (!vim_isxdigit(nc))
4417 break;
4418 cc = cc * 16 + hex2nr(nc);
4419 }
4420 else if (octal)
4421 {
4422 if (nc < '0' || nc > '7')
4423 break;
4424 cc = cc * 8 + nc - '0';
4425 }
4426 else
4427 {
4428 if (!VIM_ISDIGIT(nc))
4429 break;
4430 cc = cc * 10 + nc - '0';
4431 }
4432
4433 ++i;
4434 }
4435
4436 if (cc > 255
4437#ifdef FEAT_MBYTE
4438 && unicode == 0
4439#endif
4440 )
4441 cc = 255; /* limit range to 0-255 */
4442 nc = 0;
4443
4444 if (hex) /* hex: up to two chars */
4445 {
4446 if (i >= 2)
4447 break;
4448 }
4449#ifdef FEAT_MBYTE
4450 else if (unicode) /* Unicode: up to four or eight chars */
4451 {
4452 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4453 break;
4454 }
4455#endif
4456 else if (i >= 3) /* decimal or octal: up to three chars */
4457 break;
4458 }
4459 if (i == 0) /* no number entered */
4460 {
4461 if (nc == K_ZERO) /* NUL is stored as NL */
4462 {
4463 cc = '\n';
4464 nc = 0;
4465 }
4466 else
4467 {
4468 cc = nc;
4469 nc = 0;
4470 }
4471 }
4472
4473 if (cc == 0) /* NUL is stored as NL */
4474 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004475#ifdef FEAT_MBYTE
4476 if (enc_dbcs && (cc & 0xff) == 0)
4477 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4478 second byte will cause trouble! */
4479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
4481 --no_mapping;
4482#ifdef FEAT_GUI
4483 if (gui.in_use)
4484 --allow_keys;
4485#endif
4486 if (nc)
4487 vungetc(nc);
4488 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4489 return cc;
4490}
4491
4492/*
4493 * Insert character, taking care of special keys and mod_mask
4494 */
4495 static void
4496insert_special(c, allow_modmask, ctrlv)
4497 int c;
4498 int allow_modmask;
4499 int ctrlv; /* c was typed after CTRL-V */
4500{
4501 char_u *p;
4502 int len;
4503
4504 /*
4505 * Special function key, translate into "<Key>". Up to the last '>' is
4506 * inserted with ins_str(), so as not to replace characters in replace
4507 * mode.
4508 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4509 * unless 'allow_modmask' is TRUE.
4510 */
4511#ifdef MACOS
4512 /* Command-key never produces a normal key */
4513 if (mod_mask & MOD_MASK_CMD)
4514 allow_modmask = TRUE;
4515#endif
4516 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4517 {
4518 p = get_special_key_name(c, mod_mask);
4519 len = (int)STRLEN(p);
4520 c = p[len - 1];
4521 if (len > 2)
4522 {
4523 if (stop_arrow() == FAIL)
4524 return;
4525 p[len - 1] = NUL;
4526 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004527 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 ctrlv = FALSE;
4529 }
4530 }
4531 if (stop_arrow() == OK)
4532 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4533}
4534
4535/*
4536 * Special characters in this context are those that need processing other
4537 * than the simple insertion that can be performed here. This includes ESC
4538 * which terminates the insert, and CR/NL which need special processing to
4539 * open up a new line. This routine tries to optimize insertions performed by
4540 * the "redo", "undo" or "put" commands, so it needs to know when it should
4541 * stop and defer processing to the "normal" mechanism.
4542 * '0' and '^' are special, because they can be followed by CTRL-D.
4543 */
4544#ifdef EBCDIC
4545# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4546#else
4547# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4548#endif
4549
4550#ifdef FEAT_MBYTE
4551# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4552#else
4553# define WHITECHAR(cc) vim_iswhite(cc)
4554#endif
4555
4556 void
4557insertchar(c, flags, second_indent)
4558 int c; /* character to insert or NUL */
4559 int flags; /* INSCHAR_FORMAT, etc. */
4560 int second_indent; /* indent for second line if >= 0 */
4561{
4562 int haveto_redraw = FALSE;
4563 int textwidth;
4564#ifdef FEAT_COMMENTS
4565 colnr_T leader_len;
4566 char_u *p;
4567 int no_leader = FALSE;
4568 int do_comments = (flags & INSCHAR_DO_COM);
4569#endif
4570 int fo_white_par;
4571 int first_line = TRUE;
4572 int fo_ins_blank;
4573#ifdef FEAT_MBYTE
4574 int fo_multibyte;
4575#endif
4576 int save_char = NUL;
4577 int cc;
4578
4579 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4580 fo_ins_blank = has_format_option(FO_INS_BLANK);
4581#ifdef FEAT_MBYTE
4582 fo_multibyte = has_format_option(FO_MBYTE_BREAK);
4583#endif
4584 fo_white_par = has_format_option(FO_WHITE_PAR);
4585
4586 /*
4587 * Try to break the line in two or more pieces when:
4588 * - Always do this if we have been called to do formatting only.
4589 * - Always do this when 'formatoptions' has the 'a' flag and the line
4590 * ends in white space.
4591 * - Otherwise:
4592 * - Don't do this if inserting a blank
4593 * - Don't do this if an existing character is being replaced, unless
4594 * we're in VREPLACE mode.
4595 * - Do this if the cursor is not on the line where insert started
4596 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4597 * before the insert.
4598 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4599 * before 'textwidth'
4600 */
4601 if (textwidth
4602 && ((flags & INSCHAR_FORMAT)
4603 || (!vim_iswhite(c)
4604 && !((State & REPLACE_FLAG)
4605#ifdef FEAT_VREPLACE
4606 && !(State & VREPLACE_FLAG)
4607#endif
4608 && *ml_get_cursor() != NUL)
4609 && (curwin->w_cursor.lnum != Insstart.lnum
4610 || ((!has_format_option(FO_INS_LONG)
4611 || Insstart_textlen <= (colnr_T)textwidth)
4612 && (!fo_ins_blank
4613 || Insstart_blank_vcol <= (colnr_T)textwidth
4614 ))))))
4615 {
4616 /*
4617 * When 'ai' is off we don't want a space under the cursor to be
4618 * deleted. Replace it with an 'x' temporarily.
4619 */
4620 if (!curbuf->b_p_ai)
4621 {
4622 cc = gchar_cursor();
4623 if (vim_iswhite(cc))
4624 {
4625 save_char = cc;
4626 pchar_cursor('x');
4627 }
4628 }
4629
4630 /*
4631 * Repeat breaking lines, until the current line is not too long.
4632 */
4633 while (!got_int)
4634 {
4635 int startcol; /* Cursor column at entry */
4636 int wantcol; /* column at textwidth border */
4637 int foundcol; /* column for start of spaces */
4638 int end_foundcol = 0; /* column for start of word */
4639 colnr_T len;
4640 colnr_T virtcol;
4641#ifdef FEAT_VREPLACE
4642 int orig_col = 0;
4643 char_u *saved_text = NULL;
4644#endif
4645 colnr_T col;
4646
4647 virtcol = get_nolist_virtcol();
4648 if (virtcol < (colnr_T)textwidth)
4649 break;
4650
4651#ifdef FEAT_COMMENTS
4652 if (no_leader)
4653 do_comments = FALSE;
4654 else if (!(flags & INSCHAR_FORMAT)
4655 && has_format_option(FO_WRAP_COMS))
4656 do_comments = TRUE;
4657
4658 /* Don't break until after the comment leader */
4659 if (do_comments)
4660 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
4661 else
4662 leader_len = 0;
4663
4664 /* If the line doesn't start with a comment leader, then don't
4665 * start one in a following broken line. Avoids that a %word
4666 * moved to the start of the next line causes all following lines
4667 * to start with %. */
4668 if (leader_len == 0)
4669 no_leader = TRUE;
4670#endif
4671 if (!(flags & INSCHAR_FORMAT)
4672#ifdef FEAT_COMMENTS
4673 && leader_len == 0
4674#endif
4675 && !has_format_option(FO_WRAP))
4676
4677 {
4678 textwidth = 0;
4679 break;
4680 }
4681 if ((startcol = curwin->w_cursor.col) == 0)
4682 break;
4683
4684 /* find column of textwidth border */
4685 coladvance((colnr_T)textwidth);
4686 wantcol = curwin->w_cursor.col;
4687
4688 curwin->w_cursor.col = startcol - 1;
4689#ifdef FEAT_MBYTE
4690 /* Correct cursor for multi-byte character. */
4691 if (has_mbyte)
4692 mb_adjust_cursor();
4693#endif
4694 foundcol = 0;
4695
4696 /*
4697 * Find position to break at.
4698 * Stop at first entered white when 'formatoptions' has 'v'
4699 */
4700 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
4701 || curwin->w_cursor.lnum != Insstart.lnum
4702 || curwin->w_cursor.col >= Insstart.col)
4703 {
4704 cc = gchar_cursor();
4705 if (WHITECHAR(cc))
4706 {
4707 /* remember position of blank just before text */
4708 end_foundcol = curwin->w_cursor.col;
4709
4710 /* find start of sequence of blanks */
4711 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
4712 {
4713 dec_cursor();
4714 cc = gchar_cursor();
4715 }
4716 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
4717 break; /* only spaces in front of text */
4718#ifdef FEAT_COMMENTS
4719 /* Don't break until after the comment leader */
4720 if (curwin->w_cursor.col < leader_len)
4721 break;
4722#endif
4723 if (has_format_option(FO_ONE_LETTER))
4724 {
4725 /* do not break after one-letter words */
4726 if (curwin->w_cursor.col == 0)
4727 break; /* one-letter word at begin */
4728
4729 col = curwin->w_cursor.col;
4730 dec_cursor();
4731 cc = gchar_cursor();
4732
4733 if (WHITECHAR(cc))
4734 continue; /* one-letter, continue */
4735 curwin->w_cursor.col = col;
4736 }
4737#ifdef FEAT_MBYTE
4738 if (has_mbyte)
4739 foundcol = curwin->w_cursor.col
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004740 + (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 else
4742#endif
4743 foundcol = curwin->w_cursor.col + 1;
4744 if (curwin->w_cursor.col < (colnr_T)wantcol)
4745 break;
4746 }
4747#ifdef FEAT_MBYTE
4748 else if (cc >= 0x100 && fo_multibyte
4749 && curwin->w_cursor.col <= (colnr_T)wantcol)
4750 {
4751 /* Break after or before a multi-byte character. */
4752 foundcol = curwin->w_cursor.col;
4753 if (curwin->w_cursor.col < (colnr_T)wantcol)
4754 foundcol += (*mb_char2len)(cc);
4755 end_foundcol = foundcol;
4756 break;
4757 }
4758#endif
4759 if (curwin->w_cursor.col == 0)
4760 break;
4761 dec_cursor();
4762 }
4763
4764 if (foundcol == 0) /* no spaces, cannot break line */
4765 {
4766 curwin->w_cursor.col = startcol;
4767 break;
4768 }
4769
4770 /* Going to break the line, remove any "$" now. */
4771 undisplay_dollar();
4772
4773 /*
4774 * Offset between cursor position and line break is used by replace
4775 * stack functions. VREPLACE does not use this, and backspaces
4776 * over the text instead.
4777 */
4778#ifdef FEAT_VREPLACE
4779 if (State & VREPLACE_FLAG)
4780 orig_col = startcol; /* Will start backspacing from here */
4781 else
4782#endif
4783 replace_offset = startcol - end_foundcol - 1;
4784
4785 /*
4786 * adjust startcol for spaces that will be deleted and
4787 * characters that will remain on top line
4788 */
4789 curwin->w_cursor.col = foundcol;
4790 while (cc = gchar_cursor(), WHITECHAR(cc))
4791 inc_cursor();
4792 startcol -= curwin->w_cursor.col;
4793 if (startcol < 0)
4794 startcol = 0;
4795
4796#ifdef FEAT_VREPLACE
4797 if (State & VREPLACE_FLAG)
4798 {
4799 /*
4800 * In VREPLACE mode, we will backspace over the text to be
4801 * wrapped, so save a copy now to put on the next line.
4802 */
4803 saved_text = vim_strsave(ml_get_cursor());
4804 curwin->w_cursor.col = orig_col;
4805 if (saved_text == NULL)
4806 break; /* Can't do it, out of memory */
4807 saved_text[startcol] = NUL;
4808
4809 /* Backspace over characters that will move to the next line */
4810 if (!fo_white_par)
4811 backspace_until_column(foundcol);
4812 }
4813 else
4814#endif
4815 {
4816 /* put cursor after pos. to break line */
4817 if (!fo_white_par)
4818 curwin->w_cursor.col = foundcol;
4819 }
4820
4821 /*
4822 * Split the line just before the margin.
4823 * Only insert/delete lines, but don't really redraw the window.
4824 */
4825 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
4826 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
4827#ifdef FEAT_COMMENTS
4828 + (do_comments ? OPENLINE_DO_COM : 0)
4829#endif
4830 , old_indent);
4831 old_indent = 0;
4832
4833 replace_offset = 0;
4834 if (first_line)
4835 {
4836 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
4837 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
4838 if (second_indent >= 0)
4839 {
4840#ifdef FEAT_VREPLACE
4841 if (State & VREPLACE_FLAG)
4842 change_indent(INDENT_SET, second_indent, FALSE, NUL);
4843 else
4844#endif
4845 (void)set_indent(second_indent, SIN_CHANGED);
4846 }
4847 first_line = FALSE;
4848 }
4849
4850#ifdef FEAT_VREPLACE
4851 if (State & VREPLACE_FLAG)
4852 {
4853 /*
4854 * In VREPLACE mode we have backspaced over the text to be
4855 * moved, now we re-insert it into the new line.
4856 */
4857 ins_bytes(saved_text);
4858 vim_free(saved_text);
4859 }
4860 else
4861#endif
4862 {
4863 /*
4864 * Check if cursor is not past the NUL off the line, cindent
4865 * may have added or removed indent.
4866 */
4867 curwin->w_cursor.col += startcol;
4868 len = (colnr_T)STRLEN(ml_get_curline());
4869 if (curwin->w_cursor.col > len)
4870 curwin->w_cursor.col = len;
4871 }
4872
4873 haveto_redraw = TRUE;
4874#ifdef FEAT_CINDENT
4875 can_cindent = TRUE;
4876#endif
4877 /* moved the cursor, don't autoindent or cindent now */
4878 did_ai = FALSE;
4879#ifdef FEAT_SMARTINDENT
4880 did_si = FALSE;
4881 can_si = FALSE;
4882 can_si_back = FALSE;
4883#endif
4884 line_breakcheck();
4885 }
4886
4887 if (save_char) /* put back space after cursor */
4888 pchar_cursor(save_char);
4889
4890 if (c == NUL) /* formatting only */
4891 return;
4892 if (haveto_redraw)
4893 {
4894 update_topline();
4895 redraw_curbuf_later(VALID);
4896 }
4897 }
4898 if (c == NUL) /* only formatting was wanted */
4899 return;
4900
4901#ifdef FEAT_COMMENTS
4902 /* Check whether this character should end a comment. */
4903 if (did_ai && (int)c == end_comment_pending)
4904 {
4905 char_u *line;
4906 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4907 int middle_len, end_len;
4908 int i;
4909
4910 /*
4911 * Need to remove existing (middle) comment leader and insert end
4912 * comment leader. First, check what comment leader we can find.
4913 */
4914 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4915 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4916 {
4917 /* Skip middle-comment string */
4918 while (*p && p[-1] != ':') /* find end of middle flags */
4919 ++p;
4920 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4921 /* Don't count trailing white space for middle_len */
4922 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4923 --middle_len;
4924
4925 /* Find the end-comment string */
4926 while (*p && p[-1] != ':') /* find end of end flags */
4927 ++p;
4928 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4929
4930 /* Skip white space before the cursor */
4931 i = curwin->w_cursor.col;
4932 while (--i >= 0 && vim_iswhite(line[i]))
4933 ;
4934 i++;
4935
4936 /* Skip to before the middle leader */
4937 i -= middle_len;
4938
4939 /* Check some expected things before we go on */
4940 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4941 {
4942 /* Backspace over all the stuff we want to replace */
4943 backspace_until_column(i);
4944
4945 /*
4946 * Insert the end-comment string, except for the last
4947 * character, which will get inserted as normal later.
4948 */
4949 ins_bytes_len(lead_end, end_len - 1);
4950 }
4951 }
4952 }
4953 end_comment_pending = NUL;
4954#endif
4955
4956 did_ai = FALSE;
4957#ifdef FEAT_SMARTINDENT
4958 did_si = FALSE;
4959 can_si = FALSE;
4960 can_si_back = FALSE;
4961#endif
4962
4963 /*
4964 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4965 * This speeds up normal text input considerably.
4966 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4967 * need to re-indent at a ':', or any other character (but not what
4968 * 'paste' is set)..
4969 */
4970#ifdef USE_ON_FLY_SCROLL
4971 dont_scroll = FALSE; /* allow scrolling here */
4972#endif
4973
4974 if ( !ISSPECIAL(c)
4975#ifdef FEAT_MBYTE
4976 && (!has_mbyte || (*mb_char2len)(c) == 1)
4977#endif
4978 && vpeekc() != NUL
4979 && !(State & REPLACE_FLAG)
4980#ifdef FEAT_CINDENT
4981 && !cindent_on()
4982#endif
4983#ifdef FEAT_RIGHTLEFT
4984 && !p_ri
4985#endif
4986 )
4987 {
4988#define INPUT_BUFLEN 100
4989 char_u buf[INPUT_BUFLEN + 1];
4990 int i;
4991 colnr_T virtcol = 0;
4992
4993 buf[0] = c;
4994 i = 1;
4995 if (textwidth)
4996 virtcol = get_nolist_virtcol();
4997 /*
4998 * Stop the string when:
4999 * - no more chars available
5000 * - finding a special character (command key)
5001 * - buffer is full
5002 * - running into the 'textwidth' boundary
5003 * - need to check for abbreviation: A non-word char after a word-char
5004 */
5005 while ( (c = vpeekc()) != NUL
5006 && !ISSPECIAL(c)
5007#ifdef FEAT_MBYTE
5008 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5009#endif
5010 && i < INPUT_BUFLEN
5011 && (textwidth == 0
5012 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5013 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5014 {
5015#ifdef FEAT_RIGHTLEFT
5016 c = vgetc();
5017 if (p_hkmap && KeyTyped)
5018 c = hkmap(c); /* Hebrew mode mapping */
5019# ifdef FEAT_FKMAP
5020 if (p_fkmap && KeyTyped)
5021 c = fkmap(c); /* Farsi mode mapping */
5022# endif
5023 buf[i++] = c;
5024#else
5025 buf[i++] = vgetc();
5026#endif
5027 }
5028
5029#ifdef FEAT_DIGRAPHS
5030 do_digraph(-1); /* clear digraphs */
5031 do_digraph(buf[i-1]); /* may be the start of a digraph */
5032#endif
5033 buf[i] = NUL;
5034 ins_str(buf);
5035 if (flags & INSCHAR_CTRLV)
5036 {
5037 redo_literal(*buf);
5038 i = 1;
5039 }
5040 else
5041 i = 0;
5042 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005043 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 }
5045 else
5046 {
5047#ifdef FEAT_MBYTE
5048 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5049 {
5050 char_u buf[MB_MAXBYTES + 1];
5051
5052 (*mb_char2bytes)(c, buf);
5053 buf[cc] = NUL;
5054 ins_char_bytes(buf, cc);
5055 AppendCharToRedobuff(c);
5056 }
5057 else
5058#endif
5059 {
5060 ins_char(c);
5061 if (flags & INSCHAR_CTRLV)
5062 redo_literal(c);
5063 else
5064 AppendCharToRedobuff(c);
5065 }
5066 }
5067}
5068
5069/*
5070 * Called after inserting or deleting text: When 'formatoptions' includes the
5071 * 'a' flag format from the current line until the end of the paragraph.
5072 * Keep the cursor at the same position relative to the text.
5073 * The caller must have saved the cursor line for undo, following ones will be
5074 * saved here.
5075 */
5076 void
5077auto_format(trailblank, prev_line)
5078 int trailblank; /* when TRUE also format with trailing blank */
5079 int prev_line; /* may start in previous line */
5080{
5081 pos_T pos;
5082 colnr_T len;
5083 char_u *old;
5084 char_u *new, *pnew;
5085 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005086 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087
5088 if (!has_format_option(FO_AUTO))
5089 return;
5090
5091 pos = curwin->w_cursor;
5092 old = ml_get_curline();
5093
5094 /* may remove added space */
5095 check_auto_format(FALSE);
5096
5097 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5098 * user might insert normal text next. Also skip formatting when "1" is
5099 * in 'formatoptions' and there is a single character before the cursor.
5100 * Otherwise the line would be broken and when typing another non-white
5101 * next they are not joined back together. */
5102 wasatend = (pos.col == STRLEN(old));
5103 if (*old != NUL && !trailblank && wasatend)
5104 {
5105 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005106 cc = gchar_cursor();
5107 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5108 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005110 cc = gchar_cursor();
5111 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 curwin->w_cursor = pos;
5114 return;
5115 }
5116 curwin->w_cursor = pos;
5117 }
5118
5119#ifdef FEAT_COMMENTS
5120 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5121 * comments. */
5122 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5123 && get_leader_len(old, NULL, FALSE) == 0)
5124 return;
5125#endif
5126
5127 /*
5128 * May start formatting in a previous line, so that after "x" a word is
5129 * moved to the previous line if it fits there now. Only when this is not
5130 * the start of a paragraph.
5131 */
5132 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5133 {
5134 --curwin->w_cursor.lnum;
5135 if (u_save_cursor() == FAIL)
5136 return;
5137 }
5138
5139 /*
5140 * Do the formatting and restore the cursor position. "saved_cursor" will
5141 * be adjusted for the text formatting.
5142 */
5143 saved_cursor = pos;
5144 format_lines((linenr_T)-1);
5145 curwin->w_cursor = saved_cursor;
5146 saved_cursor.lnum = 0;
5147
5148 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5149 {
5150 /* "cannot happen" */
5151 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5152 coladvance((colnr_T)MAXCOL);
5153 }
5154 else
5155 check_cursor_col();
5156
5157 /* Insert mode: If the cursor is now after the end of the line while it
5158 * previously wasn't, the line was broken. Because of the rule above we
5159 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5160 * formatted. */
5161 if (!wasatend && has_format_option(FO_WHITE_PAR))
5162 {
5163 new = ml_get_curline();
5164 len = STRLEN(new);
5165 if (curwin->w_cursor.col == len)
5166 {
5167 pnew = vim_strnsave(new, len + 2);
5168 pnew[len] = ' ';
5169 pnew[len + 1] = NUL;
5170 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5171 /* remove the space later */
5172 did_add_space = TRUE;
5173 }
5174 else
5175 /* may remove added space */
5176 check_auto_format(FALSE);
5177 }
5178
5179 check_cursor();
5180}
5181
5182/*
5183 * When an extra space was added to continue a paragraph for auto-formatting,
5184 * delete it now. The space must be under the cursor, just after the insert
5185 * position.
5186 */
5187 static void
5188check_auto_format(end_insert)
5189 int end_insert; /* TRUE when ending Insert mode */
5190{
5191 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005192 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193
5194 if (did_add_space)
5195 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005196 cc = gchar_cursor();
5197 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 /* Somehow the space was removed already. */
5199 did_add_space = FALSE;
5200 else
5201 {
5202 if (!end_insert)
5203 {
5204 inc_cursor();
5205 c = gchar_cursor();
5206 dec_cursor();
5207 }
5208 if (c != NUL)
5209 {
5210 /* The space is no longer at the end of the line, delete it. */
5211 del_char(FALSE);
5212 did_add_space = FALSE;
5213 }
5214 }
5215 }
5216}
5217
5218/*
5219 * Find out textwidth to be used for formatting:
5220 * if 'textwidth' option is set, use it
5221 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5222 * if invalid value, use 0.
5223 * Set default to window width (maximum 79) for "gq" operator.
5224 */
5225 int
5226comp_textwidth(ff)
5227 int ff; /* force formatting (for "Q" command) */
5228{
5229 int textwidth;
5230
5231 textwidth = curbuf->b_p_tw;
5232 if (textwidth == 0 && curbuf->b_p_wm)
5233 {
5234 /* The width is the window width minus 'wrapmargin' minus all the
5235 * things that add to the margin. */
5236 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5237#ifdef FEAT_CMDWIN
5238 if (cmdwin_type != 0)
5239 textwidth -= 1;
5240#endif
5241#ifdef FEAT_FOLDING
5242 textwidth -= curwin->w_p_fdc;
5243#endif
5244#ifdef FEAT_SIGNS
5245 if (curwin->w_buffer->b_signlist != NULL
5246# ifdef FEAT_NETBEANS_INTG
5247 || usingNetbeans
5248# endif
5249 )
5250 textwidth -= 1;
5251#endif
5252 if (curwin->w_p_nu)
5253 textwidth -= 8;
5254 }
5255 if (textwidth < 0)
5256 textwidth = 0;
5257 if (ff && textwidth == 0)
5258 {
5259 textwidth = W_WIDTH(curwin) - 1;
5260 if (textwidth > 79)
5261 textwidth = 79;
5262 }
5263 return textwidth;
5264}
5265
5266/*
5267 * Put a character in the redo buffer, for when just after a CTRL-V.
5268 */
5269 static void
5270redo_literal(c)
5271 int c;
5272{
5273 char_u buf[10];
5274
5275 /* Only digits need special treatment. Translate them into a string of
5276 * three digits. */
5277 if (VIM_ISDIGIT(c))
5278 {
5279 sprintf((char *)buf, "%03d", c);
5280 AppendToRedobuff(buf);
5281 }
5282 else
5283 AppendCharToRedobuff(c);
5284}
5285
5286/*
5287 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005288 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 */
5290 static void
5291start_arrow(end_insert_pos)
5292 pos_T *end_insert_pos;
5293{
5294 if (!arrow_used) /* something has been inserted */
5295 {
5296 AppendToRedobuff(ESC_STR);
5297 stop_insert(end_insert_pos, FALSE);
5298 arrow_used = TRUE; /* this means we stopped the current insert */
5299 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005300#ifdef FEAT_SYN_HL
5301 check_spell_redraw();
5302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303}
5304
Bram Moolenaar217ad922005-03-20 22:37:15 +00005305#ifdef FEAT_SYN_HL
5306/*
5307 * If we skipped highlighting word at cursor, do it now.
5308 * It may be skipped again, thus reset spell_redraw_lnum first.
5309 */
5310 static void
5311check_spell_redraw()
5312{
5313 if (spell_redraw_lnum != 0)
5314 {
5315 linenr_T lnum = spell_redraw_lnum;
5316
5317 spell_redraw_lnum = 0;
5318 redrawWinline(lnum, FALSE);
5319 }
5320}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005321
5322/*
5323 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5324 * spelled word, if there is one.
5325 */
5326 static void
5327spell_back_to_badword()
5328{
5329 pos_T tpos = curwin->w_cursor;
5330
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005331 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005332 if (curwin->w_cursor.col != tpos.col)
5333 start_arrow(&tpos);
5334}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005335#endif
5336
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337/*
5338 * stop_arrow() is called before a change is made in insert mode.
5339 * If an arrow key has been used, start a new insertion.
5340 * Returns FAIL if undo is impossible, shouldn't insert then.
5341 */
5342 int
5343stop_arrow()
5344{
5345 if (arrow_used)
5346 {
5347 if (u_save_cursor() == OK)
5348 {
5349 arrow_used = FALSE;
5350 ins_need_undo = FALSE;
5351 }
5352 Insstart = curwin->w_cursor; /* new insertion starts here */
5353 Insstart_textlen = linetabsize(ml_get_curline());
5354 ai_col = 0;
5355#ifdef FEAT_VREPLACE
5356 if (State & VREPLACE_FLAG)
5357 {
5358 orig_line_count = curbuf->b_ml.ml_line_count;
5359 vr_lines_changed = 1;
5360 }
5361#endif
5362 ResetRedobuff();
5363 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005364 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 }
5366 else if (ins_need_undo)
5367 {
5368 if (u_save_cursor() == OK)
5369 ins_need_undo = FALSE;
5370 }
5371
5372#ifdef FEAT_FOLDING
5373 /* Always open fold at the cursor line when inserting something. */
5374 foldOpenCursor();
5375#endif
5376
5377 return (arrow_used || ins_need_undo ? FAIL : OK);
5378}
5379
5380/*
5381 * do a few things to stop inserting
5382 */
5383 static void
5384stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005385 pos_T *end_insert_pos; /* where insert ended */
5386 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005388 int cc;
5389 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
5391 stop_redo_ins();
5392 replace_flush(); /* abandon replace stack */
5393
5394 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005395 * Save the inserted text for later redo with ^@ and CTRL-A.
5396 * Don't do it when "restart_edit" was set and nothing was inserted,
5397 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005399 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005400 if (did_restart_edit == 0 || (ptr != NULL
5401 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005402 {
5403 vim_free(last_insert);
5404 last_insert = ptr;
5405 last_insert_skip = new_insert_skip;
5406 }
5407 else
5408 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
5410 if (!arrow_used)
5411 {
5412 /* Auto-format now. It may seem strange to do this when stopping an
5413 * insertion (or moving the cursor), but it's required when appending
5414 * a line and having it end in a space. But only do it when something
5415 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005416 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005418 pos_T tpos = curwin->w_cursor;
5419
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 /* When the cursor is at the end of the line after a space the
5421 * formatting will move it to the following word. Avoid that by
5422 * moving the cursor onto the space. */
5423 cc = 'x';
5424 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5425 {
5426 dec_cursor();
5427 cc = gchar_cursor();
5428 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005429 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
5431
5432 auto_format(TRUE, FALSE);
5433
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005434 if (vim_iswhite(cc))
5435 {
5436 if (gchar_cursor() != NUL)
5437 inc_cursor();
5438#ifdef FEAT_VIRTUALEDIT
5439 /* If the cursor is still at the same character, also keep
5440 * the "coladd". */
5441 if (gchar_cursor() == NUL
5442 && curwin->w_cursor.lnum == tpos.lnum
5443 && curwin->w_cursor.col == tpos.col)
5444 curwin->w_cursor.coladd = tpos.coladd;
5445#endif
5446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 }
5448
5449 /* If a space was inserted for auto-formatting, remove it now. */
5450 check_auto_format(TRUE);
5451
5452 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005453 * of the line, and put the cursor back.
5454 * Do this when ESC was used or moving the cursor up/down. */
5455 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5456 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005458 pos_T tpos = curwin->w_cursor;
5459
5460 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5462 --curwin->w_cursor.col;
5463 while (cc = gchar_cursor(), vim_iswhite(cc))
5464 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005465 if (curwin->w_cursor.lnum != tpos.lnum)
5466 curwin->w_cursor = tpos;
5467 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5469
5470#ifdef FEAT_VISUAL
5471 /* <C-S-Right> may have started Visual mode, adjust the position for
5472 * deleted characters. */
5473 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5474 {
5475 cc = STRLEN(ml_get_curline());
5476 if (VIsual.col > (colnr_T)cc)
5477 {
5478 VIsual.col = cc;
5479# ifdef FEAT_VIRTUALEDIT
5480 VIsual.coladd = 0;
5481# endif
5482 }
5483 }
5484#endif
5485 }
5486 }
5487 did_ai = FALSE;
5488#ifdef FEAT_SMARTINDENT
5489 did_si = FALSE;
5490 can_si = FALSE;
5491 can_si_back = FALSE;
5492#endif
5493
5494 /* set '[ and '] to the inserted text */
5495 curbuf->b_op_start = Insstart;
5496 curbuf->b_op_end = *end_insert_pos;
5497}
5498
5499/*
5500 * Set the last inserted text to a single character.
5501 * Used for the replace command.
5502 */
5503 void
5504set_last_insert(c)
5505 int c;
5506{
5507 char_u *s;
5508
5509 vim_free(last_insert);
5510#ifdef FEAT_MBYTE
5511 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5512#else
5513 last_insert = alloc(6);
5514#endif
5515 if (last_insert != NULL)
5516 {
5517 s = last_insert;
5518 /* Use the CTRL-V only when entering a special char */
5519 if (c < ' ' || c == DEL)
5520 *s++ = Ctrl_V;
5521 s = add_char2buf(c, s);
5522 *s++ = ESC;
5523 *s++ = NUL;
5524 last_insert_skip = 0;
5525 }
5526}
5527
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005528#if defined(EXITFREE) || defined(PROTO)
5529 void
5530free_last_insert()
5531{
5532 vim_free(last_insert);
5533 last_insert = NULL;
5534}
5535#endif
5536
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537/*
5538 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5539 * and CSI. Handle multi-byte characters.
5540 * Returns a pointer to after the added bytes.
5541 */
5542 char_u *
5543add_char2buf(c, s)
5544 int c;
5545 char_u *s;
5546{
5547#ifdef FEAT_MBYTE
5548 char_u temp[MB_MAXBYTES];
5549 int i;
5550 int len;
5551
5552 len = (*mb_char2bytes)(c, temp);
5553 for (i = 0; i < len; ++i)
5554 {
5555 c = temp[i];
5556#endif
5557 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5558 if (c == K_SPECIAL)
5559 {
5560 *s++ = K_SPECIAL;
5561 *s++ = KS_SPECIAL;
5562 *s++ = KE_FILLER;
5563 }
5564#ifdef FEAT_GUI
5565 else if (c == CSI)
5566 {
5567 *s++ = CSI;
5568 *s++ = KS_EXTRA;
5569 *s++ = (int)KE_CSI;
5570 }
5571#endif
5572 else
5573 *s++ = c;
5574#ifdef FEAT_MBYTE
5575 }
5576#endif
5577 return s;
5578}
5579
5580/*
5581 * move cursor to start of line
5582 * if flags & BL_WHITE move to first non-white
5583 * if flags & BL_SOL move to first non-white if startofline is set,
5584 * otherwise keep "curswant" column
5585 * if flags & BL_FIX don't leave the cursor on a NUL.
5586 */
5587 void
5588beginline(flags)
5589 int flags;
5590{
5591 if ((flags & BL_SOL) && !p_sol)
5592 coladvance(curwin->w_curswant);
5593 else
5594 {
5595 curwin->w_cursor.col = 0;
5596#ifdef FEAT_VIRTUALEDIT
5597 curwin->w_cursor.coladd = 0;
5598#endif
5599
5600 if (flags & (BL_WHITE | BL_SOL))
5601 {
5602 char_u *ptr;
5603
5604 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5605 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5606 ++curwin->w_cursor.col;
5607 }
5608 curwin->w_set_curswant = TRUE;
5609 }
5610}
5611
5612/*
5613 * oneright oneleft cursor_down cursor_up
5614 *
5615 * Move one char {right,left,down,up}.
5616 * Doesn't move onto the NUL past the end of the line.
5617 * Return OK when successful, FAIL when we hit a line of file boundary.
5618 */
5619
5620 int
5621oneright()
5622{
5623 char_u *ptr;
5624#ifdef FEAT_MBYTE
5625 int l;
5626#endif
5627
5628#ifdef FEAT_VIRTUALEDIT
5629 if (virtual_active())
5630 {
5631 pos_T prevpos = curwin->w_cursor;
5632
5633 /* Adjust for multi-wide char (excluding TAB) */
5634 ptr = ml_get_cursor();
5635 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5636#ifdef FEAT_MBYTE
5637 (*mb_ptr2char)(ptr)
5638#else
5639 *ptr
5640#endif
5641 ))
5642 ? ptr2cells(ptr) : 1));
5643 curwin->w_set_curswant = TRUE;
5644 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5645 return (prevpos.col != curwin->w_cursor.col
5646 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5647 }
5648#endif
5649
5650 ptr = ml_get_cursor();
5651#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005652 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 {
5654 /* The character under the cursor is a multi-byte character, move
5655 * several bytes right, but don't end up on the NUL. */
5656 if (ptr[l] == NUL)
5657 return FAIL;
5658 curwin->w_cursor.col += l;
5659 }
5660 else
5661#endif
5662 {
5663 if (*ptr++ == NUL || *ptr == NUL)
5664 return FAIL;
5665 ++curwin->w_cursor.col;
5666 }
5667
5668 curwin->w_set_curswant = TRUE;
5669 return OK;
5670}
5671
5672 int
5673oneleft()
5674{
5675#ifdef FEAT_VIRTUALEDIT
5676 if (virtual_active())
5677 {
5678 int width;
5679 int v = getviscol();
5680
5681 if (v == 0)
5682 return FAIL;
5683
5684# ifdef FEAT_LINEBREAK
5685 /* We might get stuck on 'showbreak', skip over it. */
5686 width = 1;
5687 for (;;)
5688 {
5689 coladvance(v - width);
5690 /* getviscol() is slow, skip it when 'showbreak' is empty and
5691 * there are no multi-byte characters */
5692 if ((*p_sbr == NUL
5693# ifdef FEAT_MBYTE
5694 && !has_mbyte
5695# endif
5696 ) || getviscol() < v)
5697 break;
5698 ++width;
5699 }
5700# else
5701 coladvance(v - 1);
5702# endif
5703
5704 if (curwin->w_cursor.coladd == 1)
5705 {
5706 char_u *ptr;
5707
5708 /* Adjust for multi-wide char (not a TAB) */
5709 ptr = ml_get_cursor();
5710 if (*ptr != TAB && vim_isprintc(
5711# ifdef FEAT_MBYTE
5712 (*mb_ptr2char)(ptr)
5713# else
5714 *ptr
5715# endif
5716 ) && ptr2cells(ptr) > 1)
5717 curwin->w_cursor.coladd = 0;
5718 }
5719
5720 curwin->w_set_curswant = TRUE;
5721 return OK;
5722 }
5723#endif
5724
5725 if (curwin->w_cursor.col == 0)
5726 return FAIL;
5727
5728 curwin->w_set_curswant = TRUE;
5729 --curwin->w_cursor.col;
5730
5731#ifdef FEAT_MBYTE
5732 /* if the character on the left of the current cursor is a multi-byte
5733 * character, move to its first byte */
5734 if (has_mbyte)
5735 mb_adjust_cursor();
5736#endif
5737 return OK;
5738}
5739
5740 int
5741cursor_up(n, upd_topline)
5742 long n;
5743 int upd_topline; /* When TRUE: update topline */
5744{
5745 linenr_T lnum;
5746
5747 if (n > 0)
5748 {
5749 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005750 /* This fails if the cursor is already in the first line or the count
5751 * is larger than the line number and '-' is in 'cpoptions' */
5752 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 return FAIL;
5754 if (n >= lnum)
5755 lnum = 1;
5756 else
5757#ifdef FEAT_FOLDING
5758 if (hasAnyFolding(curwin))
5759 {
5760 /*
5761 * Count each sequence of folded lines as one logical line.
5762 */
5763 /* go to the the start of the current fold */
5764 (void)hasFolding(lnum, &lnum, NULL);
5765
5766 while (n--)
5767 {
5768 /* move up one line */
5769 --lnum;
5770 if (lnum <= 1)
5771 break;
5772 /* If we entered a fold, move to the beginning, unless in
5773 * Insert mode or when 'foldopen' contains "all": it will open
5774 * in a moment. */
5775 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
5776 (void)hasFolding(lnum, &lnum, NULL);
5777 }
5778 if (lnum < 1)
5779 lnum = 1;
5780 }
5781 else
5782#endif
5783 lnum -= n;
5784 curwin->w_cursor.lnum = lnum;
5785 }
5786
5787 /* try to advance to the column we want to be at */
5788 coladvance(curwin->w_curswant);
5789
5790 if (upd_topline)
5791 update_topline(); /* make sure curwin->w_topline is valid */
5792
5793 return OK;
5794}
5795
5796/*
5797 * Cursor down a number of logical lines.
5798 */
5799 int
5800cursor_down(n, upd_topline)
5801 long n;
5802 int upd_topline; /* When TRUE: update topline */
5803{
5804 linenr_T lnum;
5805
5806 if (n > 0)
5807 {
5808 lnum = curwin->w_cursor.lnum;
5809#ifdef FEAT_FOLDING
5810 /* Move to last line of fold, will fail if it's the end-of-file. */
5811 (void)hasFolding(lnum, NULL, &lnum);
5812#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00005813 /* This fails if the cursor is already in the last line or would move
5814 * beyound the last line and '-' is in 'cpoptions' */
5815 if (lnum >= curbuf->b_ml.ml_line_count
5816 || (lnum + n > curbuf->b_ml.ml_line_count
5817 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 return FAIL;
5819 if (lnum + n >= curbuf->b_ml.ml_line_count)
5820 lnum = curbuf->b_ml.ml_line_count;
5821 else
5822#ifdef FEAT_FOLDING
5823 if (hasAnyFolding(curwin))
5824 {
5825 linenr_T last;
5826
5827 /* count each sequence of folded lines as one logical line */
5828 while (n--)
5829 {
5830 if (hasFolding(lnum, NULL, &last))
5831 lnum = last + 1;
5832 else
5833 ++lnum;
5834 if (lnum >= curbuf->b_ml.ml_line_count)
5835 break;
5836 }
5837 if (lnum > curbuf->b_ml.ml_line_count)
5838 lnum = curbuf->b_ml.ml_line_count;
5839 }
5840 else
5841#endif
5842 lnum += n;
5843 curwin->w_cursor.lnum = lnum;
5844 }
5845
5846 /* try to advance to the column we want to be at */
5847 coladvance(curwin->w_curswant);
5848
5849 if (upd_topline)
5850 update_topline(); /* make sure curwin->w_topline is valid */
5851
5852 return OK;
5853}
5854
5855/*
5856 * Stuff the last inserted text in the read buffer.
5857 * Last_insert actually is a copy of the redo buffer, so we
5858 * first have to remove the command.
5859 */
5860 int
5861stuff_inserted(c, count, no_esc)
5862 int c; /* Command character to be inserted */
5863 long count; /* Repeat this many times */
5864 int no_esc; /* Don't add an ESC at the end */
5865{
5866 char_u *esc_ptr;
5867 char_u *ptr;
5868 char_u *last_ptr;
5869 char_u last = NUL;
5870
5871 ptr = get_last_insert();
5872 if (ptr == NULL)
5873 {
5874 EMSG(_(e_noinstext));
5875 return FAIL;
5876 }
5877
5878 /* may want to stuff the command character, to start Insert mode */
5879 if (c != NUL)
5880 stuffcharReadbuff(c);
5881 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
5882 *esc_ptr = NUL; /* remove the ESC */
5883
5884 /* when the last char is either "0" or "^" it will be quoted if no ESC
5885 * comes after it OR if it will inserted more than once and "ptr"
5886 * starts with ^D. -- Acevedo
5887 */
5888 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
5889 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
5890 && (no_esc || (*ptr == Ctrl_D && count > 1)))
5891 {
5892 last = *last_ptr;
5893 *last_ptr = NUL;
5894 }
5895
5896 do
5897 {
5898 stuffReadbuff(ptr);
5899 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
5900 if (last)
5901 stuffReadbuff((char_u *)(last == '0'
5902 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
5903 : IF_EB("\026^", CTRL_V_STR "^")));
5904 }
5905 while (--count > 0);
5906
5907 if (last)
5908 *last_ptr = last;
5909
5910 if (esc_ptr != NULL)
5911 *esc_ptr = ESC; /* put the ESC back */
5912
5913 /* may want to stuff a trailing ESC, to get out of Insert mode */
5914 if (!no_esc)
5915 stuffcharReadbuff(ESC);
5916
5917 return OK;
5918}
5919
5920 char_u *
5921get_last_insert()
5922{
5923 if (last_insert == NULL)
5924 return NULL;
5925 return last_insert + last_insert_skip;
5926}
5927
5928/*
5929 * Get last inserted string, and remove trailing <Esc>.
5930 * Returns pointer to allocated memory (must be freed) or NULL.
5931 */
5932 char_u *
5933get_last_insert_save()
5934{
5935 char_u *s;
5936 int len;
5937
5938 if (last_insert == NULL)
5939 return NULL;
5940 s = vim_strsave(last_insert + last_insert_skip);
5941 if (s != NULL)
5942 {
5943 len = (int)STRLEN(s);
5944 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
5945 s[len - 1] = NUL;
5946 }
5947 return s;
5948}
5949
5950/*
5951 * Check the word in front of the cursor for an abbreviation.
5952 * Called when the non-id character "c" has been entered.
5953 * When an abbreviation is recognized it is removed from the text and
5954 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
5955 */
5956 static int
5957echeck_abbr(c)
5958 int c;
5959{
5960 /* Don't check for abbreviation in paste mode, when disabled and just
5961 * after moving around with cursor keys. */
5962 if (p_paste || no_abbr || arrow_used)
5963 return FALSE;
5964
5965 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
5966 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
5967}
5968
5969/*
5970 * replace-stack functions
5971 *
5972 * When replacing characters, the replaced characters are remembered for each
5973 * new character. This is used to re-insert the old text when backspacing.
5974 *
5975 * There is a NUL headed list of characters for each character that is
5976 * currently in the file after the insertion point. When BS is used, one NUL
5977 * headed list is put back for the deleted character.
5978 *
5979 * For a newline, there are two NUL headed lists. One contains the characters
5980 * that the NL replaced. The extra one stores the characters after the cursor
5981 * that were deleted (always white space).
5982 *
5983 * Replace_offset is normally 0, in which case replace_push will add a new
5984 * character at the end of the stack. If replace_offset is not 0, that many
5985 * characters will be left on the stack above the newly inserted character.
5986 */
5987
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00005988static char_u *replace_stack = NULL;
5989static long replace_stack_nr = 0; /* next entry in replace stack */
5990static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991
5992 void
5993replace_push(c)
5994 int c; /* character that is replaced (NUL is none) */
5995{
5996 char_u *p;
5997
5998 if (replace_stack_nr < replace_offset) /* nothing to do */
5999 return;
6000 if (replace_stack_len <= replace_stack_nr)
6001 {
6002 replace_stack_len += 50;
6003 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6004 if (p == NULL) /* out of memory */
6005 {
6006 replace_stack_len -= 50;
6007 return;
6008 }
6009 if (replace_stack != NULL)
6010 {
6011 mch_memmove(p, replace_stack,
6012 (size_t)(replace_stack_nr * sizeof(char_u)));
6013 vim_free(replace_stack);
6014 }
6015 replace_stack = p;
6016 }
6017 p = replace_stack + replace_stack_nr - replace_offset;
6018 if (replace_offset)
6019 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6020 *p = c;
6021 ++replace_stack_nr;
6022}
6023
6024/*
6025 * call replace_push(c) with replace_offset set to the first NUL.
6026 */
6027 static void
6028replace_push_off(c)
6029 int c;
6030{
6031 char_u *p;
6032
6033 p = replace_stack + replace_stack_nr;
6034 for (replace_offset = 1; replace_offset < replace_stack_nr;
6035 ++replace_offset)
6036 if (*--p == NUL)
6037 break;
6038 replace_push(c);
6039 replace_offset = 0;
6040}
6041
6042/*
6043 * Pop one item from the replace stack.
6044 * return -1 if stack empty
6045 * return replaced character or NUL otherwise
6046 */
6047 static int
6048replace_pop()
6049{
6050 if (replace_stack_nr == 0)
6051 return -1;
6052 return (int)replace_stack[--replace_stack_nr];
6053}
6054
6055/*
6056 * Join the top two items on the replace stack. This removes to "off"'th NUL
6057 * encountered.
6058 */
6059 static void
6060replace_join(off)
6061 int off; /* offset for which NUL to remove */
6062{
6063 int i;
6064
6065 for (i = replace_stack_nr; --i >= 0; )
6066 if (replace_stack[i] == NUL && off-- <= 0)
6067 {
6068 --replace_stack_nr;
6069 mch_memmove(replace_stack + i, replace_stack + i + 1,
6070 (size_t)(replace_stack_nr - i));
6071 return;
6072 }
6073}
6074
6075/*
6076 * Pop bytes from the replace stack until a NUL is found, and insert them
6077 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6078 */
6079 static void
6080replace_pop_ins()
6081{
6082 int cc;
6083 int oldState = State;
6084
6085 State = NORMAL; /* don't want REPLACE here */
6086 while ((cc = replace_pop()) > 0)
6087 {
6088#ifdef FEAT_MBYTE
6089 mb_replace_pop_ins(cc);
6090#else
6091 ins_char(cc);
6092#endif
6093 dec_cursor();
6094 }
6095 State = oldState;
6096}
6097
6098#ifdef FEAT_MBYTE
6099/*
6100 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6101 * indicates a multi-byte char, pop the other bytes too.
6102 */
6103 static void
6104mb_replace_pop_ins(cc)
6105 int cc;
6106{
6107 int n;
6108 char_u buf[MB_MAXBYTES];
6109 int i;
6110 int c;
6111
6112 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6113 {
6114 buf[0] = cc;
6115 for (i = 1; i < n; ++i)
6116 buf[i] = replace_pop();
6117 ins_bytes_len(buf, n);
6118 }
6119 else
6120 ins_char(cc);
6121
6122 if (enc_utf8)
6123 /* Handle composing chars. */
6124 for (;;)
6125 {
6126 c = replace_pop();
6127 if (c == -1) /* stack empty */
6128 break;
6129 if ((n = MB_BYTE2LEN(c)) == 1)
6130 {
6131 /* Not a multi-byte char, put it back. */
6132 replace_push(c);
6133 break;
6134 }
6135 else
6136 {
6137 buf[0] = c;
6138 for (i = 1; i < n; ++i)
6139 buf[i] = replace_pop();
6140 if (utf_iscomposing(utf_ptr2char(buf)))
6141 ins_bytes_len(buf, n);
6142 else
6143 {
6144 /* Not a composing char, put it back. */
6145 for (i = n - 1; i >= 0; --i)
6146 replace_push(buf[i]);
6147 break;
6148 }
6149 }
6150 }
6151}
6152#endif
6153
6154/*
6155 * make the replace stack empty
6156 * (called when exiting replace mode)
6157 */
6158 static void
6159replace_flush()
6160{
6161 vim_free(replace_stack);
6162 replace_stack = NULL;
6163 replace_stack_len = 0;
6164 replace_stack_nr = 0;
6165}
6166
6167/*
6168 * Handle doing a BS for one character.
6169 * cc < 0: replace stack empty, just move cursor
6170 * cc == 0: character was inserted, delete it
6171 * cc > 0: character was replaced, put cc (first byte of original char) back
6172 * and check for more characters to be put back
6173 */
6174 static void
6175replace_do_bs()
6176{
6177 int cc;
6178#ifdef FEAT_VREPLACE
6179 int orig_len = 0;
6180 int ins_len;
6181 int orig_vcols = 0;
6182 colnr_T start_vcol;
6183 char_u *p;
6184 int i;
6185 int vcol;
6186#endif
6187
6188 cc = replace_pop();
6189 if (cc > 0)
6190 {
6191#ifdef FEAT_VREPLACE
6192 if (State & VREPLACE_FLAG)
6193 {
6194 /* Get the number of screen cells used by the character we are
6195 * going to delete. */
6196 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6197 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6198 }
6199#endif
6200#ifdef FEAT_MBYTE
6201 if (has_mbyte)
6202 {
6203 del_char(FALSE);
6204# ifdef FEAT_VREPLACE
6205 if (State & VREPLACE_FLAG)
6206 orig_len = STRLEN(ml_get_cursor());
6207# endif
6208 replace_push(cc);
6209 }
6210 else
6211#endif
6212 {
6213 pchar_cursor(cc);
6214#ifdef FEAT_VREPLACE
6215 if (State & VREPLACE_FLAG)
6216 orig_len = STRLEN(ml_get_cursor()) - 1;
6217#endif
6218 }
6219 replace_pop_ins();
6220
6221#ifdef FEAT_VREPLACE
6222 if (State & VREPLACE_FLAG)
6223 {
6224 /* Get the number of screen cells used by the inserted characters */
6225 p = ml_get_cursor();
6226 ins_len = STRLEN(p) - orig_len;
6227 vcol = start_vcol;
6228 for (i = 0; i < ins_len; ++i)
6229 {
6230 vcol += chartabsize(p + i, vcol);
6231#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006232 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233#endif
6234 }
6235 vcol -= start_vcol;
6236
6237 /* Delete spaces that were inserted after the cursor to keep the
6238 * text aligned. */
6239 curwin->w_cursor.col += ins_len;
6240 while (vcol > orig_vcols && gchar_cursor() == ' ')
6241 {
6242 del_char(FALSE);
6243 ++orig_vcols;
6244 }
6245 curwin->w_cursor.col -= ins_len;
6246 }
6247#endif
6248
6249 /* mark the buffer as changed and prepare for displaying */
6250 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6251 }
6252 else if (cc == 0)
6253 (void)del_char(FALSE);
6254}
6255
6256#ifdef FEAT_CINDENT
6257/*
6258 * Return TRUE if C-indenting is on.
6259 */
6260 static int
6261cindent_on()
6262{
6263 return (!p_paste && (curbuf->b_p_cin
6264# ifdef FEAT_EVAL
6265 || *curbuf->b_p_inde != NUL
6266# endif
6267 ));
6268}
6269#endif
6270
6271#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6272/*
6273 * Re-indent the current line, based on the current contents of it and the
6274 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6275 * confused what all the part that handles Control-T is doing that I'm not.
6276 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6277 */
6278
6279 void
6280fixthisline(get_the_indent)
6281 int (*get_the_indent) __ARGS((void));
6282{
6283 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6284 if (linewhite(curwin->w_cursor.lnum))
6285 did_ai = TRUE; /* delete the indent if the line stays empty */
6286}
6287
6288 void
6289fix_indent()
6290{
6291 if (p_paste)
6292 return;
6293# ifdef FEAT_LISP
6294 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6295 fixthisline(get_lisp_indent);
6296# endif
6297# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6298 else
6299# endif
6300# ifdef FEAT_CINDENT
6301 if (cindent_on())
6302 do_c_expr_indent();
6303# endif
6304}
6305
6306#endif
6307
6308#ifdef FEAT_CINDENT
6309/*
6310 * return TRUE if 'cinkeys' contains the key "keytyped",
6311 * when == '*': Only if key is preceded with '*' (indent before insert)
6312 * when == '!': Only if key is prededed with '!' (don't insert)
6313 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6314 *
6315 * "keytyped" can have a few special values:
6316 * KEY_OPEN_FORW
6317 * KEY_OPEN_BACK
6318 * KEY_COMPLETE just finished completion.
6319 *
6320 * If line_is_empty is TRUE accept keys with '0' before them.
6321 */
6322 int
6323in_cinkeys(keytyped, when, line_is_empty)
6324 int keytyped;
6325 int when;
6326 int line_is_empty;
6327{
6328 char_u *look;
6329 int try_match;
6330 int try_match_word;
6331 char_u *p;
6332 char_u *line;
6333 int icase;
6334 int i;
6335
6336#ifdef FEAT_EVAL
6337 if (*curbuf->b_p_inde != NUL)
6338 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6339 else
6340#endif
6341 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6342 while (*look)
6343 {
6344 /*
6345 * Find out if we want to try a match with this key, depending on
6346 * 'when' and a '*' or '!' before the key.
6347 */
6348 switch (when)
6349 {
6350 case '*': try_match = (*look == '*'); break;
6351 case '!': try_match = (*look == '!'); break;
6352 default: try_match = (*look != '*'); break;
6353 }
6354 if (*look == '*' || *look == '!')
6355 ++look;
6356
6357 /*
6358 * If there is a '0', only accept a match if the line is empty.
6359 * But may still match when typing last char of a word.
6360 */
6361 if (*look == '0')
6362 {
6363 try_match_word = try_match;
6364 if (!line_is_empty)
6365 try_match = FALSE;
6366 ++look;
6367 }
6368 else
6369 try_match_word = FALSE;
6370
6371 /*
6372 * does it look like a control character?
6373 */
6374 if (*look == '^'
6375#ifdef EBCDIC
6376 && (Ctrl_chr(look[1]) != 0)
6377#else
6378 && look[1] >= '?' && look[1] <= '_'
6379#endif
6380 )
6381 {
6382 if (try_match && keytyped == Ctrl_chr(look[1]))
6383 return TRUE;
6384 look += 2;
6385 }
6386 /*
6387 * 'o' means "o" command, open forward.
6388 * 'O' means "O" command, open backward.
6389 */
6390 else if (*look == 'o')
6391 {
6392 if (try_match && keytyped == KEY_OPEN_FORW)
6393 return TRUE;
6394 ++look;
6395 }
6396 else if (*look == 'O')
6397 {
6398 if (try_match && keytyped == KEY_OPEN_BACK)
6399 return TRUE;
6400 ++look;
6401 }
6402
6403 /*
6404 * 'e' means to check for "else" at start of line and just before the
6405 * cursor.
6406 */
6407 else if (*look == 'e')
6408 {
6409 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6410 {
6411 p = ml_get_curline();
6412 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6413 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6414 return TRUE;
6415 }
6416 ++look;
6417 }
6418
6419 /*
6420 * ':' only causes an indent if it is at the end of a label or case
6421 * statement, or when it was before typing the ':' (to fix
6422 * class::method for C++).
6423 */
6424 else if (*look == ':')
6425 {
6426 if (try_match && keytyped == ':')
6427 {
6428 p = ml_get_curline();
6429 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6430 return TRUE;
6431 if (curwin->w_cursor.col > 2
6432 && p[curwin->w_cursor.col - 1] == ':'
6433 && p[curwin->w_cursor.col - 2] == ':')
6434 {
6435 p[curwin->w_cursor.col - 1] = ' ';
6436 i = (cin_iscase(p) || cin_isscopedecl(p)
6437 || cin_islabel(30));
6438 p = ml_get_curline();
6439 p[curwin->w_cursor.col - 1] = ':';
6440 if (i)
6441 return TRUE;
6442 }
6443 }
6444 ++look;
6445 }
6446
6447
6448 /*
6449 * Is it a key in <>, maybe?
6450 */
6451 else if (*look == '<')
6452 {
6453 if (try_match)
6454 {
6455 /*
6456 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6457 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6458 * >, *, : and ! keys if they really really want to.
6459 */
6460 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6461 && keytyped == look[1])
6462 return TRUE;
6463
6464 if (keytyped == get_special_key_code(look + 1))
6465 return TRUE;
6466 }
6467 while (*look && *look != '>')
6468 look++;
6469 while (*look == '>')
6470 look++;
6471 }
6472
6473 /*
6474 * Is it a word: "=word"?
6475 */
6476 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6477 {
6478 ++look;
6479 if (*look == '~')
6480 {
6481 icase = TRUE;
6482 ++look;
6483 }
6484 else
6485 icase = FALSE;
6486 p = vim_strchr(look, ',');
6487 if (p == NULL)
6488 p = look + STRLEN(look);
6489 if ((try_match || try_match_word)
6490 && curwin->w_cursor.col >= (colnr_T)(p - look))
6491 {
6492 int match = FALSE;
6493
6494#ifdef FEAT_INS_EXPAND
6495 if (keytyped == KEY_COMPLETE)
6496 {
6497 char_u *s;
6498
6499 /* Just completed a word, check if it starts with "look".
6500 * search back for the start of a word. */
6501 line = ml_get_curline();
6502# ifdef FEAT_MBYTE
6503 if (has_mbyte)
6504 {
6505 char_u *n;
6506
6507 for (s = line + curwin->w_cursor.col; s > line; s = n)
6508 {
6509 n = mb_prevptr(line, s);
6510 if (!vim_iswordp(n))
6511 break;
6512 }
6513 }
6514 else
6515# endif
6516 for (s = line + curwin->w_cursor.col; s > line; --s)
6517 if (!vim_iswordc(s[-1]))
6518 break;
6519 if (s + (p - look) <= line + curwin->w_cursor.col
6520 && (icase
6521 ? MB_STRNICMP(s, look, p - look)
6522 : STRNCMP(s, look, p - look)) == 0)
6523 match = TRUE;
6524 }
6525 else
6526#endif
6527 /* TODO: multi-byte */
6528 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6529 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6530 {
6531 line = ml_get_cursor();
6532 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6533 || !vim_iswordc(line[-(p - look) - 1]))
6534 && (icase
6535 ? MB_STRNICMP(line - (p - look), look, p - look)
6536 : STRNCMP(line - (p - look), look, p - look))
6537 == 0)
6538 match = TRUE;
6539 }
6540 if (match && try_match_word && !try_match)
6541 {
6542 /* "0=word": Check if there are only blanks before the
6543 * word. */
6544 line = ml_get_curline();
6545 if ((int)(skipwhite(line) - line) !=
6546 (int)(curwin->w_cursor.col - (p - look)))
6547 match = FALSE;
6548 }
6549 if (match)
6550 return TRUE;
6551 }
6552 look = p;
6553 }
6554
6555 /*
6556 * ok, it's a boring generic character.
6557 */
6558 else
6559 {
6560 if (try_match && *look == keytyped)
6561 return TRUE;
6562 ++look;
6563 }
6564
6565 /*
6566 * Skip over ", ".
6567 */
6568 look = skip_to_option_part(look);
6569 }
6570 return FALSE;
6571}
6572#endif /* FEAT_CINDENT */
6573
6574#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6575/*
6576 * Map Hebrew keyboard when in hkmap mode.
6577 */
6578 int
6579hkmap(c)
6580 int c;
6581{
6582 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6583 {
6584 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6585 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6586 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6587 static char_u map[26] =
6588 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6589 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6590 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6591 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6592 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6593 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6594 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6595 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6596 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6597
6598 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6599 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6600 /* '-1'='sofit' */
6601 else if (c == 'x')
6602 return 'X';
6603 else if (c == 'q')
6604 return '\''; /* {geresh}={'} */
6605 else if (c == 246)
6606 return ' '; /* \"o --> ' ' for a german keyboard */
6607 else if (c == 228)
6608 return ' '; /* \"a --> ' ' -- / -- */
6609 else if (c == 252)
6610 return ' '; /* \"u --> ' ' -- / -- */
6611#ifdef EBCDIC
6612 else if (islower(c))
6613#else
6614 /* NOTE: islower() does not do the right thing for us on Linux so we
6615 * do this the same was as 5.7 and previous, so it works correctly on
6616 * all systems. Specifically, the e.g. Delete and Arrow keys are
6617 * munged and won't work if e.g. searching for Hebrew text.
6618 */
6619 else if (c >= 'a' && c <= 'z')
6620#endif
6621 return (int)(map[CharOrdLow(c)] + p_aleph);
6622 else
6623 return c;
6624 }
6625 else
6626 {
6627 switch (c)
6628 {
6629 case '`': return ';';
6630 case '/': return '.';
6631 case '\'': return ',';
6632 case 'q': return '/';
6633 case 'w': return '\'';
6634
6635 /* Hebrew letters - set offset from 'a' */
6636 case ',': c = '{'; break;
6637 case '.': c = 'v'; break;
6638 case ';': c = 't'; break;
6639 default: {
6640 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6641
6642#ifdef EBCDIC
6643 /* see note about islower() above */
6644 if (!islower(c))
6645#else
6646 if (c < 'a' || c > 'z')
6647#endif
6648 return c;
6649 c = str[CharOrdLow(c)];
6650 break;
6651 }
6652 }
6653
6654 return (int)(CharOrdLow(c) + p_aleph);
6655 }
6656}
6657#endif
6658
6659 static void
6660ins_reg()
6661{
6662 int need_redraw = FALSE;
6663 int regname;
6664 int literally = 0;
6665
6666 /*
6667 * If we are going to wait for a character, show a '"'.
6668 */
6669 pc_status = PC_STATUS_UNSET;
6670 if (redrawing() && !char_avail())
6671 {
6672 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00006673 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674
6675 edit_putchar('"', TRUE);
6676#ifdef FEAT_CMDL_INFO
6677 add_to_showcmd_c(Ctrl_R);
6678#endif
6679 }
6680
6681#ifdef USE_ON_FLY_SCROLL
6682 dont_scroll = TRUE; /* disallow scrolling here */
6683#endif
6684
6685 /*
6686 * Don't map the register name. This also prevents the mode message to be
6687 * deleted when ESC is hit.
6688 */
6689 ++no_mapping;
6690 regname = safe_vgetc();
6691#ifdef FEAT_LANGMAP
6692 LANGMAP_ADJUST(regname, TRUE);
6693#endif
6694 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6695 {
6696 /* Get a third key for literal register insertion */
6697 literally = regname;
6698#ifdef FEAT_CMDL_INFO
6699 add_to_showcmd_c(literally);
6700#endif
6701 regname = safe_vgetc();
6702#ifdef FEAT_LANGMAP
6703 LANGMAP_ADJUST(regname, TRUE);
6704#endif
6705 }
6706 --no_mapping;
6707
6708#ifdef FEAT_EVAL
6709 /*
6710 * Don't call u_sync() while getting the expression,
6711 * evaluating it or giving an error message for it!
6712 */
6713 ++no_u_sync;
6714 if (regname == '=')
6715 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006716# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006720# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 /* Restore the Input Method. */
6722 if (im_on)
6723 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006724# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006726 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6727 {
6728 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 else
6732 {
6733#endif
6734 if (literally == Ctrl_O || literally == Ctrl_P)
6735 {
6736 /* Append the command to the redo buffer. */
6737 AppendCharToRedobuff(Ctrl_R);
6738 AppendCharToRedobuff(literally);
6739 AppendCharToRedobuff(regname);
6740
6741 do_put(regname, BACKWARD, 1L,
6742 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6743 }
6744 else if (insert_reg(regname, literally) == FAIL)
6745 {
6746 vim_beep();
6747 need_redraw = TRUE; /* remove the '"' */
6748 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006749 else if (stop_insert_mode)
6750 /* When the '=' register was used and a function was invoked that
6751 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
6752 * insert anything, need to remove the '"' */
6753 need_redraw = TRUE;
6754
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755#ifdef FEAT_EVAL
6756 }
6757 --no_u_sync;
6758#endif
6759#ifdef FEAT_CMDL_INFO
6760 clear_showcmd();
6761#endif
6762
6763 /* If the inserted register is empty, we need to remove the '"' */
6764 if (need_redraw || stuff_empty())
6765 edit_unputchar();
6766}
6767
6768/*
6769 * CTRL-G commands in Insert mode.
6770 */
6771 static void
6772ins_ctrl_g()
6773{
6774 int c;
6775
6776#ifdef FEAT_INS_EXPAND
6777 /* Right after CTRL-X the cursor will be after the ruler. */
6778 setcursor();
6779#endif
6780
6781 /*
6782 * Don't map the second key. This also prevents the mode message to be
6783 * deleted when ESC is hit.
6784 */
6785 ++no_mapping;
6786 c = safe_vgetc();
6787 --no_mapping;
6788 switch (c)
6789 {
6790 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
6791 case K_UP:
6792 case Ctrl_K:
6793 case 'k': ins_up(TRUE);
6794 break;
6795
6796 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
6797 case K_DOWN:
6798 case Ctrl_J:
6799 case 'j': ins_down(TRUE);
6800 break;
6801
6802 /* CTRL-G u: start new undoable edit */
6803 case 'u': u_sync();
6804 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006805
6806 /* Need to reset Insstart, esp. because a BS that joins
6807 * aline to the previous one must save for undo. */
6808 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 break;
6810
6811 /* Unknown CTRL-G command, reserved for future expansion. */
6812 default: vim_beep();
6813 }
6814}
6815
6816/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006817 * CTRL-^ in Insert mode.
6818 */
6819 static void
6820ins_ctrl_hat()
6821{
6822 if (map_to_exists_mode((char_u *)"", LANGMAP))
6823 {
6824 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
6825 if (State & LANGMAP)
6826 {
6827 curbuf->b_p_iminsert = B_IMODE_NONE;
6828 State &= ~LANGMAP;
6829 }
6830 else
6831 {
6832 curbuf->b_p_iminsert = B_IMODE_LMAP;
6833 State |= LANGMAP;
6834#ifdef USE_IM_CONTROL
6835 im_set_active(FALSE);
6836#endif
6837 }
6838 }
6839#ifdef USE_IM_CONTROL
6840 else
6841 {
6842 /* There are no ":lmap" mappings, toggle IM */
6843 if (im_get_status())
6844 {
6845 curbuf->b_p_iminsert = B_IMODE_NONE;
6846 im_set_active(FALSE);
6847 }
6848 else
6849 {
6850 curbuf->b_p_iminsert = B_IMODE_IM;
6851 State &= ~LANGMAP;
6852 im_set_active(TRUE);
6853 }
6854 }
6855#endif
6856 set_iminsert_global();
6857 showmode();
6858#ifdef FEAT_GUI
6859 /* may show different cursor shape or color */
6860 if (gui.in_use)
6861 gui_update_cursor(TRUE, FALSE);
6862#endif
6863#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6864 /* Show/unshow value of 'keymap' in status lines. */
6865 status_redraw_curbuf();
6866#endif
6867}
6868
6869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 * Handle ESC in insert mode.
6871 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
6872 * insert.
6873 */
6874 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00006875ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 long *count;
6877 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00006878 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879{
6880 int temp;
6881 static int disabled_redraw = FALSE;
6882
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006883#ifdef FEAT_SYN_HL
6884 check_spell_redraw();
6885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886#if defined(FEAT_HANGULIN)
6887# if defined(ESC_CHG_TO_ENG_MODE)
6888 hangul_input_state_set(0);
6889# endif
6890 if (composing_hangul)
6891 {
6892 push_raw_key(composing_hangul_buffer, 2);
6893 composing_hangul = 0;
6894 }
6895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896
6897 temp = curwin->w_cursor.col;
6898 if (disabled_redraw)
6899 {
6900 --RedrawingDisabled;
6901 disabled_redraw = FALSE;
6902 }
6903 if (!arrow_used)
6904 {
6905 /*
6906 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00006907 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
6908 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 */
6910 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00006911 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912
6913 /*
6914 * Repeating insert may take a long time. Check for
6915 * interrupt now and then.
6916 */
6917 if (*count > 0)
6918 {
6919 line_breakcheck();
6920 if (got_int)
6921 *count = 0;
6922 }
6923
6924 if (--*count > 0) /* repeat what was typed */
6925 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006926 /* Vi repeats the insert without replacing characters. */
6927 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
6928 State &= ~REPLACE_FLAG;
6929
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 (void)start_redo_ins();
6931 if (cmdchar == 'r' || cmdchar == 'v')
6932 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
6933 ++RedrawingDisabled;
6934 disabled_redraw = TRUE;
6935 return FALSE; /* repeat the insert */
6936 }
6937 stop_insert(&curwin->w_cursor, TRUE);
6938 undisplay_dollar();
6939 }
6940
6941 /* When an autoindent was removed, curswant stays after the
6942 * indent */
6943 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
6944 curwin->w_set_curswant = TRUE;
6945
6946 /* Remember the last Insert position in the '^ mark. */
6947 if (!cmdmod.keepjumps)
6948 curbuf->b_last_insert = curwin->w_cursor;
6949
6950 /*
6951 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00006952 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00006954 if (!nomove
6955 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956#ifdef FEAT_VIRTUALEDIT
6957 || curwin->w_cursor.coladd > 0
6958#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006959 )
6960 && (restart_edit == NUL
6961 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00006963 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00006965 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966#ifdef FEAT_RIGHTLEFT
6967 && !revins_on
6968#endif
6969 )
6970 {
6971#ifdef FEAT_VIRTUALEDIT
6972 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
6973 {
6974 oneleft();
6975 if (restart_edit != NUL)
6976 ++curwin->w_cursor.coladd;
6977 }
6978 else
6979#endif
6980 {
6981 --curwin->w_cursor.col;
6982#ifdef FEAT_MBYTE
6983 /* Correct cursor for multi-byte character. */
6984 if (has_mbyte)
6985 mb_adjust_cursor();
6986#endif
6987 }
6988 }
6989
6990#ifdef USE_IM_CONTROL
6991 /* Disable IM to allow typing English directly for Normal mode commands.
6992 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
6993 * well). */
6994 if (!(State & LANGMAP))
6995 im_save_status(&curbuf->b_p_iminsert);
6996 im_set_active(FALSE);
6997#endif
6998
6999 State = NORMAL;
7000 /* need to position cursor again (e.g. when on a TAB ) */
7001 changed_cline_bef_curs();
7002
7003#ifdef FEAT_MOUSE
7004 setmouse();
7005#endif
7006#ifdef CURSOR_SHAPE
7007 ui_cursor_shape(); /* may show different cursor shape */
7008#endif
7009
7010 /*
7011 * When recording or for CTRL-O, need to display the new mode.
7012 * Otherwise remove the mode message.
7013 */
7014 if (Recording || restart_edit != NUL)
7015 showmode();
7016 else if (p_smd)
7017 MSG("");
7018
7019 return TRUE; /* exit Insert mode */
7020}
7021
7022#ifdef FEAT_RIGHTLEFT
7023/*
7024 * Toggle language: hkmap and revins_on.
7025 * Move to end of reverse inserted text.
7026 */
7027 static void
7028ins_ctrl_()
7029{
7030 if (revins_on && revins_chars && revins_scol >= 0)
7031 {
7032 while (gchar_cursor() != NUL && revins_chars--)
7033 ++curwin->w_cursor.col;
7034 }
7035 p_ri = !p_ri;
7036 revins_on = (State == INSERT && p_ri);
7037 if (revins_on)
7038 {
7039 revins_scol = curwin->w_cursor.col;
7040 revins_legal++;
7041 revins_chars = 0;
7042 undisplay_dollar();
7043 }
7044 else
7045 revins_scol = -1;
7046#ifdef FEAT_FKMAP
7047 if (p_altkeymap)
7048 {
7049 /*
7050 * to be consistent also for redo command, using '.'
7051 * set arrow_used to true and stop it - causing to redo
7052 * characters entered in one mode (normal/reverse insert).
7053 */
7054 arrow_used = TRUE;
7055 (void)stop_arrow();
7056 p_fkmap = curwin->w_p_rl ^ p_ri;
7057 if (p_fkmap && p_ri)
7058 State = INSERT;
7059 }
7060 else
7061#endif
7062 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7063 showmode();
7064}
7065#endif
7066
7067#ifdef FEAT_VISUAL
7068/*
7069 * If 'keymodel' contains "startsel", may start selection.
7070 * Returns TRUE when a CTRL-O and other keys stuffed.
7071 */
7072 static int
7073ins_start_select(c)
7074 int c;
7075{
7076 if (km_startsel)
7077 switch (c)
7078 {
7079 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 case K_PAGEUP:
7082 case K_KPAGEUP:
7083 case K_PAGEDOWN:
7084 case K_KPAGEDOWN:
7085# ifdef MACOS
7086 case K_LEFT:
7087 case K_RIGHT:
7088 case K_UP:
7089 case K_DOWN:
7090 case K_END:
7091 case K_HOME:
7092# endif
7093 if (!(mod_mask & MOD_MASK_SHIFT))
7094 break;
7095 /* FALLTHROUGH */
7096 case K_S_LEFT:
7097 case K_S_RIGHT:
7098 case K_S_UP:
7099 case K_S_DOWN:
7100 case K_S_END:
7101 case K_S_HOME:
7102 /* Start selection right away, the cursor can move with
7103 * CTRL-O when beyond the end of the line. */
7104 start_selection();
7105
7106 /* Execute the key in (insert) Select mode. */
7107 stuffcharReadbuff(Ctrl_O);
7108 if (mod_mask)
7109 {
7110 char_u buf[4];
7111
7112 buf[0] = K_SPECIAL;
7113 buf[1] = KS_MODIFIER;
7114 buf[2] = mod_mask;
7115 buf[3] = NUL;
7116 stuffReadbuff(buf);
7117 }
7118 stuffcharReadbuff(c);
7119 return TRUE;
7120 }
7121 return FALSE;
7122}
7123#endif
7124
7125/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007126 * <Insert> key in Insert mode: toggle insert/remplace mode.
7127 */
7128 static void
7129ins_insert(replaceState)
7130 int replaceState;
7131{
7132#ifdef FEAT_FKMAP
7133 if (p_fkmap && p_ri)
7134 {
7135 beep_flush();
7136 EMSG(farsi_text_3); /* encoded in Farsi */
7137 return;
7138 }
7139#endif
7140
7141#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007142# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007143 set_vim_var_string(VV_INSERTMODE,
7144 (char_u *)((State & REPLACE_FLAG) ? "i" :
7145 replaceState == VREPLACE ? "v" : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007146# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007147 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7148#endif
7149 if (State & REPLACE_FLAG)
7150 State = INSERT | (State & LANGMAP);
7151 else
7152 State = replaceState | (State & LANGMAP);
7153 AppendCharToRedobuff(K_INS);
7154 showmode();
7155#ifdef CURSOR_SHAPE
7156 ui_cursor_shape(); /* may show different cursor shape */
7157#endif
7158}
7159
7160/*
7161 * Pressed CTRL-O in Insert mode.
7162 */
7163 static void
7164ins_ctrl_o()
7165{
7166#ifdef FEAT_VREPLACE
7167 if (State & VREPLACE_FLAG)
7168 restart_edit = 'V';
7169 else
7170#endif
7171 if (State & REPLACE_FLAG)
7172 restart_edit = 'R';
7173 else
7174 restart_edit = 'I';
7175#ifdef FEAT_VIRTUALEDIT
7176 if (virtual_active())
7177 ins_at_eol = FALSE; /* cursor always keeps its column */
7178 else
7179#endif
7180 ins_at_eol = (gchar_cursor() == NUL);
7181}
7182
7183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 * If the cursor is on an indent, ^T/^D insert/delete one
7185 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7186 * Always round the indent to 'shiftwith', this is compatible
7187 * with vi. But vi only supports ^T and ^D after an
7188 * autoindent, we support it everywhere.
7189 */
7190 static void
7191ins_shift(c, lastc)
7192 int c;
7193 int lastc;
7194{
7195 if (stop_arrow() == FAIL)
7196 return;
7197 AppendCharToRedobuff(c);
7198
7199 /*
7200 * 0^D and ^^D: remove all indent.
7201 */
7202 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7203 {
7204 --curwin->w_cursor.col;
7205 (void)del_char(FALSE); /* delete the '^' or '0' */
7206 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7207 if (State & REPLACE_FLAG)
7208 replace_pop_ins();
7209 if (lastc == '^')
7210 old_indent = get_indent(); /* remember curr. indent */
7211 change_indent(INDENT_SET, 0, TRUE, 0);
7212 }
7213 else
7214 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7215
7216 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7217 did_ai = FALSE;
7218#ifdef FEAT_SMARTINDENT
7219 did_si = FALSE;
7220 can_si = FALSE;
7221 can_si_back = FALSE;
7222#endif
7223#ifdef FEAT_CINDENT
7224 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7225#endif
7226}
7227
7228 static void
7229ins_del()
7230{
7231 int temp;
7232
7233 if (stop_arrow() == FAIL)
7234 return;
7235 if (gchar_cursor() == NUL) /* delete newline */
7236 {
7237 temp = curwin->w_cursor.col;
7238 if (!can_bs(BS_EOL) /* only if "eol" included */
7239 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7240 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7241 || do_join(FALSE) == FAIL)
7242 vim_beep();
7243 else
7244 curwin->w_cursor.col = temp;
7245 }
7246 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7247 vim_beep();
7248 did_ai = FALSE;
7249#ifdef FEAT_SMARTINDENT
7250 did_si = FALSE;
7251 can_si = FALSE;
7252 can_si_back = FALSE;
7253#endif
7254 AppendCharToRedobuff(K_DEL);
7255}
7256
7257/*
7258 * Handle Backspace, delete-word and delete-line in Insert mode.
7259 * Return TRUE when backspace was actually used.
7260 */
7261 static int
7262ins_bs(c, mode, inserted_space_p)
7263 int c;
7264 int mode;
7265 int *inserted_space_p;
7266{
7267 linenr_T lnum;
7268 int cc;
7269 int temp = 0; /* init for GCC */
7270 colnr_T mincol;
7271 int did_backspace = FALSE;
7272 int in_indent;
7273 int oldState;
7274#ifdef FEAT_MBYTE
7275 int p1, p2;
7276#endif
7277
7278 /*
7279 * can't delete anything in an empty file
7280 * can't backup past first character in buffer
7281 * can't backup past starting point unless 'backspace' > 1
7282 * can backup to a previous line if 'backspace' == 0
7283 */
7284 if ( bufempty()
7285 || (
7286#ifdef FEAT_RIGHTLEFT
7287 !revins_on &&
7288#endif
7289 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7290 || (!can_bs(BS_START)
7291 && (arrow_used
7292 || (curwin->w_cursor.lnum == Insstart.lnum
7293 && curwin->w_cursor.col <= Insstart.col)))
7294 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7295 && curwin->w_cursor.col <= ai_col)
7296 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7297 {
7298 vim_beep();
7299 return FALSE;
7300 }
7301
7302 if (stop_arrow() == FAIL)
7303 return FALSE;
7304 in_indent = inindent(0);
7305#ifdef FEAT_CINDENT
7306 if (in_indent)
7307 can_cindent = FALSE;
7308#endif
7309#ifdef FEAT_COMMENTS
7310 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7311#endif
7312#ifdef FEAT_RIGHTLEFT
7313 if (revins_on) /* put cursor after last inserted char */
7314 inc_cursor();
7315#endif
7316
7317#ifdef FEAT_VIRTUALEDIT
7318 /* Virtualedit:
7319 * BACKSPACE_CHAR eats a virtual space
7320 * BACKSPACE_WORD eats all coladd
7321 * BACKSPACE_LINE eats all coladd and keeps going
7322 */
7323 if (curwin->w_cursor.coladd > 0)
7324 {
7325 if (mode == BACKSPACE_CHAR)
7326 {
7327 --curwin->w_cursor.coladd;
7328 return TRUE;
7329 }
7330 if (mode == BACKSPACE_WORD)
7331 {
7332 curwin->w_cursor.coladd = 0;
7333 return TRUE;
7334 }
7335 curwin->w_cursor.coladd = 0;
7336 }
7337#endif
7338
7339 /*
7340 * delete newline!
7341 */
7342 if (curwin->w_cursor.col == 0)
7343 {
7344 lnum = Insstart.lnum;
7345 if (curwin->w_cursor.lnum == Insstart.lnum
7346#ifdef FEAT_RIGHTLEFT
7347 || revins_on
7348#endif
7349 )
7350 {
7351 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7352 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7353 return FALSE;
7354 --Insstart.lnum;
7355 Insstart.col = MAXCOL;
7356 }
7357 /*
7358 * In replace mode:
7359 * cc < 0: NL was inserted, delete it
7360 * cc >= 0: NL was replaced, put original characters back
7361 */
7362 cc = -1;
7363 if (State & REPLACE_FLAG)
7364 cc = replace_pop(); /* returns -1 if NL was inserted */
7365 /*
7366 * In replace mode, in the line we started replacing, we only move the
7367 * cursor.
7368 */
7369 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7370 {
7371 dec_cursor();
7372 }
7373 else
7374 {
7375#ifdef FEAT_VREPLACE
7376 if (!(State & VREPLACE_FLAG)
7377 || curwin->w_cursor.lnum > orig_line_count)
7378#endif
7379 {
7380 temp = gchar_cursor(); /* remember current char */
7381 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007382
7383 /* When "aw" is in 'formatoptions' we must delete the space at
7384 * the end of the line, otherwise the line will be broken
7385 * again when auto-formatting. */
7386 if (has_format_option(FO_AUTO)
7387 && has_format_option(FO_WHITE_PAR))
7388 {
7389 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7390 TRUE);
7391 int len;
7392
7393 len = STRLEN(ptr);
7394 if (len > 0 && ptr[len - 1] == ' ')
7395 ptr[len - 1] = NUL;
7396 }
7397
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 (void)do_join(FALSE);
7399 if (temp == NUL && gchar_cursor() != NUL)
7400 inc_cursor();
7401 }
7402#ifdef FEAT_VREPLACE
7403 else
7404 dec_cursor();
7405#endif
7406
7407 /*
7408 * In REPLACE mode we have to put back the text that was replaced
7409 * by the NL. On the replace stack is first a NUL-terminated
7410 * sequence of characters that were deleted and then the
7411 * characters that NL replaced.
7412 */
7413 if (State & REPLACE_FLAG)
7414 {
7415 /*
7416 * Do the next ins_char() in NORMAL state, to
7417 * prevent ins_char() from replacing characters and
7418 * avoiding showmatch().
7419 */
7420 oldState = State;
7421 State = NORMAL;
7422 /*
7423 * restore characters (blanks) deleted after cursor
7424 */
7425 while (cc > 0)
7426 {
7427 temp = curwin->w_cursor.col;
7428#ifdef FEAT_MBYTE
7429 mb_replace_pop_ins(cc);
7430#else
7431 ins_char(cc);
7432#endif
7433 curwin->w_cursor.col = temp;
7434 cc = replace_pop();
7435 }
7436 /* restore the characters that NL replaced */
7437 replace_pop_ins();
7438 State = oldState;
7439 }
7440 }
7441 did_ai = FALSE;
7442 }
7443 else
7444 {
7445 /*
7446 * Delete character(s) before the cursor.
7447 */
7448#ifdef FEAT_RIGHTLEFT
7449 if (revins_on) /* put cursor on last inserted char */
7450 dec_cursor();
7451#endif
7452 mincol = 0;
7453 /* keep indent */
7454 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7455#ifdef FEAT_RIGHTLEFT
7456 && !revins_on
7457#endif
7458 )
7459 {
7460 temp = curwin->w_cursor.col;
7461 beginline(BL_WHITE);
7462 if (curwin->w_cursor.col < (colnr_T)temp)
7463 mincol = curwin->w_cursor.col;
7464 curwin->w_cursor.col = temp;
7465 }
7466
7467 /*
7468 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7469 */
7470 if ( mode == BACKSPACE_CHAR
7471 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007472 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 && (*(ml_get_cursor() - 1) == TAB
7474 || (*(ml_get_cursor() - 1) == ' '
7475 && (!*inserted_space_p
7476 || arrow_used))))))
7477 {
7478 int ts;
7479 colnr_T vcol;
7480 colnr_T want_vcol;
7481 int extra = 0;
7482
7483 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007484 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 ts = curbuf->b_p_sw;
7486 else
7487 ts = curbuf->b_p_sts;
7488 /* Compute the virtual column where we want to be. Since
7489 * 'showbreak' may get in the way, need to get the last column of
7490 * the previous character. */
7491 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7492 dec_cursor();
7493 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7494 inc_cursor();
7495 want_vcol = (want_vcol / ts) * ts;
7496
7497 /* delete characters until we are at or before want_vcol */
7498 while (vcol > want_vcol
7499 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7500 {
7501 dec_cursor();
7502 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7503 if (State & REPLACE_FLAG)
7504 {
7505 /* Don't delete characters before the insert point when in
7506 * Replace mode */
7507 if (curwin->w_cursor.lnum != Insstart.lnum
7508 || curwin->w_cursor.col >= Insstart.col)
7509 {
7510#if 0 /* what was this for? It causes problems when sw != ts. */
7511 if (State == REPLACE && (int)vcol < want_vcol)
7512 {
7513 (void)del_char(FALSE);
7514 extra = 2; /* don't pop too much */
7515 }
7516 else
7517#endif
7518 replace_do_bs();
7519 }
7520 }
7521 else
7522 (void)del_char(FALSE);
7523 }
7524
7525 /* insert extra spaces until we are at want_vcol */
7526 while (vcol < want_vcol)
7527 {
7528 /* Remember the first char we inserted */
7529 if (curwin->w_cursor.lnum == Insstart.lnum
7530 && curwin->w_cursor.col < Insstart.col)
7531 Insstart.col = curwin->w_cursor.col;
7532
7533#ifdef FEAT_VREPLACE
7534 if (State & VREPLACE_FLAG)
7535 ins_char(' ');
7536 else
7537#endif
7538 {
7539 ins_str((char_u *)" ");
7540 if ((State & REPLACE_FLAG) && extra <= 1)
7541 {
7542 if (extra)
7543 replace_push_off(NUL);
7544 else
7545 replace_push(NUL);
7546 }
7547 if (extra == 2)
7548 extra = 1;
7549 }
7550 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7551 }
7552 }
7553
7554 /*
7555 * Delete upto starting point, start of line or previous word.
7556 */
7557 else do
7558 {
7559#ifdef FEAT_RIGHTLEFT
7560 if (!revins_on) /* put cursor on char to be deleted */
7561#endif
7562 dec_cursor();
7563
7564 /* start of word? */
7565 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7566 {
7567 mode = BACKSPACE_WORD_NOT_SPACE;
7568 temp = vim_iswordc(gchar_cursor());
7569 }
7570 /* end of word? */
7571 else if (mode == BACKSPACE_WORD_NOT_SPACE
7572 && (vim_isspace(cc = gchar_cursor())
7573 || vim_iswordc(cc) != temp))
7574 {
7575#ifdef FEAT_RIGHTLEFT
7576 if (!revins_on)
7577#endif
7578 inc_cursor();
7579#ifdef FEAT_RIGHTLEFT
7580 else if (State & REPLACE_FLAG)
7581 dec_cursor();
7582#endif
7583 break;
7584 }
7585 if (State & REPLACE_FLAG)
7586 replace_do_bs();
7587 else
7588 {
7589#ifdef FEAT_MBYTE
7590 if (enc_utf8 && p_deco)
7591 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7592#endif
7593 (void)del_char(FALSE);
7594#ifdef FEAT_MBYTE
7595 /*
7596 * If p1 or p2 is non-zero, there are combining characters we
7597 * need to take account of. Don't back up before the base
7598 * character.
7599 */
7600 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7601 inc_cursor();
7602#endif
7603#ifdef FEAT_RIGHTLEFT
7604 if (revins_chars)
7605 {
7606 revins_chars--;
7607 revins_legal++;
7608 }
7609 if (revins_on && gchar_cursor() == NUL)
7610 break;
7611#endif
7612 }
7613 /* Just a single backspace?: */
7614 if (mode == BACKSPACE_CHAR)
7615 break;
7616 } while (
7617#ifdef FEAT_RIGHTLEFT
7618 revins_on ||
7619#endif
7620 (curwin->w_cursor.col > mincol
7621 && (curwin->w_cursor.lnum != Insstart.lnum
7622 || curwin->w_cursor.col != Insstart.col)));
7623 did_backspace = TRUE;
7624 }
7625#ifdef FEAT_SMARTINDENT
7626 did_si = FALSE;
7627 can_si = FALSE;
7628 can_si_back = FALSE;
7629#endif
7630 if (curwin->w_cursor.col <= 1)
7631 did_ai = FALSE;
7632 /*
7633 * It's a little strange to put backspaces into the redo
7634 * buffer, but it makes auto-indent a lot easier to deal
7635 * with.
7636 */
7637 AppendCharToRedobuff(c);
7638
7639 /* If deleted before the insertion point, adjust it */
7640 if (curwin->w_cursor.lnum == Insstart.lnum
7641 && curwin->w_cursor.col < Insstart.col)
7642 Insstart.col = curwin->w_cursor.col;
7643
7644 /* vi behaviour: the cursor moves backward but the character that
7645 * was there remains visible
7646 * Vim behaviour: the cursor moves backward and the character that
7647 * was there is erased from the screen.
7648 * We can emulate the vi behaviour by pretending there is a dollar
7649 * displayed even when there isn't.
7650 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7651 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7652 dollar_vcol = curwin->w_virtcol;
7653
7654 return did_backspace;
7655}
7656
7657#ifdef FEAT_MOUSE
7658 static void
7659ins_mouse(c)
7660 int c;
7661{
7662 pos_T tpos;
7663
7664# ifdef FEAT_GUI
7665 /* When GUI is active, also move/paste when 'mouse' is empty */
7666 if (!gui.in_use)
7667# endif
7668 if (!mouse_has(MOUSE_INSERT))
7669 return;
7670
7671 undisplay_dollar();
7672 tpos = curwin->w_cursor;
7673 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7674 {
7675 start_arrow(&tpos);
7676# ifdef FEAT_CINDENT
7677 can_cindent = TRUE;
7678# endif
7679 }
7680
7681#ifdef FEAT_WINDOWS
7682 /* redraw status lines (in case another window became active) */
7683 redraw_statuslines();
7684#endif
7685}
7686
7687 static void
7688ins_mousescroll(up)
7689 int up;
7690{
7691 pos_T tpos;
7692# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7693 win_T *old_curwin;
7694# endif
7695
7696 tpos = curwin->w_cursor;
7697
7698# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7699 old_curwin = curwin;
7700
7701 /* Currently the mouse coordinates are only known in the GUI. */
7702 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7703 {
7704 int row, col;
7705
7706 row = mouse_row;
7707 col = mouse_col;
7708
7709 /* find the window at the pointer coordinates */
7710 curwin = mouse_find_win(&row, &col);
7711 curbuf = curwin->w_buffer;
7712 }
7713 if (curwin == old_curwin)
7714# endif
7715 undisplay_dollar();
7716
7717 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7718 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7719 else
7720 scroll_redraw(up, 3L);
7721
7722# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7723 curwin->w_redr_status = TRUE;
7724
7725 curwin = old_curwin;
7726 curbuf = curwin->w_buffer;
7727# endif
7728
7729 if (!equalpos(curwin->w_cursor, tpos))
7730 {
7731 start_arrow(&tpos);
7732# ifdef FEAT_CINDENT
7733 can_cindent = TRUE;
7734# endif
7735 }
7736}
7737#endif
7738
7739#ifdef FEAT_GUI
7740 void
7741ins_scroll()
7742{
7743 pos_T tpos;
7744
7745 undisplay_dollar();
7746 tpos = curwin->w_cursor;
7747 if (gui_do_scroll())
7748 {
7749 start_arrow(&tpos);
7750# ifdef FEAT_CINDENT
7751 can_cindent = TRUE;
7752# endif
7753 }
7754}
7755
7756 void
7757ins_horscroll()
7758{
7759 pos_T tpos;
7760
7761 undisplay_dollar();
7762 tpos = curwin->w_cursor;
7763 if (gui_do_horiz_scroll())
7764 {
7765 start_arrow(&tpos);
7766# ifdef FEAT_CINDENT
7767 can_cindent = TRUE;
7768# endif
7769 }
7770}
7771#endif
7772
7773 static void
7774ins_left()
7775{
7776 pos_T tpos;
7777
7778#ifdef FEAT_FOLDING
7779 if ((fdo_flags & FDO_HOR) && KeyTyped)
7780 foldOpenCursor();
7781#endif
7782 undisplay_dollar();
7783 tpos = curwin->w_cursor;
7784 if (oneleft() == OK)
7785 {
7786 start_arrow(&tpos);
7787#ifdef FEAT_RIGHTLEFT
7788 /* If exit reversed string, position is fixed */
7789 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
7790 revins_legal++;
7791 revins_chars++;
7792#endif
7793 }
7794
7795 /*
7796 * if 'whichwrap' set for cursor in insert mode may go to
7797 * previous line
7798 */
7799 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
7800 {
7801 start_arrow(&tpos);
7802 --(curwin->w_cursor.lnum);
7803 coladvance((colnr_T)MAXCOL);
7804 curwin->w_set_curswant = TRUE; /* so we stay at the end */
7805 }
7806 else
7807 vim_beep();
7808}
7809
7810 static void
7811ins_home(c)
7812 int c;
7813{
7814 pos_T tpos;
7815
7816#ifdef FEAT_FOLDING
7817 if ((fdo_flags & FDO_HOR) && KeyTyped)
7818 foldOpenCursor();
7819#endif
7820 undisplay_dollar();
7821 tpos = curwin->w_cursor;
7822 if (c == K_C_HOME)
7823 curwin->w_cursor.lnum = 1;
7824 curwin->w_cursor.col = 0;
7825#ifdef FEAT_VIRTUALEDIT
7826 curwin->w_cursor.coladd = 0;
7827#endif
7828 curwin->w_curswant = 0;
7829 start_arrow(&tpos);
7830}
7831
7832 static void
7833ins_end(c)
7834 int c;
7835{
7836 pos_T tpos;
7837
7838#ifdef FEAT_FOLDING
7839 if ((fdo_flags & FDO_HOR) && KeyTyped)
7840 foldOpenCursor();
7841#endif
7842 undisplay_dollar();
7843 tpos = curwin->w_cursor;
7844 if (c == K_C_END)
7845 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7846 coladvance((colnr_T)MAXCOL);
7847 curwin->w_curswant = MAXCOL;
7848
7849 start_arrow(&tpos);
7850}
7851
7852 static void
7853ins_s_left()
7854{
7855#ifdef FEAT_FOLDING
7856 if ((fdo_flags & FDO_HOR) && KeyTyped)
7857 foldOpenCursor();
7858#endif
7859 undisplay_dollar();
7860 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
7861 {
7862 start_arrow(&curwin->w_cursor);
7863 (void)bck_word(1L, FALSE, FALSE);
7864 curwin->w_set_curswant = TRUE;
7865 }
7866 else
7867 vim_beep();
7868}
7869
7870 static void
7871ins_right()
7872{
7873#ifdef FEAT_FOLDING
7874 if ((fdo_flags & FDO_HOR) && KeyTyped)
7875 foldOpenCursor();
7876#endif
7877 undisplay_dollar();
7878 if (gchar_cursor() != NUL || virtual_active()
7879 )
7880 {
7881 start_arrow(&curwin->w_cursor);
7882 curwin->w_set_curswant = TRUE;
7883#ifdef FEAT_VIRTUALEDIT
7884 if (virtual_active())
7885 oneright();
7886 else
7887#endif
7888 {
7889#ifdef FEAT_MBYTE
7890 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007891 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 else
7893#endif
7894 ++curwin->w_cursor.col;
7895 }
7896
7897#ifdef FEAT_RIGHTLEFT
7898 revins_legal++;
7899 if (revins_chars)
7900 revins_chars--;
7901#endif
7902 }
7903 /* if 'whichwrap' set for cursor in insert mode, may move the
7904 * cursor to the next line */
7905 else if (vim_strchr(p_ww, ']') != NULL
7906 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7907 {
7908 start_arrow(&curwin->w_cursor);
7909 curwin->w_set_curswant = TRUE;
7910 ++curwin->w_cursor.lnum;
7911 curwin->w_cursor.col = 0;
7912 }
7913 else
7914 vim_beep();
7915}
7916
7917 static void
7918ins_s_right()
7919{
7920#ifdef FEAT_FOLDING
7921 if ((fdo_flags & FDO_HOR) && KeyTyped)
7922 foldOpenCursor();
7923#endif
7924 undisplay_dollar();
7925 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
7926 || gchar_cursor() != NUL)
7927 {
7928 start_arrow(&curwin->w_cursor);
7929 (void)fwd_word(1L, FALSE, 0);
7930 curwin->w_set_curswant = TRUE;
7931 }
7932 else
7933 vim_beep();
7934}
7935
7936 static void
7937ins_up(startcol)
7938 int startcol; /* when TRUE move to Insstart.col */
7939{
7940 pos_T tpos;
7941 linenr_T old_topline = curwin->w_topline;
7942#ifdef FEAT_DIFF
7943 int old_topfill = curwin->w_topfill;
7944#endif
7945
7946 undisplay_dollar();
7947 tpos = curwin->w_cursor;
7948 if (cursor_up(1L, TRUE) == OK)
7949 {
7950 if (startcol)
7951 coladvance(getvcol_nolist(&Insstart));
7952 if (old_topline != curwin->w_topline
7953#ifdef FEAT_DIFF
7954 || old_topfill != curwin->w_topfill
7955#endif
7956 )
7957 redraw_later(VALID);
7958 start_arrow(&tpos);
7959#ifdef FEAT_CINDENT
7960 can_cindent = TRUE;
7961#endif
7962 }
7963 else
7964 vim_beep();
7965}
7966
7967 static void
7968ins_pageup()
7969{
7970 pos_T tpos;
7971
7972 undisplay_dollar();
7973 tpos = curwin->w_cursor;
7974 if (onepage(BACKWARD, 1L) == OK)
7975 {
7976 start_arrow(&tpos);
7977#ifdef FEAT_CINDENT
7978 can_cindent = TRUE;
7979#endif
7980 }
7981 else
7982 vim_beep();
7983}
7984
7985 static void
7986ins_down(startcol)
7987 int startcol; /* when TRUE move to Insstart.col */
7988{
7989 pos_T tpos;
7990 linenr_T old_topline = curwin->w_topline;
7991#ifdef FEAT_DIFF
7992 int old_topfill = curwin->w_topfill;
7993#endif
7994
7995 undisplay_dollar();
7996 tpos = curwin->w_cursor;
7997 if (cursor_down(1L, TRUE) == OK)
7998 {
7999 if (startcol)
8000 coladvance(getvcol_nolist(&Insstart));
8001 if (old_topline != curwin->w_topline
8002#ifdef FEAT_DIFF
8003 || old_topfill != curwin->w_topfill
8004#endif
8005 )
8006 redraw_later(VALID);
8007 start_arrow(&tpos);
8008#ifdef FEAT_CINDENT
8009 can_cindent = TRUE;
8010#endif
8011 }
8012 else
8013 vim_beep();
8014}
8015
8016 static void
8017ins_pagedown()
8018{
8019 pos_T tpos;
8020
8021 undisplay_dollar();
8022 tpos = curwin->w_cursor;
8023 if (onepage(FORWARD, 1L) == OK)
8024 {
8025 start_arrow(&tpos);
8026#ifdef FEAT_CINDENT
8027 can_cindent = TRUE;
8028#endif
8029 }
8030 else
8031 vim_beep();
8032}
8033
8034#ifdef FEAT_DND
8035 static void
8036ins_drop()
8037{
8038 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8039}
8040#endif
8041
8042/*
8043 * Handle TAB in Insert or Replace mode.
8044 * Return TRUE when the TAB needs to be inserted like a normal character.
8045 */
8046 static int
8047ins_tab()
8048{
8049 int ind;
8050 int i;
8051 int temp;
8052
8053 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8054 Insstart_blank_vcol = get_nolist_virtcol();
8055 if (echeck_abbr(TAB + ABBR_OFF))
8056 return FALSE;
8057
8058 ind = inindent(0);
8059#ifdef FEAT_CINDENT
8060 if (ind)
8061 can_cindent = FALSE;
8062#endif
8063
8064 /*
8065 * When nothing special, insert TAB like a normal character
8066 */
8067 if (!curbuf->b_p_et
8068 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8069 && curbuf->b_p_sts == 0)
8070 return TRUE;
8071
8072 if (stop_arrow() == FAIL)
8073 return TRUE;
8074
8075 did_ai = FALSE;
8076#ifdef FEAT_SMARTINDENT
8077 did_si = FALSE;
8078 can_si = FALSE;
8079 can_si_back = FALSE;
8080#endif
8081 AppendToRedobuff((char_u *)"\t");
8082
8083 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8084 temp = (int)curbuf->b_p_sw;
8085 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8086 temp = (int)curbuf->b_p_sts;
8087 else /* otherwise use 'tabstop' */
8088 temp = (int)curbuf->b_p_ts;
8089 temp -= get_nolist_virtcol() % temp;
8090
8091 /*
8092 * Insert the first space with ins_char(). It will delete one char in
8093 * replace mode. Insert the rest with ins_str(); it will not delete any
8094 * chars. For VREPLACE mode, we use ins_char() for all characters.
8095 */
8096 ins_char(' ');
8097 while (--temp > 0)
8098 {
8099#ifdef FEAT_VREPLACE
8100 if (State & VREPLACE_FLAG)
8101 ins_char(' ');
8102 else
8103#endif
8104 {
8105 ins_str((char_u *)" ");
8106 if (State & REPLACE_FLAG) /* no char replaced */
8107 replace_push(NUL);
8108 }
8109 }
8110
8111 /*
8112 * When 'expandtab' not set: Replace spaces by TABs where possible.
8113 */
8114 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8115 {
8116 char_u *ptr;
8117#ifdef FEAT_VREPLACE
8118 char_u *saved_line = NULL; /* init for GCC */
8119 pos_T pos;
8120#endif
8121 pos_T fpos;
8122 pos_T *cursor;
8123 colnr_T want_vcol, vcol;
8124 int change_col = -1;
8125 int save_list = curwin->w_p_list;
8126
8127 /*
8128 * Get the current line. For VREPLACE mode, don't make real changes
8129 * yet, just work on a copy of the line.
8130 */
8131#ifdef FEAT_VREPLACE
8132 if (State & VREPLACE_FLAG)
8133 {
8134 pos = curwin->w_cursor;
8135 cursor = &pos;
8136 saved_line = vim_strsave(ml_get_curline());
8137 if (saved_line == NULL)
8138 return FALSE;
8139 ptr = saved_line + pos.col;
8140 }
8141 else
8142#endif
8143 {
8144 ptr = ml_get_cursor();
8145 cursor = &curwin->w_cursor;
8146 }
8147
8148 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8149 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8150 curwin->w_p_list = FALSE;
8151
8152 /* Find first white before the cursor */
8153 fpos = curwin->w_cursor;
8154 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8155 {
8156 --fpos.col;
8157 --ptr;
8158 }
8159
8160 /* In Replace mode, don't change characters before the insert point. */
8161 if ((State & REPLACE_FLAG)
8162 && fpos.lnum == Insstart.lnum
8163 && fpos.col < Insstart.col)
8164 {
8165 ptr += Insstart.col - fpos.col;
8166 fpos.col = Insstart.col;
8167 }
8168
8169 /* compute virtual column numbers of first white and cursor */
8170 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8171 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8172
8173 /* Use as many TABs as possible. Beware of 'showbreak' and
8174 * 'linebreak' adding extra virtual columns. */
8175 while (vim_iswhite(*ptr))
8176 {
8177 i = lbr_chartabsize((char_u *)"\t", vcol);
8178 if (vcol + i > want_vcol)
8179 break;
8180 if (*ptr != TAB)
8181 {
8182 *ptr = TAB;
8183 if (change_col < 0)
8184 {
8185 change_col = fpos.col; /* Column of first change */
8186 /* May have to adjust Insstart */
8187 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8188 Insstart.col = fpos.col;
8189 }
8190 }
8191 ++fpos.col;
8192 ++ptr;
8193 vcol += i;
8194 }
8195
8196 if (change_col >= 0)
8197 {
8198 int repl_off = 0;
8199
8200 /* Skip over the spaces we need. */
8201 while (vcol < want_vcol && *ptr == ' ')
8202 {
8203 vcol += lbr_chartabsize(ptr, vcol);
8204 ++ptr;
8205 ++repl_off;
8206 }
8207 if (vcol > want_vcol)
8208 {
8209 /* Must have a char with 'showbreak' just before it. */
8210 --ptr;
8211 --repl_off;
8212 }
8213 fpos.col += repl_off;
8214
8215 /* Delete following spaces. */
8216 i = cursor->col - fpos.col;
8217 if (i > 0)
8218 {
8219 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8220 /* correct replace stack. */
8221 if ((State & REPLACE_FLAG)
8222#ifdef FEAT_VREPLACE
8223 && !(State & VREPLACE_FLAG)
8224#endif
8225 )
8226 for (temp = i; --temp >= 0; )
8227 replace_join(repl_off);
8228 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008229#ifdef FEAT_NETBEANS_INTG
8230 if (usingNetbeans)
8231 {
8232 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8233 (long)(i + 1));
8234 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8235 (char_u *)"\t", 1);
8236 }
8237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 cursor->col -= i;
8239
8240#ifdef FEAT_VREPLACE
8241 /*
8242 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8243 * backspacing over the changed spacing and then inserting the new
8244 * spacing.
8245 */
8246 if (State & VREPLACE_FLAG)
8247 {
8248 /* Backspace from real cursor to change_col */
8249 backspace_until_column(change_col);
8250
8251 /* Insert each char in saved_line from changed_col to
8252 * ptr-cursor */
8253 ins_bytes_len(saved_line + change_col,
8254 cursor->col - change_col);
8255 }
8256#endif
8257 }
8258
8259#ifdef FEAT_VREPLACE
8260 if (State & VREPLACE_FLAG)
8261 vim_free(saved_line);
8262#endif
8263 curwin->w_p_list = save_list;
8264 }
8265
8266 return FALSE;
8267}
8268
8269/*
8270 * Handle CR or NL in insert mode.
8271 * Return TRUE when out of memory or can't undo.
8272 */
8273 static int
8274ins_eol(c)
8275 int c;
8276{
8277 int i;
8278
8279 if (echeck_abbr(c + ABBR_OFF))
8280 return FALSE;
8281 if (stop_arrow() == FAIL)
8282 return TRUE;
8283 undisplay_dollar();
8284
8285 /*
8286 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8287 * character under the cursor. Only push a NUL on the replace stack,
8288 * nothing to put back when the NL is deleted.
8289 */
8290 if ((State & REPLACE_FLAG)
8291#ifdef FEAT_VREPLACE
8292 && !(State & VREPLACE_FLAG)
8293#endif
8294 )
8295 replace_push(NUL);
8296
8297 /*
8298 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8299 * replacing the next line, so we push all of the characters left on the
8300 * line onto the replace stack. This is not done here though, it is done
8301 * in open_line().
8302 */
8303
8304#ifdef FEAT_RIGHTLEFT
8305# ifdef FEAT_FKMAP
8306 if (p_altkeymap && p_fkmap)
8307 fkmap(NL);
8308# endif
8309 /* NL in reverse insert will always start in the end of
8310 * current line. */
8311 if (revins_on)
8312 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8313#endif
8314
8315 AppendToRedobuff(NL_STR);
8316 i = open_line(FORWARD,
8317#ifdef FEAT_COMMENTS
8318 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8319#endif
8320 0, old_indent);
8321 old_indent = 0;
8322#ifdef FEAT_CINDENT
8323 can_cindent = TRUE;
8324#endif
8325
8326 return (!i);
8327}
8328
8329#ifdef FEAT_DIGRAPHS
8330/*
8331 * Handle digraph in insert mode.
8332 * Returns character still to be inserted, or NUL when nothing remaining to be
8333 * done.
8334 */
8335 static int
8336ins_digraph()
8337{
8338 int c;
8339 int cc;
8340
8341 pc_status = PC_STATUS_UNSET;
8342 if (redrawing() && !char_avail())
8343 {
8344 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008345 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346
8347 edit_putchar('?', TRUE);
8348#ifdef FEAT_CMDL_INFO
8349 add_to_showcmd_c(Ctrl_K);
8350#endif
8351 }
8352
8353#ifdef USE_ON_FLY_SCROLL
8354 dont_scroll = TRUE; /* disallow scrolling here */
8355#endif
8356
8357 /* don't map the digraph chars. This also prevents the
8358 * mode message to be deleted when ESC is hit */
8359 ++no_mapping;
8360 ++allow_keys;
8361 c = safe_vgetc();
8362 --no_mapping;
8363 --allow_keys;
8364 if (IS_SPECIAL(c) || mod_mask) /* special key */
8365 {
8366#ifdef FEAT_CMDL_INFO
8367 clear_showcmd();
8368#endif
8369 insert_special(c, TRUE, FALSE);
8370 return NUL;
8371 }
8372 if (c != ESC)
8373 {
8374 if (redrawing() && !char_avail())
8375 {
8376 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008377 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378
8379 if (char2cells(c) == 1)
8380 {
8381 /* first remove the '?', otherwise it's restored when typing
8382 * an ESC next */
8383 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008384 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 edit_putchar(c, TRUE);
8386 }
8387#ifdef FEAT_CMDL_INFO
8388 add_to_showcmd_c(c);
8389#endif
8390 }
8391 ++no_mapping;
8392 ++allow_keys;
8393 cc = safe_vgetc();
8394 --no_mapping;
8395 --allow_keys;
8396 if (cc != ESC)
8397 {
8398 AppendToRedobuff((char_u *)CTRL_V_STR);
8399 c = getdigraph(c, cc, TRUE);
8400#ifdef FEAT_CMDL_INFO
8401 clear_showcmd();
8402#endif
8403 return c;
8404 }
8405 }
8406 edit_unputchar();
8407#ifdef FEAT_CMDL_INFO
8408 clear_showcmd();
8409#endif
8410 return NUL;
8411}
8412#endif
8413
8414/*
8415 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8416 * Returns the char to be inserted, or NUL if none found.
8417 */
8418 static int
8419ins_copychar(lnum)
8420 linenr_T lnum;
8421{
8422 int c;
8423 int temp;
8424 char_u *ptr, *prev_ptr;
8425
8426 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8427 {
8428 vim_beep();
8429 return NUL;
8430 }
8431
8432 /* try to advance to the cursor column */
8433 temp = 0;
8434 ptr = ml_get(lnum);
8435 prev_ptr = ptr;
8436 validate_virtcol();
8437 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8438 {
8439 prev_ptr = ptr;
8440 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8441 }
8442 if ((colnr_T)temp > curwin->w_virtcol)
8443 ptr = prev_ptr;
8444
8445#ifdef FEAT_MBYTE
8446 c = (*mb_ptr2char)(ptr);
8447#else
8448 c = *ptr;
8449#endif
8450 if (c == NUL)
8451 vim_beep();
8452 return c;
8453}
8454
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008455/*
8456 * CTRL-Y or CTRL-E typed in Insert mode.
8457 */
8458 static int
8459ins_ctrl_ey(tc)
8460 int tc;
8461{
8462 int c = tc;
8463
8464#ifdef FEAT_INS_EXPAND
8465 if (ctrl_x_mode == CTRL_X_SCROLL)
8466 {
8467 if (c == Ctrl_Y)
8468 scrolldown_clamp();
8469 else
8470 scrollup_clamp();
8471 redraw_later(VALID);
8472 }
8473 else
8474#endif
8475 {
8476 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8477 if (c != NUL)
8478 {
8479 long tw_save;
8480
8481 /* The character must be taken literally, insert like it
8482 * was typed after a CTRL-V, and pretend 'textwidth'
8483 * wasn't set. Digits, 'o' and 'x' are special after a
8484 * CTRL-V, don't use it for these. */
8485 if (c < 256 && !isalnum(c))
8486 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8487 tw_save = curbuf->b_p_tw;
8488 curbuf->b_p_tw = -1;
8489 insert_special(c, TRUE, FALSE);
8490 curbuf->b_p_tw = tw_save;
8491#ifdef FEAT_RIGHTLEFT
8492 revins_chars++;
8493 revins_legal++;
8494#endif
8495 c = Ctrl_V; /* pretend CTRL-V is last character */
8496 auto_format(FALSE, TRUE);
8497 }
8498 }
8499 return c;
8500}
8501
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502#ifdef FEAT_SMARTINDENT
8503/*
8504 * Try to do some very smart auto-indenting.
8505 * Used when inserting a "normal" character.
8506 */
8507 static void
8508ins_try_si(c)
8509 int c;
8510{
8511 pos_T *pos, old_pos;
8512 char_u *ptr;
8513 int i;
8514 int temp;
8515
8516 /*
8517 * do some very smart indenting when entering '{' or '}'
8518 */
8519 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8520 {
8521 /*
8522 * for '}' set indent equal to indent of line containing matching '{'
8523 */
8524 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8525 {
8526 old_pos = curwin->w_cursor;
8527 /*
8528 * If the matching '{' has a ')' immediately before it (ignoring
8529 * white-space), then line up with the start of the line
8530 * containing the matching '(' if there is one. This handles the
8531 * case where an "if (..\n..) {" statement continues over multiple
8532 * lines -- webb
8533 */
8534 ptr = ml_get(pos->lnum);
8535 i = pos->col;
8536 if (i > 0) /* skip blanks before '{' */
8537 while (--i > 0 && vim_iswhite(ptr[i]))
8538 ;
8539 curwin->w_cursor.lnum = pos->lnum;
8540 curwin->w_cursor.col = i;
8541 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8542 curwin->w_cursor = *pos;
8543 i = get_indent();
8544 curwin->w_cursor = old_pos;
8545#ifdef FEAT_VREPLACE
8546 if (State & VREPLACE_FLAG)
8547 change_indent(INDENT_SET, i, FALSE, NUL);
8548 else
8549#endif
8550 (void)set_indent(i, SIN_CHANGED);
8551 }
8552 else if (curwin->w_cursor.col > 0)
8553 {
8554 /*
8555 * when inserting '{' after "O" reduce indent, but not
8556 * more than indent of previous line
8557 */
8558 temp = TRUE;
8559 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8560 {
8561 old_pos = curwin->w_cursor;
8562 i = get_indent();
8563 while (curwin->w_cursor.lnum > 1)
8564 {
8565 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8566
8567 /* ignore empty lines and lines starting with '#'. */
8568 if (*ptr != '#' && *ptr != NUL)
8569 break;
8570 }
8571 if (get_indent() >= i)
8572 temp = FALSE;
8573 curwin->w_cursor = old_pos;
8574 }
8575 if (temp)
8576 shift_line(TRUE, FALSE, 1);
8577 }
8578 }
8579
8580 /*
8581 * set indent of '#' always to 0
8582 */
8583 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8584 {
8585 /* remember current indent for next line */
8586 old_indent = get_indent();
8587 (void)set_indent(0, SIN_CHANGED);
8588 }
8589
8590 /* Adjust ai_col, the char at this position can be deleted. */
8591 if (ai_col > curwin->w_cursor.col)
8592 ai_col = curwin->w_cursor.col;
8593}
8594#endif
8595
8596/*
8597 * Get the value that w_virtcol would have when 'list' is off.
8598 * Unless 'cpo' contains the 'L' flag.
8599 */
8600 static colnr_T
8601get_nolist_virtcol()
8602{
8603 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8604 return getvcol_nolist(&curwin->w_cursor);
8605 validate_virtcol();
8606 return curwin->w_virtcol;
8607}