blob: 98d287abf10596951e02d649e4f4ca0aaa400af5 [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 Moolenaara6557602006-02-04 22:43:20 +0000132static int pum_two_or_more __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 Moolenaar1c7715d2005-10-03 22:02:18 +00002350 /* 'completeopt' must contain "menu" */
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.
2366 */
2367 static int
2368pum_two_or_more()
2369{
2370 compl_T *compl;
2371 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002372
2373 /* Don't display the popup menu if there are no matches or there is only
2374 * one (ignoring the original text). */
2375 compl = compl_first_match;
2376 i = 0;
2377 do
2378 {
2379 if (compl == NULL
2380 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2381 break;
2382 compl = compl->cp_next;
2383 } while (compl != compl_first_match);
2384
2385 return (i >= 2);
2386}
2387
2388/*
2389 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002390 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002391 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002392 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002393ins_compl_show_pum()
2394{
2395 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002396 compl_T *shown_compl = NULL;
2397 int did_find_shown_match = FALSE;
2398 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002399 int i;
2400 int cur = -1;
2401 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002402 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002403
Bram Moolenaara6557602006-02-04 22:43:20 +00002404 if (!pum_wanted() || !pum_two_or_more())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002405 return;
2406
2407 /* Update the screen before drawing the popup menu over it. */
2408 update_screen(0);
2409
2410 if (compl_match_array == NULL)
2411 {
2412 /* Need to build the popup menu list. */
2413 compl_match_arraysize = 0;
2414 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002415 if (compl_leader != NULL)
2416 lead_len = STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002417 do
2418 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002419 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2420 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002421 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002422 ++compl_match_arraysize;
2423 compl = compl->cp_next;
2424 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002425 if (compl_match_arraysize == 0)
2426 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002427 compl_match_array = (pumitem_T *)alloc_clear(
2428 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002429 * compl_match_arraysize));
2430 if (compl_match_array != NULL)
2431 {
2432 i = 0;
2433 compl = compl_first_match;
2434 do
2435 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002436 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2437 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002438 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002439 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002440 if (!shown_match_ok)
2441 {
2442 if (compl == compl_shown_match || did_find_shown_match)
2443 {
2444 /* This item is the shown match or this is the
2445 * first displayed item after the shown match. */
2446 compl_shown_match = compl;
2447 did_find_shown_match = TRUE;
2448 shown_match_ok = TRUE;
2449 }
2450 else
2451 /* Remember this displayed match for when the
2452 * shown match is just below it. */
2453 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002454 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002455 }
2456 compl_match_array[i].pum_text = compl->cp_str;
2457 if (compl->cp_extra != NULL)
2458 compl_match_array[i++].pum_extra = compl->cp_extra;
2459 else
2460 compl_match_array[i++].pum_extra = compl->cp_fname;
2461 }
2462
2463 if (compl == compl_shown_match)
2464 {
2465 did_find_shown_match = TRUE;
2466 if (!shown_match_ok && shown_compl != NULL)
2467 {
2468 /* The shown match isn't displayed, set it to the
2469 * previously displayed match. */
2470 compl_shown_match = shown_compl;
2471 shown_match_ok = TRUE;
2472 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002473 }
2474 compl = compl->cp_next;
2475 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002476
2477 if (!shown_match_ok) /* no displayed match at all */
2478 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002479 }
2480 }
2481 else
2482 {
2483 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002484 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002485 if (compl_match_array[i].pum_text == compl_shown_match->cp_str)
Bram Moolenaara6557602006-02-04 22:43:20 +00002486 break;
2487 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002488 }
2489
2490 if (compl_match_array != NULL)
2491 {
2492 /* Compute the screen column of the start of the completed text.
2493 * Use the cursor to get all wrapping and other settings right. */
2494 col = curwin->w_cursor.col;
2495 curwin->w_cursor.col = compl_col;
2496 validate_cursor_col();
2497 pum_display(compl_match_array, compl_match_arraysize, cur,
2498 curwin->w_cline_row + W_WINROW(curwin),
2499 curwin->w_cline_height,
Bram Moolenaar280f1262006-01-30 00:14:18 +00002500 curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002501 curwin->w_cursor.col = col;
2502 }
2503}
2504
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505#define DICT_FIRST (1) /* use just first element in "dict" */
2506#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002507
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002509 * Add any identifiers that match the given pattern in the list of dictionary
2510 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 */
2512 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002513ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2514 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002516 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002517 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002519 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 char_u *ptr;
2521 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 char_u **files;
2524 int count;
2525 int i;
2526 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002527 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaar0b238792006-03-02 22:49:12 +00002529 if (*dict == NUL)
2530 {
2531#ifdef FEAT_SYN_HL
2532 /* When 'dictionary' is empty and spell checking is enabled use
2533 * "spell". */
2534 if (!thesaurus && curwin->w_p_spell)
2535 dict = (char_u *)"spell";
2536 else
2537#endif
2538 return;
2539 }
2540
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002542 if (buf == NULL)
2543 return;
2544
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 /* If 'infercase' is set, don't use 'smartcase' here */
2546 save_p_scs = p_scs;
2547 if (curbuf->b_p_inf)
2548 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002549
2550 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2551 * to only match at the start of a line. Otherwise just match the
2552 * pattern. */
2553 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2554 {
2555 i = STRLEN(pat) + 8;
2556 ptr = alloc(i);
2557 if (ptr == NULL)
2558 return;
2559 vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat);
2560 regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0);
2561 vim_free(ptr);
2562 }
2563 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002564 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002565 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002566 if (regmatch.regprog == NULL)
2567 goto theend;
2568 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002569
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2571 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002572 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 {
2574 /* copy one dictionary file name into buf */
2575 if (flags == DICT_EXACT)
2576 {
2577 count = 1;
2578 files = &dict;
2579 }
2580 else
2581 {
2582 /* Expand wildcards in the dictionary name, but do not allow
2583 * backticks (for security, the 'dict' option may have been set in
2584 * a modeline). */
2585 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaar0b238792006-03-02 22:49:12 +00002586 if (!thesaurus && STRCMP(buf, "spell") == 0)
2587 count = -1;
2588 else if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 || expand_wildcards(1, &buf, &count, &files,
2590 EW_FILE|EW_SILENT) != OK)
2591 count = 0;
2592 }
2593
Bram Moolenaar0b238792006-03-02 22:49:12 +00002594 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
Bram Moolenaar0b238792006-03-02 22:49:12 +00002596 /* Skip "\<" in the pattern, we don't use it as a RE. */
2597 if (pat[0] == '\\' && pat[1] == '<')
2598 ptr = pat + 2;
2599 else
2600 ptr = pat;
2601 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002603 else
2604 {
2605 ins_compl_files(count, files, thesaurus, flags,
2606 &regmatch, buf, &dir);
2607 if (flags != DICT_EXACT)
2608 FreeWild(count, files);
2609 }
2610 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 break;
2612 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002613
2614theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 p_scs = save_p_scs;
2616 vim_free(regmatch.regprog);
2617 vim_free(buf);
2618}
2619
Bram Moolenaar0b238792006-03-02 22:49:12 +00002620 static void
2621ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2622 int count;
2623 char_u **files;
2624 int thesaurus;
2625 int flags;
2626 regmatch_T *regmatch;
2627 char_u *buf;
2628 int *dir;
2629{
2630 char_u *ptr;
2631 int i;
2632 FILE *fp;
2633 int add_r;
2634
2635 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2636 {
2637 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2638 if (flags != DICT_EXACT)
2639 {
2640 vim_snprintf((char *)IObuff, IOSIZE,
2641 _("Scanning dictionary: %s"), (char *)files[i]);
2642 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2643 }
2644
2645 if (fp != NULL)
2646 {
2647 /*
2648 * Read dictionary file line by line.
2649 * Check each line for a match.
2650 */
2651 while (!got_int && !compl_interrupted
2652 && !vim_fgets(buf, LSIZE, fp))
2653 {
2654 ptr = buf;
2655 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2656 {
2657 ptr = regmatch->startp[0];
2658 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2659 ptr = find_line_end(ptr);
2660 else
2661 ptr = find_word_end(ptr);
2662 add_r = ins_compl_add_infercase(regmatch->startp[0],
2663 (int)(ptr - regmatch->startp[0]),
2664 p_ic, files[i], *dir, 0);
2665 if (thesaurus)
2666 {
2667 char_u *wstart;
2668
2669 /*
2670 * Add the other matches on the line
2671 */
2672 while (!got_int)
2673 {
2674 /* Find start of the next word. Skip white
2675 * space and punctuation. */
2676 ptr = find_word_start(ptr);
2677 if (*ptr == NUL || *ptr == NL)
2678 break;
2679 wstart = ptr;
2680
2681 /* Find end of the word and add it. */
2682#ifdef FEAT_MBYTE
2683 if (has_mbyte)
2684 /* Japanese words may have characters in
2685 * different classes, only separate words
2686 * with single-byte non-word characters. */
2687 while (*ptr != NUL)
2688 {
2689 int l = (*mb_ptr2len)(ptr);
2690
2691 if (l < 2 && !vim_iswordc(*ptr))
2692 break;
2693 ptr += l;
2694 }
2695 else
2696#endif
2697 ptr = find_word_end(ptr);
2698 add_r = ins_compl_add_infercase(wstart,
2699 (int)(ptr - wstart),
2700 p_ic, files[i], *dir, 0);
2701 }
2702 }
2703 if (add_r == OK)
2704 /* if dir was BACKWARD then honor it just once */
2705 *dir = FORWARD;
2706 else if (add_r == FAIL)
2707 break;
2708 /* avoid expensive call to vim_regexec() when at end
2709 * of line */
2710 if (*ptr == '\n' || got_int)
2711 break;
2712 }
2713 line_breakcheck();
2714 ins_compl_check_keys(50);
2715 }
2716 fclose(fp);
2717 }
2718 }
2719}
2720
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721/*
2722 * Find the start of the next word.
2723 * Returns a pointer to the first char of the word. Also stops at a NUL.
2724 */
2725 char_u *
2726find_word_start(ptr)
2727 char_u *ptr;
2728{
2729#ifdef FEAT_MBYTE
2730 if (has_mbyte)
2731 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002732 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 else
2734#endif
2735 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2736 ++ptr;
2737 return ptr;
2738}
2739
2740/*
2741 * Find the end of the word. Assumes it starts inside a word.
2742 * Returns a pointer to just after the word.
2743 */
2744 char_u *
2745find_word_end(ptr)
2746 char_u *ptr;
2747{
2748#ifdef FEAT_MBYTE
2749 int start_class;
2750
2751 if (has_mbyte)
2752 {
2753 start_class = mb_get_class(ptr);
2754 if (start_class > 1)
2755 while (*ptr != NUL)
2756 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002757 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 if (mb_get_class(ptr) != start_class)
2759 break;
2760 }
2761 }
2762 else
2763#endif
2764 while (vim_iswordc(*ptr))
2765 ++ptr;
2766 return ptr;
2767}
2768
2769/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002770 * Find the end of the line, omitting CR and NL at the end.
2771 * Returns a pointer to just after the line.
2772 */
2773 static char_u *
2774find_line_end(ptr)
2775 char_u *ptr;
2776{
2777 char_u *s;
2778
2779 s = ptr + STRLEN(ptr);
2780 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2781 --s;
2782 return s;
2783}
2784
2785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 * Free the list of completions
2787 */
2788 static void
2789ins_compl_free()
2790{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002791 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002793 vim_free(compl_pattern);
2794 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002795 vim_free(compl_leader);
2796 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002798 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002800
2801 ins_compl_del_pum();
2802 pum_clear();
2803
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002804 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 do
2806 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002807 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002808 compl_curr_match = compl_curr_match->cp_next;
2809 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002811 if (match->cp_flags & FREE_FNAME)
2812 vim_free(match->cp_fname);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002813 vim_free(match->cp_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002815 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2816 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817}
2818
2819 static void
2820ins_compl_clear()
2821{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002822 compl_cont_status = 0;
2823 compl_started = FALSE;
2824 compl_matches = 0;
2825 vim_free(compl_pattern);
2826 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002827 vim_free(compl_leader);
2828 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 edit_submode_extra = NULL;
2830}
2831
2832/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002833 * Return TRUE when Insert completion is active.
2834 */
2835 int
2836ins_compl_active()
2837{
2838 return compl_started;
2839}
2840
2841/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002842 * Delete one character before the cursor and show the subset of the matches
2843 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002844 * Returns TRUE if the work is done and another char to be got from the user.
2845 */
2846 static int
2847ins_compl_bs()
2848{
2849 char_u *line;
2850 char_u *p;
2851
2852 if (curwin->w_cursor.col <= compl_col + compl_length)
2853 {
2854 /* Deleted more than what was used to find matches, need to look for
2855 * matches all over again. */
2856 ins_compl_free();
2857 compl_started = FALSE;
2858 compl_matches = 0;
2859 }
2860
2861 line = ml_get_curline();
2862 p = line + curwin->w_cursor.col;
2863 mb_ptr_back(line, p);
2864
2865 vim_free(compl_leader);
2866 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2867 if (compl_leader != NULL)
2868 {
2869 ins_compl_del_pum();
2870 ins_compl_delete();
2871 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2872
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002873 if (compl_started)
2874 ins_compl_set_original_text(compl_leader);
2875 else
Bram Moolenaara6557602006-02-04 22:43:20 +00002876 {
2877 /* Matches were cleared, need to search for them now. */
2878 if (ins_complete(Ctrl_N) == FAIL)
2879 compl_cont_status = 0;
2880 else
2881 {
2882 /* Remove the completed word again. */
2883 ins_compl_delete();
2884 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2885 }
2886 }
2887
2888 /* Show the popup menu with a different set of matches. */
2889 ins_compl_show_pum();
2890 compl_used_match = FALSE;
2891
2892 return TRUE;
2893 }
2894 return FALSE;
2895}
2896
2897/*
2898 * Append one character to the match leader. May reduce the number of
2899 * matches.
2900 */
2901 static void
2902ins_compl_addleader(c)
2903 int c;
2904{
2905#ifdef FEAT_MBYTE
2906 int cc;
2907
2908 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2909 {
2910 char_u buf[MB_MAXBYTES + 1];
2911
2912 (*mb_char2bytes)(c, buf);
2913 buf[cc] = NUL;
2914 ins_char_bytes(buf, cc);
2915 }
2916 else
2917#endif
2918 ins_char(c);
2919
2920 vim_free(compl_leader);
2921 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
2922 curwin->w_cursor.col - compl_col);
2923 if (compl_leader != NULL)
2924 {
2925 /* Show the popup menu with a different set of matches. */
2926 ins_compl_del_pum();
2927 ins_compl_show_pum();
2928 compl_used_match = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002929 ins_compl_set_original_text(compl_leader);
2930 }
2931}
2932
2933/*
2934 * Set the first match, the original text.
2935 */
2936 static void
2937ins_compl_set_original_text(str)
2938 char_u *str;
2939{
2940 char_u *p;
2941
2942 /* Replace the original text entry. */
2943 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
2944 {
2945 p = vim_strsave(str);
2946 if (p != NULL)
2947 {
2948 vim_free(compl_first_match->cp_str);
2949 compl_first_match->cp_str = p;
2950 }
Bram Moolenaara6557602006-02-04 22:43:20 +00002951 }
2952}
2953
2954/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002955 * Append one character to the match leader. May reduce the number of
2956 * matches.
2957 */
2958 static void
2959ins_compl_addfrommatch()
2960{
2961 char_u *p;
2962 int len = curwin->w_cursor.col - compl_col;
2963 int c;
2964
2965 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002966 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002967 return;
2968 p += len;
2969#ifdef FEAT_MBYTE
2970 c = mb_ptr2char(p);
2971#else
2972 c = *p;
2973#endif
2974 ins_compl_addleader(c);
2975}
2976
2977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002979 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002980 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002982 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983ins_compl_prep(c)
2984 int c;
2985{
2986 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 int temp;
2988 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002989 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990
2991 /* Forget any previous 'special' messages if this is actually
2992 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2993 */
2994 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2995 edit_submode_extra = NULL;
2996
2997 /* Ignore end of Select mode mapping */
2998 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002999 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003001 /* Set "compl_get_longest" when finding the first matches. */
3002 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3003 || (ctrl_x_mode == 0 && !compl_started))
3004 {
3005 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3006 compl_used_match = TRUE;
3007 }
3008
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3010 {
3011 /*
3012 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3013 * it will be yet. Now we decide.
3014 */
3015 switch (c)
3016 {
3017 case Ctrl_E:
3018 case Ctrl_Y:
3019 ctrl_x_mode = CTRL_X_SCROLL;
3020 if (!(State & REPLACE_FLAG))
3021 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3022 else
3023 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3024 edit_submode_pre = NULL;
3025 showmode();
3026 break;
3027 case Ctrl_L:
3028 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3029 break;
3030 case Ctrl_F:
3031 ctrl_x_mode = CTRL_X_FILES;
3032 break;
3033 case Ctrl_K:
3034 ctrl_x_mode = CTRL_X_DICTIONARY;
3035 break;
3036 case Ctrl_R:
3037 /* Simply allow ^R to happen without affecting ^X mode */
3038 break;
3039 case Ctrl_T:
3040 ctrl_x_mode = CTRL_X_THESAURUS;
3041 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003042#ifdef FEAT_COMPL_FUNC
3043 case Ctrl_U:
3044 ctrl_x_mode = CTRL_X_FUNCTION;
3045 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003046 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003047 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003048 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003049#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003050 case 's':
3051 case Ctrl_S:
3052 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003053#ifdef FEAT_SYN_HL
3054 spell_back_to_badword();
3055#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003056 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 case Ctrl_RSB:
3058 ctrl_x_mode = CTRL_X_TAGS;
3059 break;
3060#ifdef FEAT_FIND_ID
3061 case Ctrl_I:
3062 case K_S_TAB:
3063 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3064 break;
3065 case Ctrl_D:
3066 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3067 break;
3068#endif
3069 case Ctrl_V:
3070 case Ctrl_Q:
3071 ctrl_x_mode = CTRL_X_CMDLINE;
3072 break;
3073 case Ctrl_P:
3074 case Ctrl_N:
3075 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3076 * just started ^X mode, or there were enough ^X's to cancel
3077 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3078 * do normal expansion when interrupting a different mode (say
3079 * ^X^F^X^P or ^P^X^X^P, see below)
3080 * nothing changes if interrupting mode 0, (eg, the flag
3081 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003082 if (!(compl_cont_status & CONT_INTRPT))
3083 compl_cont_status |= CONT_LOCAL;
3084 else if (compl_cont_mode != 0)
3085 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 /* FALLTHROUGH */
3087 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003088 /* If we have typed at least 2 ^X's... for modes != 0, we set
3089 * compl_cont_status = 0 (eg, as if we had just started ^X
3090 * mode).
3091 * For mode 0, we set "compl_cont_mode" to an impossible
3092 * value, in both cases ^X^X can be used to restart the same
3093 * mode (avoiding ADDING mode).
3094 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3095 * 'complete' and local ^P expansions respectively.
3096 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3097 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 if (c == Ctrl_X)
3099 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003100 if (compl_cont_mode != 0)
3101 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003103 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
3105 ctrl_x_mode = 0;
3106 edit_submode = NULL;
3107 showmode();
3108 break;
3109 }
3110 }
3111 else if (ctrl_x_mode != 0)
3112 {
3113 /* We're already in CTRL-X mode, do we stay in it? */
3114 if (!vim_is_ctrl_x_key(c))
3115 {
3116 if (ctrl_x_mode == CTRL_X_SCROLL)
3117 ctrl_x_mode = 0;
3118 else
3119 ctrl_x_mode = CTRL_X_FINISHED;
3120 edit_submode = NULL;
3121 }
3122 showmode();
3123 }
3124
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003125 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 {
3127 /* Show error message from attempted keyword completion (probably
3128 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003129 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003131 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3132 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 || ctrl_x_mode == CTRL_X_FINISHED)
3134 {
3135 /* Get here when we have finished typing a sequence of ^N and
3136 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003137 * memory that was used, and make sure we can redo the insert. */
3138 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003140 char_u *p;
3141
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 /*
3143 * If any of the original typed text has been changed,
3144 * eg when ignorecase is set, we must add back-spaces to
3145 * the redo buffer. We add as few as necessary to delete
3146 * just the part of the original text that has changed.
3147 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003148 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003149 p = compl_orig_text;
3150 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003152 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 ++ptr;
3154 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003155 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00003157 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 }
3159
3160#ifdef FEAT_CINDENT
3161 want_cindent = (can_cindent && cindent_on());
3162#endif
3163 /*
3164 * When completing whole lines: fix indent for 'cindent'.
3165 * Otherwise, break line if it's too long.
3166 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003167 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 {
3169#ifdef FEAT_CINDENT
3170 /* re-indent the current line */
3171 if (want_cindent)
3172 {
3173 do_c_expr_indent();
3174 want_cindent = FALSE; /* don't do it again */
3175 }
3176#endif
3177 }
3178 else
3179 {
3180 /* put the cursor on the last char, for 'tw' formatting */
3181 curwin->w_cursor.col--;
3182 if (stop_arrow() == OK)
3183 insertchar(NUL, 0, -1);
3184 curwin->w_cursor.col++;
3185 }
3186
3187 auto_format(FALSE, TRUE);
3188
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003189 /* if the popup menu is displayed hitting Enter means accepting
3190 * the selection without inserting anything. */
3191 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
3192 retval = TRUE;
3193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003195 compl_started = FALSE;
3196 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 msg_clr_cmdline(); /* necessary for "noshowmode" */
3198 ctrl_x_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 if (edit_submode != NULL)
3200 {
3201 edit_submode = NULL;
3202 showmode();
3203 }
3204
3205#ifdef FEAT_CINDENT
3206 /*
3207 * Indent now if a key was typed that is in 'cinkeys'.
3208 */
3209 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3210 do_c_expr_indent();
3211#endif
3212 }
3213 }
3214
3215 /* reset continue_* if we left expansion-mode, if we stay they'll be
3216 * (re)set properly in ins_complete() */
3217 if (!vim_is_ctrl_x_key(c))
3218 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003219 compl_cont_status = 0;
3220 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003222
3223 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224}
3225
3226/*
3227 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3228 * (depending on flag) starting from buf and looking for a non-scanned
3229 * buffer (other than curbuf). curbuf is special, if it is called with
3230 * buf=curbuf then it has to be the first call for a given flag/expansion.
3231 *
3232 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3233 */
3234 static buf_T *
3235ins_compl_next_buf(buf, flag)
3236 buf_T *buf;
3237 int flag;
3238{
3239#ifdef FEAT_WINDOWS
3240 static win_T *wp;
3241#endif
3242
3243 if (flag == 'w') /* just windows */
3244 {
3245#ifdef FEAT_WINDOWS
3246 if (buf == curbuf) /* first call for this flag/expansion */
3247 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003248 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 && wp->w_buffer->b_scanned)
3250 ;
3251 buf = wp->w_buffer;
3252#else
3253 buf = curbuf;
3254#endif
3255 }
3256 else
3257 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3258 * (unlisted buffers)
3259 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003260 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 && ((flag == 'U'
3262 ? buf->b_p_bl
3263 : (!buf->b_p_bl
3264 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003265 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 ;
3267 return buf;
3268}
3269
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003270#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003271static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003272
3273/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003274 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003275 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003276 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003277 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003278 static void
3279expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003280 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003281 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003282{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003283 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003284 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003285 listitem_T *li;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003286 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003287 char_u *funcname;
3288 pos_T pos;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003289 int dir = compl_direction;
3290 char_u *x;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003291 int icase;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003292
Bram Moolenaare344bea2005-09-01 20:46:49 +00003293 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3294 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003295 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003296
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003297 /* Call 'completefunc' to obtain the list of matches. */
3298 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003299 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003300
Bram Moolenaare344bea2005-09-01 20:46:49 +00003301 pos = curwin->w_cursor;
3302 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3303 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003304 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003305 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003306
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003307 /* Go through the List with matches and add each of them. */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003308 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003309 {
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003310 icase = p_ic;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003311 if (li->li_tv.v_type == VAR_DICT && li->li_tv.vval.v_dict != NULL)
3312 {
3313 p = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"word", FALSE);
3314 x = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"menu", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003315 if (get_dict_string(li->li_tv.vval.v_dict, (char_u *)"icase",
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003316 FALSE) != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003317 icase = get_dict_number(li->li_tv.vval.v_dict,
3318 (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003319 }
3320 else
3321 {
3322 p = get_tv_string_chk(&li->li_tv);
3323 x = NULL;
3324 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003325 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003326 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003327 if (ins_compl_add(p, -1, icase, NULL, x, dir, 0) == OK)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003328 /* if dir was BACKWARD then honor it just once */
3329 dir = FORWARD;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003330 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003331 else if (did_emsg)
3332 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003333 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003334
3335 list_unref(matchlist);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003336}
3337#endif /* FEAT_COMPL_FUNC */
3338
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003339/*
3340 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003341 * The search starts at position "ini" in curbuf and in the direction
3342 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003343 * When "compl_started" is FALSE start at that position, otherwise continue
3344 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003345 * This may return before finding all the matches.
3346 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 */
3348 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003349ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
3352 static pos_T first_match_pos;
3353 static pos_T last_match_pos;
3354 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003355 static int found_all = FALSE; /* Found all matches of a
3356 certain type. */
3357 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
Bram Moolenaar572cb562005-08-05 21:35:02 +00003359 pos_T *pos;
3360 char_u **matches;
3361 int save_p_scs;
3362 int save_p_ws;
3363 int save_p_ic;
3364 int i;
3365 int num_matches;
3366 int len;
3367 int found_new_match;
3368 int type = ctrl_x_mode;
3369 char_u *ptr;
3370 char_u *dict = NULL;
3371 int dict_f = 0;
3372 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003374 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 {
3376 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3377 ins_buf->b_scanned = 0;
3378 found_all = FALSE;
3379 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003380 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003381 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 last_match_pos = first_match_pos = *ini;
3383 }
3384
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003385 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003386 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3388 for (;;)
3389 {
3390 found_new_match = FAIL;
3391
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003392 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 * or if found_all says this entry is done. For ^X^L only use the
3394 * entries from 'complete' that look in loaded buffers. */
3395 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003396 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 {
3398 found_all = FALSE;
3399 while (*e_cpt == ',' || *e_cpt == ' ')
3400 e_cpt++;
3401 if (*e_cpt == '.' && !curbuf->b_scanned)
3402 {
3403 ins_buf = curbuf;
3404 first_match_pos = *ini;
3405 /* So that ^N can match word immediately after cursor */
3406 if (ctrl_x_mode == 0)
3407 dec(&first_match_pos);
3408 last_match_pos = first_match_pos;
3409 type = 0;
3410 }
3411 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3412 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3413 {
3414 /* Scan a buffer, but not the current one. */
3415 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3416 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003417 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 first_match_pos.col = last_match_pos.col = 0;
3419 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3420 last_match_pos.lnum = 0;
3421 type = 0;
3422 }
3423 else /* unloaded buffer, scan like dictionary */
3424 {
3425 found_all = TRUE;
3426 if (ins_buf->b_fname == NULL)
3427 continue;
3428 type = CTRL_X_DICTIONARY;
3429 dict = ins_buf->b_fname;
3430 dict_f = DICT_EXACT;
3431 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003432 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 ins_buf->b_fname == NULL
3434 ? buf_spname(ins_buf)
3435 : ins_buf->b_sfname == NULL
3436 ? (char *)ins_buf->b_fname
3437 : (char *)ins_buf->b_sfname);
3438 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3439 }
3440 else if (*e_cpt == NUL)
3441 break;
3442 else
3443 {
3444 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3445 type = -1;
3446 else if (*e_cpt == 'k' || *e_cpt == 's')
3447 {
3448 if (*e_cpt == 'k')
3449 type = CTRL_X_DICTIONARY;
3450 else
3451 type = CTRL_X_THESAURUS;
3452 if (*++e_cpt != ',' && *e_cpt != NUL)
3453 {
3454 dict = e_cpt;
3455 dict_f = DICT_FIRST;
3456 }
3457 }
3458#ifdef FEAT_FIND_ID
3459 else if (*e_cpt == 'i')
3460 type = CTRL_X_PATH_PATTERNS;
3461 else if (*e_cpt == 'd')
3462 type = CTRL_X_PATH_DEFINES;
3463#endif
3464 else if (*e_cpt == ']' || *e_cpt == 't')
3465 {
3466 type = CTRL_X_TAGS;
3467 sprintf((char*)IObuff, _("Scanning tags."));
3468 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3469 }
3470 else
3471 type = -1;
3472
3473 /* in any case e_cpt is advanced to the next entry */
3474 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3475
3476 found_all = TRUE;
3477 if (type == -1)
3478 continue;
3479 }
3480 }
3481
3482 switch (type)
3483 {
3484 case -1:
3485 break;
3486#ifdef FEAT_FIND_ID
3487 case CTRL_X_PATH_PATTERNS:
3488 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003489 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003490 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003492 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3494 (linenr_T)1, (linenr_T)MAXLNUM);
3495 break;
3496#endif
3497
3498 case CTRL_X_DICTIONARY:
3499 case CTRL_X_THESAURUS:
3500 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003501 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 : (type == CTRL_X_THESAURUS
3503 ? (*curbuf->b_p_tsr == NUL
3504 ? p_tsr
3505 : curbuf->b_p_tsr)
3506 : (*curbuf->b_p_dict == NUL
3507 ? p_dict
3508 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003509 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003510 dict != NULL ? dict_f
3511 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 dict = NULL;
3513 break;
3514
3515 case CTRL_X_TAGS:
3516 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3517 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003518 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519
3520 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003521 * of matches is found when compl_pattern is empty */
3522 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3524 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3525 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3526 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003527 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
3529 p_ic = save_p_ic;
3530 break;
3531
3532 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003533 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3535 {
3536
3537 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003538 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003539 ins_compl_add_matches(num_matches, matches,
3540#ifdef CASE_INSENSITIVE_FILENAME
3541 TRUE
3542#else
3543 FALSE
3544#endif
3545 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547 break;
3548
3549 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003550 if (expand_cmdline(&compl_xp, compl_pattern,
3551 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003553 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 break;
3555
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003556#ifdef FEAT_COMPL_FUNC
3557 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003558 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003559 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003560 break;
3561#endif
3562
Bram Moolenaar488c6512005-08-11 20:09:58 +00003563 case CTRL_X_SPELL:
3564#ifdef FEAT_SYN_HL
3565 num_matches = expand_spelling(first_match_pos.lnum,
3566 first_match_pos.col, compl_pattern, &matches);
3567 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003568 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003569#endif
3570 break;
3571
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 default: /* normal ^P/^N and ^X^L */
3573 /*
3574 * If 'infercase' is set, don't use 'smartcase' here
3575 */
3576 save_p_scs = p_scs;
3577 if (ins_buf->b_p_inf)
3578 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003579
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 /* buffers other than curbuf are scanned from the beginning or the
3581 * end but never from the middle, thus setting nowrapscan in this
3582 * buffers is a good idea, on the other hand, we always set
3583 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3584 save_p_ws = p_ws;
3585 if (ins_buf != curbuf)
3586 p_ws = FALSE;
3587 else if (*e_cpt == '.')
3588 p_ws = TRUE;
3589 for (;;)
3590 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003591 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003593 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3594 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003596 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003598 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003600 found_new_match = searchit(NULL, ins_buf, pos,
3601 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003602 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003603 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003604 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003606 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003607 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 first_match_pos = *pos;
3609 last_match_pos = *pos;
3610 }
3611 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003612 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 found_new_match = FAIL;
3614 if (found_new_match == FAIL)
3615 {
3616 if (ins_buf == curbuf)
3617 found_all = TRUE;
3618 break;
3619 }
3620
3621 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003622 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 && ini->lnum == pos->lnum
3624 && ini->col == pos->col)
3625 continue;
3626 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3627 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3628 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003629 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 {
3631 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3632 continue;
3633 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3634 if (!p_paste)
3635 ptr = skipwhite(ptr);
3636 }
3637 len = (int)STRLEN(ptr);
3638 }
3639 else
3640 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003641 char_u *tmp_ptr = ptr;
3642
3643 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003645 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 /* Skip if already inside a word. */
3647 if (vim_iswordp(tmp_ptr))
3648 continue;
3649 /* Find start of next word. */
3650 tmp_ptr = find_word_start(tmp_ptr);
3651 }
3652 /* Find end of this word. */
3653 tmp_ptr = find_word_end(tmp_ptr);
3654 len = (int)(tmp_ptr - ptr);
3655
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003656 if ((compl_cont_status & CONT_ADDING)
3657 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
3659 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3660 {
3661 /* Try next line, if any. the new word will be
3662 * "join" as if the normal command "J" was used.
3663 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003664 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 * works -- Acevedo */
3666 STRNCPY(IObuff, ptr, len);
3667 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3668 tmp_ptr = ptr = skipwhite(ptr);
3669 /* Find start of next word. */
3670 tmp_ptr = find_word_start(tmp_ptr);
3671 /* Find end of next word. */
3672 tmp_ptr = find_word_end(tmp_ptr);
3673 if (tmp_ptr > ptr)
3674 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003675 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003677 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 IObuff[len++] = ' ';
3679 /* IObuf =~ "\k.* ", thus len >= 2 */
3680 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003681 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 || (vim_strchr(p_cpo, CPO_JOINSP)
3683 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003684 && (IObuff[len - 2] == '?'
3685 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 IObuff[len++] = ' ';
3687 }
3688 /* copy as much as posible of the new word */
3689 if (tmp_ptr - ptr >= IOSIZE - len)
3690 tmp_ptr = ptr + IOSIZE - len - 1;
3691 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3692 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003693 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
3695 IObuff[len] = NUL;
3696 ptr = IObuff;
3697 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003698 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 continue;
3700 }
3701 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003702 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003703 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003704 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
3706 found_new_match = OK;
3707 break;
3708 }
3709 }
3710 p_scs = save_p_scs;
3711 p_ws = save_p_ws;
3712 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003713
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003714 /* check if compl_curr_match has changed, (e.g. other type of
3715 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003716 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 found_new_match = OK;
3718
3719 /* break the loop for specialized modes (use 'complete' just for the
3720 * generic ctrl_x_mode == 0) or when we've found a new match */
3721 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003722 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003723 {
3724 if (got_int)
3725 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003726 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003727 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003728 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003729
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003730 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3731 || compl_interrupted)
3732 break;
3733 compl_started = TRUE;
3734 }
3735 else
3736 {
3737 /* Mark a buffer scanned when it has been scanned completely */
3738 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3739 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003741 compl_started = FALSE;
3742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003744 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745
3746 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3747 && *e_cpt == NUL) /* Got to end of 'complete' */
3748 found_new_match = FAIL;
3749
3750 i = -1; /* total of matches, unknown */
3751 if (found_new_match == FAIL
3752 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3753 i = ins_compl_make_cyclic();
3754
3755 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003756 * just been made cyclic then we have to move compl_curr_match to the next
3757 * or previous entry (if any) -- Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003758 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003759 if (compl_curr_match == NULL)
3760 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 return i;
3762}
3763
3764/* Delete the old text being completed. */
3765 static void
3766ins_compl_delete()
3767{
3768 int i;
3769
3770 /*
3771 * In insert mode: Delete the typed part.
3772 * In replace mode: Put the old characters back, if any.
3773 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003774 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 backspace_until_column(i);
3776 changed_cline_bef_curs();
3777}
3778
3779/* Insert the new text being completed. */
3780 static void
3781ins_compl_insert()
3782{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003783 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003784 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3785 compl_used_match = FALSE;
3786 else
3787 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788}
3789
3790/*
3791 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003792 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3793 * get more completions. If it is FALSE, then we just do nothing when there
3794 * are no more completions in a given direction. The latter case is used when
3795 * we are still in the middle of finding completions, to allow browsing
3796 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 * Return the total number of matches, or -1 if still unknown -- webb.
3798 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003799 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3800 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 *
3802 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003803 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3804 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 */
3806 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003807ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003809 int count; /* repeat completion this many times; should
3810 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003811 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812{
3813 int num_matches = -1;
3814 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003815 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003816 compl_T *found_compl = NULL;
3817 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003819 if (compl_leader != NULL
3820 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003822 /* Set "compl_shown_match" to the actually shown match, it may differ
3823 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003824 while (!ins_compl_equal(compl_shown_match,
3825 compl_leader, STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003826 && compl_shown_match->cp_next != NULL
3827 && compl_shown_match->cp_next != compl_first_match)
3828 compl_shown_match = compl_shown_match->cp_next;
3829 }
3830
3831 if (allow_get_expansion && insert_match
3832 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 /* Delete old text to be replaced */
3834 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003835
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003836 compl_pending = FALSE;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003837
3838 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3839 * around. */
3840 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003842 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003844 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003845 found_end = (compl_first_match != NULL
3846 && (compl_shown_match->cp_next == compl_first_match
3847 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003848 }
3849 else if (compl_shows_dir == BACKWARD
3850 && compl_shown_match->cp_prev != NULL)
3851 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003852 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003853 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003854 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
3856 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003857 {
3858 compl_pending = TRUE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003859 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003860 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003861
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003862 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara6557602006-02-04 22:43:20 +00003863 if (compl_pending && compl_direction == compl_shows_dir)
3864 compl_shown_match = compl_curr_match;
3865 found_end = FALSE;
3866 }
3867 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
3868 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003869 && !ins_compl_equal(compl_shown_match,
3870 compl_leader, STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00003871 ++todo;
3872 else
3873 /* Remember a matching item. */
3874 found_compl = compl_shown_match;
3875
3876 /* Stop at the end of the list when we found a usable match. */
3877 if (found_end)
3878 {
3879 if (found_compl != NULL)
3880 {
3881 compl_shown_match = found_compl;
3882 break;
3883 }
3884 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 }
3887
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003888 /* Insert the text of the new completion, or the compl_leader. */
3889 if (insert_match)
3890 {
3891 if (!compl_get_longest || compl_used_match)
3892 ins_compl_insert();
3893 else
3894 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3895 }
3896 else
3897 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898
3899 if (!allow_get_expansion)
3900 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003901 /* may undisplay the popup menu first */
3902 ins_compl_upd_pum();
3903
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003904 /* redraw to show the user what was inserted */
3905 update_screen(0);
3906
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003907 /* display the updated popup menu */
3908 ins_compl_show_pum();
3909
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 /* Delete old text to be replaced, since we're still searching and
3911 * don't want to match ourselves! */
3912 ins_compl_delete();
3913 }
3914
3915 /*
3916 * Show the file name for the match (if any)
3917 * Truncate the file name to avoid a wait for return.
3918 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003919 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 {
3921 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003922 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 if (i <= 0)
3924 i = 0;
3925 else
3926 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003927 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 msg(IObuff);
3929 redraw_cmdline = FALSE; /* don't overwrite! */
3930 }
3931
3932 return num_matches;
3933}
3934
3935/*
3936 * Call this while finding completions, to check whether the user has hit a key
3937 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003938 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003940 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 */
3942 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003943ins_compl_check_keys(frequency)
3944 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945{
3946 static int count = 0;
3947
3948 int c;
3949
3950 /* Don't check when reading keys from a script. That would break the test
3951 * scripts */
3952 if (using_script())
3953 return;
3954
3955 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003956 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 return;
3958 count = 0;
3959
3960 ++no_mapping;
3961 c = vpeekc_any();
3962 --no_mapping;
3963 if (c != NUL)
3964 {
3965 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3966 {
3967 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003968 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003969 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
3970 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
3972 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003973 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003975 if (compl_pending && !got_int)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003976 (void)ins_compl_next(FALSE, 1, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003977}
3978
3979/*
3980 * Decide the direction of Insert mode complete from the key typed.
3981 * Returns BACKWARD or FORWARD.
3982 */
3983 static int
3984ins_compl_key2dir(c)
3985 int c;
3986{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003987 if (c == Ctrl_P || c == Ctrl_L
3988 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
3989 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00003990 return BACKWARD;
3991 return FORWARD;
3992}
3993
3994/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003995 * Return TRUE for keys that are used for completion only when the popup menu
3996 * is visible.
3997 */
3998 static int
3999ins_compl_pum_key(c)
4000 int c;
4001{
4002 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004003 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4004 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004005}
4006
4007/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004008 * Decide the number of completions to move forward.
4009 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4010 */
4011 static int
4012ins_compl_key2count(c)
4013 int c;
4014{
4015 int h;
4016
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004017 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004018 {
4019 h = pum_get_height();
4020 if (h > 3)
4021 h -= 2; /* keep some context */
4022 return h;
4023 }
4024 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025}
4026
4027/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004028 * Return TRUE if completion with "c" should insert the match, FALSE if only
4029 * to change the currently selected completion.
4030 */
4031 static int
4032ins_compl_use_match(c)
4033 int c;
4034{
4035 switch (c)
4036 {
4037 case K_UP:
4038 case K_DOWN:
4039 case K_PAGEDOWN:
4040 case K_KPAGEDOWN:
4041 case K_S_DOWN:
4042 case K_PAGEUP:
4043 case K_KPAGEUP:
4044 case K_S_UP:
4045 return FALSE;
4046 }
4047 return TRUE;
4048}
4049
4050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 * Do Insert mode completion.
4052 * Called when character "c" was typed, which has a meaning for completion.
4053 * Returns OK if completion was done, FAIL if something failed (out of mem).
4054 */
4055 static int
4056ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004057 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004059 char_u *line;
4060 int startcol = 0; /* column where searched text starts */
4061 colnr_T curs_col; /* cursor column */
4062 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
Bram Moolenaare3226be2005-12-18 22:10:00 +00004064 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004065 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 {
4067 /* First time we hit ^N or ^P (in a row, I mean) */
4068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 did_ai = FALSE;
4070#ifdef FEAT_SMARTINDENT
4071 did_si = FALSE;
4072 can_si = FALSE;
4073 can_si_back = FALSE;
4074#endif
4075 if (stop_arrow() == FAIL)
4076 return FAIL;
4077
4078 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004079 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004082 * "compl_startpos" to the cursor as a pattern to add a new word
4083 * instead of expand the one before the cursor, in word-wise if
4084 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 * is not in the same line as the cursor then fix it (the line has
4086 * been split because it was longer than 'tw'). if SOL is set then
4087 * skip the previous pattern, a word at the beginning of the line has
4088 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004089 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4090 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
4092 /*
4093 * it is a continued search
4094 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004095 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4097 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4098 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004099 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004101 /* line (probably) wrapped, set compl_startpos to the
4102 * first non_blank in the line, if it is not a wordchar
4103 * include it to get a better pattern, but then we don't
4104 * want the "\\<" prefix, check it bellow */
4105 compl_col = (colnr_T)(skipwhite(line) - line);
4106 compl_startpos.col = compl_col;
4107 compl_startpos.lnum = curwin->w_cursor.lnum;
4108 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110 else
4111 {
4112 /* S_IPOS was set when we inserted a word that was at the
4113 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004114 * mode but first we need to redefine compl_startpos */
4115 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004117 compl_cont_status |= CONT_SOL;
4118 compl_startpos.col = (colnr_T)(skipwhite(
4119 line + compl_length
4120 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004122 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004124 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004125 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 * have enough space? just being paranoic */
4127#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004128 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004130 compl_cont_status &= ~CONT_SOL;
4131 compl_length = (IOSIZE - MIN_SPACE);
4132 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004134 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4135 if (compl_length < 1)
4136 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
4138 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004139 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004141 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004144 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004146 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004148 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004150 compl_cont_status = 0;
4151 compl_cont_status |= CONT_N_ADDS;
4152 compl_startpos = curwin->w_cursor;
4153 startcol = (int)curs_col;
4154 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156
4157 /* Work out completion pattern and original text -- webb */
4158 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4159 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004160 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4162 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004163 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004165 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004167 compl_col += ++startcol;
4168 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 }
4170 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004171 compl_pattern = str_foldcase(line + compl_col,
4172 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004174 compl_pattern = vim_strnsave(line + compl_col,
4175 compl_length);
4176 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
4178 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004179 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
4181 char_u *prefix = (char_u *)"\\<";
4182
4183 /* we need 3 extra chars, 1 for the NUL and
4184 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004185 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4186 compl_length) + 3);
4187 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004189 if (!vim_iswordp(line + compl_col)
4190 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 && (
4192#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004193 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004195 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196#endif
4197 )))
4198 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004199 STRCPY((char *)compl_pattern, prefix);
4200 (void)quote_meta(compl_pattern + STRLEN(prefix),
4201 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004203 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004205 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004207 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208#endif
4209 )
4210 {
4211 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004212 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4213 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004215 compl_col += curs_col;
4216 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218 else
4219 {
4220#ifdef FEAT_MBYTE
4221 /* Search the point of change class of multibyte character
4222 * or not a word single byte character backward. */
4223 if (has_mbyte)
4224 {
4225 int base_class;
4226 int head_off;
4227
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004228 startcol -= (*mb_head_off)(line, line + startcol);
4229 base_class = mb_get_class(line + startcol);
4230 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004232 head_off = (*mb_head_off)(line, line + startcol);
4233 if (base_class != mb_get_class(line + startcol
4234 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004236 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 }
4238 }
4239 else
4240#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004241 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004243 compl_col += ++startcol;
4244 compl_length = (int)curs_col - startcol;
4245 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 {
4247 /* Only match word with at least two chars -- webb
4248 * there's no need to call quote_meta,
4249 * alloc(7) is enough -- Acevedo
4250 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251 compl_pattern = alloc(7);
4252 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004254 STRCPY((char *)compl_pattern, "\\<");
4255 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4256 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258 else
4259 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004260 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4261 compl_length) + 3);
4262 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004264 STRCPY((char *)compl_pattern, "\\<");
4265 (void)quote_meta(compl_pattern + 2, line + compl_col,
4266 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268 }
4269 }
4270 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4271 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004272 compl_col = skipwhite(line) - line;
4273 compl_length = (int)curs_col - (int)compl_col;
4274 if (compl_length < 0) /* cursor in indent: empty pattern */
4275 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004277 compl_pattern = str_foldcase(line + compl_col, compl_length,
4278 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004280 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4281 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 return FAIL;
4283 }
4284 else if (ctrl_x_mode == CTRL_X_FILES)
4285 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004286 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004288 compl_col += ++startcol;
4289 compl_length = (int)curs_col - startcol;
4290 compl_pattern = addstar(line + compl_col, compl_length,
4291 EXPAND_FILES);
4292 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 return FAIL;
4294 }
4295 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4296 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004297 compl_pattern = vim_strnsave(line, curs_col);
4298 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004300 set_cmd_context(&compl_xp, compl_pattern,
4301 (int)STRLEN(compl_pattern), curs_col);
4302 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4303 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004305 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4306 compl_col = startcol;
4307 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004309 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004310 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004311#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004312 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004313 * Call user defined function 'completefunc' with "a:findstart"
4314 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004315 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004316 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004317 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004318 char_u *funcname;
4319 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004320
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004321 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004322 * string */
4323 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4324 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4325 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004326 {
4327 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4328 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004329 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004330 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004331
4332 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004333 args[1] = NULL;
4334 pos = curwin->w_cursor;
4335 col = call_func_retnr(funcname, 2, args, FALSE);
4336 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004337
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004338 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004339 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004340 compl_col = col;
4341 if ((colnr_T)compl_col > curs_col)
4342 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004343
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004344 /* Setup variables for completion. Need to obtain "line" again,
4345 * it may have become invalid. */
4346 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004347 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004348 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4349 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004350#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004351 return FAIL;
4352 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004353 else if (ctrl_x_mode == CTRL_X_SPELL)
4354 {
4355#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004356 if (spell_bad_len > 0)
4357 compl_col = curs_col - spell_bad_len;
4358 else
4359 compl_col = spell_word_start(startcol);
4360 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004361 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004362 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004363 compl_length = (int)curs_col - compl_col;
4364 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4365 if (compl_pattern == NULL)
4366#endif
4367 return FAIL;
4368 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004369 else
4370 {
4371 EMSG2(_(e_intern2), "ins_complete()");
4372 return FAIL;
4373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004375 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 {
4377 edit_submode_pre = (char_u *)_(" Adding");
4378 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4379 {
4380 /* Insert a new line, keep indentation but ignore 'comments' */
4381#ifdef FEAT_COMMENTS
4382 char_u *old = curbuf->b_p_com;
4383
4384 curbuf->b_p_com = (char_u *)"";
4385#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004386 compl_startpos.lnum = curwin->w_cursor.lnum;
4387 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 ins_eol('\r');
4389#ifdef FEAT_COMMENTS
4390 curbuf->b_p_com = old;
4391#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 compl_length = 0;
4393 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
4395 }
4396 else
4397 {
4398 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004399 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 }
4401
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004402 if (compl_cont_status & CONT_LOCAL)
4403 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 else
4405 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4406
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004407 /* Always add completion for the original text. */
4408 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004409 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4410 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004411 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004413 vim_free(compl_pattern);
4414 compl_pattern = NULL;
4415 vim_free(compl_orig_text);
4416 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 return FAIL;
4418 }
4419
4420 /* showmode might reset the internal line pointers, so it must
4421 * be called before line = ml_get(), or when this address is no
4422 * longer needed. -- Acevedo.
4423 */
4424 edit_submode_extra = (char_u *)_("-- Searching...");
4425 edit_submode_highl = HLF_COUNT;
4426 showmode();
4427 edit_submode_extra = NULL;
4428 out_flush();
4429 }
4430
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004431 compl_shown_match = compl_curr_match;
4432 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
4434 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004435 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004437 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004439 /* may undisplay the popup menu */
4440 ins_compl_upd_pum();
4441
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004442 if (n > 1) /* all matches have been found */
4443 compl_matches = n;
4444 compl_curr_match = compl_shown_match;
4445 compl_direction = compl_shows_dir;
4446 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
4448 /* eat the ESC to avoid leaving insert mode */
4449 if (got_int && !global_busy)
4450 {
4451 (void)vgetc();
4452 got_int = FALSE;
4453 }
4454
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004455 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004456 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004458 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4459 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4461 edit_submode_highl = HLF_E;
4462 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4463 * because we couldn't expand anything at first place, but if we used
4464 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4465 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004466 if ( compl_length > 1
4467 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 || (ctrl_x_mode != 0
4469 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4470 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004471 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473
Bram Moolenaar572cb562005-08-05 21:35:02 +00004474 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004475 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004477 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
4479 if (edit_submode_extra == NULL)
4480 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004481 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 {
4483 edit_submode_extra = (char_u *)_("Back at original");
4484 edit_submode_highl = HLF_W;
4485 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004486 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
4488 edit_submode_extra = (char_u *)_("Word from other line");
4489 edit_submode_highl = HLF_COUNT;
4490 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004491 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
4493 edit_submode_extra = (char_u *)_("The only match");
4494 edit_submode_highl = HLF_COUNT;
4495 }
4496 else
4497 {
4498 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004499 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004501 int number = 0;
4502 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004504 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
4506 /* search backwards for the first valid (!= -1) number.
4507 * This should normally succeed already at the first loop
4508 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004509 for (match = compl_curr_match->cp_prev; match != NULL
4510 && match != compl_first_match;
4511 match = match->cp_prev)
4512 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004514 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 break;
4516 }
4517 if (match != NULL)
4518 /* go up and assign all numbers which are not assigned
4519 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004520 for (match = match->cp_next;
4521 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004522 match = match->cp_next)
4523 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 }
4525 else /* BACKWARD */
4526 {
4527 /* search forwards (upwards) for the first valid (!= -1)
4528 * number. This should normally succeed already at the
4529 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004530 for (match = compl_curr_match->cp_next; match != NULL
4531 && match != compl_first_match;
4532 match = match->cp_next)
4533 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004535 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 break;
4537 }
4538 if (match != NULL)
4539 /* go down and assign all numbers which are not
4540 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004541 for (match = match->cp_prev; match
4542 && match->cp_number == -1;
4543 match = match->cp_prev)
4544 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 }
4546 }
4547
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004548 /* The match should always have a sequence number now, this is
4549 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004550 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
4552 /* Space for 10 text chars. + 2x10-digit no.s */
4553 static char_u match_ref[31];
4554
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004555 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004557 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004559 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004560 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004561 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 edit_submode_extra = match_ref;
4563 edit_submode_highl = HLF_R;
4564 if (dollar_vcol)
4565 curs_columns(FALSE);
4566 }
4567 }
4568 }
4569
4570 /* Show a message about what (completion) mode we're in. */
4571 showmode();
4572 if (edit_submode_extra != NULL)
4573 {
4574 if (!p_smd)
4575 msg_attr(edit_submode_extra,
4576 edit_submode_highl < HLF_COUNT
4577 ? hl_attr(edit_submode_highl) : 0);
4578 }
4579 else
4580 msg_clr_cmdline(); /* necessary for "noshowmode" */
4581
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004582 ins_compl_show_pum();
4583
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 return OK;
4585}
4586
4587/*
4588 * Looks in the first "len" chars. of "src" for search-metachars.
4589 * If dest is not NULL the chars. are copied there quoting (with
4590 * a backslash) the metachars, and dest would be NUL terminated.
4591 * Returns the length (needed) of dest
4592 */
4593 static int
4594quote_meta(dest, src, len)
4595 char_u *dest;
4596 char_u *src;
4597 int len;
4598{
4599 int m;
4600
4601 for (m = len; --len >= 0; src++)
4602 {
4603 switch (*src)
4604 {
4605 case '.':
4606 case '*':
4607 case '[':
4608 if (ctrl_x_mode == CTRL_X_DICTIONARY
4609 || ctrl_x_mode == CTRL_X_THESAURUS)
4610 break;
4611 case '~':
4612 if (!p_magic) /* quote these only if magic is set */
4613 break;
4614 case '\\':
4615 if (ctrl_x_mode == CTRL_X_DICTIONARY
4616 || ctrl_x_mode == CTRL_X_THESAURUS)
4617 break;
4618 case '^': /* currently it's not needed. */
4619 case '$':
4620 m++;
4621 if (dest != NULL)
4622 *dest++ = '\\';
4623 break;
4624 }
4625 if (dest != NULL)
4626 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004627# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 /* Copy remaining bytes of a multibyte character. */
4629 if (has_mbyte)
4630 {
4631 int i, mb_len;
4632
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004633 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (mb_len > 0 && len >= mb_len)
4635 for (i = 0; i < mb_len; ++i)
4636 {
4637 --len;
4638 ++src;
4639 if (dest != NULL)
4640 *dest++ = *src;
4641 }
4642 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004643# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
4645 if (dest != NULL)
4646 *dest = NUL;
4647
4648 return m;
4649}
4650#endif /* FEAT_INS_EXPAND */
4651
4652/*
4653 * Next character is interpreted literally.
4654 * A one, two or three digit decimal number is interpreted as its byte value.
4655 * If one or two digits are entered, the next character is given to vungetc().
4656 * For Unicode a character > 255 may be returned.
4657 */
4658 int
4659get_literal()
4660{
4661 int cc;
4662 int nc;
4663 int i;
4664 int hex = FALSE;
4665 int octal = FALSE;
4666#ifdef FEAT_MBYTE
4667 int unicode = 0;
4668#endif
4669
4670 if (got_int)
4671 return Ctrl_C;
4672
4673#ifdef FEAT_GUI
4674 /*
4675 * In GUI there is no point inserting the internal code for a special key.
4676 * It is more useful to insert the string "<KEY>" instead. This would
4677 * probably be useful in a text window too, but it would not be
4678 * vi-compatible (maybe there should be an option for it?) -- webb
4679 */
4680 if (gui.in_use)
4681 ++allow_keys;
4682#endif
4683#ifdef USE_ON_FLY_SCROLL
4684 dont_scroll = TRUE; /* disallow scrolling here */
4685#endif
4686 ++no_mapping; /* don't map the next key hits */
4687 cc = 0;
4688 i = 0;
4689 for (;;)
4690 {
4691 do
4692 nc = safe_vgetc();
4693 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4694 || nc == K_HOR_SCROLLBAR);
4695#ifdef FEAT_CMDL_INFO
4696 if (!(State & CMDLINE)
4697# ifdef FEAT_MBYTE
4698 && MB_BYTE2LEN_CHECK(nc) == 1
4699# endif
4700 )
4701 add_to_showcmd(nc);
4702#endif
4703 if (nc == 'x' || nc == 'X')
4704 hex = TRUE;
4705 else if (nc == 'o' || nc == 'O')
4706 octal = TRUE;
4707#ifdef FEAT_MBYTE
4708 else if (nc == 'u' || nc == 'U')
4709 unicode = nc;
4710#endif
4711 else
4712 {
4713 if (hex
4714#ifdef FEAT_MBYTE
4715 || unicode != 0
4716#endif
4717 )
4718 {
4719 if (!vim_isxdigit(nc))
4720 break;
4721 cc = cc * 16 + hex2nr(nc);
4722 }
4723 else if (octal)
4724 {
4725 if (nc < '0' || nc > '7')
4726 break;
4727 cc = cc * 8 + nc - '0';
4728 }
4729 else
4730 {
4731 if (!VIM_ISDIGIT(nc))
4732 break;
4733 cc = cc * 10 + nc - '0';
4734 }
4735
4736 ++i;
4737 }
4738
4739 if (cc > 255
4740#ifdef FEAT_MBYTE
4741 && unicode == 0
4742#endif
4743 )
4744 cc = 255; /* limit range to 0-255 */
4745 nc = 0;
4746
4747 if (hex) /* hex: up to two chars */
4748 {
4749 if (i >= 2)
4750 break;
4751 }
4752#ifdef FEAT_MBYTE
4753 else if (unicode) /* Unicode: up to four or eight chars */
4754 {
4755 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4756 break;
4757 }
4758#endif
4759 else if (i >= 3) /* decimal or octal: up to three chars */
4760 break;
4761 }
4762 if (i == 0) /* no number entered */
4763 {
4764 if (nc == K_ZERO) /* NUL is stored as NL */
4765 {
4766 cc = '\n';
4767 nc = 0;
4768 }
4769 else
4770 {
4771 cc = nc;
4772 nc = 0;
4773 }
4774 }
4775
4776 if (cc == 0) /* NUL is stored as NL */
4777 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004778#ifdef FEAT_MBYTE
4779 if (enc_dbcs && (cc & 0xff) == 0)
4780 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4781 second byte will cause trouble! */
4782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
4784 --no_mapping;
4785#ifdef FEAT_GUI
4786 if (gui.in_use)
4787 --allow_keys;
4788#endif
4789 if (nc)
4790 vungetc(nc);
4791 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4792 return cc;
4793}
4794
4795/*
4796 * Insert character, taking care of special keys and mod_mask
4797 */
4798 static void
4799insert_special(c, allow_modmask, ctrlv)
4800 int c;
4801 int allow_modmask;
4802 int ctrlv; /* c was typed after CTRL-V */
4803{
4804 char_u *p;
4805 int len;
4806
4807 /*
4808 * Special function key, translate into "<Key>". Up to the last '>' is
4809 * inserted with ins_str(), so as not to replace characters in replace
4810 * mode.
4811 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4812 * unless 'allow_modmask' is TRUE.
4813 */
4814#ifdef MACOS
4815 /* Command-key never produces a normal key */
4816 if (mod_mask & MOD_MASK_CMD)
4817 allow_modmask = TRUE;
4818#endif
4819 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4820 {
4821 p = get_special_key_name(c, mod_mask);
4822 len = (int)STRLEN(p);
4823 c = p[len - 1];
4824 if (len > 2)
4825 {
4826 if (stop_arrow() == FAIL)
4827 return;
4828 p[len - 1] = NUL;
4829 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004830 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 ctrlv = FALSE;
4832 }
4833 }
4834 if (stop_arrow() == OK)
4835 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4836}
4837
4838/*
4839 * Special characters in this context are those that need processing other
4840 * than the simple insertion that can be performed here. This includes ESC
4841 * which terminates the insert, and CR/NL which need special processing to
4842 * open up a new line. This routine tries to optimize insertions performed by
4843 * the "redo", "undo" or "put" commands, so it needs to know when it should
4844 * stop and defer processing to the "normal" mechanism.
4845 * '0' and '^' are special, because they can be followed by CTRL-D.
4846 */
4847#ifdef EBCDIC
4848# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4849#else
4850# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4851#endif
4852
4853#ifdef FEAT_MBYTE
4854# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4855#else
4856# define WHITECHAR(cc) vim_iswhite(cc)
4857#endif
4858
4859 void
4860insertchar(c, flags, second_indent)
4861 int c; /* character to insert or NUL */
4862 int flags; /* INSCHAR_FORMAT, etc. */
4863 int second_indent; /* indent for second line if >= 0 */
4864{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 int textwidth;
4866#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
4871 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4872 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
4874 /*
4875 * Try to break the line in two or more pieces when:
4876 * - Always do this if we have been called to do formatting only.
4877 * - Always do this when 'formatoptions' has the 'a' flag and the line
4878 * ends in white space.
4879 * - Otherwise:
4880 * - Don't do this if inserting a blank
4881 * - Don't do this if an existing character is being replaced, unless
4882 * we're in VREPLACE mode.
4883 * - Do this if the cursor is not on the line where insert started
4884 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4885 * before the insert.
4886 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4887 * before 'textwidth'
4888 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004889 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 && ((flags & INSCHAR_FORMAT)
4891 || (!vim_iswhite(c)
4892 && !((State & REPLACE_FLAG)
4893#ifdef FEAT_VREPLACE
4894 && !(State & VREPLACE_FLAG)
4895#endif
4896 && *ml_get_cursor() != NUL)
4897 && (curwin->w_cursor.lnum != Insstart.lnum
4898 || ((!has_format_option(FO_INS_LONG)
4899 || Insstart_textlen <= (colnr_T)textwidth)
4900 && (!fo_ins_blank
4901 || Insstart_blank_vcol <= (colnr_T)textwidth
4902 ))))))
4903 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004904 /* Format with 'formatexpr' when it's set. Use internal formatting
4905 * when 'formatexpr' isn't set or it returns non-zero. */
4906#if defined(FEAT_EVAL)
4907 if (*curbuf->b_p_fex == NUL
4908 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004910 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004912
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 if (c == NUL) /* only formatting was wanted */
4914 return;
4915
4916#ifdef FEAT_COMMENTS
4917 /* Check whether this character should end a comment. */
4918 if (did_ai && (int)c == end_comment_pending)
4919 {
4920 char_u *line;
4921 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4922 int middle_len, end_len;
4923 int i;
4924
4925 /*
4926 * Need to remove existing (middle) comment leader and insert end
4927 * comment leader. First, check what comment leader we can find.
4928 */
4929 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4930 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4931 {
4932 /* Skip middle-comment string */
4933 while (*p && p[-1] != ':') /* find end of middle flags */
4934 ++p;
4935 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4936 /* Don't count trailing white space for middle_len */
4937 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4938 --middle_len;
4939
4940 /* Find the end-comment string */
4941 while (*p && p[-1] != ':') /* find end of end flags */
4942 ++p;
4943 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4944
4945 /* Skip white space before the cursor */
4946 i = curwin->w_cursor.col;
4947 while (--i >= 0 && vim_iswhite(line[i]))
4948 ;
4949 i++;
4950
4951 /* Skip to before the middle leader */
4952 i -= middle_len;
4953
4954 /* Check some expected things before we go on */
4955 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4956 {
4957 /* Backspace over all the stuff we want to replace */
4958 backspace_until_column(i);
4959
4960 /*
4961 * Insert the end-comment string, except for the last
4962 * character, which will get inserted as normal later.
4963 */
4964 ins_bytes_len(lead_end, end_len - 1);
4965 }
4966 }
4967 }
4968 end_comment_pending = NUL;
4969#endif
4970
4971 did_ai = FALSE;
4972#ifdef FEAT_SMARTINDENT
4973 did_si = FALSE;
4974 can_si = FALSE;
4975 can_si_back = FALSE;
4976#endif
4977
4978 /*
4979 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4980 * This speeds up normal text input considerably.
4981 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4982 * need to re-indent at a ':', or any other character (but not what
4983 * 'paste' is set)..
4984 */
4985#ifdef USE_ON_FLY_SCROLL
4986 dont_scroll = FALSE; /* allow scrolling here */
4987#endif
4988
4989 if ( !ISSPECIAL(c)
4990#ifdef FEAT_MBYTE
4991 && (!has_mbyte || (*mb_char2len)(c) == 1)
4992#endif
4993 && vpeekc() != NUL
4994 && !(State & REPLACE_FLAG)
4995#ifdef FEAT_CINDENT
4996 && !cindent_on()
4997#endif
4998#ifdef FEAT_RIGHTLEFT
4999 && !p_ri
5000#endif
5001 )
5002 {
5003#define INPUT_BUFLEN 100
5004 char_u buf[INPUT_BUFLEN + 1];
5005 int i;
5006 colnr_T virtcol = 0;
5007
5008 buf[0] = c;
5009 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005010 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 virtcol = get_nolist_virtcol();
5012 /*
5013 * Stop the string when:
5014 * - no more chars available
5015 * - finding a special character (command key)
5016 * - buffer is full
5017 * - running into the 'textwidth' boundary
5018 * - need to check for abbreviation: A non-word char after a word-char
5019 */
5020 while ( (c = vpeekc()) != NUL
5021 && !ISSPECIAL(c)
5022#ifdef FEAT_MBYTE
5023 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5024#endif
5025 && i < INPUT_BUFLEN
5026 && (textwidth == 0
5027 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5028 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5029 {
5030#ifdef FEAT_RIGHTLEFT
5031 c = vgetc();
5032 if (p_hkmap && KeyTyped)
5033 c = hkmap(c); /* Hebrew mode mapping */
5034# ifdef FEAT_FKMAP
5035 if (p_fkmap && KeyTyped)
5036 c = fkmap(c); /* Farsi mode mapping */
5037# endif
5038 buf[i++] = c;
5039#else
5040 buf[i++] = vgetc();
5041#endif
5042 }
5043
5044#ifdef FEAT_DIGRAPHS
5045 do_digraph(-1); /* clear digraphs */
5046 do_digraph(buf[i-1]); /* may be the start of a digraph */
5047#endif
5048 buf[i] = NUL;
5049 ins_str(buf);
5050 if (flags & INSCHAR_CTRLV)
5051 {
5052 redo_literal(*buf);
5053 i = 1;
5054 }
5055 else
5056 i = 0;
5057 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005058 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060 else
5061 {
5062#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005063 int cc;
5064
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5066 {
5067 char_u buf[MB_MAXBYTES + 1];
5068
5069 (*mb_char2bytes)(c, buf);
5070 buf[cc] = NUL;
5071 ins_char_bytes(buf, cc);
5072 AppendCharToRedobuff(c);
5073 }
5074 else
5075#endif
5076 {
5077 ins_char(c);
5078 if (flags & INSCHAR_CTRLV)
5079 redo_literal(c);
5080 else
5081 AppendCharToRedobuff(c);
5082 }
5083 }
5084}
5085
5086/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005087 * Format text at the current insert position.
5088 */
5089 static void
5090internal_format(textwidth, second_indent, flags, format_only)
5091 int textwidth;
5092 int second_indent;
5093 int flags;
5094 int format_only;
5095{
5096 int cc;
5097 int save_char = NUL;
5098 int haveto_redraw = FALSE;
5099 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5100#ifdef FEAT_MBYTE
5101 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5102#endif
5103 int fo_white_par = has_format_option(FO_WHITE_PAR);
5104 int first_line = TRUE;
5105#ifdef FEAT_COMMENTS
5106 colnr_T leader_len;
5107 int no_leader = FALSE;
5108 int do_comments = (flags & INSCHAR_DO_COM);
5109#endif
5110
5111 /*
5112 * When 'ai' is off we don't want a space under the cursor to be
5113 * deleted. Replace it with an 'x' temporarily.
5114 */
5115 if (!curbuf->b_p_ai)
5116 {
5117 cc = gchar_cursor();
5118 if (vim_iswhite(cc))
5119 {
5120 save_char = cc;
5121 pchar_cursor('x');
5122 }
5123 }
5124
5125 /*
5126 * Repeat breaking lines, until the current line is not too long.
5127 */
5128 while (!got_int)
5129 {
5130 int startcol; /* Cursor column at entry */
5131 int wantcol; /* column at textwidth border */
5132 int foundcol; /* column for start of spaces */
5133 int end_foundcol = 0; /* column for start of word */
5134 colnr_T len;
5135 colnr_T virtcol;
5136#ifdef FEAT_VREPLACE
5137 int orig_col = 0;
5138 char_u *saved_text = NULL;
5139#endif
5140 colnr_T col;
5141
5142 virtcol = get_nolist_virtcol();
5143 if (virtcol < (colnr_T)textwidth)
5144 break;
5145
5146#ifdef FEAT_COMMENTS
5147 if (no_leader)
5148 do_comments = FALSE;
5149 else if (!(flags & INSCHAR_FORMAT)
5150 && has_format_option(FO_WRAP_COMS))
5151 do_comments = TRUE;
5152
5153 /* Don't break until after the comment leader */
5154 if (do_comments)
5155 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5156 else
5157 leader_len = 0;
5158
5159 /* If the line doesn't start with a comment leader, then don't
5160 * start one in a following broken line. Avoids that a %word
5161 * moved to the start of the next line causes all following lines
5162 * to start with %. */
5163 if (leader_len == 0)
5164 no_leader = TRUE;
5165#endif
5166 if (!(flags & INSCHAR_FORMAT)
5167#ifdef FEAT_COMMENTS
5168 && leader_len == 0
5169#endif
5170 && !has_format_option(FO_WRAP))
5171
5172 {
5173 textwidth = 0;
5174 break;
5175 }
5176 if ((startcol = curwin->w_cursor.col) == 0)
5177 break;
5178
5179 /* find column of textwidth border */
5180 coladvance((colnr_T)textwidth);
5181 wantcol = curwin->w_cursor.col;
5182
5183 curwin->w_cursor.col = startcol - 1;
5184#ifdef FEAT_MBYTE
5185 /* Correct cursor for multi-byte character. */
5186 if (has_mbyte)
5187 mb_adjust_cursor();
5188#endif
5189 foundcol = 0;
5190
5191 /*
5192 * Find position to break at.
5193 * Stop at first entered white when 'formatoptions' has 'v'
5194 */
5195 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5196 || curwin->w_cursor.lnum != Insstart.lnum
5197 || curwin->w_cursor.col >= Insstart.col)
5198 {
5199 cc = gchar_cursor();
5200 if (WHITECHAR(cc))
5201 {
5202 /* remember position of blank just before text */
5203 end_foundcol = curwin->w_cursor.col;
5204
5205 /* find start of sequence of blanks */
5206 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5207 {
5208 dec_cursor();
5209 cc = gchar_cursor();
5210 }
5211 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5212 break; /* only spaces in front of text */
5213#ifdef FEAT_COMMENTS
5214 /* Don't break until after the comment leader */
5215 if (curwin->w_cursor.col < leader_len)
5216 break;
5217#endif
5218 if (has_format_option(FO_ONE_LETTER))
5219 {
5220 /* do not break after one-letter words */
5221 if (curwin->w_cursor.col == 0)
5222 break; /* one-letter word at begin */
5223
5224 col = curwin->w_cursor.col;
5225 dec_cursor();
5226 cc = gchar_cursor();
5227
5228 if (WHITECHAR(cc))
5229 continue; /* one-letter, continue */
5230 curwin->w_cursor.col = col;
5231 }
5232#ifdef FEAT_MBYTE
5233 if (has_mbyte)
5234 foundcol = curwin->w_cursor.col
5235 + (*mb_ptr2len)(ml_get_cursor());
5236 else
5237#endif
5238 foundcol = curwin->w_cursor.col + 1;
5239 if (curwin->w_cursor.col < (colnr_T)wantcol)
5240 break;
5241 }
5242#ifdef FEAT_MBYTE
5243 else if (cc >= 0x100 && fo_multibyte
5244 && curwin->w_cursor.col <= (colnr_T)wantcol)
5245 {
5246 /* Break after or before a multi-byte character. */
5247 foundcol = curwin->w_cursor.col;
5248 if (curwin->w_cursor.col < (colnr_T)wantcol)
5249 foundcol += (*mb_char2len)(cc);
5250 end_foundcol = foundcol;
5251 break;
5252 }
5253#endif
5254 if (curwin->w_cursor.col == 0)
5255 break;
5256 dec_cursor();
5257 }
5258
5259 if (foundcol == 0) /* no spaces, cannot break line */
5260 {
5261 curwin->w_cursor.col = startcol;
5262 break;
5263 }
5264
5265 /* Going to break the line, remove any "$" now. */
5266 undisplay_dollar();
5267
5268 /*
5269 * Offset between cursor position and line break is used by replace
5270 * stack functions. VREPLACE does not use this, and backspaces
5271 * over the text instead.
5272 */
5273#ifdef FEAT_VREPLACE
5274 if (State & VREPLACE_FLAG)
5275 orig_col = startcol; /* Will start backspacing from here */
5276 else
5277#endif
5278 replace_offset = startcol - end_foundcol - 1;
5279
5280 /*
5281 * adjust startcol for spaces that will be deleted and
5282 * characters that will remain on top line
5283 */
5284 curwin->w_cursor.col = foundcol;
5285 while (cc = gchar_cursor(), WHITECHAR(cc))
5286 inc_cursor();
5287 startcol -= curwin->w_cursor.col;
5288 if (startcol < 0)
5289 startcol = 0;
5290
5291#ifdef FEAT_VREPLACE
5292 if (State & VREPLACE_FLAG)
5293 {
5294 /*
5295 * In VREPLACE mode, we will backspace over the text to be
5296 * wrapped, so save a copy now to put on the next line.
5297 */
5298 saved_text = vim_strsave(ml_get_cursor());
5299 curwin->w_cursor.col = orig_col;
5300 if (saved_text == NULL)
5301 break; /* Can't do it, out of memory */
5302 saved_text[startcol] = NUL;
5303
5304 /* Backspace over characters that will move to the next line */
5305 if (!fo_white_par)
5306 backspace_until_column(foundcol);
5307 }
5308 else
5309#endif
5310 {
5311 /* put cursor after pos. to break line */
5312 if (!fo_white_par)
5313 curwin->w_cursor.col = foundcol;
5314 }
5315
5316 /*
5317 * Split the line just before the margin.
5318 * Only insert/delete lines, but don't really redraw the window.
5319 */
5320 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5321 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5322#ifdef FEAT_COMMENTS
5323 + (do_comments ? OPENLINE_DO_COM : 0)
5324#endif
5325 , old_indent);
5326 old_indent = 0;
5327
5328 replace_offset = 0;
5329 if (first_line)
5330 {
5331 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5332 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5333 if (second_indent >= 0)
5334 {
5335#ifdef FEAT_VREPLACE
5336 if (State & VREPLACE_FLAG)
5337 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5338 else
5339#endif
5340 (void)set_indent(second_indent, SIN_CHANGED);
5341 }
5342 first_line = FALSE;
5343 }
5344
5345#ifdef FEAT_VREPLACE
5346 if (State & VREPLACE_FLAG)
5347 {
5348 /*
5349 * In VREPLACE mode we have backspaced over the text to be
5350 * moved, now we re-insert it into the new line.
5351 */
5352 ins_bytes(saved_text);
5353 vim_free(saved_text);
5354 }
5355 else
5356#endif
5357 {
5358 /*
5359 * Check if cursor is not past the NUL off the line, cindent
5360 * may have added or removed indent.
5361 */
5362 curwin->w_cursor.col += startcol;
5363 len = (colnr_T)STRLEN(ml_get_curline());
5364 if (curwin->w_cursor.col > len)
5365 curwin->w_cursor.col = len;
5366 }
5367
5368 haveto_redraw = TRUE;
5369#ifdef FEAT_CINDENT
5370 can_cindent = TRUE;
5371#endif
5372 /* moved the cursor, don't autoindent or cindent now */
5373 did_ai = FALSE;
5374#ifdef FEAT_SMARTINDENT
5375 did_si = FALSE;
5376 can_si = FALSE;
5377 can_si_back = FALSE;
5378#endif
5379 line_breakcheck();
5380 }
5381
5382 if (save_char != NUL) /* put back space after cursor */
5383 pchar_cursor(save_char);
5384
5385 if (!format_only && haveto_redraw)
5386 {
5387 update_topline();
5388 redraw_curbuf_later(VALID);
5389 }
5390}
5391
5392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 * Called after inserting or deleting text: When 'formatoptions' includes the
5394 * 'a' flag format from the current line until the end of the paragraph.
5395 * Keep the cursor at the same position relative to the text.
5396 * The caller must have saved the cursor line for undo, following ones will be
5397 * saved here.
5398 */
5399 void
5400auto_format(trailblank, prev_line)
5401 int trailblank; /* when TRUE also format with trailing blank */
5402 int prev_line; /* may start in previous line */
5403{
5404 pos_T pos;
5405 colnr_T len;
5406 char_u *old;
5407 char_u *new, *pnew;
5408 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005409 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
5411 if (!has_format_option(FO_AUTO))
5412 return;
5413
5414 pos = curwin->w_cursor;
5415 old = ml_get_curline();
5416
5417 /* may remove added space */
5418 check_auto_format(FALSE);
5419
5420 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5421 * user might insert normal text next. Also skip formatting when "1" is
5422 * in 'formatoptions' and there is a single character before the cursor.
5423 * Otherwise the line would be broken and when typing another non-white
5424 * next they are not joined back together. */
5425 wasatend = (pos.col == STRLEN(old));
5426 if (*old != NUL && !trailblank && wasatend)
5427 {
5428 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005429 cc = gchar_cursor();
5430 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5431 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005433 cc = gchar_cursor();
5434 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 {
5436 curwin->w_cursor = pos;
5437 return;
5438 }
5439 curwin->w_cursor = pos;
5440 }
5441
5442#ifdef FEAT_COMMENTS
5443 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5444 * comments. */
5445 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5446 && get_leader_len(old, NULL, FALSE) == 0)
5447 return;
5448#endif
5449
5450 /*
5451 * May start formatting in a previous line, so that after "x" a word is
5452 * moved to the previous line if it fits there now. Only when this is not
5453 * the start of a paragraph.
5454 */
5455 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5456 {
5457 --curwin->w_cursor.lnum;
5458 if (u_save_cursor() == FAIL)
5459 return;
5460 }
5461
5462 /*
5463 * Do the formatting and restore the cursor position. "saved_cursor" will
5464 * be adjusted for the text formatting.
5465 */
5466 saved_cursor = pos;
5467 format_lines((linenr_T)-1);
5468 curwin->w_cursor = saved_cursor;
5469 saved_cursor.lnum = 0;
5470
5471 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5472 {
5473 /* "cannot happen" */
5474 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5475 coladvance((colnr_T)MAXCOL);
5476 }
5477 else
5478 check_cursor_col();
5479
5480 /* Insert mode: If the cursor is now after the end of the line while it
5481 * previously wasn't, the line was broken. Because of the rule above we
5482 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5483 * formatted. */
5484 if (!wasatend && has_format_option(FO_WHITE_PAR))
5485 {
5486 new = ml_get_curline();
5487 len = STRLEN(new);
5488 if (curwin->w_cursor.col == len)
5489 {
5490 pnew = vim_strnsave(new, len + 2);
5491 pnew[len] = ' ';
5492 pnew[len + 1] = NUL;
5493 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5494 /* remove the space later */
5495 did_add_space = TRUE;
5496 }
5497 else
5498 /* may remove added space */
5499 check_auto_format(FALSE);
5500 }
5501
5502 check_cursor();
5503}
5504
5505/*
5506 * When an extra space was added to continue a paragraph for auto-formatting,
5507 * delete it now. The space must be under the cursor, just after the insert
5508 * position.
5509 */
5510 static void
5511check_auto_format(end_insert)
5512 int end_insert; /* TRUE when ending Insert mode */
5513{
5514 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005515 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
5517 if (did_add_space)
5518 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005519 cc = gchar_cursor();
5520 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 /* Somehow the space was removed already. */
5522 did_add_space = FALSE;
5523 else
5524 {
5525 if (!end_insert)
5526 {
5527 inc_cursor();
5528 c = gchar_cursor();
5529 dec_cursor();
5530 }
5531 if (c != NUL)
5532 {
5533 /* The space is no longer at the end of the line, delete it. */
5534 del_char(FALSE);
5535 did_add_space = FALSE;
5536 }
5537 }
5538 }
5539}
5540
5541/*
5542 * Find out textwidth to be used for formatting:
5543 * if 'textwidth' option is set, use it
5544 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5545 * if invalid value, use 0.
5546 * Set default to window width (maximum 79) for "gq" operator.
5547 */
5548 int
5549comp_textwidth(ff)
5550 int ff; /* force formatting (for "Q" command) */
5551{
5552 int textwidth;
5553
5554 textwidth = curbuf->b_p_tw;
5555 if (textwidth == 0 && curbuf->b_p_wm)
5556 {
5557 /* The width is the window width minus 'wrapmargin' minus all the
5558 * things that add to the margin. */
5559 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5560#ifdef FEAT_CMDWIN
5561 if (cmdwin_type != 0)
5562 textwidth -= 1;
5563#endif
5564#ifdef FEAT_FOLDING
5565 textwidth -= curwin->w_p_fdc;
5566#endif
5567#ifdef FEAT_SIGNS
5568 if (curwin->w_buffer->b_signlist != NULL
5569# ifdef FEAT_NETBEANS_INTG
5570 || usingNetbeans
5571# endif
5572 )
5573 textwidth -= 1;
5574#endif
5575 if (curwin->w_p_nu)
5576 textwidth -= 8;
5577 }
5578 if (textwidth < 0)
5579 textwidth = 0;
5580 if (ff && textwidth == 0)
5581 {
5582 textwidth = W_WIDTH(curwin) - 1;
5583 if (textwidth > 79)
5584 textwidth = 79;
5585 }
5586 return textwidth;
5587}
5588
5589/*
5590 * Put a character in the redo buffer, for when just after a CTRL-V.
5591 */
5592 static void
5593redo_literal(c)
5594 int c;
5595{
5596 char_u buf[10];
5597
5598 /* Only digits need special treatment. Translate them into a string of
5599 * three digits. */
5600 if (VIM_ISDIGIT(c))
5601 {
5602 sprintf((char *)buf, "%03d", c);
5603 AppendToRedobuff(buf);
5604 }
5605 else
5606 AppendCharToRedobuff(c);
5607}
5608
5609/*
5610 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005611 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 */
5613 static void
5614start_arrow(end_insert_pos)
5615 pos_T *end_insert_pos;
5616{
5617 if (!arrow_used) /* something has been inserted */
5618 {
5619 AppendToRedobuff(ESC_STR);
5620 stop_insert(end_insert_pos, FALSE);
5621 arrow_used = TRUE; /* this means we stopped the current insert */
5622 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005623#ifdef FEAT_SYN_HL
5624 check_spell_redraw();
5625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626}
5627
Bram Moolenaar217ad922005-03-20 22:37:15 +00005628#ifdef FEAT_SYN_HL
5629/*
5630 * If we skipped highlighting word at cursor, do it now.
5631 * It may be skipped again, thus reset spell_redraw_lnum first.
5632 */
5633 static void
5634check_spell_redraw()
5635{
5636 if (spell_redraw_lnum != 0)
5637 {
5638 linenr_T lnum = spell_redraw_lnum;
5639
5640 spell_redraw_lnum = 0;
5641 redrawWinline(lnum, FALSE);
5642 }
5643}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005644
5645/*
5646 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5647 * spelled word, if there is one.
5648 */
5649 static void
5650spell_back_to_badword()
5651{
5652 pos_T tpos = curwin->w_cursor;
5653
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005654 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005655 if (curwin->w_cursor.col != tpos.col)
5656 start_arrow(&tpos);
5657}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005658#endif
5659
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660/*
5661 * stop_arrow() is called before a change is made in insert mode.
5662 * If an arrow key has been used, start a new insertion.
5663 * Returns FAIL if undo is impossible, shouldn't insert then.
5664 */
5665 int
5666stop_arrow()
5667{
5668 if (arrow_used)
5669 {
5670 if (u_save_cursor() == OK)
5671 {
5672 arrow_used = FALSE;
5673 ins_need_undo = FALSE;
5674 }
5675 Insstart = curwin->w_cursor; /* new insertion starts here */
5676 Insstart_textlen = linetabsize(ml_get_curline());
5677 ai_col = 0;
5678#ifdef FEAT_VREPLACE
5679 if (State & VREPLACE_FLAG)
5680 {
5681 orig_line_count = curbuf->b_ml.ml_line_count;
5682 vr_lines_changed = 1;
5683 }
5684#endif
5685 ResetRedobuff();
5686 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005687 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 else if (ins_need_undo)
5690 {
5691 if (u_save_cursor() == OK)
5692 ins_need_undo = FALSE;
5693 }
5694
5695#ifdef FEAT_FOLDING
5696 /* Always open fold at the cursor line when inserting something. */
5697 foldOpenCursor();
5698#endif
5699
5700 return (arrow_used || ins_need_undo ? FAIL : OK);
5701}
5702
5703/*
5704 * do a few things to stop inserting
5705 */
5706 static void
5707stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005708 pos_T *end_insert_pos; /* where insert ended */
5709 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005711 int cc;
5712 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713
5714 stop_redo_ins();
5715 replace_flush(); /* abandon replace stack */
5716
5717 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005718 * Save the inserted text for later redo with ^@ and CTRL-A.
5719 * Don't do it when "restart_edit" was set and nothing was inserted,
5720 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005722 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005723 if (did_restart_edit == 0 || (ptr != NULL
5724 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005725 {
5726 vim_free(last_insert);
5727 last_insert = ptr;
5728 last_insert_skip = new_insert_skip;
5729 }
5730 else
5731 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732
5733 if (!arrow_used)
5734 {
5735 /* Auto-format now. It may seem strange to do this when stopping an
5736 * insertion (or moving the cursor), but it's required when appending
5737 * a line and having it end in a space. But only do it when something
5738 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005739 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005741 pos_T tpos = curwin->w_cursor;
5742
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 /* When the cursor is at the end of the line after a space the
5744 * formatting will move it to the following word. Avoid that by
5745 * moving the cursor onto the space. */
5746 cc = 'x';
5747 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5748 {
5749 dec_cursor();
5750 cc = gchar_cursor();
5751 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005752 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
5754
5755 auto_format(TRUE, FALSE);
5756
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005757 if (vim_iswhite(cc))
5758 {
5759 if (gchar_cursor() != NUL)
5760 inc_cursor();
5761#ifdef FEAT_VIRTUALEDIT
5762 /* If the cursor is still at the same character, also keep
5763 * the "coladd". */
5764 if (gchar_cursor() == NUL
5765 && curwin->w_cursor.lnum == tpos.lnum
5766 && curwin->w_cursor.col == tpos.col)
5767 curwin->w_cursor.coladd = tpos.coladd;
5768#endif
5769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 }
5771
5772 /* If a space was inserted for auto-formatting, remove it now. */
5773 check_auto_format(TRUE);
5774
5775 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005776 * of the line, and put the cursor back.
5777 * Do this when ESC was used or moving the cursor up/down. */
5778 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5779 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005781 pos_T tpos = curwin->w_cursor;
5782
5783 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5785 --curwin->w_cursor.col;
5786 while (cc = gchar_cursor(), vim_iswhite(cc))
5787 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005788 if (curwin->w_cursor.lnum != tpos.lnum)
5789 curwin->w_cursor = tpos;
5790 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5792
5793#ifdef FEAT_VISUAL
5794 /* <C-S-Right> may have started Visual mode, adjust the position for
5795 * deleted characters. */
5796 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5797 {
5798 cc = STRLEN(ml_get_curline());
5799 if (VIsual.col > (colnr_T)cc)
5800 {
5801 VIsual.col = cc;
5802# ifdef FEAT_VIRTUALEDIT
5803 VIsual.coladd = 0;
5804# endif
5805 }
5806 }
5807#endif
5808 }
5809 }
5810 did_ai = FALSE;
5811#ifdef FEAT_SMARTINDENT
5812 did_si = FALSE;
5813 can_si = FALSE;
5814 can_si_back = FALSE;
5815#endif
5816
5817 /* set '[ and '] to the inserted text */
5818 curbuf->b_op_start = Insstart;
5819 curbuf->b_op_end = *end_insert_pos;
5820}
5821
5822/*
5823 * Set the last inserted text to a single character.
5824 * Used for the replace command.
5825 */
5826 void
5827set_last_insert(c)
5828 int c;
5829{
5830 char_u *s;
5831
5832 vim_free(last_insert);
5833#ifdef FEAT_MBYTE
5834 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5835#else
5836 last_insert = alloc(6);
5837#endif
5838 if (last_insert != NULL)
5839 {
5840 s = last_insert;
5841 /* Use the CTRL-V only when entering a special char */
5842 if (c < ' ' || c == DEL)
5843 *s++ = Ctrl_V;
5844 s = add_char2buf(c, s);
5845 *s++ = ESC;
5846 *s++ = NUL;
5847 last_insert_skip = 0;
5848 }
5849}
5850
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005851#if defined(EXITFREE) || defined(PROTO)
5852 void
5853free_last_insert()
5854{
5855 vim_free(last_insert);
5856 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005857 vim_free(compl_orig_text);
5858 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005859}
5860#endif
5861
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862/*
5863 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5864 * and CSI. Handle multi-byte characters.
5865 * Returns a pointer to after the added bytes.
5866 */
5867 char_u *
5868add_char2buf(c, s)
5869 int c;
5870 char_u *s;
5871{
5872#ifdef FEAT_MBYTE
5873 char_u temp[MB_MAXBYTES];
5874 int i;
5875 int len;
5876
5877 len = (*mb_char2bytes)(c, temp);
5878 for (i = 0; i < len; ++i)
5879 {
5880 c = temp[i];
5881#endif
5882 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5883 if (c == K_SPECIAL)
5884 {
5885 *s++ = K_SPECIAL;
5886 *s++ = KS_SPECIAL;
5887 *s++ = KE_FILLER;
5888 }
5889#ifdef FEAT_GUI
5890 else if (c == CSI)
5891 {
5892 *s++ = CSI;
5893 *s++ = KS_EXTRA;
5894 *s++ = (int)KE_CSI;
5895 }
5896#endif
5897 else
5898 *s++ = c;
5899#ifdef FEAT_MBYTE
5900 }
5901#endif
5902 return s;
5903}
5904
5905/*
5906 * move cursor to start of line
5907 * if flags & BL_WHITE move to first non-white
5908 * if flags & BL_SOL move to first non-white if startofline is set,
5909 * otherwise keep "curswant" column
5910 * if flags & BL_FIX don't leave the cursor on a NUL.
5911 */
5912 void
5913beginline(flags)
5914 int flags;
5915{
5916 if ((flags & BL_SOL) && !p_sol)
5917 coladvance(curwin->w_curswant);
5918 else
5919 {
5920 curwin->w_cursor.col = 0;
5921#ifdef FEAT_VIRTUALEDIT
5922 curwin->w_cursor.coladd = 0;
5923#endif
5924
5925 if (flags & (BL_WHITE | BL_SOL))
5926 {
5927 char_u *ptr;
5928
5929 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5930 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5931 ++curwin->w_cursor.col;
5932 }
5933 curwin->w_set_curswant = TRUE;
5934 }
5935}
5936
5937/*
5938 * oneright oneleft cursor_down cursor_up
5939 *
5940 * Move one char {right,left,down,up}.
5941 * Doesn't move onto the NUL past the end of the line.
5942 * Return OK when successful, FAIL when we hit a line of file boundary.
5943 */
5944
5945 int
5946oneright()
5947{
5948 char_u *ptr;
5949#ifdef FEAT_MBYTE
5950 int l;
5951#endif
5952
5953#ifdef FEAT_VIRTUALEDIT
5954 if (virtual_active())
5955 {
5956 pos_T prevpos = curwin->w_cursor;
5957
5958 /* Adjust for multi-wide char (excluding TAB) */
5959 ptr = ml_get_cursor();
5960 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5961#ifdef FEAT_MBYTE
5962 (*mb_ptr2char)(ptr)
5963#else
5964 *ptr
5965#endif
5966 ))
5967 ? ptr2cells(ptr) : 1));
5968 curwin->w_set_curswant = TRUE;
5969 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5970 return (prevpos.col != curwin->w_cursor.col
5971 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5972 }
5973#endif
5974
5975 ptr = ml_get_cursor();
5976#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005977 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 {
5979 /* The character under the cursor is a multi-byte character, move
5980 * several bytes right, but don't end up on the NUL. */
5981 if (ptr[l] == NUL)
5982 return FAIL;
5983 curwin->w_cursor.col += l;
5984 }
5985 else
5986#endif
5987 {
5988 if (*ptr++ == NUL || *ptr == NUL)
5989 return FAIL;
5990 ++curwin->w_cursor.col;
5991 }
5992
5993 curwin->w_set_curswant = TRUE;
5994 return OK;
5995}
5996
5997 int
5998oneleft()
5999{
6000#ifdef FEAT_VIRTUALEDIT
6001 if (virtual_active())
6002 {
6003 int width;
6004 int v = getviscol();
6005
6006 if (v == 0)
6007 return FAIL;
6008
6009# ifdef FEAT_LINEBREAK
6010 /* We might get stuck on 'showbreak', skip over it. */
6011 width = 1;
6012 for (;;)
6013 {
6014 coladvance(v - width);
6015 /* getviscol() is slow, skip it when 'showbreak' is empty and
6016 * there are no multi-byte characters */
6017 if ((*p_sbr == NUL
6018# ifdef FEAT_MBYTE
6019 && !has_mbyte
6020# endif
6021 ) || getviscol() < v)
6022 break;
6023 ++width;
6024 }
6025# else
6026 coladvance(v - 1);
6027# endif
6028
6029 if (curwin->w_cursor.coladd == 1)
6030 {
6031 char_u *ptr;
6032
6033 /* Adjust for multi-wide char (not a TAB) */
6034 ptr = ml_get_cursor();
6035 if (*ptr != TAB && vim_isprintc(
6036# ifdef FEAT_MBYTE
6037 (*mb_ptr2char)(ptr)
6038# else
6039 *ptr
6040# endif
6041 ) && ptr2cells(ptr) > 1)
6042 curwin->w_cursor.coladd = 0;
6043 }
6044
6045 curwin->w_set_curswant = TRUE;
6046 return OK;
6047 }
6048#endif
6049
6050 if (curwin->w_cursor.col == 0)
6051 return FAIL;
6052
6053 curwin->w_set_curswant = TRUE;
6054 --curwin->w_cursor.col;
6055
6056#ifdef FEAT_MBYTE
6057 /* if the character on the left of the current cursor is a multi-byte
6058 * character, move to its first byte */
6059 if (has_mbyte)
6060 mb_adjust_cursor();
6061#endif
6062 return OK;
6063}
6064
6065 int
6066cursor_up(n, upd_topline)
6067 long n;
6068 int upd_topline; /* When TRUE: update topline */
6069{
6070 linenr_T lnum;
6071
6072 if (n > 0)
6073 {
6074 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006075 /* This fails if the cursor is already in the first line or the count
6076 * is larger than the line number and '-' is in 'cpoptions' */
6077 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 return FAIL;
6079 if (n >= lnum)
6080 lnum = 1;
6081 else
6082#ifdef FEAT_FOLDING
6083 if (hasAnyFolding(curwin))
6084 {
6085 /*
6086 * Count each sequence of folded lines as one logical line.
6087 */
6088 /* go to the the start of the current fold */
6089 (void)hasFolding(lnum, &lnum, NULL);
6090
6091 while (n--)
6092 {
6093 /* move up one line */
6094 --lnum;
6095 if (lnum <= 1)
6096 break;
6097 /* If we entered a fold, move to the beginning, unless in
6098 * Insert mode or when 'foldopen' contains "all": it will open
6099 * in a moment. */
6100 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6101 (void)hasFolding(lnum, &lnum, NULL);
6102 }
6103 if (lnum < 1)
6104 lnum = 1;
6105 }
6106 else
6107#endif
6108 lnum -= n;
6109 curwin->w_cursor.lnum = lnum;
6110 }
6111
6112 /* try to advance to the column we want to be at */
6113 coladvance(curwin->w_curswant);
6114
6115 if (upd_topline)
6116 update_topline(); /* make sure curwin->w_topline is valid */
6117
6118 return OK;
6119}
6120
6121/*
6122 * Cursor down a number of logical lines.
6123 */
6124 int
6125cursor_down(n, upd_topline)
6126 long n;
6127 int upd_topline; /* When TRUE: update topline */
6128{
6129 linenr_T lnum;
6130
6131 if (n > 0)
6132 {
6133 lnum = curwin->w_cursor.lnum;
6134#ifdef FEAT_FOLDING
6135 /* Move to last line of fold, will fail if it's the end-of-file. */
6136 (void)hasFolding(lnum, NULL, &lnum);
6137#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006138 /* This fails if the cursor is already in the last line or would move
6139 * beyound the last line and '-' is in 'cpoptions' */
6140 if (lnum >= curbuf->b_ml.ml_line_count
6141 || (lnum + n > curbuf->b_ml.ml_line_count
6142 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 return FAIL;
6144 if (lnum + n >= curbuf->b_ml.ml_line_count)
6145 lnum = curbuf->b_ml.ml_line_count;
6146 else
6147#ifdef FEAT_FOLDING
6148 if (hasAnyFolding(curwin))
6149 {
6150 linenr_T last;
6151
6152 /* count each sequence of folded lines as one logical line */
6153 while (n--)
6154 {
6155 if (hasFolding(lnum, NULL, &last))
6156 lnum = last + 1;
6157 else
6158 ++lnum;
6159 if (lnum >= curbuf->b_ml.ml_line_count)
6160 break;
6161 }
6162 if (lnum > curbuf->b_ml.ml_line_count)
6163 lnum = curbuf->b_ml.ml_line_count;
6164 }
6165 else
6166#endif
6167 lnum += n;
6168 curwin->w_cursor.lnum = lnum;
6169 }
6170
6171 /* try to advance to the column we want to be at */
6172 coladvance(curwin->w_curswant);
6173
6174 if (upd_topline)
6175 update_topline(); /* make sure curwin->w_topline is valid */
6176
6177 return OK;
6178}
6179
6180/*
6181 * Stuff the last inserted text in the read buffer.
6182 * Last_insert actually is a copy of the redo buffer, so we
6183 * first have to remove the command.
6184 */
6185 int
6186stuff_inserted(c, count, no_esc)
6187 int c; /* Command character to be inserted */
6188 long count; /* Repeat this many times */
6189 int no_esc; /* Don't add an ESC at the end */
6190{
6191 char_u *esc_ptr;
6192 char_u *ptr;
6193 char_u *last_ptr;
6194 char_u last = NUL;
6195
6196 ptr = get_last_insert();
6197 if (ptr == NULL)
6198 {
6199 EMSG(_(e_noinstext));
6200 return FAIL;
6201 }
6202
6203 /* may want to stuff the command character, to start Insert mode */
6204 if (c != NUL)
6205 stuffcharReadbuff(c);
6206 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6207 *esc_ptr = NUL; /* remove the ESC */
6208
6209 /* when the last char is either "0" or "^" it will be quoted if no ESC
6210 * comes after it OR if it will inserted more than once and "ptr"
6211 * starts with ^D. -- Acevedo
6212 */
6213 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6214 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6215 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6216 {
6217 last = *last_ptr;
6218 *last_ptr = NUL;
6219 }
6220
6221 do
6222 {
6223 stuffReadbuff(ptr);
6224 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6225 if (last)
6226 stuffReadbuff((char_u *)(last == '0'
6227 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6228 : IF_EB("\026^", CTRL_V_STR "^")));
6229 }
6230 while (--count > 0);
6231
6232 if (last)
6233 *last_ptr = last;
6234
6235 if (esc_ptr != NULL)
6236 *esc_ptr = ESC; /* put the ESC back */
6237
6238 /* may want to stuff a trailing ESC, to get out of Insert mode */
6239 if (!no_esc)
6240 stuffcharReadbuff(ESC);
6241
6242 return OK;
6243}
6244
6245 char_u *
6246get_last_insert()
6247{
6248 if (last_insert == NULL)
6249 return NULL;
6250 return last_insert + last_insert_skip;
6251}
6252
6253/*
6254 * Get last inserted string, and remove trailing <Esc>.
6255 * Returns pointer to allocated memory (must be freed) or NULL.
6256 */
6257 char_u *
6258get_last_insert_save()
6259{
6260 char_u *s;
6261 int len;
6262
6263 if (last_insert == NULL)
6264 return NULL;
6265 s = vim_strsave(last_insert + last_insert_skip);
6266 if (s != NULL)
6267 {
6268 len = (int)STRLEN(s);
6269 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6270 s[len - 1] = NUL;
6271 }
6272 return s;
6273}
6274
6275/*
6276 * Check the word in front of the cursor for an abbreviation.
6277 * Called when the non-id character "c" has been entered.
6278 * When an abbreviation is recognized it is removed from the text and
6279 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6280 */
6281 static int
6282echeck_abbr(c)
6283 int c;
6284{
6285 /* Don't check for abbreviation in paste mode, when disabled and just
6286 * after moving around with cursor keys. */
6287 if (p_paste || no_abbr || arrow_used)
6288 return FALSE;
6289
6290 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6291 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6292}
6293
6294/*
6295 * replace-stack functions
6296 *
6297 * When replacing characters, the replaced characters are remembered for each
6298 * new character. This is used to re-insert the old text when backspacing.
6299 *
6300 * There is a NUL headed list of characters for each character that is
6301 * currently in the file after the insertion point. When BS is used, one NUL
6302 * headed list is put back for the deleted character.
6303 *
6304 * For a newline, there are two NUL headed lists. One contains the characters
6305 * that the NL replaced. The extra one stores the characters after the cursor
6306 * that were deleted (always white space).
6307 *
6308 * Replace_offset is normally 0, in which case replace_push will add a new
6309 * character at the end of the stack. If replace_offset is not 0, that many
6310 * characters will be left on the stack above the newly inserted character.
6311 */
6312
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006313static char_u *replace_stack = NULL;
6314static long replace_stack_nr = 0; /* next entry in replace stack */
6315static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316
6317 void
6318replace_push(c)
6319 int c; /* character that is replaced (NUL is none) */
6320{
6321 char_u *p;
6322
6323 if (replace_stack_nr < replace_offset) /* nothing to do */
6324 return;
6325 if (replace_stack_len <= replace_stack_nr)
6326 {
6327 replace_stack_len += 50;
6328 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6329 if (p == NULL) /* out of memory */
6330 {
6331 replace_stack_len -= 50;
6332 return;
6333 }
6334 if (replace_stack != NULL)
6335 {
6336 mch_memmove(p, replace_stack,
6337 (size_t)(replace_stack_nr * sizeof(char_u)));
6338 vim_free(replace_stack);
6339 }
6340 replace_stack = p;
6341 }
6342 p = replace_stack + replace_stack_nr - replace_offset;
6343 if (replace_offset)
6344 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6345 *p = c;
6346 ++replace_stack_nr;
6347}
6348
6349/*
6350 * call replace_push(c) with replace_offset set to the first NUL.
6351 */
6352 static void
6353replace_push_off(c)
6354 int c;
6355{
6356 char_u *p;
6357
6358 p = replace_stack + replace_stack_nr;
6359 for (replace_offset = 1; replace_offset < replace_stack_nr;
6360 ++replace_offset)
6361 if (*--p == NUL)
6362 break;
6363 replace_push(c);
6364 replace_offset = 0;
6365}
6366
6367/*
6368 * Pop one item from the replace stack.
6369 * return -1 if stack empty
6370 * return replaced character or NUL otherwise
6371 */
6372 static int
6373replace_pop()
6374{
6375 if (replace_stack_nr == 0)
6376 return -1;
6377 return (int)replace_stack[--replace_stack_nr];
6378}
6379
6380/*
6381 * Join the top two items on the replace stack. This removes to "off"'th NUL
6382 * encountered.
6383 */
6384 static void
6385replace_join(off)
6386 int off; /* offset for which NUL to remove */
6387{
6388 int i;
6389
6390 for (i = replace_stack_nr; --i >= 0; )
6391 if (replace_stack[i] == NUL && off-- <= 0)
6392 {
6393 --replace_stack_nr;
6394 mch_memmove(replace_stack + i, replace_stack + i + 1,
6395 (size_t)(replace_stack_nr - i));
6396 return;
6397 }
6398}
6399
6400/*
6401 * Pop bytes from the replace stack until a NUL is found, and insert them
6402 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6403 */
6404 static void
6405replace_pop_ins()
6406{
6407 int cc;
6408 int oldState = State;
6409
6410 State = NORMAL; /* don't want REPLACE here */
6411 while ((cc = replace_pop()) > 0)
6412 {
6413#ifdef FEAT_MBYTE
6414 mb_replace_pop_ins(cc);
6415#else
6416 ins_char(cc);
6417#endif
6418 dec_cursor();
6419 }
6420 State = oldState;
6421}
6422
6423#ifdef FEAT_MBYTE
6424/*
6425 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6426 * indicates a multi-byte char, pop the other bytes too.
6427 */
6428 static void
6429mb_replace_pop_ins(cc)
6430 int cc;
6431{
6432 int n;
6433 char_u buf[MB_MAXBYTES];
6434 int i;
6435 int c;
6436
6437 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6438 {
6439 buf[0] = cc;
6440 for (i = 1; i < n; ++i)
6441 buf[i] = replace_pop();
6442 ins_bytes_len(buf, n);
6443 }
6444 else
6445 ins_char(cc);
6446
6447 if (enc_utf8)
6448 /* Handle composing chars. */
6449 for (;;)
6450 {
6451 c = replace_pop();
6452 if (c == -1) /* stack empty */
6453 break;
6454 if ((n = MB_BYTE2LEN(c)) == 1)
6455 {
6456 /* Not a multi-byte char, put it back. */
6457 replace_push(c);
6458 break;
6459 }
6460 else
6461 {
6462 buf[0] = c;
6463 for (i = 1; i < n; ++i)
6464 buf[i] = replace_pop();
6465 if (utf_iscomposing(utf_ptr2char(buf)))
6466 ins_bytes_len(buf, n);
6467 else
6468 {
6469 /* Not a composing char, put it back. */
6470 for (i = n - 1; i >= 0; --i)
6471 replace_push(buf[i]);
6472 break;
6473 }
6474 }
6475 }
6476}
6477#endif
6478
6479/*
6480 * make the replace stack empty
6481 * (called when exiting replace mode)
6482 */
6483 static void
6484replace_flush()
6485{
6486 vim_free(replace_stack);
6487 replace_stack = NULL;
6488 replace_stack_len = 0;
6489 replace_stack_nr = 0;
6490}
6491
6492/*
6493 * Handle doing a BS for one character.
6494 * cc < 0: replace stack empty, just move cursor
6495 * cc == 0: character was inserted, delete it
6496 * cc > 0: character was replaced, put cc (first byte of original char) back
6497 * and check for more characters to be put back
6498 */
6499 static void
6500replace_do_bs()
6501{
6502 int cc;
6503#ifdef FEAT_VREPLACE
6504 int orig_len = 0;
6505 int ins_len;
6506 int orig_vcols = 0;
6507 colnr_T start_vcol;
6508 char_u *p;
6509 int i;
6510 int vcol;
6511#endif
6512
6513 cc = replace_pop();
6514 if (cc > 0)
6515 {
6516#ifdef FEAT_VREPLACE
6517 if (State & VREPLACE_FLAG)
6518 {
6519 /* Get the number of screen cells used by the character we are
6520 * going to delete. */
6521 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6522 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6523 }
6524#endif
6525#ifdef FEAT_MBYTE
6526 if (has_mbyte)
6527 {
6528 del_char(FALSE);
6529# ifdef FEAT_VREPLACE
6530 if (State & VREPLACE_FLAG)
6531 orig_len = STRLEN(ml_get_cursor());
6532# endif
6533 replace_push(cc);
6534 }
6535 else
6536#endif
6537 {
6538 pchar_cursor(cc);
6539#ifdef FEAT_VREPLACE
6540 if (State & VREPLACE_FLAG)
6541 orig_len = STRLEN(ml_get_cursor()) - 1;
6542#endif
6543 }
6544 replace_pop_ins();
6545
6546#ifdef FEAT_VREPLACE
6547 if (State & VREPLACE_FLAG)
6548 {
6549 /* Get the number of screen cells used by the inserted characters */
6550 p = ml_get_cursor();
6551 ins_len = STRLEN(p) - orig_len;
6552 vcol = start_vcol;
6553 for (i = 0; i < ins_len; ++i)
6554 {
6555 vcol += chartabsize(p + i, vcol);
6556#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006557 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558#endif
6559 }
6560 vcol -= start_vcol;
6561
6562 /* Delete spaces that were inserted after the cursor to keep the
6563 * text aligned. */
6564 curwin->w_cursor.col += ins_len;
6565 while (vcol > orig_vcols && gchar_cursor() == ' ')
6566 {
6567 del_char(FALSE);
6568 ++orig_vcols;
6569 }
6570 curwin->w_cursor.col -= ins_len;
6571 }
6572#endif
6573
6574 /* mark the buffer as changed and prepare for displaying */
6575 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6576 }
6577 else if (cc == 0)
6578 (void)del_char(FALSE);
6579}
6580
6581#ifdef FEAT_CINDENT
6582/*
6583 * Return TRUE if C-indenting is on.
6584 */
6585 static int
6586cindent_on()
6587{
6588 return (!p_paste && (curbuf->b_p_cin
6589# ifdef FEAT_EVAL
6590 || *curbuf->b_p_inde != NUL
6591# endif
6592 ));
6593}
6594#endif
6595
6596#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6597/*
6598 * Re-indent the current line, based on the current contents of it and the
6599 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6600 * confused what all the part that handles Control-T is doing that I'm not.
6601 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6602 */
6603
6604 void
6605fixthisline(get_the_indent)
6606 int (*get_the_indent) __ARGS((void));
6607{
6608 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6609 if (linewhite(curwin->w_cursor.lnum))
6610 did_ai = TRUE; /* delete the indent if the line stays empty */
6611}
6612
6613 void
6614fix_indent()
6615{
6616 if (p_paste)
6617 return;
6618# ifdef FEAT_LISP
6619 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6620 fixthisline(get_lisp_indent);
6621# endif
6622# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6623 else
6624# endif
6625# ifdef FEAT_CINDENT
6626 if (cindent_on())
6627 do_c_expr_indent();
6628# endif
6629}
6630
6631#endif
6632
6633#ifdef FEAT_CINDENT
6634/*
6635 * return TRUE if 'cinkeys' contains the key "keytyped",
6636 * when == '*': Only if key is preceded with '*' (indent before insert)
6637 * when == '!': Only if key is prededed with '!' (don't insert)
6638 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6639 *
6640 * "keytyped" can have a few special values:
6641 * KEY_OPEN_FORW
6642 * KEY_OPEN_BACK
6643 * KEY_COMPLETE just finished completion.
6644 *
6645 * If line_is_empty is TRUE accept keys with '0' before them.
6646 */
6647 int
6648in_cinkeys(keytyped, when, line_is_empty)
6649 int keytyped;
6650 int when;
6651 int line_is_empty;
6652{
6653 char_u *look;
6654 int try_match;
6655 int try_match_word;
6656 char_u *p;
6657 char_u *line;
6658 int icase;
6659 int i;
6660
6661#ifdef FEAT_EVAL
6662 if (*curbuf->b_p_inde != NUL)
6663 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6664 else
6665#endif
6666 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6667 while (*look)
6668 {
6669 /*
6670 * Find out if we want to try a match with this key, depending on
6671 * 'when' and a '*' or '!' before the key.
6672 */
6673 switch (when)
6674 {
6675 case '*': try_match = (*look == '*'); break;
6676 case '!': try_match = (*look == '!'); break;
6677 default: try_match = (*look != '*'); break;
6678 }
6679 if (*look == '*' || *look == '!')
6680 ++look;
6681
6682 /*
6683 * If there is a '0', only accept a match if the line is empty.
6684 * But may still match when typing last char of a word.
6685 */
6686 if (*look == '0')
6687 {
6688 try_match_word = try_match;
6689 if (!line_is_empty)
6690 try_match = FALSE;
6691 ++look;
6692 }
6693 else
6694 try_match_word = FALSE;
6695
6696 /*
6697 * does it look like a control character?
6698 */
6699 if (*look == '^'
6700#ifdef EBCDIC
6701 && (Ctrl_chr(look[1]) != 0)
6702#else
6703 && look[1] >= '?' && look[1] <= '_'
6704#endif
6705 )
6706 {
6707 if (try_match && keytyped == Ctrl_chr(look[1]))
6708 return TRUE;
6709 look += 2;
6710 }
6711 /*
6712 * 'o' means "o" command, open forward.
6713 * 'O' means "O" command, open backward.
6714 */
6715 else if (*look == 'o')
6716 {
6717 if (try_match && keytyped == KEY_OPEN_FORW)
6718 return TRUE;
6719 ++look;
6720 }
6721 else if (*look == 'O')
6722 {
6723 if (try_match && keytyped == KEY_OPEN_BACK)
6724 return TRUE;
6725 ++look;
6726 }
6727
6728 /*
6729 * 'e' means to check for "else" at start of line and just before the
6730 * cursor.
6731 */
6732 else if (*look == 'e')
6733 {
6734 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6735 {
6736 p = ml_get_curline();
6737 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6738 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6739 return TRUE;
6740 }
6741 ++look;
6742 }
6743
6744 /*
6745 * ':' only causes an indent if it is at the end of a label or case
6746 * statement, or when it was before typing the ':' (to fix
6747 * class::method for C++).
6748 */
6749 else if (*look == ':')
6750 {
6751 if (try_match && keytyped == ':')
6752 {
6753 p = ml_get_curline();
6754 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6755 return TRUE;
6756 if (curwin->w_cursor.col > 2
6757 && p[curwin->w_cursor.col - 1] == ':'
6758 && p[curwin->w_cursor.col - 2] == ':')
6759 {
6760 p[curwin->w_cursor.col - 1] = ' ';
6761 i = (cin_iscase(p) || cin_isscopedecl(p)
6762 || cin_islabel(30));
6763 p = ml_get_curline();
6764 p[curwin->w_cursor.col - 1] = ':';
6765 if (i)
6766 return TRUE;
6767 }
6768 }
6769 ++look;
6770 }
6771
6772
6773 /*
6774 * Is it a key in <>, maybe?
6775 */
6776 else if (*look == '<')
6777 {
6778 if (try_match)
6779 {
6780 /*
6781 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6782 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6783 * >, *, : and ! keys if they really really want to.
6784 */
6785 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6786 && keytyped == look[1])
6787 return TRUE;
6788
6789 if (keytyped == get_special_key_code(look + 1))
6790 return TRUE;
6791 }
6792 while (*look && *look != '>')
6793 look++;
6794 while (*look == '>')
6795 look++;
6796 }
6797
6798 /*
6799 * Is it a word: "=word"?
6800 */
6801 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6802 {
6803 ++look;
6804 if (*look == '~')
6805 {
6806 icase = TRUE;
6807 ++look;
6808 }
6809 else
6810 icase = FALSE;
6811 p = vim_strchr(look, ',');
6812 if (p == NULL)
6813 p = look + STRLEN(look);
6814 if ((try_match || try_match_word)
6815 && curwin->w_cursor.col >= (colnr_T)(p - look))
6816 {
6817 int match = FALSE;
6818
6819#ifdef FEAT_INS_EXPAND
6820 if (keytyped == KEY_COMPLETE)
6821 {
6822 char_u *s;
6823
6824 /* Just completed a word, check if it starts with "look".
6825 * search back for the start of a word. */
6826 line = ml_get_curline();
6827# ifdef FEAT_MBYTE
6828 if (has_mbyte)
6829 {
6830 char_u *n;
6831
6832 for (s = line + curwin->w_cursor.col; s > line; s = n)
6833 {
6834 n = mb_prevptr(line, s);
6835 if (!vim_iswordp(n))
6836 break;
6837 }
6838 }
6839 else
6840# endif
6841 for (s = line + curwin->w_cursor.col; s > line; --s)
6842 if (!vim_iswordc(s[-1]))
6843 break;
6844 if (s + (p - look) <= line + curwin->w_cursor.col
6845 && (icase
6846 ? MB_STRNICMP(s, look, p - look)
6847 : STRNCMP(s, look, p - look)) == 0)
6848 match = TRUE;
6849 }
6850 else
6851#endif
6852 /* TODO: multi-byte */
6853 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6854 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6855 {
6856 line = ml_get_cursor();
6857 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6858 || !vim_iswordc(line[-(p - look) - 1]))
6859 && (icase
6860 ? MB_STRNICMP(line - (p - look), look, p - look)
6861 : STRNCMP(line - (p - look), look, p - look))
6862 == 0)
6863 match = TRUE;
6864 }
6865 if (match && try_match_word && !try_match)
6866 {
6867 /* "0=word": Check if there are only blanks before the
6868 * word. */
6869 line = ml_get_curline();
6870 if ((int)(skipwhite(line) - line) !=
6871 (int)(curwin->w_cursor.col - (p - look)))
6872 match = FALSE;
6873 }
6874 if (match)
6875 return TRUE;
6876 }
6877 look = p;
6878 }
6879
6880 /*
6881 * ok, it's a boring generic character.
6882 */
6883 else
6884 {
6885 if (try_match && *look == keytyped)
6886 return TRUE;
6887 ++look;
6888 }
6889
6890 /*
6891 * Skip over ", ".
6892 */
6893 look = skip_to_option_part(look);
6894 }
6895 return FALSE;
6896}
6897#endif /* FEAT_CINDENT */
6898
6899#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6900/*
6901 * Map Hebrew keyboard when in hkmap mode.
6902 */
6903 int
6904hkmap(c)
6905 int c;
6906{
6907 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6908 {
6909 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6910 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6911 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6912 static char_u map[26] =
6913 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6914 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6915 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6916 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6917 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6918 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6919 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6920 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6921 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6922
6923 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6924 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6925 /* '-1'='sofit' */
6926 else if (c == 'x')
6927 return 'X';
6928 else if (c == 'q')
6929 return '\''; /* {geresh}={'} */
6930 else if (c == 246)
6931 return ' '; /* \"o --> ' ' for a german keyboard */
6932 else if (c == 228)
6933 return ' '; /* \"a --> ' ' -- / -- */
6934 else if (c == 252)
6935 return ' '; /* \"u --> ' ' -- / -- */
6936#ifdef EBCDIC
6937 else if (islower(c))
6938#else
6939 /* NOTE: islower() does not do the right thing for us on Linux so we
6940 * do this the same was as 5.7 and previous, so it works correctly on
6941 * all systems. Specifically, the e.g. Delete and Arrow keys are
6942 * munged and won't work if e.g. searching for Hebrew text.
6943 */
6944 else if (c >= 'a' && c <= 'z')
6945#endif
6946 return (int)(map[CharOrdLow(c)] + p_aleph);
6947 else
6948 return c;
6949 }
6950 else
6951 {
6952 switch (c)
6953 {
6954 case '`': return ';';
6955 case '/': return '.';
6956 case '\'': return ',';
6957 case 'q': return '/';
6958 case 'w': return '\'';
6959
6960 /* Hebrew letters - set offset from 'a' */
6961 case ',': c = '{'; break;
6962 case '.': c = 'v'; break;
6963 case ';': c = 't'; break;
6964 default: {
6965 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6966
6967#ifdef EBCDIC
6968 /* see note about islower() above */
6969 if (!islower(c))
6970#else
6971 if (c < 'a' || c > 'z')
6972#endif
6973 return c;
6974 c = str[CharOrdLow(c)];
6975 break;
6976 }
6977 }
6978
6979 return (int)(CharOrdLow(c) + p_aleph);
6980 }
6981}
6982#endif
6983
6984 static void
6985ins_reg()
6986{
6987 int need_redraw = FALSE;
6988 int regname;
6989 int literally = 0;
6990
6991 /*
6992 * If we are going to wait for a character, show a '"'.
6993 */
6994 pc_status = PC_STATUS_UNSET;
6995 if (redrawing() && !char_avail())
6996 {
6997 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00006998 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
7000 edit_putchar('"', TRUE);
7001#ifdef FEAT_CMDL_INFO
7002 add_to_showcmd_c(Ctrl_R);
7003#endif
7004 }
7005
7006#ifdef USE_ON_FLY_SCROLL
7007 dont_scroll = TRUE; /* disallow scrolling here */
7008#endif
7009
7010 /*
7011 * Don't map the register name. This also prevents the mode message to be
7012 * deleted when ESC is hit.
7013 */
7014 ++no_mapping;
7015 regname = safe_vgetc();
7016#ifdef FEAT_LANGMAP
7017 LANGMAP_ADJUST(regname, TRUE);
7018#endif
7019 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7020 {
7021 /* Get a third key for literal register insertion */
7022 literally = regname;
7023#ifdef FEAT_CMDL_INFO
7024 add_to_showcmd_c(literally);
7025#endif
7026 regname = safe_vgetc();
7027#ifdef FEAT_LANGMAP
7028 LANGMAP_ADJUST(regname, TRUE);
7029#endif
7030 }
7031 --no_mapping;
7032
7033#ifdef FEAT_EVAL
7034 /*
7035 * Don't call u_sync() while getting the expression,
7036 * evaluating it or giving an error message for it!
7037 */
7038 ++no_u_sync;
7039 if (regname == '=')
7040 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007041# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007045# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 /* Restore the Input Method. */
7047 if (im_on)
7048 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007051 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7052 {
7053 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 else
7057 {
7058#endif
7059 if (literally == Ctrl_O || literally == Ctrl_P)
7060 {
7061 /* Append the command to the redo buffer. */
7062 AppendCharToRedobuff(Ctrl_R);
7063 AppendCharToRedobuff(literally);
7064 AppendCharToRedobuff(regname);
7065
7066 do_put(regname, BACKWARD, 1L,
7067 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7068 }
7069 else if (insert_reg(regname, literally) == FAIL)
7070 {
7071 vim_beep();
7072 need_redraw = TRUE; /* remove the '"' */
7073 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007074 else if (stop_insert_mode)
7075 /* When the '=' register was used and a function was invoked that
7076 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7077 * insert anything, need to remove the '"' */
7078 need_redraw = TRUE;
7079
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080#ifdef FEAT_EVAL
7081 }
7082 --no_u_sync;
7083#endif
7084#ifdef FEAT_CMDL_INFO
7085 clear_showcmd();
7086#endif
7087
7088 /* If the inserted register is empty, we need to remove the '"' */
7089 if (need_redraw || stuff_empty())
7090 edit_unputchar();
7091}
7092
7093/*
7094 * CTRL-G commands in Insert mode.
7095 */
7096 static void
7097ins_ctrl_g()
7098{
7099 int c;
7100
7101#ifdef FEAT_INS_EXPAND
7102 /* Right after CTRL-X the cursor will be after the ruler. */
7103 setcursor();
7104#endif
7105
7106 /*
7107 * Don't map the second key. This also prevents the mode message to be
7108 * deleted when ESC is hit.
7109 */
7110 ++no_mapping;
7111 c = safe_vgetc();
7112 --no_mapping;
7113 switch (c)
7114 {
7115 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7116 case K_UP:
7117 case Ctrl_K:
7118 case 'k': ins_up(TRUE);
7119 break;
7120
7121 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7122 case K_DOWN:
7123 case Ctrl_J:
7124 case 'j': ins_down(TRUE);
7125 break;
7126
7127 /* CTRL-G u: start new undoable edit */
7128 case 'u': u_sync();
7129 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007130
7131 /* Need to reset Insstart, esp. because a BS that joins
7132 * aline to the previous one must save for undo. */
7133 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 break;
7135
7136 /* Unknown CTRL-G command, reserved for future expansion. */
7137 default: vim_beep();
7138 }
7139}
7140
7141/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007142 * CTRL-^ in Insert mode.
7143 */
7144 static void
7145ins_ctrl_hat()
7146{
7147 if (map_to_exists_mode((char_u *)"", LANGMAP))
7148 {
7149 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7150 if (State & LANGMAP)
7151 {
7152 curbuf->b_p_iminsert = B_IMODE_NONE;
7153 State &= ~LANGMAP;
7154 }
7155 else
7156 {
7157 curbuf->b_p_iminsert = B_IMODE_LMAP;
7158 State |= LANGMAP;
7159#ifdef USE_IM_CONTROL
7160 im_set_active(FALSE);
7161#endif
7162 }
7163 }
7164#ifdef USE_IM_CONTROL
7165 else
7166 {
7167 /* There are no ":lmap" mappings, toggle IM */
7168 if (im_get_status())
7169 {
7170 curbuf->b_p_iminsert = B_IMODE_NONE;
7171 im_set_active(FALSE);
7172 }
7173 else
7174 {
7175 curbuf->b_p_iminsert = B_IMODE_IM;
7176 State &= ~LANGMAP;
7177 im_set_active(TRUE);
7178 }
7179 }
7180#endif
7181 set_iminsert_global();
7182 showmode();
7183#ifdef FEAT_GUI
7184 /* may show different cursor shape or color */
7185 if (gui.in_use)
7186 gui_update_cursor(TRUE, FALSE);
7187#endif
7188#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7189 /* Show/unshow value of 'keymap' in status lines. */
7190 status_redraw_curbuf();
7191#endif
7192}
7193
7194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 * Handle ESC in insert mode.
7196 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7197 * insert.
7198 */
7199 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007200ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 long *count;
7202 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007203 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204{
7205 int temp;
7206 static int disabled_redraw = FALSE;
7207
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007208#ifdef FEAT_SYN_HL
7209 check_spell_redraw();
7210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211#if defined(FEAT_HANGULIN)
7212# if defined(ESC_CHG_TO_ENG_MODE)
7213 hangul_input_state_set(0);
7214# endif
7215 if (composing_hangul)
7216 {
7217 push_raw_key(composing_hangul_buffer, 2);
7218 composing_hangul = 0;
7219 }
7220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221
7222 temp = curwin->w_cursor.col;
7223 if (disabled_redraw)
7224 {
7225 --RedrawingDisabled;
7226 disabled_redraw = FALSE;
7227 }
7228 if (!arrow_used)
7229 {
7230 /*
7231 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007232 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7233 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 */
7235 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007236 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
7238 /*
7239 * Repeating insert may take a long time. Check for
7240 * interrupt now and then.
7241 */
7242 if (*count > 0)
7243 {
7244 line_breakcheck();
7245 if (got_int)
7246 *count = 0;
7247 }
7248
7249 if (--*count > 0) /* repeat what was typed */
7250 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007251 /* Vi repeats the insert without replacing characters. */
7252 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7253 State &= ~REPLACE_FLAG;
7254
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 (void)start_redo_ins();
7256 if (cmdchar == 'r' || cmdchar == 'v')
7257 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7258 ++RedrawingDisabled;
7259 disabled_redraw = TRUE;
7260 return FALSE; /* repeat the insert */
7261 }
7262 stop_insert(&curwin->w_cursor, TRUE);
7263 undisplay_dollar();
7264 }
7265
7266 /* When an autoindent was removed, curswant stays after the
7267 * indent */
7268 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7269 curwin->w_set_curswant = TRUE;
7270
7271 /* Remember the last Insert position in the '^ mark. */
7272 if (!cmdmod.keepjumps)
7273 curbuf->b_last_insert = curwin->w_cursor;
7274
7275 /*
7276 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007277 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007279 if (!nomove
7280 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281#ifdef FEAT_VIRTUALEDIT
7282 || curwin->w_cursor.coladd > 0
7283#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007284 )
7285 && (restart_edit == NUL
7286 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007288 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007290 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291#ifdef FEAT_RIGHTLEFT
7292 && !revins_on
7293#endif
7294 )
7295 {
7296#ifdef FEAT_VIRTUALEDIT
7297 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7298 {
7299 oneleft();
7300 if (restart_edit != NUL)
7301 ++curwin->w_cursor.coladd;
7302 }
7303 else
7304#endif
7305 {
7306 --curwin->w_cursor.col;
7307#ifdef FEAT_MBYTE
7308 /* Correct cursor for multi-byte character. */
7309 if (has_mbyte)
7310 mb_adjust_cursor();
7311#endif
7312 }
7313 }
7314
7315#ifdef USE_IM_CONTROL
7316 /* Disable IM to allow typing English directly for Normal mode commands.
7317 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7318 * well). */
7319 if (!(State & LANGMAP))
7320 im_save_status(&curbuf->b_p_iminsert);
7321 im_set_active(FALSE);
7322#endif
7323
7324 State = NORMAL;
7325 /* need to position cursor again (e.g. when on a TAB ) */
7326 changed_cline_bef_curs();
7327
7328#ifdef FEAT_MOUSE
7329 setmouse();
7330#endif
7331#ifdef CURSOR_SHAPE
7332 ui_cursor_shape(); /* may show different cursor shape */
7333#endif
7334
7335 /*
7336 * When recording or for CTRL-O, need to display the new mode.
7337 * Otherwise remove the mode message.
7338 */
7339 if (Recording || restart_edit != NUL)
7340 showmode();
7341 else if (p_smd)
7342 MSG("");
7343
7344 return TRUE; /* exit Insert mode */
7345}
7346
7347#ifdef FEAT_RIGHTLEFT
7348/*
7349 * Toggle language: hkmap and revins_on.
7350 * Move to end of reverse inserted text.
7351 */
7352 static void
7353ins_ctrl_()
7354{
7355 if (revins_on && revins_chars && revins_scol >= 0)
7356 {
7357 while (gchar_cursor() != NUL && revins_chars--)
7358 ++curwin->w_cursor.col;
7359 }
7360 p_ri = !p_ri;
7361 revins_on = (State == INSERT && p_ri);
7362 if (revins_on)
7363 {
7364 revins_scol = curwin->w_cursor.col;
7365 revins_legal++;
7366 revins_chars = 0;
7367 undisplay_dollar();
7368 }
7369 else
7370 revins_scol = -1;
7371#ifdef FEAT_FKMAP
7372 if (p_altkeymap)
7373 {
7374 /*
7375 * to be consistent also for redo command, using '.'
7376 * set arrow_used to true and stop it - causing to redo
7377 * characters entered in one mode (normal/reverse insert).
7378 */
7379 arrow_used = TRUE;
7380 (void)stop_arrow();
7381 p_fkmap = curwin->w_p_rl ^ p_ri;
7382 if (p_fkmap && p_ri)
7383 State = INSERT;
7384 }
7385 else
7386#endif
7387 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7388 showmode();
7389}
7390#endif
7391
7392#ifdef FEAT_VISUAL
7393/*
7394 * If 'keymodel' contains "startsel", may start selection.
7395 * Returns TRUE when a CTRL-O and other keys stuffed.
7396 */
7397 static int
7398ins_start_select(c)
7399 int c;
7400{
7401 if (km_startsel)
7402 switch (c)
7403 {
7404 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 case K_PAGEUP:
7407 case K_KPAGEUP:
7408 case K_PAGEDOWN:
7409 case K_KPAGEDOWN:
7410# ifdef MACOS
7411 case K_LEFT:
7412 case K_RIGHT:
7413 case K_UP:
7414 case K_DOWN:
7415 case K_END:
7416 case K_HOME:
7417# endif
7418 if (!(mod_mask & MOD_MASK_SHIFT))
7419 break;
7420 /* FALLTHROUGH */
7421 case K_S_LEFT:
7422 case K_S_RIGHT:
7423 case K_S_UP:
7424 case K_S_DOWN:
7425 case K_S_END:
7426 case K_S_HOME:
7427 /* Start selection right away, the cursor can move with
7428 * CTRL-O when beyond the end of the line. */
7429 start_selection();
7430
7431 /* Execute the key in (insert) Select mode. */
7432 stuffcharReadbuff(Ctrl_O);
7433 if (mod_mask)
7434 {
7435 char_u buf[4];
7436
7437 buf[0] = K_SPECIAL;
7438 buf[1] = KS_MODIFIER;
7439 buf[2] = mod_mask;
7440 buf[3] = NUL;
7441 stuffReadbuff(buf);
7442 }
7443 stuffcharReadbuff(c);
7444 return TRUE;
7445 }
7446 return FALSE;
7447}
7448#endif
7449
7450/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007451 * <Insert> key in Insert mode: toggle insert/remplace mode.
7452 */
7453 static void
7454ins_insert(replaceState)
7455 int replaceState;
7456{
7457#ifdef FEAT_FKMAP
7458 if (p_fkmap && p_ri)
7459 {
7460 beep_flush();
7461 EMSG(farsi_text_3); /* encoded in Farsi */
7462 return;
7463 }
7464#endif
7465
7466#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007467# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007468 set_vim_var_string(VV_INSERTMODE,
7469 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007470# ifdef FEAT_VREPLACE
7471 replaceState == VREPLACE ? "v" :
7472# endif
7473 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007474# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007475 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7476#endif
7477 if (State & REPLACE_FLAG)
7478 State = INSERT | (State & LANGMAP);
7479 else
7480 State = replaceState | (State & LANGMAP);
7481 AppendCharToRedobuff(K_INS);
7482 showmode();
7483#ifdef CURSOR_SHAPE
7484 ui_cursor_shape(); /* may show different cursor shape */
7485#endif
7486}
7487
7488/*
7489 * Pressed CTRL-O in Insert mode.
7490 */
7491 static void
7492ins_ctrl_o()
7493{
7494#ifdef FEAT_VREPLACE
7495 if (State & VREPLACE_FLAG)
7496 restart_edit = 'V';
7497 else
7498#endif
7499 if (State & REPLACE_FLAG)
7500 restart_edit = 'R';
7501 else
7502 restart_edit = 'I';
7503#ifdef FEAT_VIRTUALEDIT
7504 if (virtual_active())
7505 ins_at_eol = FALSE; /* cursor always keeps its column */
7506 else
7507#endif
7508 ins_at_eol = (gchar_cursor() == NUL);
7509}
7510
7511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 * If the cursor is on an indent, ^T/^D insert/delete one
7513 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7514 * Always round the indent to 'shiftwith', this is compatible
7515 * with vi. But vi only supports ^T and ^D after an
7516 * autoindent, we support it everywhere.
7517 */
7518 static void
7519ins_shift(c, lastc)
7520 int c;
7521 int lastc;
7522{
7523 if (stop_arrow() == FAIL)
7524 return;
7525 AppendCharToRedobuff(c);
7526
7527 /*
7528 * 0^D and ^^D: remove all indent.
7529 */
7530 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7531 {
7532 --curwin->w_cursor.col;
7533 (void)del_char(FALSE); /* delete the '^' or '0' */
7534 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7535 if (State & REPLACE_FLAG)
7536 replace_pop_ins();
7537 if (lastc == '^')
7538 old_indent = get_indent(); /* remember curr. indent */
7539 change_indent(INDENT_SET, 0, TRUE, 0);
7540 }
7541 else
7542 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7543
7544 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7545 did_ai = FALSE;
7546#ifdef FEAT_SMARTINDENT
7547 did_si = FALSE;
7548 can_si = FALSE;
7549 can_si_back = FALSE;
7550#endif
7551#ifdef FEAT_CINDENT
7552 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7553#endif
7554}
7555
7556 static void
7557ins_del()
7558{
7559 int temp;
7560
7561 if (stop_arrow() == FAIL)
7562 return;
7563 if (gchar_cursor() == NUL) /* delete newline */
7564 {
7565 temp = curwin->w_cursor.col;
7566 if (!can_bs(BS_EOL) /* only if "eol" included */
7567 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7568 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7569 || do_join(FALSE) == FAIL)
7570 vim_beep();
7571 else
7572 curwin->w_cursor.col = temp;
7573 }
7574 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7575 vim_beep();
7576 did_ai = FALSE;
7577#ifdef FEAT_SMARTINDENT
7578 did_si = FALSE;
7579 can_si = FALSE;
7580 can_si_back = FALSE;
7581#endif
7582 AppendCharToRedobuff(K_DEL);
7583}
7584
7585/*
7586 * Handle Backspace, delete-word and delete-line in Insert mode.
7587 * Return TRUE when backspace was actually used.
7588 */
7589 static int
7590ins_bs(c, mode, inserted_space_p)
7591 int c;
7592 int mode;
7593 int *inserted_space_p;
7594{
7595 linenr_T lnum;
7596 int cc;
7597 int temp = 0; /* init for GCC */
7598 colnr_T mincol;
7599 int did_backspace = FALSE;
7600 int in_indent;
7601 int oldState;
7602#ifdef FEAT_MBYTE
7603 int p1, p2;
7604#endif
7605
7606 /*
7607 * can't delete anything in an empty file
7608 * can't backup past first character in buffer
7609 * can't backup past starting point unless 'backspace' > 1
7610 * can backup to a previous line if 'backspace' == 0
7611 */
7612 if ( bufempty()
7613 || (
7614#ifdef FEAT_RIGHTLEFT
7615 !revins_on &&
7616#endif
7617 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7618 || (!can_bs(BS_START)
7619 && (arrow_used
7620 || (curwin->w_cursor.lnum == Insstart.lnum
7621 && curwin->w_cursor.col <= Insstart.col)))
7622 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7623 && curwin->w_cursor.col <= ai_col)
7624 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7625 {
7626 vim_beep();
7627 return FALSE;
7628 }
7629
7630 if (stop_arrow() == FAIL)
7631 return FALSE;
7632 in_indent = inindent(0);
7633#ifdef FEAT_CINDENT
7634 if (in_indent)
7635 can_cindent = FALSE;
7636#endif
7637#ifdef FEAT_COMMENTS
7638 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7639#endif
7640#ifdef FEAT_RIGHTLEFT
7641 if (revins_on) /* put cursor after last inserted char */
7642 inc_cursor();
7643#endif
7644
7645#ifdef FEAT_VIRTUALEDIT
7646 /* Virtualedit:
7647 * BACKSPACE_CHAR eats a virtual space
7648 * BACKSPACE_WORD eats all coladd
7649 * BACKSPACE_LINE eats all coladd and keeps going
7650 */
7651 if (curwin->w_cursor.coladd > 0)
7652 {
7653 if (mode == BACKSPACE_CHAR)
7654 {
7655 --curwin->w_cursor.coladd;
7656 return TRUE;
7657 }
7658 if (mode == BACKSPACE_WORD)
7659 {
7660 curwin->w_cursor.coladd = 0;
7661 return TRUE;
7662 }
7663 curwin->w_cursor.coladd = 0;
7664 }
7665#endif
7666
7667 /*
7668 * delete newline!
7669 */
7670 if (curwin->w_cursor.col == 0)
7671 {
7672 lnum = Insstart.lnum;
7673 if (curwin->w_cursor.lnum == Insstart.lnum
7674#ifdef FEAT_RIGHTLEFT
7675 || revins_on
7676#endif
7677 )
7678 {
7679 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7680 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7681 return FALSE;
7682 --Insstart.lnum;
7683 Insstart.col = MAXCOL;
7684 }
7685 /*
7686 * In replace mode:
7687 * cc < 0: NL was inserted, delete it
7688 * cc >= 0: NL was replaced, put original characters back
7689 */
7690 cc = -1;
7691 if (State & REPLACE_FLAG)
7692 cc = replace_pop(); /* returns -1 if NL was inserted */
7693 /*
7694 * In replace mode, in the line we started replacing, we only move the
7695 * cursor.
7696 */
7697 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7698 {
7699 dec_cursor();
7700 }
7701 else
7702 {
7703#ifdef FEAT_VREPLACE
7704 if (!(State & VREPLACE_FLAG)
7705 || curwin->w_cursor.lnum > orig_line_count)
7706#endif
7707 {
7708 temp = gchar_cursor(); /* remember current char */
7709 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007710
7711 /* When "aw" is in 'formatoptions' we must delete the space at
7712 * the end of the line, otherwise the line will be broken
7713 * again when auto-formatting. */
7714 if (has_format_option(FO_AUTO)
7715 && has_format_option(FO_WHITE_PAR))
7716 {
7717 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7718 TRUE);
7719 int len;
7720
7721 len = STRLEN(ptr);
7722 if (len > 0 && ptr[len - 1] == ' ')
7723 ptr[len - 1] = NUL;
7724 }
7725
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 (void)do_join(FALSE);
7727 if (temp == NUL && gchar_cursor() != NUL)
7728 inc_cursor();
7729 }
7730#ifdef FEAT_VREPLACE
7731 else
7732 dec_cursor();
7733#endif
7734
7735 /*
7736 * In REPLACE mode we have to put back the text that was replaced
7737 * by the NL. On the replace stack is first a NUL-terminated
7738 * sequence of characters that were deleted and then the
7739 * characters that NL replaced.
7740 */
7741 if (State & REPLACE_FLAG)
7742 {
7743 /*
7744 * Do the next ins_char() in NORMAL state, to
7745 * prevent ins_char() from replacing characters and
7746 * avoiding showmatch().
7747 */
7748 oldState = State;
7749 State = NORMAL;
7750 /*
7751 * restore characters (blanks) deleted after cursor
7752 */
7753 while (cc > 0)
7754 {
7755 temp = curwin->w_cursor.col;
7756#ifdef FEAT_MBYTE
7757 mb_replace_pop_ins(cc);
7758#else
7759 ins_char(cc);
7760#endif
7761 curwin->w_cursor.col = temp;
7762 cc = replace_pop();
7763 }
7764 /* restore the characters that NL replaced */
7765 replace_pop_ins();
7766 State = oldState;
7767 }
7768 }
7769 did_ai = FALSE;
7770 }
7771 else
7772 {
7773 /*
7774 * Delete character(s) before the cursor.
7775 */
7776#ifdef FEAT_RIGHTLEFT
7777 if (revins_on) /* put cursor on last inserted char */
7778 dec_cursor();
7779#endif
7780 mincol = 0;
7781 /* keep indent */
7782 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7783#ifdef FEAT_RIGHTLEFT
7784 && !revins_on
7785#endif
7786 )
7787 {
7788 temp = curwin->w_cursor.col;
7789 beginline(BL_WHITE);
7790 if (curwin->w_cursor.col < (colnr_T)temp)
7791 mincol = curwin->w_cursor.col;
7792 curwin->w_cursor.col = temp;
7793 }
7794
7795 /*
7796 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7797 */
7798 if ( mode == BACKSPACE_CHAR
7799 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007800 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 && (*(ml_get_cursor() - 1) == TAB
7802 || (*(ml_get_cursor() - 1) == ' '
7803 && (!*inserted_space_p
7804 || arrow_used))))))
7805 {
7806 int ts;
7807 colnr_T vcol;
7808 colnr_T want_vcol;
7809 int extra = 0;
7810
7811 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007812 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 ts = curbuf->b_p_sw;
7814 else
7815 ts = curbuf->b_p_sts;
7816 /* Compute the virtual column where we want to be. Since
7817 * 'showbreak' may get in the way, need to get the last column of
7818 * the previous character. */
7819 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7820 dec_cursor();
7821 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7822 inc_cursor();
7823 want_vcol = (want_vcol / ts) * ts;
7824
7825 /* delete characters until we are at or before want_vcol */
7826 while (vcol > want_vcol
7827 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7828 {
7829 dec_cursor();
7830 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7831 if (State & REPLACE_FLAG)
7832 {
7833 /* Don't delete characters before the insert point when in
7834 * Replace mode */
7835 if (curwin->w_cursor.lnum != Insstart.lnum
7836 || curwin->w_cursor.col >= Insstart.col)
7837 {
7838#if 0 /* what was this for? It causes problems when sw != ts. */
7839 if (State == REPLACE && (int)vcol < want_vcol)
7840 {
7841 (void)del_char(FALSE);
7842 extra = 2; /* don't pop too much */
7843 }
7844 else
7845#endif
7846 replace_do_bs();
7847 }
7848 }
7849 else
7850 (void)del_char(FALSE);
7851 }
7852
7853 /* insert extra spaces until we are at want_vcol */
7854 while (vcol < want_vcol)
7855 {
7856 /* Remember the first char we inserted */
7857 if (curwin->w_cursor.lnum == Insstart.lnum
7858 && curwin->w_cursor.col < Insstart.col)
7859 Insstart.col = curwin->w_cursor.col;
7860
7861#ifdef FEAT_VREPLACE
7862 if (State & VREPLACE_FLAG)
7863 ins_char(' ');
7864 else
7865#endif
7866 {
7867 ins_str((char_u *)" ");
7868 if ((State & REPLACE_FLAG) && extra <= 1)
7869 {
7870 if (extra)
7871 replace_push_off(NUL);
7872 else
7873 replace_push(NUL);
7874 }
7875 if (extra == 2)
7876 extra = 1;
7877 }
7878 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7879 }
7880 }
7881
7882 /*
7883 * Delete upto starting point, start of line or previous word.
7884 */
7885 else do
7886 {
7887#ifdef FEAT_RIGHTLEFT
7888 if (!revins_on) /* put cursor on char to be deleted */
7889#endif
7890 dec_cursor();
7891
7892 /* start of word? */
7893 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7894 {
7895 mode = BACKSPACE_WORD_NOT_SPACE;
7896 temp = vim_iswordc(gchar_cursor());
7897 }
7898 /* end of word? */
7899 else if (mode == BACKSPACE_WORD_NOT_SPACE
7900 && (vim_isspace(cc = gchar_cursor())
7901 || vim_iswordc(cc) != temp))
7902 {
7903#ifdef FEAT_RIGHTLEFT
7904 if (!revins_on)
7905#endif
7906 inc_cursor();
7907#ifdef FEAT_RIGHTLEFT
7908 else if (State & REPLACE_FLAG)
7909 dec_cursor();
7910#endif
7911 break;
7912 }
7913 if (State & REPLACE_FLAG)
7914 replace_do_bs();
7915 else
7916 {
7917#ifdef FEAT_MBYTE
7918 if (enc_utf8 && p_deco)
7919 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7920#endif
7921 (void)del_char(FALSE);
7922#ifdef FEAT_MBYTE
7923 /*
7924 * If p1 or p2 is non-zero, there are combining characters we
7925 * need to take account of. Don't back up before the base
7926 * character.
7927 */
7928 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7929 inc_cursor();
7930#endif
7931#ifdef FEAT_RIGHTLEFT
7932 if (revins_chars)
7933 {
7934 revins_chars--;
7935 revins_legal++;
7936 }
7937 if (revins_on && gchar_cursor() == NUL)
7938 break;
7939#endif
7940 }
7941 /* Just a single backspace?: */
7942 if (mode == BACKSPACE_CHAR)
7943 break;
7944 } while (
7945#ifdef FEAT_RIGHTLEFT
7946 revins_on ||
7947#endif
7948 (curwin->w_cursor.col > mincol
7949 && (curwin->w_cursor.lnum != Insstart.lnum
7950 || curwin->w_cursor.col != Insstart.col)));
7951 did_backspace = TRUE;
7952 }
7953#ifdef FEAT_SMARTINDENT
7954 did_si = FALSE;
7955 can_si = FALSE;
7956 can_si_back = FALSE;
7957#endif
7958 if (curwin->w_cursor.col <= 1)
7959 did_ai = FALSE;
7960 /*
7961 * It's a little strange to put backspaces into the redo
7962 * buffer, but it makes auto-indent a lot easier to deal
7963 * with.
7964 */
7965 AppendCharToRedobuff(c);
7966
7967 /* If deleted before the insertion point, adjust it */
7968 if (curwin->w_cursor.lnum == Insstart.lnum
7969 && curwin->w_cursor.col < Insstart.col)
7970 Insstart.col = curwin->w_cursor.col;
7971
7972 /* vi behaviour: the cursor moves backward but the character that
7973 * was there remains visible
7974 * Vim behaviour: the cursor moves backward and the character that
7975 * was there is erased from the screen.
7976 * We can emulate the vi behaviour by pretending there is a dollar
7977 * displayed even when there isn't.
7978 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7979 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7980 dollar_vcol = curwin->w_virtcol;
7981
7982 return did_backspace;
7983}
7984
7985#ifdef FEAT_MOUSE
7986 static void
7987ins_mouse(c)
7988 int c;
7989{
7990 pos_T tpos;
7991
7992# ifdef FEAT_GUI
7993 /* When GUI is active, also move/paste when 'mouse' is empty */
7994 if (!gui.in_use)
7995# endif
7996 if (!mouse_has(MOUSE_INSERT))
7997 return;
7998
7999 undisplay_dollar();
8000 tpos = curwin->w_cursor;
8001 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8002 {
8003 start_arrow(&tpos);
8004# ifdef FEAT_CINDENT
8005 can_cindent = TRUE;
8006# endif
8007 }
8008
8009#ifdef FEAT_WINDOWS
8010 /* redraw status lines (in case another window became active) */
8011 redraw_statuslines();
8012#endif
8013}
8014
8015 static void
8016ins_mousescroll(up)
8017 int up;
8018{
8019 pos_T tpos;
8020# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8021 win_T *old_curwin;
8022# endif
8023
8024 tpos = curwin->w_cursor;
8025
8026# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8027 old_curwin = curwin;
8028
8029 /* Currently the mouse coordinates are only known in the GUI. */
8030 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8031 {
8032 int row, col;
8033
8034 row = mouse_row;
8035 col = mouse_col;
8036
8037 /* find the window at the pointer coordinates */
8038 curwin = mouse_find_win(&row, &col);
8039 curbuf = curwin->w_buffer;
8040 }
8041 if (curwin == old_curwin)
8042# endif
8043 undisplay_dollar();
8044
8045 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8046 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8047 else
8048 scroll_redraw(up, 3L);
8049
8050# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8051 curwin->w_redr_status = TRUE;
8052
8053 curwin = old_curwin;
8054 curbuf = curwin->w_buffer;
8055# endif
8056
8057 if (!equalpos(curwin->w_cursor, tpos))
8058 {
8059 start_arrow(&tpos);
8060# ifdef FEAT_CINDENT
8061 can_cindent = TRUE;
8062# endif
8063 }
8064}
8065#endif
8066
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008067#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8068 void
8069ins_tabline(c)
8070 int c;
8071{
8072 /* We will be leaving the current window, unless closing another tab. */
8073 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8074 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8075 {
8076 undisplay_dollar();
8077 start_arrow(&curwin->w_cursor);
8078# ifdef FEAT_CINDENT
8079 can_cindent = TRUE;
8080# endif
8081 }
8082
8083 if (c == K_TABLINE)
8084 goto_tabpage(current_tab);
8085 else
8086 handle_tabmenu();
8087
8088}
8089#endif
8090
8091#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 void
8093ins_scroll()
8094{
8095 pos_T tpos;
8096
8097 undisplay_dollar();
8098 tpos = curwin->w_cursor;
8099 if (gui_do_scroll())
8100 {
8101 start_arrow(&tpos);
8102# ifdef FEAT_CINDENT
8103 can_cindent = TRUE;
8104# endif
8105 }
8106}
8107
8108 void
8109ins_horscroll()
8110{
8111 pos_T tpos;
8112
8113 undisplay_dollar();
8114 tpos = curwin->w_cursor;
8115 if (gui_do_horiz_scroll())
8116 {
8117 start_arrow(&tpos);
8118# ifdef FEAT_CINDENT
8119 can_cindent = TRUE;
8120# endif
8121 }
8122}
8123#endif
8124
8125 static void
8126ins_left()
8127{
8128 pos_T tpos;
8129
8130#ifdef FEAT_FOLDING
8131 if ((fdo_flags & FDO_HOR) && KeyTyped)
8132 foldOpenCursor();
8133#endif
8134 undisplay_dollar();
8135 tpos = curwin->w_cursor;
8136 if (oneleft() == OK)
8137 {
8138 start_arrow(&tpos);
8139#ifdef FEAT_RIGHTLEFT
8140 /* If exit reversed string, position is fixed */
8141 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8142 revins_legal++;
8143 revins_chars++;
8144#endif
8145 }
8146
8147 /*
8148 * if 'whichwrap' set for cursor in insert mode may go to
8149 * previous line
8150 */
8151 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8152 {
8153 start_arrow(&tpos);
8154 --(curwin->w_cursor.lnum);
8155 coladvance((colnr_T)MAXCOL);
8156 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8157 }
8158 else
8159 vim_beep();
8160}
8161
8162 static void
8163ins_home(c)
8164 int c;
8165{
8166 pos_T tpos;
8167
8168#ifdef FEAT_FOLDING
8169 if ((fdo_flags & FDO_HOR) && KeyTyped)
8170 foldOpenCursor();
8171#endif
8172 undisplay_dollar();
8173 tpos = curwin->w_cursor;
8174 if (c == K_C_HOME)
8175 curwin->w_cursor.lnum = 1;
8176 curwin->w_cursor.col = 0;
8177#ifdef FEAT_VIRTUALEDIT
8178 curwin->w_cursor.coladd = 0;
8179#endif
8180 curwin->w_curswant = 0;
8181 start_arrow(&tpos);
8182}
8183
8184 static void
8185ins_end(c)
8186 int c;
8187{
8188 pos_T tpos;
8189
8190#ifdef FEAT_FOLDING
8191 if ((fdo_flags & FDO_HOR) && KeyTyped)
8192 foldOpenCursor();
8193#endif
8194 undisplay_dollar();
8195 tpos = curwin->w_cursor;
8196 if (c == K_C_END)
8197 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8198 coladvance((colnr_T)MAXCOL);
8199 curwin->w_curswant = MAXCOL;
8200
8201 start_arrow(&tpos);
8202}
8203
8204 static void
8205ins_s_left()
8206{
8207#ifdef FEAT_FOLDING
8208 if ((fdo_flags & FDO_HOR) && KeyTyped)
8209 foldOpenCursor();
8210#endif
8211 undisplay_dollar();
8212 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8213 {
8214 start_arrow(&curwin->w_cursor);
8215 (void)bck_word(1L, FALSE, FALSE);
8216 curwin->w_set_curswant = TRUE;
8217 }
8218 else
8219 vim_beep();
8220}
8221
8222 static void
8223ins_right()
8224{
8225#ifdef FEAT_FOLDING
8226 if ((fdo_flags & FDO_HOR) && KeyTyped)
8227 foldOpenCursor();
8228#endif
8229 undisplay_dollar();
8230 if (gchar_cursor() != NUL || virtual_active()
8231 )
8232 {
8233 start_arrow(&curwin->w_cursor);
8234 curwin->w_set_curswant = TRUE;
8235#ifdef FEAT_VIRTUALEDIT
8236 if (virtual_active())
8237 oneright();
8238 else
8239#endif
8240 {
8241#ifdef FEAT_MBYTE
8242 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008243 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 else
8245#endif
8246 ++curwin->w_cursor.col;
8247 }
8248
8249#ifdef FEAT_RIGHTLEFT
8250 revins_legal++;
8251 if (revins_chars)
8252 revins_chars--;
8253#endif
8254 }
8255 /* if 'whichwrap' set for cursor in insert mode, may move the
8256 * cursor to the next line */
8257 else if (vim_strchr(p_ww, ']') != NULL
8258 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8259 {
8260 start_arrow(&curwin->w_cursor);
8261 curwin->w_set_curswant = TRUE;
8262 ++curwin->w_cursor.lnum;
8263 curwin->w_cursor.col = 0;
8264 }
8265 else
8266 vim_beep();
8267}
8268
8269 static void
8270ins_s_right()
8271{
8272#ifdef FEAT_FOLDING
8273 if ((fdo_flags & FDO_HOR) && KeyTyped)
8274 foldOpenCursor();
8275#endif
8276 undisplay_dollar();
8277 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8278 || gchar_cursor() != NUL)
8279 {
8280 start_arrow(&curwin->w_cursor);
8281 (void)fwd_word(1L, FALSE, 0);
8282 curwin->w_set_curswant = TRUE;
8283 }
8284 else
8285 vim_beep();
8286}
8287
8288 static void
8289ins_up(startcol)
8290 int startcol; /* when TRUE move to Insstart.col */
8291{
8292 pos_T tpos;
8293 linenr_T old_topline = curwin->w_topline;
8294#ifdef FEAT_DIFF
8295 int old_topfill = curwin->w_topfill;
8296#endif
8297
8298 undisplay_dollar();
8299 tpos = curwin->w_cursor;
8300 if (cursor_up(1L, TRUE) == OK)
8301 {
8302 if (startcol)
8303 coladvance(getvcol_nolist(&Insstart));
8304 if (old_topline != curwin->w_topline
8305#ifdef FEAT_DIFF
8306 || old_topfill != curwin->w_topfill
8307#endif
8308 )
8309 redraw_later(VALID);
8310 start_arrow(&tpos);
8311#ifdef FEAT_CINDENT
8312 can_cindent = TRUE;
8313#endif
8314 }
8315 else
8316 vim_beep();
8317}
8318
8319 static void
8320ins_pageup()
8321{
8322 pos_T tpos;
8323
8324 undisplay_dollar();
8325 tpos = curwin->w_cursor;
8326 if (onepage(BACKWARD, 1L) == OK)
8327 {
8328 start_arrow(&tpos);
8329#ifdef FEAT_CINDENT
8330 can_cindent = TRUE;
8331#endif
8332 }
8333 else
8334 vim_beep();
8335}
8336
8337 static void
8338ins_down(startcol)
8339 int startcol; /* when TRUE move to Insstart.col */
8340{
8341 pos_T tpos;
8342 linenr_T old_topline = curwin->w_topline;
8343#ifdef FEAT_DIFF
8344 int old_topfill = curwin->w_topfill;
8345#endif
8346
8347 undisplay_dollar();
8348 tpos = curwin->w_cursor;
8349 if (cursor_down(1L, TRUE) == OK)
8350 {
8351 if (startcol)
8352 coladvance(getvcol_nolist(&Insstart));
8353 if (old_topline != curwin->w_topline
8354#ifdef FEAT_DIFF
8355 || old_topfill != curwin->w_topfill
8356#endif
8357 )
8358 redraw_later(VALID);
8359 start_arrow(&tpos);
8360#ifdef FEAT_CINDENT
8361 can_cindent = TRUE;
8362#endif
8363 }
8364 else
8365 vim_beep();
8366}
8367
8368 static void
8369ins_pagedown()
8370{
8371 pos_T tpos;
8372
8373 undisplay_dollar();
8374 tpos = curwin->w_cursor;
8375 if (onepage(FORWARD, 1L) == OK)
8376 {
8377 start_arrow(&tpos);
8378#ifdef FEAT_CINDENT
8379 can_cindent = TRUE;
8380#endif
8381 }
8382 else
8383 vim_beep();
8384}
8385
8386#ifdef FEAT_DND
8387 static void
8388ins_drop()
8389{
8390 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8391}
8392#endif
8393
8394/*
8395 * Handle TAB in Insert or Replace mode.
8396 * Return TRUE when the TAB needs to be inserted like a normal character.
8397 */
8398 static int
8399ins_tab()
8400{
8401 int ind;
8402 int i;
8403 int temp;
8404
8405 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8406 Insstart_blank_vcol = get_nolist_virtcol();
8407 if (echeck_abbr(TAB + ABBR_OFF))
8408 return FALSE;
8409
8410 ind = inindent(0);
8411#ifdef FEAT_CINDENT
8412 if (ind)
8413 can_cindent = FALSE;
8414#endif
8415
8416 /*
8417 * When nothing special, insert TAB like a normal character
8418 */
8419 if (!curbuf->b_p_et
8420 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8421 && curbuf->b_p_sts == 0)
8422 return TRUE;
8423
8424 if (stop_arrow() == FAIL)
8425 return TRUE;
8426
8427 did_ai = FALSE;
8428#ifdef FEAT_SMARTINDENT
8429 did_si = FALSE;
8430 can_si = FALSE;
8431 can_si_back = FALSE;
8432#endif
8433 AppendToRedobuff((char_u *)"\t");
8434
8435 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8436 temp = (int)curbuf->b_p_sw;
8437 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8438 temp = (int)curbuf->b_p_sts;
8439 else /* otherwise use 'tabstop' */
8440 temp = (int)curbuf->b_p_ts;
8441 temp -= get_nolist_virtcol() % temp;
8442
8443 /*
8444 * Insert the first space with ins_char(). It will delete one char in
8445 * replace mode. Insert the rest with ins_str(); it will not delete any
8446 * chars. For VREPLACE mode, we use ins_char() for all characters.
8447 */
8448 ins_char(' ');
8449 while (--temp > 0)
8450 {
8451#ifdef FEAT_VREPLACE
8452 if (State & VREPLACE_FLAG)
8453 ins_char(' ');
8454 else
8455#endif
8456 {
8457 ins_str((char_u *)" ");
8458 if (State & REPLACE_FLAG) /* no char replaced */
8459 replace_push(NUL);
8460 }
8461 }
8462
8463 /*
8464 * When 'expandtab' not set: Replace spaces by TABs where possible.
8465 */
8466 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8467 {
8468 char_u *ptr;
8469#ifdef FEAT_VREPLACE
8470 char_u *saved_line = NULL; /* init for GCC */
8471 pos_T pos;
8472#endif
8473 pos_T fpos;
8474 pos_T *cursor;
8475 colnr_T want_vcol, vcol;
8476 int change_col = -1;
8477 int save_list = curwin->w_p_list;
8478
8479 /*
8480 * Get the current line. For VREPLACE mode, don't make real changes
8481 * yet, just work on a copy of the line.
8482 */
8483#ifdef FEAT_VREPLACE
8484 if (State & VREPLACE_FLAG)
8485 {
8486 pos = curwin->w_cursor;
8487 cursor = &pos;
8488 saved_line = vim_strsave(ml_get_curline());
8489 if (saved_line == NULL)
8490 return FALSE;
8491 ptr = saved_line + pos.col;
8492 }
8493 else
8494#endif
8495 {
8496 ptr = ml_get_cursor();
8497 cursor = &curwin->w_cursor;
8498 }
8499
8500 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8501 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8502 curwin->w_p_list = FALSE;
8503
8504 /* Find first white before the cursor */
8505 fpos = curwin->w_cursor;
8506 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8507 {
8508 --fpos.col;
8509 --ptr;
8510 }
8511
8512 /* In Replace mode, don't change characters before the insert point. */
8513 if ((State & REPLACE_FLAG)
8514 && fpos.lnum == Insstart.lnum
8515 && fpos.col < Insstart.col)
8516 {
8517 ptr += Insstart.col - fpos.col;
8518 fpos.col = Insstart.col;
8519 }
8520
8521 /* compute virtual column numbers of first white and cursor */
8522 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8523 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8524
8525 /* Use as many TABs as possible. Beware of 'showbreak' and
8526 * 'linebreak' adding extra virtual columns. */
8527 while (vim_iswhite(*ptr))
8528 {
8529 i = lbr_chartabsize((char_u *)"\t", vcol);
8530 if (vcol + i > want_vcol)
8531 break;
8532 if (*ptr != TAB)
8533 {
8534 *ptr = TAB;
8535 if (change_col < 0)
8536 {
8537 change_col = fpos.col; /* Column of first change */
8538 /* May have to adjust Insstart */
8539 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8540 Insstart.col = fpos.col;
8541 }
8542 }
8543 ++fpos.col;
8544 ++ptr;
8545 vcol += i;
8546 }
8547
8548 if (change_col >= 0)
8549 {
8550 int repl_off = 0;
8551
8552 /* Skip over the spaces we need. */
8553 while (vcol < want_vcol && *ptr == ' ')
8554 {
8555 vcol += lbr_chartabsize(ptr, vcol);
8556 ++ptr;
8557 ++repl_off;
8558 }
8559 if (vcol > want_vcol)
8560 {
8561 /* Must have a char with 'showbreak' just before it. */
8562 --ptr;
8563 --repl_off;
8564 }
8565 fpos.col += repl_off;
8566
8567 /* Delete following spaces. */
8568 i = cursor->col - fpos.col;
8569 if (i > 0)
8570 {
8571 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8572 /* correct replace stack. */
8573 if ((State & REPLACE_FLAG)
8574#ifdef FEAT_VREPLACE
8575 && !(State & VREPLACE_FLAG)
8576#endif
8577 )
8578 for (temp = i; --temp >= 0; )
8579 replace_join(repl_off);
8580 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008581#ifdef FEAT_NETBEANS_INTG
8582 if (usingNetbeans)
8583 {
8584 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8585 (long)(i + 1));
8586 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8587 (char_u *)"\t", 1);
8588 }
8589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 cursor->col -= i;
8591
8592#ifdef FEAT_VREPLACE
8593 /*
8594 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8595 * backspacing over the changed spacing and then inserting the new
8596 * spacing.
8597 */
8598 if (State & VREPLACE_FLAG)
8599 {
8600 /* Backspace from real cursor to change_col */
8601 backspace_until_column(change_col);
8602
8603 /* Insert each char in saved_line from changed_col to
8604 * ptr-cursor */
8605 ins_bytes_len(saved_line + change_col,
8606 cursor->col - change_col);
8607 }
8608#endif
8609 }
8610
8611#ifdef FEAT_VREPLACE
8612 if (State & VREPLACE_FLAG)
8613 vim_free(saved_line);
8614#endif
8615 curwin->w_p_list = save_list;
8616 }
8617
8618 return FALSE;
8619}
8620
8621/*
8622 * Handle CR or NL in insert mode.
8623 * Return TRUE when out of memory or can't undo.
8624 */
8625 static int
8626ins_eol(c)
8627 int c;
8628{
8629 int i;
8630
8631 if (echeck_abbr(c + ABBR_OFF))
8632 return FALSE;
8633 if (stop_arrow() == FAIL)
8634 return TRUE;
8635 undisplay_dollar();
8636
8637 /*
8638 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8639 * character under the cursor. Only push a NUL on the replace stack,
8640 * nothing to put back when the NL is deleted.
8641 */
8642 if ((State & REPLACE_FLAG)
8643#ifdef FEAT_VREPLACE
8644 && !(State & VREPLACE_FLAG)
8645#endif
8646 )
8647 replace_push(NUL);
8648
8649 /*
8650 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8651 * replacing the next line, so we push all of the characters left on the
8652 * line onto the replace stack. This is not done here though, it is done
8653 * in open_line().
8654 */
8655
8656#ifdef FEAT_RIGHTLEFT
8657# ifdef FEAT_FKMAP
8658 if (p_altkeymap && p_fkmap)
8659 fkmap(NL);
8660# endif
8661 /* NL in reverse insert will always start in the end of
8662 * current line. */
8663 if (revins_on)
8664 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8665#endif
8666
8667 AppendToRedobuff(NL_STR);
8668 i = open_line(FORWARD,
8669#ifdef FEAT_COMMENTS
8670 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8671#endif
8672 0, old_indent);
8673 old_indent = 0;
8674#ifdef FEAT_CINDENT
8675 can_cindent = TRUE;
8676#endif
8677
8678 return (!i);
8679}
8680
8681#ifdef FEAT_DIGRAPHS
8682/*
8683 * Handle digraph in insert mode.
8684 * Returns character still to be inserted, or NUL when nothing remaining to be
8685 * done.
8686 */
8687 static int
8688ins_digraph()
8689{
8690 int c;
8691 int cc;
8692
8693 pc_status = PC_STATUS_UNSET;
8694 if (redrawing() && !char_avail())
8695 {
8696 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008697 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698
8699 edit_putchar('?', TRUE);
8700#ifdef FEAT_CMDL_INFO
8701 add_to_showcmd_c(Ctrl_K);
8702#endif
8703 }
8704
8705#ifdef USE_ON_FLY_SCROLL
8706 dont_scroll = TRUE; /* disallow scrolling here */
8707#endif
8708
8709 /* don't map the digraph chars. This also prevents the
8710 * mode message to be deleted when ESC is hit */
8711 ++no_mapping;
8712 ++allow_keys;
8713 c = safe_vgetc();
8714 --no_mapping;
8715 --allow_keys;
8716 if (IS_SPECIAL(c) || mod_mask) /* special key */
8717 {
8718#ifdef FEAT_CMDL_INFO
8719 clear_showcmd();
8720#endif
8721 insert_special(c, TRUE, FALSE);
8722 return NUL;
8723 }
8724 if (c != ESC)
8725 {
8726 if (redrawing() && !char_avail())
8727 {
8728 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008729 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730
8731 if (char2cells(c) == 1)
8732 {
8733 /* first remove the '?', otherwise it's restored when typing
8734 * an ESC next */
8735 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008736 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 edit_putchar(c, TRUE);
8738 }
8739#ifdef FEAT_CMDL_INFO
8740 add_to_showcmd_c(c);
8741#endif
8742 }
8743 ++no_mapping;
8744 ++allow_keys;
8745 cc = safe_vgetc();
8746 --no_mapping;
8747 --allow_keys;
8748 if (cc != ESC)
8749 {
8750 AppendToRedobuff((char_u *)CTRL_V_STR);
8751 c = getdigraph(c, cc, TRUE);
8752#ifdef FEAT_CMDL_INFO
8753 clear_showcmd();
8754#endif
8755 return c;
8756 }
8757 }
8758 edit_unputchar();
8759#ifdef FEAT_CMDL_INFO
8760 clear_showcmd();
8761#endif
8762 return NUL;
8763}
8764#endif
8765
8766/*
8767 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8768 * Returns the char to be inserted, or NUL if none found.
8769 */
8770 static int
8771ins_copychar(lnum)
8772 linenr_T lnum;
8773{
8774 int c;
8775 int temp;
8776 char_u *ptr, *prev_ptr;
8777
8778 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8779 {
8780 vim_beep();
8781 return NUL;
8782 }
8783
8784 /* try to advance to the cursor column */
8785 temp = 0;
8786 ptr = ml_get(lnum);
8787 prev_ptr = ptr;
8788 validate_virtcol();
8789 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8790 {
8791 prev_ptr = ptr;
8792 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8793 }
8794 if ((colnr_T)temp > curwin->w_virtcol)
8795 ptr = prev_ptr;
8796
8797#ifdef FEAT_MBYTE
8798 c = (*mb_ptr2char)(ptr);
8799#else
8800 c = *ptr;
8801#endif
8802 if (c == NUL)
8803 vim_beep();
8804 return c;
8805}
8806
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008807/*
8808 * CTRL-Y or CTRL-E typed in Insert mode.
8809 */
8810 static int
8811ins_ctrl_ey(tc)
8812 int tc;
8813{
8814 int c = tc;
8815
8816#ifdef FEAT_INS_EXPAND
8817 if (ctrl_x_mode == CTRL_X_SCROLL)
8818 {
8819 if (c == Ctrl_Y)
8820 scrolldown_clamp();
8821 else
8822 scrollup_clamp();
8823 redraw_later(VALID);
8824 }
8825 else
8826#endif
8827 {
8828 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8829 if (c != NUL)
8830 {
8831 long tw_save;
8832
8833 /* The character must be taken literally, insert like it
8834 * was typed after a CTRL-V, and pretend 'textwidth'
8835 * wasn't set. Digits, 'o' and 'x' are special after a
8836 * CTRL-V, don't use it for these. */
8837 if (c < 256 && !isalnum(c))
8838 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8839 tw_save = curbuf->b_p_tw;
8840 curbuf->b_p_tw = -1;
8841 insert_special(c, TRUE, FALSE);
8842 curbuf->b_p_tw = tw_save;
8843#ifdef FEAT_RIGHTLEFT
8844 revins_chars++;
8845 revins_legal++;
8846#endif
8847 c = Ctrl_V; /* pretend CTRL-V is last character */
8848 auto_format(FALSE, TRUE);
8849 }
8850 }
8851 return c;
8852}
8853
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854#ifdef FEAT_SMARTINDENT
8855/*
8856 * Try to do some very smart auto-indenting.
8857 * Used when inserting a "normal" character.
8858 */
8859 static void
8860ins_try_si(c)
8861 int c;
8862{
8863 pos_T *pos, old_pos;
8864 char_u *ptr;
8865 int i;
8866 int temp;
8867
8868 /*
8869 * do some very smart indenting when entering '{' or '}'
8870 */
8871 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8872 {
8873 /*
8874 * for '}' set indent equal to indent of line containing matching '{'
8875 */
8876 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8877 {
8878 old_pos = curwin->w_cursor;
8879 /*
8880 * If the matching '{' has a ')' immediately before it (ignoring
8881 * white-space), then line up with the start of the line
8882 * containing the matching '(' if there is one. This handles the
8883 * case where an "if (..\n..) {" statement continues over multiple
8884 * lines -- webb
8885 */
8886 ptr = ml_get(pos->lnum);
8887 i = pos->col;
8888 if (i > 0) /* skip blanks before '{' */
8889 while (--i > 0 && vim_iswhite(ptr[i]))
8890 ;
8891 curwin->w_cursor.lnum = pos->lnum;
8892 curwin->w_cursor.col = i;
8893 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8894 curwin->w_cursor = *pos;
8895 i = get_indent();
8896 curwin->w_cursor = old_pos;
8897#ifdef FEAT_VREPLACE
8898 if (State & VREPLACE_FLAG)
8899 change_indent(INDENT_SET, i, FALSE, NUL);
8900 else
8901#endif
8902 (void)set_indent(i, SIN_CHANGED);
8903 }
8904 else if (curwin->w_cursor.col > 0)
8905 {
8906 /*
8907 * when inserting '{' after "O" reduce indent, but not
8908 * more than indent of previous line
8909 */
8910 temp = TRUE;
8911 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8912 {
8913 old_pos = curwin->w_cursor;
8914 i = get_indent();
8915 while (curwin->w_cursor.lnum > 1)
8916 {
8917 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8918
8919 /* ignore empty lines and lines starting with '#'. */
8920 if (*ptr != '#' && *ptr != NUL)
8921 break;
8922 }
8923 if (get_indent() >= i)
8924 temp = FALSE;
8925 curwin->w_cursor = old_pos;
8926 }
8927 if (temp)
8928 shift_line(TRUE, FALSE, 1);
8929 }
8930 }
8931
8932 /*
8933 * set indent of '#' always to 0
8934 */
8935 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8936 {
8937 /* remember current indent for next line */
8938 old_indent = get_indent();
8939 (void)set_indent(0, SIN_CHANGED);
8940 }
8941
8942 /* Adjust ai_col, the char at this position can be deleted. */
8943 if (ai_col > curwin->w_cursor.col)
8944 ai_col = curwin->w_cursor.col;
8945}
8946#endif
8947
8948/*
8949 * Get the value that w_virtcol would have when 'list' is off.
8950 * Unless 'cpo' contains the 'L' flag.
8951 */
8952 static colnr_T
8953get_nolist_virtcol()
8954{
8955 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8956 return getvcol_nolist(&curwin->w_cursor);
8957 validate_virtcol();
8958 return curwin->w_virtcol;
8959}