blob: 9224c58970293e668c6e3e6a037f788993ddc71e [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
Bram Moolenaarc7453f52006-02-10 23:20:28 +000098static int compl_get_longest = FALSE; /* put longest common string
99 in compl_leader */
100
Bram Moolenaara6557602006-02-04 22:43:20 +0000101static int compl_used_match; /* Selected one of the matches. When
102 FALSE the match was edited or using
103 the longest common string. */
104
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000105/* When the first completion is done "compl_started" is set. When it's
106 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000107static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000108
Bram Moolenaar572cb562005-08-05 21:35:02 +0000109static int compl_matches = 0;
110static char_u *compl_pattern = NULL;
111static int compl_direction = FORWARD;
112static int compl_shows_dir = FORWARD;
113static int compl_pending = FALSE;
114static pos_T compl_startpos;
115static colnr_T compl_col = 0; /* column where the text starts
116 * that is being completed */
117static int save_sm = -1;
118static char_u *compl_orig_text = NULL; /* text as it was before
119 * completion started */
120static int compl_cont_mode = 0;
121static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000123static void ins_ctrl_x __ARGS((void));
124static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000125static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000126static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000128static void ins_compl_upd_pum __ARGS((void));
129static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000130static int pum_wanted __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000131static int pum_two_or_more __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000132static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000133static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134static void ins_compl_free __ARGS((void));
135static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000136static int ins_compl_bs __ARGS((void));
137static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000138static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000139static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000141static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142static void ins_compl_delete __ARGS((void));
143static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000144static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000145static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000146static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000147static int ins_compl_key2count __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148static int ins_complete __ARGS((int c));
149static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
150#endif /* FEAT_INS_EXPAND */
151
152#define BACKSPACE_CHAR 1
153#define BACKSPACE_WORD 2
154#define BACKSPACE_WORD_NOT_SPACE 3
155#define BACKSPACE_LINE 4
156
Bram Moolenaar754b5602006-02-09 23:53:20 +0000157static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static void ins_ctrl_v __ARGS((void));
159static void undisplay_dollar __ARGS((void));
160static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000161static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162static void check_auto_format __ARGS((int));
163static void redo_literal __ARGS((int c));
164static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000165#ifdef FEAT_SYN_HL
166static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000167static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000168static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
171static int echeck_abbr __ARGS((int));
172static void replace_push_off __ARGS((int c));
173static int replace_pop __ARGS((void));
174static void replace_join __ARGS((int off));
175static void replace_pop_ins __ARGS((void));
176#ifdef FEAT_MBYTE
177static void mb_replace_pop_ins __ARGS((int cc));
178#endif
179static void replace_flush __ARGS((void));
180static void replace_do_bs __ARGS((void));
181#ifdef FEAT_CINDENT
182static int cindent_on __ARGS((void));
183#endif
184static void ins_reg __ARGS((void));
185static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000186static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000187static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#ifdef FEAT_RIGHTLEFT
189static void ins_ctrl_ __ARGS((void));
190#endif
191#ifdef FEAT_VISUAL
192static int ins_start_select __ARGS((int c));
193#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000194static void ins_insert __ARGS((int replaceState));
195static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196static void ins_shift __ARGS((int c, int lastc));
197static void ins_del __ARGS((void));
198static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
199#ifdef FEAT_MOUSE
200static void ins_mouse __ARGS((int c));
201static void ins_mousescroll __ARGS((int up));
202#endif
203static void ins_left __ARGS((void));
204static void ins_home __ARGS((int c));
205static void ins_end __ARGS((int c));
206static void ins_s_left __ARGS((void));
207static void ins_right __ARGS((void));
208static void ins_s_right __ARGS((void));
209static void ins_up __ARGS((int startcol));
210static void ins_pageup __ARGS((void));
211static void ins_down __ARGS((int startcol));
212static void ins_pagedown __ARGS((void));
213#ifdef FEAT_DND
214static void ins_drop __ARGS((void));
215#endif
216static int ins_tab __ARGS((void));
217static int ins_eol __ARGS((int c));
218#ifdef FEAT_DIGRAPHS
219static int ins_digraph __ARGS((void));
220#endif
221static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000222static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#ifdef FEAT_SMARTINDENT
224static void ins_try_si __ARGS((int c));
225#endif
226static colnr_T get_nolist_virtcol __ARGS((void));
227
228static colnr_T Insstart_textlen; /* length of line when insert started */
229static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
230
231static char_u *last_insert = NULL; /* the text of the previous insert,
232 K_SPECIAL and CSI are escaped */
233static int last_insert_skip; /* nr of chars in front of previous insert */
234static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000235static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236
237#ifdef FEAT_CINDENT
238static int can_cindent; /* may do cindenting on this line */
239#endif
240
241static int old_indent = 0; /* for ^^D command in insert mode */
242
243#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000244static int revins_on; /* reverse insert mode on */
245static int revins_chars; /* how much to skip after edit */
246static int revins_legal; /* was the last char 'legal'? */
247static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248#endif
249
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250static int ins_need_undo; /* call u_save() before inserting a
251 char. Set when edit() is called.
252 after that arrow_used is used. */
253
254static int did_add_space = FALSE; /* auto_format() added an extra space
255 under the cursor */
256
257/*
258 * edit(): Start inserting text.
259 *
260 * "cmdchar" can be:
261 * 'i' normal insert command
262 * 'a' normal append command
263 * 'R' replace command
264 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
265 * but still only one <CR> is inserted. The <Esc> is not used for redo.
266 * 'g' "gI" command.
267 * 'V' "gR" command for Virtual Replace mode.
268 * 'v' "gr" command for single character Virtual Replace mode.
269 *
270 * This function is not called recursively. For CTRL-O commands, it returns
271 * and lets the caller handle the Normal-mode command.
272 *
273 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
274 */
275 int
276edit(cmdchar, startln, count)
277 int cmdchar;
278 int startln; /* if set, insert at start of line */
279 long count;
280{
281 int c = 0;
282 char_u *ptr;
283 int lastc;
284 colnr_T mincol;
285 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 int i;
287 int did_backspace = TRUE; /* previous char was backspace */
288#ifdef FEAT_CINDENT
289 int line_is_white = FALSE; /* line is empty before insert */
290#endif
291 linenr_T old_topline = 0; /* topline before insertion */
292#ifdef FEAT_DIFF
293 int old_topfill = -1;
294#endif
295 int inserted_space = FALSE; /* just inserted a space */
296 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000297 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000299 /* Remember whether editing was restarted after CTRL-O. */
300 did_restart_edit = restart_edit;
301
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 /* sleep before redrawing, needed for "CTRL-O :" that results in an
303 * error message */
304 check_for_delay(TRUE);
305
306#ifdef HAVE_SANDBOX
307 /* Don't allow inserting in the sandbox. */
308 if (sandbox != 0)
309 {
310 EMSG(_(e_sandbox));
311 return FALSE;
312 }
313#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000314 /* Don't allow changes in the buffer while editing the cmdline. The
315 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000316 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000317 {
318 EMSG(_(e_secure));
319 return FALSE;
320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321
322#ifdef FEAT_INS_EXPAND
323 ins_compl_clear(); /* clear stuff for CTRL-X mode */
324#endif
325
Bram Moolenaar843ee412004-06-30 16:16:41 +0000326#ifdef FEAT_AUTOCMD
327 /*
328 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
329 */
330 if (cmdchar != 'r' && cmdchar != 'v')
331 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000332# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000333 if (cmdchar == 'R')
334 ptr = (char_u *)"r";
335 else if (cmdchar == 'V')
336 ptr = (char_u *)"v";
337 else
338 ptr = (char_u *)"i";
339 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000340# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000341 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
342 }
343#endif
344
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#ifdef FEAT_MOUSE
346 /*
347 * When doing a paste with the middle mouse button, Insstart is set to
348 * where the paste started.
349 */
350 if (where_paste_started.lnum != 0)
351 Insstart = where_paste_started;
352 else
353#endif
354 {
355 Insstart = curwin->w_cursor;
356 if (startln)
357 Insstart.col = 0;
358 }
359 Insstart_textlen = linetabsize(ml_get_curline());
360 Insstart_blank_vcol = MAXCOL;
361 if (!did_ai)
362 ai_col = 0;
363
364 if (cmdchar != NUL && restart_edit == 0)
365 {
366 ResetRedobuff();
367 AppendNumberToRedobuff(count);
368#ifdef FEAT_VREPLACE
369 if (cmdchar == 'V' || cmdchar == 'v')
370 {
371 /* "gR" or "gr" command */
372 AppendCharToRedobuff('g');
373 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
374 }
375 else
376#endif
377 {
378 AppendCharToRedobuff(cmdchar);
379 if (cmdchar == 'g') /* "gI" command */
380 AppendCharToRedobuff('I');
381 else if (cmdchar == 'r') /* "r<CR>" command */
382 count = 1; /* insert only one <CR> */
383 }
384 }
385
386 if (cmdchar == 'R')
387 {
388#ifdef FEAT_FKMAP
389 if (p_fkmap && p_ri)
390 {
391 beep_flush();
392 EMSG(farsi_text_3); /* encoded in Farsi */
393 State = INSERT;
394 }
395 else
396#endif
397 State = REPLACE;
398 }
399#ifdef FEAT_VREPLACE
400 else if (cmdchar == 'V' || cmdchar == 'v')
401 {
402 State = VREPLACE;
403 replaceState = VREPLACE;
404 orig_line_count = curbuf->b_ml.ml_line_count;
405 vr_lines_changed = 1;
406 }
407#endif
408 else
409 State = INSERT;
410
411 stop_insert_mode = FALSE;
412
413 /*
414 * Need to recompute the cursor position, it might move when the cursor is
415 * on a TAB or special character.
416 */
417 curs_columns(TRUE);
418
419 /*
420 * Enable langmap or IME, indicated by 'iminsert'.
421 * Note that IME may enabled/disabled without us noticing here, thus the
422 * 'iminsert' value may not reflect what is actually used. It is updated
423 * when hitting <Esc>.
424 */
425 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
426 State |= LANGMAP;
427#ifdef USE_IM_CONTROL
428 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
429#endif
430
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#ifdef FEAT_MOUSE
432 setmouse();
433#endif
434#ifdef FEAT_CMDL_INFO
435 clear_showcmd();
436#endif
437#ifdef FEAT_RIGHTLEFT
438 /* there is no reverse replace mode */
439 revins_on = (State == INSERT && p_ri);
440 if (revins_on)
441 undisplay_dollar();
442 revins_chars = 0;
443 revins_legal = 0;
444 revins_scol = -1;
445#endif
446
447 /*
448 * Handle restarting Insert mode.
449 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
450 * restart_edit non-zero, and something in the stuff buffer.
451 */
452 if (restart_edit != 0 && stuff_empty())
453 {
454#ifdef FEAT_MOUSE
455 /*
456 * After a paste we consider text typed to be part of the insert for
457 * the pasted text. You can backspace over the pasted text too.
458 */
459 if (where_paste_started.lnum)
460 arrow_used = FALSE;
461 else
462#endif
463 arrow_used = TRUE;
464 restart_edit = 0;
465
466 /*
467 * If the cursor was after the end-of-line before the CTRL-O and it is
468 * now at the end-of-line, put it after the end-of-line (this is not
469 * correct in very rare cases).
470 * Also do this if curswant is greater than the current virtual
471 * column. Eg after "^O$" or "^O80|".
472 */
473 validate_virtcol();
474 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000475 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 || curwin->w_curswant > curwin->w_virtcol)
477 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
478 {
479 if (ptr[1] == NUL)
480 ++curwin->w_cursor.col;
481#ifdef FEAT_MBYTE
482 else if (has_mbyte)
483 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000484 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 if (ptr[i] == NUL)
486 curwin->w_cursor.col += i;
487 }
488#endif
489 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000490 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 }
492 else
493 arrow_used = FALSE;
494
495 /* we are in insert mode now, don't need to start it anymore */
496 need_start_insertmode = FALSE;
497
498 /* Need to save the line for undo before inserting the first char. */
499 ins_need_undo = TRUE;
500
501#ifdef FEAT_MOUSE
502 where_paste_started.lnum = 0;
503#endif
504#ifdef FEAT_CINDENT
505 can_cindent = TRUE;
506#endif
507#ifdef FEAT_FOLDING
508 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
509 * restarting. */
510 if (!p_im && did_restart_edit == 0)
511 foldOpenCursor();
512#endif
513
514 /*
515 * If 'showmode' is set, show the current (insert/replace/..) mode.
516 * A warning message for changing a readonly file is given here, before
517 * actually changing anything. It's put after the mode, if any.
518 */
519 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000520 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 i = showmode();
522
523 if (!p_im && did_restart_edit == 0)
524 change_warning(i + 1);
525
526#ifdef CURSOR_SHAPE
527 ui_cursor_shape(); /* may show different cursor shape */
528#endif
529#ifdef FEAT_DIGRAPHS
530 do_digraph(-1); /* clear digraphs */
531#endif
532
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000533 /*
534 * Get the current length of the redo buffer, those characters have to be
535 * skipped if we want to get to the inserted characters.
536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 ptr = get_inserted();
538 if (ptr == NULL)
539 new_insert_skip = 0;
540 else
541 {
542 new_insert_skip = (int)STRLEN(ptr);
543 vim_free(ptr);
544 }
545
546 old_indent = 0;
547
548 /*
549 * Main loop in Insert mode: repeat until Insert mode is left.
550 */
551 for (;;)
552 {
553#ifdef FEAT_RIGHTLEFT
554 if (!revins_legal)
555 revins_scol = -1; /* reset on illegal motions */
556 else
557 revins_legal = 0;
558#endif
559 if (arrow_used) /* don't repeat insert when arrow key used */
560 count = 0;
561
562 if (stop_insert_mode)
563 {
564 /* ":stopinsert" used or 'insertmode' reset */
565 count = 0;
566 goto doESCkey;
567 }
568
569 /* set curwin->w_curswant for next K_DOWN or K_UP */
570 if (!arrow_used)
571 curwin->w_set_curswant = TRUE;
572
573 /* If there is no typeahead may check for timestamps (e.g., for when a
574 * menu invoked a shell command). */
575 if (stuff_empty())
576 {
577 did_check_timestamps = FALSE;
578 if (need_check_timestamps)
579 check_timestamps(FALSE);
580 }
581
582 /*
583 * When emsg() was called msg_scroll will have been set.
584 */
585 msg_scroll = FALSE;
586
587#ifdef FEAT_GUI
588 /* When 'mousefocus' is set a mouse movement may have taken us to
589 * another window. "need_mouse_correct" may then be set because of an
590 * autocommand. */
591 if (need_mouse_correct)
592 gui_mouse_correct();
593#endif
594
595#ifdef FEAT_FOLDING
596 /* Open fold at the cursor line, according to 'foldopen'. */
597 if (fdo_flags & FDO_INSERT)
598 foldOpenCursor();
599 /* Close folds where the cursor isn't, according to 'foldclose' */
600 if (!char_avail())
601 foldCheckClose();
602#endif
603
604 /*
605 * If we inserted a character at the last position of the last line in
606 * the window, scroll the window one line up. This avoids an extra
607 * redraw.
608 * This is detected when the cursor column is smaller after inserting
609 * something.
610 * Don't do this when the topline changed already, it has
611 * already been adjusted (by insertchar() calling open_line())).
612 */
613 if (curbuf->b_mod_set
614 && curwin->w_p_wrap
615 && !did_backspace
616 && curwin->w_topline == old_topline
617#ifdef FEAT_DIFF
618 && curwin->w_topfill == old_topfill
619#endif
620 )
621 {
622 mincol = curwin->w_wcol;
623 validate_cursor_col();
624
625 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
626 && curwin->w_wrow == W_WINROW(curwin)
627 + curwin->w_height - 1 - p_so
628 && (curwin->w_cursor.lnum != curwin->w_topline
629#ifdef FEAT_DIFF
630 || curwin->w_topfill > 0
631#endif
632 ))
633 {
634#ifdef FEAT_DIFF
635 if (curwin->w_topfill > 0)
636 --curwin->w_topfill;
637 else
638#endif
639#ifdef FEAT_FOLDING
640 if (hasFolding(curwin->w_topline, NULL, &old_topline))
641 set_topline(curwin, old_topline + 1);
642 else
643#endif
644 set_topline(curwin, curwin->w_topline + 1);
645 }
646 }
647
648 /* May need to adjust w_topline to show the cursor. */
649 update_topline();
650
651 did_backspace = FALSE;
652
653 validate_cursor(); /* may set must_redraw */
654
655 /*
656 * Redraw the display when no characters are waiting.
657 * Also shows mode, ruler and positions cursor.
658 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000659 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661#ifdef FEAT_SCROLLBIND
662 if (curwin->w_p_scb)
663 do_check_scrollbind(TRUE);
664#endif
665
666 update_curswant();
667 old_topline = curwin->w_topline;
668#ifdef FEAT_DIFF
669 old_topfill = curwin->w_topfill;
670#endif
671
672#ifdef USE_ON_FLY_SCROLL
673 dont_scroll = FALSE; /* allow scrolling here */
674#endif
675
676 /*
677 * Get a character for Insert mode.
678 */
679 lastc = c; /* remember previous char for CTRL-D */
680 c = safe_vgetc();
681
682#ifdef FEAT_RIGHTLEFT
683 if (p_hkmap && KeyTyped)
684 c = hkmap(c); /* Hebrew mode mapping */
685#endif
686#ifdef FEAT_FKMAP
687 if (p_fkmap && KeyTyped)
688 c = fkmap(c); /* Farsi mode mapping */
689#endif
690
691#ifdef FEAT_INS_EXPAND
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 Moolenaarc7453f52006-02-10 23:20:28 +0000720
721 /* Pressing Enter selects the current match. */
722 if (c == CAR || c == K_KENTER || c == NL)
723 {
724 ins_compl_delete();
725 ins_compl_insert();
726 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000727 }
728 }
729
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
731 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000732 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000733 if (c != K_IGNORE && ins_compl_prep(c))
734 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735#endif
736
Bram Moolenaar488c6512005-08-11 20:09:58 +0000737 /* CTRL-\ CTRL-N goes to Normal mode,
738 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
739 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 if (c == Ctrl_BSL)
741 {
742 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000743 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 ++no_mapping;
745 ++allow_keys;
746 c = safe_vgetc();
747 --no_mapping;
748 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000749 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000751 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 vungetc(c);
753 c = Ctrl_BSL;
754 }
755 else if (c == Ctrl_G && p_im)
756 continue;
757 else
758 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000759 if (c == Ctrl_O)
760 {
761 ins_ctrl_o();
762 ins_at_eol = FALSE; /* cursor keeps its column */
763 nomove = TRUE;
764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 count = 0;
766 goto doESCkey;
767 }
768 }
769
770#ifdef FEAT_DIGRAPHS
771 c = do_digraph(c);
772#endif
773
774#ifdef FEAT_INS_EXPAND
775 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
776 goto docomplete;
777#endif
778 if (c == Ctrl_V || c == Ctrl_Q)
779 {
780 ins_ctrl_v();
781 c = Ctrl_V; /* pretend CTRL-V is last typed character */
782 continue;
783 }
784
785#ifdef FEAT_CINDENT
786 if (cindent_on()
787# ifdef FEAT_INS_EXPAND
788 && ctrl_x_mode == 0
789# endif
790 )
791 {
792 /* A key name preceded by a bang means this key is not to be
793 * inserted. Skip ahead to the re-indenting below.
794 * A key name preceded by a star means that indenting has to be
795 * done before inserting the key. */
796 line_is_white = inindent(0);
797 if (in_cinkeys(c, '!', line_is_white))
798 goto force_cindent;
799 if (can_cindent && in_cinkeys(c, '*', line_is_white)
800 && stop_arrow() == OK)
801 do_c_expr_indent();
802 }
803#endif
804
805#ifdef FEAT_RIGHTLEFT
806 if (curwin->w_p_rl)
807 switch (c)
808 {
809 case K_LEFT: c = K_RIGHT; break;
810 case K_S_LEFT: c = K_S_RIGHT; break;
811 case K_C_LEFT: c = K_C_RIGHT; break;
812 case K_RIGHT: c = K_LEFT; break;
813 case K_S_RIGHT: c = K_S_LEFT; break;
814 case K_C_RIGHT: c = K_C_LEFT; break;
815 }
816#endif
817
818#ifdef FEAT_VISUAL
819 /*
820 * If 'keymodel' contains "startsel", may start selection. If it
821 * does, a CTRL-O and c will be stuffed, we need to get these
822 * characters.
823 */
824 if (ins_start_select(c))
825 continue;
826#endif
827
828 /*
829 * The big switch to handle a character in insert mode.
830 */
831 switch (c)
832 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000833 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (echeck_abbr(ESC + ABBR_OFF))
835 break;
836 /*FALLTHROUGH*/
837
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000838 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839#ifdef FEAT_CMDWIN
840 if (c == Ctrl_C && cmdwin_type != 0)
841 {
842 /* Close the cmdline window. */
843 cmdwin_result = K_IGNORE;
844 got_int = FALSE; /* don't stop executing autocommands et al. */
845 goto doESCkey;
846 }
847#endif
848
849#ifdef UNIX
850do_intr:
851#endif
852 /* when 'insertmode' set, and not halfway a mapping, don't leave
853 * Insert mode */
854 if (goto_im())
855 {
856 if (got_int)
857 {
858 (void)vgetc(); /* flush all buffers */
859 got_int = FALSE;
860 }
861 else
862 vim_beep();
863 break;
864 }
865doESCkey:
866 /*
867 * This is the ONLY return from edit()!
868 */
869 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
870 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000871 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 o_lnum = curwin->w_cursor.lnum;
873
Bram Moolenaar488c6512005-08-11 20:09:58 +0000874 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000875 {
876#ifdef FEAT_AUTOCMD
877 if (cmdchar != 'r' && cmdchar != 'v')
878 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
879 FALSE, curbuf);
880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 continue;
884
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000885 case Ctrl_Z: /* suspend when 'insertmode' set */
886 if (!p_im)
887 goto normalchar; /* insert CTRL-Z as normal char */
888 stuffReadbuff((char_u *)":st\r");
889 c = Ctrl_O;
890 /*FALLTHROUGH*/
891
892 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000893#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000894 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000895 goto docomplete;
896#endif
897 if (echeck_abbr(Ctrl_O + ABBR_OFF))
898 break;
899 ins_ctrl_o();
900 count = 0;
901 goto doESCkey;
902
Bram Moolenaar572cb562005-08-05 21:35:02 +0000903 case K_INS: /* toggle insert/replace mode */
904 case K_KINS:
905 ins_insert(replaceState);
906 break;
907
908 case K_SELECT: /* end of Select mode mapping - ignore */
909 break;
910
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000911#ifdef FEAT_SNIFF
912 case K_SNIFF: /* Sniff command received */
913 stuffcharReadbuff(K_SNIFF);
914 goto doESCkey;
915#endif
916
917 case K_HELP: /* Help key works like <ESC> <Help> */
918 case K_F1:
919 case K_XF1:
920 stuffcharReadbuff(K_HELP);
921 if (p_im)
922 need_start_insertmode = TRUE;
923 goto doESCkey;
924
925#ifdef FEAT_NETBEANS_INTG
926 case K_F21: /* NetBeans command */
927 ++no_mapping; /* don't map the next key hits */
928 i = safe_vgetc();
929 --no_mapping;
930 netbeans_keycommand(i);
931 break;
932#endif
933
934 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 case NUL:
936 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000937 /* For ^@ the trailing ESC will end the insert, unless there is an
938 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
940 && c != Ctrl_A && !p_im)
941 goto doESCkey; /* quit insert mode */
942 inserted_space = FALSE;
943 break;
944
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000945 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 ins_reg();
947 auto_format(FALSE, TRUE);
948 inserted_space = FALSE;
949 break;
950
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000951 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 ins_ctrl_g();
953 break;
954
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000955 case Ctrl_HAT: /* switch input mode and/or langmap */
956 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 break;
958
959#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000960 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 if (!p_ari)
962 goto normalchar;
963 ins_ctrl_();
964 break;
965#endif
966
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000967 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
969 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
970 goto docomplete;
971#endif
972 /* FALLTHROUGH */
973
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000974 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975# ifdef FEAT_INS_EXPAND
976 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
977 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000978 if (has_compl_option(FALSE))
979 goto docomplete;
980 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 }
982# endif
983 ins_shift(c, lastc);
984 auto_format(FALSE, TRUE);
985 inserted_space = FALSE;
986 break;
987
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000988 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 case K_KDEL:
990 ins_del();
991 auto_format(FALSE, TRUE);
992 break;
993
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000994 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 case Ctrl_H:
996 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
997 auto_format(FALSE, TRUE);
998 break;
999
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001000 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1002 auto_format(FALSE, TRUE);
1003 break;
1004
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001005 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001006# ifdef FEAT_COMPL_FUNC
1007 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001008 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001009 goto docomplete;
1010# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1012 auto_format(FALSE, TRUE);
1013 inserted_space = FALSE;
1014 break;
1015
1016#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001017 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 case K_LEFTMOUSE_NM:
1019 case K_LEFTDRAG:
1020 case K_LEFTRELEASE:
1021 case K_LEFTRELEASE_NM:
1022 case K_MIDDLEMOUSE:
1023 case K_MIDDLEDRAG:
1024 case K_MIDDLERELEASE:
1025 case K_RIGHTMOUSE:
1026 case K_RIGHTDRAG:
1027 case K_RIGHTRELEASE:
1028 case K_X1MOUSE:
1029 case K_X1DRAG:
1030 case K_X1RELEASE:
1031 case K_X2MOUSE:
1032 case K_X2DRAG:
1033 case K_X2RELEASE:
1034 ins_mouse(c);
1035 break;
1036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 ins_mousescroll(FALSE);
1039 break;
1040
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001041 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 ins_mousescroll(TRUE);
1043 break;
1044#endif
1045
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001046 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 break;
1048
Bram Moolenaar754b5602006-02-09 23:53:20 +00001049#ifdef FEAT_AUTOCMD
1050 case K_CURSORHOLD: /* Didn't type something for a while. */
1051 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1052 did_cursorhold = TRUE;
1053 break;
1054#endif
1055
Bram Moolenaar4770d092006-01-12 23:22:24 +00001056#ifdef FEAT_GUI_W32
1057 /* On Win32 ignore <M-F4>, we get it when closing the window was
1058 * cancelled. */
1059 case K_F4:
1060 if (mod_mask != MOD_MASK_ALT)
1061 goto normalchar;
1062 break;
1063#endif
1064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065#ifdef FEAT_GUI
1066 case K_VER_SCROLLBAR:
1067 ins_scroll();
1068 break;
1069
1070 case K_HOR_SCROLLBAR:
1071 ins_horscroll();
1072 break;
1073#endif
1074
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001075 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 case K_S_HOME:
1078 case K_C_HOME:
1079 ins_home(c);
1080 break;
1081
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001082 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 case K_S_END:
1085 case K_C_END:
1086 ins_end(c);
1087 break;
1088
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001089 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001090 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1091 ins_s_left();
1092 else
1093 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 break;
1095
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001096 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 case K_C_LEFT:
1098 ins_s_left();
1099 break;
1100
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001101 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001102 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1103 ins_s_right();
1104 else
1105 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 case K_C_RIGHT:
1110 ins_s_right();
1111 break;
1112
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001113 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001114#ifdef FEAT_INS_EXPAND
1115 if (pum_visible())
1116 goto docomplete;
1117#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001118 if (mod_mask & MOD_MASK_SHIFT)
1119 ins_pageup();
1120 else
1121 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 break;
1123
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001124 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 case K_PAGEUP:
1126 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001127#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001128 if (pum_visible())
1129 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 ins_pageup();
1132 break;
1133
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001134 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001135#ifdef FEAT_INS_EXPAND
1136 if (pum_visible())
1137 goto docomplete;
1138#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001139 if (mod_mask & MOD_MASK_SHIFT)
1140 ins_pagedown();
1141 else
1142 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 break;
1144
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001145 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 case K_PAGEDOWN:
1147 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001148#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001149 if (pum_visible())
1150 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 ins_pagedown();
1153 break;
1154
1155#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001156 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 ins_drop();
1158 break;
1159#endif
1160
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001161 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 c = TAB;
1163 /* FALLTHROUGH */
1164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1167 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1168 goto docomplete;
1169#endif
1170 inserted_space = FALSE;
1171 if (ins_tab())
1172 goto normalchar; /* insert TAB as a normal char */
1173 auto_format(FALSE, TRUE);
1174 break;
1175
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001176 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 c = CAR;
1178 /* FALLTHROUGH */
1179 case CAR:
1180 case NL:
1181#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1182 /* In a quickfix window a <CR> jumps to the error under the
1183 * cursor. */
1184 if (bt_quickfix(curbuf) && c == CAR)
1185 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001186 if (curwin->w_llist_ref == NULL) /* quickfix window */
1187 do_cmdline_cmd((char_u *)".cc");
1188 else /* location list window */
1189 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 break;
1191 }
1192#endif
1193#ifdef FEAT_CMDWIN
1194 if (cmdwin_type != 0)
1195 {
1196 /* Execute the command in the cmdline window. */
1197 cmdwin_result = CAR;
1198 goto doESCkey;
1199 }
1200#endif
1201 if (ins_eol(c) && !p_im)
1202 goto doESCkey; /* out of memory */
1203 auto_format(FALSE, FALSE);
1204 inserted_space = FALSE;
1205 break;
1206
1207#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001208 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209# ifdef FEAT_INS_EXPAND
1210 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1211 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001212 if (has_compl_option(TRUE))
1213 goto docomplete;
1214 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 }
1216# endif
1217# ifdef FEAT_DIGRAPHS
1218 c = ins_digraph();
1219 if (c == NUL)
1220 break;
1221# endif
1222 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
1225#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001226 case Ctrl_X: /* Enter CTRL-X mode */
1227 ins_ctrl_x();
1228 break;
1229
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001230 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 if (ctrl_x_mode != CTRL_X_TAGS)
1232 goto normalchar;
1233 goto docomplete;
1234
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001235 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (ctrl_x_mode != CTRL_X_FILES)
1237 goto normalchar;
1238 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001239
1240 case 's': /* Spelling completion after ^X */
1241 case Ctrl_S:
1242 if (ctrl_x_mode != CTRL_X_SPELL)
1243 goto normalchar;
1244 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245#endif
1246
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001247 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248#ifdef FEAT_INS_EXPAND
1249 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1250#endif
1251 {
1252 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1253 if (p_im)
1254 {
1255 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1256 break;
1257 goto doESCkey;
1258 }
1259 goto normalchar;
1260 }
1261#ifdef FEAT_INS_EXPAND
1262 /* FALLTHROUGH */
1263
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001264 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 case Ctrl_N:
1266 /* if 'complete' is empty then plain ^P is no longer special,
1267 * but it is under other ^X modes */
1268 if (*curbuf->b_p_cpt == NUL
1269 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001270 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 goto normalchar;
1272
1273docomplete:
1274 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001275 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 break;
1277#endif /* FEAT_INS_EXPAND */
1278
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001279 case Ctrl_Y: /* copy from previous line or scroll down */
1280 case Ctrl_E: /* copy from next line or scroll up */
1281 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 break;
1283
1284 default:
1285#ifdef UNIX
1286 if (c == intr_char) /* special interrupt char */
1287 goto do_intr;
1288#endif
1289
1290 /*
1291 * Insert a nomal character.
1292 */
1293normalchar:
1294#ifdef FEAT_SMARTINDENT
1295 /* Try to perform smart-indenting. */
1296 ins_try_si(c);
1297#endif
1298
1299 if (c == ' ')
1300 {
1301 inserted_space = TRUE;
1302#ifdef FEAT_CINDENT
1303 if (inindent(0))
1304 can_cindent = FALSE;
1305#endif
1306 if (Insstart_blank_vcol == MAXCOL
1307 && curwin->w_cursor.lnum == Insstart.lnum)
1308 Insstart_blank_vcol = get_nolist_virtcol();
1309 }
1310
1311 if (vim_iswordc(c) || !echeck_abbr(
1312#ifdef FEAT_MBYTE
1313 /* Add ABBR_OFF for characters above 0x100, this is
1314 * what check_abbr() expects. */
1315 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1316#endif
1317 c))
1318 {
1319 insert_special(c, FALSE, FALSE);
1320#ifdef FEAT_RIGHTLEFT
1321 revins_legal++;
1322 revins_chars++;
1323#endif
1324 }
1325
1326 auto_format(FALSE, TRUE);
1327
1328#ifdef FEAT_FOLDING
1329 /* When inserting a character the cursor line must never be in a
1330 * closed fold. */
1331 foldOpenCursor();
1332#endif
1333 break;
1334 } /* end of switch (c) */
1335
1336 /* If the cursor was moved we didn't just insert a space */
1337 if (arrow_used)
1338 inserted_space = FALSE;
1339
1340#ifdef FEAT_CINDENT
1341 if (can_cindent && cindent_on()
1342# ifdef FEAT_INS_EXPAND
1343 && ctrl_x_mode == 0
1344# endif
1345 )
1346 {
1347force_cindent:
1348 /*
1349 * Indent now if a key was typed that is in 'cinkeys'.
1350 */
1351 if (in_cinkeys(c, ' ', line_is_white))
1352 {
1353 if (stop_arrow() == OK)
1354 /* re-indent the current line */
1355 do_c_expr_indent();
1356 }
1357 }
1358#endif /* FEAT_CINDENT */
1359
1360 } /* for (;;) */
1361 /* NOTREACHED */
1362}
1363
1364/*
1365 * Redraw for Insert mode.
1366 * This is postponed until getting the next character to make '$' in the 'cpo'
1367 * option work correctly.
1368 * Only redraw when there are no characters available. This speeds up
1369 * inserting sequences of characters (e.g., for CTRL-R).
1370 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001371/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001373ins_redraw(ready)
1374 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
1376 if (!char_avail())
1377 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001378#ifdef FEAT_AUTOCMD
1379 /* Trigger CursorMoved if the cursor moved. */
1380 if (ready && has_cursormovedI()
1381 && !equalpos(last_cursormoved, curwin->w_cursor))
1382 {
1383 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1384 last_cursormoved = curwin->w_cursor;
1385 }
1386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (must_redraw)
1388 update_screen(0);
1389 else if (clear_cmdline || redraw_cmdline)
1390 showmode(); /* clear cmdline and show mode */
1391 showruler(FALSE);
1392 setcursor();
1393 emsg_on_display = FALSE; /* may remove error message now */
1394 }
1395}
1396
1397/*
1398 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1399 */
1400 static void
1401ins_ctrl_v()
1402{
1403 int c;
1404
1405 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001406 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
1408 if (redrawing() && !char_avail())
1409 edit_putchar('^', TRUE);
1410 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1411
1412#ifdef FEAT_CMDL_INFO
1413 add_to_showcmd_c(Ctrl_V);
1414#endif
1415
1416 c = get_literal();
1417#ifdef FEAT_CMDL_INFO
1418 clear_showcmd();
1419#endif
1420 insert_special(c, FALSE, TRUE);
1421#ifdef FEAT_RIGHTLEFT
1422 revins_chars++;
1423 revins_legal++;
1424#endif
1425}
1426
1427/*
1428 * Put a character directly onto the screen. It's not stored in a buffer.
1429 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1430 */
1431static int pc_status;
1432#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1433#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1434#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1435#define PC_STATUS_SET 3 /* pc_bytes was filled */
1436#ifdef FEAT_MBYTE
1437static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1438#else
1439static char_u pc_bytes[2]; /* saved bytes */
1440#endif
1441static int pc_attr;
1442static int pc_row;
1443static int pc_col;
1444
1445 void
1446edit_putchar(c, highlight)
1447 int c;
1448 int highlight;
1449{
1450 int attr;
1451
1452 if (ScreenLines != NULL)
1453 {
1454 update_topline(); /* just in case w_topline isn't valid */
1455 validate_cursor();
1456 if (highlight)
1457 attr = hl_attr(HLF_8);
1458 else
1459 attr = 0;
1460 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1461 pc_col = W_WINCOL(curwin);
1462#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1463 pc_status = PC_STATUS_UNSET;
1464#endif
1465#ifdef FEAT_RIGHTLEFT
1466 if (curwin->w_p_rl)
1467 {
1468 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1469# ifdef FEAT_MBYTE
1470 if (has_mbyte)
1471 {
1472 int fix_col = mb_fix_col(pc_col, pc_row);
1473
1474 if (fix_col != pc_col)
1475 {
1476 screen_putchar(' ', pc_row, fix_col, attr);
1477 --curwin->w_wcol;
1478 pc_status = PC_STATUS_RIGHT;
1479 }
1480 }
1481# endif
1482 }
1483 else
1484#endif
1485 {
1486 pc_col += curwin->w_wcol;
1487#ifdef FEAT_MBYTE
1488 if (mb_lefthalve(pc_row, pc_col))
1489 pc_status = PC_STATUS_LEFT;
1490#endif
1491 }
1492
1493 /* save the character to be able to put it back */
1494#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1495 if (pc_status == PC_STATUS_UNSET)
1496#endif
1497 {
1498 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1499 pc_status = PC_STATUS_SET;
1500 }
1501 screen_putchar(c, pc_row, pc_col, attr);
1502 }
1503}
1504
1505/*
1506 * Undo the previous edit_putchar().
1507 */
1508 void
1509edit_unputchar()
1510{
1511 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1512 {
1513#if defined(FEAT_MBYTE)
1514 if (pc_status == PC_STATUS_RIGHT)
1515 ++curwin->w_wcol;
1516 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1517 redrawWinline(curwin->w_cursor.lnum, FALSE);
1518 else
1519#endif
1520 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1521 }
1522}
1523
1524/*
1525 * Called when p_dollar is set: display a '$' at the end of the changed text
1526 * Only works when cursor is in the line that changes.
1527 */
1528 void
1529display_dollar(col)
1530 colnr_T col;
1531{
1532 colnr_T save_col;
1533
1534 if (!redrawing())
1535 return;
1536
1537 cursor_off();
1538 save_col = curwin->w_cursor.col;
1539 curwin->w_cursor.col = col;
1540#ifdef FEAT_MBYTE
1541 if (has_mbyte)
1542 {
1543 char_u *p;
1544
1545 /* If on the last byte of a multi-byte move to the first byte. */
1546 p = ml_get_curline();
1547 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1548 }
1549#endif
1550 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1551 if (curwin->w_wcol < W_WIDTH(curwin))
1552 {
1553 edit_putchar('$', FALSE);
1554 dollar_vcol = curwin->w_virtcol;
1555 }
1556 curwin->w_cursor.col = save_col;
1557}
1558
1559/*
1560 * Call this function before moving the cursor from the normal insert position
1561 * in insert mode.
1562 */
1563 static void
1564undisplay_dollar()
1565{
1566 if (dollar_vcol)
1567 {
1568 dollar_vcol = 0;
1569 redrawWinline(curwin->w_cursor.lnum, FALSE);
1570 }
1571}
1572
1573/*
1574 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1575 * Keep the cursor on the same character.
1576 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1577 * type == INDENT_DEC decrease indent (for CTRL-D)
1578 * type == INDENT_SET set indent to "amount"
1579 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1580 */
1581 void
1582change_indent(type, amount, round, replaced)
1583 int type;
1584 int amount;
1585 int round;
1586 int replaced; /* replaced character, put on replace stack */
1587{
1588 int vcol;
1589 int last_vcol;
1590 int insstart_less; /* reduction for Insstart.col */
1591 int new_cursor_col;
1592 int i;
1593 char_u *ptr;
1594 int save_p_list;
1595 int start_col;
1596 colnr_T vc;
1597#ifdef FEAT_VREPLACE
1598 colnr_T orig_col = 0; /* init for GCC */
1599 char_u *new_line, *orig_line = NULL; /* init for GCC */
1600
1601 /* VREPLACE mode needs to know what the line was like before changing */
1602 if (State & VREPLACE_FLAG)
1603 {
1604 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1605 orig_col = curwin->w_cursor.col;
1606 }
1607#endif
1608
1609 /* for the following tricks we don't want list mode */
1610 save_p_list = curwin->w_p_list;
1611 curwin->w_p_list = FALSE;
1612 vc = getvcol_nolist(&curwin->w_cursor);
1613 vcol = vc;
1614
1615 /*
1616 * For Replace mode we need to fix the replace stack later, which is only
1617 * possible when the cursor is in the indent. Remember the number of
1618 * characters before the cursor if it's possible.
1619 */
1620 start_col = curwin->w_cursor.col;
1621
1622 /* determine offset from first non-blank */
1623 new_cursor_col = curwin->w_cursor.col;
1624 beginline(BL_WHITE);
1625 new_cursor_col -= curwin->w_cursor.col;
1626
1627 insstart_less = curwin->w_cursor.col;
1628
1629 /*
1630 * If the cursor is in the indent, compute how many screen columns the
1631 * cursor is to the left of the first non-blank.
1632 */
1633 if (new_cursor_col < 0)
1634 vcol = get_indent() - vcol;
1635
1636 if (new_cursor_col > 0) /* can't fix replace stack */
1637 start_col = -1;
1638
1639 /*
1640 * Set the new indent. The cursor will be put on the first non-blank.
1641 */
1642 if (type == INDENT_SET)
1643 (void)set_indent(amount, SIN_CHANGED);
1644 else
1645 {
1646#ifdef FEAT_VREPLACE
1647 int save_State = State;
1648
1649 /* Avoid being called recursively. */
1650 if (State & VREPLACE_FLAG)
1651 State = INSERT;
1652#endif
1653 shift_line(type == INDENT_DEC, round, 1);
1654#ifdef FEAT_VREPLACE
1655 State = save_State;
1656#endif
1657 }
1658 insstart_less -= curwin->w_cursor.col;
1659
1660 /*
1661 * Try to put cursor on same character.
1662 * If the cursor is at or after the first non-blank in the line,
1663 * compute the cursor column relative to the column of the first
1664 * non-blank character.
1665 * If we are not in insert mode, leave the cursor on the first non-blank.
1666 * If the cursor is before the first non-blank, position it relative
1667 * to the first non-blank, counted in screen columns.
1668 */
1669 if (new_cursor_col >= 0)
1670 {
1671 /*
1672 * When changing the indent while the cursor is touching it, reset
1673 * Insstart_col to 0.
1674 */
1675 if (new_cursor_col == 0)
1676 insstart_less = MAXCOL;
1677 new_cursor_col += curwin->w_cursor.col;
1678 }
1679 else if (!(State & INSERT))
1680 new_cursor_col = curwin->w_cursor.col;
1681 else
1682 {
1683 /*
1684 * Compute the screen column where the cursor should be.
1685 */
1686 vcol = get_indent() - vcol;
1687 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1688
1689 /*
1690 * Advance the cursor until we reach the right screen column.
1691 */
1692 vcol = last_vcol = 0;
1693 new_cursor_col = -1;
1694 ptr = ml_get_curline();
1695 while (vcol <= (int)curwin->w_virtcol)
1696 {
1697 last_vcol = vcol;
1698#ifdef FEAT_MBYTE
1699 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001700 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 else
1702#endif
1703 ++new_cursor_col;
1704 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1705 }
1706 vcol = last_vcol;
1707
1708 /*
1709 * May need to insert spaces to be able to position the cursor on
1710 * the right screen column.
1711 */
1712 if (vcol != (int)curwin->w_virtcol)
1713 {
1714 curwin->w_cursor.col = new_cursor_col;
1715 i = (int)curwin->w_virtcol - vcol;
1716 ptr = alloc(i + 1);
1717 if (ptr != NULL)
1718 {
1719 new_cursor_col += i;
1720 ptr[i] = NUL;
1721 while (--i >= 0)
1722 ptr[i] = ' ';
1723 ins_str(ptr);
1724 vim_free(ptr);
1725 }
1726 }
1727
1728 /*
1729 * When changing the indent while the cursor is in it, reset
1730 * Insstart_col to 0.
1731 */
1732 insstart_less = MAXCOL;
1733 }
1734
1735 curwin->w_p_list = save_p_list;
1736
1737 if (new_cursor_col <= 0)
1738 curwin->w_cursor.col = 0;
1739 else
1740 curwin->w_cursor.col = new_cursor_col;
1741 curwin->w_set_curswant = TRUE;
1742 changed_cline_bef_curs();
1743
1744 /*
1745 * May have to adjust the start of the insert.
1746 */
1747 if (State & INSERT)
1748 {
1749 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1750 {
1751 if ((int)Insstart.col <= insstart_less)
1752 Insstart.col = 0;
1753 else
1754 Insstart.col -= insstart_less;
1755 }
1756 if ((int)ai_col <= insstart_less)
1757 ai_col = 0;
1758 else
1759 ai_col -= insstart_less;
1760 }
1761
1762 /*
1763 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1764 * If the number of characters before the cursor decreased, need to pop a
1765 * few characters from the replace stack.
1766 * If the number of characters before the cursor increased, need to push a
1767 * few NULs onto the replace stack.
1768 */
1769 if (REPLACE_NORMAL(State) && start_col >= 0)
1770 {
1771 while (start_col > (int)curwin->w_cursor.col)
1772 {
1773 replace_join(0); /* remove a NUL from the replace stack */
1774 --start_col;
1775 }
1776 while (start_col < (int)curwin->w_cursor.col || replaced)
1777 {
1778 replace_push(NUL);
1779 if (replaced)
1780 {
1781 replace_push(replaced);
1782 replaced = NUL;
1783 }
1784 ++start_col;
1785 }
1786 }
1787
1788#ifdef FEAT_VREPLACE
1789 /*
1790 * For VREPLACE mode, we also have to fix the replace stack. In this case
1791 * it is always possible because we backspace over the whole line and then
1792 * put it back again the way we wanted it.
1793 */
1794 if (State & VREPLACE_FLAG)
1795 {
1796 /* If orig_line didn't allocate, just return. At least we did the job,
1797 * even if you can't backspace. */
1798 if (orig_line == NULL)
1799 return;
1800
1801 /* Save new line */
1802 new_line = vim_strsave(ml_get_curline());
1803 if (new_line == NULL)
1804 return;
1805
1806 /* We only put back the new line up to the cursor */
1807 new_line[curwin->w_cursor.col] = NUL;
1808
1809 /* Put back original line */
1810 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1811 curwin->w_cursor.col = orig_col;
1812
1813 /* Backspace from cursor to start of line */
1814 backspace_until_column(0);
1815
1816 /* Insert new stuff into line again */
1817 ins_bytes(new_line);
1818
1819 vim_free(new_line);
1820 }
1821#endif
1822}
1823
1824/*
1825 * Truncate the space at the end of a line. This is to be used only in an
1826 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1827 * modes.
1828 */
1829 void
1830truncate_spaces(line)
1831 char_u *line;
1832{
1833 int i;
1834
1835 /* find start of trailing white space */
1836 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1837 {
1838 if (State & REPLACE_FLAG)
1839 replace_join(0); /* remove a NUL from the replace stack */
1840 }
1841 line[i + 1] = NUL;
1842}
1843
1844#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1845 || defined(FEAT_COMMENTS) || defined(PROTO)
1846/*
1847 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1848 * modes correctly. May also be used when not in insert mode at all.
1849 */
1850 void
1851backspace_until_column(col)
1852 int col;
1853{
1854 while ((int)curwin->w_cursor.col > col)
1855 {
1856 curwin->w_cursor.col--;
1857 if (State & REPLACE_FLAG)
1858 replace_do_bs();
1859 else
1860 (void)del_char(FALSE);
1861 }
1862}
1863#endif
1864
1865#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1866/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001867 * CTRL-X pressed in Insert mode.
1868 */
1869 static void
1870ins_ctrl_x()
1871{
1872 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1873 * CTRL-V works like CTRL-N */
1874 if (ctrl_x_mode != CTRL_X_CMDLINE)
1875 {
1876 /* if the next ^X<> won't ADD nothing, then reset
1877 * compl_cont_status */
1878 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001879 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001880 else
1881 compl_cont_status = 0;
1882 /* We're not sure which CTRL-X mode it will be yet */
1883 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1884 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1885 edit_submode_pre = NULL;
1886 showmode();
1887 }
1888}
1889
1890/*
1891 * Return TRUE if the 'dict' or 'tsr' option can be used.
1892 */
1893 static int
1894has_compl_option(dict_opt)
1895 int dict_opt;
1896{
1897 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1898 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1899 {
1900 ctrl_x_mode = 0;
1901 edit_submode = NULL;
1902 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1903 : (char_u *)_("'thesaurus' option is empty"),
1904 hl_attr(HLF_E));
1905 if (emsg_silent == 0)
1906 {
1907 vim_beep();
1908 setcursor();
1909 out_flush();
1910 ui_delay(2000L, FALSE);
1911 }
1912 return FALSE;
1913 }
1914 return TRUE;
1915}
1916
1917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1919 * This depends on the current mode.
1920 */
1921 int
1922vim_is_ctrl_x_key(c)
1923 int c;
1924{
1925 /* Always allow ^R - let it's results then be checked */
1926 if (c == Ctrl_R)
1927 return TRUE;
1928
Bram Moolenaare3226be2005-12-18 22:10:00 +00001929 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001930 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001931 return TRUE;
1932
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 switch (ctrl_x_mode)
1934 {
1935 case 0: /* Not in any CTRL-X mode */
1936 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1937 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001938 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1940 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1941 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001942 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1943 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 case CTRL_X_SCROLL:
1945 return (c == Ctrl_Y || c == Ctrl_E);
1946 case CTRL_X_WHOLE_LINE:
1947 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1948 case CTRL_X_FILES:
1949 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1950 case CTRL_X_DICTIONARY:
1951 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1952 case CTRL_X_THESAURUS:
1953 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1954 case CTRL_X_TAGS:
1955 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1956#ifdef FEAT_FIND_ID
1957 case CTRL_X_PATH_PATTERNS:
1958 return (c == Ctrl_P || c == Ctrl_N);
1959 case CTRL_X_PATH_DEFINES:
1960 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1961#endif
1962 case CTRL_X_CMDLINE:
1963 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1964 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001965#ifdef FEAT_COMPL_FUNC
1966 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001967 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001968 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001969 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001970#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001971 case CTRL_X_SPELL:
1972 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 }
1974 EMSG(_(e_internal));
1975 return FALSE;
1976}
1977
1978/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001979 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 * case of the originally typed text is used, and the case of the completed
1981 * text is infered, ie this tries to work out what case you probably wanted
1982 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001983 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 */
1985 int
Bram Moolenaar572cb562005-08-05 21:35:02 +00001986ins_compl_add_infercase(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 char_u *str;
1988 int len;
1989 char_u *fname;
1990 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001991 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992{
1993 int has_lower = FALSE;
1994 int was_letter = FALSE;
1995 int idx;
1996
1997 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
1998 {
1999 /* Infer case of completed part -- webb */
2000 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002001 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002
2003 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002004 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002006 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 has_lower = TRUE;
2009 if (isupper(IObuff[idx]))
2010 {
2011 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002012 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2014 break;
2015 }
2016 }
2017 }
2018
2019 /*
2020 * Rule 2: No lower case, 2nd consecutive letter converted to
2021 * upper case.
2022 */
2023 if (!has_lower)
2024 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002025 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002027 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 && islower(IObuff[idx]))
2029 {
2030 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002031 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2033 break;
2034 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002035 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 }
2037 }
2038
2039 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002040 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002042 return ins_compl_add(IObuff, len, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002044 return ins_compl_add(str, len, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045}
2046
2047/*
2048 * Add a match to the list of matches.
2049 * If the given string is already in the list of completions, then return
2050 * FAIL, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaar572cb562005-08-05 21:35:02 +00002051 * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb.
2052 *
2053 * New:
2054 * If the given string is already in the list of completions, then return
2055 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2056 * maybe because alloc() returns NULL, then FAIL is returned -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002058 int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002059ins_compl_add(str, len, fname, extra, cdir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 char_u *str;
2061 int len;
2062 char_u *fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002063 char_u *extra; /* extra text for popup menu or NULL */
2064 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002065 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002067 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002068 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070 ui_breakcheck();
2071 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002072 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 if (len < 0)
2074 len = (int)STRLEN(str);
2075
2076 /*
2077 * If the same match is already present, don't add it.
2078 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002079 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002081 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 do
2083 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002084 if ( !(match->cp_flags & ORIGINAL_TEXT)
2085 && STRNCMP(match->cp_str, str, (size_t)len) == 0
2086 && match->cp_str[len] == NUL)
2087 return NOTDONE;
2088 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002089 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 }
2091
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002092 /* Remove any popup menu before changing the list of matches. */
2093 ins_compl_del_pum();
2094
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 /*
2096 * Allocate a new match structure.
2097 * Copy the values to the new match structure.
2098 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002099 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002101 return FAIL;
2102 match->cp_number = -1;
2103 if (flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002105 match->cp_number = 0;
2106 match->cp_str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002108 else if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
2110 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002111 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002113
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002115 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2117 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002118 if (fname != NULL
2119 && compl_curr_match
2120 && compl_curr_match->cp_fname != NULL
2121 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002122 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002123 else if (fname != NULL)
2124 {
2125 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002126 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002129 match->cp_fname = NULL;
2130 match->cp_flags = flags;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002131 if (extra != NULL)
2132 match->cp_extra = vim_strsave(extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
2134 /*
2135 * Link the new match structure in the list of matches.
2136 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002137 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002138 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 else if (dir == FORWARD)
2140 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002141 match->cp_next = compl_curr_match->cp_next;
2142 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
2144 else /* BACKWARD */
2145 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002146 match->cp_next = compl_curr_match;
2147 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002149 if (match->cp_next)
2150 match->cp_next->cp_prev = match;
2151 if (match->cp_prev)
2152 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002154 compl_first_match = match;
2155 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002157 /*
2158 * Find the longest common string if still doing that.
2159 */
2160 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2161 ins_compl_longest_match(match);
2162
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 return OK;
2164}
2165
2166/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002167 * Reduce the longest common string for match "match".
2168 */
2169 static void
2170ins_compl_longest_match(match)
2171 compl_T *match;
2172{
2173 char_u *p, *s;
2174 int l;
2175 int had_match;
2176
2177 if (compl_leader == NULL)
2178 /* First match, use it as a whole. */
2179 compl_leader = vim_strsave(match->cp_str);
2180 else
2181 {
2182 /* Reduce the text if this match differs from compl_leader. */
2183 for (p = compl_leader, s = match->cp_str; *p != NUL; p += l, s += l)
2184 {
2185#ifdef FEAT_MBYTE
2186 if (has_mbyte)
2187 {
2188 l = mb_ptr2len(p);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002189 if (STRNICMP(p, s, l) != 0)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002190 break;
2191 }
2192 else
2193#endif
2194 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002195 if (MB_TOLOWER(*p) != MB_TOLOWER(*s))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002196 break;
2197 l = 1;
2198 }
2199 }
2200
2201 if (*p != NUL)
2202 {
2203 /* Leader was shortened, need to change the inserted text. */
2204 *p = NUL;
2205 had_match = (curwin->w_cursor.col > compl_col);
2206 ins_compl_delete();
2207 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2208 ins_redraw(FALSE);
2209
2210 /* When the match isn't there (to avoid matching itself) remove it
2211 * again after redrawing. */
2212 if (!had_match)
2213 ins_compl_delete();
2214 }
2215
2216 compl_used_match = FALSE;
2217 }
2218}
2219
2220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 * Add an array of matches to the list of matches.
2222 * Frees matches[].
2223 */
2224 static void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002225ins_compl_add_matches(num_matches, matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 int num_matches;
2227 char_u **matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
2229 int i;
2230 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002231 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
Bram Moolenaar572cb562005-08-05 21:35:02 +00002233 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002234 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, dir, 0)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002236 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 FreeWild(num_matches, matches);
2238}
2239
2240/* Make the completion list cyclic.
2241 * Return the number of matches (excluding the original).
2242 */
2243 static int
2244ins_compl_make_cyclic()
2245{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002246 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 int count = 0;
2248
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002249 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {
2251 /*
2252 * Find the end of the list.
2253 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002254 match = compl_first_match;
2255 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002256 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002258 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 ++count;
2260 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002261 match->cp_next = compl_first_match;
2262 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 }
2264 return count;
2265}
2266
Bram Moolenaar9372a112005-12-06 19:59:18 +00002267/* "compl_match_array" points the currently displayed list of entries in the
2268 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002269static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002270static int compl_match_arraysize;
2271
2272/*
2273 * Update the screen and when there is any scrolling remove the popup menu.
2274 */
2275 static void
2276ins_compl_upd_pum()
2277{
2278 int h;
2279
2280 if (compl_match_array != NULL)
2281 {
2282 h = curwin->w_cline_height;
2283 update_screen(0);
2284 if (h != curwin->w_cline_height)
2285 ins_compl_del_pum();
2286 }
2287}
2288
2289/*
2290 * Remove any popup menu.
2291 */
2292 static void
2293ins_compl_del_pum()
2294{
2295 if (compl_match_array != NULL)
2296 {
2297 pum_undisplay();
2298 vim_free(compl_match_array);
2299 compl_match_array = NULL;
2300 }
2301}
2302
2303/*
2304 * Return TRUE if the popup menu should be displayed.
2305 */
2306 static int
2307pum_wanted()
2308{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002309 /* 'completeopt' must contain "menu" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002310 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002311 return FALSE;
2312
2313 /* The display looks bad on a B&W display. */
2314 if (t_colors < 8
2315#ifdef FEAT_GUI
2316 && !gui.in_use
2317#endif
2318 )
2319 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002320 return TRUE;
2321}
2322
2323/*
2324 * Return TRUE if there are two or more matches to be shown in the popup menu.
2325 */
2326 static int
2327pum_two_or_more()
2328{
2329 compl_T *compl;
2330 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002331
2332 /* Don't display the popup menu if there are no matches or there is only
2333 * one (ignoring the original text). */
2334 compl = compl_first_match;
2335 i = 0;
2336 do
2337 {
2338 if (compl == NULL
2339 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2340 break;
2341 compl = compl->cp_next;
2342 } while (compl != compl_first_match);
2343
2344 return (i >= 2);
2345}
2346
2347/*
2348 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002349 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002350 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002351 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002352ins_compl_show_pum()
2353{
2354 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002355 compl_T *shown_compl = NULL;
2356 int did_find_shown_match = FALSE;
2357 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002358 int i;
2359 int cur = -1;
2360 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002361 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002362
Bram Moolenaara6557602006-02-04 22:43:20 +00002363 if (!pum_wanted() || !pum_two_or_more())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002364 return;
2365
2366 /* Update the screen before drawing the popup menu over it. */
2367 update_screen(0);
2368
2369 if (compl_match_array == NULL)
2370 {
2371 /* Need to build the popup menu list. */
2372 compl_match_arraysize = 0;
2373 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002374 if (compl_leader != NULL)
2375 lead_len = STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002376 do
2377 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002378 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2379 && (compl_leader == NULL
2380 || STRNCMP(compl->cp_str, compl_leader, lead_len) == 0))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002381 ++compl_match_arraysize;
2382 compl = compl->cp_next;
2383 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002384 if (compl_match_arraysize == 0)
2385 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002386 compl_match_array = (pumitem_T *)alloc_clear(
2387 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002388 * compl_match_arraysize));
2389 if (compl_match_array != NULL)
2390 {
2391 i = 0;
2392 compl = compl_first_match;
2393 do
2394 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002395 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2396 && (compl_leader == NULL
2397 || STRNCMP(compl->cp_str, compl_leader,
2398 lead_len) == 0))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002399 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002400 if (!shown_match_ok)
2401 {
2402 if (compl == compl_shown_match || did_find_shown_match)
2403 {
2404 /* This item is the shown match or this is the
2405 * first displayed item after the shown match. */
2406 compl_shown_match = compl;
2407 did_find_shown_match = TRUE;
2408 shown_match_ok = TRUE;
2409 }
2410 else
2411 /* Remember this displayed match for when the
2412 * shown match is just below it. */
2413 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002414 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002415 }
2416 compl_match_array[i].pum_text = compl->cp_str;
2417 if (compl->cp_extra != NULL)
2418 compl_match_array[i++].pum_extra = compl->cp_extra;
2419 else
2420 compl_match_array[i++].pum_extra = compl->cp_fname;
2421 }
2422
2423 if (compl == compl_shown_match)
2424 {
2425 did_find_shown_match = TRUE;
2426 if (!shown_match_ok && shown_compl != NULL)
2427 {
2428 /* The shown match isn't displayed, set it to the
2429 * previously displayed match. */
2430 compl_shown_match = shown_compl;
2431 shown_match_ok = TRUE;
2432 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002433 }
2434 compl = compl->cp_next;
2435 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002436
2437 if (!shown_match_ok) /* no displayed match at all */
2438 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002439 }
2440 }
2441 else
2442 {
2443 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002444 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002445 if (compl_match_array[i].pum_text == compl_shown_match->cp_str)
Bram Moolenaara6557602006-02-04 22:43:20 +00002446 break;
2447 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002448 }
2449
2450 if (compl_match_array != NULL)
2451 {
2452 /* Compute the screen column of the start of the completed text.
2453 * Use the cursor to get all wrapping and other settings right. */
2454 col = curwin->w_cursor.col;
2455 curwin->w_cursor.col = compl_col;
2456 validate_cursor_col();
2457 pum_display(compl_match_array, compl_match_arraysize, cur,
2458 curwin->w_cline_row + W_WINROW(curwin),
2459 curwin->w_cline_height,
Bram Moolenaar280f1262006-01-30 00:14:18 +00002460 curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002461 curwin->w_cursor.col = col;
2462 }
2463}
2464
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465#define DICT_FIRST (1) /* use just first element in "dict" */
2466#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002467
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468/*
2469 * Add any identifiers that match the given pattern to the list of
2470 * completions.
2471 */
2472 static void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002473ins_compl_dictionaries(dict, pat, flags, thesaurus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 char_u *dict;
2475 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002476 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 int thesaurus;
2478{
2479 char_u *ptr;
2480 char_u *buf;
2481 FILE *fp;
2482 regmatch_T regmatch;
2483 int add_r;
2484 char_u **files;
2485 int count;
2486 int i;
2487 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002488 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489
2490 buf = alloc(LSIZE);
2491 /* If 'infercase' is set, don't use 'smartcase' here */
2492 save_p_scs = p_scs;
2493 if (curbuf->b_p_inf)
2494 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002495
2496 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2497 * to only match at the start of a line. Otherwise just match the
2498 * pattern. */
2499 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2500 {
2501 i = STRLEN(pat) + 8;
2502 ptr = alloc(i);
2503 if (ptr == NULL)
2504 return;
2505 vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat);
2506 regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0);
2507 vim_free(ptr);
2508 }
2509 else
2510 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2511
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2513 regmatch.rm_ic = ignorecase(pat);
2514 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002515 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
2517 /* copy one dictionary file name into buf */
2518 if (flags == DICT_EXACT)
2519 {
2520 count = 1;
2521 files = &dict;
2522 }
2523 else
2524 {
2525 /* Expand wildcards in the dictionary name, but do not allow
2526 * backticks (for security, the 'dict' option may have been set in
2527 * a modeline). */
2528 copy_option_part(&dict, buf, LSIZE, ",");
2529 if (vim_strchr(buf, '`') != NULL
2530 || expand_wildcards(1, &buf, &count, &files,
2531 EW_FILE|EW_SILENT) != OK)
2532 count = 0;
2533 }
2534
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002535 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
2537 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2538 if (flags != DICT_EXACT)
2539 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002540 vim_snprintf((char *)IObuff, IOSIZE,
2541 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2543 }
2544
2545 if (fp != NULL)
2546 {
2547 /*
2548 * Read dictionary file line by line.
2549 * Check each line for a match.
2550 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002551 while (!got_int && !compl_interrupted
2552 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
2554 ptr = buf;
2555 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2556 {
2557 ptr = regmatch.startp[0];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002558 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2559 ptr = find_line_end(ptr);
2560 else
2561 ptr = find_word_end(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 add_r = ins_compl_add_infercase(regmatch.startp[0],
2563 (int)(ptr - regmatch.startp[0]),
2564 files[i], dir, 0);
2565 if (thesaurus)
2566 {
2567 char_u *wstart;
2568
2569 /*
2570 * Add the other matches on the line
2571 */
2572 while (!got_int)
2573 {
2574 /* Find start of the next word. Skip white
2575 * space and punctuation. */
2576 ptr = find_word_start(ptr);
2577 if (*ptr == NUL || *ptr == NL)
2578 break;
2579 wstart = ptr;
2580
2581 /* Find end of the word and add it. */
2582#ifdef FEAT_MBYTE
2583 if (has_mbyte)
2584 /* Japanese words may have characters in
2585 * different classes, only separate words
2586 * with single-byte non-word characters. */
2587 while (*ptr != NUL)
2588 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002589 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
2591 if (l < 2 && !vim_iswordc(*ptr))
2592 break;
2593 ptr += l;
2594 }
2595 else
2596#endif
2597 ptr = find_word_end(ptr);
2598 add_r = ins_compl_add_infercase(wstart,
2599 (int)(ptr - wstart), files[i], dir, 0);
2600 }
2601 }
2602 if (add_r == OK)
2603 /* if dir was BACKWARD then honor it just once */
2604 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002605 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 break;
2607 /* avoid expensive call to vim_regexec() when at end
2608 * of line */
2609 if (*ptr == '\n' || got_int)
2610 break;
2611 }
2612 line_breakcheck();
Bram Moolenaar572cb562005-08-05 21:35:02 +00002613 ins_compl_check_keys(50);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 }
2615 fclose(fp);
2616 }
2617 }
2618 if (flags != DICT_EXACT)
2619 FreeWild(count, files);
2620 if (flags)
2621 break;
2622 }
2623 p_scs = save_p_scs;
2624 vim_free(regmatch.regprog);
2625 vim_free(buf);
2626}
2627
2628/*
2629 * Find the start of the next word.
2630 * Returns a pointer to the first char of the word. Also stops at a NUL.
2631 */
2632 char_u *
2633find_word_start(ptr)
2634 char_u *ptr;
2635{
2636#ifdef FEAT_MBYTE
2637 if (has_mbyte)
2638 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002639 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 else
2641#endif
2642 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2643 ++ptr;
2644 return ptr;
2645}
2646
2647/*
2648 * Find the end of the word. Assumes it starts inside a word.
2649 * Returns a pointer to just after the word.
2650 */
2651 char_u *
2652find_word_end(ptr)
2653 char_u *ptr;
2654{
2655#ifdef FEAT_MBYTE
2656 int start_class;
2657
2658 if (has_mbyte)
2659 {
2660 start_class = mb_get_class(ptr);
2661 if (start_class > 1)
2662 while (*ptr != NUL)
2663 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002664 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (mb_get_class(ptr) != start_class)
2666 break;
2667 }
2668 }
2669 else
2670#endif
2671 while (vim_iswordc(*ptr))
2672 ++ptr;
2673 return ptr;
2674}
2675
2676/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002677 * Find the end of the line, omitting CR and NL at the end.
2678 * Returns a pointer to just after the line.
2679 */
2680 static char_u *
2681find_line_end(ptr)
2682 char_u *ptr;
2683{
2684 char_u *s;
2685
2686 s = ptr + STRLEN(ptr);
2687 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2688 --s;
2689 return s;
2690}
2691
2692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 * Free the list of completions
2694 */
2695 static void
2696ins_compl_free()
2697{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002698 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002700 vim_free(compl_pattern);
2701 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002702 vim_free(compl_leader);
2703 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002705 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002707
2708 ins_compl_del_pum();
2709 pum_clear();
2710
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002711 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 do
2713 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002714 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002715 compl_curr_match = compl_curr_match->cp_next;
2716 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002718 if (match->cp_flags & FREE_FNAME)
2719 vim_free(match->cp_fname);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002720 vim_free(match->cp_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002722 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2723 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724}
2725
2726 static void
2727ins_compl_clear()
2728{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002729 compl_cont_status = 0;
2730 compl_started = FALSE;
2731 compl_matches = 0;
2732 vim_free(compl_pattern);
2733 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002734 vim_free(compl_leader);
2735 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 save_sm = -1;
2737 edit_submode_extra = NULL;
2738}
2739
2740/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002741 * Delete one character before the cursor and show the subset of the matches
2742 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002743 * Returns TRUE if the work is done and another char to be got from the user.
2744 */
2745 static int
2746ins_compl_bs()
2747{
2748 char_u *line;
2749 char_u *p;
2750
2751 if (curwin->w_cursor.col <= compl_col + compl_length)
2752 {
2753 /* Deleted more than what was used to find matches, need to look for
2754 * matches all over again. */
2755 ins_compl_free();
2756 compl_started = FALSE;
2757 compl_matches = 0;
2758 }
2759
2760 line = ml_get_curline();
2761 p = line + curwin->w_cursor.col;
2762 mb_ptr_back(line, p);
2763
2764 vim_free(compl_leader);
2765 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2766 if (compl_leader != NULL)
2767 {
2768 ins_compl_del_pum();
2769 ins_compl_delete();
2770 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2771
2772 if (!compl_started)
2773 {
2774 /* Matches were cleared, need to search for them now. */
2775 if (ins_complete(Ctrl_N) == FAIL)
2776 compl_cont_status = 0;
2777 else
2778 {
2779 /* Remove the completed word again. */
2780 ins_compl_delete();
2781 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2782 }
2783 }
2784
2785 /* Show the popup menu with a different set of matches. */
2786 ins_compl_show_pum();
2787 compl_used_match = FALSE;
2788
2789 return TRUE;
2790 }
2791 return FALSE;
2792}
2793
2794/*
2795 * Append one character to the match leader. May reduce the number of
2796 * matches.
2797 */
2798 static void
2799ins_compl_addleader(c)
2800 int c;
2801{
2802#ifdef FEAT_MBYTE
2803 int cc;
2804
2805 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2806 {
2807 char_u buf[MB_MAXBYTES + 1];
2808
2809 (*mb_char2bytes)(c, buf);
2810 buf[cc] = NUL;
2811 ins_char_bytes(buf, cc);
2812 }
2813 else
2814#endif
2815 ins_char(c);
2816
2817 vim_free(compl_leader);
2818 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
2819 curwin->w_cursor.col - compl_col);
2820 if (compl_leader != NULL)
2821 {
2822 /* Show the popup menu with a different set of matches. */
2823 ins_compl_del_pum();
2824 ins_compl_show_pum();
2825 compl_used_match = FALSE;
2826 }
2827}
2828
2829/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002830 * Append one character to the match leader. May reduce the number of
2831 * matches.
2832 */
2833 static void
2834ins_compl_addfrommatch()
2835{
2836 char_u *p;
2837 int len = curwin->w_cursor.col - compl_col;
2838 int c;
2839
2840 p = compl_shown_match->cp_str;
2841 if (STRLEN(p) <= len) /* the match is too short */
2842 return;
2843 p += len;
2844#ifdef FEAT_MBYTE
2845 c = mb_ptr2char(p);
2846#else
2847 c = *p;
2848#endif
2849 ins_compl_addleader(c);
2850}
2851
2852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002854 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002855 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002857 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858ins_compl_prep(c)
2859 int c;
2860{
2861 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 int temp;
2863 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002864 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865
2866 /* Forget any previous 'special' messages if this is actually
2867 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2868 */
2869 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2870 edit_submode_extra = NULL;
2871
2872 /* Ignore end of Select mode mapping */
2873 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002874 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002876 /* Set "compl_get_longest" when finding the first matches. */
2877 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
2878 || (ctrl_x_mode == 0 && !compl_started))
2879 {
2880 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
2881 compl_used_match = TRUE;
2882 }
2883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2885 {
2886 /*
2887 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2888 * it will be yet. Now we decide.
2889 */
2890 switch (c)
2891 {
2892 case Ctrl_E:
2893 case Ctrl_Y:
2894 ctrl_x_mode = CTRL_X_SCROLL;
2895 if (!(State & REPLACE_FLAG))
2896 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2897 else
2898 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2899 edit_submode_pre = NULL;
2900 showmode();
2901 break;
2902 case Ctrl_L:
2903 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2904 break;
2905 case Ctrl_F:
2906 ctrl_x_mode = CTRL_X_FILES;
2907 break;
2908 case Ctrl_K:
2909 ctrl_x_mode = CTRL_X_DICTIONARY;
2910 break;
2911 case Ctrl_R:
2912 /* Simply allow ^R to happen without affecting ^X mode */
2913 break;
2914 case Ctrl_T:
2915 ctrl_x_mode = CTRL_X_THESAURUS;
2916 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002917#ifdef FEAT_COMPL_FUNC
2918 case Ctrl_U:
2919 ctrl_x_mode = CTRL_X_FUNCTION;
2920 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002921 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002922 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002923 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002924#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002925 case 's':
2926 case Ctrl_S:
2927 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002928#ifdef FEAT_SYN_HL
2929 spell_back_to_badword();
2930#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 case Ctrl_RSB:
2933 ctrl_x_mode = CTRL_X_TAGS;
2934 break;
2935#ifdef FEAT_FIND_ID
2936 case Ctrl_I:
2937 case K_S_TAB:
2938 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2939 break;
2940 case Ctrl_D:
2941 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2942 break;
2943#endif
2944 case Ctrl_V:
2945 case Ctrl_Q:
2946 ctrl_x_mode = CTRL_X_CMDLINE;
2947 break;
2948 case Ctrl_P:
2949 case Ctrl_N:
2950 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
2951 * just started ^X mode, or there were enough ^X's to cancel
2952 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2953 * do normal expansion when interrupting a different mode (say
2954 * ^X^F^X^P or ^P^X^X^P, see below)
2955 * nothing changes if interrupting mode 0, (eg, the flag
2956 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002957 if (!(compl_cont_status & CONT_INTRPT))
2958 compl_cont_status |= CONT_LOCAL;
2959 else if (compl_cont_mode != 0)
2960 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 /* FALLTHROUGH */
2962 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002963 /* If we have typed at least 2 ^X's... for modes != 0, we set
2964 * compl_cont_status = 0 (eg, as if we had just started ^X
2965 * mode).
2966 * For mode 0, we set "compl_cont_mode" to an impossible
2967 * value, in both cases ^X^X can be used to restart the same
2968 * mode (avoiding ADDING mode).
2969 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2970 * 'complete' and local ^P expansions respectively.
2971 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2972 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 if (c == Ctrl_X)
2974 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002975 if (compl_cont_mode != 0)
2976 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002978 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 }
2980 ctrl_x_mode = 0;
2981 edit_submode = NULL;
2982 showmode();
2983 break;
2984 }
2985 }
2986 else if (ctrl_x_mode != 0)
2987 {
2988 /* We're already in CTRL-X mode, do we stay in it? */
2989 if (!vim_is_ctrl_x_key(c))
2990 {
2991 if (ctrl_x_mode == CTRL_X_SCROLL)
2992 ctrl_x_mode = 0;
2993 else
2994 ctrl_x_mode = CTRL_X_FINISHED;
2995 edit_submode = NULL;
2996 }
2997 showmode();
2998 }
2999
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003000 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 {
3002 /* Show error message from attempted keyword completion (probably
3003 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003004 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003006 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3007 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 || ctrl_x_mode == CTRL_X_FINISHED)
3009 {
3010 /* Get here when we have finished typing a sequence of ^N and
3011 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003012 * memory that was used, and make sure we can redo the insert. */
3013 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003015 char_u *p;
3016
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 /*
3018 * If any of the original typed text has been changed,
3019 * eg when ignorecase is set, we must add back-spaces to
3020 * the redo buffer. We add as few as necessary to delete
3021 * just the part of the original text that has changed.
3022 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003023 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003024 p = compl_orig_text;
3025 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003027 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 ++ptr;
3029 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003030 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00003032 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 }
3034
3035#ifdef FEAT_CINDENT
3036 want_cindent = (can_cindent && cindent_on());
3037#endif
3038 /*
3039 * When completing whole lines: fix indent for 'cindent'.
3040 * Otherwise, break line if it's too long.
3041 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003042 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 {
3044#ifdef FEAT_CINDENT
3045 /* re-indent the current line */
3046 if (want_cindent)
3047 {
3048 do_c_expr_indent();
3049 want_cindent = FALSE; /* don't do it again */
3050 }
3051#endif
3052 }
3053 else
3054 {
3055 /* put the cursor on the last char, for 'tw' formatting */
3056 curwin->w_cursor.col--;
3057 if (stop_arrow() == OK)
3058 insertchar(NUL, 0, -1);
3059 curwin->w_cursor.col++;
3060 }
3061
3062 auto_format(FALSE, TRUE);
3063
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003064 /* if the popup menu is displayed hitting Enter means accepting
3065 * the selection without inserting anything. */
3066 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
3067 retval = TRUE;
3068
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003070 compl_started = FALSE;
3071 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 msg_clr_cmdline(); /* necessary for "noshowmode" */
3073 ctrl_x_mode = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003074 if (save_sm >= 0)
3075 p_sm = save_sm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 if (edit_submode != NULL)
3077 {
3078 edit_submode = NULL;
3079 showmode();
3080 }
3081
3082#ifdef FEAT_CINDENT
3083 /*
3084 * Indent now if a key was typed that is in 'cinkeys'.
3085 */
3086 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3087 do_c_expr_indent();
3088#endif
3089 }
3090 }
3091
3092 /* reset continue_* if we left expansion-mode, if we stay they'll be
3093 * (re)set properly in ins_complete() */
3094 if (!vim_is_ctrl_x_key(c))
3095 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003096 compl_cont_status = 0;
3097 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003099
3100 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101}
3102
3103/*
3104 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3105 * (depending on flag) starting from buf and looking for a non-scanned
3106 * buffer (other than curbuf). curbuf is special, if it is called with
3107 * buf=curbuf then it has to be the first call for a given flag/expansion.
3108 *
3109 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3110 */
3111 static buf_T *
3112ins_compl_next_buf(buf, flag)
3113 buf_T *buf;
3114 int flag;
3115{
3116#ifdef FEAT_WINDOWS
3117 static win_T *wp;
3118#endif
3119
3120 if (flag == 'w') /* just windows */
3121 {
3122#ifdef FEAT_WINDOWS
3123 if (buf == curbuf) /* first call for this flag/expansion */
3124 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003125 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 && wp->w_buffer->b_scanned)
3127 ;
3128 buf = wp->w_buffer;
3129#else
3130 buf = curbuf;
3131#endif
3132 }
3133 else
3134 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3135 * (unlisted buffers)
3136 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003137 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 && ((flag == 'U'
3139 ? buf->b_p_bl
3140 : (!buf->b_p_bl
3141 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003142 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 ;
3144 return buf;
3145}
3146
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003147#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003148static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003149
3150/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003151 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003152 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003153 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003154 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003155 static void
3156expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003157 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003158 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003159{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003160 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003161 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003162 listitem_T *li;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003163 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003164 char_u *funcname;
3165 pos_T pos;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003166 int dir = compl_direction;
3167 char_u *x;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003168
Bram Moolenaare344bea2005-09-01 20:46:49 +00003169 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3170 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003171 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003172
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003173 /* Call 'completefunc' to obtain the list of matches. */
3174 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003175 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003176
Bram Moolenaare344bea2005-09-01 20:46:49 +00003177 pos = curwin->w_cursor;
3178 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3179 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003180 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003181 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003182
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003183 /* Go through the List with matches and add each of them. */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003184 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003185 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003186 if (li->li_tv.v_type == VAR_DICT && li->li_tv.vval.v_dict != NULL)
3187 {
3188 p = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"word", FALSE);
3189 x = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"menu", FALSE);
3190 }
3191 else
3192 {
3193 p = get_tv_string_chk(&li->li_tv);
3194 x = NULL;
3195 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003196 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003197 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003198 if (ins_compl_add(p, -1, NULL, x, dir, 0) == OK)
3199 /* if dir was BACKWARD then honor it just once */
3200 dir = FORWARD;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003201 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003202 else if (did_emsg)
3203 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003204 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003205
3206 list_unref(matchlist);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003207}
3208#endif /* FEAT_COMPL_FUNC */
3209
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003210/*
3211 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003212 * The search starts at position "ini" in curbuf and in the direction
3213 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003214 * When "compl_started" is FALSE start at that position, otherwise continue
3215 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003216 * This may return before finding all the matches.
3217 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 */
3219 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003220ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
3223 static pos_T first_match_pos;
3224 static pos_T last_match_pos;
3225 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003226 static int found_all = FALSE; /* Found all matches of a
3227 certain type. */
3228 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
Bram Moolenaar572cb562005-08-05 21:35:02 +00003230 pos_T *pos;
3231 char_u **matches;
3232 int save_p_scs;
3233 int save_p_ws;
3234 int save_p_ic;
3235 int i;
3236 int num_matches;
3237 int len;
3238 int found_new_match;
3239 int type = ctrl_x_mode;
3240 char_u *ptr;
3241 char_u *dict = NULL;
3242 int dict_f = 0;
3243 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003245 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
3247 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3248 ins_buf->b_scanned = 0;
3249 found_all = FALSE;
3250 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003251 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003252 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 last_match_pos = first_match_pos = *ini;
3254 }
3255
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003256 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003257 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3259 for (;;)
3260 {
3261 found_new_match = FAIL;
3262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003263 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 * or if found_all says this entry is done. For ^X^L only use the
3265 * entries from 'complete' that look in loaded buffers. */
3266 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003267 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 {
3269 found_all = FALSE;
3270 while (*e_cpt == ',' || *e_cpt == ' ')
3271 e_cpt++;
3272 if (*e_cpt == '.' && !curbuf->b_scanned)
3273 {
3274 ins_buf = curbuf;
3275 first_match_pos = *ini;
3276 /* So that ^N can match word immediately after cursor */
3277 if (ctrl_x_mode == 0)
3278 dec(&first_match_pos);
3279 last_match_pos = first_match_pos;
3280 type = 0;
3281 }
3282 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3283 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3284 {
3285 /* Scan a buffer, but not the current one. */
3286 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3287 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003288 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 first_match_pos.col = last_match_pos.col = 0;
3290 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3291 last_match_pos.lnum = 0;
3292 type = 0;
3293 }
3294 else /* unloaded buffer, scan like dictionary */
3295 {
3296 found_all = TRUE;
3297 if (ins_buf->b_fname == NULL)
3298 continue;
3299 type = CTRL_X_DICTIONARY;
3300 dict = ins_buf->b_fname;
3301 dict_f = DICT_EXACT;
3302 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003303 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 ins_buf->b_fname == NULL
3305 ? buf_spname(ins_buf)
3306 : ins_buf->b_sfname == NULL
3307 ? (char *)ins_buf->b_fname
3308 : (char *)ins_buf->b_sfname);
3309 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3310 }
3311 else if (*e_cpt == NUL)
3312 break;
3313 else
3314 {
3315 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3316 type = -1;
3317 else if (*e_cpt == 'k' || *e_cpt == 's')
3318 {
3319 if (*e_cpt == 'k')
3320 type = CTRL_X_DICTIONARY;
3321 else
3322 type = CTRL_X_THESAURUS;
3323 if (*++e_cpt != ',' && *e_cpt != NUL)
3324 {
3325 dict = e_cpt;
3326 dict_f = DICT_FIRST;
3327 }
3328 }
3329#ifdef FEAT_FIND_ID
3330 else if (*e_cpt == 'i')
3331 type = CTRL_X_PATH_PATTERNS;
3332 else if (*e_cpt == 'd')
3333 type = CTRL_X_PATH_DEFINES;
3334#endif
3335 else if (*e_cpt == ']' || *e_cpt == 't')
3336 {
3337 type = CTRL_X_TAGS;
3338 sprintf((char*)IObuff, _("Scanning tags."));
3339 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3340 }
3341 else
3342 type = -1;
3343
3344 /* in any case e_cpt is advanced to the next entry */
3345 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3346
3347 found_all = TRUE;
3348 if (type == -1)
3349 continue;
3350 }
3351 }
3352
3353 switch (type)
3354 {
3355 case -1:
3356 break;
3357#ifdef FEAT_FIND_ID
3358 case CTRL_X_PATH_PATTERNS:
3359 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003360 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003361 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003363 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3365 (linenr_T)1, (linenr_T)MAXLNUM);
3366 break;
3367#endif
3368
3369 case CTRL_X_DICTIONARY:
3370 case CTRL_X_THESAURUS:
3371 ins_compl_dictionaries(
3372 dict ? dict
3373 : (type == CTRL_X_THESAURUS
3374 ? (*curbuf->b_p_tsr == NUL
3375 ? p_tsr
3376 : curbuf->b_p_tsr)
3377 : (*curbuf->b_p_dict == NUL
3378 ? p_dict
3379 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003380 compl_pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
3382 dict = NULL;
3383 break;
3384
3385 case CTRL_X_TAGS:
3386 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3387 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003388 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
3390 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003391 * of matches is found when compl_pattern is empty */
3392 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3394 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3395 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3396 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003397 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 }
3399 p_ic = save_p_ic;
3400 break;
3401
3402 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003403 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3405 {
3406
3407 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003408 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003409 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 }
3411 break;
3412
3413 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003414 if (expand_cmdline(&compl_xp, compl_pattern,
3415 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003417 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 break;
3419
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003420#ifdef FEAT_COMPL_FUNC
3421 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003422 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003423 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003424 break;
3425#endif
3426
Bram Moolenaar488c6512005-08-11 20:09:58 +00003427 case CTRL_X_SPELL:
3428#ifdef FEAT_SYN_HL
3429 num_matches = expand_spelling(first_match_pos.lnum,
3430 first_match_pos.col, compl_pattern, &matches);
3431 if (num_matches > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003432 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003433#endif
3434 break;
3435
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 default: /* normal ^P/^N and ^X^L */
3437 /*
3438 * If 'infercase' is set, don't use 'smartcase' here
3439 */
3440 save_p_scs = p_scs;
3441 if (ins_buf->b_p_inf)
3442 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003443
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 /* buffers other than curbuf are scanned from the beginning or the
3445 * end but never from the middle, thus setting nowrapscan in this
3446 * buffers is a good idea, on the other hand, we always set
3447 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3448 save_p_ws = p_ws;
3449 if (ins_buf != curbuf)
3450 p_ws = FALSE;
3451 else if (*e_cpt == '.')
3452 p_ws = TRUE;
3453 for (;;)
3454 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003455 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003457 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3458 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003460 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003462 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003464 found_new_match = searchit(NULL, ins_buf, pos,
3465 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003466 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 RE_LAST);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003468 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003470 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003471 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 first_match_pos = *pos;
3473 last_match_pos = *pos;
3474 }
3475 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003476 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 found_new_match = FAIL;
3478 if (found_new_match == FAIL)
3479 {
3480 if (ins_buf == curbuf)
3481 found_all = TRUE;
3482 break;
3483 }
3484
3485 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003486 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 && ini->lnum == pos->lnum
3488 && ini->col == pos->col)
3489 continue;
3490 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3491 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3492 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003493 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 {
3495 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3496 continue;
3497 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3498 if (!p_paste)
3499 ptr = skipwhite(ptr);
3500 }
3501 len = (int)STRLEN(ptr);
3502 }
3503 else
3504 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003505 char_u *tmp_ptr = ptr;
3506
3507 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003509 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 /* Skip if already inside a word. */
3511 if (vim_iswordp(tmp_ptr))
3512 continue;
3513 /* Find start of next word. */
3514 tmp_ptr = find_word_start(tmp_ptr);
3515 }
3516 /* Find end of this word. */
3517 tmp_ptr = find_word_end(tmp_ptr);
3518 len = (int)(tmp_ptr - ptr);
3519
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003520 if ((compl_cont_status & CONT_ADDING)
3521 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
3523 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3524 {
3525 /* Try next line, if any. the new word will be
3526 * "join" as if the normal command "J" was used.
3527 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003528 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 * works -- Acevedo */
3530 STRNCPY(IObuff, ptr, len);
3531 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3532 tmp_ptr = ptr = skipwhite(ptr);
3533 /* Find start of next word. */
3534 tmp_ptr = find_word_start(tmp_ptr);
3535 /* Find end of next word. */
3536 tmp_ptr = find_word_end(tmp_ptr);
3537 if (tmp_ptr > ptr)
3538 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003539 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003541 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 IObuff[len++] = ' ';
3543 /* IObuf =~ "\k.* ", thus len >= 2 */
3544 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003545 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 || (vim_strchr(p_cpo, CPO_JOINSP)
3547 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003548 && (IObuff[len - 2] == '?'
3549 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 IObuff[len++] = ' ';
3551 }
3552 /* copy as much as posible of the new word */
3553 if (tmp_ptr - ptr >= IOSIZE - len)
3554 tmp_ptr = ptr + IOSIZE - len - 1;
3555 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3556 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003557 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
3559 IObuff[len] = NUL;
3560 ptr = IObuff;
3561 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003562 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 continue;
3564 }
3565 }
3566 if (ins_compl_add_infercase(ptr, len,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003567 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003568 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
3570 found_new_match = OK;
3571 break;
3572 }
3573 }
3574 p_scs = save_p_scs;
3575 p_ws = save_p_ws;
3576 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003577
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003578 /* check if compl_curr_match has changed, (e.g. other type of
3579 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003580 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 found_new_match = OK;
3582
3583 /* break the loop for specialized modes (use 'complete' just for the
3584 * generic ctrl_x_mode == 0) or when we've found a new match */
3585 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003586 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003587 {
3588 if (got_int)
3589 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003590 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003591 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003592 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003593
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003594 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3595 || compl_interrupted)
3596 break;
3597 compl_started = TRUE;
3598 }
3599 else
3600 {
3601 /* Mark a buffer scanned when it has been scanned completely */
3602 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3603 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003605 compl_started = FALSE;
3606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003608 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
3610 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3611 && *e_cpt == NUL) /* Got to end of 'complete' */
3612 found_new_match = FAIL;
3613
3614 i = -1; /* total of matches, unknown */
3615 if (found_new_match == FAIL
3616 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3617 i = ins_compl_make_cyclic();
3618
3619 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003620 * just been made cyclic then we have to move compl_curr_match to the next
3621 * or previous entry (if any) -- Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003622 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003623 if (compl_curr_match == NULL)
3624 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 return i;
3626}
3627
3628/* Delete the old text being completed. */
3629 static void
3630ins_compl_delete()
3631{
3632 int i;
3633
3634 /*
3635 * In insert mode: Delete the typed part.
3636 * In replace mode: Put the old characters back, if any.
3637 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003638 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 backspace_until_column(i);
3640 changed_cline_bef_curs();
3641}
3642
3643/* Insert the new text being completed. */
3644 static void
3645ins_compl_insert()
3646{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003647 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003648 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649}
3650
3651/*
3652 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003653 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3654 * get more completions. If it is FALSE, then we just do nothing when there
3655 * are no more completions in a given direction. The latter case is used when
3656 * we are still in the middle of finding completions, to allow browsing
3657 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 * Return the total number of matches, or -1 if still unknown -- webb.
3659 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003660 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3661 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 *
3663 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003664 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3665 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 */
3667 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003668ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003670 int count; /* repeat completion this many times; should
3671 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003672 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
3674 int num_matches = -1;
3675 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003676 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003677 compl_T *found_compl = NULL;
3678 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003680 if (compl_leader != NULL
3681 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003683 /* Set "compl_shown_match" to the actually shown match, it may differ
3684 * when "compl_leader" is used to omit some of the matches. */
3685 while (STRNCMP(compl_shown_match->cp_str,
3686 compl_leader, STRLEN(compl_leader)) != 0
3687 && compl_shown_match->cp_next != NULL
3688 && compl_shown_match->cp_next != compl_first_match)
3689 compl_shown_match = compl_shown_match->cp_next;
3690 }
3691
3692 if (allow_get_expansion && insert_match
3693 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 /* Delete old text to be replaced */
3695 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003696
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003697 compl_pending = FALSE;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003698
3699 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3700 * around. */
3701 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003703 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003705 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003706 found_end = (compl_first_match != NULL
3707 && (compl_shown_match->cp_next == compl_first_match
3708 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003709 }
3710 else if (compl_shows_dir == BACKWARD
3711 && compl_shown_match->cp_prev != NULL)
3712 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003713 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003714 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003715 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003718 {
3719 compl_pending = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003720 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003721 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003722
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003723 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara6557602006-02-04 22:43:20 +00003724 if (compl_pending && compl_direction == compl_shows_dir)
3725 compl_shown_match = compl_curr_match;
3726 found_end = FALSE;
3727 }
3728 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
3729 && compl_leader != NULL
3730 && STRNCMP(compl_shown_match->cp_str,
3731 compl_leader, STRLEN(compl_leader)) != 0)
3732 ++todo;
3733 else
3734 /* Remember a matching item. */
3735 found_compl = compl_shown_match;
3736
3737 /* Stop at the end of the list when we found a usable match. */
3738 if (found_end)
3739 {
3740 if (found_compl != NULL)
3741 {
3742 compl_shown_match = found_compl;
3743 break;
3744 }
3745 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
3748
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003749 /* Insert the text of the new completion, or the compl_leader. */
3750 if (insert_match)
3751 {
3752 if (!compl_get_longest || compl_used_match)
3753 ins_compl_insert();
3754 else
3755 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3756 }
3757 else
3758 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759
3760 if (!allow_get_expansion)
3761 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003762 /* may undisplay the popup menu first */
3763 ins_compl_upd_pum();
3764
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003765 /* redraw to show the user what was inserted */
3766 update_screen(0);
3767
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003768 /* display the updated popup menu */
3769 ins_compl_show_pum();
3770
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 /* Delete old text to be replaced, since we're still searching and
3772 * don't want to match ourselves! */
3773 ins_compl_delete();
3774 }
3775
3776 /*
3777 * Show the file name for the match (if any)
3778 * Truncate the file name to avoid a wait for return.
3779 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003780 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 {
3782 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003783 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 if (i <= 0)
3785 i = 0;
3786 else
3787 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003788 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 msg(IObuff);
3790 redraw_cmdline = FALSE; /* don't overwrite! */
3791 }
3792
3793 return num_matches;
3794}
3795
3796/*
3797 * Call this while finding completions, to check whether the user has hit a key
3798 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003799 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003801 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 */
3803 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003804ins_compl_check_keys(frequency)
3805 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
3807 static int count = 0;
3808
3809 int c;
3810
3811 /* Don't check when reading keys from a script. That would break the test
3812 * scripts */
3813 if (using_script())
3814 return;
3815
3816 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003817 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 return;
3819 count = 0;
3820
3821 ++no_mapping;
3822 c = vpeekc_any();
3823 --no_mapping;
3824 if (c != NUL)
3825 {
3826 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3827 {
3828 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003829 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003830 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
3831 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
3833 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003834 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003836 if (compl_pending && !got_int)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003837 (void)ins_compl_next(FALSE, 1, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003838}
3839
3840/*
3841 * Decide the direction of Insert mode complete from the key typed.
3842 * Returns BACKWARD or FORWARD.
3843 */
3844 static int
3845ins_compl_key2dir(c)
3846 int c;
3847{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003848 if (c == Ctrl_P || c == Ctrl_L
3849 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
3850 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00003851 return BACKWARD;
3852 return FORWARD;
3853}
3854
3855/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003856 * Return TRUE for keys that are used for completion only when the popup menu
3857 * is visible.
3858 */
3859 static int
3860ins_compl_pum_key(c)
3861 int c;
3862{
3863 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003864 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
3865 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003866}
3867
3868/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00003869 * Decide the number of completions to move forward.
3870 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3871 */
3872 static int
3873ins_compl_key2count(c)
3874 int c;
3875{
3876 int h;
3877
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003878 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003879 {
3880 h = pum_get_height();
3881 if (h > 3)
3882 h -= 2; /* keep some context */
3883 return h;
3884 }
3885 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886}
3887
3888/*
3889 * Do Insert mode completion.
3890 * Called when character "c" was typed, which has a meaning for completion.
3891 * Returns OK if completion was done, FAIL if something failed (out of mem).
3892 */
3893 static int
3894ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003895 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003897 char_u *line;
3898 int startcol = 0; /* column where searched text starts */
3899 colnr_T curs_col; /* cursor column */
3900 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
Bram Moolenaare3226be2005-12-18 22:10:00 +00003902 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003903 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 {
3905 /* First time we hit ^N or ^P (in a row, I mean) */
3906
3907 /* Turn off 'sm' so we don't show matches with ^X^L */
3908 save_sm = p_sm;
3909 p_sm = FALSE;
3910
3911 did_ai = FALSE;
3912#ifdef FEAT_SMARTINDENT
3913 did_si = FALSE;
3914 can_si = FALSE;
3915 can_si_back = FALSE;
3916#endif
3917 if (stop_arrow() == FAIL)
3918 return FAIL;
3919
3920 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
3923 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003924 * "compl_startpos" to the cursor as a pattern to add a new word
3925 * instead of expand the one before the cursor, in word-wise if
3926 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 * is not in the same line as the cursor then fix it (the line has
3928 * been split because it was longer than 'tw'). if SOL is set then
3929 * skip the previous pattern, a word at the beginning of the line has
3930 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003931 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
3932 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 {
3934 /*
3935 * it is a continued search
3936 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003937 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3939 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3940 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003941 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003943 /* line (probably) wrapped, set compl_startpos to the
3944 * first non_blank in the line, if it is not a wordchar
3945 * include it to get a better pattern, but then we don't
3946 * want the "\\<" prefix, check it bellow */
3947 compl_col = (colnr_T)(skipwhite(line) - line);
3948 compl_startpos.col = compl_col;
3949 compl_startpos.lnum = curwin->w_cursor.lnum;
3950 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
3952 else
3953 {
3954 /* S_IPOS was set when we inserted a word that was at the
3955 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003956 * mode but first we need to redefine compl_startpos */
3957 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003959 compl_cont_status |= CONT_SOL;
3960 compl_startpos.col = (colnr_T)(skipwhite(
3961 line + compl_length
3962 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003964 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003966 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003967 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 * have enough space? just being paranoic */
3969#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003970 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003972 compl_cont_status &= ~CONT_SOL;
3973 compl_length = (IOSIZE - MIN_SPACE);
3974 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003976 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3977 if (compl_length < 1)
3978 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 }
3980 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003981 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003983 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 }
3985 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003986 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003988 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003990 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003992 compl_cont_status = 0;
3993 compl_cont_status |= CONT_N_ADDS;
3994 compl_startpos = curwin->w_cursor;
3995 startcol = (int)curs_col;
3996 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
3998
3999 /* Work out completion pattern and original text -- webb */
4000 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4001 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004002 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4004 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004005 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004007 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004009 compl_col += ++startcol;
4010 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 }
4012 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004013 compl_pattern = str_foldcase(line + compl_col,
4014 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004016 compl_pattern = vim_strnsave(line + compl_col,
4017 compl_length);
4018 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 return FAIL;
4020 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004021 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 {
4023 char_u *prefix = (char_u *)"\\<";
4024
4025 /* we need 3 extra chars, 1 for the NUL and
4026 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004027 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4028 compl_length) + 3);
4029 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004031 if (!vim_iswordp(line + compl_col)
4032 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 && (
4034#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004035 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004037 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038#endif
4039 )))
4040 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004041 STRCPY((char *)compl_pattern, prefix);
4042 (void)quote_meta(compl_pattern + STRLEN(prefix),
4043 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004045 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004047 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004049 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050#endif
4051 )
4052 {
4053 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004054 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4055 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004057 compl_col += curs_col;
4058 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060 else
4061 {
4062#ifdef FEAT_MBYTE
4063 /* Search the point of change class of multibyte character
4064 * or not a word single byte character backward. */
4065 if (has_mbyte)
4066 {
4067 int base_class;
4068 int head_off;
4069
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004070 startcol -= (*mb_head_off)(line, line + startcol);
4071 base_class = mb_get_class(line + startcol);
4072 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004074 head_off = (*mb_head_off)(line, line + startcol);
4075 if (base_class != mb_get_class(line + startcol
4076 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004078 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 }
4080 }
4081 else
4082#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004083 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004085 compl_col += ++startcol;
4086 compl_length = (int)curs_col - startcol;
4087 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 {
4089 /* Only match word with at least two chars -- webb
4090 * there's no need to call quote_meta,
4091 * alloc(7) is enough -- Acevedo
4092 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004093 compl_pattern = alloc(7);
4094 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004096 STRCPY((char *)compl_pattern, "\\<");
4097 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4098 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
4100 else
4101 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004102 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4103 compl_length) + 3);
4104 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004106 STRCPY((char *)compl_pattern, "\\<");
4107 (void)quote_meta(compl_pattern + 2, line + compl_col,
4108 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110 }
4111 }
4112 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4113 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004114 compl_col = skipwhite(line) - line;
4115 compl_length = (int)curs_col - (int)compl_col;
4116 if (compl_length < 0) /* cursor in indent: empty pattern */
4117 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004119 compl_pattern = str_foldcase(line + compl_col, compl_length,
4120 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004122 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4123 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 return FAIL;
4125 }
4126 else if (ctrl_x_mode == CTRL_X_FILES)
4127 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004128 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004130 compl_col += ++startcol;
4131 compl_length = (int)curs_col - startcol;
4132 compl_pattern = addstar(line + compl_col, compl_length,
4133 EXPAND_FILES);
4134 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 return FAIL;
4136 }
4137 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4138 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004139 compl_pattern = vim_strnsave(line, curs_col);
4140 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004142 set_cmd_context(&compl_xp, compl_pattern,
4143 (int)STRLEN(compl_pattern), curs_col);
4144 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4145 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004147 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4148 compl_col = startcol;
4149 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004151 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004152 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004153#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004154 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004155 * Call user defined function 'completefunc' with "a:findstart"
4156 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004157 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004158 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004159 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004160 char_u *funcname;
4161 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004162
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004163 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004164 * string */
4165 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4166 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4167 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004168 {
4169 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4170 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004171 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004172 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004173
4174 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004175 args[1] = NULL;
4176 pos = curwin->w_cursor;
4177 col = call_func_retnr(funcname, 2, args, FALSE);
4178 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004179
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004180 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004181 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004182 compl_col = col;
4183 if ((colnr_T)compl_col > curs_col)
4184 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004185
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004186 /* Setup variables for completion. Need to obtain "line" again,
4187 * it may have become invalid. */
4188 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004189 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004190 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4191 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004192#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004193 return FAIL;
4194 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004195 else if (ctrl_x_mode == CTRL_X_SPELL)
4196 {
4197#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004198 if (spell_bad_len > 0)
4199 compl_col = curs_col - spell_bad_len;
4200 else
4201 compl_col = spell_word_start(startcol);
4202 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004203 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004204 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004205 compl_length = (int)curs_col - compl_col;
4206 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4207 if (compl_pattern == NULL)
4208#endif
4209 return FAIL;
4210 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004211 else
4212 {
4213 EMSG2(_(e_intern2), "ins_complete()");
4214 return FAIL;
4215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004217 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 {
4219 edit_submode_pre = (char_u *)_(" Adding");
4220 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4221 {
4222 /* Insert a new line, keep indentation but ignore 'comments' */
4223#ifdef FEAT_COMMENTS
4224 char_u *old = curbuf->b_p_com;
4225
4226 curbuf->b_p_com = (char_u *)"";
4227#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004228 compl_startpos.lnum = curwin->w_cursor.lnum;
4229 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 ins_eol('\r');
4231#ifdef FEAT_COMMENTS
4232 curbuf->b_p_com = old;
4233#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004234 compl_length = 0;
4235 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 }
4237 }
4238 else
4239 {
4240 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004241 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004244 if (compl_cont_status & CONT_LOCAL)
4245 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 else
4247 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4248
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004250 * "compl_orig_text" itself (not a copy) is added, it will be freed
4251 * when the list of matches is freed. */
4252 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4253 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004254 -1, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004256 vim_free(compl_pattern);
4257 compl_pattern = NULL;
4258 vim_free(compl_orig_text);
4259 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 return FAIL;
4261 }
4262
4263 /* showmode might reset the internal line pointers, so it must
4264 * be called before line = ml_get(), or when this address is no
4265 * longer needed. -- Acevedo.
4266 */
4267 edit_submode_extra = (char_u *)_("-- Searching...");
4268 edit_submode_highl = HLF_COUNT;
4269 showmode();
4270 edit_submode_extra = NULL;
4271 out_flush();
4272 }
4273
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004274 compl_shown_match = compl_curr_match;
4275 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
4277 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004278 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004280 n = ins_compl_next(TRUE, ins_compl_key2count(c), c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004282 /* may undisplay the popup menu */
4283 ins_compl_upd_pum();
4284
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004285 if (n > 1) /* all matches have been found */
4286 compl_matches = n;
4287 compl_curr_match = compl_shown_match;
4288 compl_direction = compl_shows_dir;
4289 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290
4291 /* eat the ESC to avoid leaving insert mode */
4292 if (got_int && !global_busy)
4293 {
4294 (void)vgetc();
4295 got_int = FALSE;
4296 }
4297
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004298 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004299 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004301 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4302 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4304 edit_submode_highl = HLF_E;
4305 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4306 * because we couldn't expand anything at first place, but if we used
4307 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4308 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004309 if ( compl_length > 1
4310 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 || (ctrl_x_mode != 0
4312 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4313 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004314 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
4316
Bram Moolenaar572cb562005-08-05 21:35:02 +00004317 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004318 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004320 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
4322 if (edit_submode_extra == NULL)
4323 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004324 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 {
4326 edit_submode_extra = (char_u *)_("Back at original");
4327 edit_submode_highl = HLF_W;
4328 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004329 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 {
4331 edit_submode_extra = (char_u *)_("Word from other line");
4332 edit_submode_highl = HLF_COUNT;
4333 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004334 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 {
4336 edit_submode_extra = (char_u *)_("The only match");
4337 edit_submode_highl = HLF_COUNT;
4338 }
4339 else
4340 {
4341 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004342 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004344 int number = 0;
4345 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004347 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
4349 /* search backwards for the first valid (!= -1) number.
4350 * This should normally succeed already at the first loop
4351 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004352 for (match = compl_curr_match->cp_prev; match != NULL
4353 && match != compl_first_match;
4354 match = match->cp_prev)
4355 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004357 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 break;
4359 }
4360 if (match != NULL)
4361 /* go up and assign all numbers which are not assigned
4362 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004363 for (match = match->cp_next;
4364 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004365 match = match->cp_next)
4366 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
4368 else /* BACKWARD */
4369 {
4370 /* search forwards (upwards) for the first valid (!= -1)
4371 * number. This should normally succeed already at the
4372 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004373 for (match = compl_curr_match->cp_next; match != NULL
4374 && match != compl_first_match;
4375 match = match->cp_next)
4376 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004378 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 break;
4380 }
4381 if (match != NULL)
4382 /* go down and assign all numbers which are not
4383 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004384 for (match = match->cp_prev; match
4385 && match->cp_number == -1;
4386 match = match->cp_prev)
4387 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 }
4389 }
4390
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004391 /* The match should always have a sequence number now, this is
4392 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004393 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
4395 /* Space for 10 text chars. + 2x10-digit no.s */
4396 static char_u match_ref[31];
4397
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004398 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004400 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004402 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004403 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004404 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 edit_submode_extra = match_ref;
4406 edit_submode_highl = HLF_R;
4407 if (dollar_vcol)
4408 curs_columns(FALSE);
4409 }
4410 }
4411 }
4412
4413 /* Show a message about what (completion) mode we're in. */
4414 showmode();
4415 if (edit_submode_extra != NULL)
4416 {
4417 if (!p_smd)
4418 msg_attr(edit_submode_extra,
4419 edit_submode_highl < HLF_COUNT
4420 ? hl_attr(edit_submode_highl) : 0);
4421 }
4422 else
4423 msg_clr_cmdline(); /* necessary for "noshowmode" */
4424
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004425 ins_compl_show_pum();
4426
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 return OK;
4428}
4429
4430/*
4431 * Looks in the first "len" chars. of "src" for search-metachars.
4432 * If dest is not NULL the chars. are copied there quoting (with
4433 * a backslash) the metachars, and dest would be NUL terminated.
4434 * Returns the length (needed) of dest
4435 */
4436 static int
4437quote_meta(dest, src, len)
4438 char_u *dest;
4439 char_u *src;
4440 int len;
4441{
4442 int m;
4443
4444 for (m = len; --len >= 0; src++)
4445 {
4446 switch (*src)
4447 {
4448 case '.':
4449 case '*':
4450 case '[':
4451 if (ctrl_x_mode == CTRL_X_DICTIONARY
4452 || ctrl_x_mode == CTRL_X_THESAURUS)
4453 break;
4454 case '~':
4455 if (!p_magic) /* quote these only if magic is set */
4456 break;
4457 case '\\':
4458 if (ctrl_x_mode == CTRL_X_DICTIONARY
4459 || ctrl_x_mode == CTRL_X_THESAURUS)
4460 break;
4461 case '^': /* currently it's not needed. */
4462 case '$':
4463 m++;
4464 if (dest != NULL)
4465 *dest++ = '\\';
4466 break;
4467 }
4468 if (dest != NULL)
4469 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004470# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 /* Copy remaining bytes of a multibyte character. */
4472 if (has_mbyte)
4473 {
4474 int i, mb_len;
4475
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004476 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 if (mb_len > 0 && len >= mb_len)
4478 for (i = 0; i < mb_len; ++i)
4479 {
4480 --len;
4481 ++src;
4482 if (dest != NULL)
4483 *dest++ = *src;
4484 }
4485 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004486# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488 if (dest != NULL)
4489 *dest = NUL;
4490
4491 return m;
4492}
4493#endif /* FEAT_INS_EXPAND */
4494
4495/*
4496 * Next character is interpreted literally.
4497 * A one, two or three digit decimal number is interpreted as its byte value.
4498 * If one or two digits are entered, the next character is given to vungetc().
4499 * For Unicode a character > 255 may be returned.
4500 */
4501 int
4502get_literal()
4503{
4504 int cc;
4505 int nc;
4506 int i;
4507 int hex = FALSE;
4508 int octal = FALSE;
4509#ifdef FEAT_MBYTE
4510 int unicode = 0;
4511#endif
4512
4513 if (got_int)
4514 return Ctrl_C;
4515
4516#ifdef FEAT_GUI
4517 /*
4518 * In GUI there is no point inserting the internal code for a special key.
4519 * It is more useful to insert the string "<KEY>" instead. This would
4520 * probably be useful in a text window too, but it would not be
4521 * vi-compatible (maybe there should be an option for it?) -- webb
4522 */
4523 if (gui.in_use)
4524 ++allow_keys;
4525#endif
4526#ifdef USE_ON_FLY_SCROLL
4527 dont_scroll = TRUE; /* disallow scrolling here */
4528#endif
4529 ++no_mapping; /* don't map the next key hits */
4530 cc = 0;
4531 i = 0;
4532 for (;;)
4533 {
4534 do
4535 nc = safe_vgetc();
4536 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4537 || nc == K_HOR_SCROLLBAR);
4538#ifdef FEAT_CMDL_INFO
4539 if (!(State & CMDLINE)
4540# ifdef FEAT_MBYTE
4541 && MB_BYTE2LEN_CHECK(nc) == 1
4542# endif
4543 )
4544 add_to_showcmd(nc);
4545#endif
4546 if (nc == 'x' || nc == 'X')
4547 hex = TRUE;
4548 else if (nc == 'o' || nc == 'O')
4549 octal = TRUE;
4550#ifdef FEAT_MBYTE
4551 else if (nc == 'u' || nc == 'U')
4552 unicode = nc;
4553#endif
4554 else
4555 {
4556 if (hex
4557#ifdef FEAT_MBYTE
4558 || unicode != 0
4559#endif
4560 )
4561 {
4562 if (!vim_isxdigit(nc))
4563 break;
4564 cc = cc * 16 + hex2nr(nc);
4565 }
4566 else if (octal)
4567 {
4568 if (nc < '0' || nc > '7')
4569 break;
4570 cc = cc * 8 + nc - '0';
4571 }
4572 else
4573 {
4574 if (!VIM_ISDIGIT(nc))
4575 break;
4576 cc = cc * 10 + nc - '0';
4577 }
4578
4579 ++i;
4580 }
4581
4582 if (cc > 255
4583#ifdef FEAT_MBYTE
4584 && unicode == 0
4585#endif
4586 )
4587 cc = 255; /* limit range to 0-255 */
4588 nc = 0;
4589
4590 if (hex) /* hex: up to two chars */
4591 {
4592 if (i >= 2)
4593 break;
4594 }
4595#ifdef FEAT_MBYTE
4596 else if (unicode) /* Unicode: up to four or eight chars */
4597 {
4598 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4599 break;
4600 }
4601#endif
4602 else if (i >= 3) /* decimal or octal: up to three chars */
4603 break;
4604 }
4605 if (i == 0) /* no number entered */
4606 {
4607 if (nc == K_ZERO) /* NUL is stored as NL */
4608 {
4609 cc = '\n';
4610 nc = 0;
4611 }
4612 else
4613 {
4614 cc = nc;
4615 nc = 0;
4616 }
4617 }
4618
4619 if (cc == 0) /* NUL is stored as NL */
4620 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004621#ifdef FEAT_MBYTE
4622 if (enc_dbcs && (cc & 0xff) == 0)
4623 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4624 second byte will cause trouble! */
4625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
4627 --no_mapping;
4628#ifdef FEAT_GUI
4629 if (gui.in_use)
4630 --allow_keys;
4631#endif
4632 if (nc)
4633 vungetc(nc);
4634 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4635 return cc;
4636}
4637
4638/*
4639 * Insert character, taking care of special keys and mod_mask
4640 */
4641 static void
4642insert_special(c, allow_modmask, ctrlv)
4643 int c;
4644 int allow_modmask;
4645 int ctrlv; /* c was typed after CTRL-V */
4646{
4647 char_u *p;
4648 int len;
4649
4650 /*
4651 * Special function key, translate into "<Key>". Up to the last '>' is
4652 * inserted with ins_str(), so as not to replace characters in replace
4653 * mode.
4654 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4655 * unless 'allow_modmask' is TRUE.
4656 */
4657#ifdef MACOS
4658 /* Command-key never produces a normal key */
4659 if (mod_mask & MOD_MASK_CMD)
4660 allow_modmask = TRUE;
4661#endif
4662 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4663 {
4664 p = get_special_key_name(c, mod_mask);
4665 len = (int)STRLEN(p);
4666 c = p[len - 1];
4667 if (len > 2)
4668 {
4669 if (stop_arrow() == FAIL)
4670 return;
4671 p[len - 1] = NUL;
4672 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004673 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 ctrlv = FALSE;
4675 }
4676 }
4677 if (stop_arrow() == OK)
4678 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4679}
4680
4681/*
4682 * Special characters in this context are those that need processing other
4683 * than the simple insertion that can be performed here. This includes ESC
4684 * which terminates the insert, and CR/NL which need special processing to
4685 * open up a new line. This routine tries to optimize insertions performed by
4686 * the "redo", "undo" or "put" commands, so it needs to know when it should
4687 * stop and defer processing to the "normal" mechanism.
4688 * '0' and '^' are special, because they can be followed by CTRL-D.
4689 */
4690#ifdef EBCDIC
4691# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4692#else
4693# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4694#endif
4695
4696#ifdef FEAT_MBYTE
4697# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4698#else
4699# define WHITECHAR(cc) vim_iswhite(cc)
4700#endif
4701
4702 void
4703insertchar(c, flags, second_indent)
4704 int c; /* character to insert or NUL */
4705 int flags; /* INSCHAR_FORMAT, etc. */
4706 int second_indent; /* indent for second line if >= 0 */
4707{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 int textwidth;
4709#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713
4714 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4715 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716
4717 /*
4718 * Try to break the line in two or more pieces when:
4719 * - Always do this if we have been called to do formatting only.
4720 * - Always do this when 'formatoptions' has the 'a' flag and the line
4721 * ends in white space.
4722 * - Otherwise:
4723 * - Don't do this if inserting a blank
4724 * - Don't do this if an existing character is being replaced, unless
4725 * we're in VREPLACE mode.
4726 * - Do this if the cursor is not on the line where insert started
4727 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4728 * before the insert.
4729 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4730 * before 'textwidth'
4731 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004732 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 && ((flags & INSCHAR_FORMAT)
4734 || (!vim_iswhite(c)
4735 && !((State & REPLACE_FLAG)
4736#ifdef FEAT_VREPLACE
4737 && !(State & VREPLACE_FLAG)
4738#endif
4739 && *ml_get_cursor() != NUL)
4740 && (curwin->w_cursor.lnum != Insstart.lnum
4741 || ((!has_format_option(FO_INS_LONG)
4742 || Insstart_textlen <= (colnr_T)textwidth)
4743 && (!fo_ins_blank
4744 || Insstart_blank_vcol <= (colnr_T)textwidth
4745 ))))))
4746 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004747 /* Format with 'formatexpr' when it's set. Use internal formatting
4748 * when 'formatexpr' isn't set or it returns non-zero. */
4749#if defined(FEAT_EVAL)
4750 if (*curbuf->b_p_fex == NUL
4751 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004753 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004755
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 if (c == NUL) /* only formatting was wanted */
4757 return;
4758
4759#ifdef FEAT_COMMENTS
4760 /* Check whether this character should end a comment. */
4761 if (did_ai && (int)c == end_comment_pending)
4762 {
4763 char_u *line;
4764 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4765 int middle_len, end_len;
4766 int i;
4767
4768 /*
4769 * Need to remove existing (middle) comment leader and insert end
4770 * comment leader. First, check what comment leader we can find.
4771 */
4772 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4773 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4774 {
4775 /* Skip middle-comment string */
4776 while (*p && p[-1] != ':') /* find end of middle flags */
4777 ++p;
4778 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4779 /* Don't count trailing white space for middle_len */
4780 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4781 --middle_len;
4782
4783 /* Find the end-comment string */
4784 while (*p && p[-1] != ':') /* find end of end flags */
4785 ++p;
4786 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4787
4788 /* Skip white space before the cursor */
4789 i = curwin->w_cursor.col;
4790 while (--i >= 0 && vim_iswhite(line[i]))
4791 ;
4792 i++;
4793
4794 /* Skip to before the middle leader */
4795 i -= middle_len;
4796
4797 /* Check some expected things before we go on */
4798 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4799 {
4800 /* Backspace over all the stuff we want to replace */
4801 backspace_until_column(i);
4802
4803 /*
4804 * Insert the end-comment string, except for the last
4805 * character, which will get inserted as normal later.
4806 */
4807 ins_bytes_len(lead_end, end_len - 1);
4808 }
4809 }
4810 }
4811 end_comment_pending = NUL;
4812#endif
4813
4814 did_ai = FALSE;
4815#ifdef FEAT_SMARTINDENT
4816 did_si = FALSE;
4817 can_si = FALSE;
4818 can_si_back = FALSE;
4819#endif
4820
4821 /*
4822 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4823 * This speeds up normal text input considerably.
4824 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4825 * need to re-indent at a ':', or any other character (but not what
4826 * 'paste' is set)..
4827 */
4828#ifdef USE_ON_FLY_SCROLL
4829 dont_scroll = FALSE; /* allow scrolling here */
4830#endif
4831
4832 if ( !ISSPECIAL(c)
4833#ifdef FEAT_MBYTE
4834 && (!has_mbyte || (*mb_char2len)(c) == 1)
4835#endif
4836 && vpeekc() != NUL
4837 && !(State & REPLACE_FLAG)
4838#ifdef FEAT_CINDENT
4839 && !cindent_on()
4840#endif
4841#ifdef FEAT_RIGHTLEFT
4842 && !p_ri
4843#endif
4844 )
4845 {
4846#define INPUT_BUFLEN 100
4847 char_u buf[INPUT_BUFLEN + 1];
4848 int i;
4849 colnr_T virtcol = 0;
4850
4851 buf[0] = c;
4852 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004853 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 virtcol = get_nolist_virtcol();
4855 /*
4856 * Stop the string when:
4857 * - no more chars available
4858 * - finding a special character (command key)
4859 * - buffer is full
4860 * - running into the 'textwidth' boundary
4861 * - need to check for abbreviation: A non-word char after a word-char
4862 */
4863 while ( (c = vpeekc()) != NUL
4864 && !ISSPECIAL(c)
4865#ifdef FEAT_MBYTE
4866 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4867#endif
4868 && i < INPUT_BUFLEN
4869 && (textwidth == 0
4870 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4871 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4872 {
4873#ifdef FEAT_RIGHTLEFT
4874 c = vgetc();
4875 if (p_hkmap && KeyTyped)
4876 c = hkmap(c); /* Hebrew mode mapping */
4877# ifdef FEAT_FKMAP
4878 if (p_fkmap && KeyTyped)
4879 c = fkmap(c); /* Farsi mode mapping */
4880# endif
4881 buf[i++] = c;
4882#else
4883 buf[i++] = vgetc();
4884#endif
4885 }
4886
4887#ifdef FEAT_DIGRAPHS
4888 do_digraph(-1); /* clear digraphs */
4889 do_digraph(buf[i-1]); /* may be the start of a digraph */
4890#endif
4891 buf[i] = NUL;
4892 ins_str(buf);
4893 if (flags & INSCHAR_CTRLV)
4894 {
4895 redo_literal(*buf);
4896 i = 1;
4897 }
4898 else
4899 i = 0;
4900 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004901 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903 else
4904 {
4905#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004906 int cc;
4907
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
4909 {
4910 char_u buf[MB_MAXBYTES + 1];
4911
4912 (*mb_char2bytes)(c, buf);
4913 buf[cc] = NUL;
4914 ins_char_bytes(buf, cc);
4915 AppendCharToRedobuff(c);
4916 }
4917 else
4918#endif
4919 {
4920 ins_char(c);
4921 if (flags & INSCHAR_CTRLV)
4922 redo_literal(c);
4923 else
4924 AppendCharToRedobuff(c);
4925 }
4926 }
4927}
4928
4929/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004930 * Format text at the current insert position.
4931 */
4932 static void
4933internal_format(textwidth, second_indent, flags, format_only)
4934 int textwidth;
4935 int second_indent;
4936 int flags;
4937 int format_only;
4938{
4939 int cc;
4940 int save_char = NUL;
4941 int haveto_redraw = FALSE;
4942 int fo_ins_blank = has_format_option(FO_INS_BLANK);
4943#ifdef FEAT_MBYTE
4944 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
4945#endif
4946 int fo_white_par = has_format_option(FO_WHITE_PAR);
4947 int first_line = TRUE;
4948#ifdef FEAT_COMMENTS
4949 colnr_T leader_len;
4950 int no_leader = FALSE;
4951 int do_comments = (flags & INSCHAR_DO_COM);
4952#endif
4953
4954 /*
4955 * When 'ai' is off we don't want a space under the cursor to be
4956 * deleted. Replace it with an 'x' temporarily.
4957 */
4958 if (!curbuf->b_p_ai)
4959 {
4960 cc = gchar_cursor();
4961 if (vim_iswhite(cc))
4962 {
4963 save_char = cc;
4964 pchar_cursor('x');
4965 }
4966 }
4967
4968 /*
4969 * Repeat breaking lines, until the current line is not too long.
4970 */
4971 while (!got_int)
4972 {
4973 int startcol; /* Cursor column at entry */
4974 int wantcol; /* column at textwidth border */
4975 int foundcol; /* column for start of spaces */
4976 int end_foundcol = 0; /* column for start of word */
4977 colnr_T len;
4978 colnr_T virtcol;
4979#ifdef FEAT_VREPLACE
4980 int orig_col = 0;
4981 char_u *saved_text = NULL;
4982#endif
4983 colnr_T col;
4984
4985 virtcol = get_nolist_virtcol();
4986 if (virtcol < (colnr_T)textwidth)
4987 break;
4988
4989#ifdef FEAT_COMMENTS
4990 if (no_leader)
4991 do_comments = FALSE;
4992 else if (!(flags & INSCHAR_FORMAT)
4993 && has_format_option(FO_WRAP_COMS))
4994 do_comments = TRUE;
4995
4996 /* Don't break until after the comment leader */
4997 if (do_comments)
4998 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
4999 else
5000 leader_len = 0;
5001
5002 /* If the line doesn't start with a comment leader, then don't
5003 * start one in a following broken line. Avoids that a %word
5004 * moved to the start of the next line causes all following lines
5005 * to start with %. */
5006 if (leader_len == 0)
5007 no_leader = TRUE;
5008#endif
5009 if (!(flags & INSCHAR_FORMAT)
5010#ifdef FEAT_COMMENTS
5011 && leader_len == 0
5012#endif
5013 && !has_format_option(FO_WRAP))
5014
5015 {
5016 textwidth = 0;
5017 break;
5018 }
5019 if ((startcol = curwin->w_cursor.col) == 0)
5020 break;
5021
5022 /* find column of textwidth border */
5023 coladvance((colnr_T)textwidth);
5024 wantcol = curwin->w_cursor.col;
5025
5026 curwin->w_cursor.col = startcol - 1;
5027#ifdef FEAT_MBYTE
5028 /* Correct cursor for multi-byte character. */
5029 if (has_mbyte)
5030 mb_adjust_cursor();
5031#endif
5032 foundcol = 0;
5033
5034 /*
5035 * Find position to break at.
5036 * Stop at first entered white when 'formatoptions' has 'v'
5037 */
5038 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5039 || curwin->w_cursor.lnum != Insstart.lnum
5040 || curwin->w_cursor.col >= Insstart.col)
5041 {
5042 cc = gchar_cursor();
5043 if (WHITECHAR(cc))
5044 {
5045 /* remember position of blank just before text */
5046 end_foundcol = curwin->w_cursor.col;
5047
5048 /* find start of sequence of blanks */
5049 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5050 {
5051 dec_cursor();
5052 cc = gchar_cursor();
5053 }
5054 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5055 break; /* only spaces in front of text */
5056#ifdef FEAT_COMMENTS
5057 /* Don't break until after the comment leader */
5058 if (curwin->w_cursor.col < leader_len)
5059 break;
5060#endif
5061 if (has_format_option(FO_ONE_LETTER))
5062 {
5063 /* do not break after one-letter words */
5064 if (curwin->w_cursor.col == 0)
5065 break; /* one-letter word at begin */
5066
5067 col = curwin->w_cursor.col;
5068 dec_cursor();
5069 cc = gchar_cursor();
5070
5071 if (WHITECHAR(cc))
5072 continue; /* one-letter, continue */
5073 curwin->w_cursor.col = col;
5074 }
5075#ifdef FEAT_MBYTE
5076 if (has_mbyte)
5077 foundcol = curwin->w_cursor.col
5078 + (*mb_ptr2len)(ml_get_cursor());
5079 else
5080#endif
5081 foundcol = curwin->w_cursor.col + 1;
5082 if (curwin->w_cursor.col < (colnr_T)wantcol)
5083 break;
5084 }
5085#ifdef FEAT_MBYTE
5086 else if (cc >= 0x100 && fo_multibyte
5087 && curwin->w_cursor.col <= (colnr_T)wantcol)
5088 {
5089 /* Break after or before a multi-byte character. */
5090 foundcol = curwin->w_cursor.col;
5091 if (curwin->w_cursor.col < (colnr_T)wantcol)
5092 foundcol += (*mb_char2len)(cc);
5093 end_foundcol = foundcol;
5094 break;
5095 }
5096#endif
5097 if (curwin->w_cursor.col == 0)
5098 break;
5099 dec_cursor();
5100 }
5101
5102 if (foundcol == 0) /* no spaces, cannot break line */
5103 {
5104 curwin->w_cursor.col = startcol;
5105 break;
5106 }
5107
5108 /* Going to break the line, remove any "$" now. */
5109 undisplay_dollar();
5110
5111 /*
5112 * Offset between cursor position and line break is used by replace
5113 * stack functions. VREPLACE does not use this, and backspaces
5114 * over the text instead.
5115 */
5116#ifdef FEAT_VREPLACE
5117 if (State & VREPLACE_FLAG)
5118 orig_col = startcol; /* Will start backspacing from here */
5119 else
5120#endif
5121 replace_offset = startcol - end_foundcol - 1;
5122
5123 /*
5124 * adjust startcol for spaces that will be deleted and
5125 * characters that will remain on top line
5126 */
5127 curwin->w_cursor.col = foundcol;
5128 while (cc = gchar_cursor(), WHITECHAR(cc))
5129 inc_cursor();
5130 startcol -= curwin->w_cursor.col;
5131 if (startcol < 0)
5132 startcol = 0;
5133
5134#ifdef FEAT_VREPLACE
5135 if (State & VREPLACE_FLAG)
5136 {
5137 /*
5138 * In VREPLACE mode, we will backspace over the text to be
5139 * wrapped, so save a copy now to put on the next line.
5140 */
5141 saved_text = vim_strsave(ml_get_cursor());
5142 curwin->w_cursor.col = orig_col;
5143 if (saved_text == NULL)
5144 break; /* Can't do it, out of memory */
5145 saved_text[startcol] = NUL;
5146
5147 /* Backspace over characters that will move to the next line */
5148 if (!fo_white_par)
5149 backspace_until_column(foundcol);
5150 }
5151 else
5152#endif
5153 {
5154 /* put cursor after pos. to break line */
5155 if (!fo_white_par)
5156 curwin->w_cursor.col = foundcol;
5157 }
5158
5159 /*
5160 * Split the line just before the margin.
5161 * Only insert/delete lines, but don't really redraw the window.
5162 */
5163 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5164 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5165#ifdef FEAT_COMMENTS
5166 + (do_comments ? OPENLINE_DO_COM : 0)
5167#endif
5168 , old_indent);
5169 old_indent = 0;
5170
5171 replace_offset = 0;
5172 if (first_line)
5173 {
5174 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5175 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5176 if (second_indent >= 0)
5177 {
5178#ifdef FEAT_VREPLACE
5179 if (State & VREPLACE_FLAG)
5180 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5181 else
5182#endif
5183 (void)set_indent(second_indent, SIN_CHANGED);
5184 }
5185 first_line = FALSE;
5186 }
5187
5188#ifdef FEAT_VREPLACE
5189 if (State & VREPLACE_FLAG)
5190 {
5191 /*
5192 * In VREPLACE mode we have backspaced over the text to be
5193 * moved, now we re-insert it into the new line.
5194 */
5195 ins_bytes(saved_text);
5196 vim_free(saved_text);
5197 }
5198 else
5199#endif
5200 {
5201 /*
5202 * Check if cursor is not past the NUL off the line, cindent
5203 * may have added or removed indent.
5204 */
5205 curwin->w_cursor.col += startcol;
5206 len = (colnr_T)STRLEN(ml_get_curline());
5207 if (curwin->w_cursor.col > len)
5208 curwin->w_cursor.col = len;
5209 }
5210
5211 haveto_redraw = TRUE;
5212#ifdef FEAT_CINDENT
5213 can_cindent = TRUE;
5214#endif
5215 /* moved the cursor, don't autoindent or cindent now */
5216 did_ai = FALSE;
5217#ifdef FEAT_SMARTINDENT
5218 did_si = FALSE;
5219 can_si = FALSE;
5220 can_si_back = FALSE;
5221#endif
5222 line_breakcheck();
5223 }
5224
5225 if (save_char != NUL) /* put back space after cursor */
5226 pchar_cursor(save_char);
5227
5228 if (!format_only && haveto_redraw)
5229 {
5230 update_topline();
5231 redraw_curbuf_later(VALID);
5232 }
5233}
5234
5235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 * Called after inserting or deleting text: When 'formatoptions' includes the
5237 * 'a' flag format from the current line until the end of the paragraph.
5238 * Keep the cursor at the same position relative to the text.
5239 * The caller must have saved the cursor line for undo, following ones will be
5240 * saved here.
5241 */
5242 void
5243auto_format(trailblank, prev_line)
5244 int trailblank; /* when TRUE also format with trailing blank */
5245 int prev_line; /* may start in previous line */
5246{
5247 pos_T pos;
5248 colnr_T len;
5249 char_u *old;
5250 char_u *new, *pnew;
5251 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005252 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253
5254 if (!has_format_option(FO_AUTO))
5255 return;
5256
5257 pos = curwin->w_cursor;
5258 old = ml_get_curline();
5259
5260 /* may remove added space */
5261 check_auto_format(FALSE);
5262
5263 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5264 * user might insert normal text next. Also skip formatting when "1" is
5265 * in 'formatoptions' and there is a single character before the cursor.
5266 * Otherwise the line would be broken and when typing another non-white
5267 * next they are not joined back together. */
5268 wasatend = (pos.col == STRLEN(old));
5269 if (*old != NUL && !trailblank && wasatend)
5270 {
5271 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005272 cc = gchar_cursor();
5273 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5274 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005276 cc = gchar_cursor();
5277 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 {
5279 curwin->w_cursor = pos;
5280 return;
5281 }
5282 curwin->w_cursor = pos;
5283 }
5284
5285#ifdef FEAT_COMMENTS
5286 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5287 * comments. */
5288 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5289 && get_leader_len(old, NULL, FALSE) == 0)
5290 return;
5291#endif
5292
5293 /*
5294 * May start formatting in a previous line, so that after "x" a word is
5295 * moved to the previous line if it fits there now. Only when this is not
5296 * the start of a paragraph.
5297 */
5298 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5299 {
5300 --curwin->w_cursor.lnum;
5301 if (u_save_cursor() == FAIL)
5302 return;
5303 }
5304
5305 /*
5306 * Do the formatting and restore the cursor position. "saved_cursor" will
5307 * be adjusted for the text formatting.
5308 */
5309 saved_cursor = pos;
5310 format_lines((linenr_T)-1);
5311 curwin->w_cursor = saved_cursor;
5312 saved_cursor.lnum = 0;
5313
5314 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5315 {
5316 /* "cannot happen" */
5317 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5318 coladvance((colnr_T)MAXCOL);
5319 }
5320 else
5321 check_cursor_col();
5322
5323 /* Insert mode: If the cursor is now after the end of the line while it
5324 * previously wasn't, the line was broken. Because of the rule above we
5325 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5326 * formatted. */
5327 if (!wasatend && has_format_option(FO_WHITE_PAR))
5328 {
5329 new = ml_get_curline();
5330 len = STRLEN(new);
5331 if (curwin->w_cursor.col == len)
5332 {
5333 pnew = vim_strnsave(new, len + 2);
5334 pnew[len] = ' ';
5335 pnew[len + 1] = NUL;
5336 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5337 /* remove the space later */
5338 did_add_space = TRUE;
5339 }
5340 else
5341 /* may remove added space */
5342 check_auto_format(FALSE);
5343 }
5344
5345 check_cursor();
5346}
5347
5348/*
5349 * When an extra space was added to continue a paragraph for auto-formatting,
5350 * delete it now. The space must be under the cursor, just after the insert
5351 * position.
5352 */
5353 static void
5354check_auto_format(end_insert)
5355 int end_insert; /* TRUE when ending Insert mode */
5356{
5357 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005358 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359
5360 if (did_add_space)
5361 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005362 cc = gchar_cursor();
5363 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 /* Somehow the space was removed already. */
5365 did_add_space = FALSE;
5366 else
5367 {
5368 if (!end_insert)
5369 {
5370 inc_cursor();
5371 c = gchar_cursor();
5372 dec_cursor();
5373 }
5374 if (c != NUL)
5375 {
5376 /* The space is no longer at the end of the line, delete it. */
5377 del_char(FALSE);
5378 did_add_space = FALSE;
5379 }
5380 }
5381 }
5382}
5383
5384/*
5385 * Find out textwidth to be used for formatting:
5386 * if 'textwidth' option is set, use it
5387 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5388 * if invalid value, use 0.
5389 * Set default to window width (maximum 79) for "gq" operator.
5390 */
5391 int
5392comp_textwidth(ff)
5393 int ff; /* force formatting (for "Q" command) */
5394{
5395 int textwidth;
5396
5397 textwidth = curbuf->b_p_tw;
5398 if (textwidth == 0 && curbuf->b_p_wm)
5399 {
5400 /* The width is the window width minus 'wrapmargin' minus all the
5401 * things that add to the margin. */
5402 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5403#ifdef FEAT_CMDWIN
5404 if (cmdwin_type != 0)
5405 textwidth -= 1;
5406#endif
5407#ifdef FEAT_FOLDING
5408 textwidth -= curwin->w_p_fdc;
5409#endif
5410#ifdef FEAT_SIGNS
5411 if (curwin->w_buffer->b_signlist != NULL
5412# ifdef FEAT_NETBEANS_INTG
5413 || usingNetbeans
5414# endif
5415 )
5416 textwidth -= 1;
5417#endif
5418 if (curwin->w_p_nu)
5419 textwidth -= 8;
5420 }
5421 if (textwidth < 0)
5422 textwidth = 0;
5423 if (ff && textwidth == 0)
5424 {
5425 textwidth = W_WIDTH(curwin) - 1;
5426 if (textwidth > 79)
5427 textwidth = 79;
5428 }
5429 return textwidth;
5430}
5431
5432/*
5433 * Put a character in the redo buffer, for when just after a CTRL-V.
5434 */
5435 static void
5436redo_literal(c)
5437 int c;
5438{
5439 char_u buf[10];
5440
5441 /* Only digits need special treatment. Translate them into a string of
5442 * three digits. */
5443 if (VIM_ISDIGIT(c))
5444 {
5445 sprintf((char *)buf, "%03d", c);
5446 AppendToRedobuff(buf);
5447 }
5448 else
5449 AppendCharToRedobuff(c);
5450}
5451
5452/*
5453 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005454 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 */
5456 static void
5457start_arrow(end_insert_pos)
5458 pos_T *end_insert_pos;
5459{
5460 if (!arrow_used) /* something has been inserted */
5461 {
5462 AppendToRedobuff(ESC_STR);
5463 stop_insert(end_insert_pos, FALSE);
5464 arrow_used = TRUE; /* this means we stopped the current insert */
5465 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005466#ifdef FEAT_SYN_HL
5467 check_spell_redraw();
5468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469}
5470
Bram Moolenaar217ad922005-03-20 22:37:15 +00005471#ifdef FEAT_SYN_HL
5472/*
5473 * If we skipped highlighting word at cursor, do it now.
5474 * It may be skipped again, thus reset spell_redraw_lnum first.
5475 */
5476 static void
5477check_spell_redraw()
5478{
5479 if (spell_redraw_lnum != 0)
5480 {
5481 linenr_T lnum = spell_redraw_lnum;
5482
5483 spell_redraw_lnum = 0;
5484 redrawWinline(lnum, FALSE);
5485 }
5486}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005487
5488/*
5489 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5490 * spelled word, if there is one.
5491 */
5492 static void
5493spell_back_to_badword()
5494{
5495 pos_T tpos = curwin->w_cursor;
5496
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005497 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005498 if (curwin->w_cursor.col != tpos.col)
5499 start_arrow(&tpos);
5500}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005501#endif
5502
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503/*
5504 * stop_arrow() is called before a change is made in insert mode.
5505 * If an arrow key has been used, start a new insertion.
5506 * Returns FAIL if undo is impossible, shouldn't insert then.
5507 */
5508 int
5509stop_arrow()
5510{
5511 if (arrow_used)
5512 {
5513 if (u_save_cursor() == OK)
5514 {
5515 arrow_used = FALSE;
5516 ins_need_undo = FALSE;
5517 }
5518 Insstart = curwin->w_cursor; /* new insertion starts here */
5519 Insstart_textlen = linetabsize(ml_get_curline());
5520 ai_col = 0;
5521#ifdef FEAT_VREPLACE
5522 if (State & VREPLACE_FLAG)
5523 {
5524 orig_line_count = curbuf->b_ml.ml_line_count;
5525 vr_lines_changed = 1;
5526 }
5527#endif
5528 ResetRedobuff();
5529 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005530 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 }
5532 else if (ins_need_undo)
5533 {
5534 if (u_save_cursor() == OK)
5535 ins_need_undo = FALSE;
5536 }
5537
5538#ifdef FEAT_FOLDING
5539 /* Always open fold at the cursor line when inserting something. */
5540 foldOpenCursor();
5541#endif
5542
5543 return (arrow_used || ins_need_undo ? FAIL : OK);
5544}
5545
5546/*
5547 * do a few things to stop inserting
5548 */
5549 static void
5550stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005551 pos_T *end_insert_pos; /* where insert ended */
5552 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005554 int cc;
5555 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
5557 stop_redo_ins();
5558 replace_flush(); /* abandon replace stack */
5559
5560 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005561 * Save the inserted text for later redo with ^@ and CTRL-A.
5562 * Don't do it when "restart_edit" was set and nothing was inserted,
5563 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005565 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005566 if (did_restart_edit == 0 || (ptr != NULL
5567 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005568 {
5569 vim_free(last_insert);
5570 last_insert = ptr;
5571 last_insert_skip = new_insert_skip;
5572 }
5573 else
5574 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575
5576 if (!arrow_used)
5577 {
5578 /* Auto-format now. It may seem strange to do this when stopping an
5579 * insertion (or moving the cursor), but it's required when appending
5580 * a line and having it end in a space. But only do it when something
5581 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005582 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005584 pos_T tpos = curwin->w_cursor;
5585
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 /* When the cursor is at the end of the line after a space the
5587 * formatting will move it to the following word. Avoid that by
5588 * moving the cursor onto the space. */
5589 cc = 'x';
5590 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5591 {
5592 dec_cursor();
5593 cc = gchar_cursor();
5594 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005595 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597
5598 auto_format(TRUE, FALSE);
5599
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005600 if (vim_iswhite(cc))
5601 {
5602 if (gchar_cursor() != NUL)
5603 inc_cursor();
5604#ifdef FEAT_VIRTUALEDIT
5605 /* If the cursor is still at the same character, also keep
5606 * the "coladd". */
5607 if (gchar_cursor() == NUL
5608 && curwin->w_cursor.lnum == tpos.lnum
5609 && curwin->w_cursor.col == tpos.col)
5610 curwin->w_cursor.coladd = tpos.coladd;
5611#endif
5612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614
5615 /* If a space was inserted for auto-formatting, remove it now. */
5616 check_auto_format(TRUE);
5617
5618 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005619 * of the line, and put the cursor back.
5620 * Do this when ESC was used or moving the cursor up/down. */
5621 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5622 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005624 pos_T tpos = curwin->w_cursor;
5625
5626 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5628 --curwin->w_cursor.col;
5629 while (cc = gchar_cursor(), vim_iswhite(cc))
5630 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005631 if (curwin->w_cursor.lnum != tpos.lnum)
5632 curwin->w_cursor = tpos;
5633 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5635
5636#ifdef FEAT_VISUAL
5637 /* <C-S-Right> may have started Visual mode, adjust the position for
5638 * deleted characters. */
5639 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5640 {
5641 cc = STRLEN(ml_get_curline());
5642 if (VIsual.col > (colnr_T)cc)
5643 {
5644 VIsual.col = cc;
5645# ifdef FEAT_VIRTUALEDIT
5646 VIsual.coladd = 0;
5647# endif
5648 }
5649 }
5650#endif
5651 }
5652 }
5653 did_ai = FALSE;
5654#ifdef FEAT_SMARTINDENT
5655 did_si = FALSE;
5656 can_si = FALSE;
5657 can_si_back = FALSE;
5658#endif
5659
5660 /* set '[ and '] to the inserted text */
5661 curbuf->b_op_start = Insstart;
5662 curbuf->b_op_end = *end_insert_pos;
5663}
5664
5665/*
5666 * Set the last inserted text to a single character.
5667 * Used for the replace command.
5668 */
5669 void
5670set_last_insert(c)
5671 int c;
5672{
5673 char_u *s;
5674
5675 vim_free(last_insert);
5676#ifdef FEAT_MBYTE
5677 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5678#else
5679 last_insert = alloc(6);
5680#endif
5681 if (last_insert != NULL)
5682 {
5683 s = last_insert;
5684 /* Use the CTRL-V only when entering a special char */
5685 if (c < ' ' || c == DEL)
5686 *s++ = Ctrl_V;
5687 s = add_char2buf(c, s);
5688 *s++ = ESC;
5689 *s++ = NUL;
5690 last_insert_skip = 0;
5691 }
5692}
5693
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005694#if defined(EXITFREE) || defined(PROTO)
5695 void
5696free_last_insert()
5697{
5698 vim_free(last_insert);
5699 last_insert = NULL;
5700}
5701#endif
5702
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703/*
5704 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5705 * and CSI. Handle multi-byte characters.
5706 * Returns a pointer to after the added bytes.
5707 */
5708 char_u *
5709add_char2buf(c, s)
5710 int c;
5711 char_u *s;
5712{
5713#ifdef FEAT_MBYTE
5714 char_u temp[MB_MAXBYTES];
5715 int i;
5716 int len;
5717
5718 len = (*mb_char2bytes)(c, temp);
5719 for (i = 0; i < len; ++i)
5720 {
5721 c = temp[i];
5722#endif
5723 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5724 if (c == K_SPECIAL)
5725 {
5726 *s++ = K_SPECIAL;
5727 *s++ = KS_SPECIAL;
5728 *s++ = KE_FILLER;
5729 }
5730#ifdef FEAT_GUI
5731 else if (c == CSI)
5732 {
5733 *s++ = CSI;
5734 *s++ = KS_EXTRA;
5735 *s++ = (int)KE_CSI;
5736 }
5737#endif
5738 else
5739 *s++ = c;
5740#ifdef FEAT_MBYTE
5741 }
5742#endif
5743 return s;
5744}
5745
5746/*
5747 * move cursor to start of line
5748 * if flags & BL_WHITE move to first non-white
5749 * if flags & BL_SOL move to first non-white if startofline is set,
5750 * otherwise keep "curswant" column
5751 * if flags & BL_FIX don't leave the cursor on a NUL.
5752 */
5753 void
5754beginline(flags)
5755 int flags;
5756{
5757 if ((flags & BL_SOL) && !p_sol)
5758 coladvance(curwin->w_curswant);
5759 else
5760 {
5761 curwin->w_cursor.col = 0;
5762#ifdef FEAT_VIRTUALEDIT
5763 curwin->w_cursor.coladd = 0;
5764#endif
5765
5766 if (flags & (BL_WHITE | BL_SOL))
5767 {
5768 char_u *ptr;
5769
5770 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5771 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5772 ++curwin->w_cursor.col;
5773 }
5774 curwin->w_set_curswant = TRUE;
5775 }
5776}
5777
5778/*
5779 * oneright oneleft cursor_down cursor_up
5780 *
5781 * Move one char {right,left,down,up}.
5782 * Doesn't move onto the NUL past the end of the line.
5783 * Return OK when successful, FAIL when we hit a line of file boundary.
5784 */
5785
5786 int
5787oneright()
5788{
5789 char_u *ptr;
5790#ifdef FEAT_MBYTE
5791 int l;
5792#endif
5793
5794#ifdef FEAT_VIRTUALEDIT
5795 if (virtual_active())
5796 {
5797 pos_T prevpos = curwin->w_cursor;
5798
5799 /* Adjust for multi-wide char (excluding TAB) */
5800 ptr = ml_get_cursor();
5801 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5802#ifdef FEAT_MBYTE
5803 (*mb_ptr2char)(ptr)
5804#else
5805 *ptr
5806#endif
5807 ))
5808 ? ptr2cells(ptr) : 1));
5809 curwin->w_set_curswant = TRUE;
5810 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5811 return (prevpos.col != curwin->w_cursor.col
5812 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5813 }
5814#endif
5815
5816 ptr = ml_get_cursor();
5817#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005818 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 {
5820 /* The character under the cursor is a multi-byte character, move
5821 * several bytes right, but don't end up on the NUL. */
5822 if (ptr[l] == NUL)
5823 return FAIL;
5824 curwin->w_cursor.col += l;
5825 }
5826 else
5827#endif
5828 {
5829 if (*ptr++ == NUL || *ptr == NUL)
5830 return FAIL;
5831 ++curwin->w_cursor.col;
5832 }
5833
5834 curwin->w_set_curswant = TRUE;
5835 return OK;
5836}
5837
5838 int
5839oneleft()
5840{
5841#ifdef FEAT_VIRTUALEDIT
5842 if (virtual_active())
5843 {
5844 int width;
5845 int v = getviscol();
5846
5847 if (v == 0)
5848 return FAIL;
5849
5850# ifdef FEAT_LINEBREAK
5851 /* We might get stuck on 'showbreak', skip over it. */
5852 width = 1;
5853 for (;;)
5854 {
5855 coladvance(v - width);
5856 /* getviscol() is slow, skip it when 'showbreak' is empty and
5857 * there are no multi-byte characters */
5858 if ((*p_sbr == NUL
5859# ifdef FEAT_MBYTE
5860 && !has_mbyte
5861# endif
5862 ) || getviscol() < v)
5863 break;
5864 ++width;
5865 }
5866# else
5867 coladvance(v - 1);
5868# endif
5869
5870 if (curwin->w_cursor.coladd == 1)
5871 {
5872 char_u *ptr;
5873
5874 /* Adjust for multi-wide char (not a TAB) */
5875 ptr = ml_get_cursor();
5876 if (*ptr != TAB && vim_isprintc(
5877# ifdef FEAT_MBYTE
5878 (*mb_ptr2char)(ptr)
5879# else
5880 *ptr
5881# endif
5882 ) && ptr2cells(ptr) > 1)
5883 curwin->w_cursor.coladd = 0;
5884 }
5885
5886 curwin->w_set_curswant = TRUE;
5887 return OK;
5888 }
5889#endif
5890
5891 if (curwin->w_cursor.col == 0)
5892 return FAIL;
5893
5894 curwin->w_set_curswant = TRUE;
5895 --curwin->w_cursor.col;
5896
5897#ifdef FEAT_MBYTE
5898 /* if the character on the left of the current cursor is a multi-byte
5899 * character, move to its first byte */
5900 if (has_mbyte)
5901 mb_adjust_cursor();
5902#endif
5903 return OK;
5904}
5905
5906 int
5907cursor_up(n, upd_topline)
5908 long n;
5909 int upd_topline; /* When TRUE: update topline */
5910{
5911 linenr_T lnum;
5912
5913 if (n > 0)
5914 {
5915 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005916 /* This fails if the cursor is already in the first line or the count
5917 * is larger than the line number and '-' is in 'cpoptions' */
5918 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 return FAIL;
5920 if (n >= lnum)
5921 lnum = 1;
5922 else
5923#ifdef FEAT_FOLDING
5924 if (hasAnyFolding(curwin))
5925 {
5926 /*
5927 * Count each sequence of folded lines as one logical line.
5928 */
5929 /* go to the the start of the current fold */
5930 (void)hasFolding(lnum, &lnum, NULL);
5931
5932 while (n--)
5933 {
5934 /* move up one line */
5935 --lnum;
5936 if (lnum <= 1)
5937 break;
5938 /* If we entered a fold, move to the beginning, unless in
5939 * Insert mode or when 'foldopen' contains "all": it will open
5940 * in a moment. */
5941 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
5942 (void)hasFolding(lnum, &lnum, NULL);
5943 }
5944 if (lnum < 1)
5945 lnum = 1;
5946 }
5947 else
5948#endif
5949 lnum -= n;
5950 curwin->w_cursor.lnum = lnum;
5951 }
5952
5953 /* try to advance to the column we want to be at */
5954 coladvance(curwin->w_curswant);
5955
5956 if (upd_topline)
5957 update_topline(); /* make sure curwin->w_topline is valid */
5958
5959 return OK;
5960}
5961
5962/*
5963 * Cursor down a number of logical lines.
5964 */
5965 int
5966cursor_down(n, upd_topline)
5967 long n;
5968 int upd_topline; /* When TRUE: update topline */
5969{
5970 linenr_T lnum;
5971
5972 if (n > 0)
5973 {
5974 lnum = curwin->w_cursor.lnum;
5975#ifdef FEAT_FOLDING
5976 /* Move to last line of fold, will fail if it's the end-of-file. */
5977 (void)hasFolding(lnum, NULL, &lnum);
5978#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00005979 /* This fails if the cursor is already in the last line or would move
5980 * beyound the last line and '-' is in 'cpoptions' */
5981 if (lnum >= curbuf->b_ml.ml_line_count
5982 || (lnum + n > curbuf->b_ml.ml_line_count
5983 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 return FAIL;
5985 if (lnum + n >= curbuf->b_ml.ml_line_count)
5986 lnum = curbuf->b_ml.ml_line_count;
5987 else
5988#ifdef FEAT_FOLDING
5989 if (hasAnyFolding(curwin))
5990 {
5991 linenr_T last;
5992
5993 /* count each sequence of folded lines as one logical line */
5994 while (n--)
5995 {
5996 if (hasFolding(lnum, NULL, &last))
5997 lnum = last + 1;
5998 else
5999 ++lnum;
6000 if (lnum >= curbuf->b_ml.ml_line_count)
6001 break;
6002 }
6003 if (lnum > curbuf->b_ml.ml_line_count)
6004 lnum = curbuf->b_ml.ml_line_count;
6005 }
6006 else
6007#endif
6008 lnum += n;
6009 curwin->w_cursor.lnum = lnum;
6010 }
6011
6012 /* try to advance to the column we want to be at */
6013 coladvance(curwin->w_curswant);
6014
6015 if (upd_topline)
6016 update_topline(); /* make sure curwin->w_topline is valid */
6017
6018 return OK;
6019}
6020
6021/*
6022 * Stuff the last inserted text in the read buffer.
6023 * Last_insert actually is a copy of the redo buffer, so we
6024 * first have to remove the command.
6025 */
6026 int
6027stuff_inserted(c, count, no_esc)
6028 int c; /* Command character to be inserted */
6029 long count; /* Repeat this many times */
6030 int no_esc; /* Don't add an ESC at the end */
6031{
6032 char_u *esc_ptr;
6033 char_u *ptr;
6034 char_u *last_ptr;
6035 char_u last = NUL;
6036
6037 ptr = get_last_insert();
6038 if (ptr == NULL)
6039 {
6040 EMSG(_(e_noinstext));
6041 return FAIL;
6042 }
6043
6044 /* may want to stuff the command character, to start Insert mode */
6045 if (c != NUL)
6046 stuffcharReadbuff(c);
6047 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6048 *esc_ptr = NUL; /* remove the ESC */
6049
6050 /* when the last char is either "0" or "^" it will be quoted if no ESC
6051 * comes after it OR if it will inserted more than once and "ptr"
6052 * starts with ^D. -- Acevedo
6053 */
6054 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6055 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6056 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6057 {
6058 last = *last_ptr;
6059 *last_ptr = NUL;
6060 }
6061
6062 do
6063 {
6064 stuffReadbuff(ptr);
6065 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6066 if (last)
6067 stuffReadbuff((char_u *)(last == '0'
6068 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6069 : IF_EB("\026^", CTRL_V_STR "^")));
6070 }
6071 while (--count > 0);
6072
6073 if (last)
6074 *last_ptr = last;
6075
6076 if (esc_ptr != NULL)
6077 *esc_ptr = ESC; /* put the ESC back */
6078
6079 /* may want to stuff a trailing ESC, to get out of Insert mode */
6080 if (!no_esc)
6081 stuffcharReadbuff(ESC);
6082
6083 return OK;
6084}
6085
6086 char_u *
6087get_last_insert()
6088{
6089 if (last_insert == NULL)
6090 return NULL;
6091 return last_insert + last_insert_skip;
6092}
6093
6094/*
6095 * Get last inserted string, and remove trailing <Esc>.
6096 * Returns pointer to allocated memory (must be freed) or NULL.
6097 */
6098 char_u *
6099get_last_insert_save()
6100{
6101 char_u *s;
6102 int len;
6103
6104 if (last_insert == NULL)
6105 return NULL;
6106 s = vim_strsave(last_insert + last_insert_skip);
6107 if (s != NULL)
6108 {
6109 len = (int)STRLEN(s);
6110 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6111 s[len - 1] = NUL;
6112 }
6113 return s;
6114}
6115
6116/*
6117 * Check the word in front of the cursor for an abbreviation.
6118 * Called when the non-id character "c" has been entered.
6119 * When an abbreviation is recognized it is removed from the text and
6120 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6121 */
6122 static int
6123echeck_abbr(c)
6124 int c;
6125{
6126 /* Don't check for abbreviation in paste mode, when disabled and just
6127 * after moving around with cursor keys. */
6128 if (p_paste || no_abbr || arrow_used)
6129 return FALSE;
6130
6131 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6132 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6133}
6134
6135/*
6136 * replace-stack functions
6137 *
6138 * When replacing characters, the replaced characters are remembered for each
6139 * new character. This is used to re-insert the old text when backspacing.
6140 *
6141 * There is a NUL headed list of characters for each character that is
6142 * currently in the file after the insertion point. When BS is used, one NUL
6143 * headed list is put back for the deleted character.
6144 *
6145 * For a newline, there are two NUL headed lists. One contains the characters
6146 * that the NL replaced. The extra one stores the characters after the cursor
6147 * that were deleted (always white space).
6148 *
6149 * Replace_offset is normally 0, in which case replace_push will add a new
6150 * character at the end of the stack. If replace_offset is not 0, that many
6151 * characters will be left on the stack above the newly inserted character.
6152 */
6153
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006154static char_u *replace_stack = NULL;
6155static long replace_stack_nr = 0; /* next entry in replace stack */
6156static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157
6158 void
6159replace_push(c)
6160 int c; /* character that is replaced (NUL is none) */
6161{
6162 char_u *p;
6163
6164 if (replace_stack_nr < replace_offset) /* nothing to do */
6165 return;
6166 if (replace_stack_len <= replace_stack_nr)
6167 {
6168 replace_stack_len += 50;
6169 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6170 if (p == NULL) /* out of memory */
6171 {
6172 replace_stack_len -= 50;
6173 return;
6174 }
6175 if (replace_stack != NULL)
6176 {
6177 mch_memmove(p, replace_stack,
6178 (size_t)(replace_stack_nr * sizeof(char_u)));
6179 vim_free(replace_stack);
6180 }
6181 replace_stack = p;
6182 }
6183 p = replace_stack + replace_stack_nr - replace_offset;
6184 if (replace_offset)
6185 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6186 *p = c;
6187 ++replace_stack_nr;
6188}
6189
6190/*
6191 * call replace_push(c) with replace_offset set to the first NUL.
6192 */
6193 static void
6194replace_push_off(c)
6195 int c;
6196{
6197 char_u *p;
6198
6199 p = replace_stack + replace_stack_nr;
6200 for (replace_offset = 1; replace_offset < replace_stack_nr;
6201 ++replace_offset)
6202 if (*--p == NUL)
6203 break;
6204 replace_push(c);
6205 replace_offset = 0;
6206}
6207
6208/*
6209 * Pop one item from the replace stack.
6210 * return -1 if stack empty
6211 * return replaced character or NUL otherwise
6212 */
6213 static int
6214replace_pop()
6215{
6216 if (replace_stack_nr == 0)
6217 return -1;
6218 return (int)replace_stack[--replace_stack_nr];
6219}
6220
6221/*
6222 * Join the top two items on the replace stack. This removes to "off"'th NUL
6223 * encountered.
6224 */
6225 static void
6226replace_join(off)
6227 int off; /* offset for which NUL to remove */
6228{
6229 int i;
6230
6231 for (i = replace_stack_nr; --i >= 0; )
6232 if (replace_stack[i] == NUL && off-- <= 0)
6233 {
6234 --replace_stack_nr;
6235 mch_memmove(replace_stack + i, replace_stack + i + 1,
6236 (size_t)(replace_stack_nr - i));
6237 return;
6238 }
6239}
6240
6241/*
6242 * Pop bytes from the replace stack until a NUL is found, and insert them
6243 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6244 */
6245 static void
6246replace_pop_ins()
6247{
6248 int cc;
6249 int oldState = State;
6250
6251 State = NORMAL; /* don't want REPLACE here */
6252 while ((cc = replace_pop()) > 0)
6253 {
6254#ifdef FEAT_MBYTE
6255 mb_replace_pop_ins(cc);
6256#else
6257 ins_char(cc);
6258#endif
6259 dec_cursor();
6260 }
6261 State = oldState;
6262}
6263
6264#ifdef FEAT_MBYTE
6265/*
6266 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6267 * indicates a multi-byte char, pop the other bytes too.
6268 */
6269 static void
6270mb_replace_pop_ins(cc)
6271 int cc;
6272{
6273 int n;
6274 char_u buf[MB_MAXBYTES];
6275 int i;
6276 int c;
6277
6278 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6279 {
6280 buf[0] = cc;
6281 for (i = 1; i < n; ++i)
6282 buf[i] = replace_pop();
6283 ins_bytes_len(buf, n);
6284 }
6285 else
6286 ins_char(cc);
6287
6288 if (enc_utf8)
6289 /* Handle composing chars. */
6290 for (;;)
6291 {
6292 c = replace_pop();
6293 if (c == -1) /* stack empty */
6294 break;
6295 if ((n = MB_BYTE2LEN(c)) == 1)
6296 {
6297 /* Not a multi-byte char, put it back. */
6298 replace_push(c);
6299 break;
6300 }
6301 else
6302 {
6303 buf[0] = c;
6304 for (i = 1; i < n; ++i)
6305 buf[i] = replace_pop();
6306 if (utf_iscomposing(utf_ptr2char(buf)))
6307 ins_bytes_len(buf, n);
6308 else
6309 {
6310 /* Not a composing char, put it back. */
6311 for (i = n - 1; i >= 0; --i)
6312 replace_push(buf[i]);
6313 break;
6314 }
6315 }
6316 }
6317}
6318#endif
6319
6320/*
6321 * make the replace stack empty
6322 * (called when exiting replace mode)
6323 */
6324 static void
6325replace_flush()
6326{
6327 vim_free(replace_stack);
6328 replace_stack = NULL;
6329 replace_stack_len = 0;
6330 replace_stack_nr = 0;
6331}
6332
6333/*
6334 * Handle doing a BS for one character.
6335 * cc < 0: replace stack empty, just move cursor
6336 * cc == 0: character was inserted, delete it
6337 * cc > 0: character was replaced, put cc (first byte of original char) back
6338 * and check for more characters to be put back
6339 */
6340 static void
6341replace_do_bs()
6342{
6343 int cc;
6344#ifdef FEAT_VREPLACE
6345 int orig_len = 0;
6346 int ins_len;
6347 int orig_vcols = 0;
6348 colnr_T start_vcol;
6349 char_u *p;
6350 int i;
6351 int vcol;
6352#endif
6353
6354 cc = replace_pop();
6355 if (cc > 0)
6356 {
6357#ifdef FEAT_VREPLACE
6358 if (State & VREPLACE_FLAG)
6359 {
6360 /* Get the number of screen cells used by the character we are
6361 * going to delete. */
6362 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6363 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6364 }
6365#endif
6366#ifdef FEAT_MBYTE
6367 if (has_mbyte)
6368 {
6369 del_char(FALSE);
6370# ifdef FEAT_VREPLACE
6371 if (State & VREPLACE_FLAG)
6372 orig_len = STRLEN(ml_get_cursor());
6373# endif
6374 replace_push(cc);
6375 }
6376 else
6377#endif
6378 {
6379 pchar_cursor(cc);
6380#ifdef FEAT_VREPLACE
6381 if (State & VREPLACE_FLAG)
6382 orig_len = STRLEN(ml_get_cursor()) - 1;
6383#endif
6384 }
6385 replace_pop_ins();
6386
6387#ifdef FEAT_VREPLACE
6388 if (State & VREPLACE_FLAG)
6389 {
6390 /* Get the number of screen cells used by the inserted characters */
6391 p = ml_get_cursor();
6392 ins_len = STRLEN(p) - orig_len;
6393 vcol = start_vcol;
6394 for (i = 0; i < ins_len; ++i)
6395 {
6396 vcol += chartabsize(p + i, vcol);
6397#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006398 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399#endif
6400 }
6401 vcol -= start_vcol;
6402
6403 /* Delete spaces that were inserted after the cursor to keep the
6404 * text aligned. */
6405 curwin->w_cursor.col += ins_len;
6406 while (vcol > orig_vcols && gchar_cursor() == ' ')
6407 {
6408 del_char(FALSE);
6409 ++orig_vcols;
6410 }
6411 curwin->w_cursor.col -= ins_len;
6412 }
6413#endif
6414
6415 /* mark the buffer as changed and prepare for displaying */
6416 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6417 }
6418 else if (cc == 0)
6419 (void)del_char(FALSE);
6420}
6421
6422#ifdef FEAT_CINDENT
6423/*
6424 * Return TRUE if C-indenting is on.
6425 */
6426 static int
6427cindent_on()
6428{
6429 return (!p_paste && (curbuf->b_p_cin
6430# ifdef FEAT_EVAL
6431 || *curbuf->b_p_inde != NUL
6432# endif
6433 ));
6434}
6435#endif
6436
6437#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6438/*
6439 * Re-indent the current line, based on the current contents of it and the
6440 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6441 * confused what all the part that handles Control-T is doing that I'm not.
6442 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6443 */
6444
6445 void
6446fixthisline(get_the_indent)
6447 int (*get_the_indent) __ARGS((void));
6448{
6449 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6450 if (linewhite(curwin->w_cursor.lnum))
6451 did_ai = TRUE; /* delete the indent if the line stays empty */
6452}
6453
6454 void
6455fix_indent()
6456{
6457 if (p_paste)
6458 return;
6459# ifdef FEAT_LISP
6460 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6461 fixthisline(get_lisp_indent);
6462# endif
6463# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6464 else
6465# endif
6466# ifdef FEAT_CINDENT
6467 if (cindent_on())
6468 do_c_expr_indent();
6469# endif
6470}
6471
6472#endif
6473
6474#ifdef FEAT_CINDENT
6475/*
6476 * return TRUE if 'cinkeys' contains the key "keytyped",
6477 * when == '*': Only if key is preceded with '*' (indent before insert)
6478 * when == '!': Only if key is prededed with '!' (don't insert)
6479 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6480 *
6481 * "keytyped" can have a few special values:
6482 * KEY_OPEN_FORW
6483 * KEY_OPEN_BACK
6484 * KEY_COMPLETE just finished completion.
6485 *
6486 * If line_is_empty is TRUE accept keys with '0' before them.
6487 */
6488 int
6489in_cinkeys(keytyped, when, line_is_empty)
6490 int keytyped;
6491 int when;
6492 int line_is_empty;
6493{
6494 char_u *look;
6495 int try_match;
6496 int try_match_word;
6497 char_u *p;
6498 char_u *line;
6499 int icase;
6500 int i;
6501
6502#ifdef FEAT_EVAL
6503 if (*curbuf->b_p_inde != NUL)
6504 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6505 else
6506#endif
6507 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6508 while (*look)
6509 {
6510 /*
6511 * Find out if we want to try a match with this key, depending on
6512 * 'when' and a '*' or '!' before the key.
6513 */
6514 switch (when)
6515 {
6516 case '*': try_match = (*look == '*'); break;
6517 case '!': try_match = (*look == '!'); break;
6518 default: try_match = (*look != '*'); break;
6519 }
6520 if (*look == '*' || *look == '!')
6521 ++look;
6522
6523 /*
6524 * If there is a '0', only accept a match if the line is empty.
6525 * But may still match when typing last char of a word.
6526 */
6527 if (*look == '0')
6528 {
6529 try_match_word = try_match;
6530 if (!line_is_empty)
6531 try_match = FALSE;
6532 ++look;
6533 }
6534 else
6535 try_match_word = FALSE;
6536
6537 /*
6538 * does it look like a control character?
6539 */
6540 if (*look == '^'
6541#ifdef EBCDIC
6542 && (Ctrl_chr(look[1]) != 0)
6543#else
6544 && look[1] >= '?' && look[1] <= '_'
6545#endif
6546 )
6547 {
6548 if (try_match && keytyped == Ctrl_chr(look[1]))
6549 return TRUE;
6550 look += 2;
6551 }
6552 /*
6553 * 'o' means "o" command, open forward.
6554 * 'O' means "O" command, open backward.
6555 */
6556 else if (*look == 'o')
6557 {
6558 if (try_match && keytyped == KEY_OPEN_FORW)
6559 return TRUE;
6560 ++look;
6561 }
6562 else if (*look == 'O')
6563 {
6564 if (try_match && keytyped == KEY_OPEN_BACK)
6565 return TRUE;
6566 ++look;
6567 }
6568
6569 /*
6570 * 'e' means to check for "else" at start of line and just before the
6571 * cursor.
6572 */
6573 else if (*look == 'e')
6574 {
6575 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6576 {
6577 p = ml_get_curline();
6578 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6579 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6580 return TRUE;
6581 }
6582 ++look;
6583 }
6584
6585 /*
6586 * ':' only causes an indent if it is at the end of a label or case
6587 * statement, or when it was before typing the ':' (to fix
6588 * class::method for C++).
6589 */
6590 else if (*look == ':')
6591 {
6592 if (try_match && keytyped == ':')
6593 {
6594 p = ml_get_curline();
6595 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6596 return TRUE;
6597 if (curwin->w_cursor.col > 2
6598 && p[curwin->w_cursor.col - 1] == ':'
6599 && p[curwin->w_cursor.col - 2] == ':')
6600 {
6601 p[curwin->w_cursor.col - 1] = ' ';
6602 i = (cin_iscase(p) || cin_isscopedecl(p)
6603 || cin_islabel(30));
6604 p = ml_get_curline();
6605 p[curwin->w_cursor.col - 1] = ':';
6606 if (i)
6607 return TRUE;
6608 }
6609 }
6610 ++look;
6611 }
6612
6613
6614 /*
6615 * Is it a key in <>, maybe?
6616 */
6617 else if (*look == '<')
6618 {
6619 if (try_match)
6620 {
6621 /*
6622 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6623 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6624 * >, *, : and ! keys if they really really want to.
6625 */
6626 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6627 && keytyped == look[1])
6628 return TRUE;
6629
6630 if (keytyped == get_special_key_code(look + 1))
6631 return TRUE;
6632 }
6633 while (*look && *look != '>')
6634 look++;
6635 while (*look == '>')
6636 look++;
6637 }
6638
6639 /*
6640 * Is it a word: "=word"?
6641 */
6642 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6643 {
6644 ++look;
6645 if (*look == '~')
6646 {
6647 icase = TRUE;
6648 ++look;
6649 }
6650 else
6651 icase = FALSE;
6652 p = vim_strchr(look, ',');
6653 if (p == NULL)
6654 p = look + STRLEN(look);
6655 if ((try_match || try_match_word)
6656 && curwin->w_cursor.col >= (colnr_T)(p - look))
6657 {
6658 int match = FALSE;
6659
6660#ifdef FEAT_INS_EXPAND
6661 if (keytyped == KEY_COMPLETE)
6662 {
6663 char_u *s;
6664
6665 /* Just completed a word, check if it starts with "look".
6666 * search back for the start of a word. */
6667 line = ml_get_curline();
6668# ifdef FEAT_MBYTE
6669 if (has_mbyte)
6670 {
6671 char_u *n;
6672
6673 for (s = line + curwin->w_cursor.col; s > line; s = n)
6674 {
6675 n = mb_prevptr(line, s);
6676 if (!vim_iswordp(n))
6677 break;
6678 }
6679 }
6680 else
6681# endif
6682 for (s = line + curwin->w_cursor.col; s > line; --s)
6683 if (!vim_iswordc(s[-1]))
6684 break;
6685 if (s + (p - look) <= line + curwin->w_cursor.col
6686 && (icase
6687 ? MB_STRNICMP(s, look, p - look)
6688 : STRNCMP(s, look, p - look)) == 0)
6689 match = TRUE;
6690 }
6691 else
6692#endif
6693 /* TODO: multi-byte */
6694 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6695 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6696 {
6697 line = ml_get_cursor();
6698 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6699 || !vim_iswordc(line[-(p - look) - 1]))
6700 && (icase
6701 ? MB_STRNICMP(line - (p - look), look, p - look)
6702 : STRNCMP(line - (p - look), look, p - look))
6703 == 0)
6704 match = TRUE;
6705 }
6706 if (match && try_match_word && !try_match)
6707 {
6708 /* "0=word": Check if there are only blanks before the
6709 * word. */
6710 line = ml_get_curline();
6711 if ((int)(skipwhite(line) - line) !=
6712 (int)(curwin->w_cursor.col - (p - look)))
6713 match = FALSE;
6714 }
6715 if (match)
6716 return TRUE;
6717 }
6718 look = p;
6719 }
6720
6721 /*
6722 * ok, it's a boring generic character.
6723 */
6724 else
6725 {
6726 if (try_match && *look == keytyped)
6727 return TRUE;
6728 ++look;
6729 }
6730
6731 /*
6732 * Skip over ", ".
6733 */
6734 look = skip_to_option_part(look);
6735 }
6736 return FALSE;
6737}
6738#endif /* FEAT_CINDENT */
6739
6740#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6741/*
6742 * Map Hebrew keyboard when in hkmap mode.
6743 */
6744 int
6745hkmap(c)
6746 int c;
6747{
6748 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6749 {
6750 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6751 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6752 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6753 static char_u map[26] =
6754 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6755 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6756 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6757 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6758 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6759 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6760 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6761 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6762 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6763
6764 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6765 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6766 /* '-1'='sofit' */
6767 else if (c == 'x')
6768 return 'X';
6769 else if (c == 'q')
6770 return '\''; /* {geresh}={'} */
6771 else if (c == 246)
6772 return ' '; /* \"o --> ' ' for a german keyboard */
6773 else if (c == 228)
6774 return ' '; /* \"a --> ' ' -- / -- */
6775 else if (c == 252)
6776 return ' '; /* \"u --> ' ' -- / -- */
6777#ifdef EBCDIC
6778 else if (islower(c))
6779#else
6780 /* NOTE: islower() does not do the right thing for us on Linux so we
6781 * do this the same was as 5.7 and previous, so it works correctly on
6782 * all systems. Specifically, the e.g. Delete and Arrow keys are
6783 * munged and won't work if e.g. searching for Hebrew text.
6784 */
6785 else if (c >= 'a' && c <= 'z')
6786#endif
6787 return (int)(map[CharOrdLow(c)] + p_aleph);
6788 else
6789 return c;
6790 }
6791 else
6792 {
6793 switch (c)
6794 {
6795 case '`': return ';';
6796 case '/': return '.';
6797 case '\'': return ',';
6798 case 'q': return '/';
6799 case 'w': return '\'';
6800
6801 /* Hebrew letters - set offset from 'a' */
6802 case ',': c = '{'; break;
6803 case '.': c = 'v'; break;
6804 case ';': c = 't'; break;
6805 default: {
6806 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6807
6808#ifdef EBCDIC
6809 /* see note about islower() above */
6810 if (!islower(c))
6811#else
6812 if (c < 'a' || c > 'z')
6813#endif
6814 return c;
6815 c = str[CharOrdLow(c)];
6816 break;
6817 }
6818 }
6819
6820 return (int)(CharOrdLow(c) + p_aleph);
6821 }
6822}
6823#endif
6824
6825 static void
6826ins_reg()
6827{
6828 int need_redraw = FALSE;
6829 int regname;
6830 int literally = 0;
6831
6832 /*
6833 * If we are going to wait for a character, show a '"'.
6834 */
6835 pc_status = PC_STATUS_UNSET;
6836 if (redrawing() && !char_avail())
6837 {
6838 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00006839 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840
6841 edit_putchar('"', TRUE);
6842#ifdef FEAT_CMDL_INFO
6843 add_to_showcmd_c(Ctrl_R);
6844#endif
6845 }
6846
6847#ifdef USE_ON_FLY_SCROLL
6848 dont_scroll = TRUE; /* disallow scrolling here */
6849#endif
6850
6851 /*
6852 * Don't map the register name. This also prevents the mode message to be
6853 * deleted when ESC is hit.
6854 */
6855 ++no_mapping;
6856 regname = safe_vgetc();
6857#ifdef FEAT_LANGMAP
6858 LANGMAP_ADJUST(regname, TRUE);
6859#endif
6860 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6861 {
6862 /* Get a third key for literal register insertion */
6863 literally = regname;
6864#ifdef FEAT_CMDL_INFO
6865 add_to_showcmd_c(literally);
6866#endif
6867 regname = safe_vgetc();
6868#ifdef FEAT_LANGMAP
6869 LANGMAP_ADJUST(regname, TRUE);
6870#endif
6871 }
6872 --no_mapping;
6873
6874#ifdef FEAT_EVAL
6875 /*
6876 * Don't call u_sync() while getting the expression,
6877 * evaluating it or giving an error message for it!
6878 */
6879 ++no_u_sync;
6880 if (regname == '=')
6881 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006882# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006884# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006886# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 /* Restore the Input Method. */
6888 if (im_on)
6889 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006892 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6893 {
6894 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 else
6898 {
6899#endif
6900 if (literally == Ctrl_O || literally == Ctrl_P)
6901 {
6902 /* Append the command to the redo buffer. */
6903 AppendCharToRedobuff(Ctrl_R);
6904 AppendCharToRedobuff(literally);
6905 AppendCharToRedobuff(regname);
6906
6907 do_put(regname, BACKWARD, 1L,
6908 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6909 }
6910 else if (insert_reg(regname, literally) == FAIL)
6911 {
6912 vim_beep();
6913 need_redraw = TRUE; /* remove the '"' */
6914 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006915 else if (stop_insert_mode)
6916 /* When the '=' register was used and a function was invoked that
6917 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
6918 * insert anything, need to remove the '"' */
6919 need_redraw = TRUE;
6920
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921#ifdef FEAT_EVAL
6922 }
6923 --no_u_sync;
6924#endif
6925#ifdef FEAT_CMDL_INFO
6926 clear_showcmd();
6927#endif
6928
6929 /* If the inserted register is empty, we need to remove the '"' */
6930 if (need_redraw || stuff_empty())
6931 edit_unputchar();
6932}
6933
6934/*
6935 * CTRL-G commands in Insert mode.
6936 */
6937 static void
6938ins_ctrl_g()
6939{
6940 int c;
6941
6942#ifdef FEAT_INS_EXPAND
6943 /* Right after CTRL-X the cursor will be after the ruler. */
6944 setcursor();
6945#endif
6946
6947 /*
6948 * Don't map the second key. This also prevents the mode message to be
6949 * deleted when ESC is hit.
6950 */
6951 ++no_mapping;
6952 c = safe_vgetc();
6953 --no_mapping;
6954 switch (c)
6955 {
6956 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
6957 case K_UP:
6958 case Ctrl_K:
6959 case 'k': ins_up(TRUE);
6960 break;
6961
6962 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
6963 case K_DOWN:
6964 case Ctrl_J:
6965 case 'j': ins_down(TRUE);
6966 break;
6967
6968 /* CTRL-G u: start new undoable edit */
6969 case 'u': u_sync();
6970 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006971
6972 /* Need to reset Insstart, esp. because a BS that joins
6973 * aline to the previous one must save for undo. */
6974 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 break;
6976
6977 /* Unknown CTRL-G command, reserved for future expansion. */
6978 default: vim_beep();
6979 }
6980}
6981
6982/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006983 * CTRL-^ in Insert mode.
6984 */
6985 static void
6986ins_ctrl_hat()
6987{
6988 if (map_to_exists_mode((char_u *)"", LANGMAP))
6989 {
6990 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
6991 if (State & LANGMAP)
6992 {
6993 curbuf->b_p_iminsert = B_IMODE_NONE;
6994 State &= ~LANGMAP;
6995 }
6996 else
6997 {
6998 curbuf->b_p_iminsert = B_IMODE_LMAP;
6999 State |= LANGMAP;
7000#ifdef USE_IM_CONTROL
7001 im_set_active(FALSE);
7002#endif
7003 }
7004 }
7005#ifdef USE_IM_CONTROL
7006 else
7007 {
7008 /* There are no ":lmap" mappings, toggle IM */
7009 if (im_get_status())
7010 {
7011 curbuf->b_p_iminsert = B_IMODE_NONE;
7012 im_set_active(FALSE);
7013 }
7014 else
7015 {
7016 curbuf->b_p_iminsert = B_IMODE_IM;
7017 State &= ~LANGMAP;
7018 im_set_active(TRUE);
7019 }
7020 }
7021#endif
7022 set_iminsert_global();
7023 showmode();
7024#ifdef FEAT_GUI
7025 /* may show different cursor shape or color */
7026 if (gui.in_use)
7027 gui_update_cursor(TRUE, FALSE);
7028#endif
7029#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7030 /* Show/unshow value of 'keymap' in status lines. */
7031 status_redraw_curbuf();
7032#endif
7033}
7034
7035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 * Handle ESC in insert mode.
7037 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7038 * insert.
7039 */
7040 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007041ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 long *count;
7043 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007044 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046 int temp;
7047 static int disabled_redraw = FALSE;
7048
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007049#ifdef FEAT_SYN_HL
7050 check_spell_redraw();
7051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052#if defined(FEAT_HANGULIN)
7053# if defined(ESC_CHG_TO_ENG_MODE)
7054 hangul_input_state_set(0);
7055# endif
7056 if (composing_hangul)
7057 {
7058 push_raw_key(composing_hangul_buffer, 2);
7059 composing_hangul = 0;
7060 }
7061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062
7063 temp = curwin->w_cursor.col;
7064 if (disabled_redraw)
7065 {
7066 --RedrawingDisabled;
7067 disabled_redraw = FALSE;
7068 }
7069 if (!arrow_used)
7070 {
7071 /*
7072 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007073 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7074 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 */
7076 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007077 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078
7079 /*
7080 * Repeating insert may take a long time. Check for
7081 * interrupt now and then.
7082 */
7083 if (*count > 0)
7084 {
7085 line_breakcheck();
7086 if (got_int)
7087 *count = 0;
7088 }
7089
7090 if (--*count > 0) /* repeat what was typed */
7091 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007092 /* Vi repeats the insert without replacing characters. */
7093 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7094 State &= ~REPLACE_FLAG;
7095
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 (void)start_redo_ins();
7097 if (cmdchar == 'r' || cmdchar == 'v')
7098 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7099 ++RedrawingDisabled;
7100 disabled_redraw = TRUE;
7101 return FALSE; /* repeat the insert */
7102 }
7103 stop_insert(&curwin->w_cursor, TRUE);
7104 undisplay_dollar();
7105 }
7106
7107 /* When an autoindent was removed, curswant stays after the
7108 * indent */
7109 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7110 curwin->w_set_curswant = TRUE;
7111
7112 /* Remember the last Insert position in the '^ mark. */
7113 if (!cmdmod.keepjumps)
7114 curbuf->b_last_insert = curwin->w_cursor;
7115
7116 /*
7117 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007118 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007120 if (!nomove
7121 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122#ifdef FEAT_VIRTUALEDIT
7123 || curwin->w_cursor.coladd > 0
7124#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007125 )
7126 && (restart_edit == NUL
7127 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007129 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007131 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132#ifdef FEAT_RIGHTLEFT
7133 && !revins_on
7134#endif
7135 )
7136 {
7137#ifdef FEAT_VIRTUALEDIT
7138 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7139 {
7140 oneleft();
7141 if (restart_edit != NUL)
7142 ++curwin->w_cursor.coladd;
7143 }
7144 else
7145#endif
7146 {
7147 --curwin->w_cursor.col;
7148#ifdef FEAT_MBYTE
7149 /* Correct cursor for multi-byte character. */
7150 if (has_mbyte)
7151 mb_adjust_cursor();
7152#endif
7153 }
7154 }
7155
7156#ifdef USE_IM_CONTROL
7157 /* Disable IM to allow typing English directly for Normal mode commands.
7158 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7159 * well). */
7160 if (!(State & LANGMAP))
7161 im_save_status(&curbuf->b_p_iminsert);
7162 im_set_active(FALSE);
7163#endif
7164
7165 State = NORMAL;
7166 /* need to position cursor again (e.g. when on a TAB ) */
7167 changed_cline_bef_curs();
7168
7169#ifdef FEAT_MOUSE
7170 setmouse();
7171#endif
7172#ifdef CURSOR_SHAPE
7173 ui_cursor_shape(); /* may show different cursor shape */
7174#endif
7175
7176 /*
7177 * When recording or for CTRL-O, need to display the new mode.
7178 * Otherwise remove the mode message.
7179 */
7180 if (Recording || restart_edit != NUL)
7181 showmode();
7182 else if (p_smd)
7183 MSG("");
7184
7185 return TRUE; /* exit Insert mode */
7186}
7187
7188#ifdef FEAT_RIGHTLEFT
7189/*
7190 * Toggle language: hkmap and revins_on.
7191 * Move to end of reverse inserted text.
7192 */
7193 static void
7194ins_ctrl_()
7195{
7196 if (revins_on && revins_chars && revins_scol >= 0)
7197 {
7198 while (gchar_cursor() != NUL && revins_chars--)
7199 ++curwin->w_cursor.col;
7200 }
7201 p_ri = !p_ri;
7202 revins_on = (State == INSERT && p_ri);
7203 if (revins_on)
7204 {
7205 revins_scol = curwin->w_cursor.col;
7206 revins_legal++;
7207 revins_chars = 0;
7208 undisplay_dollar();
7209 }
7210 else
7211 revins_scol = -1;
7212#ifdef FEAT_FKMAP
7213 if (p_altkeymap)
7214 {
7215 /*
7216 * to be consistent also for redo command, using '.'
7217 * set arrow_used to true and stop it - causing to redo
7218 * characters entered in one mode (normal/reverse insert).
7219 */
7220 arrow_used = TRUE;
7221 (void)stop_arrow();
7222 p_fkmap = curwin->w_p_rl ^ p_ri;
7223 if (p_fkmap && p_ri)
7224 State = INSERT;
7225 }
7226 else
7227#endif
7228 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7229 showmode();
7230}
7231#endif
7232
7233#ifdef FEAT_VISUAL
7234/*
7235 * If 'keymodel' contains "startsel", may start selection.
7236 * Returns TRUE when a CTRL-O and other keys stuffed.
7237 */
7238 static int
7239ins_start_select(c)
7240 int c;
7241{
7242 if (km_startsel)
7243 switch (c)
7244 {
7245 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 case K_PAGEUP:
7248 case K_KPAGEUP:
7249 case K_PAGEDOWN:
7250 case K_KPAGEDOWN:
7251# ifdef MACOS
7252 case K_LEFT:
7253 case K_RIGHT:
7254 case K_UP:
7255 case K_DOWN:
7256 case K_END:
7257 case K_HOME:
7258# endif
7259 if (!(mod_mask & MOD_MASK_SHIFT))
7260 break;
7261 /* FALLTHROUGH */
7262 case K_S_LEFT:
7263 case K_S_RIGHT:
7264 case K_S_UP:
7265 case K_S_DOWN:
7266 case K_S_END:
7267 case K_S_HOME:
7268 /* Start selection right away, the cursor can move with
7269 * CTRL-O when beyond the end of the line. */
7270 start_selection();
7271
7272 /* Execute the key in (insert) Select mode. */
7273 stuffcharReadbuff(Ctrl_O);
7274 if (mod_mask)
7275 {
7276 char_u buf[4];
7277
7278 buf[0] = K_SPECIAL;
7279 buf[1] = KS_MODIFIER;
7280 buf[2] = mod_mask;
7281 buf[3] = NUL;
7282 stuffReadbuff(buf);
7283 }
7284 stuffcharReadbuff(c);
7285 return TRUE;
7286 }
7287 return FALSE;
7288}
7289#endif
7290
7291/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007292 * <Insert> key in Insert mode: toggle insert/remplace mode.
7293 */
7294 static void
7295ins_insert(replaceState)
7296 int replaceState;
7297{
7298#ifdef FEAT_FKMAP
7299 if (p_fkmap && p_ri)
7300 {
7301 beep_flush();
7302 EMSG(farsi_text_3); /* encoded in Farsi */
7303 return;
7304 }
7305#endif
7306
7307#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007308# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007309 set_vim_var_string(VV_INSERTMODE,
7310 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007311# ifdef FEAT_VREPLACE
7312 replaceState == VREPLACE ? "v" :
7313# endif
7314 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007315# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007316 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7317#endif
7318 if (State & REPLACE_FLAG)
7319 State = INSERT | (State & LANGMAP);
7320 else
7321 State = replaceState | (State & LANGMAP);
7322 AppendCharToRedobuff(K_INS);
7323 showmode();
7324#ifdef CURSOR_SHAPE
7325 ui_cursor_shape(); /* may show different cursor shape */
7326#endif
7327}
7328
7329/*
7330 * Pressed CTRL-O in Insert mode.
7331 */
7332 static void
7333ins_ctrl_o()
7334{
7335#ifdef FEAT_VREPLACE
7336 if (State & VREPLACE_FLAG)
7337 restart_edit = 'V';
7338 else
7339#endif
7340 if (State & REPLACE_FLAG)
7341 restart_edit = 'R';
7342 else
7343 restart_edit = 'I';
7344#ifdef FEAT_VIRTUALEDIT
7345 if (virtual_active())
7346 ins_at_eol = FALSE; /* cursor always keeps its column */
7347 else
7348#endif
7349 ins_at_eol = (gchar_cursor() == NUL);
7350}
7351
7352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 * If the cursor is on an indent, ^T/^D insert/delete one
7354 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7355 * Always round the indent to 'shiftwith', this is compatible
7356 * with vi. But vi only supports ^T and ^D after an
7357 * autoindent, we support it everywhere.
7358 */
7359 static void
7360ins_shift(c, lastc)
7361 int c;
7362 int lastc;
7363{
7364 if (stop_arrow() == FAIL)
7365 return;
7366 AppendCharToRedobuff(c);
7367
7368 /*
7369 * 0^D and ^^D: remove all indent.
7370 */
7371 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7372 {
7373 --curwin->w_cursor.col;
7374 (void)del_char(FALSE); /* delete the '^' or '0' */
7375 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7376 if (State & REPLACE_FLAG)
7377 replace_pop_ins();
7378 if (lastc == '^')
7379 old_indent = get_indent(); /* remember curr. indent */
7380 change_indent(INDENT_SET, 0, TRUE, 0);
7381 }
7382 else
7383 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7384
7385 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7386 did_ai = FALSE;
7387#ifdef FEAT_SMARTINDENT
7388 did_si = FALSE;
7389 can_si = FALSE;
7390 can_si_back = FALSE;
7391#endif
7392#ifdef FEAT_CINDENT
7393 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7394#endif
7395}
7396
7397 static void
7398ins_del()
7399{
7400 int temp;
7401
7402 if (stop_arrow() == FAIL)
7403 return;
7404 if (gchar_cursor() == NUL) /* delete newline */
7405 {
7406 temp = curwin->w_cursor.col;
7407 if (!can_bs(BS_EOL) /* only if "eol" included */
7408 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7409 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7410 || do_join(FALSE) == FAIL)
7411 vim_beep();
7412 else
7413 curwin->w_cursor.col = temp;
7414 }
7415 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7416 vim_beep();
7417 did_ai = FALSE;
7418#ifdef FEAT_SMARTINDENT
7419 did_si = FALSE;
7420 can_si = FALSE;
7421 can_si_back = FALSE;
7422#endif
7423 AppendCharToRedobuff(K_DEL);
7424}
7425
7426/*
7427 * Handle Backspace, delete-word and delete-line in Insert mode.
7428 * Return TRUE when backspace was actually used.
7429 */
7430 static int
7431ins_bs(c, mode, inserted_space_p)
7432 int c;
7433 int mode;
7434 int *inserted_space_p;
7435{
7436 linenr_T lnum;
7437 int cc;
7438 int temp = 0; /* init for GCC */
7439 colnr_T mincol;
7440 int did_backspace = FALSE;
7441 int in_indent;
7442 int oldState;
7443#ifdef FEAT_MBYTE
7444 int p1, p2;
7445#endif
7446
7447 /*
7448 * can't delete anything in an empty file
7449 * can't backup past first character in buffer
7450 * can't backup past starting point unless 'backspace' > 1
7451 * can backup to a previous line if 'backspace' == 0
7452 */
7453 if ( bufempty()
7454 || (
7455#ifdef FEAT_RIGHTLEFT
7456 !revins_on &&
7457#endif
7458 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7459 || (!can_bs(BS_START)
7460 && (arrow_used
7461 || (curwin->w_cursor.lnum == Insstart.lnum
7462 && curwin->w_cursor.col <= Insstart.col)))
7463 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7464 && curwin->w_cursor.col <= ai_col)
7465 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7466 {
7467 vim_beep();
7468 return FALSE;
7469 }
7470
7471 if (stop_arrow() == FAIL)
7472 return FALSE;
7473 in_indent = inindent(0);
7474#ifdef FEAT_CINDENT
7475 if (in_indent)
7476 can_cindent = FALSE;
7477#endif
7478#ifdef FEAT_COMMENTS
7479 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7480#endif
7481#ifdef FEAT_RIGHTLEFT
7482 if (revins_on) /* put cursor after last inserted char */
7483 inc_cursor();
7484#endif
7485
7486#ifdef FEAT_VIRTUALEDIT
7487 /* Virtualedit:
7488 * BACKSPACE_CHAR eats a virtual space
7489 * BACKSPACE_WORD eats all coladd
7490 * BACKSPACE_LINE eats all coladd and keeps going
7491 */
7492 if (curwin->w_cursor.coladd > 0)
7493 {
7494 if (mode == BACKSPACE_CHAR)
7495 {
7496 --curwin->w_cursor.coladd;
7497 return TRUE;
7498 }
7499 if (mode == BACKSPACE_WORD)
7500 {
7501 curwin->w_cursor.coladd = 0;
7502 return TRUE;
7503 }
7504 curwin->w_cursor.coladd = 0;
7505 }
7506#endif
7507
7508 /*
7509 * delete newline!
7510 */
7511 if (curwin->w_cursor.col == 0)
7512 {
7513 lnum = Insstart.lnum;
7514 if (curwin->w_cursor.lnum == Insstart.lnum
7515#ifdef FEAT_RIGHTLEFT
7516 || revins_on
7517#endif
7518 )
7519 {
7520 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7521 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7522 return FALSE;
7523 --Insstart.lnum;
7524 Insstart.col = MAXCOL;
7525 }
7526 /*
7527 * In replace mode:
7528 * cc < 0: NL was inserted, delete it
7529 * cc >= 0: NL was replaced, put original characters back
7530 */
7531 cc = -1;
7532 if (State & REPLACE_FLAG)
7533 cc = replace_pop(); /* returns -1 if NL was inserted */
7534 /*
7535 * In replace mode, in the line we started replacing, we only move the
7536 * cursor.
7537 */
7538 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7539 {
7540 dec_cursor();
7541 }
7542 else
7543 {
7544#ifdef FEAT_VREPLACE
7545 if (!(State & VREPLACE_FLAG)
7546 || curwin->w_cursor.lnum > orig_line_count)
7547#endif
7548 {
7549 temp = gchar_cursor(); /* remember current char */
7550 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007551
7552 /* When "aw" is in 'formatoptions' we must delete the space at
7553 * the end of the line, otherwise the line will be broken
7554 * again when auto-formatting. */
7555 if (has_format_option(FO_AUTO)
7556 && has_format_option(FO_WHITE_PAR))
7557 {
7558 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7559 TRUE);
7560 int len;
7561
7562 len = STRLEN(ptr);
7563 if (len > 0 && ptr[len - 1] == ' ')
7564 ptr[len - 1] = NUL;
7565 }
7566
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 (void)do_join(FALSE);
7568 if (temp == NUL && gchar_cursor() != NUL)
7569 inc_cursor();
7570 }
7571#ifdef FEAT_VREPLACE
7572 else
7573 dec_cursor();
7574#endif
7575
7576 /*
7577 * In REPLACE mode we have to put back the text that was replaced
7578 * by the NL. On the replace stack is first a NUL-terminated
7579 * sequence of characters that were deleted and then the
7580 * characters that NL replaced.
7581 */
7582 if (State & REPLACE_FLAG)
7583 {
7584 /*
7585 * Do the next ins_char() in NORMAL state, to
7586 * prevent ins_char() from replacing characters and
7587 * avoiding showmatch().
7588 */
7589 oldState = State;
7590 State = NORMAL;
7591 /*
7592 * restore characters (blanks) deleted after cursor
7593 */
7594 while (cc > 0)
7595 {
7596 temp = curwin->w_cursor.col;
7597#ifdef FEAT_MBYTE
7598 mb_replace_pop_ins(cc);
7599#else
7600 ins_char(cc);
7601#endif
7602 curwin->w_cursor.col = temp;
7603 cc = replace_pop();
7604 }
7605 /* restore the characters that NL replaced */
7606 replace_pop_ins();
7607 State = oldState;
7608 }
7609 }
7610 did_ai = FALSE;
7611 }
7612 else
7613 {
7614 /*
7615 * Delete character(s) before the cursor.
7616 */
7617#ifdef FEAT_RIGHTLEFT
7618 if (revins_on) /* put cursor on last inserted char */
7619 dec_cursor();
7620#endif
7621 mincol = 0;
7622 /* keep indent */
7623 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7624#ifdef FEAT_RIGHTLEFT
7625 && !revins_on
7626#endif
7627 )
7628 {
7629 temp = curwin->w_cursor.col;
7630 beginline(BL_WHITE);
7631 if (curwin->w_cursor.col < (colnr_T)temp)
7632 mincol = curwin->w_cursor.col;
7633 curwin->w_cursor.col = temp;
7634 }
7635
7636 /*
7637 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7638 */
7639 if ( mode == BACKSPACE_CHAR
7640 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007641 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 && (*(ml_get_cursor() - 1) == TAB
7643 || (*(ml_get_cursor() - 1) == ' '
7644 && (!*inserted_space_p
7645 || arrow_used))))))
7646 {
7647 int ts;
7648 colnr_T vcol;
7649 colnr_T want_vcol;
7650 int extra = 0;
7651
7652 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007653 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 ts = curbuf->b_p_sw;
7655 else
7656 ts = curbuf->b_p_sts;
7657 /* Compute the virtual column where we want to be. Since
7658 * 'showbreak' may get in the way, need to get the last column of
7659 * the previous character. */
7660 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7661 dec_cursor();
7662 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7663 inc_cursor();
7664 want_vcol = (want_vcol / ts) * ts;
7665
7666 /* delete characters until we are at or before want_vcol */
7667 while (vcol > want_vcol
7668 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7669 {
7670 dec_cursor();
7671 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7672 if (State & REPLACE_FLAG)
7673 {
7674 /* Don't delete characters before the insert point when in
7675 * Replace mode */
7676 if (curwin->w_cursor.lnum != Insstart.lnum
7677 || curwin->w_cursor.col >= Insstart.col)
7678 {
7679#if 0 /* what was this for? It causes problems when sw != ts. */
7680 if (State == REPLACE && (int)vcol < want_vcol)
7681 {
7682 (void)del_char(FALSE);
7683 extra = 2; /* don't pop too much */
7684 }
7685 else
7686#endif
7687 replace_do_bs();
7688 }
7689 }
7690 else
7691 (void)del_char(FALSE);
7692 }
7693
7694 /* insert extra spaces until we are at want_vcol */
7695 while (vcol < want_vcol)
7696 {
7697 /* Remember the first char we inserted */
7698 if (curwin->w_cursor.lnum == Insstart.lnum
7699 && curwin->w_cursor.col < Insstart.col)
7700 Insstart.col = curwin->w_cursor.col;
7701
7702#ifdef FEAT_VREPLACE
7703 if (State & VREPLACE_FLAG)
7704 ins_char(' ');
7705 else
7706#endif
7707 {
7708 ins_str((char_u *)" ");
7709 if ((State & REPLACE_FLAG) && extra <= 1)
7710 {
7711 if (extra)
7712 replace_push_off(NUL);
7713 else
7714 replace_push(NUL);
7715 }
7716 if (extra == 2)
7717 extra = 1;
7718 }
7719 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7720 }
7721 }
7722
7723 /*
7724 * Delete upto starting point, start of line or previous word.
7725 */
7726 else do
7727 {
7728#ifdef FEAT_RIGHTLEFT
7729 if (!revins_on) /* put cursor on char to be deleted */
7730#endif
7731 dec_cursor();
7732
7733 /* start of word? */
7734 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7735 {
7736 mode = BACKSPACE_WORD_NOT_SPACE;
7737 temp = vim_iswordc(gchar_cursor());
7738 }
7739 /* end of word? */
7740 else if (mode == BACKSPACE_WORD_NOT_SPACE
7741 && (vim_isspace(cc = gchar_cursor())
7742 || vim_iswordc(cc) != temp))
7743 {
7744#ifdef FEAT_RIGHTLEFT
7745 if (!revins_on)
7746#endif
7747 inc_cursor();
7748#ifdef FEAT_RIGHTLEFT
7749 else if (State & REPLACE_FLAG)
7750 dec_cursor();
7751#endif
7752 break;
7753 }
7754 if (State & REPLACE_FLAG)
7755 replace_do_bs();
7756 else
7757 {
7758#ifdef FEAT_MBYTE
7759 if (enc_utf8 && p_deco)
7760 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7761#endif
7762 (void)del_char(FALSE);
7763#ifdef FEAT_MBYTE
7764 /*
7765 * If p1 or p2 is non-zero, there are combining characters we
7766 * need to take account of. Don't back up before the base
7767 * character.
7768 */
7769 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7770 inc_cursor();
7771#endif
7772#ifdef FEAT_RIGHTLEFT
7773 if (revins_chars)
7774 {
7775 revins_chars--;
7776 revins_legal++;
7777 }
7778 if (revins_on && gchar_cursor() == NUL)
7779 break;
7780#endif
7781 }
7782 /* Just a single backspace?: */
7783 if (mode == BACKSPACE_CHAR)
7784 break;
7785 } while (
7786#ifdef FEAT_RIGHTLEFT
7787 revins_on ||
7788#endif
7789 (curwin->w_cursor.col > mincol
7790 && (curwin->w_cursor.lnum != Insstart.lnum
7791 || curwin->w_cursor.col != Insstart.col)));
7792 did_backspace = TRUE;
7793 }
7794#ifdef FEAT_SMARTINDENT
7795 did_si = FALSE;
7796 can_si = FALSE;
7797 can_si_back = FALSE;
7798#endif
7799 if (curwin->w_cursor.col <= 1)
7800 did_ai = FALSE;
7801 /*
7802 * It's a little strange to put backspaces into the redo
7803 * buffer, but it makes auto-indent a lot easier to deal
7804 * with.
7805 */
7806 AppendCharToRedobuff(c);
7807
7808 /* If deleted before the insertion point, adjust it */
7809 if (curwin->w_cursor.lnum == Insstart.lnum
7810 && curwin->w_cursor.col < Insstart.col)
7811 Insstart.col = curwin->w_cursor.col;
7812
7813 /* vi behaviour: the cursor moves backward but the character that
7814 * was there remains visible
7815 * Vim behaviour: the cursor moves backward and the character that
7816 * was there is erased from the screen.
7817 * We can emulate the vi behaviour by pretending there is a dollar
7818 * displayed even when there isn't.
7819 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7820 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7821 dollar_vcol = curwin->w_virtcol;
7822
7823 return did_backspace;
7824}
7825
7826#ifdef FEAT_MOUSE
7827 static void
7828ins_mouse(c)
7829 int c;
7830{
7831 pos_T tpos;
7832
7833# ifdef FEAT_GUI
7834 /* When GUI is active, also move/paste when 'mouse' is empty */
7835 if (!gui.in_use)
7836# endif
7837 if (!mouse_has(MOUSE_INSERT))
7838 return;
7839
7840 undisplay_dollar();
7841 tpos = curwin->w_cursor;
7842 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7843 {
7844 start_arrow(&tpos);
7845# ifdef FEAT_CINDENT
7846 can_cindent = TRUE;
7847# endif
7848 }
7849
7850#ifdef FEAT_WINDOWS
7851 /* redraw status lines (in case another window became active) */
7852 redraw_statuslines();
7853#endif
7854}
7855
7856 static void
7857ins_mousescroll(up)
7858 int up;
7859{
7860 pos_T tpos;
7861# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7862 win_T *old_curwin;
7863# endif
7864
7865 tpos = curwin->w_cursor;
7866
7867# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7868 old_curwin = curwin;
7869
7870 /* Currently the mouse coordinates are only known in the GUI. */
7871 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7872 {
7873 int row, col;
7874
7875 row = mouse_row;
7876 col = mouse_col;
7877
7878 /* find the window at the pointer coordinates */
7879 curwin = mouse_find_win(&row, &col);
7880 curbuf = curwin->w_buffer;
7881 }
7882 if (curwin == old_curwin)
7883# endif
7884 undisplay_dollar();
7885
7886 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7887 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7888 else
7889 scroll_redraw(up, 3L);
7890
7891# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7892 curwin->w_redr_status = TRUE;
7893
7894 curwin = old_curwin;
7895 curbuf = curwin->w_buffer;
7896# endif
7897
7898 if (!equalpos(curwin->w_cursor, tpos))
7899 {
7900 start_arrow(&tpos);
7901# ifdef FEAT_CINDENT
7902 can_cindent = TRUE;
7903# endif
7904 }
7905}
7906#endif
7907
7908#ifdef FEAT_GUI
7909 void
7910ins_scroll()
7911{
7912 pos_T tpos;
7913
7914 undisplay_dollar();
7915 tpos = curwin->w_cursor;
7916 if (gui_do_scroll())
7917 {
7918 start_arrow(&tpos);
7919# ifdef FEAT_CINDENT
7920 can_cindent = TRUE;
7921# endif
7922 }
7923}
7924
7925 void
7926ins_horscroll()
7927{
7928 pos_T tpos;
7929
7930 undisplay_dollar();
7931 tpos = curwin->w_cursor;
7932 if (gui_do_horiz_scroll())
7933 {
7934 start_arrow(&tpos);
7935# ifdef FEAT_CINDENT
7936 can_cindent = TRUE;
7937# endif
7938 }
7939}
7940#endif
7941
7942 static void
7943ins_left()
7944{
7945 pos_T tpos;
7946
7947#ifdef FEAT_FOLDING
7948 if ((fdo_flags & FDO_HOR) && KeyTyped)
7949 foldOpenCursor();
7950#endif
7951 undisplay_dollar();
7952 tpos = curwin->w_cursor;
7953 if (oneleft() == OK)
7954 {
7955 start_arrow(&tpos);
7956#ifdef FEAT_RIGHTLEFT
7957 /* If exit reversed string, position is fixed */
7958 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
7959 revins_legal++;
7960 revins_chars++;
7961#endif
7962 }
7963
7964 /*
7965 * if 'whichwrap' set for cursor in insert mode may go to
7966 * previous line
7967 */
7968 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
7969 {
7970 start_arrow(&tpos);
7971 --(curwin->w_cursor.lnum);
7972 coladvance((colnr_T)MAXCOL);
7973 curwin->w_set_curswant = TRUE; /* so we stay at the end */
7974 }
7975 else
7976 vim_beep();
7977}
7978
7979 static void
7980ins_home(c)
7981 int c;
7982{
7983 pos_T tpos;
7984
7985#ifdef FEAT_FOLDING
7986 if ((fdo_flags & FDO_HOR) && KeyTyped)
7987 foldOpenCursor();
7988#endif
7989 undisplay_dollar();
7990 tpos = curwin->w_cursor;
7991 if (c == K_C_HOME)
7992 curwin->w_cursor.lnum = 1;
7993 curwin->w_cursor.col = 0;
7994#ifdef FEAT_VIRTUALEDIT
7995 curwin->w_cursor.coladd = 0;
7996#endif
7997 curwin->w_curswant = 0;
7998 start_arrow(&tpos);
7999}
8000
8001 static void
8002ins_end(c)
8003 int c;
8004{
8005 pos_T tpos;
8006
8007#ifdef FEAT_FOLDING
8008 if ((fdo_flags & FDO_HOR) && KeyTyped)
8009 foldOpenCursor();
8010#endif
8011 undisplay_dollar();
8012 tpos = curwin->w_cursor;
8013 if (c == K_C_END)
8014 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8015 coladvance((colnr_T)MAXCOL);
8016 curwin->w_curswant = MAXCOL;
8017
8018 start_arrow(&tpos);
8019}
8020
8021 static void
8022ins_s_left()
8023{
8024#ifdef FEAT_FOLDING
8025 if ((fdo_flags & FDO_HOR) && KeyTyped)
8026 foldOpenCursor();
8027#endif
8028 undisplay_dollar();
8029 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8030 {
8031 start_arrow(&curwin->w_cursor);
8032 (void)bck_word(1L, FALSE, FALSE);
8033 curwin->w_set_curswant = TRUE;
8034 }
8035 else
8036 vim_beep();
8037}
8038
8039 static void
8040ins_right()
8041{
8042#ifdef FEAT_FOLDING
8043 if ((fdo_flags & FDO_HOR) && KeyTyped)
8044 foldOpenCursor();
8045#endif
8046 undisplay_dollar();
8047 if (gchar_cursor() != NUL || virtual_active()
8048 )
8049 {
8050 start_arrow(&curwin->w_cursor);
8051 curwin->w_set_curswant = TRUE;
8052#ifdef FEAT_VIRTUALEDIT
8053 if (virtual_active())
8054 oneright();
8055 else
8056#endif
8057 {
8058#ifdef FEAT_MBYTE
8059 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008060 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 else
8062#endif
8063 ++curwin->w_cursor.col;
8064 }
8065
8066#ifdef FEAT_RIGHTLEFT
8067 revins_legal++;
8068 if (revins_chars)
8069 revins_chars--;
8070#endif
8071 }
8072 /* if 'whichwrap' set for cursor in insert mode, may move the
8073 * cursor to the next line */
8074 else if (vim_strchr(p_ww, ']') != NULL
8075 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8076 {
8077 start_arrow(&curwin->w_cursor);
8078 curwin->w_set_curswant = TRUE;
8079 ++curwin->w_cursor.lnum;
8080 curwin->w_cursor.col = 0;
8081 }
8082 else
8083 vim_beep();
8084}
8085
8086 static void
8087ins_s_right()
8088{
8089#ifdef FEAT_FOLDING
8090 if ((fdo_flags & FDO_HOR) && KeyTyped)
8091 foldOpenCursor();
8092#endif
8093 undisplay_dollar();
8094 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8095 || gchar_cursor() != NUL)
8096 {
8097 start_arrow(&curwin->w_cursor);
8098 (void)fwd_word(1L, FALSE, 0);
8099 curwin->w_set_curswant = TRUE;
8100 }
8101 else
8102 vim_beep();
8103}
8104
8105 static void
8106ins_up(startcol)
8107 int startcol; /* when TRUE move to Insstart.col */
8108{
8109 pos_T tpos;
8110 linenr_T old_topline = curwin->w_topline;
8111#ifdef FEAT_DIFF
8112 int old_topfill = curwin->w_topfill;
8113#endif
8114
8115 undisplay_dollar();
8116 tpos = curwin->w_cursor;
8117 if (cursor_up(1L, TRUE) == OK)
8118 {
8119 if (startcol)
8120 coladvance(getvcol_nolist(&Insstart));
8121 if (old_topline != curwin->w_topline
8122#ifdef FEAT_DIFF
8123 || old_topfill != curwin->w_topfill
8124#endif
8125 )
8126 redraw_later(VALID);
8127 start_arrow(&tpos);
8128#ifdef FEAT_CINDENT
8129 can_cindent = TRUE;
8130#endif
8131 }
8132 else
8133 vim_beep();
8134}
8135
8136 static void
8137ins_pageup()
8138{
8139 pos_T tpos;
8140
8141 undisplay_dollar();
8142 tpos = curwin->w_cursor;
8143 if (onepage(BACKWARD, 1L) == OK)
8144 {
8145 start_arrow(&tpos);
8146#ifdef FEAT_CINDENT
8147 can_cindent = TRUE;
8148#endif
8149 }
8150 else
8151 vim_beep();
8152}
8153
8154 static void
8155ins_down(startcol)
8156 int startcol; /* when TRUE move to Insstart.col */
8157{
8158 pos_T tpos;
8159 linenr_T old_topline = curwin->w_topline;
8160#ifdef FEAT_DIFF
8161 int old_topfill = curwin->w_topfill;
8162#endif
8163
8164 undisplay_dollar();
8165 tpos = curwin->w_cursor;
8166 if (cursor_down(1L, TRUE) == OK)
8167 {
8168 if (startcol)
8169 coladvance(getvcol_nolist(&Insstart));
8170 if (old_topline != curwin->w_topline
8171#ifdef FEAT_DIFF
8172 || old_topfill != curwin->w_topfill
8173#endif
8174 )
8175 redraw_later(VALID);
8176 start_arrow(&tpos);
8177#ifdef FEAT_CINDENT
8178 can_cindent = TRUE;
8179#endif
8180 }
8181 else
8182 vim_beep();
8183}
8184
8185 static void
8186ins_pagedown()
8187{
8188 pos_T tpos;
8189
8190 undisplay_dollar();
8191 tpos = curwin->w_cursor;
8192 if (onepage(FORWARD, 1L) == OK)
8193 {
8194 start_arrow(&tpos);
8195#ifdef FEAT_CINDENT
8196 can_cindent = TRUE;
8197#endif
8198 }
8199 else
8200 vim_beep();
8201}
8202
8203#ifdef FEAT_DND
8204 static void
8205ins_drop()
8206{
8207 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8208}
8209#endif
8210
8211/*
8212 * Handle TAB in Insert or Replace mode.
8213 * Return TRUE when the TAB needs to be inserted like a normal character.
8214 */
8215 static int
8216ins_tab()
8217{
8218 int ind;
8219 int i;
8220 int temp;
8221
8222 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8223 Insstart_blank_vcol = get_nolist_virtcol();
8224 if (echeck_abbr(TAB + ABBR_OFF))
8225 return FALSE;
8226
8227 ind = inindent(0);
8228#ifdef FEAT_CINDENT
8229 if (ind)
8230 can_cindent = FALSE;
8231#endif
8232
8233 /*
8234 * When nothing special, insert TAB like a normal character
8235 */
8236 if (!curbuf->b_p_et
8237 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8238 && curbuf->b_p_sts == 0)
8239 return TRUE;
8240
8241 if (stop_arrow() == FAIL)
8242 return TRUE;
8243
8244 did_ai = FALSE;
8245#ifdef FEAT_SMARTINDENT
8246 did_si = FALSE;
8247 can_si = FALSE;
8248 can_si_back = FALSE;
8249#endif
8250 AppendToRedobuff((char_u *)"\t");
8251
8252 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8253 temp = (int)curbuf->b_p_sw;
8254 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8255 temp = (int)curbuf->b_p_sts;
8256 else /* otherwise use 'tabstop' */
8257 temp = (int)curbuf->b_p_ts;
8258 temp -= get_nolist_virtcol() % temp;
8259
8260 /*
8261 * Insert the first space with ins_char(). It will delete one char in
8262 * replace mode. Insert the rest with ins_str(); it will not delete any
8263 * chars. For VREPLACE mode, we use ins_char() for all characters.
8264 */
8265 ins_char(' ');
8266 while (--temp > 0)
8267 {
8268#ifdef FEAT_VREPLACE
8269 if (State & VREPLACE_FLAG)
8270 ins_char(' ');
8271 else
8272#endif
8273 {
8274 ins_str((char_u *)" ");
8275 if (State & REPLACE_FLAG) /* no char replaced */
8276 replace_push(NUL);
8277 }
8278 }
8279
8280 /*
8281 * When 'expandtab' not set: Replace spaces by TABs where possible.
8282 */
8283 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8284 {
8285 char_u *ptr;
8286#ifdef FEAT_VREPLACE
8287 char_u *saved_line = NULL; /* init for GCC */
8288 pos_T pos;
8289#endif
8290 pos_T fpos;
8291 pos_T *cursor;
8292 colnr_T want_vcol, vcol;
8293 int change_col = -1;
8294 int save_list = curwin->w_p_list;
8295
8296 /*
8297 * Get the current line. For VREPLACE mode, don't make real changes
8298 * yet, just work on a copy of the line.
8299 */
8300#ifdef FEAT_VREPLACE
8301 if (State & VREPLACE_FLAG)
8302 {
8303 pos = curwin->w_cursor;
8304 cursor = &pos;
8305 saved_line = vim_strsave(ml_get_curline());
8306 if (saved_line == NULL)
8307 return FALSE;
8308 ptr = saved_line + pos.col;
8309 }
8310 else
8311#endif
8312 {
8313 ptr = ml_get_cursor();
8314 cursor = &curwin->w_cursor;
8315 }
8316
8317 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8318 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8319 curwin->w_p_list = FALSE;
8320
8321 /* Find first white before the cursor */
8322 fpos = curwin->w_cursor;
8323 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8324 {
8325 --fpos.col;
8326 --ptr;
8327 }
8328
8329 /* In Replace mode, don't change characters before the insert point. */
8330 if ((State & REPLACE_FLAG)
8331 && fpos.lnum == Insstart.lnum
8332 && fpos.col < Insstart.col)
8333 {
8334 ptr += Insstart.col - fpos.col;
8335 fpos.col = Insstart.col;
8336 }
8337
8338 /* compute virtual column numbers of first white and cursor */
8339 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8340 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8341
8342 /* Use as many TABs as possible. Beware of 'showbreak' and
8343 * 'linebreak' adding extra virtual columns. */
8344 while (vim_iswhite(*ptr))
8345 {
8346 i = lbr_chartabsize((char_u *)"\t", vcol);
8347 if (vcol + i > want_vcol)
8348 break;
8349 if (*ptr != TAB)
8350 {
8351 *ptr = TAB;
8352 if (change_col < 0)
8353 {
8354 change_col = fpos.col; /* Column of first change */
8355 /* May have to adjust Insstart */
8356 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8357 Insstart.col = fpos.col;
8358 }
8359 }
8360 ++fpos.col;
8361 ++ptr;
8362 vcol += i;
8363 }
8364
8365 if (change_col >= 0)
8366 {
8367 int repl_off = 0;
8368
8369 /* Skip over the spaces we need. */
8370 while (vcol < want_vcol && *ptr == ' ')
8371 {
8372 vcol += lbr_chartabsize(ptr, vcol);
8373 ++ptr;
8374 ++repl_off;
8375 }
8376 if (vcol > want_vcol)
8377 {
8378 /* Must have a char with 'showbreak' just before it. */
8379 --ptr;
8380 --repl_off;
8381 }
8382 fpos.col += repl_off;
8383
8384 /* Delete following spaces. */
8385 i = cursor->col - fpos.col;
8386 if (i > 0)
8387 {
8388 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8389 /* correct replace stack. */
8390 if ((State & REPLACE_FLAG)
8391#ifdef FEAT_VREPLACE
8392 && !(State & VREPLACE_FLAG)
8393#endif
8394 )
8395 for (temp = i; --temp >= 0; )
8396 replace_join(repl_off);
8397 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008398#ifdef FEAT_NETBEANS_INTG
8399 if (usingNetbeans)
8400 {
8401 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8402 (long)(i + 1));
8403 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8404 (char_u *)"\t", 1);
8405 }
8406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 cursor->col -= i;
8408
8409#ifdef FEAT_VREPLACE
8410 /*
8411 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8412 * backspacing over the changed spacing and then inserting the new
8413 * spacing.
8414 */
8415 if (State & VREPLACE_FLAG)
8416 {
8417 /* Backspace from real cursor to change_col */
8418 backspace_until_column(change_col);
8419
8420 /* Insert each char in saved_line from changed_col to
8421 * ptr-cursor */
8422 ins_bytes_len(saved_line + change_col,
8423 cursor->col - change_col);
8424 }
8425#endif
8426 }
8427
8428#ifdef FEAT_VREPLACE
8429 if (State & VREPLACE_FLAG)
8430 vim_free(saved_line);
8431#endif
8432 curwin->w_p_list = save_list;
8433 }
8434
8435 return FALSE;
8436}
8437
8438/*
8439 * Handle CR or NL in insert mode.
8440 * Return TRUE when out of memory or can't undo.
8441 */
8442 static int
8443ins_eol(c)
8444 int c;
8445{
8446 int i;
8447
8448 if (echeck_abbr(c + ABBR_OFF))
8449 return FALSE;
8450 if (stop_arrow() == FAIL)
8451 return TRUE;
8452 undisplay_dollar();
8453
8454 /*
8455 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8456 * character under the cursor. Only push a NUL on the replace stack,
8457 * nothing to put back when the NL is deleted.
8458 */
8459 if ((State & REPLACE_FLAG)
8460#ifdef FEAT_VREPLACE
8461 && !(State & VREPLACE_FLAG)
8462#endif
8463 )
8464 replace_push(NUL);
8465
8466 /*
8467 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8468 * replacing the next line, so we push all of the characters left on the
8469 * line onto the replace stack. This is not done here though, it is done
8470 * in open_line().
8471 */
8472
8473#ifdef FEAT_RIGHTLEFT
8474# ifdef FEAT_FKMAP
8475 if (p_altkeymap && p_fkmap)
8476 fkmap(NL);
8477# endif
8478 /* NL in reverse insert will always start in the end of
8479 * current line. */
8480 if (revins_on)
8481 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8482#endif
8483
8484 AppendToRedobuff(NL_STR);
8485 i = open_line(FORWARD,
8486#ifdef FEAT_COMMENTS
8487 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8488#endif
8489 0, old_indent);
8490 old_indent = 0;
8491#ifdef FEAT_CINDENT
8492 can_cindent = TRUE;
8493#endif
8494
8495 return (!i);
8496}
8497
8498#ifdef FEAT_DIGRAPHS
8499/*
8500 * Handle digraph in insert mode.
8501 * Returns character still to be inserted, or NUL when nothing remaining to be
8502 * done.
8503 */
8504 static int
8505ins_digraph()
8506{
8507 int c;
8508 int cc;
8509
8510 pc_status = PC_STATUS_UNSET;
8511 if (redrawing() && !char_avail())
8512 {
8513 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008514 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515
8516 edit_putchar('?', TRUE);
8517#ifdef FEAT_CMDL_INFO
8518 add_to_showcmd_c(Ctrl_K);
8519#endif
8520 }
8521
8522#ifdef USE_ON_FLY_SCROLL
8523 dont_scroll = TRUE; /* disallow scrolling here */
8524#endif
8525
8526 /* don't map the digraph chars. This also prevents the
8527 * mode message to be deleted when ESC is hit */
8528 ++no_mapping;
8529 ++allow_keys;
8530 c = safe_vgetc();
8531 --no_mapping;
8532 --allow_keys;
8533 if (IS_SPECIAL(c) || mod_mask) /* special key */
8534 {
8535#ifdef FEAT_CMDL_INFO
8536 clear_showcmd();
8537#endif
8538 insert_special(c, TRUE, FALSE);
8539 return NUL;
8540 }
8541 if (c != ESC)
8542 {
8543 if (redrawing() && !char_avail())
8544 {
8545 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008546 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547
8548 if (char2cells(c) == 1)
8549 {
8550 /* first remove the '?', otherwise it's restored when typing
8551 * an ESC next */
8552 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008553 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 edit_putchar(c, TRUE);
8555 }
8556#ifdef FEAT_CMDL_INFO
8557 add_to_showcmd_c(c);
8558#endif
8559 }
8560 ++no_mapping;
8561 ++allow_keys;
8562 cc = safe_vgetc();
8563 --no_mapping;
8564 --allow_keys;
8565 if (cc != ESC)
8566 {
8567 AppendToRedobuff((char_u *)CTRL_V_STR);
8568 c = getdigraph(c, cc, TRUE);
8569#ifdef FEAT_CMDL_INFO
8570 clear_showcmd();
8571#endif
8572 return c;
8573 }
8574 }
8575 edit_unputchar();
8576#ifdef FEAT_CMDL_INFO
8577 clear_showcmd();
8578#endif
8579 return NUL;
8580}
8581#endif
8582
8583/*
8584 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8585 * Returns the char to be inserted, or NUL if none found.
8586 */
8587 static int
8588ins_copychar(lnum)
8589 linenr_T lnum;
8590{
8591 int c;
8592 int temp;
8593 char_u *ptr, *prev_ptr;
8594
8595 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8596 {
8597 vim_beep();
8598 return NUL;
8599 }
8600
8601 /* try to advance to the cursor column */
8602 temp = 0;
8603 ptr = ml_get(lnum);
8604 prev_ptr = ptr;
8605 validate_virtcol();
8606 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8607 {
8608 prev_ptr = ptr;
8609 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8610 }
8611 if ((colnr_T)temp > curwin->w_virtcol)
8612 ptr = prev_ptr;
8613
8614#ifdef FEAT_MBYTE
8615 c = (*mb_ptr2char)(ptr);
8616#else
8617 c = *ptr;
8618#endif
8619 if (c == NUL)
8620 vim_beep();
8621 return c;
8622}
8623
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008624/*
8625 * CTRL-Y or CTRL-E typed in Insert mode.
8626 */
8627 static int
8628ins_ctrl_ey(tc)
8629 int tc;
8630{
8631 int c = tc;
8632
8633#ifdef FEAT_INS_EXPAND
8634 if (ctrl_x_mode == CTRL_X_SCROLL)
8635 {
8636 if (c == Ctrl_Y)
8637 scrolldown_clamp();
8638 else
8639 scrollup_clamp();
8640 redraw_later(VALID);
8641 }
8642 else
8643#endif
8644 {
8645 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8646 if (c != NUL)
8647 {
8648 long tw_save;
8649
8650 /* The character must be taken literally, insert like it
8651 * was typed after a CTRL-V, and pretend 'textwidth'
8652 * wasn't set. Digits, 'o' and 'x' are special after a
8653 * CTRL-V, don't use it for these. */
8654 if (c < 256 && !isalnum(c))
8655 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8656 tw_save = curbuf->b_p_tw;
8657 curbuf->b_p_tw = -1;
8658 insert_special(c, TRUE, FALSE);
8659 curbuf->b_p_tw = tw_save;
8660#ifdef FEAT_RIGHTLEFT
8661 revins_chars++;
8662 revins_legal++;
8663#endif
8664 c = Ctrl_V; /* pretend CTRL-V is last character */
8665 auto_format(FALSE, TRUE);
8666 }
8667 }
8668 return c;
8669}
8670
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671#ifdef FEAT_SMARTINDENT
8672/*
8673 * Try to do some very smart auto-indenting.
8674 * Used when inserting a "normal" character.
8675 */
8676 static void
8677ins_try_si(c)
8678 int c;
8679{
8680 pos_T *pos, old_pos;
8681 char_u *ptr;
8682 int i;
8683 int temp;
8684
8685 /*
8686 * do some very smart indenting when entering '{' or '}'
8687 */
8688 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8689 {
8690 /*
8691 * for '}' set indent equal to indent of line containing matching '{'
8692 */
8693 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8694 {
8695 old_pos = curwin->w_cursor;
8696 /*
8697 * If the matching '{' has a ')' immediately before it (ignoring
8698 * white-space), then line up with the start of the line
8699 * containing the matching '(' if there is one. This handles the
8700 * case where an "if (..\n..) {" statement continues over multiple
8701 * lines -- webb
8702 */
8703 ptr = ml_get(pos->lnum);
8704 i = pos->col;
8705 if (i > 0) /* skip blanks before '{' */
8706 while (--i > 0 && vim_iswhite(ptr[i]))
8707 ;
8708 curwin->w_cursor.lnum = pos->lnum;
8709 curwin->w_cursor.col = i;
8710 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8711 curwin->w_cursor = *pos;
8712 i = get_indent();
8713 curwin->w_cursor = old_pos;
8714#ifdef FEAT_VREPLACE
8715 if (State & VREPLACE_FLAG)
8716 change_indent(INDENT_SET, i, FALSE, NUL);
8717 else
8718#endif
8719 (void)set_indent(i, SIN_CHANGED);
8720 }
8721 else if (curwin->w_cursor.col > 0)
8722 {
8723 /*
8724 * when inserting '{' after "O" reduce indent, but not
8725 * more than indent of previous line
8726 */
8727 temp = TRUE;
8728 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8729 {
8730 old_pos = curwin->w_cursor;
8731 i = get_indent();
8732 while (curwin->w_cursor.lnum > 1)
8733 {
8734 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8735
8736 /* ignore empty lines and lines starting with '#'. */
8737 if (*ptr != '#' && *ptr != NUL)
8738 break;
8739 }
8740 if (get_indent() >= i)
8741 temp = FALSE;
8742 curwin->w_cursor = old_pos;
8743 }
8744 if (temp)
8745 shift_line(TRUE, FALSE, 1);
8746 }
8747 }
8748
8749 /*
8750 * set indent of '#' always to 0
8751 */
8752 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8753 {
8754 /* remember current indent for next line */
8755 old_indent = get_indent();
8756 (void)set_indent(0, SIN_CHANGED);
8757 }
8758
8759 /* Adjust ai_col, the char at this position can be deleted. */
8760 if (ai_col > curwin->w_cursor.col)
8761 ai_col = curwin->w_cursor.col;
8762}
8763#endif
8764
8765/*
8766 * Get the value that w_virtcol would have when 'list' is off.
8767 * Unless 'cpo' contains the 'L' flag.
8768 */
8769 static colnr_T
8770get_nolist_virtcol()
8771{
8772 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8773 return getvcol_nolist(&curwin->w_cursor);
8774 validate_virtcol();
8775 return curwin->w_virtcol;
8776}