blob: 5809df1bbf0056b17687ab1c1f90814746ff343a [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 Moolenaard1f56e62006-02-22 21:25:37 +000065typedef struct compl_S compl_T;
66struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000067{
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 Moolenaard1f56e62006-02-22 21:25:37 +000071 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000072 char_u *cp_extra; /* extra menu text (allocated, can be NULL) */
73 char_u *cp_info; /* verbose info (can be NULL) */
74 char_u cp_kind; /* kind of match, single letter, or NUL */
75 char_u *cp_fname; /* file containing the match, allocated when
76 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000077 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
78 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000079};
80
Bram Moolenaar572cb562005-08-05 21:35:02 +000081#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#define FREE_FNAME (2)
83
84/*
85 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000086 * "compl_first_match" points to the start of the list.
87 * "compl_curr_match" points to the currently selected entry.
88 * "compl_shown_match" is different from compl_curr_match during
89 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000090 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000091static compl_T *compl_first_match = NULL;
92static compl_T *compl_curr_match = NULL;
93static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
Bram Moolenaara6557602006-02-04 22:43:20 +000095/* When "compl_leader" is not NULL only matches that start with this string
96 * are used. */
97static char_u *compl_leader = NULL;
98
Bram Moolenaarc7453f52006-02-10 23:20:28 +000099static int compl_get_longest = FALSE; /* put longest common string
100 in compl_leader */
101
Bram Moolenaara6557602006-02-04 22:43:20 +0000102static int compl_used_match; /* Selected one of the matches. When
103 FALSE the match was edited or using
104 the longest common string. */
105
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000106/* When the first completion is done "compl_started" is set. When it's
107 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000108static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000109
Bram Moolenaar572cb562005-08-05 21:35:02 +0000110static int compl_matches = 0;
111static char_u *compl_pattern = NULL;
112static int compl_direction = FORWARD;
113static int compl_shows_dir = FORWARD;
114static int compl_pending = FALSE;
115static pos_T compl_startpos;
116static colnr_T compl_col = 0; /* column where the text starts
117 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000118static 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 Moolenaard1f56e62006-02-22 21:25:37 +0000125static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000126static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000127static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000129static void ins_compl_upd_pum __ARGS((void));
130static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000131static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000132static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000133static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000134static void ins_compl_files __ARGS((int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000135static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136static void ins_compl_free __ARGS((void));
137static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000138static int ins_compl_bs __ARGS((void));
139static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000140static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000141static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000142static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000144static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145static void ins_compl_delete __ARGS((void));
146static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000147static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000148static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000149static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000150static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000151static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152static int ins_complete __ARGS((int c));
153static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
154#endif /* FEAT_INS_EXPAND */
155
156#define BACKSPACE_CHAR 1
157#define BACKSPACE_WORD 2
158#define BACKSPACE_WORD_NOT_SPACE 3
159#define BACKSPACE_LINE 4
160
Bram Moolenaar754b5602006-02-09 23:53:20 +0000161static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162static void ins_ctrl_v __ARGS((void));
163static void undisplay_dollar __ARGS((void));
164static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000165static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166static void check_auto_format __ARGS((int));
167static void redo_literal __ARGS((int c));
168static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000169#ifdef FEAT_SYN_HL
170static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000171static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000172static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
175static int echeck_abbr __ARGS((int));
176static void replace_push_off __ARGS((int c));
177static int replace_pop __ARGS((void));
178static void replace_join __ARGS((int off));
179static void replace_pop_ins __ARGS((void));
180#ifdef FEAT_MBYTE
181static void mb_replace_pop_ins __ARGS((int cc));
182#endif
183static void replace_flush __ARGS((void));
184static void replace_do_bs __ARGS((void));
185#ifdef FEAT_CINDENT
186static int cindent_on __ARGS((void));
187#endif
188static void ins_reg __ARGS((void));
189static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000190static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000191static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192#ifdef FEAT_RIGHTLEFT
193static void ins_ctrl_ __ARGS((void));
194#endif
195#ifdef FEAT_VISUAL
196static int ins_start_select __ARGS((int c));
197#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000198static void ins_insert __ARGS((int replaceState));
199static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200static void ins_shift __ARGS((int c, int lastc));
201static void ins_del __ARGS((void));
202static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
203#ifdef FEAT_MOUSE
204static void ins_mouse __ARGS((int c));
205static void ins_mousescroll __ARGS((int up));
206#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000207#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
208static void ins_tabline __ARGS((int c));
209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210static void ins_left __ARGS((void));
211static void ins_home __ARGS((int c));
212static void ins_end __ARGS((int c));
213static void ins_s_left __ARGS((void));
214static void ins_right __ARGS((void));
215static void ins_s_right __ARGS((void));
216static void ins_up __ARGS((int startcol));
217static void ins_pageup __ARGS((void));
218static void ins_down __ARGS((int startcol));
219static void ins_pagedown __ARGS((void));
220#ifdef FEAT_DND
221static void ins_drop __ARGS((void));
222#endif
223static int ins_tab __ARGS((void));
224static int ins_eol __ARGS((int c));
225#ifdef FEAT_DIGRAPHS
226static int ins_digraph __ARGS((void));
227#endif
228static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000229static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#ifdef FEAT_SMARTINDENT
231static void ins_try_si __ARGS((int c));
232#endif
233static colnr_T get_nolist_virtcol __ARGS((void));
234
235static colnr_T Insstart_textlen; /* length of line when insert started */
236static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
237
238static char_u *last_insert = NULL; /* the text of the previous insert,
239 K_SPECIAL and CSI are escaped */
240static int last_insert_skip; /* nr of chars in front of previous insert */
241static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000242static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243
244#ifdef FEAT_CINDENT
245static int can_cindent; /* may do cindenting on this line */
246#endif
247
248static int old_indent = 0; /* for ^^D command in insert mode */
249
250#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000251static int revins_on; /* reverse insert mode on */
252static int revins_chars; /* how much to skip after edit */
253static int revins_legal; /* was the last char 'legal'? */
254static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255#endif
256
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257static int ins_need_undo; /* call u_save() before inserting a
258 char. Set when edit() is called.
259 after that arrow_used is used. */
260
261static int did_add_space = FALSE; /* auto_format() added an extra space
262 under the cursor */
263
264/*
265 * edit(): Start inserting text.
266 *
267 * "cmdchar" can be:
268 * 'i' normal insert command
269 * 'a' normal append command
270 * 'R' replace command
271 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
272 * but still only one <CR> is inserted. The <Esc> is not used for redo.
273 * 'g' "gI" command.
274 * 'V' "gR" command for Virtual Replace mode.
275 * 'v' "gr" command for single character Virtual Replace mode.
276 *
277 * This function is not called recursively. For CTRL-O commands, it returns
278 * and lets the caller handle the Normal-mode command.
279 *
280 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
281 */
282 int
283edit(cmdchar, startln, count)
284 int cmdchar;
285 int startln; /* if set, insert at start of line */
286 long count;
287{
288 int c = 0;
289 char_u *ptr;
290 int lastc;
291 colnr_T mincol;
292 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 int i;
294 int did_backspace = TRUE; /* previous char was backspace */
295#ifdef FEAT_CINDENT
296 int line_is_white = FALSE; /* line is empty before insert */
297#endif
298 linenr_T old_topline = 0; /* topline before insertion */
299#ifdef FEAT_DIFF
300 int old_topfill = -1;
301#endif
302 int inserted_space = FALSE; /* just inserted a space */
303 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000304 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000306 /* Remember whether editing was restarted after CTRL-O. */
307 did_restart_edit = restart_edit;
308
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 /* sleep before redrawing, needed for "CTRL-O :" that results in an
310 * error message */
311 check_for_delay(TRUE);
312
313#ifdef HAVE_SANDBOX
314 /* Don't allow inserting in the sandbox. */
315 if (sandbox != 0)
316 {
317 EMSG(_(e_sandbox));
318 return FALSE;
319 }
320#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000321 /* Don't allow changes in the buffer while editing the cmdline. The
322 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000323 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000324 {
325 EMSG(_(e_secure));
326 return FALSE;
327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328
329#ifdef FEAT_INS_EXPAND
330 ins_compl_clear(); /* clear stuff for CTRL-X mode */
331#endif
332
Bram Moolenaar843ee412004-06-30 16:16:41 +0000333#ifdef FEAT_AUTOCMD
334 /*
335 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
336 */
337 if (cmdchar != 'r' && cmdchar != 'v')
338 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000339# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000340 if (cmdchar == 'R')
341 ptr = (char_u *)"r";
342 else if (cmdchar == 'V')
343 ptr = (char_u *)"v";
344 else
345 ptr = (char_u *)"i";
346 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000347# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000348 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
349 }
350#endif
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#ifdef FEAT_MOUSE
353 /*
354 * When doing a paste with the middle mouse button, Insstart is set to
355 * where the paste started.
356 */
357 if (where_paste_started.lnum != 0)
358 Insstart = where_paste_started;
359 else
360#endif
361 {
362 Insstart = curwin->w_cursor;
363 if (startln)
364 Insstart.col = 0;
365 }
366 Insstart_textlen = linetabsize(ml_get_curline());
367 Insstart_blank_vcol = MAXCOL;
368 if (!did_ai)
369 ai_col = 0;
370
371 if (cmdchar != NUL && restart_edit == 0)
372 {
373 ResetRedobuff();
374 AppendNumberToRedobuff(count);
375#ifdef FEAT_VREPLACE
376 if (cmdchar == 'V' || cmdchar == 'v')
377 {
378 /* "gR" or "gr" command */
379 AppendCharToRedobuff('g');
380 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
381 }
382 else
383#endif
384 {
385 AppendCharToRedobuff(cmdchar);
386 if (cmdchar == 'g') /* "gI" command */
387 AppendCharToRedobuff('I');
388 else if (cmdchar == 'r') /* "r<CR>" command */
389 count = 1; /* insert only one <CR> */
390 }
391 }
392
393 if (cmdchar == 'R')
394 {
395#ifdef FEAT_FKMAP
396 if (p_fkmap && p_ri)
397 {
398 beep_flush();
399 EMSG(farsi_text_3); /* encoded in Farsi */
400 State = INSERT;
401 }
402 else
403#endif
404 State = REPLACE;
405 }
406#ifdef FEAT_VREPLACE
407 else if (cmdchar == 'V' || cmdchar == 'v')
408 {
409 State = VREPLACE;
410 replaceState = VREPLACE;
411 orig_line_count = curbuf->b_ml.ml_line_count;
412 vr_lines_changed = 1;
413 }
414#endif
415 else
416 State = INSERT;
417
418 stop_insert_mode = FALSE;
419
420 /*
421 * Need to recompute the cursor position, it might move when the cursor is
422 * on a TAB or special character.
423 */
424 curs_columns(TRUE);
425
426 /*
427 * Enable langmap or IME, indicated by 'iminsert'.
428 * Note that IME may enabled/disabled without us noticing here, thus the
429 * 'iminsert' value may not reflect what is actually used. It is updated
430 * when hitting <Esc>.
431 */
432 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
433 State |= LANGMAP;
434#ifdef USE_IM_CONTROL
435 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
436#endif
437
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438#ifdef FEAT_MOUSE
439 setmouse();
440#endif
441#ifdef FEAT_CMDL_INFO
442 clear_showcmd();
443#endif
444#ifdef FEAT_RIGHTLEFT
445 /* there is no reverse replace mode */
446 revins_on = (State == INSERT && p_ri);
447 if (revins_on)
448 undisplay_dollar();
449 revins_chars = 0;
450 revins_legal = 0;
451 revins_scol = -1;
452#endif
453
454 /*
455 * Handle restarting Insert mode.
456 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
457 * restart_edit non-zero, and something in the stuff buffer.
458 */
459 if (restart_edit != 0 && stuff_empty())
460 {
461#ifdef FEAT_MOUSE
462 /*
463 * After a paste we consider text typed to be part of the insert for
464 * the pasted text. You can backspace over the pasted text too.
465 */
466 if (where_paste_started.lnum)
467 arrow_used = FALSE;
468 else
469#endif
470 arrow_used = TRUE;
471 restart_edit = 0;
472
473 /*
474 * If the cursor was after the end-of-line before the CTRL-O and it is
475 * now at the end-of-line, put it after the end-of-line (this is not
476 * correct in very rare cases).
477 * Also do this if curswant is greater than the current virtual
478 * column. Eg after "^O$" or "^O80|".
479 */
480 validate_virtcol();
481 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000482 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 || curwin->w_curswant > curwin->w_virtcol)
484 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
485 {
486 if (ptr[1] == NUL)
487 ++curwin->w_cursor.col;
488#ifdef FEAT_MBYTE
489 else if (has_mbyte)
490 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000491 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 if (ptr[i] == NUL)
493 curwin->w_cursor.col += i;
494 }
495#endif
496 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000497 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 }
499 else
500 arrow_used = FALSE;
501
502 /* we are in insert mode now, don't need to start it anymore */
503 need_start_insertmode = FALSE;
504
505 /* Need to save the line for undo before inserting the first char. */
506 ins_need_undo = TRUE;
507
508#ifdef FEAT_MOUSE
509 where_paste_started.lnum = 0;
510#endif
511#ifdef FEAT_CINDENT
512 can_cindent = TRUE;
513#endif
514#ifdef FEAT_FOLDING
515 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
516 * restarting. */
517 if (!p_im && did_restart_edit == 0)
518 foldOpenCursor();
519#endif
520
521 /*
522 * If 'showmode' is set, show the current (insert/replace/..) mode.
523 * A warning message for changing a readonly file is given here, before
524 * actually changing anything. It's put after the mode, if any.
525 */
526 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000527 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 i = showmode();
529
530 if (!p_im && did_restart_edit == 0)
531 change_warning(i + 1);
532
533#ifdef CURSOR_SHAPE
534 ui_cursor_shape(); /* may show different cursor shape */
535#endif
536#ifdef FEAT_DIGRAPHS
537 do_digraph(-1); /* clear digraphs */
538#endif
539
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000540 /*
541 * Get the current length of the redo buffer, those characters have to be
542 * skipped if we want to get to the inserted characters.
543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 ptr = get_inserted();
545 if (ptr == NULL)
546 new_insert_skip = 0;
547 else
548 {
549 new_insert_skip = (int)STRLEN(ptr);
550 vim_free(ptr);
551 }
552
553 old_indent = 0;
554
555 /*
556 * Main loop in Insert mode: repeat until Insert mode is left.
557 */
558 for (;;)
559 {
560#ifdef FEAT_RIGHTLEFT
561 if (!revins_legal)
562 revins_scol = -1; /* reset on illegal motions */
563 else
564 revins_legal = 0;
565#endif
566 if (arrow_used) /* don't repeat insert when arrow key used */
567 count = 0;
568
569 if (stop_insert_mode)
570 {
571 /* ":stopinsert" used or 'insertmode' reset */
572 count = 0;
573 goto doESCkey;
574 }
575
576 /* set curwin->w_curswant for next K_DOWN or K_UP */
577 if (!arrow_used)
578 curwin->w_set_curswant = TRUE;
579
580 /* If there is no typeahead may check for timestamps (e.g., for when a
581 * menu invoked a shell command). */
582 if (stuff_empty())
583 {
584 did_check_timestamps = FALSE;
585 if (need_check_timestamps)
586 check_timestamps(FALSE);
587 }
588
589 /*
590 * When emsg() was called msg_scroll will have been set.
591 */
592 msg_scroll = FALSE;
593
594#ifdef FEAT_GUI
595 /* When 'mousefocus' is set a mouse movement may have taken us to
596 * another window. "need_mouse_correct" may then be set because of an
597 * autocommand. */
598 if (need_mouse_correct)
599 gui_mouse_correct();
600#endif
601
602#ifdef FEAT_FOLDING
603 /* Open fold at the cursor line, according to 'foldopen'. */
604 if (fdo_flags & FDO_INSERT)
605 foldOpenCursor();
606 /* Close folds where the cursor isn't, according to 'foldclose' */
607 if (!char_avail())
608 foldCheckClose();
609#endif
610
611 /*
612 * If we inserted a character at the last position of the last line in
613 * the window, scroll the window one line up. This avoids an extra
614 * redraw.
615 * This is detected when the cursor column is smaller after inserting
616 * something.
617 * Don't do this when the topline changed already, it has
618 * already been adjusted (by insertchar() calling open_line())).
619 */
620 if (curbuf->b_mod_set
621 && curwin->w_p_wrap
622 && !did_backspace
623 && curwin->w_topline == old_topline
624#ifdef FEAT_DIFF
625 && curwin->w_topfill == old_topfill
626#endif
627 )
628 {
629 mincol = curwin->w_wcol;
630 validate_cursor_col();
631
632 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
633 && curwin->w_wrow == W_WINROW(curwin)
634 + curwin->w_height - 1 - p_so
635 && (curwin->w_cursor.lnum != curwin->w_topline
636#ifdef FEAT_DIFF
637 || curwin->w_topfill > 0
638#endif
639 ))
640 {
641#ifdef FEAT_DIFF
642 if (curwin->w_topfill > 0)
643 --curwin->w_topfill;
644 else
645#endif
646#ifdef FEAT_FOLDING
647 if (hasFolding(curwin->w_topline, NULL, &old_topline))
648 set_topline(curwin, old_topline + 1);
649 else
650#endif
651 set_topline(curwin, curwin->w_topline + 1);
652 }
653 }
654
655 /* May need to adjust w_topline to show the cursor. */
656 update_topline();
657
658 did_backspace = FALSE;
659
660 validate_cursor(); /* may set must_redraw */
661
662 /*
663 * Redraw the display when no characters are waiting.
664 * Also shows mode, ruler and positions cursor.
665 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000666 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
668#ifdef FEAT_SCROLLBIND
669 if (curwin->w_p_scb)
670 do_check_scrollbind(TRUE);
671#endif
672
673 update_curswant();
674 old_topline = curwin->w_topline;
675#ifdef FEAT_DIFF
676 old_topfill = curwin->w_topfill;
677#endif
678
679#ifdef USE_ON_FLY_SCROLL
680 dont_scroll = FALSE; /* allow scrolling here */
681#endif
682
683 /*
684 * Get a character for Insert mode.
685 */
686 lastc = c; /* remember previous char for CTRL-D */
687 c = safe_vgetc();
688
689#ifdef FEAT_RIGHTLEFT
690 if (p_hkmap && KeyTyped)
691 c = hkmap(c); /* Hebrew mode mapping */
692#endif
693#ifdef FEAT_FKMAP
694 if (p_fkmap && KeyTyped)
695 c = fkmap(c); /* Farsi mode mapping */
696#endif
697
698#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000699 /*
700 * Special handling of keys while the popup menu is visible or wanted
701 * and the cursor is still in the completed word.
702 */
703 if (compl_started && pum_wanted() && curwin->w_cursor.col >= compl_col)
Bram Moolenaara6557602006-02-04 22:43:20 +0000704 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000705 /* BS: Delete one character from "compl_leader". */
706 if ((c == K_BS || c == Ctrl_H)
707 && curwin->w_cursor.col > compl_col && ins_compl_bs())
Bram Moolenaara6557602006-02-04 22:43:20 +0000708 continue;
709
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000710 /* When no match was selected or it was edited. */
711 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000712 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000713 /* CTRL-L: Add one character from the current match to
714 * "compl_leader". */
715 if (c == Ctrl_L)
716 {
717 ins_compl_addfrommatch();
718 continue;
719 }
720
Bram Moolenaardf1bdc92006-02-23 21:32:16 +0000721 /* A printable, non-white character: Add to "compl_leader". */
722 if (vim_isprintc(c) && !vim_iswhite(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000723 {
724 ins_compl_addleader(c);
725 continue;
726 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000727
728 /* Pressing Enter selects the current match. */
729 if (c == CAR || c == K_KENTER || c == NL)
730 {
731 ins_compl_delete();
732 ins_compl_insert();
733 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000734 }
735 }
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
738 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000739 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000740 if (c != K_IGNORE && ins_compl_prep(c))
741 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742#endif
743
Bram Moolenaar488c6512005-08-11 20:09:58 +0000744 /* CTRL-\ CTRL-N goes to Normal mode,
745 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
746 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 if (c == Ctrl_BSL)
748 {
749 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000750 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 ++no_mapping;
752 ++allow_keys;
753 c = safe_vgetc();
754 --no_mapping;
755 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000756 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000758 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 vungetc(c);
760 c = Ctrl_BSL;
761 }
762 else if (c == Ctrl_G && p_im)
763 continue;
764 else
765 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000766 if (c == Ctrl_O)
767 {
768 ins_ctrl_o();
769 ins_at_eol = FALSE; /* cursor keeps its column */
770 nomove = TRUE;
771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 count = 0;
773 goto doESCkey;
774 }
775 }
776
777#ifdef FEAT_DIGRAPHS
778 c = do_digraph(c);
779#endif
780
781#ifdef FEAT_INS_EXPAND
782 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
783 goto docomplete;
784#endif
785 if (c == Ctrl_V || c == Ctrl_Q)
786 {
787 ins_ctrl_v();
788 c = Ctrl_V; /* pretend CTRL-V is last typed character */
789 continue;
790 }
791
792#ifdef FEAT_CINDENT
793 if (cindent_on()
794# ifdef FEAT_INS_EXPAND
795 && ctrl_x_mode == 0
796# endif
797 )
798 {
799 /* A key name preceded by a bang means this key is not to be
800 * inserted. Skip ahead to the re-indenting below.
801 * A key name preceded by a star means that indenting has to be
802 * done before inserting the key. */
803 line_is_white = inindent(0);
804 if (in_cinkeys(c, '!', line_is_white))
805 goto force_cindent;
806 if (can_cindent && in_cinkeys(c, '*', line_is_white)
807 && stop_arrow() == OK)
808 do_c_expr_indent();
809 }
810#endif
811
812#ifdef FEAT_RIGHTLEFT
813 if (curwin->w_p_rl)
814 switch (c)
815 {
816 case K_LEFT: c = K_RIGHT; break;
817 case K_S_LEFT: c = K_S_RIGHT; break;
818 case K_C_LEFT: c = K_C_RIGHT; break;
819 case K_RIGHT: c = K_LEFT; break;
820 case K_S_RIGHT: c = K_S_LEFT; break;
821 case K_C_RIGHT: c = K_C_LEFT; break;
822 }
823#endif
824
825#ifdef FEAT_VISUAL
826 /*
827 * If 'keymodel' contains "startsel", may start selection. If it
828 * does, a CTRL-O and c will be stuffed, we need to get these
829 * characters.
830 */
831 if (ins_start_select(c))
832 continue;
833#endif
834
835 /*
836 * The big switch to handle a character in insert mode.
837 */
838 switch (c)
839 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000840 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 if (echeck_abbr(ESC + ABBR_OFF))
842 break;
843 /*FALLTHROUGH*/
844
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000845 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846#ifdef FEAT_CMDWIN
847 if (c == Ctrl_C && cmdwin_type != 0)
848 {
849 /* Close the cmdline window. */
850 cmdwin_result = K_IGNORE;
851 got_int = FALSE; /* don't stop executing autocommands et al. */
852 goto doESCkey;
853 }
854#endif
855
856#ifdef UNIX
857do_intr:
858#endif
859 /* when 'insertmode' set, and not halfway a mapping, don't leave
860 * Insert mode */
861 if (goto_im())
862 {
863 if (got_int)
864 {
865 (void)vgetc(); /* flush all buffers */
866 got_int = FALSE;
867 }
868 else
869 vim_beep();
870 break;
871 }
872doESCkey:
873 /*
874 * This is the ONLY return from edit()!
875 */
876 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
877 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000878 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 o_lnum = curwin->w_cursor.lnum;
880
Bram Moolenaar488c6512005-08-11 20:09:58 +0000881 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000882 {
883#ifdef FEAT_AUTOCMD
884 if (cmdchar != 'r' && cmdchar != 'v')
885 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
886 FALSE, curbuf);
887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 continue;
891
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000892 case Ctrl_Z: /* suspend when 'insertmode' set */
893 if (!p_im)
894 goto normalchar; /* insert CTRL-Z as normal char */
895 stuffReadbuff((char_u *)":st\r");
896 c = Ctrl_O;
897 /*FALLTHROUGH*/
898
899 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000900#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000901 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000902 goto docomplete;
903#endif
904 if (echeck_abbr(Ctrl_O + ABBR_OFF))
905 break;
906 ins_ctrl_o();
907 count = 0;
908 goto doESCkey;
909
Bram Moolenaar572cb562005-08-05 21:35:02 +0000910 case K_INS: /* toggle insert/replace mode */
911 case K_KINS:
912 ins_insert(replaceState);
913 break;
914
915 case K_SELECT: /* end of Select mode mapping - ignore */
916 break;
917
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000918#ifdef FEAT_SNIFF
919 case K_SNIFF: /* Sniff command received */
920 stuffcharReadbuff(K_SNIFF);
921 goto doESCkey;
922#endif
923
924 case K_HELP: /* Help key works like <ESC> <Help> */
925 case K_F1:
926 case K_XF1:
927 stuffcharReadbuff(K_HELP);
928 if (p_im)
929 need_start_insertmode = TRUE;
930 goto doESCkey;
931
932#ifdef FEAT_NETBEANS_INTG
933 case K_F21: /* NetBeans command */
934 ++no_mapping; /* don't map the next key hits */
935 i = safe_vgetc();
936 --no_mapping;
937 netbeans_keycommand(i);
938 break;
939#endif
940
941 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 case NUL:
943 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000944 /* For ^@ the trailing ESC will end the insert, unless there is an
945 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
947 && c != Ctrl_A && !p_im)
948 goto doESCkey; /* quit insert mode */
949 inserted_space = FALSE;
950 break;
951
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000952 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 ins_reg();
954 auto_format(FALSE, TRUE);
955 inserted_space = FALSE;
956 break;
957
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000958 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 ins_ctrl_g();
960 break;
961
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000962 case Ctrl_HAT: /* switch input mode and/or langmap */
963 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 break;
965
966#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000967 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 if (!p_ari)
969 goto normalchar;
970 ins_ctrl_();
971 break;
972#endif
973
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000974 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
976 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
977 goto docomplete;
978#endif
979 /* FALLTHROUGH */
980
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000981 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982# ifdef FEAT_INS_EXPAND
983 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
984 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000985 if (has_compl_option(FALSE))
986 goto docomplete;
987 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
989# endif
990 ins_shift(c, lastc);
991 auto_format(FALSE, TRUE);
992 inserted_space = FALSE;
993 break;
994
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000995 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 case K_KDEL:
997 ins_del();
998 auto_format(FALSE, TRUE);
999 break;
1000
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001001 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 case Ctrl_H:
1003 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1004 auto_format(FALSE, TRUE);
1005 break;
1006
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001007 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1009 auto_format(FALSE, TRUE);
1010 break;
1011
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001012 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001013# ifdef FEAT_COMPL_FUNC
1014 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001015 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001016 goto docomplete;
1017# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1019 auto_format(FALSE, TRUE);
1020 inserted_space = FALSE;
1021 break;
1022
1023#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001024 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 case K_LEFTMOUSE_NM:
1026 case K_LEFTDRAG:
1027 case K_LEFTRELEASE:
1028 case K_LEFTRELEASE_NM:
1029 case K_MIDDLEMOUSE:
1030 case K_MIDDLEDRAG:
1031 case K_MIDDLERELEASE:
1032 case K_RIGHTMOUSE:
1033 case K_RIGHTDRAG:
1034 case K_RIGHTRELEASE:
1035 case K_X1MOUSE:
1036 case K_X1DRAG:
1037 case K_X1RELEASE:
1038 case K_X2MOUSE:
1039 case K_X2DRAG:
1040 case K_X2RELEASE:
1041 ins_mouse(c);
1042 break;
1043
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001044 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 ins_mousescroll(FALSE);
1046 break;
1047
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001048 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 ins_mousescroll(TRUE);
1050 break;
1051#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001052#ifdef FEAT_GUI_TABLINE
1053 case K_TABLINE:
1054 case K_TABMENU:
1055 ins_tabline(c);
1056 break;
1057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001059 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 break;
1061
Bram Moolenaar754b5602006-02-09 23:53:20 +00001062#ifdef FEAT_AUTOCMD
1063 case K_CURSORHOLD: /* Didn't type something for a while. */
1064 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1065 did_cursorhold = TRUE;
1066 break;
1067#endif
1068
Bram Moolenaar4770d092006-01-12 23:22:24 +00001069#ifdef FEAT_GUI_W32
1070 /* On Win32 ignore <M-F4>, we get it when closing the window was
1071 * cancelled. */
1072 case K_F4:
1073 if (mod_mask != MOD_MASK_ALT)
1074 goto normalchar;
1075 break;
1076#endif
1077
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078#ifdef FEAT_GUI
1079 case K_VER_SCROLLBAR:
1080 ins_scroll();
1081 break;
1082
1083 case K_HOR_SCROLLBAR:
1084 ins_horscroll();
1085 break;
1086#endif
1087
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001088 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 case K_S_HOME:
1091 case K_C_HOME:
1092 ins_home(c);
1093 break;
1094
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001095 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 case K_S_END:
1098 case K_C_END:
1099 ins_end(c);
1100 break;
1101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001103 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1104 ins_s_left();
1105 else
1106 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 break;
1108
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001109 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 case K_C_LEFT:
1111 ins_s_left();
1112 break;
1113
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001114 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001115 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1116 ins_s_right();
1117 else
1118 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 break;
1120
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001121 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 case K_C_RIGHT:
1123 ins_s_right();
1124 break;
1125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001126 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001127#ifdef FEAT_INS_EXPAND
1128 if (pum_visible())
1129 goto docomplete;
1130#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001131 if (mod_mask & MOD_MASK_SHIFT)
1132 ins_pageup();
1133 else
1134 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 break;
1136
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001137 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 case K_PAGEUP:
1139 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001140#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001141 if (pum_visible())
1142 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 ins_pageup();
1145 break;
1146
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001147 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001148#ifdef FEAT_INS_EXPAND
1149 if (pum_visible())
1150 goto docomplete;
1151#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001152 if (mod_mask & MOD_MASK_SHIFT)
1153 ins_pagedown();
1154 else
1155 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 break;
1157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 case K_PAGEDOWN:
1160 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001161#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001162 if (pum_visible())
1163 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 ins_pagedown();
1166 break;
1167
1168#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 ins_drop();
1171 break;
1172#endif
1173
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 c = TAB;
1176 /* FALLTHROUGH */
1177
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001178 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1180 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1181 goto docomplete;
1182#endif
1183 inserted_space = FALSE;
1184 if (ins_tab())
1185 goto normalchar; /* insert TAB as a normal char */
1186 auto_format(FALSE, TRUE);
1187 break;
1188
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001189 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 c = CAR;
1191 /* FALLTHROUGH */
1192 case CAR:
1193 case NL:
1194#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1195 /* In a quickfix window a <CR> jumps to the error under the
1196 * cursor. */
1197 if (bt_quickfix(curbuf) && c == CAR)
1198 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001199 if (curwin->w_llist_ref == NULL) /* quickfix window */
1200 do_cmdline_cmd((char_u *)".cc");
1201 else /* location list window */
1202 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 break;
1204 }
1205#endif
1206#ifdef FEAT_CMDWIN
1207 if (cmdwin_type != 0)
1208 {
1209 /* Execute the command in the cmdline window. */
1210 cmdwin_result = CAR;
1211 goto doESCkey;
1212 }
1213#endif
1214 if (ins_eol(c) && !p_im)
1215 goto doESCkey; /* out of memory */
1216 auto_format(FALSE, FALSE);
1217 inserted_space = FALSE;
1218 break;
1219
1220#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001221 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222# ifdef FEAT_INS_EXPAND
1223 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1224 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001225 if (has_compl_option(TRUE))
1226 goto docomplete;
1227 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 }
1229# endif
1230# ifdef FEAT_DIGRAPHS
1231 c = ins_digraph();
1232 if (c == NUL)
1233 break;
1234# endif
1235 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
1238#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001239 case Ctrl_X: /* Enter CTRL-X mode */
1240 ins_ctrl_x();
1241 break;
1242
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001243 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if (ctrl_x_mode != CTRL_X_TAGS)
1245 goto normalchar;
1246 goto docomplete;
1247
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001248 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 if (ctrl_x_mode != CTRL_X_FILES)
1250 goto normalchar;
1251 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001252
1253 case 's': /* Spelling completion after ^X */
1254 case Ctrl_S:
1255 if (ctrl_x_mode != CTRL_X_SPELL)
1256 goto normalchar;
1257 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#endif
1259
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001260 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261#ifdef FEAT_INS_EXPAND
1262 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1263#endif
1264 {
1265 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1266 if (p_im)
1267 {
1268 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1269 break;
1270 goto doESCkey;
1271 }
1272 goto normalchar;
1273 }
1274#ifdef FEAT_INS_EXPAND
1275 /* FALLTHROUGH */
1276
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001277 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 case Ctrl_N:
1279 /* if 'complete' is empty then plain ^P is no longer special,
1280 * but it is under other ^X modes */
1281 if (*curbuf->b_p_cpt == NUL
1282 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001283 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 goto normalchar;
1285
1286docomplete:
1287 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001288 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 break;
1290#endif /* FEAT_INS_EXPAND */
1291
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001292 case Ctrl_Y: /* copy from previous line or scroll down */
1293 case Ctrl_E: /* copy from next line or scroll up */
1294 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 break;
1296
1297 default:
1298#ifdef UNIX
1299 if (c == intr_char) /* special interrupt char */
1300 goto do_intr;
1301#endif
1302
1303 /*
1304 * Insert a nomal character.
1305 */
1306normalchar:
1307#ifdef FEAT_SMARTINDENT
1308 /* Try to perform smart-indenting. */
1309 ins_try_si(c);
1310#endif
1311
1312 if (c == ' ')
1313 {
1314 inserted_space = TRUE;
1315#ifdef FEAT_CINDENT
1316 if (inindent(0))
1317 can_cindent = FALSE;
1318#endif
1319 if (Insstart_blank_vcol == MAXCOL
1320 && curwin->w_cursor.lnum == Insstart.lnum)
1321 Insstart_blank_vcol = get_nolist_virtcol();
1322 }
1323
1324 if (vim_iswordc(c) || !echeck_abbr(
1325#ifdef FEAT_MBYTE
1326 /* Add ABBR_OFF for characters above 0x100, this is
1327 * what check_abbr() expects. */
1328 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1329#endif
1330 c))
1331 {
1332 insert_special(c, FALSE, FALSE);
1333#ifdef FEAT_RIGHTLEFT
1334 revins_legal++;
1335 revins_chars++;
1336#endif
1337 }
1338
1339 auto_format(FALSE, TRUE);
1340
1341#ifdef FEAT_FOLDING
1342 /* When inserting a character the cursor line must never be in a
1343 * closed fold. */
1344 foldOpenCursor();
1345#endif
1346 break;
1347 } /* end of switch (c) */
1348
1349 /* If the cursor was moved we didn't just insert a space */
1350 if (arrow_used)
1351 inserted_space = FALSE;
1352
1353#ifdef FEAT_CINDENT
1354 if (can_cindent && cindent_on()
1355# ifdef FEAT_INS_EXPAND
1356 && ctrl_x_mode == 0
1357# endif
1358 )
1359 {
1360force_cindent:
1361 /*
1362 * Indent now if a key was typed that is in 'cinkeys'.
1363 */
1364 if (in_cinkeys(c, ' ', line_is_white))
1365 {
1366 if (stop_arrow() == OK)
1367 /* re-indent the current line */
1368 do_c_expr_indent();
1369 }
1370 }
1371#endif /* FEAT_CINDENT */
1372
1373 } /* for (;;) */
1374 /* NOTREACHED */
1375}
1376
1377/*
1378 * Redraw for Insert mode.
1379 * This is postponed until getting the next character to make '$' in the 'cpo'
1380 * option work correctly.
1381 * Only redraw when there are no characters available. This speeds up
1382 * inserting sequences of characters (e.g., for CTRL-R).
1383 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001384/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001386ins_redraw(ready)
1387 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389 if (!char_avail())
1390 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001391#ifdef FEAT_AUTOCMD
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001392 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001393 if (ready && has_cursormovedI()
1394 && !equalpos(last_cursormoved, curwin->w_cursor))
1395 {
1396 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1397 last_cursormoved = curwin->w_cursor;
1398 }
1399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 if (must_redraw)
1401 update_screen(0);
1402 else if (clear_cmdline || redraw_cmdline)
1403 showmode(); /* clear cmdline and show mode */
1404 showruler(FALSE);
1405 setcursor();
1406 emsg_on_display = FALSE; /* may remove error message now */
1407 }
1408}
1409
1410/*
1411 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1412 */
1413 static void
1414ins_ctrl_v()
1415{
1416 int c;
1417
1418 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001419 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
1421 if (redrawing() && !char_avail())
1422 edit_putchar('^', TRUE);
1423 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1424
1425#ifdef FEAT_CMDL_INFO
1426 add_to_showcmd_c(Ctrl_V);
1427#endif
1428
1429 c = get_literal();
1430#ifdef FEAT_CMDL_INFO
1431 clear_showcmd();
1432#endif
1433 insert_special(c, FALSE, TRUE);
1434#ifdef FEAT_RIGHTLEFT
1435 revins_chars++;
1436 revins_legal++;
1437#endif
1438}
1439
1440/*
1441 * Put a character directly onto the screen. It's not stored in a buffer.
1442 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1443 */
1444static int pc_status;
1445#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1446#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1447#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1448#define PC_STATUS_SET 3 /* pc_bytes was filled */
1449#ifdef FEAT_MBYTE
1450static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1451#else
1452static char_u pc_bytes[2]; /* saved bytes */
1453#endif
1454static int pc_attr;
1455static int pc_row;
1456static int pc_col;
1457
1458 void
1459edit_putchar(c, highlight)
1460 int c;
1461 int highlight;
1462{
1463 int attr;
1464
1465 if (ScreenLines != NULL)
1466 {
1467 update_topline(); /* just in case w_topline isn't valid */
1468 validate_cursor();
1469 if (highlight)
1470 attr = hl_attr(HLF_8);
1471 else
1472 attr = 0;
1473 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1474 pc_col = W_WINCOL(curwin);
1475#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1476 pc_status = PC_STATUS_UNSET;
1477#endif
1478#ifdef FEAT_RIGHTLEFT
1479 if (curwin->w_p_rl)
1480 {
1481 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1482# ifdef FEAT_MBYTE
1483 if (has_mbyte)
1484 {
1485 int fix_col = mb_fix_col(pc_col, pc_row);
1486
1487 if (fix_col != pc_col)
1488 {
1489 screen_putchar(' ', pc_row, fix_col, attr);
1490 --curwin->w_wcol;
1491 pc_status = PC_STATUS_RIGHT;
1492 }
1493 }
1494# endif
1495 }
1496 else
1497#endif
1498 {
1499 pc_col += curwin->w_wcol;
1500#ifdef FEAT_MBYTE
1501 if (mb_lefthalve(pc_row, pc_col))
1502 pc_status = PC_STATUS_LEFT;
1503#endif
1504 }
1505
1506 /* save the character to be able to put it back */
1507#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1508 if (pc_status == PC_STATUS_UNSET)
1509#endif
1510 {
1511 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1512 pc_status = PC_STATUS_SET;
1513 }
1514 screen_putchar(c, pc_row, pc_col, attr);
1515 }
1516}
1517
1518/*
1519 * Undo the previous edit_putchar().
1520 */
1521 void
1522edit_unputchar()
1523{
1524 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1525 {
1526#if defined(FEAT_MBYTE)
1527 if (pc_status == PC_STATUS_RIGHT)
1528 ++curwin->w_wcol;
1529 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1530 redrawWinline(curwin->w_cursor.lnum, FALSE);
1531 else
1532#endif
1533 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1534 }
1535}
1536
1537/*
1538 * Called when p_dollar is set: display a '$' at the end of the changed text
1539 * Only works when cursor is in the line that changes.
1540 */
1541 void
1542display_dollar(col)
1543 colnr_T col;
1544{
1545 colnr_T save_col;
1546
1547 if (!redrawing())
1548 return;
1549
1550 cursor_off();
1551 save_col = curwin->w_cursor.col;
1552 curwin->w_cursor.col = col;
1553#ifdef FEAT_MBYTE
1554 if (has_mbyte)
1555 {
1556 char_u *p;
1557
1558 /* If on the last byte of a multi-byte move to the first byte. */
1559 p = ml_get_curline();
1560 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1561 }
1562#endif
1563 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1564 if (curwin->w_wcol < W_WIDTH(curwin))
1565 {
1566 edit_putchar('$', FALSE);
1567 dollar_vcol = curwin->w_virtcol;
1568 }
1569 curwin->w_cursor.col = save_col;
1570}
1571
1572/*
1573 * Call this function before moving the cursor from the normal insert position
1574 * in insert mode.
1575 */
1576 static void
1577undisplay_dollar()
1578{
1579 if (dollar_vcol)
1580 {
1581 dollar_vcol = 0;
1582 redrawWinline(curwin->w_cursor.lnum, FALSE);
1583 }
1584}
1585
1586/*
1587 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1588 * Keep the cursor on the same character.
1589 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1590 * type == INDENT_DEC decrease indent (for CTRL-D)
1591 * type == INDENT_SET set indent to "amount"
1592 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1593 */
1594 void
1595change_indent(type, amount, round, replaced)
1596 int type;
1597 int amount;
1598 int round;
1599 int replaced; /* replaced character, put on replace stack */
1600{
1601 int vcol;
1602 int last_vcol;
1603 int insstart_less; /* reduction for Insstart.col */
1604 int new_cursor_col;
1605 int i;
1606 char_u *ptr;
1607 int save_p_list;
1608 int start_col;
1609 colnr_T vc;
1610#ifdef FEAT_VREPLACE
1611 colnr_T orig_col = 0; /* init for GCC */
1612 char_u *new_line, *orig_line = NULL; /* init for GCC */
1613
1614 /* VREPLACE mode needs to know what the line was like before changing */
1615 if (State & VREPLACE_FLAG)
1616 {
1617 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1618 orig_col = curwin->w_cursor.col;
1619 }
1620#endif
1621
1622 /* for the following tricks we don't want list mode */
1623 save_p_list = curwin->w_p_list;
1624 curwin->w_p_list = FALSE;
1625 vc = getvcol_nolist(&curwin->w_cursor);
1626 vcol = vc;
1627
1628 /*
1629 * For Replace mode we need to fix the replace stack later, which is only
1630 * possible when the cursor is in the indent. Remember the number of
1631 * characters before the cursor if it's possible.
1632 */
1633 start_col = curwin->w_cursor.col;
1634
1635 /* determine offset from first non-blank */
1636 new_cursor_col = curwin->w_cursor.col;
1637 beginline(BL_WHITE);
1638 new_cursor_col -= curwin->w_cursor.col;
1639
1640 insstart_less = curwin->w_cursor.col;
1641
1642 /*
1643 * If the cursor is in the indent, compute how many screen columns the
1644 * cursor is to the left of the first non-blank.
1645 */
1646 if (new_cursor_col < 0)
1647 vcol = get_indent() - vcol;
1648
1649 if (new_cursor_col > 0) /* can't fix replace stack */
1650 start_col = -1;
1651
1652 /*
1653 * Set the new indent. The cursor will be put on the first non-blank.
1654 */
1655 if (type == INDENT_SET)
1656 (void)set_indent(amount, SIN_CHANGED);
1657 else
1658 {
1659#ifdef FEAT_VREPLACE
1660 int save_State = State;
1661
1662 /* Avoid being called recursively. */
1663 if (State & VREPLACE_FLAG)
1664 State = INSERT;
1665#endif
1666 shift_line(type == INDENT_DEC, round, 1);
1667#ifdef FEAT_VREPLACE
1668 State = save_State;
1669#endif
1670 }
1671 insstart_less -= curwin->w_cursor.col;
1672
1673 /*
1674 * Try to put cursor on same character.
1675 * If the cursor is at or after the first non-blank in the line,
1676 * compute the cursor column relative to the column of the first
1677 * non-blank character.
1678 * If we are not in insert mode, leave the cursor on the first non-blank.
1679 * If the cursor is before the first non-blank, position it relative
1680 * to the first non-blank, counted in screen columns.
1681 */
1682 if (new_cursor_col >= 0)
1683 {
1684 /*
1685 * When changing the indent while the cursor is touching it, reset
1686 * Insstart_col to 0.
1687 */
1688 if (new_cursor_col == 0)
1689 insstart_less = MAXCOL;
1690 new_cursor_col += curwin->w_cursor.col;
1691 }
1692 else if (!(State & INSERT))
1693 new_cursor_col = curwin->w_cursor.col;
1694 else
1695 {
1696 /*
1697 * Compute the screen column where the cursor should be.
1698 */
1699 vcol = get_indent() - vcol;
1700 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1701
1702 /*
1703 * Advance the cursor until we reach the right screen column.
1704 */
1705 vcol = last_vcol = 0;
1706 new_cursor_col = -1;
1707 ptr = ml_get_curline();
1708 while (vcol <= (int)curwin->w_virtcol)
1709 {
1710 last_vcol = vcol;
1711#ifdef FEAT_MBYTE
1712 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001713 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 else
1715#endif
1716 ++new_cursor_col;
1717 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1718 }
1719 vcol = last_vcol;
1720
1721 /*
1722 * May need to insert spaces to be able to position the cursor on
1723 * the right screen column.
1724 */
1725 if (vcol != (int)curwin->w_virtcol)
1726 {
1727 curwin->w_cursor.col = new_cursor_col;
1728 i = (int)curwin->w_virtcol - vcol;
1729 ptr = alloc(i + 1);
1730 if (ptr != NULL)
1731 {
1732 new_cursor_col += i;
1733 ptr[i] = NUL;
1734 while (--i >= 0)
1735 ptr[i] = ' ';
1736 ins_str(ptr);
1737 vim_free(ptr);
1738 }
1739 }
1740
1741 /*
1742 * When changing the indent while the cursor is in it, reset
1743 * Insstart_col to 0.
1744 */
1745 insstart_less = MAXCOL;
1746 }
1747
1748 curwin->w_p_list = save_p_list;
1749
1750 if (new_cursor_col <= 0)
1751 curwin->w_cursor.col = 0;
1752 else
1753 curwin->w_cursor.col = new_cursor_col;
1754 curwin->w_set_curswant = TRUE;
1755 changed_cline_bef_curs();
1756
1757 /*
1758 * May have to adjust the start of the insert.
1759 */
1760 if (State & INSERT)
1761 {
1762 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1763 {
1764 if ((int)Insstart.col <= insstart_less)
1765 Insstart.col = 0;
1766 else
1767 Insstart.col -= insstart_less;
1768 }
1769 if ((int)ai_col <= insstart_less)
1770 ai_col = 0;
1771 else
1772 ai_col -= insstart_less;
1773 }
1774
1775 /*
1776 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1777 * If the number of characters before the cursor decreased, need to pop a
1778 * few characters from the replace stack.
1779 * If the number of characters before the cursor increased, need to push a
1780 * few NULs onto the replace stack.
1781 */
1782 if (REPLACE_NORMAL(State) && start_col >= 0)
1783 {
1784 while (start_col > (int)curwin->w_cursor.col)
1785 {
1786 replace_join(0); /* remove a NUL from the replace stack */
1787 --start_col;
1788 }
1789 while (start_col < (int)curwin->w_cursor.col || replaced)
1790 {
1791 replace_push(NUL);
1792 if (replaced)
1793 {
1794 replace_push(replaced);
1795 replaced = NUL;
1796 }
1797 ++start_col;
1798 }
1799 }
1800
1801#ifdef FEAT_VREPLACE
1802 /*
1803 * For VREPLACE mode, we also have to fix the replace stack. In this case
1804 * it is always possible because we backspace over the whole line and then
1805 * put it back again the way we wanted it.
1806 */
1807 if (State & VREPLACE_FLAG)
1808 {
1809 /* If orig_line didn't allocate, just return. At least we did the job,
1810 * even if you can't backspace. */
1811 if (orig_line == NULL)
1812 return;
1813
1814 /* Save new line */
1815 new_line = vim_strsave(ml_get_curline());
1816 if (new_line == NULL)
1817 return;
1818
1819 /* We only put back the new line up to the cursor */
1820 new_line[curwin->w_cursor.col] = NUL;
1821
1822 /* Put back original line */
1823 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1824 curwin->w_cursor.col = orig_col;
1825
1826 /* Backspace from cursor to start of line */
1827 backspace_until_column(0);
1828
1829 /* Insert new stuff into line again */
1830 ins_bytes(new_line);
1831
1832 vim_free(new_line);
1833 }
1834#endif
1835}
1836
1837/*
1838 * Truncate the space at the end of a line. This is to be used only in an
1839 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1840 * modes.
1841 */
1842 void
1843truncate_spaces(line)
1844 char_u *line;
1845{
1846 int i;
1847
1848 /* find start of trailing white space */
1849 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1850 {
1851 if (State & REPLACE_FLAG)
1852 replace_join(0); /* remove a NUL from the replace stack */
1853 }
1854 line[i + 1] = NUL;
1855}
1856
1857#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1858 || defined(FEAT_COMMENTS) || defined(PROTO)
1859/*
1860 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1861 * modes correctly. May also be used when not in insert mode at all.
1862 */
1863 void
1864backspace_until_column(col)
1865 int col;
1866{
1867 while ((int)curwin->w_cursor.col > col)
1868 {
1869 curwin->w_cursor.col--;
1870 if (State & REPLACE_FLAG)
1871 replace_do_bs();
1872 else
1873 (void)del_char(FALSE);
1874 }
1875}
1876#endif
1877
1878#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1879/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001880 * CTRL-X pressed in Insert mode.
1881 */
1882 static void
1883ins_ctrl_x()
1884{
1885 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1886 * CTRL-V works like CTRL-N */
1887 if (ctrl_x_mode != CTRL_X_CMDLINE)
1888 {
1889 /* if the next ^X<> won't ADD nothing, then reset
1890 * compl_cont_status */
1891 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001892 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001893 else
1894 compl_cont_status = 0;
1895 /* We're not sure which CTRL-X mode it will be yet */
1896 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1897 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1898 edit_submode_pre = NULL;
1899 showmode();
1900 }
1901}
1902
1903/*
1904 * Return TRUE if the 'dict' or 'tsr' option can be used.
1905 */
1906 static int
1907has_compl_option(dict_opt)
1908 int dict_opt;
1909{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001910 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
1911 && !curwin->w_p_spell)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001912 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1913 {
1914 ctrl_x_mode = 0;
1915 edit_submode = NULL;
1916 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1917 : (char_u *)_("'thesaurus' option is empty"),
1918 hl_attr(HLF_E));
1919 if (emsg_silent == 0)
1920 {
1921 vim_beep();
1922 setcursor();
1923 out_flush();
1924 ui_delay(2000L, FALSE);
1925 }
1926 return FALSE;
1927 }
1928 return TRUE;
1929}
1930
1931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1933 * This depends on the current mode.
1934 */
1935 int
1936vim_is_ctrl_x_key(c)
1937 int c;
1938{
1939 /* Always allow ^R - let it's results then be checked */
1940 if (c == Ctrl_R)
1941 return TRUE;
1942
Bram Moolenaare3226be2005-12-18 22:10:00 +00001943 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001944 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001945 return TRUE;
1946
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 switch (ctrl_x_mode)
1948 {
1949 case 0: /* Not in any CTRL-X mode */
1950 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1951 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001952 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1954 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1955 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001956 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1957 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 case CTRL_X_SCROLL:
1959 return (c == Ctrl_Y || c == Ctrl_E);
1960 case CTRL_X_WHOLE_LINE:
1961 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1962 case CTRL_X_FILES:
1963 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1964 case CTRL_X_DICTIONARY:
1965 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1966 case CTRL_X_THESAURUS:
1967 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1968 case CTRL_X_TAGS:
1969 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1970#ifdef FEAT_FIND_ID
1971 case CTRL_X_PATH_PATTERNS:
1972 return (c == Ctrl_P || c == Ctrl_N);
1973 case CTRL_X_PATH_DEFINES:
1974 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1975#endif
1976 case CTRL_X_CMDLINE:
1977 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1978 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001979#ifdef FEAT_COMPL_FUNC
1980 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001981 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001982 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001983 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001984#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001985 case CTRL_X_SPELL:
1986 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 }
1988 EMSG(_(e_internal));
1989 return FALSE;
1990}
1991
1992/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001993 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 * case of the originally typed text is used, and the case of the completed
1995 * text is infered, ie this tries to work out what case you probably wanted
1996 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001997 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 */
1999 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002000ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 char_u *str;
2002 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002003 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 char_u *fname;
2005 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002006 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
2008 int has_lower = FALSE;
2009 int was_letter = FALSE;
2010 int idx;
2011
2012 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2013 {
2014 /* Infer case of completed part -- webb */
2015 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002016 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017
2018 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002019 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002021 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 {
2023 has_lower = TRUE;
2024 if (isupper(IObuff[idx]))
2025 {
2026 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002027 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2029 break;
2030 }
2031 }
2032 }
2033
2034 /*
2035 * Rule 2: No lower case, 2nd consecutive letter converted to
2036 * upper case.
2037 */
2038 if (!has_lower)
2039 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002040 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002042 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 && islower(IObuff[idx]))
2044 {
2045 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002046 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2048 break;
2049 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002050 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
2052 }
2053
2054 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002055 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002057 return ins_compl_add(IObuff, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002059 return ins_compl_add(str, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060}
2061
2062/*
2063 * Add a match to the list of matches.
2064 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002065 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002066 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002068 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002069ins_compl_add(str, len, icase, fname, extra, cdir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 char_u *str;
2071 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002072 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 char_u *fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002074 char_u *extra; /* extra text for popup menu or NULL */
2075 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002076 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002078 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002079 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
2081 ui_breakcheck();
2082 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002083 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if (len < 0)
2085 len = (int)STRLEN(str);
2086
2087 /*
2088 * If the same match is already present, don't add it.
2089 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002090 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002092 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 do
2094 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002095 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002096 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002097 && match->cp_str[len] == NUL)
2098 return NOTDONE;
2099 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002100 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
2102
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002103 /* Remove any popup menu before changing the list of matches. */
2104 ins_compl_del_pum();
2105
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 /*
2107 * Allocate a new match structure.
2108 * Copy the values to the new match structure.
2109 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002110 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002112 return FAIL;
2113 match->cp_number = -1;
2114 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002115 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002116 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
2118 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002119 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002121 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002122
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002124 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2126 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002127 if (fname != NULL
2128 && compl_curr_match
2129 && compl_curr_match->cp_fname != NULL
2130 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002131 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002132 else if (fname != NULL)
2133 {
2134 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002135 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002138 match->cp_fname = NULL;
2139 match->cp_flags = flags;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002140 if (extra != NULL)
2141 match->cp_extra = vim_strsave(extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
2143 /*
2144 * Link the new match structure in the list of matches.
2145 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002146 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002147 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 else if (dir == FORWARD)
2149 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002150 match->cp_next = compl_curr_match->cp_next;
2151 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
2153 else /* BACKWARD */
2154 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002155 match->cp_next = compl_curr_match;
2156 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002158 if (match->cp_next)
2159 match->cp_next->cp_prev = match;
2160 if (match->cp_prev)
2161 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002163 compl_first_match = match;
2164 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002166 /*
2167 * Find the longest common string if still doing that.
2168 */
2169 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2170 ins_compl_longest_match(match);
2171
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 return OK;
2173}
2174
2175/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002176 * Return TRUE if "str[len]" matches with match->cp_str, considering
2177 * match->cp_icase.
2178 */
2179 static int
2180ins_compl_equal(match, str, len)
2181 compl_T *match;
2182 char_u *str;
2183 int len;
2184{
2185 if (match->cp_icase)
2186 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2187 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2188}
2189
2190/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002191 * Reduce the longest common string for match "match".
2192 */
2193 static void
2194ins_compl_longest_match(match)
2195 compl_T *match;
2196{
2197 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002198 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002199 int had_match;
2200
2201 if (compl_leader == NULL)
2202 /* First match, use it as a whole. */
2203 compl_leader = vim_strsave(match->cp_str);
2204 else
2205 {
2206 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002207 p = compl_leader;
2208 s = match->cp_str;
2209 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002210 {
2211#ifdef FEAT_MBYTE
2212 if (has_mbyte)
2213 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002214 c1 = mb_ptr2char(p);
2215 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002216 }
2217 else
2218#endif
2219 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002220 c1 = *p;
2221 c2 = *s;
2222 }
2223 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2224 : (c1 != c2))
2225 break;
2226#ifdef FEAT_MBYTE
2227 if (has_mbyte)
2228 {
2229 mb_ptr_adv(p);
2230 mb_ptr_adv(s);
2231 }
2232 else
2233#endif
2234 {
2235 ++p;
2236 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002237 }
2238 }
2239
2240 if (*p != NUL)
2241 {
2242 /* Leader was shortened, need to change the inserted text. */
2243 *p = NUL;
2244 had_match = (curwin->w_cursor.col > compl_col);
2245 ins_compl_delete();
2246 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2247 ins_redraw(FALSE);
2248
2249 /* When the match isn't there (to avoid matching itself) remove it
2250 * again after redrawing. */
2251 if (!had_match)
2252 ins_compl_delete();
2253 }
2254
2255 compl_used_match = FALSE;
2256 }
2257}
2258
2259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 * Add an array of matches to the list of matches.
2261 * Frees matches[].
2262 */
2263 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002264ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 int num_matches;
2266 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002267 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
2269 int i;
2270 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002271 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
Bram Moolenaar572cb562005-08-05 21:35:02 +00002273 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002274 if ((add_r = ins_compl_add(matches[i], -1, icase,
2275 NULL, NULL, dir, 0)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002277 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 FreeWild(num_matches, matches);
2279}
2280
2281/* Make the completion list cyclic.
2282 * Return the number of matches (excluding the original).
2283 */
2284 static int
2285ins_compl_make_cyclic()
2286{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002287 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 int count = 0;
2289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002290 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 /*
2293 * Find the end of the list.
2294 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002295 match = compl_first_match;
2296 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002297 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002299 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 ++count;
2301 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002302 match->cp_next = compl_first_match;
2303 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305 return count;
2306}
2307
Bram Moolenaar9372a112005-12-06 19:59:18 +00002308/* "compl_match_array" points the currently displayed list of entries in the
2309 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002310static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002311static int compl_match_arraysize;
2312
2313/*
2314 * Update the screen and when there is any scrolling remove the popup menu.
2315 */
2316 static void
2317ins_compl_upd_pum()
2318{
2319 int h;
2320
2321 if (compl_match_array != NULL)
2322 {
2323 h = curwin->w_cline_height;
2324 update_screen(0);
2325 if (h != curwin->w_cline_height)
2326 ins_compl_del_pum();
2327 }
2328}
2329
2330/*
2331 * Remove any popup menu.
2332 */
2333 static void
2334ins_compl_del_pum()
2335{
2336 if (compl_match_array != NULL)
2337 {
2338 pum_undisplay();
2339 vim_free(compl_match_array);
2340 compl_match_array = NULL;
2341 }
2342}
2343
2344/*
2345 * Return TRUE if the popup menu should be displayed.
2346 */
2347 static int
2348pum_wanted()
2349{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002350 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002351 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002352 return FALSE;
2353
2354 /* The display looks bad on a B&W display. */
2355 if (t_colors < 8
2356#ifdef FEAT_GUI
2357 && !gui.in_use
2358#endif
2359 )
2360 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002361 return TRUE;
2362}
2363
2364/*
2365 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002366 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002367 */
2368 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002369pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002370{
2371 compl_T *compl;
2372 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002373
2374 /* Don't display the popup menu if there are no matches or there is only
2375 * one (ignoring the original text). */
2376 compl = compl_first_match;
2377 i = 0;
2378 do
2379 {
2380 if (compl == NULL
2381 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2382 break;
2383 compl = compl->cp_next;
2384 } while (compl != compl_first_match);
2385
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002386 if (strstr((char *)p_cot, "menuone") != NULL)
2387 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002388 return (i >= 2);
2389}
2390
2391/*
2392 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002393 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002394 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002395 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002396ins_compl_show_pum()
2397{
2398 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002399 compl_T *shown_compl = NULL;
2400 int did_find_shown_match = FALSE;
2401 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002402 int i;
2403 int cur = -1;
2404 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002405 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002406
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002407 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002408 return;
2409
2410 /* Update the screen before drawing the popup menu over it. */
2411 update_screen(0);
2412
2413 if (compl_match_array == NULL)
2414 {
2415 /* Need to build the popup menu list. */
2416 compl_match_arraysize = 0;
2417 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002418 if (compl_leader != NULL)
2419 lead_len = STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002420 do
2421 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002422 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2423 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002424 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002425 ++compl_match_arraysize;
2426 compl = compl->cp_next;
2427 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002428 if (compl_match_arraysize == 0)
2429 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002430 compl_match_array = (pumitem_T *)alloc_clear(
2431 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002432 * compl_match_arraysize));
2433 if (compl_match_array != NULL)
2434 {
2435 i = 0;
2436 compl = compl_first_match;
2437 do
2438 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002439 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2440 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002441 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002442 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002443 if (!shown_match_ok)
2444 {
2445 if (compl == compl_shown_match || did_find_shown_match)
2446 {
2447 /* This item is the shown match or this is the
2448 * first displayed item after the shown match. */
2449 compl_shown_match = compl;
2450 did_find_shown_match = TRUE;
2451 shown_match_ok = TRUE;
2452 }
2453 else
2454 /* Remember this displayed match for when the
2455 * shown match is just below it. */
2456 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002457 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002458 }
2459 compl_match_array[i].pum_text = compl->cp_str;
2460 if (compl->cp_extra != NULL)
2461 compl_match_array[i++].pum_extra = compl->cp_extra;
2462 else
2463 compl_match_array[i++].pum_extra = compl->cp_fname;
2464 }
2465
2466 if (compl == compl_shown_match)
2467 {
2468 did_find_shown_match = TRUE;
2469 if (!shown_match_ok && shown_compl != NULL)
2470 {
2471 /* The shown match isn't displayed, set it to the
2472 * previously displayed match. */
2473 compl_shown_match = shown_compl;
2474 shown_match_ok = TRUE;
2475 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002476 }
2477 compl = compl->cp_next;
2478 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002479
2480 if (!shown_match_ok) /* no displayed match at all */
2481 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002482 }
2483 }
2484 else
2485 {
2486 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002487 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002488 if (compl_match_array[i].pum_text == compl_shown_match->cp_str)
Bram Moolenaara6557602006-02-04 22:43:20 +00002489 break;
2490 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002491 }
2492
2493 if (compl_match_array != NULL)
2494 {
2495 /* Compute the screen column of the start of the completed text.
2496 * Use the cursor to get all wrapping and other settings right. */
2497 col = curwin->w_cursor.col;
2498 curwin->w_cursor.col = compl_col;
2499 validate_cursor_col();
2500 pum_display(compl_match_array, compl_match_arraysize, cur,
2501 curwin->w_cline_row + W_WINROW(curwin),
2502 curwin->w_cline_height,
Bram Moolenaar280f1262006-01-30 00:14:18 +00002503 curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002504 curwin->w_cursor.col = col;
2505 }
2506}
2507
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508#define DICT_FIRST (1) /* use just first element in "dict" */
2509#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002510
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002512 * Add any identifiers that match the given pattern in the list of dictionary
2513 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 */
2515 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002516ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2517 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002519 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002520 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002522 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 char_u *ptr;
2524 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 char_u **files;
2527 int count;
2528 int i;
2529 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002530 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
Bram Moolenaar0b238792006-03-02 22:49:12 +00002532 if (*dict == NUL)
2533 {
2534#ifdef FEAT_SYN_HL
2535 /* When 'dictionary' is empty and spell checking is enabled use
2536 * "spell". */
2537 if (!thesaurus && curwin->w_p_spell)
2538 dict = (char_u *)"spell";
2539 else
2540#endif
2541 return;
2542 }
2543
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002545 if (buf == NULL)
2546 return;
2547
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 /* If 'infercase' is set, don't use 'smartcase' here */
2549 save_p_scs = p_scs;
2550 if (curbuf->b_p_inf)
2551 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002552
2553 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2554 * to only match at the start of a line. Otherwise just match the
2555 * pattern. */
2556 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2557 {
2558 i = STRLEN(pat) + 8;
2559 ptr = alloc(i);
2560 if (ptr == NULL)
2561 return;
2562 vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat);
2563 regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0);
2564 vim_free(ptr);
2565 }
2566 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002567 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002568 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002569 if (regmatch.regprog == NULL)
2570 goto theend;
2571 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002572
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2574 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002575 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
2577 /* copy one dictionary file name into buf */
2578 if (flags == DICT_EXACT)
2579 {
2580 count = 1;
2581 files = &dict;
2582 }
2583 else
2584 {
2585 /* Expand wildcards in the dictionary name, but do not allow
2586 * backticks (for security, the 'dict' option may have been set in
2587 * a modeline). */
2588 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaar0b238792006-03-02 22:49:12 +00002589 if (!thesaurus && STRCMP(buf, "spell") == 0)
2590 count = -1;
2591 else if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 || expand_wildcards(1, &buf, &count, &files,
2593 EW_FILE|EW_SILENT) != OK)
2594 count = 0;
2595 }
2596
Bram Moolenaar0b238792006-03-02 22:49:12 +00002597 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 {
Bram Moolenaar0b238792006-03-02 22:49:12 +00002599 /* Skip "\<" in the pattern, we don't use it as a RE. */
2600 if (pat[0] == '\\' && pat[1] == '<')
2601 ptr = pat + 2;
2602 else
2603 ptr = pat;
2604 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002606 else
2607 {
2608 ins_compl_files(count, files, thesaurus, flags,
2609 &regmatch, buf, &dir);
2610 if (flags != DICT_EXACT)
2611 FreeWild(count, files);
2612 }
2613 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 break;
2615 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002616
2617theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 p_scs = save_p_scs;
2619 vim_free(regmatch.regprog);
2620 vim_free(buf);
2621}
2622
Bram Moolenaar0b238792006-03-02 22:49:12 +00002623 static void
2624ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2625 int count;
2626 char_u **files;
2627 int thesaurus;
2628 int flags;
2629 regmatch_T *regmatch;
2630 char_u *buf;
2631 int *dir;
2632{
2633 char_u *ptr;
2634 int i;
2635 FILE *fp;
2636 int add_r;
2637
2638 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2639 {
2640 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2641 if (flags != DICT_EXACT)
2642 {
2643 vim_snprintf((char *)IObuff, IOSIZE,
2644 _("Scanning dictionary: %s"), (char *)files[i]);
2645 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2646 }
2647
2648 if (fp != NULL)
2649 {
2650 /*
2651 * Read dictionary file line by line.
2652 * Check each line for a match.
2653 */
2654 while (!got_int && !compl_interrupted
2655 && !vim_fgets(buf, LSIZE, fp))
2656 {
2657 ptr = buf;
2658 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2659 {
2660 ptr = regmatch->startp[0];
2661 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2662 ptr = find_line_end(ptr);
2663 else
2664 ptr = find_word_end(ptr);
2665 add_r = ins_compl_add_infercase(regmatch->startp[0],
2666 (int)(ptr - regmatch->startp[0]),
2667 p_ic, files[i], *dir, 0);
2668 if (thesaurus)
2669 {
2670 char_u *wstart;
2671
2672 /*
2673 * Add the other matches on the line
2674 */
2675 while (!got_int)
2676 {
2677 /* Find start of the next word. Skip white
2678 * space and punctuation. */
2679 ptr = find_word_start(ptr);
2680 if (*ptr == NUL || *ptr == NL)
2681 break;
2682 wstart = ptr;
2683
2684 /* Find end of the word and add it. */
2685#ifdef FEAT_MBYTE
2686 if (has_mbyte)
2687 /* Japanese words may have characters in
2688 * different classes, only separate words
2689 * with single-byte non-word characters. */
2690 while (*ptr != NUL)
2691 {
2692 int l = (*mb_ptr2len)(ptr);
2693
2694 if (l < 2 && !vim_iswordc(*ptr))
2695 break;
2696 ptr += l;
2697 }
2698 else
2699#endif
2700 ptr = find_word_end(ptr);
2701 add_r = ins_compl_add_infercase(wstart,
2702 (int)(ptr - wstart),
2703 p_ic, files[i], *dir, 0);
2704 }
2705 }
2706 if (add_r == OK)
2707 /* if dir was BACKWARD then honor it just once */
2708 *dir = FORWARD;
2709 else if (add_r == FAIL)
2710 break;
2711 /* avoid expensive call to vim_regexec() when at end
2712 * of line */
2713 if (*ptr == '\n' || got_int)
2714 break;
2715 }
2716 line_breakcheck();
2717 ins_compl_check_keys(50);
2718 }
2719 fclose(fp);
2720 }
2721 }
2722}
2723
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724/*
2725 * Find the start of the next word.
2726 * Returns a pointer to the first char of the word. Also stops at a NUL.
2727 */
2728 char_u *
2729find_word_start(ptr)
2730 char_u *ptr;
2731{
2732#ifdef FEAT_MBYTE
2733 if (has_mbyte)
2734 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002735 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 else
2737#endif
2738 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2739 ++ptr;
2740 return ptr;
2741}
2742
2743/*
2744 * Find the end of the word. Assumes it starts inside a word.
2745 * Returns a pointer to just after the word.
2746 */
2747 char_u *
2748find_word_end(ptr)
2749 char_u *ptr;
2750{
2751#ifdef FEAT_MBYTE
2752 int start_class;
2753
2754 if (has_mbyte)
2755 {
2756 start_class = mb_get_class(ptr);
2757 if (start_class > 1)
2758 while (*ptr != NUL)
2759 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002760 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 if (mb_get_class(ptr) != start_class)
2762 break;
2763 }
2764 }
2765 else
2766#endif
2767 while (vim_iswordc(*ptr))
2768 ++ptr;
2769 return ptr;
2770}
2771
2772/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002773 * Find the end of the line, omitting CR and NL at the end.
2774 * Returns a pointer to just after the line.
2775 */
2776 static char_u *
2777find_line_end(ptr)
2778 char_u *ptr;
2779{
2780 char_u *s;
2781
2782 s = ptr + STRLEN(ptr);
2783 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2784 --s;
2785 return s;
2786}
2787
2788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 * Free the list of completions
2790 */
2791 static void
2792ins_compl_free()
2793{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002794 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002796 vim_free(compl_pattern);
2797 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002798 vim_free(compl_leader);
2799 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002801 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002803
2804 ins_compl_del_pum();
2805 pum_clear();
2806
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002807 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 do
2809 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002810 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002811 compl_curr_match = compl_curr_match->cp_next;
2812 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002814 if (match->cp_flags & FREE_FNAME)
2815 vim_free(match->cp_fname);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002816 vim_free(match->cp_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002818 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2819 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820}
2821
2822 static void
2823ins_compl_clear()
2824{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002825 compl_cont_status = 0;
2826 compl_started = FALSE;
2827 compl_matches = 0;
2828 vim_free(compl_pattern);
2829 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002830 vim_free(compl_leader);
2831 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 edit_submode_extra = NULL;
2833}
2834
2835/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002836 * Return TRUE when Insert completion is active.
2837 */
2838 int
2839ins_compl_active()
2840{
2841 return compl_started;
2842}
2843
2844/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002845 * Delete one character before the cursor and show the subset of the matches
2846 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002847 * Returns TRUE if the work is done and another char to be got from the user.
2848 */
2849 static int
2850ins_compl_bs()
2851{
2852 char_u *line;
2853 char_u *p;
2854
2855 if (curwin->w_cursor.col <= compl_col + compl_length)
2856 {
2857 /* Deleted more than what was used to find matches, need to look for
2858 * matches all over again. */
2859 ins_compl_free();
2860 compl_started = FALSE;
2861 compl_matches = 0;
2862 }
2863
2864 line = ml_get_curline();
2865 p = line + curwin->w_cursor.col;
2866 mb_ptr_back(line, p);
2867
2868 vim_free(compl_leader);
2869 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2870 if (compl_leader != NULL)
2871 {
2872 ins_compl_del_pum();
2873 ins_compl_delete();
2874 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2875
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002876 if (compl_started)
2877 ins_compl_set_original_text(compl_leader);
2878 else
Bram Moolenaara6557602006-02-04 22:43:20 +00002879 {
2880 /* Matches were cleared, need to search for them now. */
2881 if (ins_complete(Ctrl_N) == FAIL)
2882 compl_cont_status = 0;
2883 else
2884 {
2885 /* Remove the completed word again. */
2886 ins_compl_delete();
2887 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2888 }
2889 }
2890
2891 /* Show the popup menu with a different set of matches. */
2892 ins_compl_show_pum();
2893 compl_used_match = FALSE;
2894
2895 return TRUE;
2896 }
2897 return FALSE;
2898}
2899
2900/*
2901 * Append one character to the match leader. May reduce the number of
2902 * matches.
2903 */
2904 static void
2905ins_compl_addleader(c)
2906 int c;
2907{
2908#ifdef FEAT_MBYTE
2909 int cc;
2910
2911 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2912 {
2913 char_u buf[MB_MAXBYTES + 1];
2914
2915 (*mb_char2bytes)(c, buf);
2916 buf[cc] = NUL;
2917 ins_char_bytes(buf, cc);
2918 }
2919 else
2920#endif
2921 ins_char(c);
2922
2923 vim_free(compl_leader);
2924 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
2925 curwin->w_cursor.col - compl_col);
2926 if (compl_leader != NULL)
2927 {
2928 /* Show the popup menu with a different set of matches. */
2929 ins_compl_del_pum();
2930 ins_compl_show_pum();
2931 compl_used_match = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002932 ins_compl_set_original_text(compl_leader);
2933 }
2934}
2935
2936/*
2937 * Set the first match, the original text.
2938 */
2939 static void
2940ins_compl_set_original_text(str)
2941 char_u *str;
2942{
2943 char_u *p;
2944
2945 /* Replace the original text entry. */
2946 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
2947 {
2948 p = vim_strsave(str);
2949 if (p != NULL)
2950 {
2951 vim_free(compl_first_match->cp_str);
2952 compl_first_match->cp_str = p;
2953 }
Bram Moolenaara6557602006-02-04 22:43:20 +00002954 }
2955}
2956
2957/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002958 * Append one character to the match leader. May reduce the number of
2959 * matches.
2960 */
2961 static void
2962ins_compl_addfrommatch()
2963{
2964 char_u *p;
2965 int len = curwin->w_cursor.col - compl_col;
2966 int c;
2967
2968 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002969 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002970 return;
2971 p += len;
2972#ifdef FEAT_MBYTE
2973 c = mb_ptr2char(p);
2974#else
2975 c = *p;
2976#endif
2977 ins_compl_addleader(c);
2978}
2979
2980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002982 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002983 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002985 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986ins_compl_prep(c)
2987 int c;
2988{
2989 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 int temp;
2991 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002992 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
2994 /* Forget any previous 'special' messages if this is actually
2995 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2996 */
2997 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2998 edit_submode_extra = NULL;
2999
3000 /* Ignore end of Select mode mapping */
3001 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003002 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003004 /* Set "compl_get_longest" when finding the first matches. */
3005 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3006 || (ctrl_x_mode == 0 && !compl_started))
3007 {
3008 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3009 compl_used_match = TRUE;
3010 }
3011
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3013 {
3014 /*
3015 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3016 * it will be yet. Now we decide.
3017 */
3018 switch (c)
3019 {
3020 case Ctrl_E:
3021 case Ctrl_Y:
3022 ctrl_x_mode = CTRL_X_SCROLL;
3023 if (!(State & REPLACE_FLAG))
3024 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3025 else
3026 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3027 edit_submode_pre = NULL;
3028 showmode();
3029 break;
3030 case Ctrl_L:
3031 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3032 break;
3033 case Ctrl_F:
3034 ctrl_x_mode = CTRL_X_FILES;
3035 break;
3036 case Ctrl_K:
3037 ctrl_x_mode = CTRL_X_DICTIONARY;
3038 break;
3039 case Ctrl_R:
3040 /* Simply allow ^R to happen without affecting ^X mode */
3041 break;
3042 case Ctrl_T:
3043 ctrl_x_mode = CTRL_X_THESAURUS;
3044 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003045#ifdef FEAT_COMPL_FUNC
3046 case Ctrl_U:
3047 ctrl_x_mode = CTRL_X_FUNCTION;
3048 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003049 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003050 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003051 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003052#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003053 case 's':
3054 case Ctrl_S:
3055 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003056#ifdef FEAT_SYN_HL
3057 spell_back_to_badword();
3058#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003059 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 case Ctrl_RSB:
3061 ctrl_x_mode = CTRL_X_TAGS;
3062 break;
3063#ifdef FEAT_FIND_ID
3064 case Ctrl_I:
3065 case K_S_TAB:
3066 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3067 break;
3068 case Ctrl_D:
3069 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3070 break;
3071#endif
3072 case Ctrl_V:
3073 case Ctrl_Q:
3074 ctrl_x_mode = CTRL_X_CMDLINE;
3075 break;
3076 case Ctrl_P:
3077 case Ctrl_N:
3078 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3079 * just started ^X mode, or there were enough ^X's to cancel
3080 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3081 * do normal expansion when interrupting a different mode (say
3082 * ^X^F^X^P or ^P^X^X^P, see below)
3083 * nothing changes if interrupting mode 0, (eg, the flag
3084 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003085 if (!(compl_cont_status & CONT_INTRPT))
3086 compl_cont_status |= CONT_LOCAL;
3087 else if (compl_cont_mode != 0)
3088 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 /* FALLTHROUGH */
3090 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003091 /* If we have typed at least 2 ^X's... for modes != 0, we set
3092 * compl_cont_status = 0 (eg, as if we had just started ^X
3093 * mode).
3094 * For mode 0, we set "compl_cont_mode" to an impossible
3095 * value, in both cases ^X^X can be used to restart the same
3096 * mode (avoiding ADDING mode).
3097 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3098 * 'complete' and local ^P expansions respectively.
3099 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3100 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 if (c == Ctrl_X)
3102 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003103 if (compl_cont_mode != 0)
3104 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003106 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
3108 ctrl_x_mode = 0;
3109 edit_submode = NULL;
3110 showmode();
3111 break;
3112 }
3113 }
3114 else if (ctrl_x_mode != 0)
3115 {
3116 /* We're already in CTRL-X mode, do we stay in it? */
3117 if (!vim_is_ctrl_x_key(c))
3118 {
3119 if (ctrl_x_mode == CTRL_X_SCROLL)
3120 ctrl_x_mode = 0;
3121 else
3122 ctrl_x_mode = CTRL_X_FINISHED;
3123 edit_submode = NULL;
3124 }
3125 showmode();
3126 }
3127
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003128 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 {
3130 /* Show error message from attempted keyword completion (probably
3131 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003132 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003134 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3135 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 || ctrl_x_mode == CTRL_X_FINISHED)
3137 {
3138 /* Get here when we have finished typing a sequence of ^N and
3139 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003140 * memory that was used, and make sure we can redo the insert. */
3141 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003143 char_u *p;
3144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 /*
3146 * If any of the original typed text has been changed,
3147 * eg when ignorecase is set, we must add back-spaces to
3148 * the redo buffer. We add as few as necessary to delete
3149 * just the part of the original text that has changed.
3150 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003151 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003152 p = compl_orig_text;
3153 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003155 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 ++ptr;
3157 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003158 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00003160 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 }
3162
3163#ifdef FEAT_CINDENT
3164 want_cindent = (can_cindent && cindent_on());
3165#endif
3166 /*
3167 * When completing whole lines: fix indent for 'cindent'.
3168 * Otherwise, break line if it's too long.
3169 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003170 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 {
3172#ifdef FEAT_CINDENT
3173 /* re-indent the current line */
3174 if (want_cindent)
3175 {
3176 do_c_expr_indent();
3177 want_cindent = FALSE; /* don't do it again */
3178 }
3179#endif
3180 }
3181 else
3182 {
3183 /* put the cursor on the last char, for 'tw' formatting */
3184 curwin->w_cursor.col--;
3185 if (stop_arrow() == OK)
3186 insertchar(NUL, 0, -1);
3187 curwin->w_cursor.col++;
3188 }
3189
3190 auto_format(FALSE, TRUE);
3191
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003192 /* if the popup menu is displayed hitting Enter means accepting
3193 * the selection without inserting anything. */
3194 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
3195 retval = TRUE;
3196
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003198 compl_started = FALSE;
3199 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 msg_clr_cmdline(); /* necessary for "noshowmode" */
3201 ctrl_x_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 if (edit_submode != NULL)
3203 {
3204 edit_submode = NULL;
3205 showmode();
3206 }
3207
3208#ifdef FEAT_CINDENT
3209 /*
3210 * Indent now if a key was typed that is in 'cinkeys'.
3211 */
3212 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3213 do_c_expr_indent();
3214#endif
3215 }
3216 }
3217
3218 /* reset continue_* if we left expansion-mode, if we stay they'll be
3219 * (re)set properly in ins_complete() */
3220 if (!vim_is_ctrl_x_key(c))
3221 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003222 compl_cont_status = 0;
3223 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003225
3226 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227}
3228
3229/*
3230 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3231 * (depending on flag) starting from buf and looking for a non-scanned
3232 * buffer (other than curbuf). curbuf is special, if it is called with
3233 * buf=curbuf then it has to be the first call for a given flag/expansion.
3234 *
3235 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3236 */
3237 static buf_T *
3238ins_compl_next_buf(buf, flag)
3239 buf_T *buf;
3240 int flag;
3241{
3242#ifdef FEAT_WINDOWS
3243 static win_T *wp;
3244#endif
3245
3246 if (flag == 'w') /* just windows */
3247 {
3248#ifdef FEAT_WINDOWS
3249 if (buf == curbuf) /* first call for this flag/expansion */
3250 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003251 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 && wp->w_buffer->b_scanned)
3253 ;
3254 buf = wp->w_buffer;
3255#else
3256 buf = curbuf;
3257#endif
3258 }
3259 else
3260 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3261 * (unlisted buffers)
3262 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003263 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 && ((flag == 'U'
3265 ? buf->b_p_bl
3266 : (!buf->b_p_bl
3267 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003268 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 ;
3270 return buf;
3271}
3272
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003273#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003274static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003275
3276/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003277 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003278 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003279 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003280 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003281 static void
3282expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003283 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003284 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003285{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003286 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003287 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003288 listitem_T *li;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003289 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003290 char_u *funcname;
3291 pos_T pos;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003292 int dir = compl_direction;
3293 char_u *x;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003294 int icase;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003295
Bram Moolenaare344bea2005-09-01 20:46:49 +00003296 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3297 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003298 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003299
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003300 /* Call 'completefunc' to obtain the list of matches. */
3301 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003302 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003303
Bram Moolenaare344bea2005-09-01 20:46:49 +00003304 pos = curwin->w_cursor;
3305 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3306 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003307 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003308 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003309
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003310 /* Go through the List with matches and add each of them. */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003311 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003312 {
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003313 icase = p_ic;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003314 if (li->li_tv.v_type == VAR_DICT && li->li_tv.vval.v_dict != NULL)
3315 {
3316 p = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"word", FALSE);
3317 x = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"menu", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003318 if (get_dict_string(li->li_tv.vval.v_dict, (char_u *)"icase",
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003319 FALSE) != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003320 icase = get_dict_number(li->li_tv.vval.v_dict,
3321 (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003322 }
3323 else
3324 {
3325 p = get_tv_string_chk(&li->li_tv);
3326 x = NULL;
3327 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003328 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003329 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003330 if (ins_compl_add(p, -1, icase, NULL, x, dir, 0) == OK)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003331 /* if dir was BACKWARD then honor it just once */
3332 dir = FORWARD;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003333 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003334 else if (did_emsg)
3335 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003336 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003337
3338 list_unref(matchlist);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003339}
3340#endif /* FEAT_COMPL_FUNC */
3341
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003342/*
3343 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003344 * The search starts at position "ini" in curbuf and in the direction
3345 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003346 * When "compl_started" is FALSE start at that position, otherwise continue
3347 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003348 * This may return before finding all the matches.
3349 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 */
3351 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003352ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 static pos_T first_match_pos;
3356 static pos_T last_match_pos;
3357 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003358 static int found_all = FALSE; /* Found all matches of a
3359 certain type. */
3360 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361
Bram Moolenaar572cb562005-08-05 21:35:02 +00003362 pos_T *pos;
3363 char_u **matches;
3364 int save_p_scs;
3365 int save_p_ws;
3366 int save_p_ic;
3367 int i;
3368 int num_matches;
3369 int len;
3370 int found_new_match;
3371 int type = ctrl_x_mode;
3372 char_u *ptr;
3373 char_u *dict = NULL;
3374 int dict_f = 0;
3375 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003377 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 {
3379 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3380 ins_buf->b_scanned = 0;
3381 found_all = FALSE;
3382 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003383 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003384 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 last_match_pos = first_match_pos = *ini;
3386 }
3387
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003388 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003389 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3391 for (;;)
3392 {
3393 found_new_match = FAIL;
3394
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003395 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 * or if found_all says this entry is done. For ^X^L only use the
3397 * entries from 'complete' that look in loaded buffers. */
3398 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003399 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
3401 found_all = FALSE;
3402 while (*e_cpt == ',' || *e_cpt == ' ')
3403 e_cpt++;
3404 if (*e_cpt == '.' && !curbuf->b_scanned)
3405 {
3406 ins_buf = curbuf;
3407 first_match_pos = *ini;
3408 /* So that ^N can match word immediately after cursor */
3409 if (ctrl_x_mode == 0)
3410 dec(&first_match_pos);
3411 last_match_pos = first_match_pos;
3412 type = 0;
3413 }
3414 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3415 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3416 {
3417 /* Scan a buffer, but not the current one. */
3418 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3419 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003420 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 first_match_pos.col = last_match_pos.col = 0;
3422 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3423 last_match_pos.lnum = 0;
3424 type = 0;
3425 }
3426 else /* unloaded buffer, scan like dictionary */
3427 {
3428 found_all = TRUE;
3429 if (ins_buf->b_fname == NULL)
3430 continue;
3431 type = CTRL_X_DICTIONARY;
3432 dict = ins_buf->b_fname;
3433 dict_f = DICT_EXACT;
3434 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003435 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 ins_buf->b_fname == NULL
3437 ? buf_spname(ins_buf)
3438 : ins_buf->b_sfname == NULL
3439 ? (char *)ins_buf->b_fname
3440 : (char *)ins_buf->b_sfname);
3441 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3442 }
3443 else if (*e_cpt == NUL)
3444 break;
3445 else
3446 {
3447 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3448 type = -1;
3449 else if (*e_cpt == 'k' || *e_cpt == 's')
3450 {
3451 if (*e_cpt == 'k')
3452 type = CTRL_X_DICTIONARY;
3453 else
3454 type = CTRL_X_THESAURUS;
3455 if (*++e_cpt != ',' && *e_cpt != NUL)
3456 {
3457 dict = e_cpt;
3458 dict_f = DICT_FIRST;
3459 }
3460 }
3461#ifdef FEAT_FIND_ID
3462 else if (*e_cpt == 'i')
3463 type = CTRL_X_PATH_PATTERNS;
3464 else if (*e_cpt == 'd')
3465 type = CTRL_X_PATH_DEFINES;
3466#endif
3467 else if (*e_cpt == ']' || *e_cpt == 't')
3468 {
3469 type = CTRL_X_TAGS;
3470 sprintf((char*)IObuff, _("Scanning tags."));
3471 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3472 }
3473 else
3474 type = -1;
3475
3476 /* in any case e_cpt is advanced to the next entry */
3477 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3478
3479 found_all = TRUE;
3480 if (type == -1)
3481 continue;
3482 }
3483 }
3484
3485 switch (type)
3486 {
3487 case -1:
3488 break;
3489#ifdef FEAT_FIND_ID
3490 case CTRL_X_PATH_PATTERNS:
3491 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003492 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003493 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003495 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3497 (linenr_T)1, (linenr_T)MAXLNUM);
3498 break;
3499#endif
3500
3501 case CTRL_X_DICTIONARY:
3502 case CTRL_X_THESAURUS:
3503 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003504 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 : (type == CTRL_X_THESAURUS
3506 ? (*curbuf->b_p_tsr == NUL
3507 ? p_tsr
3508 : curbuf->b_p_tsr)
3509 : (*curbuf->b_p_dict == NUL
3510 ? p_dict
3511 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003512 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003513 dict != NULL ? dict_f
3514 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 dict = NULL;
3516 break;
3517
3518 case CTRL_X_TAGS:
3519 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3520 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003521 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
3523 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003524 * of matches is found when compl_pattern is empty */
3525 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3527 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3528 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3529 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003530 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
3532 p_ic = save_p_ic;
3533 break;
3534
3535 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003536 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3538 {
3539
3540 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003541 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003542 ins_compl_add_matches(num_matches, matches,
3543#ifdef CASE_INSENSITIVE_FILENAME
3544 TRUE
3545#else
3546 FALSE
3547#endif
3548 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
3550 break;
3551
3552 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003553 if (expand_cmdline(&compl_xp, compl_pattern,
3554 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003556 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 break;
3558
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003559#ifdef FEAT_COMPL_FUNC
3560 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003561 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003562 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003563 break;
3564#endif
3565
Bram Moolenaar488c6512005-08-11 20:09:58 +00003566 case CTRL_X_SPELL:
3567#ifdef FEAT_SYN_HL
3568 num_matches = expand_spelling(first_match_pos.lnum,
3569 first_match_pos.col, compl_pattern, &matches);
3570 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003571 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003572#endif
3573 break;
3574
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 default: /* normal ^P/^N and ^X^L */
3576 /*
3577 * If 'infercase' is set, don't use 'smartcase' here
3578 */
3579 save_p_scs = p_scs;
3580 if (ins_buf->b_p_inf)
3581 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003582
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 /* buffers other than curbuf are scanned from the beginning or the
3584 * end but never from the middle, thus setting nowrapscan in this
3585 * buffers is a good idea, on the other hand, we always set
3586 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3587 save_p_ws = p_ws;
3588 if (ins_buf != curbuf)
3589 p_ws = FALSE;
3590 else if (*e_cpt == '.')
3591 p_ws = TRUE;
3592 for (;;)
3593 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003594 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003596 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3597 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003599 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003601 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003603 found_new_match = searchit(NULL, ins_buf, pos,
3604 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003605 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003606 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003607 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003609 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003610 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 first_match_pos = *pos;
3612 last_match_pos = *pos;
3613 }
3614 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003615 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 found_new_match = FAIL;
3617 if (found_new_match == FAIL)
3618 {
3619 if (ins_buf == curbuf)
3620 found_all = TRUE;
3621 break;
3622 }
3623
3624 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003625 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 && ini->lnum == pos->lnum
3627 && ini->col == pos->col)
3628 continue;
3629 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3630 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3631 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003632 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
3634 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3635 continue;
3636 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3637 if (!p_paste)
3638 ptr = skipwhite(ptr);
3639 }
3640 len = (int)STRLEN(ptr);
3641 }
3642 else
3643 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003644 char_u *tmp_ptr = ptr;
3645
3646 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003648 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 /* Skip if already inside a word. */
3650 if (vim_iswordp(tmp_ptr))
3651 continue;
3652 /* Find start of next word. */
3653 tmp_ptr = find_word_start(tmp_ptr);
3654 }
3655 /* Find end of this word. */
3656 tmp_ptr = find_word_end(tmp_ptr);
3657 len = (int)(tmp_ptr - ptr);
3658
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003659 if ((compl_cont_status & CONT_ADDING)
3660 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
3662 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3663 {
3664 /* Try next line, if any. the new word will be
3665 * "join" as if the normal command "J" was used.
3666 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003667 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 * works -- Acevedo */
3669 STRNCPY(IObuff, ptr, len);
3670 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3671 tmp_ptr = ptr = skipwhite(ptr);
3672 /* Find start of next word. */
3673 tmp_ptr = find_word_start(tmp_ptr);
3674 /* Find end of next word. */
3675 tmp_ptr = find_word_end(tmp_ptr);
3676 if (tmp_ptr > ptr)
3677 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003678 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003680 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 IObuff[len++] = ' ';
3682 /* IObuf =~ "\k.* ", thus len >= 2 */
3683 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003684 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 || (vim_strchr(p_cpo, CPO_JOINSP)
3686 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003687 && (IObuff[len - 2] == '?'
3688 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 IObuff[len++] = ' ';
3690 }
3691 /* copy as much as posible of the new word */
3692 if (tmp_ptr - ptr >= IOSIZE - len)
3693 tmp_ptr = ptr + IOSIZE - len - 1;
3694 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3695 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003696 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
3698 IObuff[len] = NUL;
3699 ptr = IObuff;
3700 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003701 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 continue;
3703 }
3704 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003705 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003706 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003707 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 {
3709 found_new_match = OK;
3710 break;
3711 }
3712 }
3713 p_scs = save_p_scs;
3714 p_ws = save_p_ws;
3715 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003716
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003717 /* check if compl_curr_match has changed, (e.g. other type of
3718 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003719 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 found_new_match = OK;
3721
3722 /* break the loop for specialized modes (use 'complete' just for the
3723 * generic ctrl_x_mode == 0) or when we've found a new match */
3724 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003725 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003726 {
3727 if (got_int)
3728 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003729 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003730 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003731 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003732
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003733 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3734 || compl_interrupted)
3735 break;
3736 compl_started = TRUE;
3737 }
3738 else
3739 {
3740 /* Mark a buffer scanned when it has been scanned completely */
3741 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3742 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003744 compl_started = FALSE;
3745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003747 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
3749 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3750 && *e_cpt == NUL) /* Got to end of 'complete' */
3751 found_new_match = FAIL;
3752
3753 i = -1; /* total of matches, unknown */
3754 if (found_new_match == FAIL
3755 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3756 i = ins_compl_make_cyclic();
3757
3758 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003759 * just been made cyclic then we have to move compl_curr_match to the next
3760 * or previous entry (if any) -- Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003761 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003762 if (compl_curr_match == NULL)
3763 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 return i;
3765}
3766
3767/* Delete the old text being completed. */
3768 static void
3769ins_compl_delete()
3770{
3771 int i;
3772
3773 /*
3774 * In insert mode: Delete the typed part.
3775 * In replace mode: Put the old characters back, if any.
3776 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003777 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 backspace_until_column(i);
3779 changed_cline_bef_curs();
3780}
3781
3782/* Insert the new text being completed. */
3783 static void
3784ins_compl_insert()
3785{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003786 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003787 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3788 compl_used_match = FALSE;
3789 else
3790 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791}
3792
3793/*
3794 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003795 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3796 * get more completions. If it is FALSE, then we just do nothing when there
3797 * are no more completions in a given direction. The latter case is used when
3798 * we are still in the middle of finding completions, to allow browsing
3799 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 * Return the total number of matches, or -1 if still unknown -- webb.
3801 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003802 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3803 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 *
3805 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003806 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3807 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 */
3809 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003810ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003812 int count; /* repeat completion this many times; should
3813 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003814 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815{
3816 int num_matches = -1;
3817 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003818 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003819 compl_T *found_compl = NULL;
3820 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003822 if (compl_leader != NULL
3823 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003825 /* Set "compl_shown_match" to the actually shown match, it may differ
3826 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003827 while (!ins_compl_equal(compl_shown_match,
3828 compl_leader, STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003829 && compl_shown_match->cp_next != NULL
3830 && compl_shown_match->cp_next != compl_first_match)
3831 compl_shown_match = compl_shown_match->cp_next;
3832 }
3833
3834 if (allow_get_expansion && insert_match
3835 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 /* Delete old text to be replaced */
3837 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003838
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003839 compl_pending = FALSE;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003840
3841 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3842 * around. */
3843 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003845 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003847 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003848 found_end = (compl_first_match != NULL
3849 && (compl_shown_match->cp_next == compl_first_match
3850 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003851 }
3852 else if (compl_shows_dir == BACKWARD
3853 && compl_shown_match->cp_prev != NULL)
3854 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003855 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003856 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003857 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 }
3859 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003860 {
3861 compl_pending = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003862 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003863 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003864
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003865 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara6557602006-02-04 22:43:20 +00003866 if (compl_pending && compl_direction == compl_shows_dir)
3867 compl_shown_match = compl_curr_match;
3868 found_end = FALSE;
3869 }
3870 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
3871 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003872 && !ins_compl_equal(compl_shown_match,
3873 compl_leader, STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00003874 ++todo;
3875 else
3876 /* Remember a matching item. */
3877 found_compl = compl_shown_match;
3878
3879 /* Stop at the end of the list when we found a usable match. */
3880 if (found_end)
3881 {
3882 if (found_compl != NULL)
3883 {
3884 compl_shown_match = found_compl;
3885 break;
3886 }
3887 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 }
3890
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003891 /* Insert the text of the new completion, or the compl_leader. */
3892 if (insert_match)
3893 {
3894 if (!compl_get_longest || compl_used_match)
3895 ins_compl_insert();
3896 else
3897 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3898 }
3899 else
3900 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
3902 if (!allow_get_expansion)
3903 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003904 /* may undisplay the popup menu first */
3905 ins_compl_upd_pum();
3906
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003907 /* redraw to show the user what was inserted */
3908 update_screen(0);
3909
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003910 /* display the updated popup menu */
3911 ins_compl_show_pum();
3912
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 /* Delete old text to be replaced, since we're still searching and
3914 * don't want to match ourselves! */
3915 ins_compl_delete();
3916 }
3917
3918 /*
3919 * Show the file name for the match (if any)
3920 * Truncate the file name to avoid a wait for return.
3921 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003922 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
3924 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003925 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 if (i <= 0)
3927 i = 0;
3928 else
3929 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003930 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 msg(IObuff);
3932 redraw_cmdline = FALSE; /* don't overwrite! */
3933 }
3934
3935 return num_matches;
3936}
3937
3938/*
3939 * Call this while finding completions, to check whether the user has hit a key
3940 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003941 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003943 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 */
3945 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003946ins_compl_check_keys(frequency)
3947 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
3949 static int count = 0;
3950
3951 int c;
3952
3953 /* Don't check when reading keys from a script. That would break the test
3954 * scripts */
3955 if (using_script())
3956 return;
3957
3958 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003959 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 return;
3961 count = 0;
3962
3963 ++no_mapping;
3964 c = vpeekc_any();
3965 --no_mapping;
3966 if (c != NUL)
3967 {
3968 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3969 {
3970 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003971 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003972 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
3973 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003976 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003978 if (compl_pending && !got_int)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003979 (void)ins_compl_next(FALSE, 1, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003980}
3981
3982/*
3983 * Decide the direction of Insert mode complete from the key typed.
3984 * Returns BACKWARD or FORWARD.
3985 */
3986 static int
3987ins_compl_key2dir(c)
3988 int c;
3989{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003990 if (c == Ctrl_P || c == Ctrl_L
3991 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
3992 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00003993 return BACKWARD;
3994 return FORWARD;
3995}
3996
3997/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003998 * Return TRUE for keys that are used for completion only when the popup menu
3999 * is visible.
4000 */
4001 static int
4002ins_compl_pum_key(c)
4003 int c;
4004{
4005 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004006 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4007 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004008}
4009
4010/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004011 * Decide the number of completions to move forward.
4012 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4013 */
4014 static int
4015ins_compl_key2count(c)
4016 int c;
4017{
4018 int h;
4019
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004020 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004021 {
4022 h = pum_get_height();
4023 if (h > 3)
4024 h -= 2; /* keep some context */
4025 return h;
4026 }
4027 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028}
4029
4030/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004031 * Return TRUE if completion with "c" should insert the match, FALSE if only
4032 * to change the currently selected completion.
4033 */
4034 static int
4035ins_compl_use_match(c)
4036 int c;
4037{
4038 switch (c)
4039 {
4040 case K_UP:
4041 case K_DOWN:
4042 case K_PAGEDOWN:
4043 case K_KPAGEDOWN:
4044 case K_S_DOWN:
4045 case K_PAGEUP:
4046 case K_KPAGEUP:
4047 case K_S_UP:
4048 return FALSE;
4049 }
4050 return TRUE;
4051}
4052
4053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 * Do Insert mode completion.
4055 * Called when character "c" was typed, which has a meaning for completion.
4056 * Returns OK if completion was done, FAIL if something failed (out of mem).
4057 */
4058 static int
4059ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004060 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004062 char_u *line;
4063 int startcol = 0; /* column where searched text starts */
4064 colnr_T curs_col; /* cursor column */
4065 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066
Bram Moolenaare3226be2005-12-18 22:10:00 +00004067 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004068 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 {
4070 /* First time we hit ^N or ^P (in a row, I mean) */
4071
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 did_ai = FALSE;
4073#ifdef FEAT_SMARTINDENT
4074 did_si = FALSE;
4075 can_si = FALSE;
4076 can_si_back = FALSE;
4077#endif
4078 if (stop_arrow() == FAIL)
4079 return FAIL;
4080
4081 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004082 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004085 * "compl_startpos" to the cursor as a pattern to add a new word
4086 * instead of expand the one before the cursor, in word-wise if
4087 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 * is not in the same line as the cursor then fix it (the line has
4089 * been split because it was longer than 'tw'). if SOL is set then
4090 * skip the previous pattern, a word at the beginning of the line has
4091 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004092 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4093 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
4095 /*
4096 * it is a continued search
4097 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004098 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4100 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4101 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004102 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004104 /* line (probably) wrapped, set compl_startpos to the
4105 * first non_blank in the line, if it is not a wordchar
4106 * include it to get a better pattern, but then we don't
4107 * want the "\\<" prefix, check it bellow */
4108 compl_col = (colnr_T)(skipwhite(line) - line);
4109 compl_startpos.col = compl_col;
4110 compl_startpos.lnum = curwin->w_cursor.lnum;
4111 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113 else
4114 {
4115 /* S_IPOS was set when we inserted a word that was at the
4116 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004117 * mode but first we need to redefine compl_startpos */
4118 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004120 compl_cont_status |= CONT_SOL;
4121 compl_startpos.col = (colnr_T)(skipwhite(
4122 line + compl_length
4123 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004125 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004127 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004128 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 * have enough space? just being paranoic */
4130#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004131 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004133 compl_cont_status &= ~CONT_SOL;
4134 compl_length = (IOSIZE - MIN_SPACE);
4135 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004137 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4138 if (compl_length < 1)
4139 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004142 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004144 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
4146 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004147 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004149 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004151 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004153 compl_cont_status = 0;
4154 compl_cont_status |= CONT_N_ADDS;
4155 compl_startpos = curwin->w_cursor;
4156 startcol = (int)curs_col;
4157 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
4159
4160 /* Work out completion pattern and original text -- webb */
4161 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4162 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004163 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4165 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004166 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004168 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004170 compl_col += ++startcol;
4171 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004174 compl_pattern = str_foldcase(line + compl_col,
4175 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004177 compl_pattern = vim_strnsave(line + compl_col,
4178 compl_length);
4179 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 return FAIL;
4181 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004182 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 {
4184 char_u *prefix = (char_u *)"\\<";
4185
4186 /* we need 3 extra chars, 1 for the NUL and
4187 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004188 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4189 compl_length) + 3);
4190 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004192 if (!vim_iswordp(line + compl_col)
4193 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 && (
4195#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004196 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004198 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199#endif
4200 )))
4201 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004202 STRCPY((char *)compl_pattern, prefix);
4203 (void)quote_meta(compl_pattern + STRLEN(prefix),
4204 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004206 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004208 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004210 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211#endif
4212 )
4213 {
4214 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004215 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4216 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004218 compl_col += curs_col;
4219 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221 else
4222 {
4223#ifdef FEAT_MBYTE
4224 /* Search the point of change class of multibyte character
4225 * or not a word single byte character backward. */
4226 if (has_mbyte)
4227 {
4228 int base_class;
4229 int head_off;
4230
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004231 startcol -= (*mb_head_off)(line, line + startcol);
4232 base_class = mb_get_class(line + startcol);
4233 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004235 head_off = (*mb_head_off)(line, line + startcol);
4236 if (base_class != mb_get_class(line + startcol
4237 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004239 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241 }
4242 else
4243#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004244 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004246 compl_col += ++startcol;
4247 compl_length = (int)curs_col - startcol;
4248 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
4250 /* Only match word with at least two chars -- webb
4251 * there's no need to call quote_meta,
4252 * alloc(7) is enough -- Acevedo
4253 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004254 compl_pattern = alloc(7);
4255 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004257 STRCPY((char *)compl_pattern, "\\<");
4258 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4259 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261 else
4262 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004263 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4264 compl_length) + 3);
4265 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004267 STRCPY((char *)compl_pattern, "\\<");
4268 (void)quote_meta(compl_pattern + 2, line + compl_col,
4269 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271 }
4272 }
4273 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4274 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004275 compl_col = skipwhite(line) - line;
4276 compl_length = (int)curs_col - (int)compl_col;
4277 if (compl_length < 0) /* cursor in indent: empty pattern */
4278 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004280 compl_pattern = str_foldcase(line + compl_col, compl_length,
4281 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004283 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4284 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 return FAIL;
4286 }
4287 else if (ctrl_x_mode == CTRL_X_FILES)
4288 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004289 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004291 compl_col += ++startcol;
4292 compl_length = (int)curs_col - startcol;
4293 compl_pattern = addstar(line + compl_col, compl_length,
4294 EXPAND_FILES);
4295 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 return FAIL;
4297 }
4298 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4299 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004300 compl_pattern = vim_strnsave(line, curs_col);
4301 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004303 set_cmd_context(&compl_xp, compl_pattern,
4304 (int)STRLEN(compl_pattern), curs_col);
4305 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4306 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004308 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4309 compl_col = startcol;
4310 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004312 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004313 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004314#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004315 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004316 * Call user defined function 'completefunc' with "a:findstart"
4317 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004318 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004319 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004320 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004321 char_u *funcname;
4322 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004323
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004324 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004325 * string */
4326 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4327 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4328 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004329 {
4330 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4331 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004332 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004333 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004334
4335 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004336 args[1] = NULL;
4337 pos = curwin->w_cursor;
4338 col = call_func_retnr(funcname, 2, args, FALSE);
4339 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004340
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004341 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004342 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004343 compl_col = col;
4344 if ((colnr_T)compl_col > curs_col)
4345 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004346
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004347 /* Setup variables for completion. Need to obtain "line" again,
4348 * it may have become invalid. */
4349 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004350 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004351 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4352 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004353#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004354 return FAIL;
4355 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004356 else if (ctrl_x_mode == CTRL_X_SPELL)
4357 {
4358#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004359 if (spell_bad_len > 0)
4360 compl_col = curs_col - spell_bad_len;
4361 else
4362 compl_col = spell_word_start(startcol);
4363 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004364 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004365 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004366 compl_length = (int)curs_col - compl_col;
4367 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4368 if (compl_pattern == NULL)
4369#endif
4370 return FAIL;
4371 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004372 else
4373 {
4374 EMSG2(_(e_intern2), "ins_complete()");
4375 return FAIL;
4376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004378 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 {
4380 edit_submode_pre = (char_u *)_(" Adding");
4381 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4382 {
4383 /* Insert a new line, keep indentation but ignore 'comments' */
4384#ifdef FEAT_COMMENTS
4385 char_u *old = curbuf->b_p_com;
4386
4387 curbuf->b_p_com = (char_u *)"";
4388#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004389 compl_startpos.lnum = curwin->w_cursor.lnum;
4390 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 ins_eol('\r');
4392#ifdef FEAT_COMMENTS
4393 curbuf->b_p_com = old;
4394#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004395 compl_length = 0;
4396 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398 }
4399 else
4400 {
4401 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004402 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
4404
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004405 if (compl_cont_status & CONT_LOCAL)
4406 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 else
4408 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4409
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004410 /* Always add completion for the original text. */
4411 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004412 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4413 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004414 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004416 vim_free(compl_pattern);
4417 compl_pattern = NULL;
4418 vim_free(compl_orig_text);
4419 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 return FAIL;
4421 }
4422
4423 /* showmode might reset the internal line pointers, so it must
4424 * be called before line = ml_get(), or when this address is no
4425 * longer needed. -- Acevedo.
4426 */
4427 edit_submode_extra = (char_u *)_("-- Searching...");
4428 edit_submode_highl = HLF_COUNT;
4429 showmode();
4430 edit_submode_extra = NULL;
4431 out_flush();
4432 }
4433
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004434 compl_shown_match = compl_curr_match;
4435 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
4437 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004438 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004440 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004442 /* may undisplay the popup menu */
4443 ins_compl_upd_pum();
4444
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004445 if (n > 1) /* all matches have been found */
4446 compl_matches = n;
4447 compl_curr_match = compl_shown_match;
4448 compl_direction = compl_shows_dir;
4449 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
4451 /* eat the ESC to avoid leaving insert mode */
4452 if (got_int && !global_busy)
4453 {
4454 (void)vgetc();
4455 got_int = FALSE;
4456 }
4457
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004458 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004459 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004461 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4462 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4464 edit_submode_highl = HLF_E;
4465 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4466 * because we couldn't expand anything at first place, but if we used
4467 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4468 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004469 if ( compl_length > 1
4470 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 || (ctrl_x_mode != 0
4472 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4473 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004474 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
4476
Bram Moolenaar572cb562005-08-05 21:35:02 +00004477 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004478 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004480 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
4482 if (edit_submode_extra == NULL)
4483 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004484 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
4486 edit_submode_extra = (char_u *)_("Back at original");
4487 edit_submode_highl = HLF_W;
4488 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004489 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
4491 edit_submode_extra = (char_u *)_("Word from other line");
4492 edit_submode_highl = HLF_COUNT;
4493 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004494 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 {
4496 edit_submode_extra = (char_u *)_("The only match");
4497 edit_submode_highl = HLF_COUNT;
4498 }
4499 else
4500 {
4501 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004502 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004504 int number = 0;
4505 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004507 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 {
4509 /* search backwards for the first valid (!= -1) number.
4510 * This should normally succeed already at the first loop
4511 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004512 for (match = compl_curr_match->cp_prev; match != NULL
4513 && match != compl_first_match;
4514 match = match->cp_prev)
4515 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004517 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 break;
4519 }
4520 if (match != NULL)
4521 /* go up and assign all numbers which are not assigned
4522 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004523 for (match = match->cp_next;
4524 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004525 match = match->cp_next)
4526 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528 else /* BACKWARD */
4529 {
4530 /* search forwards (upwards) for the first valid (!= -1)
4531 * number. This should normally succeed already at the
4532 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004533 for (match = compl_curr_match->cp_next; match != NULL
4534 && match != compl_first_match;
4535 match = match->cp_next)
4536 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004538 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 break;
4540 }
4541 if (match != NULL)
4542 /* go down and assign all numbers which are not
4543 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004544 for (match = match->cp_prev; match
4545 && match->cp_number == -1;
4546 match = match->cp_prev)
4547 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549 }
4550
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004551 /* The match should always have a sequence number now, this is
4552 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004553 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 {
4555 /* Space for 10 text chars. + 2x10-digit no.s */
4556 static char_u match_ref[31];
4557
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004558 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004560 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004562 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004563 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004564 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 edit_submode_extra = match_ref;
4566 edit_submode_highl = HLF_R;
4567 if (dollar_vcol)
4568 curs_columns(FALSE);
4569 }
4570 }
4571 }
4572
4573 /* Show a message about what (completion) mode we're in. */
4574 showmode();
4575 if (edit_submode_extra != NULL)
4576 {
4577 if (!p_smd)
4578 msg_attr(edit_submode_extra,
4579 edit_submode_highl < HLF_COUNT
4580 ? hl_attr(edit_submode_highl) : 0);
4581 }
4582 else
4583 msg_clr_cmdline(); /* necessary for "noshowmode" */
4584
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004585 ins_compl_show_pum();
4586
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 return OK;
4588}
4589
4590/*
4591 * Looks in the first "len" chars. of "src" for search-metachars.
4592 * If dest is not NULL the chars. are copied there quoting (with
4593 * a backslash) the metachars, and dest would be NUL terminated.
4594 * Returns the length (needed) of dest
4595 */
4596 static int
4597quote_meta(dest, src, len)
4598 char_u *dest;
4599 char_u *src;
4600 int len;
4601{
4602 int m;
4603
4604 for (m = len; --len >= 0; src++)
4605 {
4606 switch (*src)
4607 {
4608 case '.':
4609 case '*':
4610 case '[':
4611 if (ctrl_x_mode == CTRL_X_DICTIONARY
4612 || ctrl_x_mode == CTRL_X_THESAURUS)
4613 break;
4614 case '~':
4615 if (!p_magic) /* quote these only if magic is set */
4616 break;
4617 case '\\':
4618 if (ctrl_x_mode == CTRL_X_DICTIONARY
4619 || ctrl_x_mode == CTRL_X_THESAURUS)
4620 break;
4621 case '^': /* currently it's not needed. */
4622 case '$':
4623 m++;
4624 if (dest != NULL)
4625 *dest++ = '\\';
4626 break;
4627 }
4628 if (dest != NULL)
4629 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004630# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 /* Copy remaining bytes of a multibyte character. */
4632 if (has_mbyte)
4633 {
4634 int i, mb_len;
4635
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004636 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 if (mb_len > 0 && len >= mb_len)
4638 for (i = 0; i < mb_len; ++i)
4639 {
4640 --len;
4641 ++src;
4642 if (dest != NULL)
4643 *dest++ = *src;
4644 }
4645 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 }
4648 if (dest != NULL)
4649 *dest = NUL;
4650
4651 return m;
4652}
4653#endif /* FEAT_INS_EXPAND */
4654
4655/*
4656 * Next character is interpreted literally.
4657 * A one, two or three digit decimal number is interpreted as its byte value.
4658 * If one or two digits are entered, the next character is given to vungetc().
4659 * For Unicode a character > 255 may be returned.
4660 */
4661 int
4662get_literal()
4663{
4664 int cc;
4665 int nc;
4666 int i;
4667 int hex = FALSE;
4668 int octal = FALSE;
4669#ifdef FEAT_MBYTE
4670 int unicode = 0;
4671#endif
4672
4673 if (got_int)
4674 return Ctrl_C;
4675
4676#ifdef FEAT_GUI
4677 /*
4678 * In GUI there is no point inserting the internal code for a special key.
4679 * It is more useful to insert the string "<KEY>" instead. This would
4680 * probably be useful in a text window too, but it would not be
4681 * vi-compatible (maybe there should be an option for it?) -- webb
4682 */
4683 if (gui.in_use)
4684 ++allow_keys;
4685#endif
4686#ifdef USE_ON_FLY_SCROLL
4687 dont_scroll = TRUE; /* disallow scrolling here */
4688#endif
4689 ++no_mapping; /* don't map the next key hits */
4690 cc = 0;
4691 i = 0;
4692 for (;;)
4693 {
4694 do
4695 nc = safe_vgetc();
4696 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4697 || nc == K_HOR_SCROLLBAR);
4698#ifdef FEAT_CMDL_INFO
4699 if (!(State & CMDLINE)
4700# ifdef FEAT_MBYTE
4701 && MB_BYTE2LEN_CHECK(nc) == 1
4702# endif
4703 )
4704 add_to_showcmd(nc);
4705#endif
4706 if (nc == 'x' || nc == 'X')
4707 hex = TRUE;
4708 else if (nc == 'o' || nc == 'O')
4709 octal = TRUE;
4710#ifdef FEAT_MBYTE
4711 else if (nc == 'u' || nc == 'U')
4712 unicode = nc;
4713#endif
4714 else
4715 {
4716 if (hex
4717#ifdef FEAT_MBYTE
4718 || unicode != 0
4719#endif
4720 )
4721 {
4722 if (!vim_isxdigit(nc))
4723 break;
4724 cc = cc * 16 + hex2nr(nc);
4725 }
4726 else if (octal)
4727 {
4728 if (nc < '0' || nc > '7')
4729 break;
4730 cc = cc * 8 + nc - '0';
4731 }
4732 else
4733 {
4734 if (!VIM_ISDIGIT(nc))
4735 break;
4736 cc = cc * 10 + nc - '0';
4737 }
4738
4739 ++i;
4740 }
4741
4742 if (cc > 255
4743#ifdef FEAT_MBYTE
4744 && unicode == 0
4745#endif
4746 )
4747 cc = 255; /* limit range to 0-255 */
4748 nc = 0;
4749
4750 if (hex) /* hex: up to two chars */
4751 {
4752 if (i >= 2)
4753 break;
4754 }
4755#ifdef FEAT_MBYTE
4756 else if (unicode) /* Unicode: up to four or eight chars */
4757 {
4758 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4759 break;
4760 }
4761#endif
4762 else if (i >= 3) /* decimal or octal: up to three chars */
4763 break;
4764 }
4765 if (i == 0) /* no number entered */
4766 {
4767 if (nc == K_ZERO) /* NUL is stored as NL */
4768 {
4769 cc = '\n';
4770 nc = 0;
4771 }
4772 else
4773 {
4774 cc = nc;
4775 nc = 0;
4776 }
4777 }
4778
4779 if (cc == 0) /* NUL is stored as NL */
4780 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004781#ifdef FEAT_MBYTE
4782 if (enc_dbcs && (cc & 0xff) == 0)
4783 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4784 second byte will cause trouble! */
4785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786
4787 --no_mapping;
4788#ifdef FEAT_GUI
4789 if (gui.in_use)
4790 --allow_keys;
4791#endif
4792 if (nc)
4793 vungetc(nc);
4794 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4795 return cc;
4796}
4797
4798/*
4799 * Insert character, taking care of special keys and mod_mask
4800 */
4801 static void
4802insert_special(c, allow_modmask, ctrlv)
4803 int c;
4804 int allow_modmask;
4805 int ctrlv; /* c was typed after CTRL-V */
4806{
4807 char_u *p;
4808 int len;
4809
4810 /*
4811 * Special function key, translate into "<Key>". Up to the last '>' is
4812 * inserted with ins_str(), so as not to replace characters in replace
4813 * mode.
4814 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4815 * unless 'allow_modmask' is TRUE.
4816 */
4817#ifdef MACOS
4818 /* Command-key never produces a normal key */
4819 if (mod_mask & MOD_MASK_CMD)
4820 allow_modmask = TRUE;
4821#endif
4822 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4823 {
4824 p = get_special_key_name(c, mod_mask);
4825 len = (int)STRLEN(p);
4826 c = p[len - 1];
4827 if (len > 2)
4828 {
4829 if (stop_arrow() == FAIL)
4830 return;
4831 p[len - 1] = NUL;
4832 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004833 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 ctrlv = FALSE;
4835 }
4836 }
4837 if (stop_arrow() == OK)
4838 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4839}
4840
4841/*
4842 * Special characters in this context are those that need processing other
4843 * than the simple insertion that can be performed here. This includes ESC
4844 * which terminates the insert, and CR/NL which need special processing to
4845 * open up a new line. This routine tries to optimize insertions performed by
4846 * the "redo", "undo" or "put" commands, so it needs to know when it should
4847 * stop and defer processing to the "normal" mechanism.
4848 * '0' and '^' are special, because they can be followed by CTRL-D.
4849 */
4850#ifdef EBCDIC
4851# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4852#else
4853# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4854#endif
4855
4856#ifdef FEAT_MBYTE
4857# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4858#else
4859# define WHITECHAR(cc) vim_iswhite(cc)
4860#endif
4861
4862 void
4863insertchar(c, flags, second_indent)
4864 int c; /* character to insert or NUL */
4865 int flags; /* INSCHAR_FORMAT, etc. */
4866 int second_indent; /* indent for second line if >= 0 */
4867{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 int textwidth;
4869#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
4874 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4875 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
4877 /*
4878 * Try to break the line in two or more pieces when:
4879 * - Always do this if we have been called to do formatting only.
4880 * - Always do this when 'formatoptions' has the 'a' flag and the line
4881 * ends in white space.
4882 * - Otherwise:
4883 * - Don't do this if inserting a blank
4884 * - Don't do this if an existing character is being replaced, unless
4885 * we're in VREPLACE mode.
4886 * - Do this if the cursor is not on the line where insert started
4887 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4888 * before the insert.
4889 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4890 * before 'textwidth'
4891 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004892 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 && ((flags & INSCHAR_FORMAT)
4894 || (!vim_iswhite(c)
4895 && !((State & REPLACE_FLAG)
4896#ifdef FEAT_VREPLACE
4897 && !(State & VREPLACE_FLAG)
4898#endif
4899 && *ml_get_cursor() != NUL)
4900 && (curwin->w_cursor.lnum != Insstart.lnum
4901 || ((!has_format_option(FO_INS_LONG)
4902 || Insstart_textlen <= (colnr_T)textwidth)
4903 && (!fo_ins_blank
4904 || Insstart_blank_vcol <= (colnr_T)textwidth
4905 ))))))
4906 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004907 /* Format with 'formatexpr' when it's set. Use internal formatting
4908 * when 'formatexpr' isn't set or it returns non-zero. */
4909#if defined(FEAT_EVAL)
4910 if (*curbuf->b_p_fex == NUL
4911 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004913 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004915
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (c == NUL) /* only formatting was wanted */
4917 return;
4918
4919#ifdef FEAT_COMMENTS
4920 /* Check whether this character should end a comment. */
4921 if (did_ai && (int)c == end_comment_pending)
4922 {
4923 char_u *line;
4924 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4925 int middle_len, end_len;
4926 int i;
4927
4928 /*
4929 * Need to remove existing (middle) comment leader and insert end
4930 * comment leader. First, check what comment leader we can find.
4931 */
4932 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4933 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4934 {
4935 /* Skip middle-comment string */
4936 while (*p && p[-1] != ':') /* find end of middle flags */
4937 ++p;
4938 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4939 /* Don't count trailing white space for middle_len */
4940 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4941 --middle_len;
4942
4943 /* Find the end-comment string */
4944 while (*p && p[-1] != ':') /* find end of end flags */
4945 ++p;
4946 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4947
4948 /* Skip white space before the cursor */
4949 i = curwin->w_cursor.col;
4950 while (--i >= 0 && vim_iswhite(line[i]))
4951 ;
4952 i++;
4953
4954 /* Skip to before the middle leader */
4955 i -= middle_len;
4956
4957 /* Check some expected things before we go on */
4958 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4959 {
4960 /* Backspace over all the stuff we want to replace */
4961 backspace_until_column(i);
4962
4963 /*
4964 * Insert the end-comment string, except for the last
4965 * character, which will get inserted as normal later.
4966 */
4967 ins_bytes_len(lead_end, end_len - 1);
4968 }
4969 }
4970 }
4971 end_comment_pending = NUL;
4972#endif
4973
4974 did_ai = FALSE;
4975#ifdef FEAT_SMARTINDENT
4976 did_si = FALSE;
4977 can_si = FALSE;
4978 can_si_back = FALSE;
4979#endif
4980
4981 /*
4982 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4983 * This speeds up normal text input considerably.
4984 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4985 * need to re-indent at a ':', or any other character (but not what
4986 * 'paste' is set)..
4987 */
4988#ifdef USE_ON_FLY_SCROLL
4989 dont_scroll = FALSE; /* allow scrolling here */
4990#endif
4991
4992 if ( !ISSPECIAL(c)
4993#ifdef FEAT_MBYTE
4994 && (!has_mbyte || (*mb_char2len)(c) == 1)
4995#endif
4996 && vpeekc() != NUL
4997 && !(State & REPLACE_FLAG)
4998#ifdef FEAT_CINDENT
4999 && !cindent_on()
5000#endif
5001#ifdef FEAT_RIGHTLEFT
5002 && !p_ri
5003#endif
5004 )
5005 {
5006#define INPUT_BUFLEN 100
5007 char_u buf[INPUT_BUFLEN + 1];
5008 int i;
5009 colnr_T virtcol = 0;
5010
5011 buf[0] = c;
5012 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005013 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 virtcol = get_nolist_virtcol();
5015 /*
5016 * Stop the string when:
5017 * - no more chars available
5018 * - finding a special character (command key)
5019 * - buffer is full
5020 * - running into the 'textwidth' boundary
5021 * - need to check for abbreviation: A non-word char after a word-char
5022 */
5023 while ( (c = vpeekc()) != NUL
5024 && !ISSPECIAL(c)
5025#ifdef FEAT_MBYTE
5026 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5027#endif
5028 && i < INPUT_BUFLEN
5029 && (textwidth == 0
5030 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5031 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5032 {
5033#ifdef FEAT_RIGHTLEFT
5034 c = vgetc();
5035 if (p_hkmap && KeyTyped)
5036 c = hkmap(c); /* Hebrew mode mapping */
5037# ifdef FEAT_FKMAP
5038 if (p_fkmap && KeyTyped)
5039 c = fkmap(c); /* Farsi mode mapping */
5040# endif
5041 buf[i++] = c;
5042#else
5043 buf[i++] = vgetc();
5044#endif
5045 }
5046
5047#ifdef FEAT_DIGRAPHS
5048 do_digraph(-1); /* clear digraphs */
5049 do_digraph(buf[i-1]); /* may be the start of a digraph */
5050#endif
5051 buf[i] = NUL;
5052 ins_str(buf);
5053 if (flags & INSCHAR_CTRLV)
5054 {
5055 redo_literal(*buf);
5056 i = 1;
5057 }
5058 else
5059 i = 0;
5060 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005061 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
5063 else
5064 {
5065#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005066 int cc;
5067
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5069 {
5070 char_u buf[MB_MAXBYTES + 1];
5071
5072 (*mb_char2bytes)(c, buf);
5073 buf[cc] = NUL;
5074 ins_char_bytes(buf, cc);
5075 AppendCharToRedobuff(c);
5076 }
5077 else
5078#endif
5079 {
5080 ins_char(c);
5081 if (flags & INSCHAR_CTRLV)
5082 redo_literal(c);
5083 else
5084 AppendCharToRedobuff(c);
5085 }
5086 }
5087}
5088
5089/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005090 * Format text at the current insert position.
5091 */
5092 static void
5093internal_format(textwidth, second_indent, flags, format_only)
5094 int textwidth;
5095 int second_indent;
5096 int flags;
5097 int format_only;
5098{
5099 int cc;
5100 int save_char = NUL;
5101 int haveto_redraw = FALSE;
5102 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5103#ifdef FEAT_MBYTE
5104 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5105#endif
5106 int fo_white_par = has_format_option(FO_WHITE_PAR);
5107 int first_line = TRUE;
5108#ifdef FEAT_COMMENTS
5109 colnr_T leader_len;
5110 int no_leader = FALSE;
5111 int do_comments = (flags & INSCHAR_DO_COM);
5112#endif
5113
5114 /*
5115 * When 'ai' is off we don't want a space under the cursor to be
5116 * deleted. Replace it with an 'x' temporarily.
5117 */
5118 if (!curbuf->b_p_ai)
5119 {
5120 cc = gchar_cursor();
5121 if (vim_iswhite(cc))
5122 {
5123 save_char = cc;
5124 pchar_cursor('x');
5125 }
5126 }
5127
5128 /*
5129 * Repeat breaking lines, until the current line is not too long.
5130 */
5131 while (!got_int)
5132 {
5133 int startcol; /* Cursor column at entry */
5134 int wantcol; /* column at textwidth border */
5135 int foundcol; /* column for start of spaces */
5136 int end_foundcol = 0; /* column for start of word */
5137 colnr_T len;
5138 colnr_T virtcol;
5139#ifdef FEAT_VREPLACE
5140 int orig_col = 0;
5141 char_u *saved_text = NULL;
5142#endif
5143 colnr_T col;
5144
5145 virtcol = get_nolist_virtcol();
5146 if (virtcol < (colnr_T)textwidth)
5147 break;
5148
5149#ifdef FEAT_COMMENTS
5150 if (no_leader)
5151 do_comments = FALSE;
5152 else if (!(flags & INSCHAR_FORMAT)
5153 && has_format_option(FO_WRAP_COMS))
5154 do_comments = TRUE;
5155
5156 /* Don't break until after the comment leader */
5157 if (do_comments)
5158 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5159 else
5160 leader_len = 0;
5161
5162 /* If the line doesn't start with a comment leader, then don't
5163 * start one in a following broken line. Avoids that a %word
5164 * moved to the start of the next line causes all following lines
5165 * to start with %. */
5166 if (leader_len == 0)
5167 no_leader = TRUE;
5168#endif
5169 if (!(flags & INSCHAR_FORMAT)
5170#ifdef FEAT_COMMENTS
5171 && leader_len == 0
5172#endif
5173 && !has_format_option(FO_WRAP))
5174
5175 {
5176 textwidth = 0;
5177 break;
5178 }
5179 if ((startcol = curwin->w_cursor.col) == 0)
5180 break;
5181
5182 /* find column of textwidth border */
5183 coladvance((colnr_T)textwidth);
5184 wantcol = curwin->w_cursor.col;
5185
5186 curwin->w_cursor.col = startcol - 1;
5187#ifdef FEAT_MBYTE
5188 /* Correct cursor for multi-byte character. */
5189 if (has_mbyte)
5190 mb_adjust_cursor();
5191#endif
5192 foundcol = 0;
5193
5194 /*
5195 * Find position to break at.
5196 * Stop at first entered white when 'formatoptions' has 'v'
5197 */
5198 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5199 || curwin->w_cursor.lnum != Insstart.lnum
5200 || curwin->w_cursor.col >= Insstart.col)
5201 {
5202 cc = gchar_cursor();
5203 if (WHITECHAR(cc))
5204 {
5205 /* remember position of blank just before text */
5206 end_foundcol = curwin->w_cursor.col;
5207
5208 /* find start of sequence of blanks */
5209 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5210 {
5211 dec_cursor();
5212 cc = gchar_cursor();
5213 }
5214 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5215 break; /* only spaces in front of text */
5216#ifdef FEAT_COMMENTS
5217 /* Don't break until after the comment leader */
5218 if (curwin->w_cursor.col < leader_len)
5219 break;
5220#endif
5221 if (has_format_option(FO_ONE_LETTER))
5222 {
5223 /* do not break after one-letter words */
5224 if (curwin->w_cursor.col == 0)
5225 break; /* one-letter word at begin */
5226
5227 col = curwin->w_cursor.col;
5228 dec_cursor();
5229 cc = gchar_cursor();
5230
5231 if (WHITECHAR(cc))
5232 continue; /* one-letter, continue */
5233 curwin->w_cursor.col = col;
5234 }
5235#ifdef FEAT_MBYTE
5236 if (has_mbyte)
5237 foundcol = curwin->w_cursor.col
5238 + (*mb_ptr2len)(ml_get_cursor());
5239 else
5240#endif
5241 foundcol = curwin->w_cursor.col + 1;
5242 if (curwin->w_cursor.col < (colnr_T)wantcol)
5243 break;
5244 }
5245#ifdef FEAT_MBYTE
5246 else if (cc >= 0x100 && fo_multibyte
5247 && curwin->w_cursor.col <= (colnr_T)wantcol)
5248 {
5249 /* Break after or before a multi-byte character. */
5250 foundcol = curwin->w_cursor.col;
5251 if (curwin->w_cursor.col < (colnr_T)wantcol)
5252 foundcol += (*mb_char2len)(cc);
5253 end_foundcol = foundcol;
5254 break;
5255 }
5256#endif
5257 if (curwin->w_cursor.col == 0)
5258 break;
5259 dec_cursor();
5260 }
5261
5262 if (foundcol == 0) /* no spaces, cannot break line */
5263 {
5264 curwin->w_cursor.col = startcol;
5265 break;
5266 }
5267
5268 /* Going to break the line, remove any "$" now. */
5269 undisplay_dollar();
5270
5271 /*
5272 * Offset between cursor position and line break is used by replace
5273 * stack functions. VREPLACE does not use this, and backspaces
5274 * over the text instead.
5275 */
5276#ifdef FEAT_VREPLACE
5277 if (State & VREPLACE_FLAG)
5278 orig_col = startcol; /* Will start backspacing from here */
5279 else
5280#endif
5281 replace_offset = startcol - end_foundcol - 1;
5282
5283 /*
5284 * adjust startcol for spaces that will be deleted and
5285 * characters that will remain on top line
5286 */
5287 curwin->w_cursor.col = foundcol;
5288 while (cc = gchar_cursor(), WHITECHAR(cc))
5289 inc_cursor();
5290 startcol -= curwin->w_cursor.col;
5291 if (startcol < 0)
5292 startcol = 0;
5293
5294#ifdef FEAT_VREPLACE
5295 if (State & VREPLACE_FLAG)
5296 {
5297 /*
5298 * In VREPLACE mode, we will backspace over the text to be
5299 * wrapped, so save a copy now to put on the next line.
5300 */
5301 saved_text = vim_strsave(ml_get_cursor());
5302 curwin->w_cursor.col = orig_col;
5303 if (saved_text == NULL)
5304 break; /* Can't do it, out of memory */
5305 saved_text[startcol] = NUL;
5306
5307 /* Backspace over characters that will move to the next line */
5308 if (!fo_white_par)
5309 backspace_until_column(foundcol);
5310 }
5311 else
5312#endif
5313 {
5314 /* put cursor after pos. to break line */
5315 if (!fo_white_par)
5316 curwin->w_cursor.col = foundcol;
5317 }
5318
5319 /*
5320 * Split the line just before the margin.
5321 * Only insert/delete lines, but don't really redraw the window.
5322 */
5323 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5324 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5325#ifdef FEAT_COMMENTS
5326 + (do_comments ? OPENLINE_DO_COM : 0)
5327#endif
5328 , old_indent);
5329 old_indent = 0;
5330
5331 replace_offset = 0;
5332 if (first_line)
5333 {
5334 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5335 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5336 if (second_indent >= 0)
5337 {
5338#ifdef FEAT_VREPLACE
5339 if (State & VREPLACE_FLAG)
5340 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5341 else
5342#endif
5343 (void)set_indent(second_indent, SIN_CHANGED);
5344 }
5345 first_line = FALSE;
5346 }
5347
5348#ifdef FEAT_VREPLACE
5349 if (State & VREPLACE_FLAG)
5350 {
5351 /*
5352 * In VREPLACE mode we have backspaced over the text to be
5353 * moved, now we re-insert it into the new line.
5354 */
5355 ins_bytes(saved_text);
5356 vim_free(saved_text);
5357 }
5358 else
5359#endif
5360 {
5361 /*
5362 * Check if cursor is not past the NUL off the line, cindent
5363 * may have added or removed indent.
5364 */
5365 curwin->w_cursor.col += startcol;
5366 len = (colnr_T)STRLEN(ml_get_curline());
5367 if (curwin->w_cursor.col > len)
5368 curwin->w_cursor.col = len;
5369 }
5370
5371 haveto_redraw = TRUE;
5372#ifdef FEAT_CINDENT
5373 can_cindent = TRUE;
5374#endif
5375 /* moved the cursor, don't autoindent or cindent now */
5376 did_ai = FALSE;
5377#ifdef FEAT_SMARTINDENT
5378 did_si = FALSE;
5379 can_si = FALSE;
5380 can_si_back = FALSE;
5381#endif
5382 line_breakcheck();
5383 }
5384
5385 if (save_char != NUL) /* put back space after cursor */
5386 pchar_cursor(save_char);
5387
5388 if (!format_only && haveto_redraw)
5389 {
5390 update_topline();
5391 redraw_curbuf_later(VALID);
5392 }
5393}
5394
5395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 * Called after inserting or deleting text: When 'formatoptions' includes the
5397 * 'a' flag format from the current line until the end of the paragraph.
5398 * Keep the cursor at the same position relative to the text.
5399 * The caller must have saved the cursor line for undo, following ones will be
5400 * saved here.
5401 */
5402 void
5403auto_format(trailblank, prev_line)
5404 int trailblank; /* when TRUE also format with trailing blank */
5405 int prev_line; /* may start in previous line */
5406{
5407 pos_T pos;
5408 colnr_T len;
5409 char_u *old;
5410 char_u *new, *pnew;
5411 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005412 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413
5414 if (!has_format_option(FO_AUTO))
5415 return;
5416
5417 pos = curwin->w_cursor;
5418 old = ml_get_curline();
5419
5420 /* may remove added space */
5421 check_auto_format(FALSE);
5422
5423 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5424 * user might insert normal text next. Also skip formatting when "1" is
5425 * in 'formatoptions' and there is a single character before the cursor.
5426 * Otherwise the line would be broken and when typing another non-white
5427 * next they are not joined back together. */
5428 wasatend = (pos.col == STRLEN(old));
5429 if (*old != NUL && !trailblank && wasatend)
5430 {
5431 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005432 cc = gchar_cursor();
5433 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5434 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005436 cc = gchar_cursor();
5437 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 {
5439 curwin->w_cursor = pos;
5440 return;
5441 }
5442 curwin->w_cursor = pos;
5443 }
5444
5445#ifdef FEAT_COMMENTS
5446 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5447 * comments. */
5448 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5449 && get_leader_len(old, NULL, FALSE) == 0)
5450 return;
5451#endif
5452
5453 /*
5454 * May start formatting in a previous line, so that after "x" a word is
5455 * moved to the previous line if it fits there now. Only when this is not
5456 * the start of a paragraph.
5457 */
5458 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5459 {
5460 --curwin->w_cursor.lnum;
5461 if (u_save_cursor() == FAIL)
5462 return;
5463 }
5464
5465 /*
5466 * Do the formatting and restore the cursor position. "saved_cursor" will
5467 * be adjusted for the text formatting.
5468 */
5469 saved_cursor = pos;
5470 format_lines((linenr_T)-1);
5471 curwin->w_cursor = saved_cursor;
5472 saved_cursor.lnum = 0;
5473
5474 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5475 {
5476 /* "cannot happen" */
5477 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5478 coladvance((colnr_T)MAXCOL);
5479 }
5480 else
5481 check_cursor_col();
5482
5483 /* Insert mode: If the cursor is now after the end of the line while it
5484 * previously wasn't, the line was broken. Because of the rule above we
5485 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5486 * formatted. */
5487 if (!wasatend && has_format_option(FO_WHITE_PAR))
5488 {
5489 new = ml_get_curline();
5490 len = STRLEN(new);
5491 if (curwin->w_cursor.col == len)
5492 {
5493 pnew = vim_strnsave(new, len + 2);
5494 pnew[len] = ' ';
5495 pnew[len + 1] = NUL;
5496 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5497 /* remove the space later */
5498 did_add_space = TRUE;
5499 }
5500 else
5501 /* may remove added space */
5502 check_auto_format(FALSE);
5503 }
5504
5505 check_cursor();
5506}
5507
5508/*
5509 * When an extra space was added to continue a paragraph for auto-formatting,
5510 * delete it now. The space must be under the cursor, just after the insert
5511 * position.
5512 */
5513 static void
5514check_auto_format(end_insert)
5515 int end_insert; /* TRUE when ending Insert mode */
5516{
5517 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005518 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
5520 if (did_add_space)
5521 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005522 cc = gchar_cursor();
5523 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 /* Somehow the space was removed already. */
5525 did_add_space = FALSE;
5526 else
5527 {
5528 if (!end_insert)
5529 {
5530 inc_cursor();
5531 c = gchar_cursor();
5532 dec_cursor();
5533 }
5534 if (c != NUL)
5535 {
5536 /* The space is no longer at the end of the line, delete it. */
5537 del_char(FALSE);
5538 did_add_space = FALSE;
5539 }
5540 }
5541 }
5542}
5543
5544/*
5545 * Find out textwidth to be used for formatting:
5546 * if 'textwidth' option is set, use it
5547 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5548 * if invalid value, use 0.
5549 * Set default to window width (maximum 79) for "gq" operator.
5550 */
5551 int
5552comp_textwidth(ff)
5553 int ff; /* force formatting (for "Q" command) */
5554{
5555 int textwidth;
5556
5557 textwidth = curbuf->b_p_tw;
5558 if (textwidth == 0 && curbuf->b_p_wm)
5559 {
5560 /* The width is the window width minus 'wrapmargin' minus all the
5561 * things that add to the margin. */
5562 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5563#ifdef FEAT_CMDWIN
5564 if (cmdwin_type != 0)
5565 textwidth -= 1;
5566#endif
5567#ifdef FEAT_FOLDING
5568 textwidth -= curwin->w_p_fdc;
5569#endif
5570#ifdef FEAT_SIGNS
5571 if (curwin->w_buffer->b_signlist != NULL
5572# ifdef FEAT_NETBEANS_INTG
5573 || usingNetbeans
5574# endif
5575 )
5576 textwidth -= 1;
5577#endif
5578 if (curwin->w_p_nu)
5579 textwidth -= 8;
5580 }
5581 if (textwidth < 0)
5582 textwidth = 0;
5583 if (ff && textwidth == 0)
5584 {
5585 textwidth = W_WIDTH(curwin) - 1;
5586 if (textwidth > 79)
5587 textwidth = 79;
5588 }
5589 return textwidth;
5590}
5591
5592/*
5593 * Put a character in the redo buffer, for when just after a CTRL-V.
5594 */
5595 static void
5596redo_literal(c)
5597 int c;
5598{
5599 char_u buf[10];
5600
5601 /* Only digits need special treatment. Translate them into a string of
5602 * three digits. */
5603 if (VIM_ISDIGIT(c))
5604 {
5605 sprintf((char *)buf, "%03d", c);
5606 AppendToRedobuff(buf);
5607 }
5608 else
5609 AppendCharToRedobuff(c);
5610}
5611
5612/*
5613 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005614 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 */
5616 static void
5617start_arrow(end_insert_pos)
5618 pos_T *end_insert_pos;
5619{
5620 if (!arrow_used) /* something has been inserted */
5621 {
5622 AppendToRedobuff(ESC_STR);
5623 stop_insert(end_insert_pos, FALSE);
5624 arrow_used = TRUE; /* this means we stopped the current insert */
5625 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005626#ifdef FEAT_SYN_HL
5627 check_spell_redraw();
5628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629}
5630
Bram Moolenaar217ad922005-03-20 22:37:15 +00005631#ifdef FEAT_SYN_HL
5632/*
5633 * If we skipped highlighting word at cursor, do it now.
5634 * It may be skipped again, thus reset spell_redraw_lnum first.
5635 */
5636 static void
5637check_spell_redraw()
5638{
5639 if (spell_redraw_lnum != 0)
5640 {
5641 linenr_T lnum = spell_redraw_lnum;
5642
5643 spell_redraw_lnum = 0;
5644 redrawWinline(lnum, FALSE);
5645 }
5646}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005647
5648/*
5649 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5650 * spelled word, if there is one.
5651 */
5652 static void
5653spell_back_to_badword()
5654{
5655 pos_T tpos = curwin->w_cursor;
5656
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005657 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005658 if (curwin->w_cursor.col != tpos.col)
5659 start_arrow(&tpos);
5660}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005661#endif
5662
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663/*
5664 * stop_arrow() is called before a change is made in insert mode.
5665 * If an arrow key has been used, start a new insertion.
5666 * Returns FAIL if undo is impossible, shouldn't insert then.
5667 */
5668 int
5669stop_arrow()
5670{
5671 if (arrow_used)
5672 {
5673 if (u_save_cursor() == OK)
5674 {
5675 arrow_used = FALSE;
5676 ins_need_undo = FALSE;
5677 }
5678 Insstart = curwin->w_cursor; /* new insertion starts here */
5679 Insstart_textlen = linetabsize(ml_get_curline());
5680 ai_col = 0;
5681#ifdef FEAT_VREPLACE
5682 if (State & VREPLACE_FLAG)
5683 {
5684 orig_line_count = curbuf->b_ml.ml_line_count;
5685 vr_lines_changed = 1;
5686 }
5687#endif
5688 ResetRedobuff();
5689 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005690 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692 else if (ins_need_undo)
5693 {
5694 if (u_save_cursor() == OK)
5695 ins_need_undo = FALSE;
5696 }
5697
5698#ifdef FEAT_FOLDING
5699 /* Always open fold at the cursor line when inserting something. */
5700 foldOpenCursor();
5701#endif
5702
5703 return (arrow_used || ins_need_undo ? FAIL : OK);
5704}
5705
5706/*
5707 * do a few things to stop inserting
5708 */
5709 static void
5710stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005711 pos_T *end_insert_pos; /* where insert ended */
5712 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005714 int cc;
5715 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
5717 stop_redo_ins();
5718 replace_flush(); /* abandon replace stack */
5719
5720 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005721 * Save the inserted text for later redo with ^@ and CTRL-A.
5722 * Don't do it when "restart_edit" was set and nothing was inserted,
5723 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005725 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005726 if (did_restart_edit == 0 || (ptr != NULL
5727 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005728 {
5729 vim_free(last_insert);
5730 last_insert = ptr;
5731 last_insert_skip = new_insert_skip;
5732 }
5733 else
5734 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
5736 if (!arrow_used)
5737 {
5738 /* Auto-format now. It may seem strange to do this when stopping an
5739 * insertion (or moving the cursor), but it's required when appending
5740 * a line and having it end in a space. But only do it when something
5741 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005742 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005744 pos_T tpos = curwin->w_cursor;
5745
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 /* When the cursor is at the end of the line after a space the
5747 * formatting will move it to the following word. Avoid that by
5748 * moving the cursor onto the space. */
5749 cc = 'x';
5750 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5751 {
5752 dec_cursor();
5753 cc = gchar_cursor();
5754 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005755 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 }
5757
5758 auto_format(TRUE, FALSE);
5759
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005760 if (vim_iswhite(cc))
5761 {
5762 if (gchar_cursor() != NUL)
5763 inc_cursor();
5764#ifdef FEAT_VIRTUALEDIT
5765 /* If the cursor is still at the same character, also keep
5766 * the "coladd". */
5767 if (gchar_cursor() == NUL
5768 && curwin->w_cursor.lnum == tpos.lnum
5769 && curwin->w_cursor.col == tpos.col)
5770 curwin->w_cursor.coladd = tpos.coladd;
5771#endif
5772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 }
5774
5775 /* If a space was inserted for auto-formatting, remove it now. */
5776 check_auto_format(TRUE);
5777
5778 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005779 * of the line, and put the cursor back.
5780 * Do this when ESC was used or moving the cursor up/down. */
5781 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5782 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005784 pos_T tpos = curwin->w_cursor;
5785
5786 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5788 --curwin->w_cursor.col;
5789 while (cc = gchar_cursor(), vim_iswhite(cc))
5790 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005791 if (curwin->w_cursor.lnum != tpos.lnum)
5792 curwin->w_cursor = tpos;
5793 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5795
5796#ifdef FEAT_VISUAL
5797 /* <C-S-Right> may have started Visual mode, adjust the position for
5798 * deleted characters. */
5799 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5800 {
5801 cc = STRLEN(ml_get_curline());
5802 if (VIsual.col > (colnr_T)cc)
5803 {
5804 VIsual.col = cc;
5805# ifdef FEAT_VIRTUALEDIT
5806 VIsual.coladd = 0;
5807# endif
5808 }
5809 }
5810#endif
5811 }
5812 }
5813 did_ai = FALSE;
5814#ifdef FEAT_SMARTINDENT
5815 did_si = FALSE;
5816 can_si = FALSE;
5817 can_si_back = FALSE;
5818#endif
5819
5820 /* set '[ and '] to the inserted text */
5821 curbuf->b_op_start = Insstart;
5822 curbuf->b_op_end = *end_insert_pos;
5823}
5824
5825/*
5826 * Set the last inserted text to a single character.
5827 * Used for the replace command.
5828 */
5829 void
5830set_last_insert(c)
5831 int c;
5832{
5833 char_u *s;
5834
5835 vim_free(last_insert);
5836#ifdef FEAT_MBYTE
5837 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5838#else
5839 last_insert = alloc(6);
5840#endif
5841 if (last_insert != NULL)
5842 {
5843 s = last_insert;
5844 /* Use the CTRL-V only when entering a special char */
5845 if (c < ' ' || c == DEL)
5846 *s++ = Ctrl_V;
5847 s = add_char2buf(c, s);
5848 *s++ = ESC;
5849 *s++ = NUL;
5850 last_insert_skip = 0;
5851 }
5852}
5853
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005854#if defined(EXITFREE) || defined(PROTO)
5855 void
5856free_last_insert()
5857{
5858 vim_free(last_insert);
5859 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005860 vim_free(compl_orig_text);
5861 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005862}
5863#endif
5864
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865/*
5866 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5867 * and CSI. Handle multi-byte characters.
5868 * Returns a pointer to after the added bytes.
5869 */
5870 char_u *
5871add_char2buf(c, s)
5872 int c;
5873 char_u *s;
5874{
5875#ifdef FEAT_MBYTE
5876 char_u temp[MB_MAXBYTES];
5877 int i;
5878 int len;
5879
5880 len = (*mb_char2bytes)(c, temp);
5881 for (i = 0; i < len; ++i)
5882 {
5883 c = temp[i];
5884#endif
5885 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5886 if (c == K_SPECIAL)
5887 {
5888 *s++ = K_SPECIAL;
5889 *s++ = KS_SPECIAL;
5890 *s++ = KE_FILLER;
5891 }
5892#ifdef FEAT_GUI
5893 else if (c == CSI)
5894 {
5895 *s++ = CSI;
5896 *s++ = KS_EXTRA;
5897 *s++ = (int)KE_CSI;
5898 }
5899#endif
5900 else
5901 *s++ = c;
5902#ifdef FEAT_MBYTE
5903 }
5904#endif
5905 return s;
5906}
5907
5908/*
5909 * move cursor to start of line
5910 * if flags & BL_WHITE move to first non-white
5911 * if flags & BL_SOL move to first non-white if startofline is set,
5912 * otherwise keep "curswant" column
5913 * if flags & BL_FIX don't leave the cursor on a NUL.
5914 */
5915 void
5916beginline(flags)
5917 int flags;
5918{
5919 if ((flags & BL_SOL) && !p_sol)
5920 coladvance(curwin->w_curswant);
5921 else
5922 {
5923 curwin->w_cursor.col = 0;
5924#ifdef FEAT_VIRTUALEDIT
5925 curwin->w_cursor.coladd = 0;
5926#endif
5927
5928 if (flags & (BL_WHITE | BL_SOL))
5929 {
5930 char_u *ptr;
5931
5932 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5933 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5934 ++curwin->w_cursor.col;
5935 }
5936 curwin->w_set_curswant = TRUE;
5937 }
5938}
5939
5940/*
5941 * oneright oneleft cursor_down cursor_up
5942 *
5943 * Move one char {right,left,down,up}.
5944 * Doesn't move onto the NUL past the end of the line.
5945 * Return OK when successful, FAIL when we hit a line of file boundary.
5946 */
5947
5948 int
5949oneright()
5950{
5951 char_u *ptr;
5952#ifdef FEAT_MBYTE
5953 int l;
5954#endif
5955
5956#ifdef FEAT_VIRTUALEDIT
5957 if (virtual_active())
5958 {
5959 pos_T prevpos = curwin->w_cursor;
5960
5961 /* Adjust for multi-wide char (excluding TAB) */
5962 ptr = ml_get_cursor();
5963 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5964#ifdef FEAT_MBYTE
5965 (*mb_ptr2char)(ptr)
5966#else
5967 *ptr
5968#endif
5969 ))
5970 ? ptr2cells(ptr) : 1));
5971 curwin->w_set_curswant = TRUE;
5972 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5973 return (prevpos.col != curwin->w_cursor.col
5974 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5975 }
5976#endif
5977
5978 ptr = ml_get_cursor();
5979#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005980 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 {
5982 /* The character under the cursor is a multi-byte character, move
5983 * several bytes right, but don't end up on the NUL. */
5984 if (ptr[l] == NUL)
5985 return FAIL;
5986 curwin->w_cursor.col += l;
5987 }
5988 else
5989#endif
5990 {
5991 if (*ptr++ == NUL || *ptr == NUL)
5992 return FAIL;
5993 ++curwin->w_cursor.col;
5994 }
5995
5996 curwin->w_set_curswant = TRUE;
5997 return OK;
5998}
5999
6000 int
6001oneleft()
6002{
6003#ifdef FEAT_VIRTUALEDIT
6004 if (virtual_active())
6005 {
6006 int width;
6007 int v = getviscol();
6008
6009 if (v == 0)
6010 return FAIL;
6011
6012# ifdef FEAT_LINEBREAK
6013 /* We might get stuck on 'showbreak', skip over it. */
6014 width = 1;
6015 for (;;)
6016 {
6017 coladvance(v - width);
6018 /* getviscol() is slow, skip it when 'showbreak' is empty and
6019 * there are no multi-byte characters */
6020 if ((*p_sbr == NUL
6021# ifdef FEAT_MBYTE
6022 && !has_mbyte
6023# endif
6024 ) || getviscol() < v)
6025 break;
6026 ++width;
6027 }
6028# else
6029 coladvance(v - 1);
6030# endif
6031
6032 if (curwin->w_cursor.coladd == 1)
6033 {
6034 char_u *ptr;
6035
6036 /* Adjust for multi-wide char (not a TAB) */
6037 ptr = ml_get_cursor();
6038 if (*ptr != TAB && vim_isprintc(
6039# ifdef FEAT_MBYTE
6040 (*mb_ptr2char)(ptr)
6041# else
6042 *ptr
6043# endif
6044 ) && ptr2cells(ptr) > 1)
6045 curwin->w_cursor.coladd = 0;
6046 }
6047
6048 curwin->w_set_curswant = TRUE;
6049 return OK;
6050 }
6051#endif
6052
6053 if (curwin->w_cursor.col == 0)
6054 return FAIL;
6055
6056 curwin->w_set_curswant = TRUE;
6057 --curwin->w_cursor.col;
6058
6059#ifdef FEAT_MBYTE
6060 /* if the character on the left of the current cursor is a multi-byte
6061 * character, move to its first byte */
6062 if (has_mbyte)
6063 mb_adjust_cursor();
6064#endif
6065 return OK;
6066}
6067
6068 int
6069cursor_up(n, upd_topline)
6070 long n;
6071 int upd_topline; /* When TRUE: update topline */
6072{
6073 linenr_T lnum;
6074
6075 if (n > 0)
6076 {
6077 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006078 /* This fails if the cursor is already in the first line or the count
6079 * is larger than the line number and '-' is in 'cpoptions' */
6080 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 return FAIL;
6082 if (n >= lnum)
6083 lnum = 1;
6084 else
6085#ifdef FEAT_FOLDING
6086 if (hasAnyFolding(curwin))
6087 {
6088 /*
6089 * Count each sequence of folded lines as one logical line.
6090 */
6091 /* go to the the start of the current fold */
6092 (void)hasFolding(lnum, &lnum, NULL);
6093
6094 while (n--)
6095 {
6096 /* move up one line */
6097 --lnum;
6098 if (lnum <= 1)
6099 break;
6100 /* If we entered a fold, move to the beginning, unless in
6101 * Insert mode or when 'foldopen' contains "all": it will open
6102 * in a moment. */
6103 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6104 (void)hasFolding(lnum, &lnum, NULL);
6105 }
6106 if (lnum < 1)
6107 lnum = 1;
6108 }
6109 else
6110#endif
6111 lnum -= n;
6112 curwin->w_cursor.lnum = lnum;
6113 }
6114
6115 /* try to advance to the column we want to be at */
6116 coladvance(curwin->w_curswant);
6117
6118 if (upd_topline)
6119 update_topline(); /* make sure curwin->w_topline is valid */
6120
6121 return OK;
6122}
6123
6124/*
6125 * Cursor down a number of logical lines.
6126 */
6127 int
6128cursor_down(n, upd_topline)
6129 long n;
6130 int upd_topline; /* When TRUE: update topline */
6131{
6132 linenr_T lnum;
6133
6134 if (n > 0)
6135 {
6136 lnum = curwin->w_cursor.lnum;
6137#ifdef FEAT_FOLDING
6138 /* Move to last line of fold, will fail if it's the end-of-file. */
6139 (void)hasFolding(lnum, NULL, &lnum);
6140#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006141 /* This fails if the cursor is already in the last line or would move
6142 * beyound the last line and '-' is in 'cpoptions' */
6143 if (lnum >= curbuf->b_ml.ml_line_count
6144 || (lnum + n > curbuf->b_ml.ml_line_count
6145 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 return FAIL;
6147 if (lnum + n >= curbuf->b_ml.ml_line_count)
6148 lnum = curbuf->b_ml.ml_line_count;
6149 else
6150#ifdef FEAT_FOLDING
6151 if (hasAnyFolding(curwin))
6152 {
6153 linenr_T last;
6154
6155 /* count each sequence of folded lines as one logical line */
6156 while (n--)
6157 {
6158 if (hasFolding(lnum, NULL, &last))
6159 lnum = last + 1;
6160 else
6161 ++lnum;
6162 if (lnum >= curbuf->b_ml.ml_line_count)
6163 break;
6164 }
6165 if (lnum > curbuf->b_ml.ml_line_count)
6166 lnum = curbuf->b_ml.ml_line_count;
6167 }
6168 else
6169#endif
6170 lnum += n;
6171 curwin->w_cursor.lnum = lnum;
6172 }
6173
6174 /* try to advance to the column we want to be at */
6175 coladvance(curwin->w_curswant);
6176
6177 if (upd_topline)
6178 update_topline(); /* make sure curwin->w_topline is valid */
6179
6180 return OK;
6181}
6182
6183/*
6184 * Stuff the last inserted text in the read buffer.
6185 * Last_insert actually is a copy of the redo buffer, so we
6186 * first have to remove the command.
6187 */
6188 int
6189stuff_inserted(c, count, no_esc)
6190 int c; /* Command character to be inserted */
6191 long count; /* Repeat this many times */
6192 int no_esc; /* Don't add an ESC at the end */
6193{
6194 char_u *esc_ptr;
6195 char_u *ptr;
6196 char_u *last_ptr;
6197 char_u last = NUL;
6198
6199 ptr = get_last_insert();
6200 if (ptr == NULL)
6201 {
6202 EMSG(_(e_noinstext));
6203 return FAIL;
6204 }
6205
6206 /* may want to stuff the command character, to start Insert mode */
6207 if (c != NUL)
6208 stuffcharReadbuff(c);
6209 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6210 *esc_ptr = NUL; /* remove the ESC */
6211
6212 /* when the last char is either "0" or "^" it will be quoted if no ESC
6213 * comes after it OR if it will inserted more than once and "ptr"
6214 * starts with ^D. -- Acevedo
6215 */
6216 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6217 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6218 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6219 {
6220 last = *last_ptr;
6221 *last_ptr = NUL;
6222 }
6223
6224 do
6225 {
6226 stuffReadbuff(ptr);
6227 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6228 if (last)
6229 stuffReadbuff((char_u *)(last == '0'
6230 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6231 : IF_EB("\026^", CTRL_V_STR "^")));
6232 }
6233 while (--count > 0);
6234
6235 if (last)
6236 *last_ptr = last;
6237
6238 if (esc_ptr != NULL)
6239 *esc_ptr = ESC; /* put the ESC back */
6240
6241 /* may want to stuff a trailing ESC, to get out of Insert mode */
6242 if (!no_esc)
6243 stuffcharReadbuff(ESC);
6244
6245 return OK;
6246}
6247
6248 char_u *
6249get_last_insert()
6250{
6251 if (last_insert == NULL)
6252 return NULL;
6253 return last_insert + last_insert_skip;
6254}
6255
6256/*
6257 * Get last inserted string, and remove trailing <Esc>.
6258 * Returns pointer to allocated memory (must be freed) or NULL.
6259 */
6260 char_u *
6261get_last_insert_save()
6262{
6263 char_u *s;
6264 int len;
6265
6266 if (last_insert == NULL)
6267 return NULL;
6268 s = vim_strsave(last_insert + last_insert_skip);
6269 if (s != NULL)
6270 {
6271 len = (int)STRLEN(s);
6272 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6273 s[len - 1] = NUL;
6274 }
6275 return s;
6276}
6277
6278/*
6279 * Check the word in front of the cursor for an abbreviation.
6280 * Called when the non-id character "c" has been entered.
6281 * When an abbreviation is recognized it is removed from the text and
6282 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6283 */
6284 static int
6285echeck_abbr(c)
6286 int c;
6287{
6288 /* Don't check for abbreviation in paste mode, when disabled and just
6289 * after moving around with cursor keys. */
6290 if (p_paste || no_abbr || arrow_used)
6291 return FALSE;
6292
6293 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6294 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6295}
6296
6297/*
6298 * replace-stack functions
6299 *
6300 * When replacing characters, the replaced characters are remembered for each
6301 * new character. This is used to re-insert the old text when backspacing.
6302 *
6303 * There is a NUL headed list of characters for each character that is
6304 * currently in the file after the insertion point. When BS is used, one NUL
6305 * headed list is put back for the deleted character.
6306 *
6307 * For a newline, there are two NUL headed lists. One contains the characters
6308 * that the NL replaced. The extra one stores the characters after the cursor
6309 * that were deleted (always white space).
6310 *
6311 * Replace_offset is normally 0, in which case replace_push will add a new
6312 * character at the end of the stack. If replace_offset is not 0, that many
6313 * characters will be left on the stack above the newly inserted character.
6314 */
6315
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006316static char_u *replace_stack = NULL;
6317static long replace_stack_nr = 0; /* next entry in replace stack */
6318static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319
6320 void
6321replace_push(c)
6322 int c; /* character that is replaced (NUL is none) */
6323{
6324 char_u *p;
6325
6326 if (replace_stack_nr < replace_offset) /* nothing to do */
6327 return;
6328 if (replace_stack_len <= replace_stack_nr)
6329 {
6330 replace_stack_len += 50;
6331 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6332 if (p == NULL) /* out of memory */
6333 {
6334 replace_stack_len -= 50;
6335 return;
6336 }
6337 if (replace_stack != NULL)
6338 {
6339 mch_memmove(p, replace_stack,
6340 (size_t)(replace_stack_nr * sizeof(char_u)));
6341 vim_free(replace_stack);
6342 }
6343 replace_stack = p;
6344 }
6345 p = replace_stack + replace_stack_nr - replace_offset;
6346 if (replace_offset)
6347 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6348 *p = c;
6349 ++replace_stack_nr;
6350}
6351
6352/*
6353 * call replace_push(c) with replace_offset set to the first NUL.
6354 */
6355 static void
6356replace_push_off(c)
6357 int c;
6358{
6359 char_u *p;
6360
6361 p = replace_stack + replace_stack_nr;
6362 for (replace_offset = 1; replace_offset < replace_stack_nr;
6363 ++replace_offset)
6364 if (*--p == NUL)
6365 break;
6366 replace_push(c);
6367 replace_offset = 0;
6368}
6369
6370/*
6371 * Pop one item from the replace stack.
6372 * return -1 if stack empty
6373 * return replaced character or NUL otherwise
6374 */
6375 static int
6376replace_pop()
6377{
6378 if (replace_stack_nr == 0)
6379 return -1;
6380 return (int)replace_stack[--replace_stack_nr];
6381}
6382
6383/*
6384 * Join the top two items on the replace stack. This removes to "off"'th NUL
6385 * encountered.
6386 */
6387 static void
6388replace_join(off)
6389 int off; /* offset for which NUL to remove */
6390{
6391 int i;
6392
6393 for (i = replace_stack_nr; --i >= 0; )
6394 if (replace_stack[i] == NUL && off-- <= 0)
6395 {
6396 --replace_stack_nr;
6397 mch_memmove(replace_stack + i, replace_stack + i + 1,
6398 (size_t)(replace_stack_nr - i));
6399 return;
6400 }
6401}
6402
6403/*
6404 * Pop bytes from the replace stack until a NUL is found, and insert them
6405 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6406 */
6407 static void
6408replace_pop_ins()
6409{
6410 int cc;
6411 int oldState = State;
6412
6413 State = NORMAL; /* don't want REPLACE here */
6414 while ((cc = replace_pop()) > 0)
6415 {
6416#ifdef FEAT_MBYTE
6417 mb_replace_pop_ins(cc);
6418#else
6419 ins_char(cc);
6420#endif
6421 dec_cursor();
6422 }
6423 State = oldState;
6424}
6425
6426#ifdef FEAT_MBYTE
6427/*
6428 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6429 * indicates a multi-byte char, pop the other bytes too.
6430 */
6431 static void
6432mb_replace_pop_ins(cc)
6433 int cc;
6434{
6435 int n;
6436 char_u buf[MB_MAXBYTES];
6437 int i;
6438 int c;
6439
6440 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6441 {
6442 buf[0] = cc;
6443 for (i = 1; i < n; ++i)
6444 buf[i] = replace_pop();
6445 ins_bytes_len(buf, n);
6446 }
6447 else
6448 ins_char(cc);
6449
6450 if (enc_utf8)
6451 /* Handle composing chars. */
6452 for (;;)
6453 {
6454 c = replace_pop();
6455 if (c == -1) /* stack empty */
6456 break;
6457 if ((n = MB_BYTE2LEN(c)) == 1)
6458 {
6459 /* Not a multi-byte char, put it back. */
6460 replace_push(c);
6461 break;
6462 }
6463 else
6464 {
6465 buf[0] = c;
6466 for (i = 1; i < n; ++i)
6467 buf[i] = replace_pop();
6468 if (utf_iscomposing(utf_ptr2char(buf)))
6469 ins_bytes_len(buf, n);
6470 else
6471 {
6472 /* Not a composing char, put it back. */
6473 for (i = n - 1; i >= 0; --i)
6474 replace_push(buf[i]);
6475 break;
6476 }
6477 }
6478 }
6479}
6480#endif
6481
6482/*
6483 * make the replace stack empty
6484 * (called when exiting replace mode)
6485 */
6486 static void
6487replace_flush()
6488{
6489 vim_free(replace_stack);
6490 replace_stack = NULL;
6491 replace_stack_len = 0;
6492 replace_stack_nr = 0;
6493}
6494
6495/*
6496 * Handle doing a BS for one character.
6497 * cc < 0: replace stack empty, just move cursor
6498 * cc == 0: character was inserted, delete it
6499 * cc > 0: character was replaced, put cc (first byte of original char) back
6500 * and check for more characters to be put back
6501 */
6502 static void
6503replace_do_bs()
6504{
6505 int cc;
6506#ifdef FEAT_VREPLACE
6507 int orig_len = 0;
6508 int ins_len;
6509 int orig_vcols = 0;
6510 colnr_T start_vcol;
6511 char_u *p;
6512 int i;
6513 int vcol;
6514#endif
6515
6516 cc = replace_pop();
6517 if (cc > 0)
6518 {
6519#ifdef FEAT_VREPLACE
6520 if (State & VREPLACE_FLAG)
6521 {
6522 /* Get the number of screen cells used by the character we are
6523 * going to delete. */
6524 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6525 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6526 }
6527#endif
6528#ifdef FEAT_MBYTE
6529 if (has_mbyte)
6530 {
6531 del_char(FALSE);
6532# ifdef FEAT_VREPLACE
6533 if (State & VREPLACE_FLAG)
6534 orig_len = STRLEN(ml_get_cursor());
6535# endif
6536 replace_push(cc);
6537 }
6538 else
6539#endif
6540 {
6541 pchar_cursor(cc);
6542#ifdef FEAT_VREPLACE
6543 if (State & VREPLACE_FLAG)
6544 orig_len = STRLEN(ml_get_cursor()) - 1;
6545#endif
6546 }
6547 replace_pop_ins();
6548
6549#ifdef FEAT_VREPLACE
6550 if (State & VREPLACE_FLAG)
6551 {
6552 /* Get the number of screen cells used by the inserted characters */
6553 p = ml_get_cursor();
6554 ins_len = STRLEN(p) - orig_len;
6555 vcol = start_vcol;
6556 for (i = 0; i < ins_len; ++i)
6557 {
6558 vcol += chartabsize(p + i, vcol);
6559#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006560 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561#endif
6562 }
6563 vcol -= start_vcol;
6564
6565 /* Delete spaces that were inserted after the cursor to keep the
6566 * text aligned. */
6567 curwin->w_cursor.col += ins_len;
6568 while (vcol > orig_vcols && gchar_cursor() == ' ')
6569 {
6570 del_char(FALSE);
6571 ++orig_vcols;
6572 }
6573 curwin->w_cursor.col -= ins_len;
6574 }
6575#endif
6576
6577 /* mark the buffer as changed and prepare for displaying */
6578 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6579 }
6580 else if (cc == 0)
6581 (void)del_char(FALSE);
6582}
6583
6584#ifdef FEAT_CINDENT
6585/*
6586 * Return TRUE if C-indenting is on.
6587 */
6588 static int
6589cindent_on()
6590{
6591 return (!p_paste && (curbuf->b_p_cin
6592# ifdef FEAT_EVAL
6593 || *curbuf->b_p_inde != NUL
6594# endif
6595 ));
6596}
6597#endif
6598
6599#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6600/*
6601 * Re-indent the current line, based on the current contents of it and the
6602 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6603 * confused what all the part that handles Control-T is doing that I'm not.
6604 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6605 */
6606
6607 void
6608fixthisline(get_the_indent)
6609 int (*get_the_indent) __ARGS((void));
6610{
6611 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6612 if (linewhite(curwin->w_cursor.lnum))
6613 did_ai = TRUE; /* delete the indent if the line stays empty */
6614}
6615
6616 void
6617fix_indent()
6618{
6619 if (p_paste)
6620 return;
6621# ifdef FEAT_LISP
6622 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6623 fixthisline(get_lisp_indent);
6624# endif
6625# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6626 else
6627# endif
6628# ifdef FEAT_CINDENT
6629 if (cindent_on())
6630 do_c_expr_indent();
6631# endif
6632}
6633
6634#endif
6635
6636#ifdef FEAT_CINDENT
6637/*
6638 * return TRUE if 'cinkeys' contains the key "keytyped",
6639 * when == '*': Only if key is preceded with '*' (indent before insert)
6640 * when == '!': Only if key is prededed with '!' (don't insert)
6641 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6642 *
6643 * "keytyped" can have a few special values:
6644 * KEY_OPEN_FORW
6645 * KEY_OPEN_BACK
6646 * KEY_COMPLETE just finished completion.
6647 *
6648 * If line_is_empty is TRUE accept keys with '0' before them.
6649 */
6650 int
6651in_cinkeys(keytyped, when, line_is_empty)
6652 int keytyped;
6653 int when;
6654 int line_is_empty;
6655{
6656 char_u *look;
6657 int try_match;
6658 int try_match_word;
6659 char_u *p;
6660 char_u *line;
6661 int icase;
6662 int i;
6663
6664#ifdef FEAT_EVAL
6665 if (*curbuf->b_p_inde != NUL)
6666 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6667 else
6668#endif
6669 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6670 while (*look)
6671 {
6672 /*
6673 * Find out if we want to try a match with this key, depending on
6674 * 'when' and a '*' or '!' before the key.
6675 */
6676 switch (when)
6677 {
6678 case '*': try_match = (*look == '*'); break;
6679 case '!': try_match = (*look == '!'); break;
6680 default: try_match = (*look != '*'); break;
6681 }
6682 if (*look == '*' || *look == '!')
6683 ++look;
6684
6685 /*
6686 * If there is a '0', only accept a match if the line is empty.
6687 * But may still match when typing last char of a word.
6688 */
6689 if (*look == '0')
6690 {
6691 try_match_word = try_match;
6692 if (!line_is_empty)
6693 try_match = FALSE;
6694 ++look;
6695 }
6696 else
6697 try_match_word = FALSE;
6698
6699 /*
6700 * does it look like a control character?
6701 */
6702 if (*look == '^'
6703#ifdef EBCDIC
6704 && (Ctrl_chr(look[1]) != 0)
6705#else
6706 && look[1] >= '?' && look[1] <= '_'
6707#endif
6708 )
6709 {
6710 if (try_match && keytyped == Ctrl_chr(look[1]))
6711 return TRUE;
6712 look += 2;
6713 }
6714 /*
6715 * 'o' means "o" command, open forward.
6716 * 'O' means "O" command, open backward.
6717 */
6718 else if (*look == 'o')
6719 {
6720 if (try_match && keytyped == KEY_OPEN_FORW)
6721 return TRUE;
6722 ++look;
6723 }
6724 else if (*look == 'O')
6725 {
6726 if (try_match && keytyped == KEY_OPEN_BACK)
6727 return TRUE;
6728 ++look;
6729 }
6730
6731 /*
6732 * 'e' means to check for "else" at start of line and just before the
6733 * cursor.
6734 */
6735 else if (*look == 'e')
6736 {
6737 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6738 {
6739 p = ml_get_curline();
6740 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6741 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6742 return TRUE;
6743 }
6744 ++look;
6745 }
6746
6747 /*
6748 * ':' only causes an indent if it is at the end of a label or case
6749 * statement, or when it was before typing the ':' (to fix
6750 * class::method for C++).
6751 */
6752 else if (*look == ':')
6753 {
6754 if (try_match && keytyped == ':')
6755 {
6756 p = ml_get_curline();
6757 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6758 return TRUE;
6759 if (curwin->w_cursor.col > 2
6760 && p[curwin->w_cursor.col - 1] == ':'
6761 && p[curwin->w_cursor.col - 2] == ':')
6762 {
6763 p[curwin->w_cursor.col - 1] = ' ';
6764 i = (cin_iscase(p) || cin_isscopedecl(p)
6765 || cin_islabel(30));
6766 p = ml_get_curline();
6767 p[curwin->w_cursor.col - 1] = ':';
6768 if (i)
6769 return TRUE;
6770 }
6771 }
6772 ++look;
6773 }
6774
6775
6776 /*
6777 * Is it a key in <>, maybe?
6778 */
6779 else if (*look == '<')
6780 {
6781 if (try_match)
6782 {
6783 /*
6784 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6785 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6786 * >, *, : and ! keys if they really really want to.
6787 */
6788 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6789 && keytyped == look[1])
6790 return TRUE;
6791
6792 if (keytyped == get_special_key_code(look + 1))
6793 return TRUE;
6794 }
6795 while (*look && *look != '>')
6796 look++;
6797 while (*look == '>')
6798 look++;
6799 }
6800
6801 /*
6802 * Is it a word: "=word"?
6803 */
6804 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6805 {
6806 ++look;
6807 if (*look == '~')
6808 {
6809 icase = TRUE;
6810 ++look;
6811 }
6812 else
6813 icase = FALSE;
6814 p = vim_strchr(look, ',');
6815 if (p == NULL)
6816 p = look + STRLEN(look);
6817 if ((try_match || try_match_word)
6818 && curwin->w_cursor.col >= (colnr_T)(p - look))
6819 {
6820 int match = FALSE;
6821
6822#ifdef FEAT_INS_EXPAND
6823 if (keytyped == KEY_COMPLETE)
6824 {
6825 char_u *s;
6826
6827 /* Just completed a word, check if it starts with "look".
6828 * search back for the start of a word. */
6829 line = ml_get_curline();
6830# ifdef FEAT_MBYTE
6831 if (has_mbyte)
6832 {
6833 char_u *n;
6834
6835 for (s = line + curwin->w_cursor.col; s > line; s = n)
6836 {
6837 n = mb_prevptr(line, s);
6838 if (!vim_iswordp(n))
6839 break;
6840 }
6841 }
6842 else
6843# endif
6844 for (s = line + curwin->w_cursor.col; s > line; --s)
6845 if (!vim_iswordc(s[-1]))
6846 break;
6847 if (s + (p - look) <= line + curwin->w_cursor.col
6848 && (icase
6849 ? MB_STRNICMP(s, look, p - look)
6850 : STRNCMP(s, look, p - look)) == 0)
6851 match = TRUE;
6852 }
6853 else
6854#endif
6855 /* TODO: multi-byte */
6856 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6857 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6858 {
6859 line = ml_get_cursor();
6860 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6861 || !vim_iswordc(line[-(p - look) - 1]))
6862 && (icase
6863 ? MB_STRNICMP(line - (p - look), look, p - look)
6864 : STRNCMP(line - (p - look), look, p - look))
6865 == 0)
6866 match = TRUE;
6867 }
6868 if (match && try_match_word && !try_match)
6869 {
6870 /* "0=word": Check if there are only blanks before the
6871 * word. */
6872 line = ml_get_curline();
6873 if ((int)(skipwhite(line) - line) !=
6874 (int)(curwin->w_cursor.col - (p - look)))
6875 match = FALSE;
6876 }
6877 if (match)
6878 return TRUE;
6879 }
6880 look = p;
6881 }
6882
6883 /*
6884 * ok, it's a boring generic character.
6885 */
6886 else
6887 {
6888 if (try_match && *look == keytyped)
6889 return TRUE;
6890 ++look;
6891 }
6892
6893 /*
6894 * Skip over ", ".
6895 */
6896 look = skip_to_option_part(look);
6897 }
6898 return FALSE;
6899}
6900#endif /* FEAT_CINDENT */
6901
6902#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6903/*
6904 * Map Hebrew keyboard when in hkmap mode.
6905 */
6906 int
6907hkmap(c)
6908 int c;
6909{
6910 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6911 {
6912 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6913 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6914 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6915 static char_u map[26] =
6916 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6917 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6918 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6919 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6920 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6921 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6922 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6923 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6924 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6925
6926 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6927 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6928 /* '-1'='sofit' */
6929 else if (c == 'x')
6930 return 'X';
6931 else if (c == 'q')
6932 return '\''; /* {geresh}={'} */
6933 else if (c == 246)
6934 return ' '; /* \"o --> ' ' for a german keyboard */
6935 else if (c == 228)
6936 return ' '; /* \"a --> ' ' -- / -- */
6937 else if (c == 252)
6938 return ' '; /* \"u --> ' ' -- / -- */
6939#ifdef EBCDIC
6940 else if (islower(c))
6941#else
6942 /* NOTE: islower() does not do the right thing for us on Linux so we
6943 * do this the same was as 5.7 and previous, so it works correctly on
6944 * all systems. Specifically, the e.g. Delete and Arrow keys are
6945 * munged and won't work if e.g. searching for Hebrew text.
6946 */
6947 else if (c >= 'a' && c <= 'z')
6948#endif
6949 return (int)(map[CharOrdLow(c)] + p_aleph);
6950 else
6951 return c;
6952 }
6953 else
6954 {
6955 switch (c)
6956 {
6957 case '`': return ';';
6958 case '/': return '.';
6959 case '\'': return ',';
6960 case 'q': return '/';
6961 case 'w': return '\'';
6962
6963 /* Hebrew letters - set offset from 'a' */
6964 case ',': c = '{'; break;
6965 case '.': c = 'v'; break;
6966 case ';': c = 't'; break;
6967 default: {
6968 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6969
6970#ifdef EBCDIC
6971 /* see note about islower() above */
6972 if (!islower(c))
6973#else
6974 if (c < 'a' || c > 'z')
6975#endif
6976 return c;
6977 c = str[CharOrdLow(c)];
6978 break;
6979 }
6980 }
6981
6982 return (int)(CharOrdLow(c) + p_aleph);
6983 }
6984}
6985#endif
6986
6987 static void
6988ins_reg()
6989{
6990 int need_redraw = FALSE;
6991 int regname;
6992 int literally = 0;
6993
6994 /*
6995 * If we are going to wait for a character, show a '"'.
6996 */
6997 pc_status = PC_STATUS_UNSET;
6998 if (redrawing() && !char_avail())
6999 {
7000 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007001 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002
7003 edit_putchar('"', TRUE);
7004#ifdef FEAT_CMDL_INFO
7005 add_to_showcmd_c(Ctrl_R);
7006#endif
7007 }
7008
7009#ifdef USE_ON_FLY_SCROLL
7010 dont_scroll = TRUE; /* disallow scrolling here */
7011#endif
7012
7013 /*
7014 * Don't map the register name. This also prevents the mode message to be
7015 * deleted when ESC is hit.
7016 */
7017 ++no_mapping;
7018 regname = safe_vgetc();
7019#ifdef FEAT_LANGMAP
7020 LANGMAP_ADJUST(regname, TRUE);
7021#endif
7022 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7023 {
7024 /* Get a third key for literal register insertion */
7025 literally = regname;
7026#ifdef FEAT_CMDL_INFO
7027 add_to_showcmd_c(literally);
7028#endif
7029 regname = safe_vgetc();
7030#ifdef FEAT_LANGMAP
7031 LANGMAP_ADJUST(regname, TRUE);
7032#endif
7033 }
7034 --no_mapping;
7035
7036#ifdef FEAT_EVAL
7037 /*
7038 * Don't call u_sync() while getting the expression,
7039 * evaluating it or giving an error message for it!
7040 */
7041 ++no_u_sync;
7042 if (regname == '=')
7043 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007044# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007046# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007048# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 /* Restore the Input Method. */
7050 if (im_on)
7051 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007052# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007054 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7055 {
7056 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 else
7060 {
7061#endif
7062 if (literally == Ctrl_O || literally == Ctrl_P)
7063 {
7064 /* Append the command to the redo buffer. */
7065 AppendCharToRedobuff(Ctrl_R);
7066 AppendCharToRedobuff(literally);
7067 AppendCharToRedobuff(regname);
7068
7069 do_put(regname, BACKWARD, 1L,
7070 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7071 }
7072 else if (insert_reg(regname, literally) == FAIL)
7073 {
7074 vim_beep();
7075 need_redraw = TRUE; /* remove the '"' */
7076 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007077 else if (stop_insert_mode)
7078 /* When the '=' register was used and a function was invoked that
7079 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7080 * insert anything, need to remove the '"' */
7081 need_redraw = TRUE;
7082
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083#ifdef FEAT_EVAL
7084 }
7085 --no_u_sync;
7086#endif
7087#ifdef FEAT_CMDL_INFO
7088 clear_showcmd();
7089#endif
7090
7091 /* If the inserted register is empty, we need to remove the '"' */
7092 if (need_redraw || stuff_empty())
7093 edit_unputchar();
7094}
7095
7096/*
7097 * CTRL-G commands in Insert mode.
7098 */
7099 static void
7100ins_ctrl_g()
7101{
7102 int c;
7103
7104#ifdef FEAT_INS_EXPAND
7105 /* Right after CTRL-X the cursor will be after the ruler. */
7106 setcursor();
7107#endif
7108
7109 /*
7110 * Don't map the second key. This also prevents the mode message to be
7111 * deleted when ESC is hit.
7112 */
7113 ++no_mapping;
7114 c = safe_vgetc();
7115 --no_mapping;
7116 switch (c)
7117 {
7118 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7119 case K_UP:
7120 case Ctrl_K:
7121 case 'k': ins_up(TRUE);
7122 break;
7123
7124 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7125 case K_DOWN:
7126 case Ctrl_J:
7127 case 'j': ins_down(TRUE);
7128 break;
7129
7130 /* CTRL-G u: start new undoable edit */
7131 case 'u': u_sync();
7132 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007133
7134 /* Need to reset Insstart, esp. because a BS that joins
7135 * aline to the previous one must save for undo. */
7136 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 break;
7138
7139 /* Unknown CTRL-G command, reserved for future expansion. */
7140 default: vim_beep();
7141 }
7142}
7143
7144/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007145 * CTRL-^ in Insert mode.
7146 */
7147 static void
7148ins_ctrl_hat()
7149{
7150 if (map_to_exists_mode((char_u *)"", LANGMAP))
7151 {
7152 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7153 if (State & LANGMAP)
7154 {
7155 curbuf->b_p_iminsert = B_IMODE_NONE;
7156 State &= ~LANGMAP;
7157 }
7158 else
7159 {
7160 curbuf->b_p_iminsert = B_IMODE_LMAP;
7161 State |= LANGMAP;
7162#ifdef USE_IM_CONTROL
7163 im_set_active(FALSE);
7164#endif
7165 }
7166 }
7167#ifdef USE_IM_CONTROL
7168 else
7169 {
7170 /* There are no ":lmap" mappings, toggle IM */
7171 if (im_get_status())
7172 {
7173 curbuf->b_p_iminsert = B_IMODE_NONE;
7174 im_set_active(FALSE);
7175 }
7176 else
7177 {
7178 curbuf->b_p_iminsert = B_IMODE_IM;
7179 State &= ~LANGMAP;
7180 im_set_active(TRUE);
7181 }
7182 }
7183#endif
7184 set_iminsert_global();
7185 showmode();
7186#ifdef FEAT_GUI
7187 /* may show different cursor shape or color */
7188 if (gui.in_use)
7189 gui_update_cursor(TRUE, FALSE);
7190#endif
7191#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7192 /* Show/unshow value of 'keymap' in status lines. */
7193 status_redraw_curbuf();
7194#endif
7195}
7196
7197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 * Handle ESC in insert mode.
7199 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7200 * insert.
7201 */
7202 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007203ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 long *count;
7205 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007206 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207{
7208 int temp;
7209 static int disabled_redraw = FALSE;
7210
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007211#ifdef FEAT_SYN_HL
7212 check_spell_redraw();
7213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214#if defined(FEAT_HANGULIN)
7215# if defined(ESC_CHG_TO_ENG_MODE)
7216 hangul_input_state_set(0);
7217# endif
7218 if (composing_hangul)
7219 {
7220 push_raw_key(composing_hangul_buffer, 2);
7221 composing_hangul = 0;
7222 }
7223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
7225 temp = curwin->w_cursor.col;
7226 if (disabled_redraw)
7227 {
7228 --RedrawingDisabled;
7229 disabled_redraw = FALSE;
7230 }
7231 if (!arrow_used)
7232 {
7233 /*
7234 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007235 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7236 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 */
7238 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007239 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
7241 /*
7242 * Repeating insert may take a long time. Check for
7243 * interrupt now and then.
7244 */
7245 if (*count > 0)
7246 {
7247 line_breakcheck();
7248 if (got_int)
7249 *count = 0;
7250 }
7251
7252 if (--*count > 0) /* repeat what was typed */
7253 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007254 /* Vi repeats the insert without replacing characters. */
7255 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7256 State &= ~REPLACE_FLAG;
7257
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 (void)start_redo_ins();
7259 if (cmdchar == 'r' || cmdchar == 'v')
7260 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7261 ++RedrawingDisabled;
7262 disabled_redraw = TRUE;
7263 return FALSE; /* repeat the insert */
7264 }
7265 stop_insert(&curwin->w_cursor, TRUE);
7266 undisplay_dollar();
7267 }
7268
7269 /* When an autoindent was removed, curswant stays after the
7270 * indent */
7271 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7272 curwin->w_set_curswant = TRUE;
7273
7274 /* Remember the last Insert position in the '^ mark. */
7275 if (!cmdmod.keepjumps)
7276 curbuf->b_last_insert = curwin->w_cursor;
7277
7278 /*
7279 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007280 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007282 if (!nomove
7283 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284#ifdef FEAT_VIRTUALEDIT
7285 || curwin->w_cursor.coladd > 0
7286#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007287 )
7288 && (restart_edit == NUL
7289 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007291 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007293 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294#ifdef FEAT_RIGHTLEFT
7295 && !revins_on
7296#endif
7297 )
7298 {
7299#ifdef FEAT_VIRTUALEDIT
7300 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7301 {
7302 oneleft();
7303 if (restart_edit != NUL)
7304 ++curwin->w_cursor.coladd;
7305 }
7306 else
7307#endif
7308 {
7309 --curwin->w_cursor.col;
7310#ifdef FEAT_MBYTE
7311 /* Correct cursor for multi-byte character. */
7312 if (has_mbyte)
7313 mb_adjust_cursor();
7314#endif
7315 }
7316 }
7317
7318#ifdef USE_IM_CONTROL
7319 /* Disable IM to allow typing English directly for Normal mode commands.
7320 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7321 * well). */
7322 if (!(State & LANGMAP))
7323 im_save_status(&curbuf->b_p_iminsert);
7324 im_set_active(FALSE);
7325#endif
7326
7327 State = NORMAL;
7328 /* need to position cursor again (e.g. when on a TAB ) */
7329 changed_cline_bef_curs();
7330
7331#ifdef FEAT_MOUSE
7332 setmouse();
7333#endif
7334#ifdef CURSOR_SHAPE
7335 ui_cursor_shape(); /* may show different cursor shape */
7336#endif
7337
7338 /*
7339 * When recording or for CTRL-O, need to display the new mode.
7340 * Otherwise remove the mode message.
7341 */
7342 if (Recording || restart_edit != NUL)
7343 showmode();
7344 else if (p_smd)
7345 MSG("");
7346
7347 return TRUE; /* exit Insert mode */
7348}
7349
7350#ifdef FEAT_RIGHTLEFT
7351/*
7352 * Toggle language: hkmap and revins_on.
7353 * Move to end of reverse inserted text.
7354 */
7355 static void
7356ins_ctrl_()
7357{
7358 if (revins_on && revins_chars && revins_scol >= 0)
7359 {
7360 while (gchar_cursor() != NUL && revins_chars--)
7361 ++curwin->w_cursor.col;
7362 }
7363 p_ri = !p_ri;
7364 revins_on = (State == INSERT && p_ri);
7365 if (revins_on)
7366 {
7367 revins_scol = curwin->w_cursor.col;
7368 revins_legal++;
7369 revins_chars = 0;
7370 undisplay_dollar();
7371 }
7372 else
7373 revins_scol = -1;
7374#ifdef FEAT_FKMAP
7375 if (p_altkeymap)
7376 {
7377 /*
7378 * to be consistent also for redo command, using '.'
7379 * set arrow_used to true and stop it - causing to redo
7380 * characters entered in one mode (normal/reverse insert).
7381 */
7382 arrow_used = TRUE;
7383 (void)stop_arrow();
7384 p_fkmap = curwin->w_p_rl ^ p_ri;
7385 if (p_fkmap && p_ri)
7386 State = INSERT;
7387 }
7388 else
7389#endif
7390 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7391 showmode();
7392}
7393#endif
7394
7395#ifdef FEAT_VISUAL
7396/*
7397 * If 'keymodel' contains "startsel", may start selection.
7398 * Returns TRUE when a CTRL-O and other keys stuffed.
7399 */
7400 static int
7401ins_start_select(c)
7402 int c;
7403{
7404 if (km_startsel)
7405 switch (c)
7406 {
7407 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 case K_PAGEUP:
7410 case K_KPAGEUP:
7411 case K_PAGEDOWN:
7412 case K_KPAGEDOWN:
7413# ifdef MACOS
7414 case K_LEFT:
7415 case K_RIGHT:
7416 case K_UP:
7417 case K_DOWN:
7418 case K_END:
7419 case K_HOME:
7420# endif
7421 if (!(mod_mask & MOD_MASK_SHIFT))
7422 break;
7423 /* FALLTHROUGH */
7424 case K_S_LEFT:
7425 case K_S_RIGHT:
7426 case K_S_UP:
7427 case K_S_DOWN:
7428 case K_S_END:
7429 case K_S_HOME:
7430 /* Start selection right away, the cursor can move with
7431 * CTRL-O when beyond the end of the line. */
7432 start_selection();
7433
7434 /* Execute the key in (insert) Select mode. */
7435 stuffcharReadbuff(Ctrl_O);
7436 if (mod_mask)
7437 {
7438 char_u buf[4];
7439
7440 buf[0] = K_SPECIAL;
7441 buf[1] = KS_MODIFIER;
7442 buf[2] = mod_mask;
7443 buf[3] = NUL;
7444 stuffReadbuff(buf);
7445 }
7446 stuffcharReadbuff(c);
7447 return TRUE;
7448 }
7449 return FALSE;
7450}
7451#endif
7452
7453/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007454 * <Insert> key in Insert mode: toggle insert/remplace mode.
7455 */
7456 static void
7457ins_insert(replaceState)
7458 int replaceState;
7459{
7460#ifdef FEAT_FKMAP
7461 if (p_fkmap && p_ri)
7462 {
7463 beep_flush();
7464 EMSG(farsi_text_3); /* encoded in Farsi */
7465 return;
7466 }
7467#endif
7468
7469#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007470# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007471 set_vim_var_string(VV_INSERTMODE,
7472 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007473# ifdef FEAT_VREPLACE
7474 replaceState == VREPLACE ? "v" :
7475# endif
7476 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007477# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007478 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7479#endif
7480 if (State & REPLACE_FLAG)
7481 State = INSERT | (State & LANGMAP);
7482 else
7483 State = replaceState | (State & LANGMAP);
7484 AppendCharToRedobuff(K_INS);
7485 showmode();
7486#ifdef CURSOR_SHAPE
7487 ui_cursor_shape(); /* may show different cursor shape */
7488#endif
7489}
7490
7491/*
7492 * Pressed CTRL-O in Insert mode.
7493 */
7494 static void
7495ins_ctrl_o()
7496{
7497#ifdef FEAT_VREPLACE
7498 if (State & VREPLACE_FLAG)
7499 restart_edit = 'V';
7500 else
7501#endif
7502 if (State & REPLACE_FLAG)
7503 restart_edit = 'R';
7504 else
7505 restart_edit = 'I';
7506#ifdef FEAT_VIRTUALEDIT
7507 if (virtual_active())
7508 ins_at_eol = FALSE; /* cursor always keeps its column */
7509 else
7510#endif
7511 ins_at_eol = (gchar_cursor() == NUL);
7512}
7513
7514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 * If the cursor is on an indent, ^T/^D insert/delete one
7516 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7517 * Always round the indent to 'shiftwith', this is compatible
7518 * with vi. But vi only supports ^T and ^D after an
7519 * autoindent, we support it everywhere.
7520 */
7521 static void
7522ins_shift(c, lastc)
7523 int c;
7524 int lastc;
7525{
7526 if (stop_arrow() == FAIL)
7527 return;
7528 AppendCharToRedobuff(c);
7529
7530 /*
7531 * 0^D and ^^D: remove all indent.
7532 */
7533 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7534 {
7535 --curwin->w_cursor.col;
7536 (void)del_char(FALSE); /* delete the '^' or '0' */
7537 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7538 if (State & REPLACE_FLAG)
7539 replace_pop_ins();
7540 if (lastc == '^')
7541 old_indent = get_indent(); /* remember curr. indent */
7542 change_indent(INDENT_SET, 0, TRUE, 0);
7543 }
7544 else
7545 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7546
7547 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7548 did_ai = FALSE;
7549#ifdef FEAT_SMARTINDENT
7550 did_si = FALSE;
7551 can_si = FALSE;
7552 can_si_back = FALSE;
7553#endif
7554#ifdef FEAT_CINDENT
7555 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7556#endif
7557}
7558
7559 static void
7560ins_del()
7561{
7562 int temp;
7563
7564 if (stop_arrow() == FAIL)
7565 return;
7566 if (gchar_cursor() == NUL) /* delete newline */
7567 {
7568 temp = curwin->w_cursor.col;
7569 if (!can_bs(BS_EOL) /* only if "eol" included */
7570 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7571 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7572 || do_join(FALSE) == FAIL)
7573 vim_beep();
7574 else
7575 curwin->w_cursor.col = temp;
7576 }
7577 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7578 vim_beep();
7579 did_ai = FALSE;
7580#ifdef FEAT_SMARTINDENT
7581 did_si = FALSE;
7582 can_si = FALSE;
7583 can_si_back = FALSE;
7584#endif
7585 AppendCharToRedobuff(K_DEL);
7586}
7587
7588/*
7589 * Handle Backspace, delete-word and delete-line in Insert mode.
7590 * Return TRUE when backspace was actually used.
7591 */
7592 static int
7593ins_bs(c, mode, inserted_space_p)
7594 int c;
7595 int mode;
7596 int *inserted_space_p;
7597{
7598 linenr_T lnum;
7599 int cc;
7600 int temp = 0; /* init for GCC */
7601 colnr_T mincol;
7602 int did_backspace = FALSE;
7603 int in_indent;
7604 int oldState;
7605#ifdef FEAT_MBYTE
7606 int p1, p2;
7607#endif
7608
7609 /*
7610 * can't delete anything in an empty file
7611 * can't backup past first character in buffer
7612 * can't backup past starting point unless 'backspace' > 1
7613 * can backup to a previous line if 'backspace' == 0
7614 */
7615 if ( bufempty()
7616 || (
7617#ifdef FEAT_RIGHTLEFT
7618 !revins_on &&
7619#endif
7620 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7621 || (!can_bs(BS_START)
7622 && (arrow_used
7623 || (curwin->w_cursor.lnum == Insstart.lnum
7624 && curwin->w_cursor.col <= Insstart.col)))
7625 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7626 && curwin->w_cursor.col <= ai_col)
7627 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7628 {
7629 vim_beep();
7630 return FALSE;
7631 }
7632
7633 if (stop_arrow() == FAIL)
7634 return FALSE;
7635 in_indent = inindent(0);
7636#ifdef FEAT_CINDENT
7637 if (in_indent)
7638 can_cindent = FALSE;
7639#endif
7640#ifdef FEAT_COMMENTS
7641 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7642#endif
7643#ifdef FEAT_RIGHTLEFT
7644 if (revins_on) /* put cursor after last inserted char */
7645 inc_cursor();
7646#endif
7647
7648#ifdef FEAT_VIRTUALEDIT
7649 /* Virtualedit:
7650 * BACKSPACE_CHAR eats a virtual space
7651 * BACKSPACE_WORD eats all coladd
7652 * BACKSPACE_LINE eats all coladd and keeps going
7653 */
7654 if (curwin->w_cursor.coladd > 0)
7655 {
7656 if (mode == BACKSPACE_CHAR)
7657 {
7658 --curwin->w_cursor.coladd;
7659 return TRUE;
7660 }
7661 if (mode == BACKSPACE_WORD)
7662 {
7663 curwin->w_cursor.coladd = 0;
7664 return TRUE;
7665 }
7666 curwin->w_cursor.coladd = 0;
7667 }
7668#endif
7669
7670 /*
7671 * delete newline!
7672 */
7673 if (curwin->w_cursor.col == 0)
7674 {
7675 lnum = Insstart.lnum;
7676 if (curwin->w_cursor.lnum == Insstart.lnum
7677#ifdef FEAT_RIGHTLEFT
7678 || revins_on
7679#endif
7680 )
7681 {
7682 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7683 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7684 return FALSE;
7685 --Insstart.lnum;
7686 Insstart.col = MAXCOL;
7687 }
7688 /*
7689 * In replace mode:
7690 * cc < 0: NL was inserted, delete it
7691 * cc >= 0: NL was replaced, put original characters back
7692 */
7693 cc = -1;
7694 if (State & REPLACE_FLAG)
7695 cc = replace_pop(); /* returns -1 if NL was inserted */
7696 /*
7697 * In replace mode, in the line we started replacing, we only move the
7698 * cursor.
7699 */
7700 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7701 {
7702 dec_cursor();
7703 }
7704 else
7705 {
7706#ifdef FEAT_VREPLACE
7707 if (!(State & VREPLACE_FLAG)
7708 || curwin->w_cursor.lnum > orig_line_count)
7709#endif
7710 {
7711 temp = gchar_cursor(); /* remember current char */
7712 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007713
7714 /* When "aw" is in 'formatoptions' we must delete the space at
7715 * the end of the line, otherwise the line will be broken
7716 * again when auto-formatting. */
7717 if (has_format_option(FO_AUTO)
7718 && has_format_option(FO_WHITE_PAR))
7719 {
7720 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7721 TRUE);
7722 int len;
7723
7724 len = STRLEN(ptr);
7725 if (len > 0 && ptr[len - 1] == ' ')
7726 ptr[len - 1] = NUL;
7727 }
7728
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 (void)do_join(FALSE);
7730 if (temp == NUL && gchar_cursor() != NUL)
7731 inc_cursor();
7732 }
7733#ifdef FEAT_VREPLACE
7734 else
7735 dec_cursor();
7736#endif
7737
7738 /*
7739 * In REPLACE mode we have to put back the text that was replaced
7740 * by the NL. On the replace stack is first a NUL-terminated
7741 * sequence of characters that were deleted and then the
7742 * characters that NL replaced.
7743 */
7744 if (State & REPLACE_FLAG)
7745 {
7746 /*
7747 * Do the next ins_char() in NORMAL state, to
7748 * prevent ins_char() from replacing characters and
7749 * avoiding showmatch().
7750 */
7751 oldState = State;
7752 State = NORMAL;
7753 /*
7754 * restore characters (blanks) deleted after cursor
7755 */
7756 while (cc > 0)
7757 {
7758 temp = curwin->w_cursor.col;
7759#ifdef FEAT_MBYTE
7760 mb_replace_pop_ins(cc);
7761#else
7762 ins_char(cc);
7763#endif
7764 curwin->w_cursor.col = temp;
7765 cc = replace_pop();
7766 }
7767 /* restore the characters that NL replaced */
7768 replace_pop_ins();
7769 State = oldState;
7770 }
7771 }
7772 did_ai = FALSE;
7773 }
7774 else
7775 {
7776 /*
7777 * Delete character(s) before the cursor.
7778 */
7779#ifdef FEAT_RIGHTLEFT
7780 if (revins_on) /* put cursor on last inserted char */
7781 dec_cursor();
7782#endif
7783 mincol = 0;
7784 /* keep indent */
7785 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7786#ifdef FEAT_RIGHTLEFT
7787 && !revins_on
7788#endif
7789 )
7790 {
7791 temp = curwin->w_cursor.col;
7792 beginline(BL_WHITE);
7793 if (curwin->w_cursor.col < (colnr_T)temp)
7794 mincol = curwin->w_cursor.col;
7795 curwin->w_cursor.col = temp;
7796 }
7797
7798 /*
7799 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7800 */
7801 if ( mode == BACKSPACE_CHAR
7802 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007803 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 && (*(ml_get_cursor() - 1) == TAB
7805 || (*(ml_get_cursor() - 1) == ' '
7806 && (!*inserted_space_p
7807 || arrow_used))))))
7808 {
7809 int ts;
7810 colnr_T vcol;
7811 colnr_T want_vcol;
7812 int extra = 0;
7813
7814 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007815 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 ts = curbuf->b_p_sw;
7817 else
7818 ts = curbuf->b_p_sts;
7819 /* Compute the virtual column where we want to be. Since
7820 * 'showbreak' may get in the way, need to get the last column of
7821 * the previous character. */
7822 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7823 dec_cursor();
7824 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7825 inc_cursor();
7826 want_vcol = (want_vcol / ts) * ts;
7827
7828 /* delete characters until we are at or before want_vcol */
7829 while (vcol > want_vcol
7830 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7831 {
7832 dec_cursor();
7833 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7834 if (State & REPLACE_FLAG)
7835 {
7836 /* Don't delete characters before the insert point when in
7837 * Replace mode */
7838 if (curwin->w_cursor.lnum != Insstart.lnum
7839 || curwin->w_cursor.col >= Insstart.col)
7840 {
7841#if 0 /* what was this for? It causes problems when sw != ts. */
7842 if (State == REPLACE && (int)vcol < want_vcol)
7843 {
7844 (void)del_char(FALSE);
7845 extra = 2; /* don't pop too much */
7846 }
7847 else
7848#endif
7849 replace_do_bs();
7850 }
7851 }
7852 else
7853 (void)del_char(FALSE);
7854 }
7855
7856 /* insert extra spaces until we are at want_vcol */
7857 while (vcol < want_vcol)
7858 {
7859 /* Remember the first char we inserted */
7860 if (curwin->w_cursor.lnum == Insstart.lnum
7861 && curwin->w_cursor.col < Insstart.col)
7862 Insstart.col = curwin->w_cursor.col;
7863
7864#ifdef FEAT_VREPLACE
7865 if (State & VREPLACE_FLAG)
7866 ins_char(' ');
7867 else
7868#endif
7869 {
7870 ins_str((char_u *)" ");
7871 if ((State & REPLACE_FLAG) && extra <= 1)
7872 {
7873 if (extra)
7874 replace_push_off(NUL);
7875 else
7876 replace_push(NUL);
7877 }
7878 if (extra == 2)
7879 extra = 1;
7880 }
7881 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7882 }
7883 }
7884
7885 /*
7886 * Delete upto starting point, start of line or previous word.
7887 */
7888 else do
7889 {
7890#ifdef FEAT_RIGHTLEFT
7891 if (!revins_on) /* put cursor on char to be deleted */
7892#endif
7893 dec_cursor();
7894
7895 /* start of word? */
7896 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7897 {
7898 mode = BACKSPACE_WORD_NOT_SPACE;
7899 temp = vim_iswordc(gchar_cursor());
7900 }
7901 /* end of word? */
7902 else if (mode == BACKSPACE_WORD_NOT_SPACE
7903 && (vim_isspace(cc = gchar_cursor())
7904 || vim_iswordc(cc) != temp))
7905 {
7906#ifdef FEAT_RIGHTLEFT
7907 if (!revins_on)
7908#endif
7909 inc_cursor();
7910#ifdef FEAT_RIGHTLEFT
7911 else if (State & REPLACE_FLAG)
7912 dec_cursor();
7913#endif
7914 break;
7915 }
7916 if (State & REPLACE_FLAG)
7917 replace_do_bs();
7918 else
7919 {
7920#ifdef FEAT_MBYTE
7921 if (enc_utf8 && p_deco)
7922 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7923#endif
7924 (void)del_char(FALSE);
7925#ifdef FEAT_MBYTE
7926 /*
7927 * If p1 or p2 is non-zero, there are combining characters we
7928 * need to take account of. Don't back up before the base
7929 * character.
7930 */
7931 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7932 inc_cursor();
7933#endif
7934#ifdef FEAT_RIGHTLEFT
7935 if (revins_chars)
7936 {
7937 revins_chars--;
7938 revins_legal++;
7939 }
7940 if (revins_on && gchar_cursor() == NUL)
7941 break;
7942#endif
7943 }
7944 /* Just a single backspace?: */
7945 if (mode == BACKSPACE_CHAR)
7946 break;
7947 } while (
7948#ifdef FEAT_RIGHTLEFT
7949 revins_on ||
7950#endif
7951 (curwin->w_cursor.col > mincol
7952 && (curwin->w_cursor.lnum != Insstart.lnum
7953 || curwin->w_cursor.col != Insstart.col)));
7954 did_backspace = TRUE;
7955 }
7956#ifdef FEAT_SMARTINDENT
7957 did_si = FALSE;
7958 can_si = FALSE;
7959 can_si_back = FALSE;
7960#endif
7961 if (curwin->w_cursor.col <= 1)
7962 did_ai = FALSE;
7963 /*
7964 * It's a little strange to put backspaces into the redo
7965 * buffer, but it makes auto-indent a lot easier to deal
7966 * with.
7967 */
7968 AppendCharToRedobuff(c);
7969
7970 /* If deleted before the insertion point, adjust it */
7971 if (curwin->w_cursor.lnum == Insstart.lnum
7972 && curwin->w_cursor.col < Insstart.col)
7973 Insstart.col = curwin->w_cursor.col;
7974
7975 /* vi behaviour: the cursor moves backward but the character that
7976 * was there remains visible
7977 * Vim behaviour: the cursor moves backward and the character that
7978 * was there is erased from the screen.
7979 * We can emulate the vi behaviour by pretending there is a dollar
7980 * displayed even when there isn't.
7981 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7982 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7983 dollar_vcol = curwin->w_virtcol;
7984
7985 return did_backspace;
7986}
7987
7988#ifdef FEAT_MOUSE
7989 static void
7990ins_mouse(c)
7991 int c;
7992{
7993 pos_T tpos;
7994
7995# ifdef FEAT_GUI
7996 /* When GUI is active, also move/paste when 'mouse' is empty */
7997 if (!gui.in_use)
7998# endif
7999 if (!mouse_has(MOUSE_INSERT))
8000 return;
8001
8002 undisplay_dollar();
8003 tpos = curwin->w_cursor;
8004 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8005 {
8006 start_arrow(&tpos);
8007# ifdef FEAT_CINDENT
8008 can_cindent = TRUE;
8009# endif
8010 }
8011
8012#ifdef FEAT_WINDOWS
8013 /* redraw status lines (in case another window became active) */
8014 redraw_statuslines();
8015#endif
8016}
8017
8018 static void
8019ins_mousescroll(up)
8020 int up;
8021{
8022 pos_T tpos;
8023# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8024 win_T *old_curwin;
8025# endif
8026
8027 tpos = curwin->w_cursor;
8028
8029# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8030 old_curwin = curwin;
8031
8032 /* Currently the mouse coordinates are only known in the GUI. */
8033 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8034 {
8035 int row, col;
8036
8037 row = mouse_row;
8038 col = mouse_col;
8039
8040 /* find the window at the pointer coordinates */
8041 curwin = mouse_find_win(&row, &col);
8042 curbuf = curwin->w_buffer;
8043 }
8044 if (curwin == old_curwin)
8045# endif
8046 undisplay_dollar();
8047
8048 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8049 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8050 else
8051 scroll_redraw(up, 3L);
8052
8053# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8054 curwin->w_redr_status = TRUE;
8055
8056 curwin = old_curwin;
8057 curbuf = curwin->w_buffer;
8058# endif
8059
8060 if (!equalpos(curwin->w_cursor, tpos))
8061 {
8062 start_arrow(&tpos);
8063# ifdef FEAT_CINDENT
8064 can_cindent = TRUE;
8065# endif
8066 }
8067}
8068#endif
8069
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008070#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8071 void
8072ins_tabline(c)
8073 int c;
8074{
8075 /* We will be leaving the current window, unless closing another tab. */
8076 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8077 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8078 {
8079 undisplay_dollar();
8080 start_arrow(&curwin->w_cursor);
8081# ifdef FEAT_CINDENT
8082 can_cindent = TRUE;
8083# endif
8084 }
8085
8086 if (c == K_TABLINE)
8087 goto_tabpage(current_tab);
8088 else
8089 handle_tabmenu();
8090
8091}
8092#endif
8093
8094#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 void
8096ins_scroll()
8097{
8098 pos_T tpos;
8099
8100 undisplay_dollar();
8101 tpos = curwin->w_cursor;
8102 if (gui_do_scroll())
8103 {
8104 start_arrow(&tpos);
8105# ifdef FEAT_CINDENT
8106 can_cindent = TRUE;
8107# endif
8108 }
8109}
8110
8111 void
8112ins_horscroll()
8113{
8114 pos_T tpos;
8115
8116 undisplay_dollar();
8117 tpos = curwin->w_cursor;
8118 if (gui_do_horiz_scroll())
8119 {
8120 start_arrow(&tpos);
8121# ifdef FEAT_CINDENT
8122 can_cindent = TRUE;
8123# endif
8124 }
8125}
8126#endif
8127
8128 static void
8129ins_left()
8130{
8131 pos_T tpos;
8132
8133#ifdef FEAT_FOLDING
8134 if ((fdo_flags & FDO_HOR) && KeyTyped)
8135 foldOpenCursor();
8136#endif
8137 undisplay_dollar();
8138 tpos = curwin->w_cursor;
8139 if (oneleft() == OK)
8140 {
8141 start_arrow(&tpos);
8142#ifdef FEAT_RIGHTLEFT
8143 /* If exit reversed string, position is fixed */
8144 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8145 revins_legal++;
8146 revins_chars++;
8147#endif
8148 }
8149
8150 /*
8151 * if 'whichwrap' set for cursor in insert mode may go to
8152 * previous line
8153 */
8154 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8155 {
8156 start_arrow(&tpos);
8157 --(curwin->w_cursor.lnum);
8158 coladvance((colnr_T)MAXCOL);
8159 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8160 }
8161 else
8162 vim_beep();
8163}
8164
8165 static void
8166ins_home(c)
8167 int c;
8168{
8169 pos_T tpos;
8170
8171#ifdef FEAT_FOLDING
8172 if ((fdo_flags & FDO_HOR) && KeyTyped)
8173 foldOpenCursor();
8174#endif
8175 undisplay_dollar();
8176 tpos = curwin->w_cursor;
8177 if (c == K_C_HOME)
8178 curwin->w_cursor.lnum = 1;
8179 curwin->w_cursor.col = 0;
8180#ifdef FEAT_VIRTUALEDIT
8181 curwin->w_cursor.coladd = 0;
8182#endif
8183 curwin->w_curswant = 0;
8184 start_arrow(&tpos);
8185}
8186
8187 static void
8188ins_end(c)
8189 int c;
8190{
8191 pos_T tpos;
8192
8193#ifdef FEAT_FOLDING
8194 if ((fdo_flags & FDO_HOR) && KeyTyped)
8195 foldOpenCursor();
8196#endif
8197 undisplay_dollar();
8198 tpos = curwin->w_cursor;
8199 if (c == K_C_END)
8200 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8201 coladvance((colnr_T)MAXCOL);
8202 curwin->w_curswant = MAXCOL;
8203
8204 start_arrow(&tpos);
8205}
8206
8207 static void
8208ins_s_left()
8209{
8210#ifdef FEAT_FOLDING
8211 if ((fdo_flags & FDO_HOR) && KeyTyped)
8212 foldOpenCursor();
8213#endif
8214 undisplay_dollar();
8215 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8216 {
8217 start_arrow(&curwin->w_cursor);
8218 (void)bck_word(1L, FALSE, FALSE);
8219 curwin->w_set_curswant = TRUE;
8220 }
8221 else
8222 vim_beep();
8223}
8224
8225 static void
8226ins_right()
8227{
8228#ifdef FEAT_FOLDING
8229 if ((fdo_flags & FDO_HOR) && KeyTyped)
8230 foldOpenCursor();
8231#endif
8232 undisplay_dollar();
8233 if (gchar_cursor() != NUL || virtual_active()
8234 )
8235 {
8236 start_arrow(&curwin->w_cursor);
8237 curwin->w_set_curswant = TRUE;
8238#ifdef FEAT_VIRTUALEDIT
8239 if (virtual_active())
8240 oneright();
8241 else
8242#endif
8243 {
8244#ifdef FEAT_MBYTE
8245 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008246 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 else
8248#endif
8249 ++curwin->w_cursor.col;
8250 }
8251
8252#ifdef FEAT_RIGHTLEFT
8253 revins_legal++;
8254 if (revins_chars)
8255 revins_chars--;
8256#endif
8257 }
8258 /* if 'whichwrap' set for cursor in insert mode, may move the
8259 * cursor to the next line */
8260 else if (vim_strchr(p_ww, ']') != NULL
8261 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8262 {
8263 start_arrow(&curwin->w_cursor);
8264 curwin->w_set_curswant = TRUE;
8265 ++curwin->w_cursor.lnum;
8266 curwin->w_cursor.col = 0;
8267 }
8268 else
8269 vim_beep();
8270}
8271
8272 static void
8273ins_s_right()
8274{
8275#ifdef FEAT_FOLDING
8276 if ((fdo_flags & FDO_HOR) && KeyTyped)
8277 foldOpenCursor();
8278#endif
8279 undisplay_dollar();
8280 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8281 || gchar_cursor() != NUL)
8282 {
8283 start_arrow(&curwin->w_cursor);
8284 (void)fwd_word(1L, FALSE, 0);
8285 curwin->w_set_curswant = TRUE;
8286 }
8287 else
8288 vim_beep();
8289}
8290
8291 static void
8292ins_up(startcol)
8293 int startcol; /* when TRUE move to Insstart.col */
8294{
8295 pos_T tpos;
8296 linenr_T old_topline = curwin->w_topline;
8297#ifdef FEAT_DIFF
8298 int old_topfill = curwin->w_topfill;
8299#endif
8300
8301 undisplay_dollar();
8302 tpos = curwin->w_cursor;
8303 if (cursor_up(1L, TRUE) == OK)
8304 {
8305 if (startcol)
8306 coladvance(getvcol_nolist(&Insstart));
8307 if (old_topline != curwin->w_topline
8308#ifdef FEAT_DIFF
8309 || old_topfill != curwin->w_topfill
8310#endif
8311 )
8312 redraw_later(VALID);
8313 start_arrow(&tpos);
8314#ifdef FEAT_CINDENT
8315 can_cindent = TRUE;
8316#endif
8317 }
8318 else
8319 vim_beep();
8320}
8321
8322 static void
8323ins_pageup()
8324{
8325 pos_T tpos;
8326
8327 undisplay_dollar();
8328 tpos = curwin->w_cursor;
8329 if (onepage(BACKWARD, 1L) == OK)
8330 {
8331 start_arrow(&tpos);
8332#ifdef FEAT_CINDENT
8333 can_cindent = TRUE;
8334#endif
8335 }
8336 else
8337 vim_beep();
8338}
8339
8340 static void
8341ins_down(startcol)
8342 int startcol; /* when TRUE move to Insstart.col */
8343{
8344 pos_T tpos;
8345 linenr_T old_topline = curwin->w_topline;
8346#ifdef FEAT_DIFF
8347 int old_topfill = curwin->w_topfill;
8348#endif
8349
8350 undisplay_dollar();
8351 tpos = curwin->w_cursor;
8352 if (cursor_down(1L, TRUE) == OK)
8353 {
8354 if (startcol)
8355 coladvance(getvcol_nolist(&Insstart));
8356 if (old_topline != curwin->w_topline
8357#ifdef FEAT_DIFF
8358 || old_topfill != curwin->w_topfill
8359#endif
8360 )
8361 redraw_later(VALID);
8362 start_arrow(&tpos);
8363#ifdef FEAT_CINDENT
8364 can_cindent = TRUE;
8365#endif
8366 }
8367 else
8368 vim_beep();
8369}
8370
8371 static void
8372ins_pagedown()
8373{
8374 pos_T tpos;
8375
8376 undisplay_dollar();
8377 tpos = curwin->w_cursor;
8378 if (onepage(FORWARD, 1L) == OK)
8379 {
8380 start_arrow(&tpos);
8381#ifdef FEAT_CINDENT
8382 can_cindent = TRUE;
8383#endif
8384 }
8385 else
8386 vim_beep();
8387}
8388
8389#ifdef FEAT_DND
8390 static void
8391ins_drop()
8392{
8393 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8394}
8395#endif
8396
8397/*
8398 * Handle TAB in Insert or Replace mode.
8399 * Return TRUE when the TAB needs to be inserted like a normal character.
8400 */
8401 static int
8402ins_tab()
8403{
8404 int ind;
8405 int i;
8406 int temp;
8407
8408 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8409 Insstart_blank_vcol = get_nolist_virtcol();
8410 if (echeck_abbr(TAB + ABBR_OFF))
8411 return FALSE;
8412
8413 ind = inindent(0);
8414#ifdef FEAT_CINDENT
8415 if (ind)
8416 can_cindent = FALSE;
8417#endif
8418
8419 /*
8420 * When nothing special, insert TAB like a normal character
8421 */
8422 if (!curbuf->b_p_et
8423 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8424 && curbuf->b_p_sts == 0)
8425 return TRUE;
8426
8427 if (stop_arrow() == FAIL)
8428 return TRUE;
8429
8430 did_ai = FALSE;
8431#ifdef FEAT_SMARTINDENT
8432 did_si = FALSE;
8433 can_si = FALSE;
8434 can_si_back = FALSE;
8435#endif
8436 AppendToRedobuff((char_u *)"\t");
8437
8438 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8439 temp = (int)curbuf->b_p_sw;
8440 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8441 temp = (int)curbuf->b_p_sts;
8442 else /* otherwise use 'tabstop' */
8443 temp = (int)curbuf->b_p_ts;
8444 temp -= get_nolist_virtcol() % temp;
8445
8446 /*
8447 * Insert the first space with ins_char(). It will delete one char in
8448 * replace mode. Insert the rest with ins_str(); it will not delete any
8449 * chars. For VREPLACE mode, we use ins_char() for all characters.
8450 */
8451 ins_char(' ');
8452 while (--temp > 0)
8453 {
8454#ifdef FEAT_VREPLACE
8455 if (State & VREPLACE_FLAG)
8456 ins_char(' ');
8457 else
8458#endif
8459 {
8460 ins_str((char_u *)" ");
8461 if (State & REPLACE_FLAG) /* no char replaced */
8462 replace_push(NUL);
8463 }
8464 }
8465
8466 /*
8467 * When 'expandtab' not set: Replace spaces by TABs where possible.
8468 */
8469 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8470 {
8471 char_u *ptr;
8472#ifdef FEAT_VREPLACE
8473 char_u *saved_line = NULL; /* init for GCC */
8474 pos_T pos;
8475#endif
8476 pos_T fpos;
8477 pos_T *cursor;
8478 colnr_T want_vcol, vcol;
8479 int change_col = -1;
8480 int save_list = curwin->w_p_list;
8481
8482 /*
8483 * Get the current line. For VREPLACE mode, don't make real changes
8484 * yet, just work on a copy of the line.
8485 */
8486#ifdef FEAT_VREPLACE
8487 if (State & VREPLACE_FLAG)
8488 {
8489 pos = curwin->w_cursor;
8490 cursor = &pos;
8491 saved_line = vim_strsave(ml_get_curline());
8492 if (saved_line == NULL)
8493 return FALSE;
8494 ptr = saved_line + pos.col;
8495 }
8496 else
8497#endif
8498 {
8499 ptr = ml_get_cursor();
8500 cursor = &curwin->w_cursor;
8501 }
8502
8503 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8504 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8505 curwin->w_p_list = FALSE;
8506
8507 /* Find first white before the cursor */
8508 fpos = curwin->w_cursor;
8509 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8510 {
8511 --fpos.col;
8512 --ptr;
8513 }
8514
8515 /* In Replace mode, don't change characters before the insert point. */
8516 if ((State & REPLACE_FLAG)
8517 && fpos.lnum == Insstart.lnum
8518 && fpos.col < Insstart.col)
8519 {
8520 ptr += Insstart.col - fpos.col;
8521 fpos.col = Insstart.col;
8522 }
8523
8524 /* compute virtual column numbers of first white and cursor */
8525 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8526 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8527
8528 /* Use as many TABs as possible. Beware of 'showbreak' and
8529 * 'linebreak' adding extra virtual columns. */
8530 while (vim_iswhite(*ptr))
8531 {
8532 i = lbr_chartabsize((char_u *)"\t", vcol);
8533 if (vcol + i > want_vcol)
8534 break;
8535 if (*ptr != TAB)
8536 {
8537 *ptr = TAB;
8538 if (change_col < 0)
8539 {
8540 change_col = fpos.col; /* Column of first change */
8541 /* May have to adjust Insstart */
8542 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8543 Insstart.col = fpos.col;
8544 }
8545 }
8546 ++fpos.col;
8547 ++ptr;
8548 vcol += i;
8549 }
8550
8551 if (change_col >= 0)
8552 {
8553 int repl_off = 0;
8554
8555 /* Skip over the spaces we need. */
8556 while (vcol < want_vcol && *ptr == ' ')
8557 {
8558 vcol += lbr_chartabsize(ptr, vcol);
8559 ++ptr;
8560 ++repl_off;
8561 }
8562 if (vcol > want_vcol)
8563 {
8564 /* Must have a char with 'showbreak' just before it. */
8565 --ptr;
8566 --repl_off;
8567 }
8568 fpos.col += repl_off;
8569
8570 /* Delete following spaces. */
8571 i = cursor->col - fpos.col;
8572 if (i > 0)
8573 {
8574 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8575 /* correct replace stack. */
8576 if ((State & REPLACE_FLAG)
8577#ifdef FEAT_VREPLACE
8578 && !(State & VREPLACE_FLAG)
8579#endif
8580 )
8581 for (temp = i; --temp >= 0; )
8582 replace_join(repl_off);
8583 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008584#ifdef FEAT_NETBEANS_INTG
8585 if (usingNetbeans)
8586 {
8587 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8588 (long)(i + 1));
8589 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8590 (char_u *)"\t", 1);
8591 }
8592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 cursor->col -= i;
8594
8595#ifdef FEAT_VREPLACE
8596 /*
8597 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8598 * backspacing over the changed spacing and then inserting the new
8599 * spacing.
8600 */
8601 if (State & VREPLACE_FLAG)
8602 {
8603 /* Backspace from real cursor to change_col */
8604 backspace_until_column(change_col);
8605
8606 /* Insert each char in saved_line from changed_col to
8607 * ptr-cursor */
8608 ins_bytes_len(saved_line + change_col,
8609 cursor->col - change_col);
8610 }
8611#endif
8612 }
8613
8614#ifdef FEAT_VREPLACE
8615 if (State & VREPLACE_FLAG)
8616 vim_free(saved_line);
8617#endif
8618 curwin->w_p_list = save_list;
8619 }
8620
8621 return FALSE;
8622}
8623
8624/*
8625 * Handle CR or NL in insert mode.
8626 * Return TRUE when out of memory or can't undo.
8627 */
8628 static int
8629ins_eol(c)
8630 int c;
8631{
8632 int i;
8633
8634 if (echeck_abbr(c + ABBR_OFF))
8635 return FALSE;
8636 if (stop_arrow() == FAIL)
8637 return TRUE;
8638 undisplay_dollar();
8639
8640 /*
8641 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8642 * character under the cursor. Only push a NUL on the replace stack,
8643 * nothing to put back when the NL is deleted.
8644 */
8645 if ((State & REPLACE_FLAG)
8646#ifdef FEAT_VREPLACE
8647 && !(State & VREPLACE_FLAG)
8648#endif
8649 )
8650 replace_push(NUL);
8651
8652 /*
8653 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8654 * replacing the next line, so we push all of the characters left on the
8655 * line onto the replace stack. This is not done here though, it is done
8656 * in open_line().
8657 */
8658
8659#ifdef FEAT_RIGHTLEFT
8660# ifdef FEAT_FKMAP
8661 if (p_altkeymap && p_fkmap)
8662 fkmap(NL);
8663# endif
8664 /* NL in reverse insert will always start in the end of
8665 * current line. */
8666 if (revins_on)
8667 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8668#endif
8669
8670 AppendToRedobuff(NL_STR);
8671 i = open_line(FORWARD,
8672#ifdef FEAT_COMMENTS
8673 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8674#endif
8675 0, old_indent);
8676 old_indent = 0;
8677#ifdef FEAT_CINDENT
8678 can_cindent = TRUE;
8679#endif
8680
8681 return (!i);
8682}
8683
8684#ifdef FEAT_DIGRAPHS
8685/*
8686 * Handle digraph in insert mode.
8687 * Returns character still to be inserted, or NUL when nothing remaining to be
8688 * done.
8689 */
8690 static int
8691ins_digraph()
8692{
8693 int c;
8694 int cc;
8695
8696 pc_status = PC_STATUS_UNSET;
8697 if (redrawing() && !char_avail())
8698 {
8699 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008700 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701
8702 edit_putchar('?', TRUE);
8703#ifdef FEAT_CMDL_INFO
8704 add_to_showcmd_c(Ctrl_K);
8705#endif
8706 }
8707
8708#ifdef USE_ON_FLY_SCROLL
8709 dont_scroll = TRUE; /* disallow scrolling here */
8710#endif
8711
8712 /* don't map the digraph chars. This also prevents the
8713 * mode message to be deleted when ESC is hit */
8714 ++no_mapping;
8715 ++allow_keys;
8716 c = safe_vgetc();
8717 --no_mapping;
8718 --allow_keys;
8719 if (IS_SPECIAL(c) || mod_mask) /* special key */
8720 {
8721#ifdef FEAT_CMDL_INFO
8722 clear_showcmd();
8723#endif
8724 insert_special(c, TRUE, FALSE);
8725 return NUL;
8726 }
8727 if (c != ESC)
8728 {
8729 if (redrawing() && !char_avail())
8730 {
8731 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008732 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733
8734 if (char2cells(c) == 1)
8735 {
8736 /* first remove the '?', otherwise it's restored when typing
8737 * an ESC next */
8738 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008739 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 edit_putchar(c, TRUE);
8741 }
8742#ifdef FEAT_CMDL_INFO
8743 add_to_showcmd_c(c);
8744#endif
8745 }
8746 ++no_mapping;
8747 ++allow_keys;
8748 cc = safe_vgetc();
8749 --no_mapping;
8750 --allow_keys;
8751 if (cc != ESC)
8752 {
8753 AppendToRedobuff((char_u *)CTRL_V_STR);
8754 c = getdigraph(c, cc, TRUE);
8755#ifdef FEAT_CMDL_INFO
8756 clear_showcmd();
8757#endif
8758 return c;
8759 }
8760 }
8761 edit_unputchar();
8762#ifdef FEAT_CMDL_INFO
8763 clear_showcmd();
8764#endif
8765 return NUL;
8766}
8767#endif
8768
8769/*
8770 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8771 * Returns the char to be inserted, or NUL if none found.
8772 */
8773 static int
8774ins_copychar(lnum)
8775 linenr_T lnum;
8776{
8777 int c;
8778 int temp;
8779 char_u *ptr, *prev_ptr;
8780
8781 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8782 {
8783 vim_beep();
8784 return NUL;
8785 }
8786
8787 /* try to advance to the cursor column */
8788 temp = 0;
8789 ptr = ml_get(lnum);
8790 prev_ptr = ptr;
8791 validate_virtcol();
8792 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8793 {
8794 prev_ptr = ptr;
8795 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8796 }
8797 if ((colnr_T)temp > curwin->w_virtcol)
8798 ptr = prev_ptr;
8799
8800#ifdef FEAT_MBYTE
8801 c = (*mb_ptr2char)(ptr);
8802#else
8803 c = *ptr;
8804#endif
8805 if (c == NUL)
8806 vim_beep();
8807 return c;
8808}
8809
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008810/*
8811 * CTRL-Y or CTRL-E typed in Insert mode.
8812 */
8813 static int
8814ins_ctrl_ey(tc)
8815 int tc;
8816{
8817 int c = tc;
8818
8819#ifdef FEAT_INS_EXPAND
8820 if (ctrl_x_mode == CTRL_X_SCROLL)
8821 {
8822 if (c == Ctrl_Y)
8823 scrolldown_clamp();
8824 else
8825 scrollup_clamp();
8826 redraw_later(VALID);
8827 }
8828 else
8829#endif
8830 {
8831 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8832 if (c != NUL)
8833 {
8834 long tw_save;
8835
8836 /* The character must be taken literally, insert like it
8837 * was typed after a CTRL-V, and pretend 'textwidth'
8838 * wasn't set. Digits, 'o' and 'x' are special after a
8839 * CTRL-V, don't use it for these. */
8840 if (c < 256 && !isalnum(c))
8841 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8842 tw_save = curbuf->b_p_tw;
8843 curbuf->b_p_tw = -1;
8844 insert_special(c, TRUE, FALSE);
8845 curbuf->b_p_tw = tw_save;
8846#ifdef FEAT_RIGHTLEFT
8847 revins_chars++;
8848 revins_legal++;
8849#endif
8850 c = Ctrl_V; /* pretend CTRL-V is last character */
8851 auto_format(FALSE, TRUE);
8852 }
8853 }
8854 return c;
8855}
8856
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857#ifdef FEAT_SMARTINDENT
8858/*
8859 * Try to do some very smart auto-indenting.
8860 * Used when inserting a "normal" character.
8861 */
8862 static void
8863ins_try_si(c)
8864 int c;
8865{
8866 pos_T *pos, old_pos;
8867 char_u *ptr;
8868 int i;
8869 int temp;
8870
8871 /*
8872 * do some very smart indenting when entering '{' or '}'
8873 */
8874 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8875 {
8876 /*
8877 * for '}' set indent equal to indent of line containing matching '{'
8878 */
8879 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8880 {
8881 old_pos = curwin->w_cursor;
8882 /*
8883 * If the matching '{' has a ')' immediately before it (ignoring
8884 * white-space), then line up with the start of the line
8885 * containing the matching '(' if there is one. This handles the
8886 * case where an "if (..\n..) {" statement continues over multiple
8887 * lines -- webb
8888 */
8889 ptr = ml_get(pos->lnum);
8890 i = pos->col;
8891 if (i > 0) /* skip blanks before '{' */
8892 while (--i > 0 && vim_iswhite(ptr[i]))
8893 ;
8894 curwin->w_cursor.lnum = pos->lnum;
8895 curwin->w_cursor.col = i;
8896 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8897 curwin->w_cursor = *pos;
8898 i = get_indent();
8899 curwin->w_cursor = old_pos;
8900#ifdef FEAT_VREPLACE
8901 if (State & VREPLACE_FLAG)
8902 change_indent(INDENT_SET, i, FALSE, NUL);
8903 else
8904#endif
8905 (void)set_indent(i, SIN_CHANGED);
8906 }
8907 else if (curwin->w_cursor.col > 0)
8908 {
8909 /*
8910 * when inserting '{' after "O" reduce indent, but not
8911 * more than indent of previous line
8912 */
8913 temp = TRUE;
8914 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8915 {
8916 old_pos = curwin->w_cursor;
8917 i = get_indent();
8918 while (curwin->w_cursor.lnum > 1)
8919 {
8920 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8921
8922 /* ignore empty lines and lines starting with '#'. */
8923 if (*ptr != '#' && *ptr != NUL)
8924 break;
8925 }
8926 if (get_indent() >= i)
8927 temp = FALSE;
8928 curwin->w_cursor = old_pos;
8929 }
8930 if (temp)
8931 shift_line(TRUE, FALSE, 1);
8932 }
8933 }
8934
8935 /*
8936 * set indent of '#' always to 0
8937 */
8938 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8939 {
8940 /* remember current indent for next line */
8941 old_indent = get_indent();
8942 (void)set_indent(0, SIN_CHANGED);
8943 }
8944
8945 /* Adjust ai_col, the char at this position can be deleted. */
8946 if (ai_col > curwin->w_cursor.col)
8947 ai_col = curwin->w_cursor.col;
8948}
8949#endif
8950
8951/*
8952 * Get the value that w_virtcol would have when 'list' is off.
8953 * Unless 'cpo' contains the 'L' flag.
8954 */
8955 static colnr_T
8956get_nolist_virtcol()
8957{
8958 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8959 return getvcol_nolist(&curwin->w_cursor);
8960 validate_virtcol();
8961 return curwin->w_virtcol;
8962}