blob: 18a1cf15e478d1afda3cbf2dea3d66b2cf829e47 [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 Moolenaar071d4272004-06-13 20:20:40 +0000133static void ins_compl_free __ARGS((void));
134static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000135static int ins_compl_bs __ARGS((void));
136static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000137static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000138static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000140static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141static void ins_compl_delete __ARGS((void));
142static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000143static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000144static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000145static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000146static int ins_compl_key2count __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static int ins_complete __ARGS((int c));
148static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
149#endif /* FEAT_INS_EXPAND */
150
151#define BACKSPACE_CHAR 1
152#define BACKSPACE_WORD 2
153#define BACKSPACE_WORD_NOT_SPACE 3
154#define BACKSPACE_LINE 4
155
Bram Moolenaar754b5602006-02-09 23:53:20 +0000156static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157static void ins_ctrl_v __ARGS((void));
158static void undisplay_dollar __ARGS((void));
159static void insert_special __ARGS((int, int, int));
160static void check_auto_format __ARGS((int));
161static void redo_literal __ARGS((int c));
162static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000163#ifdef FEAT_SYN_HL
164static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000165static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000166static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
169static int echeck_abbr __ARGS((int));
170static void replace_push_off __ARGS((int c));
171static int replace_pop __ARGS((void));
172static void replace_join __ARGS((int off));
173static void replace_pop_ins __ARGS((void));
174#ifdef FEAT_MBYTE
175static void mb_replace_pop_ins __ARGS((int cc));
176#endif
177static void replace_flush __ARGS((void));
178static void replace_do_bs __ARGS((void));
179#ifdef FEAT_CINDENT
180static int cindent_on __ARGS((void));
181#endif
182static void ins_reg __ARGS((void));
183static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000184static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000185static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186#ifdef FEAT_RIGHTLEFT
187static void ins_ctrl_ __ARGS((void));
188#endif
189#ifdef FEAT_VISUAL
190static int ins_start_select __ARGS((int c));
191#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000192static void ins_insert __ARGS((int replaceState));
193static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194static void ins_shift __ARGS((int c, int lastc));
195static void ins_del __ARGS((void));
196static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
197#ifdef FEAT_MOUSE
198static void ins_mouse __ARGS((int c));
199static void ins_mousescroll __ARGS((int up));
200#endif
201static void ins_left __ARGS((void));
202static void ins_home __ARGS((int c));
203static void ins_end __ARGS((int c));
204static void ins_s_left __ARGS((void));
205static void ins_right __ARGS((void));
206static void ins_s_right __ARGS((void));
207static void ins_up __ARGS((int startcol));
208static void ins_pageup __ARGS((void));
209static void ins_down __ARGS((int startcol));
210static void ins_pagedown __ARGS((void));
211#ifdef FEAT_DND
212static void ins_drop __ARGS((void));
213#endif
214static int ins_tab __ARGS((void));
215static int ins_eol __ARGS((int c));
216#ifdef FEAT_DIGRAPHS
217static int ins_digraph __ARGS((void));
218#endif
219static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000220static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#ifdef FEAT_SMARTINDENT
222static void ins_try_si __ARGS((int c));
223#endif
224static colnr_T get_nolist_virtcol __ARGS((void));
225
226static colnr_T Insstart_textlen; /* length of line when insert started */
227static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
228
229static char_u *last_insert = NULL; /* the text of the previous insert,
230 K_SPECIAL and CSI are escaped */
231static int last_insert_skip; /* nr of chars in front of previous insert */
232static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000233static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234
235#ifdef FEAT_CINDENT
236static int can_cindent; /* may do cindenting on this line */
237#endif
238
239static int old_indent = 0; /* for ^^D command in insert mode */
240
241#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000242static int revins_on; /* reverse insert mode on */
243static int revins_chars; /* how much to skip after edit */
244static int revins_legal; /* was the last char 'legal'? */
245static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246#endif
247
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248static int ins_need_undo; /* call u_save() before inserting a
249 char. Set when edit() is called.
250 after that arrow_used is used. */
251
252static int did_add_space = FALSE; /* auto_format() added an extra space
253 under the cursor */
254
255/*
256 * edit(): Start inserting text.
257 *
258 * "cmdchar" can be:
259 * 'i' normal insert command
260 * 'a' normal append command
261 * 'R' replace command
262 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
263 * but still only one <CR> is inserted. The <Esc> is not used for redo.
264 * 'g' "gI" command.
265 * 'V' "gR" command for Virtual Replace mode.
266 * 'v' "gr" command for single character Virtual Replace mode.
267 *
268 * This function is not called recursively. For CTRL-O commands, it returns
269 * and lets the caller handle the Normal-mode command.
270 *
271 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
272 */
273 int
274edit(cmdchar, startln, count)
275 int cmdchar;
276 int startln; /* if set, insert at start of line */
277 long count;
278{
279 int c = 0;
280 char_u *ptr;
281 int lastc;
282 colnr_T mincol;
283 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 int i;
285 int did_backspace = TRUE; /* previous char was backspace */
286#ifdef FEAT_CINDENT
287 int line_is_white = FALSE; /* line is empty before insert */
288#endif
289 linenr_T old_topline = 0; /* topline before insertion */
290#ifdef FEAT_DIFF
291 int old_topfill = -1;
292#endif
293 int inserted_space = FALSE; /* just inserted a space */
294 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000295 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000297 /* Remember whether editing was restarted after CTRL-O. */
298 did_restart_edit = restart_edit;
299
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 /* sleep before redrawing, needed for "CTRL-O :" that results in an
301 * error message */
302 check_for_delay(TRUE);
303
304#ifdef HAVE_SANDBOX
305 /* Don't allow inserting in the sandbox. */
306 if (sandbox != 0)
307 {
308 EMSG(_(e_sandbox));
309 return FALSE;
310 }
311#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000312 /* Don't allow changes in the buffer while editing the cmdline. The
313 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000314 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000315 {
316 EMSG(_(e_secure));
317 return FALSE;
318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319
320#ifdef FEAT_INS_EXPAND
321 ins_compl_clear(); /* clear stuff for CTRL-X mode */
322#endif
323
Bram Moolenaar843ee412004-06-30 16:16:41 +0000324#ifdef FEAT_AUTOCMD
325 /*
326 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
327 */
328 if (cmdchar != 'r' && cmdchar != 'v')
329 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000330# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000331 if (cmdchar == 'R')
332 ptr = (char_u *)"r";
333 else if (cmdchar == 'V')
334 ptr = (char_u *)"v";
335 else
336 ptr = (char_u *)"i";
337 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000338# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000339 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
340 }
341#endif
342
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#ifdef FEAT_MOUSE
344 /*
345 * When doing a paste with the middle mouse button, Insstart is set to
346 * where the paste started.
347 */
348 if (where_paste_started.lnum != 0)
349 Insstart = where_paste_started;
350 else
351#endif
352 {
353 Insstart = curwin->w_cursor;
354 if (startln)
355 Insstart.col = 0;
356 }
357 Insstart_textlen = linetabsize(ml_get_curline());
358 Insstart_blank_vcol = MAXCOL;
359 if (!did_ai)
360 ai_col = 0;
361
362 if (cmdchar != NUL && restart_edit == 0)
363 {
364 ResetRedobuff();
365 AppendNumberToRedobuff(count);
366#ifdef FEAT_VREPLACE
367 if (cmdchar == 'V' || cmdchar == 'v')
368 {
369 /* "gR" or "gr" command */
370 AppendCharToRedobuff('g');
371 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
372 }
373 else
374#endif
375 {
376 AppendCharToRedobuff(cmdchar);
377 if (cmdchar == 'g') /* "gI" command */
378 AppendCharToRedobuff('I');
379 else if (cmdchar == 'r') /* "r<CR>" command */
380 count = 1; /* insert only one <CR> */
381 }
382 }
383
384 if (cmdchar == 'R')
385 {
386#ifdef FEAT_FKMAP
387 if (p_fkmap && p_ri)
388 {
389 beep_flush();
390 EMSG(farsi_text_3); /* encoded in Farsi */
391 State = INSERT;
392 }
393 else
394#endif
395 State = REPLACE;
396 }
397#ifdef FEAT_VREPLACE
398 else if (cmdchar == 'V' || cmdchar == 'v')
399 {
400 State = VREPLACE;
401 replaceState = VREPLACE;
402 orig_line_count = curbuf->b_ml.ml_line_count;
403 vr_lines_changed = 1;
404 }
405#endif
406 else
407 State = INSERT;
408
409 stop_insert_mode = FALSE;
410
411 /*
412 * Need to recompute the cursor position, it might move when the cursor is
413 * on a TAB or special character.
414 */
415 curs_columns(TRUE);
416
417 /*
418 * Enable langmap or IME, indicated by 'iminsert'.
419 * Note that IME may enabled/disabled without us noticing here, thus the
420 * 'iminsert' value may not reflect what is actually used. It is updated
421 * when hitting <Esc>.
422 */
423 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
424 State |= LANGMAP;
425#ifdef USE_IM_CONTROL
426 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
427#endif
428
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429#ifdef FEAT_MOUSE
430 setmouse();
431#endif
432#ifdef FEAT_CMDL_INFO
433 clear_showcmd();
434#endif
435#ifdef FEAT_RIGHTLEFT
436 /* there is no reverse replace mode */
437 revins_on = (State == INSERT && p_ri);
438 if (revins_on)
439 undisplay_dollar();
440 revins_chars = 0;
441 revins_legal = 0;
442 revins_scol = -1;
443#endif
444
445 /*
446 * Handle restarting Insert mode.
447 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
448 * restart_edit non-zero, and something in the stuff buffer.
449 */
450 if (restart_edit != 0 && stuff_empty())
451 {
452#ifdef FEAT_MOUSE
453 /*
454 * After a paste we consider text typed to be part of the insert for
455 * the pasted text. You can backspace over the pasted text too.
456 */
457 if (where_paste_started.lnum)
458 arrow_used = FALSE;
459 else
460#endif
461 arrow_used = TRUE;
462 restart_edit = 0;
463
464 /*
465 * If the cursor was after the end-of-line before the CTRL-O and it is
466 * now at the end-of-line, put it after the end-of-line (this is not
467 * correct in very rare cases).
468 * Also do this if curswant is greater than the current virtual
469 * column. Eg after "^O$" or "^O80|".
470 */
471 validate_virtcol();
472 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000473 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 || curwin->w_curswant > curwin->w_virtcol)
475 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
476 {
477 if (ptr[1] == NUL)
478 ++curwin->w_cursor.col;
479#ifdef FEAT_MBYTE
480 else if (has_mbyte)
481 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000482 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 if (ptr[i] == NUL)
484 curwin->w_cursor.col += i;
485 }
486#endif
487 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000488 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 }
490 else
491 arrow_used = FALSE;
492
493 /* we are in insert mode now, don't need to start it anymore */
494 need_start_insertmode = FALSE;
495
496 /* Need to save the line for undo before inserting the first char. */
497 ins_need_undo = TRUE;
498
499#ifdef FEAT_MOUSE
500 where_paste_started.lnum = 0;
501#endif
502#ifdef FEAT_CINDENT
503 can_cindent = TRUE;
504#endif
505#ifdef FEAT_FOLDING
506 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
507 * restarting. */
508 if (!p_im && did_restart_edit == 0)
509 foldOpenCursor();
510#endif
511
512 /*
513 * If 'showmode' is set, show the current (insert/replace/..) mode.
514 * A warning message for changing a readonly file is given here, before
515 * actually changing anything. It's put after the mode, if any.
516 */
517 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000518 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 i = showmode();
520
521 if (!p_im && did_restart_edit == 0)
522 change_warning(i + 1);
523
524#ifdef CURSOR_SHAPE
525 ui_cursor_shape(); /* may show different cursor shape */
526#endif
527#ifdef FEAT_DIGRAPHS
528 do_digraph(-1); /* clear digraphs */
529#endif
530
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000531 /*
532 * Get the current length of the redo buffer, those characters have to be
533 * skipped if we want to get to the inserted characters.
534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 ptr = get_inserted();
536 if (ptr == NULL)
537 new_insert_skip = 0;
538 else
539 {
540 new_insert_skip = (int)STRLEN(ptr);
541 vim_free(ptr);
542 }
543
544 old_indent = 0;
545
546 /*
547 * Main loop in Insert mode: repeat until Insert mode is left.
548 */
549 for (;;)
550 {
551#ifdef FEAT_RIGHTLEFT
552 if (!revins_legal)
553 revins_scol = -1; /* reset on illegal motions */
554 else
555 revins_legal = 0;
556#endif
557 if (arrow_used) /* don't repeat insert when arrow key used */
558 count = 0;
559
560 if (stop_insert_mode)
561 {
562 /* ":stopinsert" used or 'insertmode' reset */
563 count = 0;
564 goto doESCkey;
565 }
566
567 /* set curwin->w_curswant for next K_DOWN or K_UP */
568 if (!arrow_used)
569 curwin->w_set_curswant = TRUE;
570
571 /* If there is no typeahead may check for timestamps (e.g., for when a
572 * menu invoked a shell command). */
573 if (stuff_empty())
574 {
575 did_check_timestamps = FALSE;
576 if (need_check_timestamps)
577 check_timestamps(FALSE);
578 }
579
580 /*
581 * When emsg() was called msg_scroll will have been set.
582 */
583 msg_scroll = FALSE;
584
585#ifdef FEAT_GUI
586 /* When 'mousefocus' is set a mouse movement may have taken us to
587 * another window. "need_mouse_correct" may then be set because of an
588 * autocommand. */
589 if (need_mouse_correct)
590 gui_mouse_correct();
591#endif
592
593#ifdef FEAT_FOLDING
594 /* Open fold at the cursor line, according to 'foldopen'. */
595 if (fdo_flags & FDO_INSERT)
596 foldOpenCursor();
597 /* Close folds where the cursor isn't, according to 'foldclose' */
598 if (!char_avail())
599 foldCheckClose();
600#endif
601
602 /*
603 * If we inserted a character at the last position of the last line in
604 * the window, scroll the window one line up. This avoids an extra
605 * redraw.
606 * This is detected when the cursor column is smaller after inserting
607 * something.
608 * Don't do this when the topline changed already, it has
609 * already been adjusted (by insertchar() calling open_line())).
610 */
611 if (curbuf->b_mod_set
612 && curwin->w_p_wrap
613 && !did_backspace
614 && curwin->w_topline == old_topline
615#ifdef FEAT_DIFF
616 && curwin->w_topfill == old_topfill
617#endif
618 )
619 {
620 mincol = curwin->w_wcol;
621 validate_cursor_col();
622
623 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
624 && curwin->w_wrow == W_WINROW(curwin)
625 + curwin->w_height - 1 - p_so
626 && (curwin->w_cursor.lnum != curwin->w_topline
627#ifdef FEAT_DIFF
628 || curwin->w_topfill > 0
629#endif
630 ))
631 {
632#ifdef FEAT_DIFF
633 if (curwin->w_topfill > 0)
634 --curwin->w_topfill;
635 else
636#endif
637#ifdef FEAT_FOLDING
638 if (hasFolding(curwin->w_topline, NULL, &old_topline))
639 set_topline(curwin, old_topline + 1);
640 else
641#endif
642 set_topline(curwin, curwin->w_topline + 1);
643 }
644 }
645
646 /* May need to adjust w_topline to show the cursor. */
647 update_topline();
648
649 did_backspace = FALSE;
650
651 validate_cursor(); /* may set must_redraw */
652
653 /*
654 * Redraw the display when no characters are waiting.
655 * Also shows mode, ruler and positions cursor.
656 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000657 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658
659#ifdef FEAT_SCROLLBIND
660 if (curwin->w_p_scb)
661 do_check_scrollbind(TRUE);
662#endif
663
664 update_curswant();
665 old_topline = curwin->w_topline;
666#ifdef FEAT_DIFF
667 old_topfill = curwin->w_topfill;
668#endif
669
670#ifdef USE_ON_FLY_SCROLL
671 dont_scroll = FALSE; /* allow scrolling here */
672#endif
673
674 /*
675 * Get a character for Insert mode.
676 */
677 lastc = c; /* remember previous char for CTRL-D */
678 c = safe_vgetc();
679
680#ifdef FEAT_RIGHTLEFT
681 if (p_hkmap && KeyTyped)
682 c = hkmap(c); /* Hebrew mode mapping */
683#endif
684#ifdef FEAT_FKMAP
685 if (p_fkmap && KeyTyped)
686 c = fkmap(c); /* Farsi mode mapping */
687#endif
688
689#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000690 /*
691 * Special handling of keys while the popup menu is visible or wanted
692 * and the cursor is still in the completed word.
693 */
694 if (compl_started && pum_wanted() && curwin->w_cursor.col >= compl_col)
Bram Moolenaara6557602006-02-04 22:43:20 +0000695 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000696 /* BS: Delete one character from "compl_leader". */
697 if ((c == K_BS || c == Ctrl_H)
698 && curwin->w_cursor.col > compl_col && ins_compl_bs())
Bram Moolenaara6557602006-02-04 22:43:20 +0000699 continue;
700
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000701 /* When no match was selected or it was edited. */
702 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000703 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000704 /* CTRL-L: Add one character from the current match to
705 * "compl_leader". */
706 if (c == Ctrl_L)
707 {
708 ins_compl_addfrommatch();
709 continue;
710 }
711
712 /* A printable character: Add it to "compl_leader". */
713 if (vim_isprintc(c))
714 {
715 ins_compl_addleader(c);
716 continue;
717 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000718
719 /* Pressing Enter selects the current match. */
720 if (c == CAR || c == K_KENTER || c == NL)
721 {
722 ins_compl_delete();
723 ins_compl_insert();
724 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000725 }
726 }
727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
729 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000730 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000731 if (c != K_IGNORE && ins_compl_prep(c))
732 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733#endif
734
Bram Moolenaar488c6512005-08-11 20:09:58 +0000735 /* CTRL-\ CTRL-N goes to Normal mode,
736 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
737 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 if (c == Ctrl_BSL)
739 {
740 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000741 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 ++no_mapping;
743 ++allow_keys;
744 c = safe_vgetc();
745 --no_mapping;
746 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000747 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000749 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 vungetc(c);
751 c = Ctrl_BSL;
752 }
753 else if (c == Ctrl_G && p_im)
754 continue;
755 else
756 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000757 if (c == Ctrl_O)
758 {
759 ins_ctrl_o();
760 ins_at_eol = FALSE; /* cursor keeps its column */
761 nomove = TRUE;
762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 count = 0;
764 goto doESCkey;
765 }
766 }
767
768#ifdef FEAT_DIGRAPHS
769 c = do_digraph(c);
770#endif
771
772#ifdef FEAT_INS_EXPAND
773 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
774 goto docomplete;
775#endif
776 if (c == Ctrl_V || c == Ctrl_Q)
777 {
778 ins_ctrl_v();
779 c = Ctrl_V; /* pretend CTRL-V is last typed character */
780 continue;
781 }
782
783#ifdef FEAT_CINDENT
784 if (cindent_on()
785# ifdef FEAT_INS_EXPAND
786 && ctrl_x_mode == 0
787# endif
788 )
789 {
790 /* A key name preceded by a bang means this key is not to be
791 * inserted. Skip ahead to the re-indenting below.
792 * A key name preceded by a star means that indenting has to be
793 * done before inserting the key. */
794 line_is_white = inindent(0);
795 if (in_cinkeys(c, '!', line_is_white))
796 goto force_cindent;
797 if (can_cindent && in_cinkeys(c, '*', line_is_white)
798 && stop_arrow() == OK)
799 do_c_expr_indent();
800 }
801#endif
802
803#ifdef FEAT_RIGHTLEFT
804 if (curwin->w_p_rl)
805 switch (c)
806 {
807 case K_LEFT: c = K_RIGHT; break;
808 case K_S_LEFT: c = K_S_RIGHT; break;
809 case K_C_LEFT: c = K_C_RIGHT; break;
810 case K_RIGHT: c = K_LEFT; break;
811 case K_S_RIGHT: c = K_S_LEFT; break;
812 case K_C_RIGHT: c = K_C_LEFT; break;
813 }
814#endif
815
816#ifdef FEAT_VISUAL
817 /*
818 * If 'keymodel' contains "startsel", may start selection. If it
819 * does, a CTRL-O and c will be stuffed, we need to get these
820 * characters.
821 */
822 if (ins_start_select(c))
823 continue;
824#endif
825
826 /*
827 * The big switch to handle a character in insert mode.
828 */
829 switch (c)
830 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000831 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 if (echeck_abbr(ESC + ABBR_OFF))
833 break;
834 /*FALLTHROUGH*/
835
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000836 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837#ifdef FEAT_CMDWIN
838 if (c == Ctrl_C && cmdwin_type != 0)
839 {
840 /* Close the cmdline window. */
841 cmdwin_result = K_IGNORE;
842 got_int = FALSE; /* don't stop executing autocommands et al. */
843 goto doESCkey;
844 }
845#endif
846
847#ifdef UNIX
848do_intr:
849#endif
850 /* when 'insertmode' set, and not halfway a mapping, don't leave
851 * Insert mode */
852 if (goto_im())
853 {
854 if (got_int)
855 {
856 (void)vgetc(); /* flush all buffers */
857 got_int = FALSE;
858 }
859 else
860 vim_beep();
861 break;
862 }
863doESCkey:
864 /*
865 * This is the ONLY return from edit()!
866 */
867 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
868 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000869 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 o_lnum = curwin->w_cursor.lnum;
871
Bram Moolenaar488c6512005-08-11 20:09:58 +0000872 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000873 {
874#ifdef FEAT_AUTOCMD
875 if (cmdchar != 'r' && cmdchar != 'v')
876 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
877 FALSE, curbuf);
878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 continue;
882
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000883 case Ctrl_Z: /* suspend when 'insertmode' set */
884 if (!p_im)
885 goto normalchar; /* insert CTRL-Z as normal char */
886 stuffReadbuff((char_u *)":st\r");
887 c = Ctrl_O;
888 /*FALLTHROUGH*/
889
890 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000891#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000892 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000893 goto docomplete;
894#endif
895 if (echeck_abbr(Ctrl_O + ABBR_OFF))
896 break;
897 ins_ctrl_o();
898 count = 0;
899 goto doESCkey;
900
Bram Moolenaar572cb562005-08-05 21:35:02 +0000901 case K_INS: /* toggle insert/replace mode */
902 case K_KINS:
903 ins_insert(replaceState);
904 break;
905
906 case K_SELECT: /* end of Select mode mapping - ignore */
907 break;
908
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000909#ifdef FEAT_SNIFF
910 case K_SNIFF: /* Sniff command received */
911 stuffcharReadbuff(K_SNIFF);
912 goto doESCkey;
913#endif
914
915 case K_HELP: /* Help key works like <ESC> <Help> */
916 case K_F1:
917 case K_XF1:
918 stuffcharReadbuff(K_HELP);
919 if (p_im)
920 need_start_insertmode = TRUE;
921 goto doESCkey;
922
923#ifdef FEAT_NETBEANS_INTG
924 case K_F21: /* NetBeans command */
925 ++no_mapping; /* don't map the next key hits */
926 i = safe_vgetc();
927 --no_mapping;
928 netbeans_keycommand(i);
929 break;
930#endif
931
932 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 case NUL:
934 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000935 /* For ^@ the trailing ESC will end the insert, unless there is an
936 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
938 && c != Ctrl_A && !p_im)
939 goto doESCkey; /* quit insert mode */
940 inserted_space = FALSE;
941 break;
942
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000943 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 ins_reg();
945 auto_format(FALSE, TRUE);
946 inserted_space = FALSE;
947 break;
948
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000949 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 ins_ctrl_g();
951 break;
952
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000953 case Ctrl_HAT: /* switch input mode and/or langmap */
954 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 break;
956
957#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000958 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 if (!p_ari)
960 goto normalchar;
961 ins_ctrl_();
962 break;
963#endif
964
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000965 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
967 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
968 goto docomplete;
969#endif
970 /* FALLTHROUGH */
971
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000972 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973# ifdef FEAT_INS_EXPAND
974 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
975 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000976 if (has_compl_option(FALSE))
977 goto docomplete;
978 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980# endif
981 ins_shift(c, lastc);
982 auto_format(FALSE, TRUE);
983 inserted_space = FALSE;
984 break;
985
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000986 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 case K_KDEL:
988 ins_del();
989 auto_format(FALSE, TRUE);
990 break;
991
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000992 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 case Ctrl_H:
994 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
995 auto_format(FALSE, TRUE);
996 break;
997
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000998 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1000 auto_format(FALSE, TRUE);
1001 break;
1002
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001003 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001004# ifdef FEAT_COMPL_FUNC
1005 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001006 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001007 goto docomplete;
1008# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1010 auto_format(FALSE, TRUE);
1011 inserted_space = FALSE;
1012 break;
1013
1014#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001015 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 case K_LEFTMOUSE_NM:
1017 case K_LEFTDRAG:
1018 case K_LEFTRELEASE:
1019 case K_LEFTRELEASE_NM:
1020 case K_MIDDLEMOUSE:
1021 case K_MIDDLEDRAG:
1022 case K_MIDDLERELEASE:
1023 case K_RIGHTMOUSE:
1024 case K_RIGHTDRAG:
1025 case K_RIGHTRELEASE:
1026 case K_X1MOUSE:
1027 case K_X1DRAG:
1028 case K_X1RELEASE:
1029 case K_X2MOUSE:
1030 case K_X2DRAG:
1031 case K_X2RELEASE:
1032 ins_mouse(c);
1033 break;
1034
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001035 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 ins_mousescroll(FALSE);
1037 break;
1038
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001039 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 ins_mousescroll(TRUE);
1041 break;
1042#endif
1043
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001044 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 break;
1046
Bram Moolenaar754b5602006-02-09 23:53:20 +00001047#ifdef FEAT_AUTOCMD
1048 case K_CURSORHOLD: /* Didn't type something for a while. */
1049 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1050 did_cursorhold = TRUE;
1051 break;
1052#endif
1053
Bram Moolenaar4770d092006-01-12 23:22:24 +00001054#ifdef FEAT_GUI_W32
1055 /* On Win32 ignore <M-F4>, we get it when closing the window was
1056 * cancelled. */
1057 case K_F4:
1058 if (mod_mask != MOD_MASK_ALT)
1059 goto normalchar;
1060 break;
1061#endif
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063#ifdef FEAT_GUI
1064 case K_VER_SCROLLBAR:
1065 ins_scroll();
1066 break;
1067
1068 case K_HOR_SCROLLBAR:
1069 ins_horscroll();
1070 break;
1071#endif
1072
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001073 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 case K_S_HOME:
1076 case K_C_HOME:
1077 ins_home(c);
1078 break;
1079
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001080 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 case K_S_END:
1083 case K_C_END:
1084 ins_end(c);
1085 break;
1086
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001087 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001088 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1089 ins_s_left();
1090 else
1091 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 break;
1093
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001094 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 case K_C_LEFT:
1096 ins_s_left();
1097 break;
1098
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001099 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001100 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1101 ins_s_right();
1102 else
1103 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 break;
1105
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001106 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 case K_C_RIGHT:
1108 ins_s_right();
1109 break;
1110
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001111 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001112#ifdef FEAT_INS_EXPAND
1113 if (pum_visible())
1114 goto docomplete;
1115#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001116 if (mod_mask & MOD_MASK_SHIFT)
1117 ins_pageup();
1118 else
1119 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 break;
1121
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001122 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 case K_PAGEUP:
1124 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001125#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001126 if (pum_visible())
1127 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 ins_pageup();
1130 break;
1131
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001132 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001133#ifdef FEAT_INS_EXPAND
1134 if (pum_visible())
1135 goto docomplete;
1136#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001137 if (mod_mask & MOD_MASK_SHIFT)
1138 ins_pagedown();
1139 else
1140 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 break;
1142
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001143 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 case K_PAGEDOWN:
1145 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001146#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001147 if (pum_visible())
1148 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 ins_pagedown();
1151 break;
1152
1153#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001154 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 ins_drop();
1156 break;
1157#endif
1158
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001159 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 c = TAB;
1161 /* FALLTHROUGH */
1162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1165 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1166 goto docomplete;
1167#endif
1168 inserted_space = FALSE;
1169 if (ins_tab())
1170 goto normalchar; /* insert TAB as a normal char */
1171 auto_format(FALSE, TRUE);
1172 break;
1173
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 c = CAR;
1176 /* FALLTHROUGH */
1177 case CAR:
1178 case NL:
1179#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1180 /* In a quickfix window a <CR> jumps to the error under the
1181 * cursor. */
1182 if (bt_quickfix(curbuf) && c == CAR)
1183 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001184 if (curwin->w_llist_ref == NULL) /* quickfix window */
1185 do_cmdline_cmd((char_u *)".cc");
1186 else /* location list window */
1187 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 break;
1189 }
1190#endif
1191#ifdef FEAT_CMDWIN
1192 if (cmdwin_type != 0)
1193 {
1194 /* Execute the command in the cmdline window. */
1195 cmdwin_result = CAR;
1196 goto doESCkey;
1197 }
1198#endif
1199 if (ins_eol(c) && !p_im)
1200 goto doESCkey; /* out of memory */
1201 auto_format(FALSE, FALSE);
1202 inserted_space = FALSE;
1203 break;
1204
1205#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001206 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207# ifdef FEAT_INS_EXPAND
1208 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1209 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001210 if (has_compl_option(TRUE))
1211 goto docomplete;
1212 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 }
1214# endif
1215# ifdef FEAT_DIGRAPHS
1216 c = ins_digraph();
1217 if (c == NUL)
1218 break;
1219# endif
1220 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
1223#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001224 case Ctrl_X: /* Enter CTRL-X mode */
1225 ins_ctrl_x();
1226 break;
1227
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001228 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (ctrl_x_mode != CTRL_X_TAGS)
1230 goto normalchar;
1231 goto docomplete;
1232
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001233 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 if (ctrl_x_mode != CTRL_X_FILES)
1235 goto normalchar;
1236 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001237
1238 case 's': /* Spelling completion after ^X */
1239 case Ctrl_S:
1240 if (ctrl_x_mode != CTRL_X_SPELL)
1241 goto normalchar;
1242 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243#endif
1244
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001245 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246#ifdef FEAT_INS_EXPAND
1247 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1248#endif
1249 {
1250 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1251 if (p_im)
1252 {
1253 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1254 break;
1255 goto doESCkey;
1256 }
1257 goto normalchar;
1258 }
1259#ifdef FEAT_INS_EXPAND
1260 /* FALLTHROUGH */
1261
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001262 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 case Ctrl_N:
1264 /* if 'complete' is empty then plain ^P is no longer special,
1265 * but it is under other ^X modes */
1266 if (*curbuf->b_p_cpt == NUL
1267 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001268 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 goto normalchar;
1270
1271docomplete:
1272 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001273 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 break;
1275#endif /* FEAT_INS_EXPAND */
1276
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001277 case Ctrl_Y: /* copy from previous line or scroll down */
1278 case Ctrl_E: /* copy from next line or scroll up */
1279 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 break;
1281
1282 default:
1283#ifdef UNIX
1284 if (c == intr_char) /* special interrupt char */
1285 goto do_intr;
1286#endif
1287
1288 /*
1289 * Insert a nomal character.
1290 */
1291normalchar:
1292#ifdef FEAT_SMARTINDENT
1293 /* Try to perform smart-indenting. */
1294 ins_try_si(c);
1295#endif
1296
1297 if (c == ' ')
1298 {
1299 inserted_space = TRUE;
1300#ifdef FEAT_CINDENT
1301 if (inindent(0))
1302 can_cindent = FALSE;
1303#endif
1304 if (Insstart_blank_vcol == MAXCOL
1305 && curwin->w_cursor.lnum == Insstart.lnum)
1306 Insstart_blank_vcol = get_nolist_virtcol();
1307 }
1308
1309 if (vim_iswordc(c) || !echeck_abbr(
1310#ifdef FEAT_MBYTE
1311 /* Add ABBR_OFF for characters above 0x100, this is
1312 * what check_abbr() expects. */
1313 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1314#endif
1315 c))
1316 {
1317 insert_special(c, FALSE, FALSE);
1318#ifdef FEAT_RIGHTLEFT
1319 revins_legal++;
1320 revins_chars++;
1321#endif
1322 }
1323
1324 auto_format(FALSE, TRUE);
1325
1326#ifdef FEAT_FOLDING
1327 /* When inserting a character the cursor line must never be in a
1328 * closed fold. */
1329 foldOpenCursor();
1330#endif
1331 break;
1332 } /* end of switch (c) */
1333
1334 /* If the cursor was moved we didn't just insert a space */
1335 if (arrow_used)
1336 inserted_space = FALSE;
1337
1338#ifdef FEAT_CINDENT
1339 if (can_cindent && cindent_on()
1340# ifdef FEAT_INS_EXPAND
1341 && ctrl_x_mode == 0
1342# endif
1343 )
1344 {
1345force_cindent:
1346 /*
1347 * Indent now if a key was typed that is in 'cinkeys'.
1348 */
1349 if (in_cinkeys(c, ' ', line_is_white))
1350 {
1351 if (stop_arrow() == OK)
1352 /* re-indent the current line */
1353 do_c_expr_indent();
1354 }
1355 }
1356#endif /* FEAT_CINDENT */
1357
1358 } /* for (;;) */
1359 /* NOTREACHED */
1360}
1361
1362/*
1363 * Redraw for Insert mode.
1364 * This is postponed until getting the next character to make '$' in the 'cpo'
1365 * option work correctly.
1366 * Only redraw when there are no characters available. This speeds up
1367 * inserting sequences of characters (e.g., for CTRL-R).
1368 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001369/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001371ins_redraw(ready)
1372 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
1374 if (!char_avail())
1375 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001376#ifdef FEAT_AUTOCMD
1377 /* Trigger CursorMoved if the cursor moved. */
1378 if (ready && has_cursormovedI()
1379 && !equalpos(last_cursormoved, curwin->w_cursor))
1380 {
1381 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1382 last_cursormoved = curwin->w_cursor;
1383 }
1384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 if (must_redraw)
1386 update_screen(0);
1387 else if (clear_cmdline || redraw_cmdline)
1388 showmode(); /* clear cmdline and show mode */
1389 showruler(FALSE);
1390 setcursor();
1391 emsg_on_display = FALSE; /* may remove error message now */
1392 }
1393}
1394
1395/*
1396 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1397 */
1398 static void
1399ins_ctrl_v()
1400{
1401 int c;
1402
1403 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001404 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
1406 if (redrawing() && !char_avail())
1407 edit_putchar('^', TRUE);
1408 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1409
1410#ifdef FEAT_CMDL_INFO
1411 add_to_showcmd_c(Ctrl_V);
1412#endif
1413
1414 c = get_literal();
1415#ifdef FEAT_CMDL_INFO
1416 clear_showcmd();
1417#endif
1418 insert_special(c, FALSE, TRUE);
1419#ifdef FEAT_RIGHTLEFT
1420 revins_chars++;
1421 revins_legal++;
1422#endif
1423}
1424
1425/*
1426 * Put a character directly onto the screen. It's not stored in a buffer.
1427 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1428 */
1429static int pc_status;
1430#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1431#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1432#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1433#define PC_STATUS_SET 3 /* pc_bytes was filled */
1434#ifdef FEAT_MBYTE
1435static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1436#else
1437static char_u pc_bytes[2]; /* saved bytes */
1438#endif
1439static int pc_attr;
1440static int pc_row;
1441static int pc_col;
1442
1443 void
1444edit_putchar(c, highlight)
1445 int c;
1446 int highlight;
1447{
1448 int attr;
1449
1450 if (ScreenLines != NULL)
1451 {
1452 update_topline(); /* just in case w_topline isn't valid */
1453 validate_cursor();
1454 if (highlight)
1455 attr = hl_attr(HLF_8);
1456 else
1457 attr = 0;
1458 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1459 pc_col = W_WINCOL(curwin);
1460#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1461 pc_status = PC_STATUS_UNSET;
1462#endif
1463#ifdef FEAT_RIGHTLEFT
1464 if (curwin->w_p_rl)
1465 {
1466 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1467# ifdef FEAT_MBYTE
1468 if (has_mbyte)
1469 {
1470 int fix_col = mb_fix_col(pc_col, pc_row);
1471
1472 if (fix_col != pc_col)
1473 {
1474 screen_putchar(' ', pc_row, fix_col, attr);
1475 --curwin->w_wcol;
1476 pc_status = PC_STATUS_RIGHT;
1477 }
1478 }
1479# endif
1480 }
1481 else
1482#endif
1483 {
1484 pc_col += curwin->w_wcol;
1485#ifdef FEAT_MBYTE
1486 if (mb_lefthalve(pc_row, pc_col))
1487 pc_status = PC_STATUS_LEFT;
1488#endif
1489 }
1490
1491 /* save the character to be able to put it back */
1492#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1493 if (pc_status == PC_STATUS_UNSET)
1494#endif
1495 {
1496 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1497 pc_status = PC_STATUS_SET;
1498 }
1499 screen_putchar(c, pc_row, pc_col, attr);
1500 }
1501}
1502
1503/*
1504 * Undo the previous edit_putchar().
1505 */
1506 void
1507edit_unputchar()
1508{
1509 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1510 {
1511#if defined(FEAT_MBYTE)
1512 if (pc_status == PC_STATUS_RIGHT)
1513 ++curwin->w_wcol;
1514 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1515 redrawWinline(curwin->w_cursor.lnum, FALSE);
1516 else
1517#endif
1518 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1519 }
1520}
1521
1522/*
1523 * Called when p_dollar is set: display a '$' at the end of the changed text
1524 * Only works when cursor is in the line that changes.
1525 */
1526 void
1527display_dollar(col)
1528 colnr_T col;
1529{
1530 colnr_T save_col;
1531
1532 if (!redrawing())
1533 return;
1534
1535 cursor_off();
1536 save_col = curwin->w_cursor.col;
1537 curwin->w_cursor.col = col;
1538#ifdef FEAT_MBYTE
1539 if (has_mbyte)
1540 {
1541 char_u *p;
1542
1543 /* If on the last byte of a multi-byte move to the first byte. */
1544 p = ml_get_curline();
1545 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1546 }
1547#endif
1548 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1549 if (curwin->w_wcol < W_WIDTH(curwin))
1550 {
1551 edit_putchar('$', FALSE);
1552 dollar_vcol = curwin->w_virtcol;
1553 }
1554 curwin->w_cursor.col = save_col;
1555}
1556
1557/*
1558 * Call this function before moving the cursor from the normal insert position
1559 * in insert mode.
1560 */
1561 static void
1562undisplay_dollar()
1563{
1564 if (dollar_vcol)
1565 {
1566 dollar_vcol = 0;
1567 redrawWinline(curwin->w_cursor.lnum, FALSE);
1568 }
1569}
1570
1571/*
1572 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1573 * Keep the cursor on the same character.
1574 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1575 * type == INDENT_DEC decrease indent (for CTRL-D)
1576 * type == INDENT_SET set indent to "amount"
1577 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1578 */
1579 void
1580change_indent(type, amount, round, replaced)
1581 int type;
1582 int amount;
1583 int round;
1584 int replaced; /* replaced character, put on replace stack */
1585{
1586 int vcol;
1587 int last_vcol;
1588 int insstart_less; /* reduction for Insstart.col */
1589 int new_cursor_col;
1590 int i;
1591 char_u *ptr;
1592 int save_p_list;
1593 int start_col;
1594 colnr_T vc;
1595#ifdef FEAT_VREPLACE
1596 colnr_T orig_col = 0; /* init for GCC */
1597 char_u *new_line, *orig_line = NULL; /* init for GCC */
1598
1599 /* VREPLACE mode needs to know what the line was like before changing */
1600 if (State & VREPLACE_FLAG)
1601 {
1602 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1603 orig_col = curwin->w_cursor.col;
1604 }
1605#endif
1606
1607 /* for the following tricks we don't want list mode */
1608 save_p_list = curwin->w_p_list;
1609 curwin->w_p_list = FALSE;
1610 vc = getvcol_nolist(&curwin->w_cursor);
1611 vcol = vc;
1612
1613 /*
1614 * For Replace mode we need to fix the replace stack later, which is only
1615 * possible when the cursor is in the indent. Remember the number of
1616 * characters before the cursor if it's possible.
1617 */
1618 start_col = curwin->w_cursor.col;
1619
1620 /* determine offset from first non-blank */
1621 new_cursor_col = curwin->w_cursor.col;
1622 beginline(BL_WHITE);
1623 new_cursor_col -= curwin->w_cursor.col;
1624
1625 insstart_less = curwin->w_cursor.col;
1626
1627 /*
1628 * If the cursor is in the indent, compute how many screen columns the
1629 * cursor is to the left of the first non-blank.
1630 */
1631 if (new_cursor_col < 0)
1632 vcol = get_indent() - vcol;
1633
1634 if (new_cursor_col > 0) /* can't fix replace stack */
1635 start_col = -1;
1636
1637 /*
1638 * Set the new indent. The cursor will be put on the first non-blank.
1639 */
1640 if (type == INDENT_SET)
1641 (void)set_indent(amount, SIN_CHANGED);
1642 else
1643 {
1644#ifdef FEAT_VREPLACE
1645 int save_State = State;
1646
1647 /* Avoid being called recursively. */
1648 if (State & VREPLACE_FLAG)
1649 State = INSERT;
1650#endif
1651 shift_line(type == INDENT_DEC, round, 1);
1652#ifdef FEAT_VREPLACE
1653 State = save_State;
1654#endif
1655 }
1656 insstart_less -= curwin->w_cursor.col;
1657
1658 /*
1659 * Try to put cursor on same character.
1660 * If the cursor is at or after the first non-blank in the line,
1661 * compute the cursor column relative to the column of the first
1662 * non-blank character.
1663 * If we are not in insert mode, leave the cursor on the first non-blank.
1664 * If the cursor is before the first non-blank, position it relative
1665 * to the first non-blank, counted in screen columns.
1666 */
1667 if (new_cursor_col >= 0)
1668 {
1669 /*
1670 * When changing the indent while the cursor is touching it, reset
1671 * Insstart_col to 0.
1672 */
1673 if (new_cursor_col == 0)
1674 insstart_less = MAXCOL;
1675 new_cursor_col += curwin->w_cursor.col;
1676 }
1677 else if (!(State & INSERT))
1678 new_cursor_col = curwin->w_cursor.col;
1679 else
1680 {
1681 /*
1682 * Compute the screen column where the cursor should be.
1683 */
1684 vcol = get_indent() - vcol;
1685 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1686
1687 /*
1688 * Advance the cursor until we reach the right screen column.
1689 */
1690 vcol = last_vcol = 0;
1691 new_cursor_col = -1;
1692 ptr = ml_get_curline();
1693 while (vcol <= (int)curwin->w_virtcol)
1694 {
1695 last_vcol = vcol;
1696#ifdef FEAT_MBYTE
1697 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001698 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 else
1700#endif
1701 ++new_cursor_col;
1702 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1703 }
1704 vcol = last_vcol;
1705
1706 /*
1707 * May need to insert spaces to be able to position the cursor on
1708 * the right screen column.
1709 */
1710 if (vcol != (int)curwin->w_virtcol)
1711 {
1712 curwin->w_cursor.col = new_cursor_col;
1713 i = (int)curwin->w_virtcol - vcol;
1714 ptr = alloc(i + 1);
1715 if (ptr != NULL)
1716 {
1717 new_cursor_col += i;
1718 ptr[i] = NUL;
1719 while (--i >= 0)
1720 ptr[i] = ' ';
1721 ins_str(ptr);
1722 vim_free(ptr);
1723 }
1724 }
1725
1726 /*
1727 * When changing the indent while the cursor is in it, reset
1728 * Insstart_col to 0.
1729 */
1730 insstart_less = MAXCOL;
1731 }
1732
1733 curwin->w_p_list = save_p_list;
1734
1735 if (new_cursor_col <= 0)
1736 curwin->w_cursor.col = 0;
1737 else
1738 curwin->w_cursor.col = new_cursor_col;
1739 curwin->w_set_curswant = TRUE;
1740 changed_cline_bef_curs();
1741
1742 /*
1743 * May have to adjust the start of the insert.
1744 */
1745 if (State & INSERT)
1746 {
1747 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1748 {
1749 if ((int)Insstart.col <= insstart_less)
1750 Insstart.col = 0;
1751 else
1752 Insstart.col -= insstart_less;
1753 }
1754 if ((int)ai_col <= insstart_less)
1755 ai_col = 0;
1756 else
1757 ai_col -= insstart_less;
1758 }
1759
1760 /*
1761 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1762 * If the number of characters before the cursor decreased, need to pop a
1763 * few characters from the replace stack.
1764 * If the number of characters before the cursor increased, need to push a
1765 * few NULs onto the replace stack.
1766 */
1767 if (REPLACE_NORMAL(State) && start_col >= 0)
1768 {
1769 while (start_col > (int)curwin->w_cursor.col)
1770 {
1771 replace_join(0); /* remove a NUL from the replace stack */
1772 --start_col;
1773 }
1774 while (start_col < (int)curwin->w_cursor.col || replaced)
1775 {
1776 replace_push(NUL);
1777 if (replaced)
1778 {
1779 replace_push(replaced);
1780 replaced = NUL;
1781 }
1782 ++start_col;
1783 }
1784 }
1785
1786#ifdef FEAT_VREPLACE
1787 /*
1788 * For VREPLACE mode, we also have to fix the replace stack. In this case
1789 * it is always possible because we backspace over the whole line and then
1790 * put it back again the way we wanted it.
1791 */
1792 if (State & VREPLACE_FLAG)
1793 {
1794 /* If orig_line didn't allocate, just return. At least we did the job,
1795 * even if you can't backspace. */
1796 if (orig_line == NULL)
1797 return;
1798
1799 /* Save new line */
1800 new_line = vim_strsave(ml_get_curline());
1801 if (new_line == NULL)
1802 return;
1803
1804 /* We only put back the new line up to the cursor */
1805 new_line[curwin->w_cursor.col] = NUL;
1806
1807 /* Put back original line */
1808 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1809 curwin->w_cursor.col = orig_col;
1810
1811 /* Backspace from cursor to start of line */
1812 backspace_until_column(0);
1813
1814 /* Insert new stuff into line again */
1815 ins_bytes(new_line);
1816
1817 vim_free(new_line);
1818 }
1819#endif
1820}
1821
1822/*
1823 * Truncate the space at the end of a line. This is to be used only in an
1824 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1825 * modes.
1826 */
1827 void
1828truncate_spaces(line)
1829 char_u *line;
1830{
1831 int i;
1832
1833 /* find start of trailing white space */
1834 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1835 {
1836 if (State & REPLACE_FLAG)
1837 replace_join(0); /* remove a NUL from the replace stack */
1838 }
1839 line[i + 1] = NUL;
1840}
1841
1842#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1843 || defined(FEAT_COMMENTS) || defined(PROTO)
1844/*
1845 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1846 * modes correctly. May also be used when not in insert mode at all.
1847 */
1848 void
1849backspace_until_column(col)
1850 int col;
1851{
1852 while ((int)curwin->w_cursor.col > col)
1853 {
1854 curwin->w_cursor.col--;
1855 if (State & REPLACE_FLAG)
1856 replace_do_bs();
1857 else
1858 (void)del_char(FALSE);
1859 }
1860}
1861#endif
1862
1863#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1864/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001865 * CTRL-X pressed in Insert mode.
1866 */
1867 static void
1868ins_ctrl_x()
1869{
1870 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1871 * CTRL-V works like CTRL-N */
1872 if (ctrl_x_mode != CTRL_X_CMDLINE)
1873 {
1874 /* if the next ^X<> won't ADD nothing, then reset
1875 * compl_cont_status */
1876 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001877 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001878 else
1879 compl_cont_status = 0;
1880 /* We're not sure which CTRL-X mode it will be yet */
1881 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1882 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1883 edit_submode_pre = NULL;
1884 showmode();
1885 }
1886}
1887
1888/*
1889 * Return TRUE if the 'dict' or 'tsr' option can be used.
1890 */
1891 static int
1892has_compl_option(dict_opt)
1893 int dict_opt;
1894{
1895 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1896 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1897 {
1898 ctrl_x_mode = 0;
1899 edit_submode = NULL;
1900 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1901 : (char_u *)_("'thesaurus' option is empty"),
1902 hl_attr(HLF_E));
1903 if (emsg_silent == 0)
1904 {
1905 vim_beep();
1906 setcursor();
1907 out_flush();
1908 ui_delay(2000L, FALSE);
1909 }
1910 return FALSE;
1911 }
1912 return TRUE;
1913}
1914
1915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1917 * This depends on the current mode.
1918 */
1919 int
1920vim_is_ctrl_x_key(c)
1921 int c;
1922{
1923 /* Always allow ^R - let it's results then be checked */
1924 if (c == Ctrl_R)
1925 return TRUE;
1926
Bram Moolenaare3226be2005-12-18 22:10:00 +00001927 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001928 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001929 return TRUE;
1930
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 switch (ctrl_x_mode)
1932 {
1933 case 0: /* Not in any CTRL-X mode */
1934 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1935 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001936 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1938 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1939 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001940 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1941 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 case CTRL_X_SCROLL:
1943 return (c == Ctrl_Y || c == Ctrl_E);
1944 case CTRL_X_WHOLE_LINE:
1945 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1946 case CTRL_X_FILES:
1947 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1948 case CTRL_X_DICTIONARY:
1949 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1950 case CTRL_X_THESAURUS:
1951 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1952 case CTRL_X_TAGS:
1953 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1954#ifdef FEAT_FIND_ID
1955 case CTRL_X_PATH_PATTERNS:
1956 return (c == Ctrl_P || c == Ctrl_N);
1957 case CTRL_X_PATH_DEFINES:
1958 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1959#endif
1960 case CTRL_X_CMDLINE:
1961 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1962 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001963#ifdef FEAT_COMPL_FUNC
1964 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001965 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001966 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001967 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001968#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001969 case CTRL_X_SPELL:
1970 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 }
1972 EMSG(_(e_internal));
1973 return FALSE;
1974}
1975
1976/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001977 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 * case of the originally typed text is used, and the case of the completed
1979 * text is infered, ie this tries to work out what case you probably wanted
1980 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001981 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 */
1983 int
Bram Moolenaar572cb562005-08-05 21:35:02 +00001984ins_compl_add_infercase(str, len, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 char_u *str;
1986 int len;
1987 char_u *fname;
1988 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00001989 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
1991 int has_lower = FALSE;
1992 int was_letter = FALSE;
1993 int idx;
1994
1995 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
1996 {
1997 /* Infer case of completed part -- webb */
1998 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001999 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
2001 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002002 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002004 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 {
2006 has_lower = TRUE;
2007 if (isupper(IObuff[idx]))
2008 {
2009 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002010 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2012 break;
2013 }
2014 }
2015 }
2016
2017 /*
2018 * Rule 2: No lower case, 2nd consecutive letter converted to
2019 * upper case.
2020 */
2021 if (!has_lower)
2022 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002023 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002025 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 && islower(IObuff[idx]))
2027 {
2028 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002029 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2031 break;
2032 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002033 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 }
2035 }
2036
2037 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002038 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002040 return ins_compl_add(IObuff, len, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002042 return ins_compl_add(str, len, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043}
2044
2045/*
2046 * Add a match to the list of matches.
2047 * If the given string is already in the list of completions, then return
2048 * FAIL, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaar572cb562005-08-05 21:35:02 +00002049 * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb.
2050 *
2051 * New:
2052 * If the given string is already in the list of completions, then return
2053 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
2054 * maybe because alloc() returns NULL, then FAIL is returned -- webb.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002056 int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002057ins_compl_add(str, len, fname, extra, cdir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 char_u *str;
2059 int len;
2060 char_u *fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002061 char_u *extra; /* extra text for popup menu or NULL */
2062 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002063 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002065 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002066 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068 ui_breakcheck();
2069 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002070 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (len < 0)
2072 len = (int)STRLEN(str);
2073
2074 /*
2075 * If the same match is already present, don't add it.
2076 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002077 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002079 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 do
2081 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002082 if ( !(match->cp_flags & ORIGINAL_TEXT)
2083 && STRNCMP(match->cp_str, str, (size_t)len) == 0
2084 && match->cp_str[len] == NUL)
2085 return NOTDONE;
2086 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002087 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
2089
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002090 /* Remove any popup menu before changing the list of matches. */
2091 ins_compl_del_pum();
2092
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 /*
2094 * Allocate a new match structure.
2095 * Copy the values to the new match structure.
2096 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002097 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002099 return FAIL;
2100 match->cp_number = -1;
2101 if (flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002103 match->cp_number = 0;
2104 match->cp_str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002106 else if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 {
2108 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002109 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002111
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002113 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2115 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002116 if (fname != NULL
2117 && compl_curr_match
2118 && compl_curr_match->cp_fname != NULL
2119 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002120 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002121 else if (fname != NULL)
2122 {
2123 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002124 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002127 match->cp_fname = NULL;
2128 match->cp_flags = flags;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002129 if (extra != NULL)
2130 match->cp_extra = vim_strsave(extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131
2132 /*
2133 * Link the new match structure in the list of matches.
2134 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002135 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002136 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 else if (dir == FORWARD)
2138 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002139 match->cp_next = compl_curr_match->cp_next;
2140 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 }
2142 else /* BACKWARD */
2143 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002144 match->cp_next = compl_curr_match;
2145 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002147 if (match->cp_next)
2148 match->cp_next->cp_prev = match;
2149 if (match->cp_prev)
2150 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002152 compl_first_match = match;
2153 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002155 /*
2156 * Find the longest common string if still doing that.
2157 */
2158 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2159 ins_compl_longest_match(match);
2160
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 return OK;
2162}
2163
2164/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002165 * Reduce the longest common string for match "match".
2166 */
2167 static void
2168ins_compl_longest_match(match)
2169 compl_T *match;
2170{
2171 char_u *p, *s;
2172 int l;
2173 int had_match;
2174
2175 if (compl_leader == NULL)
2176 /* First match, use it as a whole. */
2177 compl_leader = vim_strsave(match->cp_str);
2178 else
2179 {
2180 /* Reduce the text if this match differs from compl_leader. */
2181 for (p = compl_leader, s = match->cp_str; *p != NUL; p += l, s += l)
2182 {
2183#ifdef FEAT_MBYTE
2184 if (has_mbyte)
2185 {
2186 l = mb_ptr2len(p);
2187 if (STRNCMP(p, s, l) != 0)
2188 break;
2189 }
2190 else
2191#endif
2192 {
2193 if (*p != *s)
2194 break;
2195 l = 1;
2196 }
2197 }
2198
2199 if (*p != NUL)
2200 {
2201 /* Leader was shortened, need to change the inserted text. */
2202 *p = NUL;
2203 had_match = (curwin->w_cursor.col > compl_col);
2204 ins_compl_delete();
2205 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2206 ins_redraw(FALSE);
2207
2208 /* When the match isn't there (to avoid matching itself) remove it
2209 * again after redrawing. */
2210 if (!had_match)
2211 ins_compl_delete();
2212 }
2213
2214 compl_used_match = FALSE;
2215 }
2216}
2217
2218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 * Add an array of matches to the list of matches.
2220 * Frees matches[].
2221 */
2222 static void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002223ins_compl_add_matches(num_matches, matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 int num_matches;
2225 char_u **matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226{
2227 int i;
2228 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002229 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230
Bram Moolenaar572cb562005-08-05 21:35:02 +00002231 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002232 if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, dir, 0)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002234 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 FreeWild(num_matches, matches);
2236}
2237
2238/* Make the completion list cyclic.
2239 * Return the number of matches (excluding the original).
2240 */
2241 static int
2242ins_compl_make_cyclic()
2243{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002244 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 int count = 0;
2246
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002247 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
2249 /*
2250 * Find the end of the list.
2251 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002252 match = compl_first_match;
2253 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002254 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002256 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 ++count;
2258 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002259 match->cp_next = compl_first_match;
2260 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
2262 return count;
2263}
2264
Bram Moolenaar9372a112005-12-06 19:59:18 +00002265/* "compl_match_array" points the currently displayed list of entries in the
2266 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002267static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002268static int compl_match_arraysize;
2269
2270/*
2271 * Update the screen and when there is any scrolling remove the popup menu.
2272 */
2273 static void
2274ins_compl_upd_pum()
2275{
2276 int h;
2277
2278 if (compl_match_array != NULL)
2279 {
2280 h = curwin->w_cline_height;
2281 update_screen(0);
2282 if (h != curwin->w_cline_height)
2283 ins_compl_del_pum();
2284 }
2285}
2286
2287/*
2288 * Remove any popup menu.
2289 */
2290 static void
2291ins_compl_del_pum()
2292{
2293 if (compl_match_array != NULL)
2294 {
2295 pum_undisplay();
2296 vim_free(compl_match_array);
2297 compl_match_array = NULL;
2298 }
2299}
2300
2301/*
2302 * Return TRUE if the popup menu should be displayed.
2303 */
2304 static int
2305pum_wanted()
2306{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002307 /* 'completeopt' must contain "menu" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002308 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002309 return FALSE;
2310
2311 /* The display looks bad on a B&W display. */
2312 if (t_colors < 8
2313#ifdef FEAT_GUI
2314 && !gui.in_use
2315#endif
2316 )
2317 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002318 return TRUE;
2319}
2320
2321/*
2322 * Return TRUE if there are two or more matches to be shown in the popup menu.
2323 */
2324 static int
2325pum_two_or_more()
2326{
2327 compl_T *compl;
2328 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002329
2330 /* Don't display the popup menu if there are no matches or there is only
2331 * one (ignoring the original text). */
2332 compl = compl_first_match;
2333 i = 0;
2334 do
2335 {
2336 if (compl == NULL
2337 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2338 break;
2339 compl = compl->cp_next;
2340 } while (compl != compl_first_match);
2341
2342 return (i >= 2);
2343}
2344
2345/*
2346 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002347 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002348 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002349 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002350ins_compl_show_pum()
2351{
2352 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002353 compl_T *shown_compl = NULL;
2354 int did_find_shown_match = FALSE;
2355 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002356 int i;
2357 int cur = -1;
2358 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002359 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002360
Bram Moolenaara6557602006-02-04 22:43:20 +00002361 if (!pum_wanted() || !pum_two_or_more())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002362 return;
2363
2364 /* Update the screen before drawing the popup menu over it. */
2365 update_screen(0);
2366
2367 if (compl_match_array == NULL)
2368 {
2369 /* Need to build the popup menu list. */
2370 compl_match_arraysize = 0;
2371 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002372 if (compl_leader != NULL)
2373 lead_len = STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002374 do
2375 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002376 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2377 && (compl_leader == NULL
2378 || STRNCMP(compl->cp_str, compl_leader, lead_len) == 0))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002379 ++compl_match_arraysize;
2380 compl = compl->cp_next;
2381 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002382 if (compl_match_arraysize == 0)
2383 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002384 compl_match_array = (pumitem_T *)alloc_clear(
2385 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002386 * compl_match_arraysize));
2387 if (compl_match_array != NULL)
2388 {
2389 i = 0;
2390 compl = compl_first_match;
2391 do
2392 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002393 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2394 && (compl_leader == NULL
2395 || STRNCMP(compl->cp_str, compl_leader,
2396 lead_len) == 0))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002397 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002398 if (!shown_match_ok)
2399 {
2400 if (compl == compl_shown_match || did_find_shown_match)
2401 {
2402 /* This item is the shown match or this is the
2403 * first displayed item after the shown match. */
2404 compl_shown_match = compl;
2405 did_find_shown_match = TRUE;
2406 shown_match_ok = TRUE;
2407 }
2408 else
2409 /* Remember this displayed match for when the
2410 * shown match is just below it. */
2411 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002412 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002413 }
2414 compl_match_array[i].pum_text = compl->cp_str;
2415 if (compl->cp_extra != NULL)
2416 compl_match_array[i++].pum_extra = compl->cp_extra;
2417 else
2418 compl_match_array[i++].pum_extra = compl->cp_fname;
2419 }
2420
2421 if (compl == compl_shown_match)
2422 {
2423 did_find_shown_match = TRUE;
2424 if (!shown_match_ok && shown_compl != NULL)
2425 {
2426 /* The shown match isn't displayed, set it to the
2427 * previously displayed match. */
2428 compl_shown_match = shown_compl;
2429 shown_match_ok = TRUE;
2430 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002431 }
2432 compl = compl->cp_next;
2433 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002434
2435 if (!shown_match_ok) /* no displayed match at all */
2436 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002437 }
2438 }
2439 else
2440 {
2441 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002442 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002443 if (compl_match_array[i].pum_text == compl_shown_match->cp_str)
Bram Moolenaara6557602006-02-04 22:43:20 +00002444 break;
2445 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002446 }
2447
2448 if (compl_match_array != NULL)
2449 {
2450 /* Compute the screen column of the start of the completed text.
2451 * Use the cursor to get all wrapping and other settings right. */
2452 col = curwin->w_cursor.col;
2453 curwin->w_cursor.col = compl_col;
2454 validate_cursor_col();
2455 pum_display(compl_match_array, compl_match_arraysize, cur,
2456 curwin->w_cline_row + W_WINROW(curwin),
2457 curwin->w_cline_height,
Bram Moolenaar280f1262006-01-30 00:14:18 +00002458 curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002459 curwin->w_cursor.col = col;
2460 }
2461}
2462
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463#define DICT_FIRST (1) /* use just first element in "dict" */
2464#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002465
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466/*
2467 * Add any identifiers that match the given pattern to the list of
2468 * completions.
2469 */
2470 static void
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002471ins_compl_dictionaries(dict, pat, flags, thesaurus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 char_u *dict;
2473 char_u *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 int flags;
2475 int thesaurus;
2476{
2477 char_u *ptr;
2478 char_u *buf;
2479 FILE *fp;
2480 regmatch_T regmatch;
2481 int add_r;
2482 char_u **files;
2483 int count;
2484 int i;
2485 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002486 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
2488 buf = alloc(LSIZE);
2489 /* If 'infercase' is set, don't use 'smartcase' here */
2490 save_p_scs = p_scs;
2491 if (curbuf->b_p_inf)
2492 p_scs = FALSE;
2493 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2494 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2495 regmatch.rm_ic = ignorecase(pat);
2496 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002497 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 {
2499 /* copy one dictionary file name into buf */
2500 if (flags == DICT_EXACT)
2501 {
2502 count = 1;
2503 files = &dict;
2504 }
2505 else
2506 {
2507 /* Expand wildcards in the dictionary name, but do not allow
2508 * backticks (for security, the 'dict' option may have been set in
2509 * a modeline). */
2510 copy_option_part(&dict, buf, LSIZE, ",");
2511 if (vim_strchr(buf, '`') != NULL
2512 || expand_wildcards(1, &buf, &count, &files,
2513 EW_FILE|EW_SILENT) != OK)
2514 count = 0;
2515 }
2516
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002517 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 {
2519 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2520 if (flags != DICT_EXACT)
2521 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002522 vim_snprintf((char *)IObuff, IOSIZE,
2523 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2525 }
2526
2527 if (fp != NULL)
2528 {
2529 /*
2530 * Read dictionary file line by line.
2531 * Check each line for a match.
2532 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002533 while (!got_int && !compl_interrupted
2534 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
2536 ptr = buf;
2537 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2538 {
2539 ptr = regmatch.startp[0];
2540 ptr = find_word_end(ptr);
2541 add_r = ins_compl_add_infercase(regmatch.startp[0],
2542 (int)(ptr - regmatch.startp[0]),
2543 files[i], dir, 0);
2544 if (thesaurus)
2545 {
2546 char_u *wstart;
2547
2548 /*
2549 * Add the other matches on the line
2550 */
2551 while (!got_int)
2552 {
2553 /* Find start of the next word. Skip white
2554 * space and punctuation. */
2555 ptr = find_word_start(ptr);
2556 if (*ptr == NUL || *ptr == NL)
2557 break;
2558 wstart = ptr;
2559
2560 /* Find end of the word and add it. */
2561#ifdef FEAT_MBYTE
2562 if (has_mbyte)
2563 /* Japanese words may have characters in
2564 * different classes, only separate words
2565 * with single-byte non-word characters. */
2566 while (*ptr != NUL)
2567 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002568 int l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
2570 if (l < 2 && !vim_iswordc(*ptr))
2571 break;
2572 ptr += l;
2573 }
2574 else
2575#endif
2576 ptr = find_word_end(ptr);
2577 add_r = ins_compl_add_infercase(wstart,
2578 (int)(ptr - wstart), files[i], dir, 0);
2579 }
2580 }
2581 if (add_r == OK)
2582 /* if dir was BACKWARD then honor it just once */
2583 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002584 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 break;
2586 /* avoid expensive call to vim_regexec() when at end
2587 * of line */
2588 if (*ptr == '\n' || got_int)
2589 break;
2590 }
2591 line_breakcheck();
Bram Moolenaar572cb562005-08-05 21:35:02 +00002592 ins_compl_check_keys(50);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
2594 fclose(fp);
2595 }
2596 }
2597 if (flags != DICT_EXACT)
2598 FreeWild(count, files);
2599 if (flags)
2600 break;
2601 }
2602 p_scs = save_p_scs;
2603 vim_free(regmatch.regprog);
2604 vim_free(buf);
2605}
2606
2607/*
2608 * Find the start of the next word.
2609 * Returns a pointer to the first char of the word. Also stops at a NUL.
2610 */
2611 char_u *
2612find_word_start(ptr)
2613 char_u *ptr;
2614{
2615#ifdef FEAT_MBYTE
2616 if (has_mbyte)
2617 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002618 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 else
2620#endif
2621 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2622 ++ptr;
2623 return ptr;
2624}
2625
2626/*
2627 * Find the end of the word. Assumes it starts inside a word.
2628 * Returns a pointer to just after the word.
2629 */
2630 char_u *
2631find_word_end(ptr)
2632 char_u *ptr;
2633{
2634#ifdef FEAT_MBYTE
2635 int start_class;
2636
2637 if (has_mbyte)
2638 {
2639 start_class = mb_get_class(ptr);
2640 if (start_class > 1)
2641 while (*ptr != NUL)
2642 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002643 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 if (mb_get_class(ptr) != start_class)
2645 break;
2646 }
2647 }
2648 else
2649#endif
2650 while (vim_iswordc(*ptr))
2651 ++ptr;
2652 return ptr;
2653}
2654
2655/*
2656 * Free the list of completions
2657 */
2658 static void
2659ins_compl_free()
2660{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002661 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002663 vim_free(compl_pattern);
2664 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002665 vim_free(compl_leader);
2666 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002668 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002670
2671 ins_compl_del_pum();
2672 pum_clear();
2673
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002674 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 do
2676 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002677 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002678 compl_curr_match = compl_curr_match->cp_next;
2679 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002681 if (match->cp_flags & FREE_FNAME)
2682 vim_free(match->cp_fname);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002683 vim_free(match->cp_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002685 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2686 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687}
2688
2689 static void
2690ins_compl_clear()
2691{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002692 compl_cont_status = 0;
2693 compl_started = FALSE;
2694 compl_matches = 0;
2695 vim_free(compl_pattern);
2696 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002697 vim_free(compl_leader);
2698 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 save_sm = -1;
2700 edit_submode_extra = NULL;
2701}
2702
2703/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002704 * Delete one character before the cursor and show the subset of the matches
2705 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002706 * Returns TRUE if the work is done and another char to be got from the user.
2707 */
2708 static int
2709ins_compl_bs()
2710{
2711 char_u *line;
2712 char_u *p;
2713
2714 if (curwin->w_cursor.col <= compl_col + compl_length)
2715 {
2716 /* Deleted more than what was used to find matches, need to look for
2717 * matches all over again. */
2718 ins_compl_free();
2719 compl_started = FALSE;
2720 compl_matches = 0;
2721 }
2722
2723 line = ml_get_curline();
2724 p = line + curwin->w_cursor.col;
2725 mb_ptr_back(line, p);
2726
2727 vim_free(compl_leader);
2728 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2729 if (compl_leader != NULL)
2730 {
2731 ins_compl_del_pum();
2732 ins_compl_delete();
2733 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2734
2735 if (!compl_started)
2736 {
2737 /* Matches were cleared, need to search for them now. */
2738 if (ins_complete(Ctrl_N) == FAIL)
2739 compl_cont_status = 0;
2740 else
2741 {
2742 /* Remove the completed word again. */
2743 ins_compl_delete();
2744 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2745 }
2746 }
2747
2748 /* Show the popup menu with a different set of matches. */
2749 ins_compl_show_pum();
2750 compl_used_match = FALSE;
2751
2752 return TRUE;
2753 }
2754 return FALSE;
2755}
2756
2757/*
2758 * Append one character to the match leader. May reduce the number of
2759 * matches.
2760 */
2761 static void
2762ins_compl_addleader(c)
2763 int c;
2764{
2765#ifdef FEAT_MBYTE
2766 int cc;
2767
2768 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2769 {
2770 char_u buf[MB_MAXBYTES + 1];
2771
2772 (*mb_char2bytes)(c, buf);
2773 buf[cc] = NUL;
2774 ins_char_bytes(buf, cc);
2775 }
2776 else
2777#endif
2778 ins_char(c);
2779
2780 vim_free(compl_leader);
2781 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
2782 curwin->w_cursor.col - compl_col);
2783 if (compl_leader != NULL)
2784 {
2785 /* Show the popup menu with a different set of matches. */
2786 ins_compl_del_pum();
2787 ins_compl_show_pum();
2788 compl_used_match = FALSE;
2789 }
2790}
2791
2792/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002793 * Append one character to the match leader. May reduce the number of
2794 * matches.
2795 */
2796 static void
2797ins_compl_addfrommatch()
2798{
2799 char_u *p;
2800 int len = curwin->w_cursor.col - compl_col;
2801 int c;
2802
2803 p = compl_shown_match->cp_str;
2804 if (STRLEN(p) <= len) /* the match is too short */
2805 return;
2806 p += len;
2807#ifdef FEAT_MBYTE
2808 c = mb_ptr2char(p);
2809#else
2810 c = *p;
2811#endif
2812 ins_compl_addleader(c);
2813}
2814
2815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002817 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002818 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002820 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821ins_compl_prep(c)
2822 int c;
2823{
2824 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 int temp;
2826 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002827 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828
2829 /* Forget any previous 'special' messages if this is actually
2830 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2831 */
2832 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2833 edit_submode_extra = NULL;
2834
2835 /* Ignore end of Select mode mapping */
2836 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002837 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002839 /* Set "compl_get_longest" when finding the first matches. */
2840 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
2841 || (ctrl_x_mode == 0 && !compl_started))
2842 {
2843 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
2844 compl_used_match = TRUE;
2845 }
2846
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2848 {
2849 /*
2850 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2851 * it will be yet. Now we decide.
2852 */
2853 switch (c)
2854 {
2855 case Ctrl_E:
2856 case Ctrl_Y:
2857 ctrl_x_mode = CTRL_X_SCROLL;
2858 if (!(State & REPLACE_FLAG))
2859 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2860 else
2861 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2862 edit_submode_pre = NULL;
2863 showmode();
2864 break;
2865 case Ctrl_L:
2866 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2867 break;
2868 case Ctrl_F:
2869 ctrl_x_mode = CTRL_X_FILES;
2870 break;
2871 case Ctrl_K:
2872 ctrl_x_mode = CTRL_X_DICTIONARY;
2873 break;
2874 case Ctrl_R:
2875 /* Simply allow ^R to happen without affecting ^X mode */
2876 break;
2877 case Ctrl_T:
2878 ctrl_x_mode = CTRL_X_THESAURUS;
2879 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002880#ifdef FEAT_COMPL_FUNC
2881 case Ctrl_U:
2882 ctrl_x_mode = CTRL_X_FUNCTION;
2883 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002884 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002885 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002886 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00002887#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002888 case 's':
2889 case Ctrl_S:
2890 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002891#ifdef FEAT_SYN_HL
2892 spell_back_to_badword();
2893#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002894 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 case Ctrl_RSB:
2896 ctrl_x_mode = CTRL_X_TAGS;
2897 break;
2898#ifdef FEAT_FIND_ID
2899 case Ctrl_I:
2900 case K_S_TAB:
2901 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2902 break;
2903 case Ctrl_D:
2904 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2905 break;
2906#endif
2907 case Ctrl_V:
2908 case Ctrl_Q:
2909 ctrl_x_mode = CTRL_X_CMDLINE;
2910 break;
2911 case Ctrl_P:
2912 case Ctrl_N:
2913 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
2914 * just started ^X mode, or there were enough ^X's to cancel
2915 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2916 * do normal expansion when interrupting a different mode (say
2917 * ^X^F^X^P or ^P^X^X^P, see below)
2918 * nothing changes if interrupting mode 0, (eg, the flag
2919 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002920 if (!(compl_cont_status & CONT_INTRPT))
2921 compl_cont_status |= CONT_LOCAL;
2922 else if (compl_cont_mode != 0)
2923 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 /* FALLTHROUGH */
2925 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002926 /* If we have typed at least 2 ^X's... for modes != 0, we set
2927 * compl_cont_status = 0 (eg, as if we had just started ^X
2928 * mode).
2929 * For mode 0, we set "compl_cont_mode" to an impossible
2930 * value, in both cases ^X^X can be used to restart the same
2931 * mode (avoiding ADDING mode).
2932 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2933 * 'complete' and local ^P expansions respectively.
2934 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2935 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 if (c == Ctrl_X)
2937 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002938 if (compl_cont_mode != 0)
2939 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002941 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 }
2943 ctrl_x_mode = 0;
2944 edit_submode = NULL;
2945 showmode();
2946 break;
2947 }
2948 }
2949 else if (ctrl_x_mode != 0)
2950 {
2951 /* We're already in CTRL-X mode, do we stay in it? */
2952 if (!vim_is_ctrl_x_key(c))
2953 {
2954 if (ctrl_x_mode == CTRL_X_SCROLL)
2955 ctrl_x_mode = 0;
2956 else
2957 ctrl_x_mode = CTRL_X_FINISHED;
2958 edit_submode = NULL;
2959 }
2960 showmode();
2961 }
2962
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002963 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 {
2965 /* Show error message from attempted keyword completion (probably
2966 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002967 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002969 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
2970 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 || ctrl_x_mode == CTRL_X_FINISHED)
2972 {
2973 /* Get here when we have finished typing a sequence of ^N and
2974 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002975 * memory that was used, and make sure we can redo the insert. */
2976 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002978 char_u *p;
2979
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 /*
2981 * If any of the original typed text has been changed,
2982 * eg when ignorecase is set, we must add back-spaces to
2983 * the redo buffer. We add as few as necessary to delete
2984 * just the part of the original text that has changed.
2985 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002986 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002987 p = compl_orig_text;
2988 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002990 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 ++ptr;
2992 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002993 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002995 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 }
2997
2998#ifdef FEAT_CINDENT
2999 want_cindent = (can_cindent && cindent_on());
3000#endif
3001 /*
3002 * When completing whole lines: fix indent for 'cindent'.
3003 * Otherwise, break line if it's too long.
3004 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003005 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 {
3007#ifdef FEAT_CINDENT
3008 /* re-indent the current line */
3009 if (want_cindent)
3010 {
3011 do_c_expr_indent();
3012 want_cindent = FALSE; /* don't do it again */
3013 }
3014#endif
3015 }
3016 else
3017 {
3018 /* put the cursor on the last char, for 'tw' formatting */
3019 curwin->w_cursor.col--;
3020 if (stop_arrow() == OK)
3021 insertchar(NUL, 0, -1);
3022 curwin->w_cursor.col++;
3023 }
3024
3025 auto_format(FALSE, TRUE);
3026
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003027 /* if the popup menu is displayed hitting Enter means accepting
3028 * the selection without inserting anything. */
3029 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
3030 retval = TRUE;
3031
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003033 compl_started = FALSE;
3034 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 msg_clr_cmdline(); /* necessary for "noshowmode" */
3036 ctrl_x_mode = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003037 if (save_sm >= 0)
3038 p_sm = save_sm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 if (edit_submode != NULL)
3040 {
3041 edit_submode = NULL;
3042 showmode();
3043 }
3044
3045#ifdef FEAT_CINDENT
3046 /*
3047 * Indent now if a key was typed that is in 'cinkeys'.
3048 */
3049 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3050 do_c_expr_indent();
3051#endif
3052 }
3053 }
3054
3055 /* reset continue_* if we left expansion-mode, if we stay they'll be
3056 * (re)set properly in ins_complete() */
3057 if (!vim_is_ctrl_x_key(c))
3058 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003059 compl_cont_status = 0;
3060 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003062
3063 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064}
3065
3066/*
3067 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3068 * (depending on flag) starting from buf and looking for a non-scanned
3069 * buffer (other than curbuf). curbuf is special, if it is called with
3070 * buf=curbuf then it has to be the first call for a given flag/expansion.
3071 *
3072 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3073 */
3074 static buf_T *
3075ins_compl_next_buf(buf, flag)
3076 buf_T *buf;
3077 int flag;
3078{
3079#ifdef FEAT_WINDOWS
3080 static win_T *wp;
3081#endif
3082
3083 if (flag == 'w') /* just windows */
3084 {
3085#ifdef FEAT_WINDOWS
3086 if (buf == curbuf) /* first call for this flag/expansion */
3087 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003088 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 && wp->w_buffer->b_scanned)
3090 ;
3091 buf = wp->w_buffer;
3092#else
3093 buf = curbuf;
3094#endif
3095 }
3096 else
3097 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3098 * (unlisted buffers)
3099 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003100 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 && ((flag == 'U'
3102 ? buf->b_p_bl
3103 : (!buf->b_p_bl
3104 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
3105 || buf->b_scanned
3106 || (buf->b_ml.ml_mfp == NULL
3107 && ctrl_x_mode == CTRL_X_WHOLE_LINE)))
3108 ;
3109 return buf;
3110}
3111
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003112#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003113static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003114
3115/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003116 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003117 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003118 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003119 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003120 static void
3121expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003122 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003123 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003124{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003125 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003126 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003127 listitem_T *li;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003128 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003129 char_u *funcname;
3130 pos_T pos;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003131 int dir = compl_direction;
3132 char_u *x;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003133
Bram Moolenaare344bea2005-09-01 20:46:49 +00003134 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3135 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003136 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003137
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003138 /* Call 'completefunc' to obtain the list of matches. */
3139 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003140 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003141
Bram Moolenaare344bea2005-09-01 20:46:49 +00003142 pos = curwin->w_cursor;
3143 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3144 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003145 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003146 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003147
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003148 /* Go through the List with matches and add each of them. */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003149 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003150 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003151 if (li->li_tv.v_type == VAR_DICT && li->li_tv.vval.v_dict != NULL)
3152 {
3153 p = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"word", FALSE);
3154 x = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"menu", FALSE);
3155 }
3156 else
3157 {
3158 p = get_tv_string_chk(&li->li_tv);
3159 x = NULL;
3160 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003161 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003162 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003163 if (ins_compl_add(p, -1, NULL, x, dir, 0) == OK)
3164 /* if dir was BACKWARD then honor it just once */
3165 dir = FORWARD;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003166 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003167 else if (did_emsg)
3168 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003169 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003170
3171 list_unref(matchlist);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003172}
3173#endif /* FEAT_COMPL_FUNC */
3174
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003175/*
3176 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003177 * The search starts at position "ini" in curbuf and in the direction
3178 * compl_direction.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003179 * When "compl_started" is FALSE start at that position, otherwise
3180 * continue where we stopped searching before.
3181 * This may return before finding all the matches.
3182 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 */
3184 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003185ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187{
3188 static pos_T first_match_pos;
3189 static pos_T last_match_pos;
3190 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003191 static int found_all = FALSE; /* Found all matches of a
3192 certain type. */
3193 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
Bram Moolenaar572cb562005-08-05 21:35:02 +00003195 pos_T *pos;
3196 char_u **matches;
3197 int save_p_scs;
3198 int save_p_ws;
3199 int save_p_ic;
3200 int i;
3201 int num_matches;
3202 int len;
3203 int found_new_match;
3204 int type = ctrl_x_mode;
3205 char_u *ptr;
3206 char_u *dict = NULL;
3207 int dict_f = 0;
3208 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003210 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
3212 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3213 ins_buf->b_scanned = 0;
3214 found_all = FALSE;
3215 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003216 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003217 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 last_match_pos = first_match_pos = *ini;
3219 }
3220
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003221 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003222 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3224 for (;;)
3225 {
3226 found_new_match = FAIL;
3227
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003228 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 * or if found_all says this entry is done. For ^X^L only use the
3230 * entries from 'complete' that look in loaded buffers. */
3231 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003232 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 {
3234 found_all = FALSE;
3235 while (*e_cpt == ',' || *e_cpt == ' ')
3236 e_cpt++;
3237 if (*e_cpt == '.' && !curbuf->b_scanned)
3238 {
3239 ins_buf = curbuf;
3240 first_match_pos = *ini;
3241 /* So that ^N can match word immediately after cursor */
3242 if (ctrl_x_mode == 0)
3243 dec(&first_match_pos);
3244 last_match_pos = first_match_pos;
3245 type = 0;
3246 }
3247 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3248 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3249 {
3250 /* Scan a buffer, but not the current one. */
3251 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3252 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003253 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 first_match_pos.col = last_match_pos.col = 0;
3255 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3256 last_match_pos.lnum = 0;
3257 type = 0;
3258 }
3259 else /* unloaded buffer, scan like dictionary */
3260 {
3261 found_all = TRUE;
3262 if (ins_buf->b_fname == NULL)
3263 continue;
3264 type = CTRL_X_DICTIONARY;
3265 dict = ins_buf->b_fname;
3266 dict_f = DICT_EXACT;
3267 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003268 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 ins_buf->b_fname == NULL
3270 ? buf_spname(ins_buf)
3271 : ins_buf->b_sfname == NULL
3272 ? (char *)ins_buf->b_fname
3273 : (char *)ins_buf->b_sfname);
3274 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3275 }
3276 else if (*e_cpt == NUL)
3277 break;
3278 else
3279 {
3280 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3281 type = -1;
3282 else if (*e_cpt == 'k' || *e_cpt == 's')
3283 {
3284 if (*e_cpt == 'k')
3285 type = CTRL_X_DICTIONARY;
3286 else
3287 type = CTRL_X_THESAURUS;
3288 if (*++e_cpt != ',' && *e_cpt != NUL)
3289 {
3290 dict = e_cpt;
3291 dict_f = DICT_FIRST;
3292 }
3293 }
3294#ifdef FEAT_FIND_ID
3295 else if (*e_cpt == 'i')
3296 type = CTRL_X_PATH_PATTERNS;
3297 else if (*e_cpt == 'd')
3298 type = CTRL_X_PATH_DEFINES;
3299#endif
3300 else if (*e_cpt == ']' || *e_cpt == 't')
3301 {
3302 type = CTRL_X_TAGS;
3303 sprintf((char*)IObuff, _("Scanning tags."));
3304 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3305 }
3306 else
3307 type = -1;
3308
3309 /* in any case e_cpt is advanced to the next entry */
3310 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3311
3312 found_all = TRUE;
3313 if (type == -1)
3314 continue;
3315 }
3316 }
3317
3318 switch (type)
3319 {
3320 case -1:
3321 break;
3322#ifdef FEAT_FIND_ID
3323 case CTRL_X_PATH_PATTERNS:
3324 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003325 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003326 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003328 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3330 (linenr_T)1, (linenr_T)MAXLNUM);
3331 break;
3332#endif
3333
3334 case CTRL_X_DICTIONARY:
3335 case CTRL_X_THESAURUS:
3336 ins_compl_dictionaries(
3337 dict ? dict
3338 : (type == CTRL_X_THESAURUS
3339 ? (*curbuf->b_p_tsr == NUL
3340 ? p_tsr
3341 : curbuf->b_p_tsr)
3342 : (*curbuf->b_p_dict == NUL
3343 ? p_dict
3344 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003345 compl_pattern,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
3347 dict = NULL;
3348 break;
3349
3350 case CTRL_X_TAGS:
3351 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3352 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003353 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
3355 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003356 * of matches is found when compl_pattern is empty */
3357 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3359 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3360 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3361 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003362 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 }
3364 p_ic = save_p_ic;
3365 break;
3366
3367 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003368 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3370 {
3371
3372 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003373 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003374 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
3376 break;
3377
3378 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003379 if (expand_cmdline(&compl_xp, compl_pattern,
3380 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003382 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 break;
3384
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003385#ifdef FEAT_COMPL_FUNC
3386 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003387 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003388 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003389 break;
3390#endif
3391
Bram Moolenaar488c6512005-08-11 20:09:58 +00003392 case CTRL_X_SPELL:
3393#ifdef FEAT_SYN_HL
3394 num_matches = expand_spelling(first_match_pos.lnum,
3395 first_match_pos.col, compl_pattern, &matches);
3396 if (num_matches > 0)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003397 ins_compl_add_matches(num_matches, matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003398#endif
3399 break;
3400
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 default: /* normal ^P/^N and ^X^L */
3402 /*
3403 * If 'infercase' is set, don't use 'smartcase' here
3404 */
3405 save_p_scs = p_scs;
3406 if (ins_buf->b_p_inf)
3407 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003408
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 /* buffers other than curbuf are scanned from the beginning or the
3410 * end but never from the middle, thus setting nowrapscan in this
3411 * buffers is a good idea, on the other hand, we always set
3412 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3413 save_p_ws = p_ws;
3414 if (ins_buf != curbuf)
3415 p_ws = FALSE;
3416 else if (*e_cpt == '.')
3417 p_ws = TRUE;
3418 for (;;)
3419 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003420 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003422 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3423 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003425 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003427 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003429 found_new_match = searchit(NULL, ins_buf, pos,
3430 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003431 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 RE_LAST);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003433 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003435 /* set compl_started even on fail */
3436 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 first_match_pos = *pos;
3438 last_match_pos = *pos;
3439 }
3440 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003441 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 found_new_match = FAIL;
3443 if (found_new_match == FAIL)
3444 {
3445 if (ins_buf == curbuf)
3446 found_all = TRUE;
3447 break;
3448 }
3449
3450 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003451 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 && ini->lnum == pos->lnum
3453 && ini->col == pos->col)
3454 continue;
3455 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3456 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3457 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003458 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
3460 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3461 continue;
3462 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3463 if (!p_paste)
3464 ptr = skipwhite(ptr);
3465 }
3466 len = (int)STRLEN(ptr);
3467 }
3468 else
3469 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003470 char_u *tmp_ptr = ptr;
3471
3472 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003474 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 /* Skip if already inside a word. */
3476 if (vim_iswordp(tmp_ptr))
3477 continue;
3478 /* Find start of next word. */
3479 tmp_ptr = find_word_start(tmp_ptr);
3480 }
3481 /* Find end of this word. */
3482 tmp_ptr = find_word_end(tmp_ptr);
3483 len = (int)(tmp_ptr - ptr);
3484
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003485 if ((compl_cont_status & CONT_ADDING)
3486 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
3488 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3489 {
3490 /* Try next line, if any. the new word will be
3491 * "join" as if the normal command "J" was used.
3492 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003493 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 * works -- Acevedo */
3495 STRNCPY(IObuff, ptr, len);
3496 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3497 tmp_ptr = ptr = skipwhite(ptr);
3498 /* Find start of next word. */
3499 tmp_ptr = find_word_start(tmp_ptr);
3500 /* Find end of next word. */
3501 tmp_ptr = find_word_end(tmp_ptr);
3502 if (tmp_ptr > ptr)
3503 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003504 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003506 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 IObuff[len++] = ' ';
3508 /* IObuf =~ "\k.* ", thus len >= 2 */
3509 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003510 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 || (vim_strchr(p_cpo, CPO_JOINSP)
3512 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003513 && (IObuff[len - 2] == '?'
3514 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 IObuff[len++] = ' ';
3516 }
3517 /* copy as much as posible of the new word */
3518 if (tmp_ptr - ptr >= IOSIZE - len)
3519 tmp_ptr = ptr + IOSIZE - len - 1;
3520 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3521 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003522 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
3524 IObuff[len] = NUL;
3525 ptr = IObuff;
3526 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003527 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 continue;
3529 }
3530 }
3531 if (ins_compl_add_infercase(ptr, len,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003532 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003533 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
3535 found_new_match = OK;
3536 break;
3537 }
3538 }
3539 p_scs = save_p_scs;
3540 p_ws = save_p_ws;
3541 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003542
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003543 /* check if compl_curr_match has changed, (e.g. other type of
3544 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003545 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 found_new_match = OK;
3547
3548 /* break the loop for specialized modes (use 'complete' just for the
3549 * generic ctrl_x_mode == 0) or when we've found a new match */
3550 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003551 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003552 {
3553 if (got_int)
3554 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003555 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003556 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003557 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003558
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003559 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3560 || compl_interrupted)
3561 break;
3562 compl_started = TRUE;
3563 }
3564 else
3565 {
3566 /* Mark a buffer scanned when it has been scanned completely */
3567 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3568 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003570 compl_started = FALSE;
3571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003573 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
3575 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3576 && *e_cpt == NUL) /* Got to end of 'complete' */
3577 found_new_match = FAIL;
3578
3579 i = -1; /* total of matches, unknown */
3580 if (found_new_match == FAIL
3581 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3582 i = ins_compl_make_cyclic();
3583
3584 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003585 * just been made cyclic then we have to move compl_curr_match to the next
3586 * or previous entry (if any) -- Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003587 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003588 if (compl_curr_match == NULL)
3589 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 return i;
3591}
3592
3593/* Delete the old text being completed. */
3594 static void
3595ins_compl_delete()
3596{
3597 int i;
3598
3599 /*
3600 * In insert mode: Delete the typed part.
3601 * In replace mode: Put the old characters back, if any.
3602 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003603 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 backspace_until_column(i);
3605 changed_cline_bef_curs();
3606}
3607
3608/* Insert the new text being completed. */
3609 static void
3610ins_compl_insert()
3611{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003612 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003613 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614}
3615
3616/*
3617 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003618 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3619 * get more completions. If it is FALSE, then we just do nothing when there
3620 * are no more completions in a given direction. The latter case is used when
3621 * we are still in the middle of finding completions, to allow browsing
3622 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 * Return the total number of matches, or -1 if still unknown -- webb.
3624 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003625 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3626 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 *
3628 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003629 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3630 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 */
3632 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003633ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003635 int count; /* repeat completion this many times; should
3636 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003637 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638{
3639 int num_matches = -1;
3640 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003641 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003642 compl_T *found_compl = NULL;
3643 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003645 if (compl_leader != NULL
3646 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003648 /* Set "compl_shown_match" to the actually shown match, it may differ
3649 * when "compl_leader" is used to omit some of the matches. */
3650 while (STRNCMP(compl_shown_match->cp_str,
3651 compl_leader, STRLEN(compl_leader)) != 0
3652 && compl_shown_match->cp_next != NULL
3653 && compl_shown_match->cp_next != compl_first_match)
3654 compl_shown_match = compl_shown_match->cp_next;
3655 }
3656
3657 if (allow_get_expansion && insert_match
3658 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 /* Delete old text to be replaced */
3660 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003661
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003662 compl_pending = FALSE;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003663
3664 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3665 * around. */
3666 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003668 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003670 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003671 found_end = (compl_first_match != NULL
3672 && (compl_shown_match->cp_next == compl_first_match
3673 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003674 }
3675 else if (compl_shows_dir == BACKWARD
3676 && compl_shown_match->cp_prev != NULL)
3677 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003678 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003679 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003680 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 }
3682 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003683 {
3684 compl_pending = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003685 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003686 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003687
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003688 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara6557602006-02-04 22:43:20 +00003689 if (compl_pending && compl_direction == compl_shows_dir)
3690 compl_shown_match = compl_curr_match;
3691 found_end = FALSE;
3692 }
3693 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
3694 && compl_leader != NULL
3695 && STRNCMP(compl_shown_match->cp_str,
3696 compl_leader, STRLEN(compl_leader)) != 0)
3697 ++todo;
3698 else
3699 /* Remember a matching item. */
3700 found_compl = compl_shown_match;
3701
3702 /* Stop at the end of the list when we found a usable match. */
3703 if (found_end)
3704 {
3705 if (found_compl != NULL)
3706 {
3707 compl_shown_match = found_compl;
3708 break;
3709 }
3710 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 }
3713
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003714 /* Insert the text of the new completion, or the compl_leader. */
3715 if (insert_match)
3716 {
3717 if (!compl_get_longest || compl_used_match)
3718 ins_compl_insert();
3719 else
3720 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3721 }
3722 else
3723 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
3725 if (!allow_get_expansion)
3726 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003727 /* may undisplay the popup menu first */
3728 ins_compl_upd_pum();
3729
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003730 /* redraw to show the user what was inserted */
3731 update_screen(0);
3732
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003733 /* display the updated popup menu */
3734 ins_compl_show_pum();
3735
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 /* Delete old text to be replaced, since we're still searching and
3737 * don't want to match ourselves! */
3738 ins_compl_delete();
3739 }
3740
3741 /*
3742 * Show the file name for the match (if any)
3743 * Truncate the file name to avoid a wait for return.
3744 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003745 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
3747 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003748 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 if (i <= 0)
3750 i = 0;
3751 else
3752 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003753 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 msg(IObuff);
3755 redraw_cmdline = FALSE; /* don't overwrite! */
3756 }
3757
3758 return num_matches;
3759}
3760
3761/*
3762 * Call this while finding completions, to check whether the user has hit a key
3763 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003764 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003766 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 */
3768 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003769ins_compl_check_keys(frequency)
3770 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771{
3772 static int count = 0;
3773
3774 int c;
3775
3776 /* Don't check when reading keys from a script. That would break the test
3777 * scripts */
3778 if (using_script())
3779 return;
3780
3781 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003782 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 return;
3784 count = 0;
3785
3786 ++no_mapping;
3787 c = vpeekc_any();
3788 --no_mapping;
3789 if (c != NUL)
3790 {
3791 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3792 {
3793 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003794 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003795 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
3796 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
3798 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003799 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003801 if (compl_pending && !got_int)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003802 (void)ins_compl_next(FALSE, 1, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003803}
3804
3805/*
3806 * Decide the direction of Insert mode complete from the key typed.
3807 * Returns BACKWARD or FORWARD.
3808 */
3809 static int
3810ins_compl_key2dir(c)
3811 int c;
3812{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003813 if (c == Ctrl_P || c == Ctrl_L
3814 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
3815 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00003816 return BACKWARD;
3817 return FORWARD;
3818}
3819
3820/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003821 * Return TRUE for keys that are used for completion only when the popup menu
3822 * is visible.
3823 */
3824 static int
3825ins_compl_pum_key(c)
3826 int c;
3827{
3828 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003829 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
3830 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003831}
3832
3833/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00003834 * Decide the number of completions to move forward.
3835 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
3836 */
3837 static int
3838ins_compl_key2count(c)
3839 int c;
3840{
3841 int h;
3842
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003843 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003844 {
3845 h = pum_get_height();
3846 if (h > 3)
3847 h -= 2; /* keep some context */
3848 return h;
3849 }
3850 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851}
3852
3853/*
3854 * Do Insert mode completion.
3855 * Called when character "c" was typed, which has a meaning for completion.
3856 * Returns OK if completion was done, FAIL if something failed (out of mem).
3857 */
3858 static int
3859ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003860 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003862 char_u *line;
3863 int startcol = 0; /* column where searched text starts */
3864 colnr_T curs_col; /* cursor column */
3865 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866
Bram Moolenaare3226be2005-12-18 22:10:00 +00003867 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003868 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
3870 /* First time we hit ^N or ^P (in a row, I mean) */
3871
3872 /* Turn off 'sm' so we don't show matches with ^X^L */
3873 save_sm = p_sm;
3874 p_sm = FALSE;
3875
3876 did_ai = FALSE;
3877#ifdef FEAT_SMARTINDENT
3878 did_si = FALSE;
3879 can_si = FALSE;
3880 can_si_back = FALSE;
3881#endif
3882 if (stop_arrow() == FAIL)
3883 return FAIL;
3884
3885 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003886 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
3888 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003889 * "compl_startpos" to the cursor as a pattern to add a new word
3890 * instead of expand the one before the cursor, in word-wise if
3891 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 * is not in the same line as the cursor then fix it (the line has
3893 * been split because it was longer than 'tw'). if SOL is set then
3894 * skip the previous pattern, a word at the beginning of the line has
3895 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003896 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
3897 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 {
3899 /*
3900 * it is a continued search
3901 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003902 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3904 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3905 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003906 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003908 /* line (probably) wrapped, set compl_startpos to the
3909 * first non_blank in the line, if it is not a wordchar
3910 * include it to get a better pattern, but then we don't
3911 * want the "\\<" prefix, check it bellow */
3912 compl_col = (colnr_T)(skipwhite(line) - line);
3913 compl_startpos.col = compl_col;
3914 compl_startpos.lnum = curwin->w_cursor.lnum;
3915 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 }
3917 else
3918 {
3919 /* S_IPOS was set when we inserted a word that was at the
3920 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 * mode but first we need to redefine compl_startpos */
3922 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003924 compl_cont_status |= CONT_SOL;
3925 compl_startpos.col = (colnr_T)(skipwhite(
3926 line + compl_length
3927 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003929 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003931 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003932 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 * have enough space? just being paranoic */
3934#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003935 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003937 compl_cont_status &= ~CONT_SOL;
3938 compl_length = (IOSIZE - MIN_SPACE);
3939 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003941 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3942 if (compl_length < 1)
3943 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
3945 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003946 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003948 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003951 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003953 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003955 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003957 compl_cont_status = 0;
3958 compl_cont_status |= CONT_N_ADDS;
3959 compl_startpos = curwin->w_cursor;
3960 startcol = (int)curs_col;
3961 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
3963
3964 /* Work out completion pattern and original text -- webb */
3965 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3966 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003967 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3969 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003970 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003972 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003974 compl_col += ++startcol;
3975 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 }
3977 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003978 compl_pattern = str_foldcase(line + compl_col,
3979 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003981 compl_pattern = vim_strnsave(line + compl_col,
3982 compl_length);
3983 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 return FAIL;
3985 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003986 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 {
3988 char_u *prefix = (char_u *)"\\<";
3989
3990 /* we need 3 extra chars, 1 for the NUL and
3991 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003992 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3993 compl_length) + 3);
3994 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003996 if (!vim_iswordp(line + compl_col)
3997 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 && (
3999#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004000 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004002 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003#endif
4004 )))
4005 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004006 STRCPY((char *)compl_pattern, prefix);
4007 (void)quote_meta(compl_pattern + STRLEN(prefix),
4008 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004010 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004012 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004014 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015#endif
4016 )
4017 {
4018 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004019 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4020 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004022 compl_col += curs_col;
4023 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 }
4025 else
4026 {
4027#ifdef FEAT_MBYTE
4028 /* Search the point of change class of multibyte character
4029 * or not a word single byte character backward. */
4030 if (has_mbyte)
4031 {
4032 int base_class;
4033 int head_off;
4034
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004035 startcol -= (*mb_head_off)(line, line + startcol);
4036 base_class = mb_get_class(line + startcol);
4037 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004039 head_off = (*mb_head_off)(line, line + startcol);
4040 if (base_class != mb_get_class(line + startcol
4041 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004043 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
4045 }
4046 else
4047#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004048 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004050 compl_col += ++startcol;
4051 compl_length = (int)curs_col - startcol;
4052 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
4054 /* Only match word with at least two chars -- webb
4055 * there's no need to call quote_meta,
4056 * alloc(7) is enough -- Acevedo
4057 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058 compl_pattern = alloc(7);
4059 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004061 STRCPY((char *)compl_pattern, "\\<");
4062 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4063 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
4065 else
4066 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004067 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4068 compl_length) + 3);
4069 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004071 STRCPY((char *)compl_pattern, "\\<");
4072 (void)quote_meta(compl_pattern + 2, line + compl_col,
4073 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
4075 }
4076 }
4077 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4078 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004079 compl_col = skipwhite(line) - line;
4080 compl_length = (int)curs_col - (int)compl_col;
4081 if (compl_length < 0) /* cursor in indent: empty pattern */
4082 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004084 compl_pattern = str_foldcase(line + compl_col, compl_length,
4085 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004087 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4088 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 return FAIL;
4090 }
4091 else if (ctrl_x_mode == CTRL_X_FILES)
4092 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004093 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004095 compl_col += ++startcol;
4096 compl_length = (int)curs_col - startcol;
4097 compl_pattern = addstar(line + compl_col, compl_length,
4098 EXPAND_FILES);
4099 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 return FAIL;
4101 }
4102 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4103 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004104 compl_pattern = vim_strnsave(line, curs_col);
4105 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004107 set_cmd_context(&compl_xp, compl_pattern,
4108 (int)STRLEN(compl_pattern), curs_col);
4109 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4110 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004112 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4113 compl_col = startcol;
4114 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004116 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004117 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004118#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004119 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004120 * Call user defined function 'completefunc' with "a:findstart"
4121 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004122 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004123 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004124 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004125 char_u *funcname;
4126 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004127
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004128 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004129 * string */
4130 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4131 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4132 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004133 {
4134 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4135 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004136 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004137 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004138
4139 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004140 args[1] = NULL;
4141 pos = curwin->w_cursor;
4142 col = call_func_retnr(funcname, 2, args, FALSE);
4143 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004144
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004145 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004146 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004147 compl_col = col;
4148 if ((colnr_T)compl_col > curs_col)
4149 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004151 /* Setup variables for completion. Need to obtain "line" again,
4152 * it may have become invalid. */
4153 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004154 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004155 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4156 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004157#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004158 return FAIL;
4159 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004160 else if (ctrl_x_mode == CTRL_X_SPELL)
4161 {
4162#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004163 if (spell_bad_len > 0)
4164 compl_col = curs_col - spell_bad_len;
4165 else
4166 compl_col = spell_word_start(startcol);
4167 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004168 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004169 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004170 compl_length = (int)curs_col - compl_col;
4171 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4172 if (compl_pattern == NULL)
4173#endif
4174 return FAIL;
4175 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004176 else
4177 {
4178 EMSG2(_(e_intern2), "ins_complete()");
4179 return FAIL;
4180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004182 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 {
4184 edit_submode_pre = (char_u *)_(" Adding");
4185 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4186 {
4187 /* Insert a new line, keep indentation but ignore 'comments' */
4188#ifdef FEAT_COMMENTS
4189 char_u *old = curbuf->b_p_com;
4190
4191 curbuf->b_p_com = (char_u *)"";
4192#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004193 compl_startpos.lnum = curwin->w_cursor.lnum;
4194 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 ins_eol('\r');
4196#ifdef FEAT_COMMENTS
4197 curbuf->b_p_com = old;
4198#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004199 compl_length = 0;
4200 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202 }
4203 else
4204 {
4205 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004206 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
4208
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004209 if (compl_cont_status & CONT_LOCAL)
4210 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 else
4212 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4213
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004215 * "compl_orig_text" itself (not a copy) is added, it will be freed
4216 * when the list of matches is freed. */
4217 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4218 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004219 -1, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004221 vim_free(compl_pattern);
4222 compl_pattern = NULL;
4223 vim_free(compl_orig_text);
4224 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 return FAIL;
4226 }
4227
4228 /* showmode might reset the internal line pointers, so it must
4229 * be called before line = ml_get(), or when this address is no
4230 * longer needed. -- Acevedo.
4231 */
4232 edit_submode_extra = (char_u *)_("-- Searching...");
4233 edit_submode_highl = HLF_COUNT;
4234 showmode();
4235 edit_submode_extra = NULL;
4236 out_flush();
4237 }
4238
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004239 compl_shown_match = compl_curr_match;
4240 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
4242 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004243 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004245 n = ins_compl_next(TRUE, ins_compl_key2count(c),
4246 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004248 /* may undisplay the popup menu */
4249 ins_compl_upd_pum();
4250
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251 if (n > 1) /* all matches have been found */
4252 compl_matches = n;
4253 compl_curr_match = compl_shown_match;
4254 compl_direction = compl_shows_dir;
4255 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 /* eat the ESC to avoid leaving insert mode */
4258 if (got_int && !global_busy)
4259 {
4260 (void)vgetc();
4261 got_int = FALSE;
4262 }
4263
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004264 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004265 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004267 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4268 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4270 edit_submode_highl = HLF_E;
4271 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4272 * because we couldn't expand anything at first place, but if we used
4273 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4274 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004275 if ( compl_length > 1
4276 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 || (ctrl_x_mode != 0
4278 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4279 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004280 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282
Bram Moolenaar572cb562005-08-05 21:35:02 +00004283 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004284 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004286 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
4288 if (edit_submode_extra == NULL)
4289 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004290 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 {
4292 edit_submode_extra = (char_u *)_("Back at original");
4293 edit_submode_highl = HLF_W;
4294 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004295 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 {
4297 edit_submode_extra = (char_u *)_("Word from other line");
4298 edit_submode_highl = HLF_COUNT;
4299 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004300 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 {
4302 edit_submode_extra = (char_u *)_("The only match");
4303 edit_submode_highl = HLF_COUNT;
4304 }
4305 else
4306 {
4307 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004308 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004310 int number = 0;
4311 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004313 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 {
4315 /* search backwards for the first valid (!= -1) number.
4316 * This should normally succeed already at the first loop
4317 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004318 for (match = compl_curr_match->cp_prev; match != NULL
4319 && match != compl_first_match;
4320 match = match->cp_prev)
4321 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004323 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 break;
4325 }
4326 if (match != NULL)
4327 /* go up and assign all numbers which are not assigned
4328 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004329 for (match = match->cp_next;
4330 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004331 match = match->cp_next)
4332 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334 else /* BACKWARD */
4335 {
4336 /* search forwards (upwards) for the first valid (!= -1)
4337 * number. This should normally succeed already at the
4338 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004339 for (match = compl_curr_match->cp_next; match != NULL
4340 && match != compl_first_match;
4341 match = match->cp_next)
4342 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004344 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 break;
4346 }
4347 if (match != NULL)
4348 /* go down and assign all numbers which are not
4349 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004350 for (match = match->cp_prev; match
4351 && match->cp_number == -1;
4352 match = match->cp_prev)
4353 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 }
4355 }
4356
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004357 /* The match should always have a sequence number now, this is
4358 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004359 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 {
4361 /* Space for 10 text chars. + 2x10-digit no.s */
4362 static char_u match_ref[31];
4363
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004364 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004366 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004368 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004369 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004370 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 edit_submode_extra = match_ref;
4372 edit_submode_highl = HLF_R;
4373 if (dollar_vcol)
4374 curs_columns(FALSE);
4375 }
4376 }
4377 }
4378
4379 /* Show a message about what (completion) mode we're in. */
4380 showmode();
4381 if (edit_submode_extra != NULL)
4382 {
4383 if (!p_smd)
4384 msg_attr(edit_submode_extra,
4385 edit_submode_highl < HLF_COUNT
4386 ? hl_attr(edit_submode_highl) : 0);
4387 }
4388 else
4389 msg_clr_cmdline(); /* necessary for "noshowmode" */
4390
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004391 ins_compl_show_pum();
4392
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 return OK;
4394}
4395
4396/*
4397 * Looks in the first "len" chars. of "src" for search-metachars.
4398 * If dest is not NULL the chars. are copied there quoting (with
4399 * a backslash) the metachars, and dest would be NUL terminated.
4400 * Returns the length (needed) of dest
4401 */
4402 static int
4403quote_meta(dest, src, len)
4404 char_u *dest;
4405 char_u *src;
4406 int len;
4407{
4408 int m;
4409
4410 for (m = len; --len >= 0; src++)
4411 {
4412 switch (*src)
4413 {
4414 case '.':
4415 case '*':
4416 case '[':
4417 if (ctrl_x_mode == CTRL_X_DICTIONARY
4418 || ctrl_x_mode == CTRL_X_THESAURUS)
4419 break;
4420 case '~':
4421 if (!p_magic) /* quote these only if magic is set */
4422 break;
4423 case '\\':
4424 if (ctrl_x_mode == CTRL_X_DICTIONARY
4425 || ctrl_x_mode == CTRL_X_THESAURUS)
4426 break;
4427 case '^': /* currently it's not needed. */
4428 case '$':
4429 m++;
4430 if (dest != NULL)
4431 *dest++ = '\\';
4432 break;
4433 }
4434 if (dest != NULL)
4435 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004436# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 /* Copy remaining bytes of a multibyte character. */
4438 if (has_mbyte)
4439 {
4440 int i, mb_len;
4441
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004442 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 if (mb_len > 0 && len >= mb_len)
4444 for (i = 0; i < mb_len; ++i)
4445 {
4446 --len;
4447 ++src;
4448 if (dest != NULL)
4449 *dest++ = *src;
4450 }
4451 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004452# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 }
4454 if (dest != NULL)
4455 *dest = NUL;
4456
4457 return m;
4458}
4459#endif /* FEAT_INS_EXPAND */
4460
4461/*
4462 * Next character is interpreted literally.
4463 * A one, two or three digit decimal number is interpreted as its byte value.
4464 * If one or two digits are entered, the next character is given to vungetc().
4465 * For Unicode a character > 255 may be returned.
4466 */
4467 int
4468get_literal()
4469{
4470 int cc;
4471 int nc;
4472 int i;
4473 int hex = FALSE;
4474 int octal = FALSE;
4475#ifdef FEAT_MBYTE
4476 int unicode = 0;
4477#endif
4478
4479 if (got_int)
4480 return Ctrl_C;
4481
4482#ifdef FEAT_GUI
4483 /*
4484 * In GUI there is no point inserting the internal code for a special key.
4485 * It is more useful to insert the string "<KEY>" instead. This would
4486 * probably be useful in a text window too, but it would not be
4487 * vi-compatible (maybe there should be an option for it?) -- webb
4488 */
4489 if (gui.in_use)
4490 ++allow_keys;
4491#endif
4492#ifdef USE_ON_FLY_SCROLL
4493 dont_scroll = TRUE; /* disallow scrolling here */
4494#endif
4495 ++no_mapping; /* don't map the next key hits */
4496 cc = 0;
4497 i = 0;
4498 for (;;)
4499 {
4500 do
4501 nc = safe_vgetc();
4502 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4503 || nc == K_HOR_SCROLLBAR);
4504#ifdef FEAT_CMDL_INFO
4505 if (!(State & CMDLINE)
4506# ifdef FEAT_MBYTE
4507 && MB_BYTE2LEN_CHECK(nc) == 1
4508# endif
4509 )
4510 add_to_showcmd(nc);
4511#endif
4512 if (nc == 'x' || nc == 'X')
4513 hex = TRUE;
4514 else if (nc == 'o' || nc == 'O')
4515 octal = TRUE;
4516#ifdef FEAT_MBYTE
4517 else if (nc == 'u' || nc == 'U')
4518 unicode = nc;
4519#endif
4520 else
4521 {
4522 if (hex
4523#ifdef FEAT_MBYTE
4524 || unicode != 0
4525#endif
4526 )
4527 {
4528 if (!vim_isxdigit(nc))
4529 break;
4530 cc = cc * 16 + hex2nr(nc);
4531 }
4532 else if (octal)
4533 {
4534 if (nc < '0' || nc > '7')
4535 break;
4536 cc = cc * 8 + nc - '0';
4537 }
4538 else
4539 {
4540 if (!VIM_ISDIGIT(nc))
4541 break;
4542 cc = cc * 10 + nc - '0';
4543 }
4544
4545 ++i;
4546 }
4547
4548 if (cc > 255
4549#ifdef FEAT_MBYTE
4550 && unicode == 0
4551#endif
4552 )
4553 cc = 255; /* limit range to 0-255 */
4554 nc = 0;
4555
4556 if (hex) /* hex: up to two chars */
4557 {
4558 if (i >= 2)
4559 break;
4560 }
4561#ifdef FEAT_MBYTE
4562 else if (unicode) /* Unicode: up to four or eight chars */
4563 {
4564 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4565 break;
4566 }
4567#endif
4568 else if (i >= 3) /* decimal or octal: up to three chars */
4569 break;
4570 }
4571 if (i == 0) /* no number entered */
4572 {
4573 if (nc == K_ZERO) /* NUL is stored as NL */
4574 {
4575 cc = '\n';
4576 nc = 0;
4577 }
4578 else
4579 {
4580 cc = nc;
4581 nc = 0;
4582 }
4583 }
4584
4585 if (cc == 0) /* NUL is stored as NL */
4586 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004587#ifdef FEAT_MBYTE
4588 if (enc_dbcs && (cc & 0xff) == 0)
4589 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4590 second byte will cause trouble! */
4591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593 --no_mapping;
4594#ifdef FEAT_GUI
4595 if (gui.in_use)
4596 --allow_keys;
4597#endif
4598 if (nc)
4599 vungetc(nc);
4600 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4601 return cc;
4602}
4603
4604/*
4605 * Insert character, taking care of special keys and mod_mask
4606 */
4607 static void
4608insert_special(c, allow_modmask, ctrlv)
4609 int c;
4610 int allow_modmask;
4611 int ctrlv; /* c was typed after CTRL-V */
4612{
4613 char_u *p;
4614 int len;
4615
4616 /*
4617 * Special function key, translate into "<Key>". Up to the last '>' is
4618 * inserted with ins_str(), so as not to replace characters in replace
4619 * mode.
4620 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4621 * unless 'allow_modmask' is TRUE.
4622 */
4623#ifdef MACOS
4624 /* Command-key never produces a normal key */
4625 if (mod_mask & MOD_MASK_CMD)
4626 allow_modmask = TRUE;
4627#endif
4628 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4629 {
4630 p = get_special_key_name(c, mod_mask);
4631 len = (int)STRLEN(p);
4632 c = p[len - 1];
4633 if (len > 2)
4634 {
4635 if (stop_arrow() == FAIL)
4636 return;
4637 p[len - 1] = NUL;
4638 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004639 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 ctrlv = FALSE;
4641 }
4642 }
4643 if (stop_arrow() == OK)
4644 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4645}
4646
4647/*
4648 * Special characters in this context are those that need processing other
4649 * than the simple insertion that can be performed here. This includes ESC
4650 * which terminates the insert, and CR/NL which need special processing to
4651 * open up a new line. This routine tries to optimize insertions performed by
4652 * the "redo", "undo" or "put" commands, so it needs to know when it should
4653 * stop and defer processing to the "normal" mechanism.
4654 * '0' and '^' are special, because they can be followed by CTRL-D.
4655 */
4656#ifdef EBCDIC
4657# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4658#else
4659# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4660#endif
4661
4662#ifdef FEAT_MBYTE
4663# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4664#else
4665# define WHITECHAR(cc) vim_iswhite(cc)
4666#endif
4667
4668 void
4669insertchar(c, flags, second_indent)
4670 int c; /* character to insert or NUL */
4671 int flags; /* INSCHAR_FORMAT, etc. */
4672 int second_indent; /* indent for second line if >= 0 */
4673{
4674 int haveto_redraw = FALSE;
4675 int textwidth;
4676#ifdef FEAT_COMMENTS
4677 colnr_T leader_len;
4678 char_u *p;
4679 int no_leader = FALSE;
4680 int do_comments = (flags & INSCHAR_DO_COM);
4681#endif
4682 int fo_white_par;
4683 int first_line = TRUE;
4684 int fo_ins_blank;
4685#ifdef FEAT_MBYTE
4686 int fo_multibyte;
4687#endif
4688 int save_char = NUL;
4689 int cc;
4690
4691 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4692 fo_ins_blank = has_format_option(FO_INS_BLANK);
4693#ifdef FEAT_MBYTE
4694 fo_multibyte = has_format_option(FO_MBYTE_BREAK);
4695#endif
4696 fo_white_par = has_format_option(FO_WHITE_PAR);
4697
4698 /*
4699 * Try to break the line in two or more pieces when:
4700 * - Always do this if we have been called to do formatting only.
4701 * - Always do this when 'formatoptions' has the 'a' flag and the line
4702 * ends in white space.
4703 * - Otherwise:
4704 * - Don't do this if inserting a blank
4705 * - Don't do this if an existing character is being replaced, unless
4706 * we're in VREPLACE mode.
4707 * - Do this if the cursor is not on the line where insert started
4708 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4709 * before the insert.
4710 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4711 * before 'textwidth'
4712 */
4713 if (textwidth
4714 && ((flags & INSCHAR_FORMAT)
4715 || (!vim_iswhite(c)
4716 && !((State & REPLACE_FLAG)
4717#ifdef FEAT_VREPLACE
4718 && !(State & VREPLACE_FLAG)
4719#endif
4720 && *ml_get_cursor() != NUL)
4721 && (curwin->w_cursor.lnum != Insstart.lnum
4722 || ((!has_format_option(FO_INS_LONG)
4723 || Insstart_textlen <= (colnr_T)textwidth)
4724 && (!fo_ins_blank
4725 || Insstart_blank_vcol <= (colnr_T)textwidth
4726 ))))))
4727 {
4728 /*
4729 * When 'ai' is off we don't want a space under the cursor to be
4730 * deleted. Replace it with an 'x' temporarily.
4731 */
4732 if (!curbuf->b_p_ai)
4733 {
4734 cc = gchar_cursor();
4735 if (vim_iswhite(cc))
4736 {
4737 save_char = cc;
4738 pchar_cursor('x');
4739 }
4740 }
4741
4742 /*
4743 * Repeat breaking lines, until the current line is not too long.
4744 */
4745 while (!got_int)
4746 {
4747 int startcol; /* Cursor column at entry */
4748 int wantcol; /* column at textwidth border */
4749 int foundcol; /* column for start of spaces */
4750 int end_foundcol = 0; /* column for start of word */
4751 colnr_T len;
4752 colnr_T virtcol;
4753#ifdef FEAT_VREPLACE
4754 int orig_col = 0;
4755 char_u *saved_text = NULL;
4756#endif
4757 colnr_T col;
4758
4759 virtcol = get_nolist_virtcol();
4760 if (virtcol < (colnr_T)textwidth)
4761 break;
4762
4763#ifdef FEAT_COMMENTS
4764 if (no_leader)
4765 do_comments = FALSE;
4766 else if (!(flags & INSCHAR_FORMAT)
4767 && has_format_option(FO_WRAP_COMS))
4768 do_comments = TRUE;
4769
4770 /* Don't break until after the comment leader */
4771 if (do_comments)
4772 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
4773 else
4774 leader_len = 0;
4775
4776 /* If the line doesn't start with a comment leader, then don't
4777 * start one in a following broken line. Avoids that a %word
4778 * moved to the start of the next line causes all following lines
4779 * to start with %. */
4780 if (leader_len == 0)
4781 no_leader = TRUE;
4782#endif
4783 if (!(flags & INSCHAR_FORMAT)
4784#ifdef FEAT_COMMENTS
4785 && leader_len == 0
4786#endif
4787 && !has_format_option(FO_WRAP))
4788
4789 {
4790 textwidth = 0;
4791 break;
4792 }
4793 if ((startcol = curwin->w_cursor.col) == 0)
4794 break;
4795
4796 /* find column of textwidth border */
4797 coladvance((colnr_T)textwidth);
4798 wantcol = curwin->w_cursor.col;
4799
4800 curwin->w_cursor.col = startcol - 1;
4801#ifdef FEAT_MBYTE
4802 /* Correct cursor for multi-byte character. */
4803 if (has_mbyte)
4804 mb_adjust_cursor();
4805#endif
4806 foundcol = 0;
4807
4808 /*
4809 * Find position to break at.
4810 * Stop at first entered white when 'formatoptions' has 'v'
4811 */
4812 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
4813 || curwin->w_cursor.lnum != Insstart.lnum
4814 || curwin->w_cursor.col >= Insstart.col)
4815 {
4816 cc = gchar_cursor();
4817 if (WHITECHAR(cc))
4818 {
4819 /* remember position of blank just before text */
4820 end_foundcol = curwin->w_cursor.col;
4821
4822 /* find start of sequence of blanks */
4823 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
4824 {
4825 dec_cursor();
4826 cc = gchar_cursor();
4827 }
4828 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
4829 break; /* only spaces in front of text */
4830#ifdef FEAT_COMMENTS
4831 /* Don't break until after the comment leader */
4832 if (curwin->w_cursor.col < leader_len)
4833 break;
4834#endif
4835 if (has_format_option(FO_ONE_LETTER))
4836 {
4837 /* do not break after one-letter words */
4838 if (curwin->w_cursor.col == 0)
4839 break; /* one-letter word at begin */
4840
4841 col = curwin->w_cursor.col;
4842 dec_cursor();
4843 cc = gchar_cursor();
4844
4845 if (WHITECHAR(cc))
4846 continue; /* one-letter, continue */
4847 curwin->w_cursor.col = col;
4848 }
4849#ifdef FEAT_MBYTE
4850 if (has_mbyte)
4851 foundcol = curwin->w_cursor.col
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004852 + (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 else
4854#endif
4855 foundcol = curwin->w_cursor.col + 1;
4856 if (curwin->w_cursor.col < (colnr_T)wantcol)
4857 break;
4858 }
4859#ifdef FEAT_MBYTE
4860 else if (cc >= 0x100 && fo_multibyte
4861 && curwin->w_cursor.col <= (colnr_T)wantcol)
4862 {
4863 /* Break after or before a multi-byte character. */
4864 foundcol = curwin->w_cursor.col;
4865 if (curwin->w_cursor.col < (colnr_T)wantcol)
4866 foundcol += (*mb_char2len)(cc);
4867 end_foundcol = foundcol;
4868 break;
4869 }
4870#endif
4871 if (curwin->w_cursor.col == 0)
4872 break;
4873 dec_cursor();
4874 }
4875
4876 if (foundcol == 0) /* no spaces, cannot break line */
4877 {
4878 curwin->w_cursor.col = startcol;
4879 break;
4880 }
4881
4882 /* Going to break the line, remove any "$" now. */
4883 undisplay_dollar();
4884
4885 /*
4886 * Offset between cursor position and line break is used by replace
4887 * stack functions. VREPLACE does not use this, and backspaces
4888 * over the text instead.
4889 */
4890#ifdef FEAT_VREPLACE
4891 if (State & VREPLACE_FLAG)
4892 orig_col = startcol; /* Will start backspacing from here */
4893 else
4894#endif
4895 replace_offset = startcol - end_foundcol - 1;
4896
4897 /*
4898 * adjust startcol for spaces that will be deleted and
4899 * characters that will remain on top line
4900 */
4901 curwin->w_cursor.col = foundcol;
4902 while (cc = gchar_cursor(), WHITECHAR(cc))
4903 inc_cursor();
4904 startcol -= curwin->w_cursor.col;
4905 if (startcol < 0)
4906 startcol = 0;
4907
4908#ifdef FEAT_VREPLACE
4909 if (State & VREPLACE_FLAG)
4910 {
4911 /*
4912 * In VREPLACE mode, we will backspace over the text to be
4913 * wrapped, so save a copy now to put on the next line.
4914 */
4915 saved_text = vim_strsave(ml_get_cursor());
4916 curwin->w_cursor.col = orig_col;
4917 if (saved_text == NULL)
4918 break; /* Can't do it, out of memory */
4919 saved_text[startcol] = NUL;
4920
4921 /* Backspace over characters that will move to the next line */
4922 if (!fo_white_par)
4923 backspace_until_column(foundcol);
4924 }
4925 else
4926#endif
4927 {
4928 /* put cursor after pos. to break line */
4929 if (!fo_white_par)
4930 curwin->w_cursor.col = foundcol;
4931 }
4932
4933 /*
4934 * Split the line just before the margin.
4935 * Only insert/delete lines, but don't really redraw the window.
4936 */
4937 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
4938 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
4939#ifdef FEAT_COMMENTS
4940 + (do_comments ? OPENLINE_DO_COM : 0)
4941#endif
4942 , old_indent);
4943 old_indent = 0;
4944
4945 replace_offset = 0;
4946 if (first_line)
4947 {
4948 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
4949 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
4950 if (second_indent >= 0)
4951 {
4952#ifdef FEAT_VREPLACE
4953 if (State & VREPLACE_FLAG)
4954 change_indent(INDENT_SET, second_indent, FALSE, NUL);
4955 else
4956#endif
4957 (void)set_indent(second_indent, SIN_CHANGED);
4958 }
4959 first_line = FALSE;
4960 }
4961
4962#ifdef FEAT_VREPLACE
4963 if (State & VREPLACE_FLAG)
4964 {
4965 /*
4966 * In VREPLACE mode we have backspaced over the text to be
4967 * moved, now we re-insert it into the new line.
4968 */
4969 ins_bytes(saved_text);
4970 vim_free(saved_text);
4971 }
4972 else
4973#endif
4974 {
4975 /*
4976 * Check if cursor is not past the NUL off the line, cindent
4977 * may have added or removed indent.
4978 */
4979 curwin->w_cursor.col += startcol;
4980 len = (colnr_T)STRLEN(ml_get_curline());
4981 if (curwin->w_cursor.col > len)
4982 curwin->w_cursor.col = len;
4983 }
4984
4985 haveto_redraw = TRUE;
4986#ifdef FEAT_CINDENT
4987 can_cindent = TRUE;
4988#endif
4989 /* moved the cursor, don't autoindent or cindent now */
4990 did_ai = FALSE;
4991#ifdef FEAT_SMARTINDENT
4992 did_si = FALSE;
4993 can_si = FALSE;
4994 can_si_back = FALSE;
4995#endif
4996 line_breakcheck();
4997 }
4998
4999 if (save_char) /* put back space after cursor */
5000 pchar_cursor(save_char);
5001
5002 if (c == NUL) /* formatting only */
5003 return;
5004 if (haveto_redraw)
5005 {
5006 update_topline();
5007 redraw_curbuf_later(VALID);
5008 }
5009 }
5010 if (c == NUL) /* only formatting was wanted */
5011 return;
5012
5013#ifdef FEAT_COMMENTS
5014 /* Check whether this character should end a comment. */
5015 if (did_ai && (int)c == end_comment_pending)
5016 {
5017 char_u *line;
5018 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5019 int middle_len, end_len;
5020 int i;
5021
5022 /*
5023 * Need to remove existing (middle) comment leader and insert end
5024 * comment leader. First, check what comment leader we can find.
5025 */
5026 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5027 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5028 {
5029 /* Skip middle-comment string */
5030 while (*p && p[-1] != ':') /* find end of middle flags */
5031 ++p;
5032 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5033 /* Don't count trailing white space for middle_len */
5034 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5035 --middle_len;
5036
5037 /* Find the end-comment string */
5038 while (*p && p[-1] != ':') /* find end of end flags */
5039 ++p;
5040 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5041
5042 /* Skip white space before the cursor */
5043 i = curwin->w_cursor.col;
5044 while (--i >= 0 && vim_iswhite(line[i]))
5045 ;
5046 i++;
5047
5048 /* Skip to before the middle leader */
5049 i -= middle_len;
5050
5051 /* Check some expected things before we go on */
5052 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5053 {
5054 /* Backspace over all the stuff we want to replace */
5055 backspace_until_column(i);
5056
5057 /*
5058 * Insert the end-comment string, except for the last
5059 * character, which will get inserted as normal later.
5060 */
5061 ins_bytes_len(lead_end, end_len - 1);
5062 }
5063 }
5064 }
5065 end_comment_pending = NUL;
5066#endif
5067
5068 did_ai = FALSE;
5069#ifdef FEAT_SMARTINDENT
5070 did_si = FALSE;
5071 can_si = FALSE;
5072 can_si_back = FALSE;
5073#endif
5074
5075 /*
5076 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5077 * This speeds up normal text input considerably.
5078 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5079 * need to re-indent at a ':', or any other character (but not what
5080 * 'paste' is set)..
5081 */
5082#ifdef USE_ON_FLY_SCROLL
5083 dont_scroll = FALSE; /* allow scrolling here */
5084#endif
5085
5086 if ( !ISSPECIAL(c)
5087#ifdef FEAT_MBYTE
5088 && (!has_mbyte || (*mb_char2len)(c) == 1)
5089#endif
5090 && vpeekc() != NUL
5091 && !(State & REPLACE_FLAG)
5092#ifdef FEAT_CINDENT
5093 && !cindent_on()
5094#endif
5095#ifdef FEAT_RIGHTLEFT
5096 && !p_ri
5097#endif
5098 )
5099 {
5100#define INPUT_BUFLEN 100
5101 char_u buf[INPUT_BUFLEN + 1];
5102 int i;
5103 colnr_T virtcol = 0;
5104
5105 buf[0] = c;
5106 i = 1;
5107 if (textwidth)
5108 virtcol = get_nolist_virtcol();
5109 /*
5110 * Stop the string when:
5111 * - no more chars available
5112 * - finding a special character (command key)
5113 * - buffer is full
5114 * - running into the 'textwidth' boundary
5115 * - need to check for abbreviation: A non-word char after a word-char
5116 */
5117 while ( (c = vpeekc()) != NUL
5118 && !ISSPECIAL(c)
5119#ifdef FEAT_MBYTE
5120 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5121#endif
5122 && i < INPUT_BUFLEN
5123 && (textwidth == 0
5124 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5125 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5126 {
5127#ifdef FEAT_RIGHTLEFT
5128 c = vgetc();
5129 if (p_hkmap && KeyTyped)
5130 c = hkmap(c); /* Hebrew mode mapping */
5131# ifdef FEAT_FKMAP
5132 if (p_fkmap && KeyTyped)
5133 c = fkmap(c); /* Farsi mode mapping */
5134# endif
5135 buf[i++] = c;
5136#else
5137 buf[i++] = vgetc();
5138#endif
5139 }
5140
5141#ifdef FEAT_DIGRAPHS
5142 do_digraph(-1); /* clear digraphs */
5143 do_digraph(buf[i-1]); /* may be the start of a digraph */
5144#endif
5145 buf[i] = NUL;
5146 ins_str(buf);
5147 if (flags & INSCHAR_CTRLV)
5148 {
5149 redo_literal(*buf);
5150 i = 1;
5151 }
5152 else
5153 i = 0;
5154 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005155 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
5157 else
5158 {
5159#ifdef FEAT_MBYTE
5160 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5161 {
5162 char_u buf[MB_MAXBYTES + 1];
5163
5164 (*mb_char2bytes)(c, buf);
5165 buf[cc] = NUL;
5166 ins_char_bytes(buf, cc);
5167 AppendCharToRedobuff(c);
5168 }
5169 else
5170#endif
5171 {
5172 ins_char(c);
5173 if (flags & INSCHAR_CTRLV)
5174 redo_literal(c);
5175 else
5176 AppendCharToRedobuff(c);
5177 }
5178 }
5179}
5180
5181/*
5182 * Called after inserting or deleting text: When 'formatoptions' includes the
5183 * 'a' flag format from the current line until the end of the paragraph.
5184 * Keep the cursor at the same position relative to the text.
5185 * The caller must have saved the cursor line for undo, following ones will be
5186 * saved here.
5187 */
5188 void
5189auto_format(trailblank, prev_line)
5190 int trailblank; /* when TRUE also format with trailing blank */
5191 int prev_line; /* may start in previous line */
5192{
5193 pos_T pos;
5194 colnr_T len;
5195 char_u *old;
5196 char_u *new, *pnew;
5197 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005198 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199
5200 if (!has_format_option(FO_AUTO))
5201 return;
5202
5203 pos = curwin->w_cursor;
5204 old = ml_get_curline();
5205
5206 /* may remove added space */
5207 check_auto_format(FALSE);
5208
5209 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5210 * user might insert normal text next. Also skip formatting when "1" is
5211 * in 'formatoptions' and there is a single character before the cursor.
5212 * Otherwise the line would be broken and when typing another non-white
5213 * next they are not joined back together. */
5214 wasatend = (pos.col == STRLEN(old));
5215 if (*old != NUL && !trailblank && wasatend)
5216 {
5217 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005218 cc = gchar_cursor();
5219 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5220 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005222 cc = gchar_cursor();
5223 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 {
5225 curwin->w_cursor = pos;
5226 return;
5227 }
5228 curwin->w_cursor = pos;
5229 }
5230
5231#ifdef FEAT_COMMENTS
5232 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5233 * comments. */
5234 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5235 && get_leader_len(old, NULL, FALSE) == 0)
5236 return;
5237#endif
5238
5239 /*
5240 * May start formatting in a previous line, so that after "x" a word is
5241 * moved to the previous line if it fits there now. Only when this is not
5242 * the start of a paragraph.
5243 */
5244 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5245 {
5246 --curwin->w_cursor.lnum;
5247 if (u_save_cursor() == FAIL)
5248 return;
5249 }
5250
5251 /*
5252 * Do the formatting and restore the cursor position. "saved_cursor" will
5253 * be adjusted for the text formatting.
5254 */
5255 saved_cursor = pos;
5256 format_lines((linenr_T)-1);
5257 curwin->w_cursor = saved_cursor;
5258 saved_cursor.lnum = 0;
5259
5260 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5261 {
5262 /* "cannot happen" */
5263 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5264 coladvance((colnr_T)MAXCOL);
5265 }
5266 else
5267 check_cursor_col();
5268
5269 /* Insert mode: If the cursor is now after the end of the line while it
5270 * previously wasn't, the line was broken. Because of the rule above we
5271 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5272 * formatted. */
5273 if (!wasatend && has_format_option(FO_WHITE_PAR))
5274 {
5275 new = ml_get_curline();
5276 len = STRLEN(new);
5277 if (curwin->w_cursor.col == len)
5278 {
5279 pnew = vim_strnsave(new, len + 2);
5280 pnew[len] = ' ';
5281 pnew[len + 1] = NUL;
5282 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5283 /* remove the space later */
5284 did_add_space = TRUE;
5285 }
5286 else
5287 /* may remove added space */
5288 check_auto_format(FALSE);
5289 }
5290
5291 check_cursor();
5292}
5293
5294/*
5295 * When an extra space was added to continue a paragraph for auto-formatting,
5296 * delete it now. The space must be under the cursor, just after the insert
5297 * position.
5298 */
5299 static void
5300check_auto_format(end_insert)
5301 int end_insert; /* TRUE when ending Insert mode */
5302{
5303 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005304 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
5306 if (did_add_space)
5307 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005308 cc = gchar_cursor();
5309 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 /* Somehow the space was removed already. */
5311 did_add_space = FALSE;
5312 else
5313 {
5314 if (!end_insert)
5315 {
5316 inc_cursor();
5317 c = gchar_cursor();
5318 dec_cursor();
5319 }
5320 if (c != NUL)
5321 {
5322 /* The space is no longer at the end of the line, delete it. */
5323 del_char(FALSE);
5324 did_add_space = FALSE;
5325 }
5326 }
5327 }
5328}
5329
5330/*
5331 * Find out textwidth to be used for formatting:
5332 * if 'textwidth' option is set, use it
5333 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5334 * if invalid value, use 0.
5335 * Set default to window width (maximum 79) for "gq" operator.
5336 */
5337 int
5338comp_textwidth(ff)
5339 int ff; /* force formatting (for "Q" command) */
5340{
5341 int textwidth;
5342
5343 textwidth = curbuf->b_p_tw;
5344 if (textwidth == 0 && curbuf->b_p_wm)
5345 {
5346 /* The width is the window width minus 'wrapmargin' minus all the
5347 * things that add to the margin. */
5348 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5349#ifdef FEAT_CMDWIN
5350 if (cmdwin_type != 0)
5351 textwidth -= 1;
5352#endif
5353#ifdef FEAT_FOLDING
5354 textwidth -= curwin->w_p_fdc;
5355#endif
5356#ifdef FEAT_SIGNS
5357 if (curwin->w_buffer->b_signlist != NULL
5358# ifdef FEAT_NETBEANS_INTG
5359 || usingNetbeans
5360# endif
5361 )
5362 textwidth -= 1;
5363#endif
5364 if (curwin->w_p_nu)
5365 textwidth -= 8;
5366 }
5367 if (textwidth < 0)
5368 textwidth = 0;
5369 if (ff && textwidth == 0)
5370 {
5371 textwidth = W_WIDTH(curwin) - 1;
5372 if (textwidth > 79)
5373 textwidth = 79;
5374 }
5375 return textwidth;
5376}
5377
5378/*
5379 * Put a character in the redo buffer, for when just after a CTRL-V.
5380 */
5381 static void
5382redo_literal(c)
5383 int c;
5384{
5385 char_u buf[10];
5386
5387 /* Only digits need special treatment. Translate them into a string of
5388 * three digits. */
5389 if (VIM_ISDIGIT(c))
5390 {
5391 sprintf((char *)buf, "%03d", c);
5392 AppendToRedobuff(buf);
5393 }
5394 else
5395 AppendCharToRedobuff(c);
5396}
5397
5398/*
5399 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005400 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 */
5402 static void
5403start_arrow(end_insert_pos)
5404 pos_T *end_insert_pos;
5405{
5406 if (!arrow_used) /* something has been inserted */
5407 {
5408 AppendToRedobuff(ESC_STR);
5409 stop_insert(end_insert_pos, FALSE);
5410 arrow_used = TRUE; /* this means we stopped the current insert */
5411 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005412#ifdef FEAT_SYN_HL
5413 check_spell_redraw();
5414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415}
5416
Bram Moolenaar217ad922005-03-20 22:37:15 +00005417#ifdef FEAT_SYN_HL
5418/*
5419 * If we skipped highlighting word at cursor, do it now.
5420 * It may be skipped again, thus reset spell_redraw_lnum first.
5421 */
5422 static void
5423check_spell_redraw()
5424{
5425 if (spell_redraw_lnum != 0)
5426 {
5427 linenr_T lnum = spell_redraw_lnum;
5428
5429 spell_redraw_lnum = 0;
5430 redrawWinline(lnum, FALSE);
5431 }
5432}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005433
5434/*
5435 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5436 * spelled word, if there is one.
5437 */
5438 static void
5439spell_back_to_badword()
5440{
5441 pos_T tpos = curwin->w_cursor;
5442
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005443 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005444 if (curwin->w_cursor.col != tpos.col)
5445 start_arrow(&tpos);
5446}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005447#endif
5448
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449/*
5450 * stop_arrow() is called before a change is made in insert mode.
5451 * If an arrow key has been used, start a new insertion.
5452 * Returns FAIL if undo is impossible, shouldn't insert then.
5453 */
5454 int
5455stop_arrow()
5456{
5457 if (arrow_used)
5458 {
5459 if (u_save_cursor() == OK)
5460 {
5461 arrow_used = FALSE;
5462 ins_need_undo = FALSE;
5463 }
5464 Insstart = curwin->w_cursor; /* new insertion starts here */
5465 Insstart_textlen = linetabsize(ml_get_curline());
5466 ai_col = 0;
5467#ifdef FEAT_VREPLACE
5468 if (State & VREPLACE_FLAG)
5469 {
5470 orig_line_count = curbuf->b_ml.ml_line_count;
5471 vr_lines_changed = 1;
5472 }
5473#endif
5474 ResetRedobuff();
5475 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005476 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 }
5478 else if (ins_need_undo)
5479 {
5480 if (u_save_cursor() == OK)
5481 ins_need_undo = FALSE;
5482 }
5483
5484#ifdef FEAT_FOLDING
5485 /* Always open fold at the cursor line when inserting something. */
5486 foldOpenCursor();
5487#endif
5488
5489 return (arrow_used || ins_need_undo ? FAIL : OK);
5490}
5491
5492/*
5493 * do a few things to stop inserting
5494 */
5495 static void
5496stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005497 pos_T *end_insert_pos; /* where insert ended */
5498 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005500 int cc;
5501 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502
5503 stop_redo_ins();
5504 replace_flush(); /* abandon replace stack */
5505
5506 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005507 * Save the inserted text for later redo with ^@ and CTRL-A.
5508 * Don't do it when "restart_edit" was set and nothing was inserted,
5509 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005511 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005512 if (did_restart_edit == 0 || (ptr != NULL
5513 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005514 {
5515 vim_free(last_insert);
5516 last_insert = ptr;
5517 last_insert_skip = new_insert_skip;
5518 }
5519 else
5520 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521
5522 if (!arrow_used)
5523 {
5524 /* Auto-format now. It may seem strange to do this when stopping an
5525 * insertion (or moving the cursor), but it's required when appending
5526 * a line and having it end in a space. But only do it when something
5527 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005528 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005530 pos_T tpos = curwin->w_cursor;
5531
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 /* When the cursor is at the end of the line after a space the
5533 * formatting will move it to the following word. Avoid that by
5534 * moving the cursor onto the space. */
5535 cc = 'x';
5536 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5537 {
5538 dec_cursor();
5539 cc = gchar_cursor();
5540 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005541 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 }
5543
5544 auto_format(TRUE, FALSE);
5545
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005546 if (vim_iswhite(cc))
5547 {
5548 if (gchar_cursor() != NUL)
5549 inc_cursor();
5550#ifdef FEAT_VIRTUALEDIT
5551 /* If the cursor is still at the same character, also keep
5552 * the "coladd". */
5553 if (gchar_cursor() == NUL
5554 && curwin->w_cursor.lnum == tpos.lnum
5555 && curwin->w_cursor.col == tpos.col)
5556 curwin->w_cursor.coladd = tpos.coladd;
5557#endif
5558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 }
5560
5561 /* If a space was inserted for auto-formatting, remove it now. */
5562 check_auto_format(TRUE);
5563
5564 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005565 * of the line, and put the cursor back.
5566 * Do this when ESC was used or moving the cursor up/down. */
5567 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5568 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005570 pos_T tpos = curwin->w_cursor;
5571
5572 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5574 --curwin->w_cursor.col;
5575 while (cc = gchar_cursor(), vim_iswhite(cc))
5576 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005577 if (curwin->w_cursor.lnum != tpos.lnum)
5578 curwin->w_cursor = tpos;
5579 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5581
5582#ifdef FEAT_VISUAL
5583 /* <C-S-Right> may have started Visual mode, adjust the position for
5584 * deleted characters. */
5585 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5586 {
5587 cc = STRLEN(ml_get_curline());
5588 if (VIsual.col > (colnr_T)cc)
5589 {
5590 VIsual.col = cc;
5591# ifdef FEAT_VIRTUALEDIT
5592 VIsual.coladd = 0;
5593# endif
5594 }
5595 }
5596#endif
5597 }
5598 }
5599 did_ai = FALSE;
5600#ifdef FEAT_SMARTINDENT
5601 did_si = FALSE;
5602 can_si = FALSE;
5603 can_si_back = FALSE;
5604#endif
5605
5606 /* set '[ and '] to the inserted text */
5607 curbuf->b_op_start = Insstart;
5608 curbuf->b_op_end = *end_insert_pos;
5609}
5610
5611/*
5612 * Set the last inserted text to a single character.
5613 * Used for the replace command.
5614 */
5615 void
5616set_last_insert(c)
5617 int c;
5618{
5619 char_u *s;
5620
5621 vim_free(last_insert);
5622#ifdef FEAT_MBYTE
5623 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5624#else
5625 last_insert = alloc(6);
5626#endif
5627 if (last_insert != NULL)
5628 {
5629 s = last_insert;
5630 /* Use the CTRL-V only when entering a special char */
5631 if (c < ' ' || c == DEL)
5632 *s++ = Ctrl_V;
5633 s = add_char2buf(c, s);
5634 *s++ = ESC;
5635 *s++ = NUL;
5636 last_insert_skip = 0;
5637 }
5638}
5639
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005640#if defined(EXITFREE) || defined(PROTO)
5641 void
5642free_last_insert()
5643{
5644 vim_free(last_insert);
5645 last_insert = NULL;
5646}
5647#endif
5648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649/*
5650 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5651 * and CSI. Handle multi-byte characters.
5652 * Returns a pointer to after the added bytes.
5653 */
5654 char_u *
5655add_char2buf(c, s)
5656 int c;
5657 char_u *s;
5658{
5659#ifdef FEAT_MBYTE
5660 char_u temp[MB_MAXBYTES];
5661 int i;
5662 int len;
5663
5664 len = (*mb_char2bytes)(c, temp);
5665 for (i = 0; i < len; ++i)
5666 {
5667 c = temp[i];
5668#endif
5669 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5670 if (c == K_SPECIAL)
5671 {
5672 *s++ = K_SPECIAL;
5673 *s++ = KS_SPECIAL;
5674 *s++ = KE_FILLER;
5675 }
5676#ifdef FEAT_GUI
5677 else if (c == CSI)
5678 {
5679 *s++ = CSI;
5680 *s++ = KS_EXTRA;
5681 *s++ = (int)KE_CSI;
5682 }
5683#endif
5684 else
5685 *s++ = c;
5686#ifdef FEAT_MBYTE
5687 }
5688#endif
5689 return s;
5690}
5691
5692/*
5693 * move cursor to start of line
5694 * if flags & BL_WHITE move to first non-white
5695 * if flags & BL_SOL move to first non-white if startofline is set,
5696 * otherwise keep "curswant" column
5697 * if flags & BL_FIX don't leave the cursor on a NUL.
5698 */
5699 void
5700beginline(flags)
5701 int flags;
5702{
5703 if ((flags & BL_SOL) && !p_sol)
5704 coladvance(curwin->w_curswant);
5705 else
5706 {
5707 curwin->w_cursor.col = 0;
5708#ifdef FEAT_VIRTUALEDIT
5709 curwin->w_cursor.coladd = 0;
5710#endif
5711
5712 if (flags & (BL_WHITE | BL_SOL))
5713 {
5714 char_u *ptr;
5715
5716 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5717 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5718 ++curwin->w_cursor.col;
5719 }
5720 curwin->w_set_curswant = TRUE;
5721 }
5722}
5723
5724/*
5725 * oneright oneleft cursor_down cursor_up
5726 *
5727 * Move one char {right,left,down,up}.
5728 * Doesn't move onto the NUL past the end of the line.
5729 * Return OK when successful, FAIL when we hit a line of file boundary.
5730 */
5731
5732 int
5733oneright()
5734{
5735 char_u *ptr;
5736#ifdef FEAT_MBYTE
5737 int l;
5738#endif
5739
5740#ifdef FEAT_VIRTUALEDIT
5741 if (virtual_active())
5742 {
5743 pos_T prevpos = curwin->w_cursor;
5744
5745 /* Adjust for multi-wide char (excluding TAB) */
5746 ptr = ml_get_cursor();
5747 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5748#ifdef FEAT_MBYTE
5749 (*mb_ptr2char)(ptr)
5750#else
5751 *ptr
5752#endif
5753 ))
5754 ? ptr2cells(ptr) : 1));
5755 curwin->w_set_curswant = TRUE;
5756 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5757 return (prevpos.col != curwin->w_cursor.col
5758 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5759 }
5760#endif
5761
5762 ptr = ml_get_cursor();
5763#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005764 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 {
5766 /* The character under the cursor is a multi-byte character, move
5767 * several bytes right, but don't end up on the NUL. */
5768 if (ptr[l] == NUL)
5769 return FAIL;
5770 curwin->w_cursor.col += l;
5771 }
5772 else
5773#endif
5774 {
5775 if (*ptr++ == NUL || *ptr == NUL)
5776 return FAIL;
5777 ++curwin->w_cursor.col;
5778 }
5779
5780 curwin->w_set_curswant = TRUE;
5781 return OK;
5782}
5783
5784 int
5785oneleft()
5786{
5787#ifdef FEAT_VIRTUALEDIT
5788 if (virtual_active())
5789 {
5790 int width;
5791 int v = getviscol();
5792
5793 if (v == 0)
5794 return FAIL;
5795
5796# ifdef FEAT_LINEBREAK
5797 /* We might get stuck on 'showbreak', skip over it. */
5798 width = 1;
5799 for (;;)
5800 {
5801 coladvance(v - width);
5802 /* getviscol() is slow, skip it when 'showbreak' is empty and
5803 * there are no multi-byte characters */
5804 if ((*p_sbr == NUL
5805# ifdef FEAT_MBYTE
5806 && !has_mbyte
5807# endif
5808 ) || getviscol() < v)
5809 break;
5810 ++width;
5811 }
5812# else
5813 coladvance(v - 1);
5814# endif
5815
5816 if (curwin->w_cursor.coladd == 1)
5817 {
5818 char_u *ptr;
5819
5820 /* Adjust for multi-wide char (not a TAB) */
5821 ptr = ml_get_cursor();
5822 if (*ptr != TAB && vim_isprintc(
5823# ifdef FEAT_MBYTE
5824 (*mb_ptr2char)(ptr)
5825# else
5826 *ptr
5827# endif
5828 ) && ptr2cells(ptr) > 1)
5829 curwin->w_cursor.coladd = 0;
5830 }
5831
5832 curwin->w_set_curswant = TRUE;
5833 return OK;
5834 }
5835#endif
5836
5837 if (curwin->w_cursor.col == 0)
5838 return FAIL;
5839
5840 curwin->w_set_curswant = TRUE;
5841 --curwin->w_cursor.col;
5842
5843#ifdef FEAT_MBYTE
5844 /* if the character on the left of the current cursor is a multi-byte
5845 * character, move to its first byte */
5846 if (has_mbyte)
5847 mb_adjust_cursor();
5848#endif
5849 return OK;
5850}
5851
5852 int
5853cursor_up(n, upd_topline)
5854 long n;
5855 int upd_topline; /* When TRUE: update topline */
5856{
5857 linenr_T lnum;
5858
5859 if (n > 0)
5860 {
5861 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005862 /* This fails if the cursor is already in the first line or the count
5863 * is larger than the line number and '-' is in 'cpoptions' */
5864 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 return FAIL;
5866 if (n >= lnum)
5867 lnum = 1;
5868 else
5869#ifdef FEAT_FOLDING
5870 if (hasAnyFolding(curwin))
5871 {
5872 /*
5873 * Count each sequence of folded lines as one logical line.
5874 */
5875 /* go to the the start of the current fold */
5876 (void)hasFolding(lnum, &lnum, NULL);
5877
5878 while (n--)
5879 {
5880 /* move up one line */
5881 --lnum;
5882 if (lnum <= 1)
5883 break;
5884 /* If we entered a fold, move to the beginning, unless in
5885 * Insert mode or when 'foldopen' contains "all": it will open
5886 * in a moment. */
5887 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
5888 (void)hasFolding(lnum, &lnum, NULL);
5889 }
5890 if (lnum < 1)
5891 lnum = 1;
5892 }
5893 else
5894#endif
5895 lnum -= n;
5896 curwin->w_cursor.lnum = lnum;
5897 }
5898
5899 /* try to advance to the column we want to be at */
5900 coladvance(curwin->w_curswant);
5901
5902 if (upd_topline)
5903 update_topline(); /* make sure curwin->w_topline is valid */
5904
5905 return OK;
5906}
5907
5908/*
5909 * Cursor down a number of logical lines.
5910 */
5911 int
5912cursor_down(n, upd_topline)
5913 long n;
5914 int upd_topline; /* When TRUE: update topline */
5915{
5916 linenr_T lnum;
5917
5918 if (n > 0)
5919 {
5920 lnum = curwin->w_cursor.lnum;
5921#ifdef FEAT_FOLDING
5922 /* Move to last line of fold, will fail if it's the end-of-file. */
5923 (void)hasFolding(lnum, NULL, &lnum);
5924#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00005925 /* This fails if the cursor is already in the last line or would move
5926 * beyound the last line and '-' is in 'cpoptions' */
5927 if (lnum >= curbuf->b_ml.ml_line_count
5928 || (lnum + n > curbuf->b_ml.ml_line_count
5929 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 return FAIL;
5931 if (lnum + n >= curbuf->b_ml.ml_line_count)
5932 lnum = curbuf->b_ml.ml_line_count;
5933 else
5934#ifdef FEAT_FOLDING
5935 if (hasAnyFolding(curwin))
5936 {
5937 linenr_T last;
5938
5939 /* count each sequence of folded lines as one logical line */
5940 while (n--)
5941 {
5942 if (hasFolding(lnum, NULL, &last))
5943 lnum = last + 1;
5944 else
5945 ++lnum;
5946 if (lnum >= curbuf->b_ml.ml_line_count)
5947 break;
5948 }
5949 if (lnum > curbuf->b_ml.ml_line_count)
5950 lnum = curbuf->b_ml.ml_line_count;
5951 }
5952 else
5953#endif
5954 lnum += n;
5955 curwin->w_cursor.lnum = lnum;
5956 }
5957
5958 /* try to advance to the column we want to be at */
5959 coladvance(curwin->w_curswant);
5960
5961 if (upd_topline)
5962 update_topline(); /* make sure curwin->w_topline is valid */
5963
5964 return OK;
5965}
5966
5967/*
5968 * Stuff the last inserted text in the read buffer.
5969 * Last_insert actually is a copy of the redo buffer, so we
5970 * first have to remove the command.
5971 */
5972 int
5973stuff_inserted(c, count, no_esc)
5974 int c; /* Command character to be inserted */
5975 long count; /* Repeat this many times */
5976 int no_esc; /* Don't add an ESC at the end */
5977{
5978 char_u *esc_ptr;
5979 char_u *ptr;
5980 char_u *last_ptr;
5981 char_u last = NUL;
5982
5983 ptr = get_last_insert();
5984 if (ptr == NULL)
5985 {
5986 EMSG(_(e_noinstext));
5987 return FAIL;
5988 }
5989
5990 /* may want to stuff the command character, to start Insert mode */
5991 if (c != NUL)
5992 stuffcharReadbuff(c);
5993 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
5994 *esc_ptr = NUL; /* remove the ESC */
5995
5996 /* when the last char is either "0" or "^" it will be quoted if no ESC
5997 * comes after it OR if it will inserted more than once and "ptr"
5998 * starts with ^D. -- Acevedo
5999 */
6000 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6001 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6002 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6003 {
6004 last = *last_ptr;
6005 *last_ptr = NUL;
6006 }
6007
6008 do
6009 {
6010 stuffReadbuff(ptr);
6011 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6012 if (last)
6013 stuffReadbuff((char_u *)(last == '0'
6014 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6015 : IF_EB("\026^", CTRL_V_STR "^")));
6016 }
6017 while (--count > 0);
6018
6019 if (last)
6020 *last_ptr = last;
6021
6022 if (esc_ptr != NULL)
6023 *esc_ptr = ESC; /* put the ESC back */
6024
6025 /* may want to stuff a trailing ESC, to get out of Insert mode */
6026 if (!no_esc)
6027 stuffcharReadbuff(ESC);
6028
6029 return OK;
6030}
6031
6032 char_u *
6033get_last_insert()
6034{
6035 if (last_insert == NULL)
6036 return NULL;
6037 return last_insert + last_insert_skip;
6038}
6039
6040/*
6041 * Get last inserted string, and remove trailing <Esc>.
6042 * Returns pointer to allocated memory (must be freed) or NULL.
6043 */
6044 char_u *
6045get_last_insert_save()
6046{
6047 char_u *s;
6048 int len;
6049
6050 if (last_insert == NULL)
6051 return NULL;
6052 s = vim_strsave(last_insert + last_insert_skip);
6053 if (s != NULL)
6054 {
6055 len = (int)STRLEN(s);
6056 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6057 s[len - 1] = NUL;
6058 }
6059 return s;
6060}
6061
6062/*
6063 * Check the word in front of the cursor for an abbreviation.
6064 * Called when the non-id character "c" has been entered.
6065 * When an abbreviation is recognized it is removed from the text and
6066 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6067 */
6068 static int
6069echeck_abbr(c)
6070 int c;
6071{
6072 /* Don't check for abbreviation in paste mode, when disabled and just
6073 * after moving around with cursor keys. */
6074 if (p_paste || no_abbr || arrow_used)
6075 return FALSE;
6076
6077 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6078 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6079}
6080
6081/*
6082 * replace-stack functions
6083 *
6084 * When replacing characters, the replaced characters are remembered for each
6085 * new character. This is used to re-insert the old text when backspacing.
6086 *
6087 * There is a NUL headed list of characters for each character that is
6088 * currently in the file after the insertion point. When BS is used, one NUL
6089 * headed list is put back for the deleted character.
6090 *
6091 * For a newline, there are two NUL headed lists. One contains the characters
6092 * that the NL replaced. The extra one stores the characters after the cursor
6093 * that were deleted (always white space).
6094 *
6095 * Replace_offset is normally 0, in which case replace_push will add a new
6096 * character at the end of the stack. If replace_offset is not 0, that many
6097 * characters will be left on the stack above the newly inserted character.
6098 */
6099
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006100static char_u *replace_stack = NULL;
6101static long replace_stack_nr = 0; /* next entry in replace stack */
6102static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103
6104 void
6105replace_push(c)
6106 int c; /* character that is replaced (NUL is none) */
6107{
6108 char_u *p;
6109
6110 if (replace_stack_nr < replace_offset) /* nothing to do */
6111 return;
6112 if (replace_stack_len <= replace_stack_nr)
6113 {
6114 replace_stack_len += 50;
6115 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6116 if (p == NULL) /* out of memory */
6117 {
6118 replace_stack_len -= 50;
6119 return;
6120 }
6121 if (replace_stack != NULL)
6122 {
6123 mch_memmove(p, replace_stack,
6124 (size_t)(replace_stack_nr * sizeof(char_u)));
6125 vim_free(replace_stack);
6126 }
6127 replace_stack = p;
6128 }
6129 p = replace_stack + replace_stack_nr - replace_offset;
6130 if (replace_offset)
6131 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6132 *p = c;
6133 ++replace_stack_nr;
6134}
6135
6136/*
6137 * call replace_push(c) with replace_offset set to the first NUL.
6138 */
6139 static void
6140replace_push_off(c)
6141 int c;
6142{
6143 char_u *p;
6144
6145 p = replace_stack + replace_stack_nr;
6146 for (replace_offset = 1; replace_offset < replace_stack_nr;
6147 ++replace_offset)
6148 if (*--p == NUL)
6149 break;
6150 replace_push(c);
6151 replace_offset = 0;
6152}
6153
6154/*
6155 * Pop one item from the replace stack.
6156 * return -1 if stack empty
6157 * return replaced character or NUL otherwise
6158 */
6159 static int
6160replace_pop()
6161{
6162 if (replace_stack_nr == 0)
6163 return -1;
6164 return (int)replace_stack[--replace_stack_nr];
6165}
6166
6167/*
6168 * Join the top two items on the replace stack. This removes to "off"'th NUL
6169 * encountered.
6170 */
6171 static void
6172replace_join(off)
6173 int off; /* offset for which NUL to remove */
6174{
6175 int i;
6176
6177 for (i = replace_stack_nr; --i >= 0; )
6178 if (replace_stack[i] == NUL && off-- <= 0)
6179 {
6180 --replace_stack_nr;
6181 mch_memmove(replace_stack + i, replace_stack + i + 1,
6182 (size_t)(replace_stack_nr - i));
6183 return;
6184 }
6185}
6186
6187/*
6188 * Pop bytes from the replace stack until a NUL is found, and insert them
6189 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6190 */
6191 static void
6192replace_pop_ins()
6193{
6194 int cc;
6195 int oldState = State;
6196
6197 State = NORMAL; /* don't want REPLACE here */
6198 while ((cc = replace_pop()) > 0)
6199 {
6200#ifdef FEAT_MBYTE
6201 mb_replace_pop_ins(cc);
6202#else
6203 ins_char(cc);
6204#endif
6205 dec_cursor();
6206 }
6207 State = oldState;
6208}
6209
6210#ifdef FEAT_MBYTE
6211/*
6212 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6213 * indicates a multi-byte char, pop the other bytes too.
6214 */
6215 static void
6216mb_replace_pop_ins(cc)
6217 int cc;
6218{
6219 int n;
6220 char_u buf[MB_MAXBYTES];
6221 int i;
6222 int c;
6223
6224 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6225 {
6226 buf[0] = cc;
6227 for (i = 1; i < n; ++i)
6228 buf[i] = replace_pop();
6229 ins_bytes_len(buf, n);
6230 }
6231 else
6232 ins_char(cc);
6233
6234 if (enc_utf8)
6235 /* Handle composing chars. */
6236 for (;;)
6237 {
6238 c = replace_pop();
6239 if (c == -1) /* stack empty */
6240 break;
6241 if ((n = MB_BYTE2LEN(c)) == 1)
6242 {
6243 /* Not a multi-byte char, put it back. */
6244 replace_push(c);
6245 break;
6246 }
6247 else
6248 {
6249 buf[0] = c;
6250 for (i = 1; i < n; ++i)
6251 buf[i] = replace_pop();
6252 if (utf_iscomposing(utf_ptr2char(buf)))
6253 ins_bytes_len(buf, n);
6254 else
6255 {
6256 /* Not a composing char, put it back. */
6257 for (i = n - 1; i >= 0; --i)
6258 replace_push(buf[i]);
6259 break;
6260 }
6261 }
6262 }
6263}
6264#endif
6265
6266/*
6267 * make the replace stack empty
6268 * (called when exiting replace mode)
6269 */
6270 static void
6271replace_flush()
6272{
6273 vim_free(replace_stack);
6274 replace_stack = NULL;
6275 replace_stack_len = 0;
6276 replace_stack_nr = 0;
6277}
6278
6279/*
6280 * Handle doing a BS for one character.
6281 * cc < 0: replace stack empty, just move cursor
6282 * cc == 0: character was inserted, delete it
6283 * cc > 0: character was replaced, put cc (first byte of original char) back
6284 * and check for more characters to be put back
6285 */
6286 static void
6287replace_do_bs()
6288{
6289 int cc;
6290#ifdef FEAT_VREPLACE
6291 int orig_len = 0;
6292 int ins_len;
6293 int orig_vcols = 0;
6294 colnr_T start_vcol;
6295 char_u *p;
6296 int i;
6297 int vcol;
6298#endif
6299
6300 cc = replace_pop();
6301 if (cc > 0)
6302 {
6303#ifdef FEAT_VREPLACE
6304 if (State & VREPLACE_FLAG)
6305 {
6306 /* Get the number of screen cells used by the character we are
6307 * going to delete. */
6308 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6309 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6310 }
6311#endif
6312#ifdef FEAT_MBYTE
6313 if (has_mbyte)
6314 {
6315 del_char(FALSE);
6316# ifdef FEAT_VREPLACE
6317 if (State & VREPLACE_FLAG)
6318 orig_len = STRLEN(ml_get_cursor());
6319# endif
6320 replace_push(cc);
6321 }
6322 else
6323#endif
6324 {
6325 pchar_cursor(cc);
6326#ifdef FEAT_VREPLACE
6327 if (State & VREPLACE_FLAG)
6328 orig_len = STRLEN(ml_get_cursor()) - 1;
6329#endif
6330 }
6331 replace_pop_ins();
6332
6333#ifdef FEAT_VREPLACE
6334 if (State & VREPLACE_FLAG)
6335 {
6336 /* Get the number of screen cells used by the inserted characters */
6337 p = ml_get_cursor();
6338 ins_len = STRLEN(p) - orig_len;
6339 vcol = start_vcol;
6340 for (i = 0; i < ins_len; ++i)
6341 {
6342 vcol += chartabsize(p + i, vcol);
6343#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006344 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345#endif
6346 }
6347 vcol -= start_vcol;
6348
6349 /* Delete spaces that were inserted after the cursor to keep the
6350 * text aligned. */
6351 curwin->w_cursor.col += ins_len;
6352 while (vcol > orig_vcols && gchar_cursor() == ' ')
6353 {
6354 del_char(FALSE);
6355 ++orig_vcols;
6356 }
6357 curwin->w_cursor.col -= ins_len;
6358 }
6359#endif
6360
6361 /* mark the buffer as changed and prepare for displaying */
6362 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6363 }
6364 else if (cc == 0)
6365 (void)del_char(FALSE);
6366}
6367
6368#ifdef FEAT_CINDENT
6369/*
6370 * Return TRUE if C-indenting is on.
6371 */
6372 static int
6373cindent_on()
6374{
6375 return (!p_paste && (curbuf->b_p_cin
6376# ifdef FEAT_EVAL
6377 || *curbuf->b_p_inde != NUL
6378# endif
6379 ));
6380}
6381#endif
6382
6383#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6384/*
6385 * Re-indent the current line, based on the current contents of it and the
6386 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6387 * confused what all the part that handles Control-T is doing that I'm not.
6388 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6389 */
6390
6391 void
6392fixthisline(get_the_indent)
6393 int (*get_the_indent) __ARGS((void));
6394{
6395 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6396 if (linewhite(curwin->w_cursor.lnum))
6397 did_ai = TRUE; /* delete the indent if the line stays empty */
6398}
6399
6400 void
6401fix_indent()
6402{
6403 if (p_paste)
6404 return;
6405# ifdef FEAT_LISP
6406 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6407 fixthisline(get_lisp_indent);
6408# endif
6409# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6410 else
6411# endif
6412# ifdef FEAT_CINDENT
6413 if (cindent_on())
6414 do_c_expr_indent();
6415# endif
6416}
6417
6418#endif
6419
6420#ifdef FEAT_CINDENT
6421/*
6422 * return TRUE if 'cinkeys' contains the key "keytyped",
6423 * when == '*': Only if key is preceded with '*' (indent before insert)
6424 * when == '!': Only if key is prededed with '!' (don't insert)
6425 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6426 *
6427 * "keytyped" can have a few special values:
6428 * KEY_OPEN_FORW
6429 * KEY_OPEN_BACK
6430 * KEY_COMPLETE just finished completion.
6431 *
6432 * If line_is_empty is TRUE accept keys with '0' before them.
6433 */
6434 int
6435in_cinkeys(keytyped, when, line_is_empty)
6436 int keytyped;
6437 int when;
6438 int line_is_empty;
6439{
6440 char_u *look;
6441 int try_match;
6442 int try_match_word;
6443 char_u *p;
6444 char_u *line;
6445 int icase;
6446 int i;
6447
6448#ifdef FEAT_EVAL
6449 if (*curbuf->b_p_inde != NUL)
6450 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6451 else
6452#endif
6453 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6454 while (*look)
6455 {
6456 /*
6457 * Find out if we want to try a match with this key, depending on
6458 * 'when' and a '*' or '!' before the key.
6459 */
6460 switch (when)
6461 {
6462 case '*': try_match = (*look == '*'); break;
6463 case '!': try_match = (*look == '!'); break;
6464 default: try_match = (*look != '*'); break;
6465 }
6466 if (*look == '*' || *look == '!')
6467 ++look;
6468
6469 /*
6470 * If there is a '0', only accept a match if the line is empty.
6471 * But may still match when typing last char of a word.
6472 */
6473 if (*look == '0')
6474 {
6475 try_match_word = try_match;
6476 if (!line_is_empty)
6477 try_match = FALSE;
6478 ++look;
6479 }
6480 else
6481 try_match_word = FALSE;
6482
6483 /*
6484 * does it look like a control character?
6485 */
6486 if (*look == '^'
6487#ifdef EBCDIC
6488 && (Ctrl_chr(look[1]) != 0)
6489#else
6490 && look[1] >= '?' && look[1] <= '_'
6491#endif
6492 )
6493 {
6494 if (try_match && keytyped == Ctrl_chr(look[1]))
6495 return TRUE;
6496 look += 2;
6497 }
6498 /*
6499 * 'o' means "o" command, open forward.
6500 * 'O' means "O" command, open backward.
6501 */
6502 else if (*look == 'o')
6503 {
6504 if (try_match && keytyped == KEY_OPEN_FORW)
6505 return TRUE;
6506 ++look;
6507 }
6508 else if (*look == 'O')
6509 {
6510 if (try_match && keytyped == KEY_OPEN_BACK)
6511 return TRUE;
6512 ++look;
6513 }
6514
6515 /*
6516 * 'e' means to check for "else" at start of line and just before the
6517 * cursor.
6518 */
6519 else if (*look == 'e')
6520 {
6521 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6522 {
6523 p = ml_get_curline();
6524 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6525 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6526 return TRUE;
6527 }
6528 ++look;
6529 }
6530
6531 /*
6532 * ':' only causes an indent if it is at the end of a label or case
6533 * statement, or when it was before typing the ':' (to fix
6534 * class::method for C++).
6535 */
6536 else if (*look == ':')
6537 {
6538 if (try_match && keytyped == ':')
6539 {
6540 p = ml_get_curline();
6541 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6542 return TRUE;
6543 if (curwin->w_cursor.col > 2
6544 && p[curwin->w_cursor.col - 1] == ':'
6545 && p[curwin->w_cursor.col - 2] == ':')
6546 {
6547 p[curwin->w_cursor.col - 1] = ' ';
6548 i = (cin_iscase(p) || cin_isscopedecl(p)
6549 || cin_islabel(30));
6550 p = ml_get_curline();
6551 p[curwin->w_cursor.col - 1] = ':';
6552 if (i)
6553 return TRUE;
6554 }
6555 }
6556 ++look;
6557 }
6558
6559
6560 /*
6561 * Is it a key in <>, maybe?
6562 */
6563 else if (*look == '<')
6564 {
6565 if (try_match)
6566 {
6567 /*
6568 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6569 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6570 * >, *, : and ! keys if they really really want to.
6571 */
6572 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6573 && keytyped == look[1])
6574 return TRUE;
6575
6576 if (keytyped == get_special_key_code(look + 1))
6577 return TRUE;
6578 }
6579 while (*look && *look != '>')
6580 look++;
6581 while (*look == '>')
6582 look++;
6583 }
6584
6585 /*
6586 * Is it a word: "=word"?
6587 */
6588 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6589 {
6590 ++look;
6591 if (*look == '~')
6592 {
6593 icase = TRUE;
6594 ++look;
6595 }
6596 else
6597 icase = FALSE;
6598 p = vim_strchr(look, ',');
6599 if (p == NULL)
6600 p = look + STRLEN(look);
6601 if ((try_match || try_match_word)
6602 && curwin->w_cursor.col >= (colnr_T)(p - look))
6603 {
6604 int match = FALSE;
6605
6606#ifdef FEAT_INS_EXPAND
6607 if (keytyped == KEY_COMPLETE)
6608 {
6609 char_u *s;
6610
6611 /* Just completed a word, check if it starts with "look".
6612 * search back for the start of a word. */
6613 line = ml_get_curline();
6614# ifdef FEAT_MBYTE
6615 if (has_mbyte)
6616 {
6617 char_u *n;
6618
6619 for (s = line + curwin->w_cursor.col; s > line; s = n)
6620 {
6621 n = mb_prevptr(line, s);
6622 if (!vim_iswordp(n))
6623 break;
6624 }
6625 }
6626 else
6627# endif
6628 for (s = line + curwin->w_cursor.col; s > line; --s)
6629 if (!vim_iswordc(s[-1]))
6630 break;
6631 if (s + (p - look) <= line + curwin->w_cursor.col
6632 && (icase
6633 ? MB_STRNICMP(s, look, p - look)
6634 : STRNCMP(s, look, p - look)) == 0)
6635 match = TRUE;
6636 }
6637 else
6638#endif
6639 /* TODO: multi-byte */
6640 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6641 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6642 {
6643 line = ml_get_cursor();
6644 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6645 || !vim_iswordc(line[-(p - look) - 1]))
6646 && (icase
6647 ? MB_STRNICMP(line - (p - look), look, p - look)
6648 : STRNCMP(line - (p - look), look, p - look))
6649 == 0)
6650 match = TRUE;
6651 }
6652 if (match && try_match_word && !try_match)
6653 {
6654 /* "0=word": Check if there are only blanks before the
6655 * word. */
6656 line = ml_get_curline();
6657 if ((int)(skipwhite(line) - line) !=
6658 (int)(curwin->w_cursor.col - (p - look)))
6659 match = FALSE;
6660 }
6661 if (match)
6662 return TRUE;
6663 }
6664 look = p;
6665 }
6666
6667 /*
6668 * ok, it's a boring generic character.
6669 */
6670 else
6671 {
6672 if (try_match && *look == keytyped)
6673 return TRUE;
6674 ++look;
6675 }
6676
6677 /*
6678 * Skip over ", ".
6679 */
6680 look = skip_to_option_part(look);
6681 }
6682 return FALSE;
6683}
6684#endif /* FEAT_CINDENT */
6685
6686#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6687/*
6688 * Map Hebrew keyboard when in hkmap mode.
6689 */
6690 int
6691hkmap(c)
6692 int c;
6693{
6694 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6695 {
6696 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6697 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6698 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6699 static char_u map[26] =
6700 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6701 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6702 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6703 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6704 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6705 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6706 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6707 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6708 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6709
6710 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6711 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6712 /* '-1'='sofit' */
6713 else if (c == 'x')
6714 return 'X';
6715 else if (c == 'q')
6716 return '\''; /* {geresh}={'} */
6717 else if (c == 246)
6718 return ' '; /* \"o --> ' ' for a german keyboard */
6719 else if (c == 228)
6720 return ' '; /* \"a --> ' ' -- / -- */
6721 else if (c == 252)
6722 return ' '; /* \"u --> ' ' -- / -- */
6723#ifdef EBCDIC
6724 else if (islower(c))
6725#else
6726 /* NOTE: islower() does not do the right thing for us on Linux so we
6727 * do this the same was as 5.7 and previous, so it works correctly on
6728 * all systems. Specifically, the e.g. Delete and Arrow keys are
6729 * munged and won't work if e.g. searching for Hebrew text.
6730 */
6731 else if (c >= 'a' && c <= 'z')
6732#endif
6733 return (int)(map[CharOrdLow(c)] + p_aleph);
6734 else
6735 return c;
6736 }
6737 else
6738 {
6739 switch (c)
6740 {
6741 case '`': return ';';
6742 case '/': return '.';
6743 case '\'': return ',';
6744 case 'q': return '/';
6745 case 'w': return '\'';
6746
6747 /* Hebrew letters - set offset from 'a' */
6748 case ',': c = '{'; break;
6749 case '.': c = 'v'; break;
6750 case ';': c = 't'; break;
6751 default: {
6752 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6753
6754#ifdef EBCDIC
6755 /* see note about islower() above */
6756 if (!islower(c))
6757#else
6758 if (c < 'a' || c > 'z')
6759#endif
6760 return c;
6761 c = str[CharOrdLow(c)];
6762 break;
6763 }
6764 }
6765
6766 return (int)(CharOrdLow(c) + p_aleph);
6767 }
6768}
6769#endif
6770
6771 static void
6772ins_reg()
6773{
6774 int need_redraw = FALSE;
6775 int regname;
6776 int literally = 0;
6777
6778 /*
6779 * If we are going to wait for a character, show a '"'.
6780 */
6781 pc_status = PC_STATUS_UNSET;
6782 if (redrawing() && !char_avail())
6783 {
6784 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00006785 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
6787 edit_putchar('"', TRUE);
6788#ifdef FEAT_CMDL_INFO
6789 add_to_showcmd_c(Ctrl_R);
6790#endif
6791 }
6792
6793#ifdef USE_ON_FLY_SCROLL
6794 dont_scroll = TRUE; /* disallow scrolling here */
6795#endif
6796
6797 /*
6798 * Don't map the register name. This also prevents the mode message to be
6799 * deleted when ESC is hit.
6800 */
6801 ++no_mapping;
6802 regname = safe_vgetc();
6803#ifdef FEAT_LANGMAP
6804 LANGMAP_ADJUST(regname, TRUE);
6805#endif
6806 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6807 {
6808 /* Get a third key for literal register insertion */
6809 literally = regname;
6810#ifdef FEAT_CMDL_INFO
6811 add_to_showcmd_c(literally);
6812#endif
6813 regname = safe_vgetc();
6814#ifdef FEAT_LANGMAP
6815 LANGMAP_ADJUST(regname, TRUE);
6816#endif
6817 }
6818 --no_mapping;
6819
6820#ifdef FEAT_EVAL
6821 /*
6822 * Don't call u_sync() while getting the expression,
6823 * evaluating it or giving an error message for it!
6824 */
6825 ++no_u_sync;
6826 if (regname == '=')
6827 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006828# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006830# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006832# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 /* Restore the Input Method. */
6834 if (im_on)
6835 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006836# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006838 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6839 {
6840 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 else
6844 {
6845#endif
6846 if (literally == Ctrl_O || literally == Ctrl_P)
6847 {
6848 /* Append the command to the redo buffer. */
6849 AppendCharToRedobuff(Ctrl_R);
6850 AppendCharToRedobuff(literally);
6851 AppendCharToRedobuff(regname);
6852
6853 do_put(regname, BACKWARD, 1L,
6854 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6855 }
6856 else if (insert_reg(regname, literally) == FAIL)
6857 {
6858 vim_beep();
6859 need_redraw = TRUE; /* remove the '"' */
6860 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006861 else if (stop_insert_mode)
6862 /* When the '=' register was used and a function was invoked that
6863 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
6864 * insert anything, need to remove the '"' */
6865 need_redraw = TRUE;
6866
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867#ifdef FEAT_EVAL
6868 }
6869 --no_u_sync;
6870#endif
6871#ifdef FEAT_CMDL_INFO
6872 clear_showcmd();
6873#endif
6874
6875 /* If the inserted register is empty, we need to remove the '"' */
6876 if (need_redraw || stuff_empty())
6877 edit_unputchar();
6878}
6879
6880/*
6881 * CTRL-G commands in Insert mode.
6882 */
6883 static void
6884ins_ctrl_g()
6885{
6886 int c;
6887
6888#ifdef FEAT_INS_EXPAND
6889 /* Right after CTRL-X the cursor will be after the ruler. */
6890 setcursor();
6891#endif
6892
6893 /*
6894 * Don't map the second key. This also prevents the mode message to be
6895 * deleted when ESC is hit.
6896 */
6897 ++no_mapping;
6898 c = safe_vgetc();
6899 --no_mapping;
6900 switch (c)
6901 {
6902 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
6903 case K_UP:
6904 case Ctrl_K:
6905 case 'k': ins_up(TRUE);
6906 break;
6907
6908 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
6909 case K_DOWN:
6910 case Ctrl_J:
6911 case 'j': ins_down(TRUE);
6912 break;
6913
6914 /* CTRL-G u: start new undoable edit */
6915 case 'u': u_sync();
6916 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006917
6918 /* Need to reset Insstart, esp. because a BS that joins
6919 * aline to the previous one must save for undo. */
6920 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 break;
6922
6923 /* Unknown CTRL-G command, reserved for future expansion. */
6924 default: vim_beep();
6925 }
6926}
6927
6928/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006929 * CTRL-^ in Insert mode.
6930 */
6931 static void
6932ins_ctrl_hat()
6933{
6934 if (map_to_exists_mode((char_u *)"", LANGMAP))
6935 {
6936 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
6937 if (State & LANGMAP)
6938 {
6939 curbuf->b_p_iminsert = B_IMODE_NONE;
6940 State &= ~LANGMAP;
6941 }
6942 else
6943 {
6944 curbuf->b_p_iminsert = B_IMODE_LMAP;
6945 State |= LANGMAP;
6946#ifdef USE_IM_CONTROL
6947 im_set_active(FALSE);
6948#endif
6949 }
6950 }
6951#ifdef USE_IM_CONTROL
6952 else
6953 {
6954 /* There are no ":lmap" mappings, toggle IM */
6955 if (im_get_status())
6956 {
6957 curbuf->b_p_iminsert = B_IMODE_NONE;
6958 im_set_active(FALSE);
6959 }
6960 else
6961 {
6962 curbuf->b_p_iminsert = B_IMODE_IM;
6963 State &= ~LANGMAP;
6964 im_set_active(TRUE);
6965 }
6966 }
6967#endif
6968 set_iminsert_global();
6969 showmode();
6970#ifdef FEAT_GUI
6971 /* may show different cursor shape or color */
6972 if (gui.in_use)
6973 gui_update_cursor(TRUE, FALSE);
6974#endif
6975#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6976 /* Show/unshow value of 'keymap' in status lines. */
6977 status_redraw_curbuf();
6978#endif
6979}
6980
6981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 * Handle ESC in insert mode.
6983 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
6984 * insert.
6985 */
6986 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00006987ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 long *count;
6989 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00006990 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991{
6992 int temp;
6993 static int disabled_redraw = FALSE;
6994
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006995#ifdef FEAT_SYN_HL
6996 check_spell_redraw();
6997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998#if defined(FEAT_HANGULIN)
6999# if defined(ESC_CHG_TO_ENG_MODE)
7000 hangul_input_state_set(0);
7001# endif
7002 if (composing_hangul)
7003 {
7004 push_raw_key(composing_hangul_buffer, 2);
7005 composing_hangul = 0;
7006 }
7007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008
7009 temp = curwin->w_cursor.col;
7010 if (disabled_redraw)
7011 {
7012 --RedrawingDisabled;
7013 disabled_redraw = FALSE;
7014 }
7015 if (!arrow_used)
7016 {
7017 /*
7018 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007019 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7020 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 */
7022 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007023 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024
7025 /*
7026 * Repeating insert may take a long time. Check for
7027 * interrupt now and then.
7028 */
7029 if (*count > 0)
7030 {
7031 line_breakcheck();
7032 if (got_int)
7033 *count = 0;
7034 }
7035
7036 if (--*count > 0) /* repeat what was typed */
7037 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007038 /* Vi repeats the insert without replacing characters. */
7039 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7040 State &= ~REPLACE_FLAG;
7041
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 (void)start_redo_ins();
7043 if (cmdchar == 'r' || cmdchar == 'v')
7044 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7045 ++RedrawingDisabled;
7046 disabled_redraw = TRUE;
7047 return FALSE; /* repeat the insert */
7048 }
7049 stop_insert(&curwin->w_cursor, TRUE);
7050 undisplay_dollar();
7051 }
7052
7053 /* When an autoindent was removed, curswant stays after the
7054 * indent */
7055 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7056 curwin->w_set_curswant = TRUE;
7057
7058 /* Remember the last Insert position in the '^ mark. */
7059 if (!cmdmod.keepjumps)
7060 curbuf->b_last_insert = curwin->w_cursor;
7061
7062 /*
7063 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007064 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007066 if (!nomove
7067 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068#ifdef FEAT_VIRTUALEDIT
7069 || curwin->w_cursor.coladd > 0
7070#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007071 )
7072 && (restart_edit == NUL
7073 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007075 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007077 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078#ifdef FEAT_RIGHTLEFT
7079 && !revins_on
7080#endif
7081 )
7082 {
7083#ifdef FEAT_VIRTUALEDIT
7084 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7085 {
7086 oneleft();
7087 if (restart_edit != NUL)
7088 ++curwin->w_cursor.coladd;
7089 }
7090 else
7091#endif
7092 {
7093 --curwin->w_cursor.col;
7094#ifdef FEAT_MBYTE
7095 /* Correct cursor for multi-byte character. */
7096 if (has_mbyte)
7097 mb_adjust_cursor();
7098#endif
7099 }
7100 }
7101
7102#ifdef USE_IM_CONTROL
7103 /* Disable IM to allow typing English directly for Normal mode commands.
7104 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7105 * well). */
7106 if (!(State & LANGMAP))
7107 im_save_status(&curbuf->b_p_iminsert);
7108 im_set_active(FALSE);
7109#endif
7110
7111 State = NORMAL;
7112 /* need to position cursor again (e.g. when on a TAB ) */
7113 changed_cline_bef_curs();
7114
7115#ifdef FEAT_MOUSE
7116 setmouse();
7117#endif
7118#ifdef CURSOR_SHAPE
7119 ui_cursor_shape(); /* may show different cursor shape */
7120#endif
7121
7122 /*
7123 * When recording or for CTRL-O, need to display the new mode.
7124 * Otherwise remove the mode message.
7125 */
7126 if (Recording || restart_edit != NUL)
7127 showmode();
7128 else if (p_smd)
7129 MSG("");
7130
7131 return TRUE; /* exit Insert mode */
7132}
7133
7134#ifdef FEAT_RIGHTLEFT
7135/*
7136 * Toggle language: hkmap and revins_on.
7137 * Move to end of reverse inserted text.
7138 */
7139 static void
7140ins_ctrl_()
7141{
7142 if (revins_on && revins_chars && revins_scol >= 0)
7143 {
7144 while (gchar_cursor() != NUL && revins_chars--)
7145 ++curwin->w_cursor.col;
7146 }
7147 p_ri = !p_ri;
7148 revins_on = (State == INSERT && p_ri);
7149 if (revins_on)
7150 {
7151 revins_scol = curwin->w_cursor.col;
7152 revins_legal++;
7153 revins_chars = 0;
7154 undisplay_dollar();
7155 }
7156 else
7157 revins_scol = -1;
7158#ifdef FEAT_FKMAP
7159 if (p_altkeymap)
7160 {
7161 /*
7162 * to be consistent also for redo command, using '.'
7163 * set arrow_used to true and stop it - causing to redo
7164 * characters entered in one mode (normal/reverse insert).
7165 */
7166 arrow_used = TRUE;
7167 (void)stop_arrow();
7168 p_fkmap = curwin->w_p_rl ^ p_ri;
7169 if (p_fkmap && p_ri)
7170 State = INSERT;
7171 }
7172 else
7173#endif
7174 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7175 showmode();
7176}
7177#endif
7178
7179#ifdef FEAT_VISUAL
7180/*
7181 * If 'keymodel' contains "startsel", may start selection.
7182 * Returns TRUE when a CTRL-O and other keys stuffed.
7183 */
7184 static int
7185ins_start_select(c)
7186 int c;
7187{
7188 if (km_startsel)
7189 switch (c)
7190 {
7191 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 case K_PAGEUP:
7194 case K_KPAGEUP:
7195 case K_PAGEDOWN:
7196 case K_KPAGEDOWN:
7197# ifdef MACOS
7198 case K_LEFT:
7199 case K_RIGHT:
7200 case K_UP:
7201 case K_DOWN:
7202 case K_END:
7203 case K_HOME:
7204# endif
7205 if (!(mod_mask & MOD_MASK_SHIFT))
7206 break;
7207 /* FALLTHROUGH */
7208 case K_S_LEFT:
7209 case K_S_RIGHT:
7210 case K_S_UP:
7211 case K_S_DOWN:
7212 case K_S_END:
7213 case K_S_HOME:
7214 /* Start selection right away, the cursor can move with
7215 * CTRL-O when beyond the end of the line. */
7216 start_selection();
7217
7218 /* Execute the key in (insert) Select mode. */
7219 stuffcharReadbuff(Ctrl_O);
7220 if (mod_mask)
7221 {
7222 char_u buf[4];
7223
7224 buf[0] = K_SPECIAL;
7225 buf[1] = KS_MODIFIER;
7226 buf[2] = mod_mask;
7227 buf[3] = NUL;
7228 stuffReadbuff(buf);
7229 }
7230 stuffcharReadbuff(c);
7231 return TRUE;
7232 }
7233 return FALSE;
7234}
7235#endif
7236
7237/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007238 * <Insert> key in Insert mode: toggle insert/remplace mode.
7239 */
7240 static void
7241ins_insert(replaceState)
7242 int replaceState;
7243{
7244#ifdef FEAT_FKMAP
7245 if (p_fkmap && p_ri)
7246 {
7247 beep_flush();
7248 EMSG(farsi_text_3); /* encoded in Farsi */
7249 return;
7250 }
7251#endif
7252
7253#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007254# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007255 set_vim_var_string(VV_INSERTMODE,
7256 (char_u *)((State & REPLACE_FLAG) ? "i" :
7257 replaceState == VREPLACE ? "v" : "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007258# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007259 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7260#endif
7261 if (State & REPLACE_FLAG)
7262 State = INSERT | (State & LANGMAP);
7263 else
7264 State = replaceState | (State & LANGMAP);
7265 AppendCharToRedobuff(K_INS);
7266 showmode();
7267#ifdef CURSOR_SHAPE
7268 ui_cursor_shape(); /* may show different cursor shape */
7269#endif
7270}
7271
7272/*
7273 * Pressed CTRL-O in Insert mode.
7274 */
7275 static void
7276ins_ctrl_o()
7277{
7278#ifdef FEAT_VREPLACE
7279 if (State & VREPLACE_FLAG)
7280 restart_edit = 'V';
7281 else
7282#endif
7283 if (State & REPLACE_FLAG)
7284 restart_edit = 'R';
7285 else
7286 restart_edit = 'I';
7287#ifdef FEAT_VIRTUALEDIT
7288 if (virtual_active())
7289 ins_at_eol = FALSE; /* cursor always keeps its column */
7290 else
7291#endif
7292 ins_at_eol = (gchar_cursor() == NUL);
7293}
7294
7295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 * If the cursor is on an indent, ^T/^D insert/delete one
7297 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7298 * Always round the indent to 'shiftwith', this is compatible
7299 * with vi. But vi only supports ^T and ^D after an
7300 * autoindent, we support it everywhere.
7301 */
7302 static void
7303ins_shift(c, lastc)
7304 int c;
7305 int lastc;
7306{
7307 if (stop_arrow() == FAIL)
7308 return;
7309 AppendCharToRedobuff(c);
7310
7311 /*
7312 * 0^D and ^^D: remove all indent.
7313 */
7314 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7315 {
7316 --curwin->w_cursor.col;
7317 (void)del_char(FALSE); /* delete the '^' or '0' */
7318 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7319 if (State & REPLACE_FLAG)
7320 replace_pop_ins();
7321 if (lastc == '^')
7322 old_indent = get_indent(); /* remember curr. indent */
7323 change_indent(INDENT_SET, 0, TRUE, 0);
7324 }
7325 else
7326 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7327
7328 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7329 did_ai = FALSE;
7330#ifdef FEAT_SMARTINDENT
7331 did_si = FALSE;
7332 can_si = FALSE;
7333 can_si_back = FALSE;
7334#endif
7335#ifdef FEAT_CINDENT
7336 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7337#endif
7338}
7339
7340 static void
7341ins_del()
7342{
7343 int temp;
7344
7345 if (stop_arrow() == FAIL)
7346 return;
7347 if (gchar_cursor() == NUL) /* delete newline */
7348 {
7349 temp = curwin->w_cursor.col;
7350 if (!can_bs(BS_EOL) /* only if "eol" included */
7351 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7352 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7353 || do_join(FALSE) == FAIL)
7354 vim_beep();
7355 else
7356 curwin->w_cursor.col = temp;
7357 }
7358 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7359 vim_beep();
7360 did_ai = FALSE;
7361#ifdef FEAT_SMARTINDENT
7362 did_si = FALSE;
7363 can_si = FALSE;
7364 can_si_back = FALSE;
7365#endif
7366 AppendCharToRedobuff(K_DEL);
7367}
7368
7369/*
7370 * Handle Backspace, delete-word and delete-line in Insert mode.
7371 * Return TRUE when backspace was actually used.
7372 */
7373 static int
7374ins_bs(c, mode, inserted_space_p)
7375 int c;
7376 int mode;
7377 int *inserted_space_p;
7378{
7379 linenr_T lnum;
7380 int cc;
7381 int temp = 0; /* init for GCC */
7382 colnr_T mincol;
7383 int did_backspace = FALSE;
7384 int in_indent;
7385 int oldState;
7386#ifdef FEAT_MBYTE
7387 int p1, p2;
7388#endif
7389
7390 /*
7391 * can't delete anything in an empty file
7392 * can't backup past first character in buffer
7393 * can't backup past starting point unless 'backspace' > 1
7394 * can backup to a previous line if 'backspace' == 0
7395 */
7396 if ( bufempty()
7397 || (
7398#ifdef FEAT_RIGHTLEFT
7399 !revins_on &&
7400#endif
7401 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7402 || (!can_bs(BS_START)
7403 && (arrow_used
7404 || (curwin->w_cursor.lnum == Insstart.lnum
7405 && curwin->w_cursor.col <= Insstart.col)))
7406 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7407 && curwin->w_cursor.col <= ai_col)
7408 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7409 {
7410 vim_beep();
7411 return FALSE;
7412 }
7413
7414 if (stop_arrow() == FAIL)
7415 return FALSE;
7416 in_indent = inindent(0);
7417#ifdef FEAT_CINDENT
7418 if (in_indent)
7419 can_cindent = FALSE;
7420#endif
7421#ifdef FEAT_COMMENTS
7422 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7423#endif
7424#ifdef FEAT_RIGHTLEFT
7425 if (revins_on) /* put cursor after last inserted char */
7426 inc_cursor();
7427#endif
7428
7429#ifdef FEAT_VIRTUALEDIT
7430 /* Virtualedit:
7431 * BACKSPACE_CHAR eats a virtual space
7432 * BACKSPACE_WORD eats all coladd
7433 * BACKSPACE_LINE eats all coladd and keeps going
7434 */
7435 if (curwin->w_cursor.coladd > 0)
7436 {
7437 if (mode == BACKSPACE_CHAR)
7438 {
7439 --curwin->w_cursor.coladd;
7440 return TRUE;
7441 }
7442 if (mode == BACKSPACE_WORD)
7443 {
7444 curwin->w_cursor.coladd = 0;
7445 return TRUE;
7446 }
7447 curwin->w_cursor.coladd = 0;
7448 }
7449#endif
7450
7451 /*
7452 * delete newline!
7453 */
7454 if (curwin->w_cursor.col == 0)
7455 {
7456 lnum = Insstart.lnum;
7457 if (curwin->w_cursor.lnum == Insstart.lnum
7458#ifdef FEAT_RIGHTLEFT
7459 || revins_on
7460#endif
7461 )
7462 {
7463 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7464 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7465 return FALSE;
7466 --Insstart.lnum;
7467 Insstart.col = MAXCOL;
7468 }
7469 /*
7470 * In replace mode:
7471 * cc < 0: NL was inserted, delete it
7472 * cc >= 0: NL was replaced, put original characters back
7473 */
7474 cc = -1;
7475 if (State & REPLACE_FLAG)
7476 cc = replace_pop(); /* returns -1 if NL was inserted */
7477 /*
7478 * In replace mode, in the line we started replacing, we only move the
7479 * cursor.
7480 */
7481 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7482 {
7483 dec_cursor();
7484 }
7485 else
7486 {
7487#ifdef FEAT_VREPLACE
7488 if (!(State & VREPLACE_FLAG)
7489 || curwin->w_cursor.lnum > orig_line_count)
7490#endif
7491 {
7492 temp = gchar_cursor(); /* remember current char */
7493 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007494
7495 /* When "aw" is in 'formatoptions' we must delete the space at
7496 * the end of the line, otherwise the line will be broken
7497 * again when auto-formatting. */
7498 if (has_format_option(FO_AUTO)
7499 && has_format_option(FO_WHITE_PAR))
7500 {
7501 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7502 TRUE);
7503 int len;
7504
7505 len = STRLEN(ptr);
7506 if (len > 0 && ptr[len - 1] == ' ')
7507 ptr[len - 1] = NUL;
7508 }
7509
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 (void)do_join(FALSE);
7511 if (temp == NUL && gchar_cursor() != NUL)
7512 inc_cursor();
7513 }
7514#ifdef FEAT_VREPLACE
7515 else
7516 dec_cursor();
7517#endif
7518
7519 /*
7520 * In REPLACE mode we have to put back the text that was replaced
7521 * by the NL. On the replace stack is first a NUL-terminated
7522 * sequence of characters that were deleted and then the
7523 * characters that NL replaced.
7524 */
7525 if (State & REPLACE_FLAG)
7526 {
7527 /*
7528 * Do the next ins_char() in NORMAL state, to
7529 * prevent ins_char() from replacing characters and
7530 * avoiding showmatch().
7531 */
7532 oldState = State;
7533 State = NORMAL;
7534 /*
7535 * restore characters (blanks) deleted after cursor
7536 */
7537 while (cc > 0)
7538 {
7539 temp = curwin->w_cursor.col;
7540#ifdef FEAT_MBYTE
7541 mb_replace_pop_ins(cc);
7542#else
7543 ins_char(cc);
7544#endif
7545 curwin->w_cursor.col = temp;
7546 cc = replace_pop();
7547 }
7548 /* restore the characters that NL replaced */
7549 replace_pop_ins();
7550 State = oldState;
7551 }
7552 }
7553 did_ai = FALSE;
7554 }
7555 else
7556 {
7557 /*
7558 * Delete character(s) before the cursor.
7559 */
7560#ifdef FEAT_RIGHTLEFT
7561 if (revins_on) /* put cursor on last inserted char */
7562 dec_cursor();
7563#endif
7564 mincol = 0;
7565 /* keep indent */
7566 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7567#ifdef FEAT_RIGHTLEFT
7568 && !revins_on
7569#endif
7570 )
7571 {
7572 temp = curwin->w_cursor.col;
7573 beginline(BL_WHITE);
7574 if (curwin->w_cursor.col < (colnr_T)temp)
7575 mincol = curwin->w_cursor.col;
7576 curwin->w_cursor.col = temp;
7577 }
7578
7579 /*
7580 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7581 */
7582 if ( mode == BACKSPACE_CHAR
7583 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007584 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 && (*(ml_get_cursor() - 1) == TAB
7586 || (*(ml_get_cursor() - 1) == ' '
7587 && (!*inserted_space_p
7588 || arrow_used))))))
7589 {
7590 int ts;
7591 colnr_T vcol;
7592 colnr_T want_vcol;
7593 int extra = 0;
7594
7595 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007596 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 ts = curbuf->b_p_sw;
7598 else
7599 ts = curbuf->b_p_sts;
7600 /* Compute the virtual column where we want to be. Since
7601 * 'showbreak' may get in the way, need to get the last column of
7602 * the previous character. */
7603 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7604 dec_cursor();
7605 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7606 inc_cursor();
7607 want_vcol = (want_vcol / ts) * ts;
7608
7609 /* delete characters until we are at or before want_vcol */
7610 while (vcol > want_vcol
7611 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7612 {
7613 dec_cursor();
7614 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7615 if (State & REPLACE_FLAG)
7616 {
7617 /* Don't delete characters before the insert point when in
7618 * Replace mode */
7619 if (curwin->w_cursor.lnum != Insstart.lnum
7620 || curwin->w_cursor.col >= Insstart.col)
7621 {
7622#if 0 /* what was this for? It causes problems when sw != ts. */
7623 if (State == REPLACE && (int)vcol < want_vcol)
7624 {
7625 (void)del_char(FALSE);
7626 extra = 2; /* don't pop too much */
7627 }
7628 else
7629#endif
7630 replace_do_bs();
7631 }
7632 }
7633 else
7634 (void)del_char(FALSE);
7635 }
7636
7637 /* insert extra spaces until we are at want_vcol */
7638 while (vcol < want_vcol)
7639 {
7640 /* Remember the first char we inserted */
7641 if (curwin->w_cursor.lnum == Insstart.lnum
7642 && curwin->w_cursor.col < Insstart.col)
7643 Insstart.col = curwin->w_cursor.col;
7644
7645#ifdef FEAT_VREPLACE
7646 if (State & VREPLACE_FLAG)
7647 ins_char(' ');
7648 else
7649#endif
7650 {
7651 ins_str((char_u *)" ");
7652 if ((State & REPLACE_FLAG) && extra <= 1)
7653 {
7654 if (extra)
7655 replace_push_off(NUL);
7656 else
7657 replace_push(NUL);
7658 }
7659 if (extra == 2)
7660 extra = 1;
7661 }
7662 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7663 }
7664 }
7665
7666 /*
7667 * Delete upto starting point, start of line or previous word.
7668 */
7669 else do
7670 {
7671#ifdef FEAT_RIGHTLEFT
7672 if (!revins_on) /* put cursor on char to be deleted */
7673#endif
7674 dec_cursor();
7675
7676 /* start of word? */
7677 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7678 {
7679 mode = BACKSPACE_WORD_NOT_SPACE;
7680 temp = vim_iswordc(gchar_cursor());
7681 }
7682 /* end of word? */
7683 else if (mode == BACKSPACE_WORD_NOT_SPACE
7684 && (vim_isspace(cc = gchar_cursor())
7685 || vim_iswordc(cc) != temp))
7686 {
7687#ifdef FEAT_RIGHTLEFT
7688 if (!revins_on)
7689#endif
7690 inc_cursor();
7691#ifdef FEAT_RIGHTLEFT
7692 else if (State & REPLACE_FLAG)
7693 dec_cursor();
7694#endif
7695 break;
7696 }
7697 if (State & REPLACE_FLAG)
7698 replace_do_bs();
7699 else
7700 {
7701#ifdef FEAT_MBYTE
7702 if (enc_utf8 && p_deco)
7703 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7704#endif
7705 (void)del_char(FALSE);
7706#ifdef FEAT_MBYTE
7707 /*
7708 * If p1 or p2 is non-zero, there are combining characters we
7709 * need to take account of. Don't back up before the base
7710 * character.
7711 */
7712 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7713 inc_cursor();
7714#endif
7715#ifdef FEAT_RIGHTLEFT
7716 if (revins_chars)
7717 {
7718 revins_chars--;
7719 revins_legal++;
7720 }
7721 if (revins_on && gchar_cursor() == NUL)
7722 break;
7723#endif
7724 }
7725 /* Just a single backspace?: */
7726 if (mode == BACKSPACE_CHAR)
7727 break;
7728 } while (
7729#ifdef FEAT_RIGHTLEFT
7730 revins_on ||
7731#endif
7732 (curwin->w_cursor.col > mincol
7733 && (curwin->w_cursor.lnum != Insstart.lnum
7734 || curwin->w_cursor.col != Insstart.col)));
7735 did_backspace = TRUE;
7736 }
7737#ifdef FEAT_SMARTINDENT
7738 did_si = FALSE;
7739 can_si = FALSE;
7740 can_si_back = FALSE;
7741#endif
7742 if (curwin->w_cursor.col <= 1)
7743 did_ai = FALSE;
7744 /*
7745 * It's a little strange to put backspaces into the redo
7746 * buffer, but it makes auto-indent a lot easier to deal
7747 * with.
7748 */
7749 AppendCharToRedobuff(c);
7750
7751 /* If deleted before the insertion point, adjust it */
7752 if (curwin->w_cursor.lnum == Insstart.lnum
7753 && curwin->w_cursor.col < Insstart.col)
7754 Insstart.col = curwin->w_cursor.col;
7755
7756 /* vi behaviour: the cursor moves backward but the character that
7757 * was there remains visible
7758 * Vim behaviour: the cursor moves backward and the character that
7759 * was there is erased from the screen.
7760 * We can emulate the vi behaviour by pretending there is a dollar
7761 * displayed even when there isn't.
7762 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7763 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7764 dollar_vcol = curwin->w_virtcol;
7765
7766 return did_backspace;
7767}
7768
7769#ifdef FEAT_MOUSE
7770 static void
7771ins_mouse(c)
7772 int c;
7773{
7774 pos_T tpos;
7775
7776# ifdef FEAT_GUI
7777 /* When GUI is active, also move/paste when 'mouse' is empty */
7778 if (!gui.in_use)
7779# endif
7780 if (!mouse_has(MOUSE_INSERT))
7781 return;
7782
7783 undisplay_dollar();
7784 tpos = curwin->w_cursor;
7785 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7786 {
7787 start_arrow(&tpos);
7788# ifdef FEAT_CINDENT
7789 can_cindent = TRUE;
7790# endif
7791 }
7792
7793#ifdef FEAT_WINDOWS
7794 /* redraw status lines (in case another window became active) */
7795 redraw_statuslines();
7796#endif
7797}
7798
7799 static void
7800ins_mousescroll(up)
7801 int up;
7802{
7803 pos_T tpos;
7804# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7805 win_T *old_curwin;
7806# endif
7807
7808 tpos = curwin->w_cursor;
7809
7810# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7811 old_curwin = curwin;
7812
7813 /* Currently the mouse coordinates are only known in the GUI. */
7814 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7815 {
7816 int row, col;
7817
7818 row = mouse_row;
7819 col = mouse_col;
7820
7821 /* find the window at the pointer coordinates */
7822 curwin = mouse_find_win(&row, &col);
7823 curbuf = curwin->w_buffer;
7824 }
7825 if (curwin == old_curwin)
7826# endif
7827 undisplay_dollar();
7828
7829 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7830 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7831 else
7832 scroll_redraw(up, 3L);
7833
7834# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7835 curwin->w_redr_status = TRUE;
7836
7837 curwin = old_curwin;
7838 curbuf = curwin->w_buffer;
7839# endif
7840
7841 if (!equalpos(curwin->w_cursor, tpos))
7842 {
7843 start_arrow(&tpos);
7844# ifdef FEAT_CINDENT
7845 can_cindent = TRUE;
7846# endif
7847 }
7848}
7849#endif
7850
7851#ifdef FEAT_GUI
7852 void
7853ins_scroll()
7854{
7855 pos_T tpos;
7856
7857 undisplay_dollar();
7858 tpos = curwin->w_cursor;
7859 if (gui_do_scroll())
7860 {
7861 start_arrow(&tpos);
7862# ifdef FEAT_CINDENT
7863 can_cindent = TRUE;
7864# endif
7865 }
7866}
7867
7868 void
7869ins_horscroll()
7870{
7871 pos_T tpos;
7872
7873 undisplay_dollar();
7874 tpos = curwin->w_cursor;
7875 if (gui_do_horiz_scroll())
7876 {
7877 start_arrow(&tpos);
7878# ifdef FEAT_CINDENT
7879 can_cindent = TRUE;
7880# endif
7881 }
7882}
7883#endif
7884
7885 static void
7886ins_left()
7887{
7888 pos_T tpos;
7889
7890#ifdef FEAT_FOLDING
7891 if ((fdo_flags & FDO_HOR) && KeyTyped)
7892 foldOpenCursor();
7893#endif
7894 undisplay_dollar();
7895 tpos = curwin->w_cursor;
7896 if (oneleft() == OK)
7897 {
7898 start_arrow(&tpos);
7899#ifdef FEAT_RIGHTLEFT
7900 /* If exit reversed string, position is fixed */
7901 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
7902 revins_legal++;
7903 revins_chars++;
7904#endif
7905 }
7906
7907 /*
7908 * if 'whichwrap' set for cursor in insert mode may go to
7909 * previous line
7910 */
7911 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
7912 {
7913 start_arrow(&tpos);
7914 --(curwin->w_cursor.lnum);
7915 coladvance((colnr_T)MAXCOL);
7916 curwin->w_set_curswant = TRUE; /* so we stay at the end */
7917 }
7918 else
7919 vim_beep();
7920}
7921
7922 static void
7923ins_home(c)
7924 int c;
7925{
7926 pos_T tpos;
7927
7928#ifdef FEAT_FOLDING
7929 if ((fdo_flags & FDO_HOR) && KeyTyped)
7930 foldOpenCursor();
7931#endif
7932 undisplay_dollar();
7933 tpos = curwin->w_cursor;
7934 if (c == K_C_HOME)
7935 curwin->w_cursor.lnum = 1;
7936 curwin->w_cursor.col = 0;
7937#ifdef FEAT_VIRTUALEDIT
7938 curwin->w_cursor.coladd = 0;
7939#endif
7940 curwin->w_curswant = 0;
7941 start_arrow(&tpos);
7942}
7943
7944 static void
7945ins_end(c)
7946 int c;
7947{
7948 pos_T tpos;
7949
7950#ifdef FEAT_FOLDING
7951 if ((fdo_flags & FDO_HOR) && KeyTyped)
7952 foldOpenCursor();
7953#endif
7954 undisplay_dollar();
7955 tpos = curwin->w_cursor;
7956 if (c == K_C_END)
7957 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7958 coladvance((colnr_T)MAXCOL);
7959 curwin->w_curswant = MAXCOL;
7960
7961 start_arrow(&tpos);
7962}
7963
7964 static void
7965ins_s_left()
7966{
7967#ifdef FEAT_FOLDING
7968 if ((fdo_flags & FDO_HOR) && KeyTyped)
7969 foldOpenCursor();
7970#endif
7971 undisplay_dollar();
7972 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
7973 {
7974 start_arrow(&curwin->w_cursor);
7975 (void)bck_word(1L, FALSE, FALSE);
7976 curwin->w_set_curswant = TRUE;
7977 }
7978 else
7979 vim_beep();
7980}
7981
7982 static void
7983ins_right()
7984{
7985#ifdef FEAT_FOLDING
7986 if ((fdo_flags & FDO_HOR) && KeyTyped)
7987 foldOpenCursor();
7988#endif
7989 undisplay_dollar();
7990 if (gchar_cursor() != NUL || virtual_active()
7991 )
7992 {
7993 start_arrow(&curwin->w_cursor);
7994 curwin->w_set_curswant = TRUE;
7995#ifdef FEAT_VIRTUALEDIT
7996 if (virtual_active())
7997 oneright();
7998 else
7999#endif
8000 {
8001#ifdef FEAT_MBYTE
8002 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008003 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 else
8005#endif
8006 ++curwin->w_cursor.col;
8007 }
8008
8009#ifdef FEAT_RIGHTLEFT
8010 revins_legal++;
8011 if (revins_chars)
8012 revins_chars--;
8013#endif
8014 }
8015 /* if 'whichwrap' set for cursor in insert mode, may move the
8016 * cursor to the next line */
8017 else if (vim_strchr(p_ww, ']') != NULL
8018 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8019 {
8020 start_arrow(&curwin->w_cursor);
8021 curwin->w_set_curswant = TRUE;
8022 ++curwin->w_cursor.lnum;
8023 curwin->w_cursor.col = 0;
8024 }
8025 else
8026 vim_beep();
8027}
8028
8029 static void
8030ins_s_right()
8031{
8032#ifdef FEAT_FOLDING
8033 if ((fdo_flags & FDO_HOR) && KeyTyped)
8034 foldOpenCursor();
8035#endif
8036 undisplay_dollar();
8037 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8038 || gchar_cursor() != NUL)
8039 {
8040 start_arrow(&curwin->w_cursor);
8041 (void)fwd_word(1L, FALSE, 0);
8042 curwin->w_set_curswant = TRUE;
8043 }
8044 else
8045 vim_beep();
8046}
8047
8048 static void
8049ins_up(startcol)
8050 int startcol; /* when TRUE move to Insstart.col */
8051{
8052 pos_T tpos;
8053 linenr_T old_topline = curwin->w_topline;
8054#ifdef FEAT_DIFF
8055 int old_topfill = curwin->w_topfill;
8056#endif
8057
8058 undisplay_dollar();
8059 tpos = curwin->w_cursor;
8060 if (cursor_up(1L, TRUE) == OK)
8061 {
8062 if (startcol)
8063 coladvance(getvcol_nolist(&Insstart));
8064 if (old_topline != curwin->w_topline
8065#ifdef FEAT_DIFF
8066 || old_topfill != curwin->w_topfill
8067#endif
8068 )
8069 redraw_later(VALID);
8070 start_arrow(&tpos);
8071#ifdef FEAT_CINDENT
8072 can_cindent = TRUE;
8073#endif
8074 }
8075 else
8076 vim_beep();
8077}
8078
8079 static void
8080ins_pageup()
8081{
8082 pos_T tpos;
8083
8084 undisplay_dollar();
8085 tpos = curwin->w_cursor;
8086 if (onepage(BACKWARD, 1L) == OK)
8087 {
8088 start_arrow(&tpos);
8089#ifdef FEAT_CINDENT
8090 can_cindent = TRUE;
8091#endif
8092 }
8093 else
8094 vim_beep();
8095}
8096
8097 static void
8098ins_down(startcol)
8099 int startcol; /* when TRUE move to Insstart.col */
8100{
8101 pos_T tpos;
8102 linenr_T old_topline = curwin->w_topline;
8103#ifdef FEAT_DIFF
8104 int old_topfill = curwin->w_topfill;
8105#endif
8106
8107 undisplay_dollar();
8108 tpos = curwin->w_cursor;
8109 if (cursor_down(1L, TRUE) == OK)
8110 {
8111 if (startcol)
8112 coladvance(getvcol_nolist(&Insstart));
8113 if (old_topline != curwin->w_topline
8114#ifdef FEAT_DIFF
8115 || old_topfill != curwin->w_topfill
8116#endif
8117 )
8118 redraw_later(VALID);
8119 start_arrow(&tpos);
8120#ifdef FEAT_CINDENT
8121 can_cindent = TRUE;
8122#endif
8123 }
8124 else
8125 vim_beep();
8126}
8127
8128 static void
8129ins_pagedown()
8130{
8131 pos_T tpos;
8132
8133 undisplay_dollar();
8134 tpos = curwin->w_cursor;
8135 if (onepage(FORWARD, 1L) == OK)
8136 {
8137 start_arrow(&tpos);
8138#ifdef FEAT_CINDENT
8139 can_cindent = TRUE;
8140#endif
8141 }
8142 else
8143 vim_beep();
8144}
8145
8146#ifdef FEAT_DND
8147 static void
8148ins_drop()
8149{
8150 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8151}
8152#endif
8153
8154/*
8155 * Handle TAB in Insert or Replace mode.
8156 * Return TRUE when the TAB needs to be inserted like a normal character.
8157 */
8158 static int
8159ins_tab()
8160{
8161 int ind;
8162 int i;
8163 int temp;
8164
8165 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8166 Insstart_blank_vcol = get_nolist_virtcol();
8167 if (echeck_abbr(TAB + ABBR_OFF))
8168 return FALSE;
8169
8170 ind = inindent(0);
8171#ifdef FEAT_CINDENT
8172 if (ind)
8173 can_cindent = FALSE;
8174#endif
8175
8176 /*
8177 * When nothing special, insert TAB like a normal character
8178 */
8179 if (!curbuf->b_p_et
8180 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8181 && curbuf->b_p_sts == 0)
8182 return TRUE;
8183
8184 if (stop_arrow() == FAIL)
8185 return TRUE;
8186
8187 did_ai = FALSE;
8188#ifdef FEAT_SMARTINDENT
8189 did_si = FALSE;
8190 can_si = FALSE;
8191 can_si_back = FALSE;
8192#endif
8193 AppendToRedobuff((char_u *)"\t");
8194
8195 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8196 temp = (int)curbuf->b_p_sw;
8197 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8198 temp = (int)curbuf->b_p_sts;
8199 else /* otherwise use 'tabstop' */
8200 temp = (int)curbuf->b_p_ts;
8201 temp -= get_nolist_virtcol() % temp;
8202
8203 /*
8204 * Insert the first space with ins_char(). It will delete one char in
8205 * replace mode. Insert the rest with ins_str(); it will not delete any
8206 * chars. For VREPLACE mode, we use ins_char() for all characters.
8207 */
8208 ins_char(' ');
8209 while (--temp > 0)
8210 {
8211#ifdef FEAT_VREPLACE
8212 if (State & VREPLACE_FLAG)
8213 ins_char(' ');
8214 else
8215#endif
8216 {
8217 ins_str((char_u *)" ");
8218 if (State & REPLACE_FLAG) /* no char replaced */
8219 replace_push(NUL);
8220 }
8221 }
8222
8223 /*
8224 * When 'expandtab' not set: Replace spaces by TABs where possible.
8225 */
8226 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8227 {
8228 char_u *ptr;
8229#ifdef FEAT_VREPLACE
8230 char_u *saved_line = NULL; /* init for GCC */
8231 pos_T pos;
8232#endif
8233 pos_T fpos;
8234 pos_T *cursor;
8235 colnr_T want_vcol, vcol;
8236 int change_col = -1;
8237 int save_list = curwin->w_p_list;
8238
8239 /*
8240 * Get the current line. For VREPLACE mode, don't make real changes
8241 * yet, just work on a copy of the line.
8242 */
8243#ifdef FEAT_VREPLACE
8244 if (State & VREPLACE_FLAG)
8245 {
8246 pos = curwin->w_cursor;
8247 cursor = &pos;
8248 saved_line = vim_strsave(ml_get_curline());
8249 if (saved_line == NULL)
8250 return FALSE;
8251 ptr = saved_line + pos.col;
8252 }
8253 else
8254#endif
8255 {
8256 ptr = ml_get_cursor();
8257 cursor = &curwin->w_cursor;
8258 }
8259
8260 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8261 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8262 curwin->w_p_list = FALSE;
8263
8264 /* Find first white before the cursor */
8265 fpos = curwin->w_cursor;
8266 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8267 {
8268 --fpos.col;
8269 --ptr;
8270 }
8271
8272 /* In Replace mode, don't change characters before the insert point. */
8273 if ((State & REPLACE_FLAG)
8274 && fpos.lnum == Insstart.lnum
8275 && fpos.col < Insstart.col)
8276 {
8277 ptr += Insstart.col - fpos.col;
8278 fpos.col = Insstart.col;
8279 }
8280
8281 /* compute virtual column numbers of first white and cursor */
8282 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8283 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8284
8285 /* Use as many TABs as possible. Beware of 'showbreak' and
8286 * 'linebreak' adding extra virtual columns. */
8287 while (vim_iswhite(*ptr))
8288 {
8289 i = lbr_chartabsize((char_u *)"\t", vcol);
8290 if (vcol + i > want_vcol)
8291 break;
8292 if (*ptr != TAB)
8293 {
8294 *ptr = TAB;
8295 if (change_col < 0)
8296 {
8297 change_col = fpos.col; /* Column of first change */
8298 /* May have to adjust Insstart */
8299 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8300 Insstart.col = fpos.col;
8301 }
8302 }
8303 ++fpos.col;
8304 ++ptr;
8305 vcol += i;
8306 }
8307
8308 if (change_col >= 0)
8309 {
8310 int repl_off = 0;
8311
8312 /* Skip over the spaces we need. */
8313 while (vcol < want_vcol && *ptr == ' ')
8314 {
8315 vcol += lbr_chartabsize(ptr, vcol);
8316 ++ptr;
8317 ++repl_off;
8318 }
8319 if (vcol > want_vcol)
8320 {
8321 /* Must have a char with 'showbreak' just before it. */
8322 --ptr;
8323 --repl_off;
8324 }
8325 fpos.col += repl_off;
8326
8327 /* Delete following spaces. */
8328 i = cursor->col - fpos.col;
8329 if (i > 0)
8330 {
8331 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8332 /* correct replace stack. */
8333 if ((State & REPLACE_FLAG)
8334#ifdef FEAT_VREPLACE
8335 && !(State & VREPLACE_FLAG)
8336#endif
8337 )
8338 for (temp = i; --temp >= 0; )
8339 replace_join(repl_off);
8340 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008341#ifdef FEAT_NETBEANS_INTG
8342 if (usingNetbeans)
8343 {
8344 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8345 (long)(i + 1));
8346 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8347 (char_u *)"\t", 1);
8348 }
8349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 cursor->col -= i;
8351
8352#ifdef FEAT_VREPLACE
8353 /*
8354 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8355 * backspacing over the changed spacing and then inserting the new
8356 * spacing.
8357 */
8358 if (State & VREPLACE_FLAG)
8359 {
8360 /* Backspace from real cursor to change_col */
8361 backspace_until_column(change_col);
8362
8363 /* Insert each char in saved_line from changed_col to
8364 * ptr-cursor */
8365 ins_bytes_len(saved_line + change_col,
8366 cursor->col - change_col);
8367 }
8368#endif
8369 }
8370
8371#ifdef FEAT_VREPLACE
8372 if (State & VREPLACE_FLAG)
8373 vim_free(saved_line);
8374#endif
8375 curwin->w_p_list = save_list;
8376 }
8377
8378 return FALSE;
8379}
8380
8381/*
8382 * Handle CR or NL in insert mode.
8383 * Return TRUE when out of memory or can't undo.
8384 */
8385 static int
8386ins_eol(c)
8387 int c;
8388{
8389 int i;
8390
8391 if (echeck_abbr(c + ABBR_OFF))
8392 return FALSE;
8393 if (stop_arrow() == FAIL)
8394 return TRUE;
8395 undisplay_dollar();
8396
8397 /*
8398 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8399 * character under the cursor. Only push a NUL on the replace stack,
8400 * nothing to put back when the NL is deleted.
8401 */
8402 if ((State & REPLACE_FLAG)
8403#ifdef FEAT_VREPLACE
8404 && !(State & VREPLACE_FLAG)
8405#endif
8406 )
8407 replace_push(NUL);
8408
8409 /*
8410 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8411 * replacing the next line, so we push all of the characters left on the
8412 * line onto the replace stack. This is not done here though, it is done
8413 * in open_line().
8414 */
8415
8416#ifdef FEAT_RIGHTLEFT
8417# ifdef FEAT_FKMAP
8418 if (p_altkeymap && p_fkmap)
8419 fkmap(NL);
8420# endif
8421 /* NL in reverse insert will always start in the end of
8422 * current line. */
8423 if (revins_on)
8424 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8425#endif
8426
8427 AppendToRedobuff(NL_STR);
8428 i = open_line(FORWARD,
8429#ifdef FEAT_COMMENTS
8430 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8431#endif
8432 0, old_indent);
8433 old_indent = 0;
8434#ifdef FEAT_CINDENT
8435 can_cindent = TRUE;
8436#endif
8437
8438 return (!i);
8439}
8440
8441#ifdef FEAT_DIGRAPHS
8442/*
8443 * Handle digraph in insert mode.
8444 * Returns character still to be inserted, or NUL when nothing remaining to be
8445 * done.
8446 */
8447 static int
8448ins_digraph()
8449{
8450 int c;
8451 int cc;
8452
8453 pc_status = PC_STATUS_UNSET;
8454 if (redrawing() && !char_avail())
8455 {
8456 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008457 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458
8459 edit_putchar('?', TRUE);
8460#ifdef FEAT_CMDL_INFO
8461 add_to_showcmd_c(Ctrl_K);
8462#endif
8463 }
8464
8465#ifdef USE_ON_FLY_SCROLL
8466 dont_scroll = TRUE; /* disallow scrolling here */
8467#endif
8468
8469 /* don't map the digraph chars. This also prevents the
8470 * mode message to be deleted when ESC is hit */
8471 ++no_mapping;
8472 ++allow_keys;
8473 c = safe_vgetc();
8474 --no_mapping;
8475 --allow_keys;
8476 if (IS_SPECIAL(c) || mod_mask) /* special key */
8477 {
8478#ifdef FEAT_CMDL_INFO
8479 clear_showcmd();
8480#endif
8481 insert_special(c, TRUE, FALSE);
8482 return NUL;
8483 }
8484 if (c != ESC)
8485 {
8486 if (redrawing() && !char_avail())
8487 {
8488 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008489 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490
8491 if (char2cells(c) == 1)
8492 {
8493 /* first remove the '?', otherwise it's restored when typing
8494 * an ESC next */
8495 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008496 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 edit_putchar(c, TRUE);
8498 }
8499#ifdef FEAT_CMDL_INFO
8500 add_to_showcmd_c(c);
8501#endif
8502 }
8503 ++no_mapping;
8504 ++allow_keys;
8505 cc = safe_vgetc();
8506 --no_mapping;
8507 --allow_keys;
8508 if (cc != ESC)
8509 {
8510 AppendToRedobuff((char_u *)CTRL_V_STR);
8511 c = getdigraph(c, cc, TRUE);
8512#ifdef FEAT_CMDL_INFO
8513 clear_showcmd();
8514#endif
8515 return c;
8516 }
8517 }
8518 edit_unputchar();
8519#ifdef FEAT_CMDL_INFO
8520 clear_showcmd();
8521#endif
8522 return NUL;
8523}
8524#endif
8525
8526/*
8527 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8528 * Returns the char to be inserted, or NUL if none found.
8529 */
8530 static int
8531ins_copychar(lnum)
8532 linenr_T lnum;
8533{
8534 int c;
8535 int temp;
8536 char_u *ptr, *prev_ptr;
8537
8538 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8539 {
8540 vim_beep();
8541 return NUL;
8542 }
8543
8544 /* try to advance to the cursor column */
8545 temp = 0;
8546 ptr = ml_get(lnum);
8547 prev_ptr = ptr;
8548 validate_virtcol();
8549 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8550 {
8551 prev_ptr = ptr;
8552 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8553 }
8554 if ((colnr_T)temp > curwin->w_virtcol)
8555 ptr = prev_ptr;
8556
8557#ifdef FEAT_MBYTE
8558 c = (*mb_ptr2char)(ptr);
8559#else
8560 c = *ptr;
8561#endif
8562 if (c == NUL)
8563 vim_beep();
8564 return c;
8565}
8566
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008567/*
8568 * CTRL-Y or CTRL-E typed in Insert mode.
8569 */
8570 static int
8571ins_ctrl_ey(tc)
8572 int tc;
8573{
8574 int c = tc;
8575
8576#ifdef FEAT_INS_EXPAND
8577 if (ctrl_x_mode == CTRL_X_SCROLL)
8578 {
8579 if (c == Ctrl_Y)
8580 scrolldown_clamp();
8581 else
8582 scrollup_clamp();
8583 redraw_later(VALID);
8584 }
8585 else
8586#endif
8587 {
8588 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8589 if (c != NUL)
8590 {
8591 long tw_save;
8592
8593 /* The character must be taken literally, insert like it
8594 * was typed after a CTRL-V, and pretend 'textwidth'
8595 * wasn't set. Digits, 'o' and 'x' are special after a
8596 * CTRL-V, don't use it for these. */
8597 if (c < 256 && !isalnum(c))
8598 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8599 tw_save = curbuf->b_p_tw;
8600 curbuf->b_p_tw = -1;
8601 insert_special(c, TRUE, FALSE);
8602 curbuf->b_p_tw = tw_save;
8603#ifdef FEAT_RIGHTLEFT
8604 revins_chars++;
8605 revins_legal++;
8606#endif
8607 c = Ctrl_V; /* pretend CTRL-V is last character */
8608 auto_format(FALSE, TRUE);
8609 }
8610 }
8611 return c;
8612}
8613
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614#ifdef FEAT_SMARTINDENT
8615/*
8616 * Try to do some very smart auto-indenting.
8617 * Used when inserting a "normal" character.
8618 */
8619 static void
8620ins_try_si(c)
8621 int c;
8622{
8623 pos_T *pos, old_pos;
8624 char_u *ptr;
8625 int i;
8626 int temp;
8627
8628 /*
8629 * do some very smart indenting when entering '{' or '}'
8630 */
8631 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8632 {
8633 /*
8634 * for '}' set indent equal to indent of line containing matching '{'
8635 */
8636 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8637 {
8638 old_pos = curwin->w_cursor;
8639 /*
8640 * If the matching '{' has a ')' immediately before it (ignoring
8641 * white-space), then line up with the start of the line
8642 * containing the matching '(' if there is one. This handles the
8643 * case where an "if (..\n..) {" statement continues over multiple
8644 * lines -- webb
8645 */
8646 ptr = ml_get(pos->lnum);
8647 i = pos->col;
8648 if (i > 0) /* skip blanks before '{' */
8649 while (--i > 0 && vim_iswhite(ptr[i]))
8650 ;
8651 curwin->w_cursor.lnum = pos->lnum;
8652 curwin->w_cursor.col = i;
8653 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8654 curwin->w_cursor = *pos;
8655 i = get_indent();
8656 curwin->w_cursor = old_pos;
8657#ifdef FEAT_VREPLACE
8658 if (State & VREPLACE_FLAG)
8659 change_indent(INDENT_SET, i, FALSE, NUL);
8660 else
8661#endif
8662 (void)set_indent(i, SIN_CHANGED);
8663 }
8664 else if (curwin->w_cursor.col > 0)
8665 {
8666 /*
8667 * when inserting '{' after "O" reduce indent, but not
8668 * more than indent of previous line
8669 */
8670 temp = TRUE;
8671 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8672 {
8673 old_pos = curwin->w_cursor;
8674 i = get_indent();
8675 while (curwin->w_cursor.lnum > 1)
8676 {
8677 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8678
8679 /* ignore empty lines and lines starting with '#'. */
8680 if (*ptr != '#' && *ptr != NUL)
8681 break;
8682 }
8683 if (get_indent() >= i)
8684 temp = FALSE;
8685 curwin->w_cursor = old_pos;
8686 }
8687 if (temp)
8688 shift_line(TRUE, FALSE, 1);
8689 }
8690 }
8691
8692 /*
8693 * set indent of '#' always to 0
8694 */
8695 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8696 {
8697 /* remember current indent for next line */
8698 old_indent = get_indent();
8699 (void)set_indent(0, SIN_CHANGED);
8700 }
8701
8702 /* Adjust ai_col, the char at this position can be deleted. */
8703 if (ai_col > curwin->w_cursor.col)
8704 ai_col = curwin->w_cursor.col;
8705}
8706#endif
8707
8708/*
8709 * Get the value that w_virtcol would have when 'list' is off.
8710 * Unless 'cpo' contains the 'L' flag.
8711 */
8712 static colnr_T
8713get_nolist_virtcol()
8714{
8715 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8716 return getvcol_nolist(&curwin->w_cursor);
8717 validate_virtcol();
8718 return curwin->w_virtcol;
8719}