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