blob: 2feb8e80f96e9c3a75baad34ab4a2abe461b930e [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;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000114static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000115static 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;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002469
2470 /* When the original text is the shown match don't set
2471 * compl_shown_match. */
2472 if (compl->cp_flags & ORIGINAL_TEXT)
2473 shown_match_ok = TRUE;
2474
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002475 if (!shown_match_ok && shown_compl != NULL)
2476 {
2477 /* The shown match isn't displayed, set it to the
2478 * previously displayed match. */
2479 compl_shown_match = shown_compl;
2480 shown_match_ok = TRUE;
2481 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002482 }
2483 compl = compl->cp_next;
2484 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002485
2486 if (!shown_match_ok) /* no displayed match at all */
2487 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002488 }
2489 }
2490 else
2491 {
2492 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002493 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002494 if (compl_match_array[i].pum_text == compl_shown_match->cp_str)
Bram Moolenaara6557602006-02-04 22:43:20 +00002495 break;
2496 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002497 }
2498
2499 if (compl_match_array != NULL)
2500 {
2501 /* Compute the screen column of the start of the completed text.
2502 * Use the cursor to get all wrapping and other settings right. */
2503 col = curwin->w_cursor.col;
2504 curwin->w_cursor.col = compl_col;
2505 validate_cursor_col();
2506 pum_display(compl_match_array, compl_match_arraysize, cur,
2507 curwin->w_cline_row + W_WINROW(curwin),
2508 curwin->w_cline_height,
Bram Moolenaar280f1262006-01-30 00:14:18 +00002509 curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002510 curwin->w_cursor.col = col;
2511 }
2512}
2513
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514#define DICT_FIRST (1) /* use just first element in "dict" */
2515#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002516
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002518 * Add any identifiers that match the given pattern in the list of dictionary
2519 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 */
2521 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002522ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2523 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002525 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002526 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002528 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 char_u *ptr;
2530 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 char_u **files;
2533 int count;
2534 int i;
2535 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002536 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537
Bram Moolenaar0b238792006-03-02 22:49:12 +00002538 if (*dict == NUL)
2539 {
2540#ifdef FEAT_SYN_HL
2541 /* When 'dictionary' is empty and spell checking is enabled use
2542 * "spell". */
2543 if (!thesaurus && curwin->w_p_spell)
2544 dict = (char_u *)"spell";
2545 else
2546#endif
2547 return;
2548 }
2549
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002551 if (buf == NULL)
2552 return;
2553
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 /* If 'infercase' is set, don't use 'smartcase' here */
2555 save_p_scs = p_scs;
2556 if (curbuf->b_p_inf)
2557 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002558
2559 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2560 * to only match at the start of a line. Otherwise just match the
2561 * pattern. */
2562 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2563 {
2564 i = STRLEN(pat) + 8;
2565 ptr = alloc(i);
2566 if (ptr == NULL)
2567 return;
2568 vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat);
2569 regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0);
2570 vim_free(ptr);
2571 }
2572 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002573 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002574 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002575 if (regmatch.regprog == NULL)
2576 goto theend;
2577 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002578
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2580 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002581 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 {
2583 /* copy one dictionary file name into buf */
2584 if (flags == DICT_EXACT)
2585 {
2586 count = 1;
2587 files = &dict;
2588 }
2589 else
2590 {
2591 /* Expand wildcards in the dictionary name, but do not allow
2592 * backticks (for security, the 'dict' option may have been set in
2593 * a modeline). */
2594 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaar0b238792006-03-02 22:49:12 +00002595 if (!thesaurus && STRCMP(buf, "spell") == 0)
2596 count = -1;
2597 else if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 || expand_wildcards(1, &buf, &count, &files,
2599 EW_FILE|EW_SILENT) != OK)
2600 count = 0;
2601 }
2602
Bram Moolenaar0b238792006-03-02 22:49:12 +00002603 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002605 /* Complete from active spelling. Skip "\<" in the pattern, we
2606 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002607 if (pat[0] == '\\' && pat[1] == '<')
2608 ptr = pat + 2;
2609 else
2610 ptr = pat;
2611 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002613 else
2614 {
2615 ins_compl_files(count, files, thesaurus, flags,
2616 &regmatch, buf, &dir);
2617 if (flags != DICT_EXACT)
2618 FreeWild(count, files);
2619 }
2620 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 break;
2622 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002623
2624theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 p_scs = save_p_scs;
2626 vim_free(regmatch.regprog);
2627 vim_free(buf);
2628}
2629
Bram Moolenaar0b238792006-03-02 22:49:12 +00002630 static void
2631ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2632 int count;
2633 char_u **files;
2634 int thesaurus;
2635 int flags;
2636 regmatch_T *regmatch;
2637 char_u *buf;
2638 int *dir;
2639{
2640 char_u *ptr;
2641 int i;
2642 FILE *fp;
2643 int add_r;
2644
2645 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2646 {
2647 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2648 if (flags != DICT_EXACT)
2649 {
2650 vim_snprintf((char *)IObuff, IOSIZE,
2651 _("Scanning dictionary: %s"), (char *)files[i]);
2652 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2653 }
2654
2655 if (fp != NULL)
2656 {
2657 /*
2658 * Read dictionary file line by line.
2659 * Check each line for a match.
2660 */
2661 while (!got_int && !compl_interrupted
2662 && !vim_fgets(buf, LSIZE, fp))
2663 {
2664 ptr = buf;
2665 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2666 {
2667 ptr = regmatch->startp[0];
2668 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2669 ptr = find_line_end(ptr);
2670 else
2671 ptr = find_word_end(ptr);
2672 add_r = ins_compl_add_infercase(regmatch->startp[0],
2673 (int)(ptr - regmatch->startp[0]),
2674 p_ic, files[i], *dir, 0);
2675 if (thesaurus)
2676 {
2677 char_u *wstart;
2678
2679 /*
2680 * Add the other matches on the line
2681 */
2682 while (!got_int)
2683 {
2684 /* Find start of the next word. Skip white
2685 * space and punctuation. */
2686 ptr = find_word_start(ptr);
2687 if (*ptr == NUL || *ptr == NL)
2688 break;
2689 wstart = ptr;
2690
2691 /* Find end of the word and add it. */
2692#ifdef FEAT_MBYTE
2693 if (has_mbyte)
2694 /* Japanese words may have characters in
2695 * different classes, only separate words
2696 * with single-byte non-word characters. */
2697 while (*ptr != NUL)
2698 {
2699 int l = (*mb_ptr2len)(ptr);
2700
2701 if (l < 2 && !vim_iswordc(*ptr))
2702 break;
2703 ptr += l;
2704 }
2705 else
2706#endif
2707 ptr = find_word_end(ptr);
2708 add_r = ins_compl_add_infercase(wstart,
2709 (int)(ptr - wstart),
2710 p_ic, files[i], *dir, 0);
2711 }
2712 }
2713 if (add_r == OK)
2714 /* if dir was BACKWARD then honor it just once */
2715 *dir = FORWARD;
2716 else if (add_r == FAIL)
2717 break;
2718 /* avoid expensive call to vim_regexec() when at end
2719 * of line */
2720 if (*ptr == '\n' || got_int)
2721 break;
2722 }
2723 line_breakcheck();
2724 ins_compl_check_keys(50);
2725 }
2726 fclose(fp);
2727 }
2728 }
2729}
2730
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731/*
2732 * Find the start of the next word.
2733 * Returns a pointer to the first char of the word. Also stops at a NUL.
2734 */
2735 char_u *
2736find_word_start(ptr)
2737 char_u *ptr;
2738{
2739#ifdef FEAT_MBYTE
2740 if (has_mbyte)
2741 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002742 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 else
2744#endif
2745 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2746 ++ptr;
2747 return ptr;
2748}
2749
2750/*
2751 * Find the end of the word. Assumes it starts inside a word.
2752 * Returns a pointer to just after the word.
2753 */
2754 char_u *
2755find_word_end(ptr)
2756 char_u *ptr;
2757{
2758#ifdef FEAT_MBYTE
2759 int start_class;
2760
2761 if (has_mbyte)
2762 {
2763 start_class = mb_get_class(ptr);
2764 if (start_class > 1)
2765 while (*ptr != NUL)
2766 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002767 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 if (mb_get_class(ptr) != start_class)
2769 break;
2770 }
2771 }
2772 else
2773#endif
2774 while (vim_iswordc(*ptr))
2775 ++ptr;
2776 return ptr;
2777}
2778
2779/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002780 * Find the end of the line, omitting CR and NL at the end.
2781 * Returns a pointer to just after the line.
2782 */
2783 static char_u *
2784find_line_end(ptr)
2785 char_u *ptr;
2786{
2787 char_u *s;
2788
2789 s = ptr + STRLEN(ptr);
2790 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2791 --s;
2792 return s;
2793}
2794
2795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 * Free the list of completions
2797 */
2798 static void
2799ins_compl_free()
2800{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002801 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002803 vim_free(compl_pattern);
2804 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002805 vim_free(compl_leader);
2806 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002808 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002810
2811 ins_compl_del_pum();
2812 pum_clear();
2813
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002814 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 do
2816 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002817 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002818 compl_curr_match = compl_curr_match->cp_next;
2819 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002821 if (match->cp_flags & FREE_FNAME)
2822 vim_free(match->cp_fname);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002823 vim_free(match->cp_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002825 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2826 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827}
2828
2829 static void
2830ins_compl_clear()
2831{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002832 compl_cont_status = 0;
2833 compl_started = FALSE;
2834 compl_matches = 0;
2835 vim_free(compl_pattern);
2836 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002837 vim_free(compl_leader);
2838 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 edit_submode_extra = NULL;
2840}
2841
2842/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002843 * Return TRUE when Insert completion is active.
2844 */
2845 int
2846ins_compl_active()
2847{
2848 return compl_started;
2849}
2850
2851/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002852 * Delete one character before the cursor and show the subset of the matches
2853 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002854 * Returns TRUE if the work is done and another char to be got from the user.
2855 */
2856 static int
2857ins_compl_bs()
2858{
2859 char_u *line;
2860 char_u *p;
2861
2862 if (curwin->w_cursor.col <= compl_col + compl_length)
2863 {
2864 /* Deleted more than what was used to find matches, need to look for
2865 * matches all over again. */
2866 ins_compl_free();
2867 compl_started = FALSE;
2868 compl_matches = 0;
2869 }
2870
2871 line = ml_get_curline();
2872 p = line + curwin->w_cursor.col;
2873 mb_ptr_back(line, p);
2874
2875 vim_free(compl_leader);
2876 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2877 if (compl_leader != NULL)
2878 {
2879 ins_compl_del_pum();
2880 ins_compl_delete();
2881 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2882
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002883 if (compl_started)
2884 ins_compl_set_original_text(compl_leader);
2885 else
Bram Moolenaara6557602006-02-04 22:43:20 +00002886 {
2887 /* Matches were cleared, need to search for them now. */
2888 if (ins_complete(Ctrl_N) == FAIL)
2889 compl_cont_status = 0;
2890 else
2891 {
2892 /* Remove the completed word again. */
2893 ins_compl_delete();
2894 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2895 }
2896 }
2897
2898 /* Show the popup menu with a different set of matches. */
2899 ins_compl_show_pum();
2900 compl_used_match = FALSE;
2901
2902 return TRUE;
2903 }
2904 return FALSE;
2905}
2906
2907/*
2908 * Append one character to the match leader. May reduce the number of
2909 * matches.
2910 */
2911 static void
2912ins_compl_addleader(c)
2913 int c;
2914{
2915#ifdef FEAT_MBYTE
2916 int cc;
2917
2918 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2919 {
2920 char_u buf[MB_MAXBYTES + 1];
2921
2922 (*mb_char2bytes)(c, buf);
2923 buf[cc] = NUL;
2924 ins_char_bytes(buf, cc);
2925 }
2926 else
2927#endif
2928 ins_char(c);
2929
2930 vim_free(compl_leader);
2931 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
2932 curwin->w_cursor.col - compl_col);
2933 if (compl_leader != NULL)
2934 {
2935 /* Show the popup menu with a different set of matches. */
2936 ins_compl_del_pum();
2937 ins_compl_show_pum();
2938 compl_used_match = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002939 ins_compl_set_original_text(compl_leader);
2940 }
2941}
2942
2943/*
2944 * Set the first match, the original text.
2945 */
2946 static void
2947ins_compl_set_original_text(str)
2948 char_u *str;
2949{
2950 char_u *p;
2951
2952 /* Replace the original text entry. */
2953 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
2954 {
2955 p = vim_strsave(str);
2956 if (p != NULL)
2957 {
2958 vim_free(compl_first_match->cp_str);
2959 compl_first_match->cp_str = p;
2960 }
Bram Moolenaara6557602006-02-04 22:43:20 +00002961 }
2962}
2963
2964/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002965 * Append one character to the match leader. May reduce the number of
2966 * matches.
2967 */
2968 static void
2969ins_compl_addfrommatch()
2970{
2971 char_u *p;
2972 int len = curwin->w_cursor.col - compl_col;
2973 int c;
2974
2975 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00002976 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002977 return;
2978 p += len;
2979#ifdef FEAT_MBYTE
2980 c = mb_ptr2char(p);
2981#else
2982 c = *p;
2983#endif
2984 ins_compl_addleader(c);
2985}
2986
2987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00002989 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002990 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002992 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993ins_compl_prep(c)
2994 int c;
2995{
2996 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 int temp;
2998 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002999 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001 /* Forget any previous 'special' messages if this is actually
3002 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3003 */
3004 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3005 edit_submode_extra = NULL;
3006
3007 /* Ignore end of Select mode mapping */
3008 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003009 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003011 /* Set "compl_get_longest" when finding the first matches. */
3012 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3013 || (ctrl_x_mode == 0 && !compl_started))
3014 {
3015 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3016 compl_used_match = TRUE;
3017 }
3018
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3020 {
3021 /*
3022 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3023 * it will be yet. Now we decide.
3024 */
3025 switch (c)
3026 {
3027 case Ctrl_E:
3028 case Ctrl_Y:
3029 ctrl_x_mode = CTRL_X_SCROLL;
3030 if (!(State & REPLACE_FLAG))
3031 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3032 else
3033 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3034 edit_submode_pre = NULL;
3035 showmode();
3036 break;
3037 case Ctrl_L:
3038 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3039 break;
3040 case Ctrl_F:
3041 ctrl_x_mode = CTRL_X_FILES;
3042 break;
3043 case Ctrl_K:
3044 ctrl_x_mode = CTRL_X_DICTIONARY;
3045 break;
3046 case Ctrl_R:
3047 /* Simply allow ^R to happen without affecting ^X mode */
3048 break;
3049 case Ctrl_T:
3050 ctrl_x_mode = CTRL_X_THESAURUS;
3051 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003052#ifdef FEAT_COMPL_FUNC
3053 case Ctrl_U:
3054 ctrl_x_mode = CTRL_X_FUNCTION;
3055 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003056 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003057 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003058 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003059#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003060 case 's':
3061 case Ctrl_S:
3062 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003063#ifdef FEAT_SYN_HL
3064 spell_back_to_badword();
3065#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003066 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 case Ctrl_RSB:
3068 ctrl_x_mode = CTRL_X_TAGS;
3069 break;
3070#ifdef FEAT_FIND_ID
3071 case Ctrl_I:
3072 case K_S_TAB:
3073 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3074 break;
3075 case Ctrl_D:
3076 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3077 break;
3078#endif
3079 case Ctrl_V:
3080 case Ctrl_Q:
3081 ctrl_x_mode = CTRL_X_CMDLINE;
3082 break;
3083 case Ctrl_P:
3084 case Ctrl_N:
3085 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3086 * just started ^X mode, or there were enough ^X's to cancel
3087 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3088 * do normal expansion when interrupting a different mode (say
3089 * ^X^F^X^P or ^P^X^X^P, see below)
3090 * nothing changes if interrupting mode 0, (eg, the flag
3091 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003092 if (!(compl_cont_status & CONT_INTRPT))
3093 compl_cont_status |= CONT_LOCAL;
3094 else if (compl_cont_mode != 0)
3095 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 /* FALLTHROUGH */
3097 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003098 /* If we have typed at least 2 ^X's... for modes != 0, we set
3099 * compl_cont_status = 0 (eg, as if we had just started ^X
3100 * mode).
3101 * For mode 0, we set "compl_cont_mode" to an impossible
3102 * value, in both cases ^X^X can be used to restart the same
3103 * mode (avoiding ADDING mode).
3104 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3105 * 'complete' and local ^P expansions respectively.
3106 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3107 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 if (c == Ctrl_X)
3109 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003110 if (compl_cont_mode != 0)
3111 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003113 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 }
3115 ctrl_x_mode = 0;
3116 edit_submode = NULL;
3117 showmode();
3118 break;
3119 }
3120 }
3121 else if (ctrl_x_mode != 0)
3122 {
3123 /* We're already in CTRL-X mode, do we stay in it? */
3124 if (!vim_is_ctrl_x_key(c))
3125 {
3126 if (ctrl_x_mode == CTRL_X_SCROLL)
3127 ctrl_x_mode = 0;
3128 else
3129 ctrl_x_mode = CTRL_X_FINISHED;
3130 edit_submode = NULL;
3131 }
3132 showmode();
3133 }
3134
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003135 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 {
3137 /* Show error message from attempted keyword completion (probably
3138 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003139 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003141 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3142 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 || ctrl_x_mode == CTRL_X_FINISHED)
3144 {
3145 /* Get here when we have finished typing a sequence of ^N and
3146 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003147 * memory that was used, and make sure we can redo the insert. */
3148 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003150 char_u *p;
3151
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 /*
3153 * If any of the original typed text has been changed,
3154 * eg when ignorecase is set, we must add back-spaces to
3155 * the redo buffer. We add as few as necessary to delete
3156 * just the part of the original text that has changed.
3157 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003158 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003159 p = compl_orig_text;
3160 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003162 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 ++ptr;
3164 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003165 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00003167 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 }
3169
3170#ifdef FEAT_CINDENT
3171 want_cindent = (can_cindent && cindent_on());
3172#endif
3173 /*
3174 * When completing whole lines: fix indent for 'cindent'.
3175 * Otherwise, break line if it's too long.
3176 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003177 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 {
3179#ifdef FEAT_CINDENT
3180 /* re-indent the current line */
3181 if (want_cindent)
3182 {
3183 do_c_expr_indent();
3184 want_cindent = FALSE; /* don't do it again */
3185 }
3186#endif
3187 }
3188 else
3189 {
3190 /* put the cursor on the last char, for 'tw' formatting */
3191 curwin->w_cursor.col--;
3192 if (stop_arrow() == OK)
3193 insertchar(NUL, 0, -1);
3194 curwin->w_cursor.col++;
3195 }
3196
3197 auto_format(FALSE, TRUE);
3198
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003199 /* if the popup menu is displayed hitting Enter means accepting
3200 * the selection without inserting anything. */
3201 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
3202 retval = TRUE;
3203
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003205 compl_started = FALSE;
3206 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 msg_clr_cmdline(); /* necessary for "noshowmode" */
3208 ctrl_x_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 if (edit_submode != NULL)
3210 {
3211 edit_submode = NULL;
3212 showmode();
3213 }
3214
3215#ifdef FEAT_CINDENT
3216 /*
3217 * Indent now if a key was typed that is in 'cinkeys'.
3218 */
3219 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3220 do_c_expr_indent();
3221#endif
3222 }
3223 }
3224
3225 /* reset continue_* if we left expansion-mode, if we stay they'll be
3226 * (re)set properly in ins_complete() */
3227 if (!vim_is_ctrl_x_key(c))
3228 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003229 compl_cont_status = 0;
3230 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003232
3233 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234}
3235
3236/*
3237 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3238 * (depending on flag) starting from buf and looking for a non-scanned
3239 * buffer (other than curbuf). curbuf is special, if it is called with
3240 * buf=curbuf then it has to be the first call for a given flag/expansion.
3241 *
3242 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3243 */
3244 static buf_T *
3245ins_compl_next_buf(buf, flag)
3246 buf_T *buf;
3247 int flag;
3248{
3249#ifdef FEAT_WINDOWS
3250 static win_T *wp;
3251#endif
3252
3253 if (flag == 'w') /* just windows */
3254 {
3255#ifdef FEAT_WINDOWS
3256 if (buf == curbuf) /* first call for this flag/expansion */
3257 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003258 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 && wp->w_buffer->b_scanned)
3260 ;
3261 buf = wp->w_buffer;
3262#else
3263 buf = curbuf;
3264#endif
3265 }
3266 else
3267 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3268 * (unlisted buffers)
3269 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003270 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 && ((flag == 'U'
3272 ? buf->b_p_bl
3273 : (!buf->b_p_bl
3274 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003275 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 ;
3277 return buf;
3278}
3279
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003280#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003281static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003282
3283/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003284 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003285 * get matches in "matches".
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003286 * Return value is number of matches.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003287 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003288 static void
3289expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003290 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003291 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003292{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003293 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003294 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003295 listitem_T *li;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003296 char_u *p;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003297 char_u *funcname;
3298 pos_T pos;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003299 int dir = compl_direction;
3300 char_u *x;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003301 int icase;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003302
Bram Moolenaare344bea2005-09-01 20:46:49 +00003303 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3304 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003305 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003306
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003307 /* Call 'completefunc' to obtain the list of matches. */
3308 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003309 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003310
Bram Moolenaare344bea2005-09-01 20:46:49 +00003311 pos = curwin->w_cursor;
3312 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3313 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003314 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003315 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003316
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003317 /* Go through the List with matches and add each of them. */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003318 for (li = matchlist->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003319 {
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003320 icase = p_ic;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003321 if (li->li_tv.v_type == VAR_DICT && li->li_tv.vval.v_dict != NULL)
3322 {
3323 p = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"word", FALSE);
3324 x = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"menu", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003325 if (get_dict_string(li->li_tv.vval.v_dict, (char_u *)"icase",
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003326 FALSE) != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003327 icase = get_dict_number(li->li_tv.vval.v_dict,
3328 (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003329 }
3330 else
3331 {
3332 p = get_tv_string_chk(&li->li_tv);
3333 x = NULL;
3334 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003335 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003336 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003337 if (ins_compl_add(p, -1, icase, NULL, x, dir, 0) == OK)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003338 /* if dir was BACKWARD then honor it just once */
3339 dir = FORWARD;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003340 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003341 else if (did_emsg)
3342 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003343 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003344
3345 list_unref(matchlist);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003346}
3347#endif /* FEAT_COMPL_FUNC */
3348
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003349/*
3350 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003351 * The search starts at position "ini" in curbuf and in the direction
3352 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003353 * When "compl_started" is FALSE start at that position, otherwise continue
3354 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003355 * This may return before finding all the matches.
3356 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 */
3358 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003359ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
3362 static pos_T first_match_pos;
3363 static pos_T last_match_pos;
3364 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003365 static int found_all = FALSE; /* Found all matches of a
3366 certain type. */
3367 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
Bram Moolenaar572cb562005-08-05 21:35:02 +00003369 pos_T *pos;
3370 char_u **matches;
3371 int save_p_scs;
3372 int save_p_ws;
3373 int save_p_ic;
3374 int i;
3375 int num_matches;
3376 int len;
3377 int found_new_match;
3378 int type = ctrl_x_mode;
3379 char_u *ptr;
3380 char_u *dict = NULL;
3381 int dict_f = 0;
3382 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003384 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
3386 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3387 ins_buf->b_scanned = 0;
3388 found_all = FALSE;
3389 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003390 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003391 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 last_match_pos = first_match_pos = *ini;
3393 }
3394
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003395 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003396 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3398 for (;;)
3399 {
3400 found_new_match = FAIL;
3401
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003402 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 * or if found_all says this entry is done. For ^X^L only use the
3404 * entries from 'complete' that look in loaded buffers. */
3405 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003406 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 {
3408 found_all = FALSE;
3409 while (*e_cpt == ',' || *e_cpt == ' ')
3410 e_cpt++;
3411 if (*e_cpt == '.' && !curbuf->b_scanned)
3412 {
3413 ins_buf = curbuf;
3414 first_match_pos = *ini;
3415 /* So that ^N can match word immediately after cursor */
3416 if (ctrl_x_mode == 0)
3417 dec(&first_match_pos);
3418 last_match_pos = first_match_pos;
3419 type = 0;
3420 }
3421 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3422 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3423 {
3424 /* Scan a buffer, but not the current one. */
3425 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3426 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003427 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 first_match_pos.col = last_match_pos.col = 0;
3429 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3430 last_match_pos.lnum = 0;
3431 type = 0;
3432 }
3433 else /* unloaded buffer, scan like dictionary */
3434 {
3435 found_all = TRUE;
3436 if (ins_buf->b_fname == NULL)
3437 continue;
3438 type = CTRL_X_DICTIONARY;
3439 dict = ins_buf->b_fname;
3440 dict_f = DICT_EXACT;
3441 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003442 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 ins_buf->b_fname == NULL
3444 ? buf_spname(ins_buf)
3445 : ins_buf->b_sfname == NULL
3446 ? (char *)ins_buf->b_fname
3447 : (char *)ins_buf->b_sfname);
3448 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3449 }
3450 else if (*e_cpt == NUL)
3451 break;
3452 else
3453 {
3454 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3455 type = -1;
3456 else if (*e_cpt == 'k' || *e_cpt == 's')
3457 {
3458 if (*e_cpt == 'k')
3459 type = CTRL_X_DICTIONARY;
3460 else
3461 type = CTRL_X_THESAURUS;
3462 if (*++e_cpt != ',' && *e_cpt != NUL)
3463 {
3464 dict = e_cpt;
3465 dict_f = DICT_FIRST;
3466 }
3467 }
3468#ifdef FEAT_FIND_ID
3469 else if (*e_cpt == 'i')
3470 type = CTRL_X_PATH_PATTERNS;
3471 else if (*e_cpt == 'd')
3472 type = CTRL_X_PATH_DEFINES;
3473#endif
3474 else if (*e_cpt == ']' || *e_cpt == 't')
3475 {
3476 type = CTRL_X_TAGS;
3477 sprintf((char*)IObuff, _("Scanning tags."));
3478 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3479 }
3480 else
3481 type = -1;
3482
3483 /* in any case e_cpt is advanced to the next entry */
3484 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3485
3486 found_all = TRUE;
3487 if (type == -1)
3488 continue;
3489 }
3490 }
3491
3492 switch (type)
3493 {
3494 case -1:
3495 break;
3496#ifdef FEAT_FIND_ID
3497 case CTRL_X_PATH_PATTERNS:
3498 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003499 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003500 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003502 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3504 (linenr_T)1, (linenr_T)MAXLNUM);
3505 break;
3506#endif
3507
3508 case CTRL_X_DICTIONARY:
3509 case CTRL_X_THESAURUS:
3510 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003511 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 : (type == CTRL_X_THESAURUS
3513 ? (*curbuf->b_p_tsr == NUL
3514 ? p_tsr
3515 : curbuf->b_p_tsr)
3516 : (*curbuf->b_p_dict == NUL
3517 ? p_dict
3518 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003519 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003520 dict != NULL ? dict_f
3521 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 dict = NULL;
3523 break;
3524
3525 case CTRL_X_TAGS:
3526 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3527 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003528 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
3530 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003531 * of matches is found when compl_pattern is empty */
3532 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3534 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3535 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3536 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003537 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
3539 p_ic = save_p_ic;
3540 break;
3541
3542 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003543 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3545 {
3546
3547 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003548 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003549 ins_compl_add_matches(num_matches, matches,
3550#ifdef CASE_INSENSITIVE_FILENAME
3551 TRUE
3552#else
3553 FALSE
3554#endif
3555 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557 break;
3558
3559 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003560 if (expand_cmdline(&compl_xp, compl_pattern,
3561 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003563 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 break;
3565
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003566#ifdef FEAT_COMPL_FUNC
3567 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003568 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003569 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003570 break;
3571#endif
3572
Bram Moolenaar488c6512005-08-11 20:09:58 +00003573 case CTRL_X_SPELL:
3574#ifdef FEAT_SYN_HL
3575 num_matches = expand_spelling(first_match_pos.lnum,
3576 first_match_pos.col, compl_pattern, &matches);
3577 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003578 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003579#endif
3580 break;
3581
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 default: /* normal ^P/^N and ^X^L */
3583 /*
3584 * If 'infercase' is set, don't use 'smartcase' here
3585 */
3586 save_p_scs = p_scs;
3587 if (ins_buf->b_p_inf)
3588 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003589
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 /* buffers other than curbuf are scanned from the beginning or the
3591 * end but never from the middle, thus setting nowrapscan in this
3592 * buffers is a good idea, on the other hand, we always set
3593 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3594 save_p_ws = p_ws;
3595 if (ins_buf != curbuf)
3596 p_ws = FALSE;
3597 else if (*e_cpt == '.')
3598 p_ws = TRUE;
3599 for (;;)
3600 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003601 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003603 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3604 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003606 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003608 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003610 found_new_match = searchit(NULL, ins_buf, pos,
3611 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003612 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003613 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003614 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003616 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003617 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 first_match_pos = *pos;
3619 last_match_pos = *pos;
3620 }
3621 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003622 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 found_new_match = FAIL;
3624 if (found_new_match == FAIL)
3625 {
3626 if (ins_buf == curbuf)
3627 found_all = TRUE;
3628 break;
3629 }
3630
3631 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003632 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 && ini->lnum == pos->lnum
3634 && ini->col == pos->col)
3635 continue;
3636 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3637 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3638 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003639 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
3641 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3642 continue;
3643 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3644 if (!p_paste)
3645 ptr = skipwhite(ptr);
3646 }
3647 len = (int)STRLEN(ptr);
3648 }
3649 else
3650 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003651 char_u *tmp_ptr = ptr;
3652
3653 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003655 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 /* Skip if already inside a word. */
3657 if (vim_iswordp(tmp_ptr))
3658 continue;
3659 /* Find start of next word. */
3660 tmp_ptr = find_word_start(tmp_ptr);
3661 }
3662 /* Find end of this word. */
3663 tmp_ptr = find_word_end(tmp_ptr);
3664 len = (int)(tmp_ptr - ptr);
3665
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003666 if ((compl_cont_status & CONT_ADDING)
3667 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
3669 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3670 {
3671 /* Try next line, if any. the new word will be
3672 * "join" as if the normal command "J" was used.
3673 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003674 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 * works -- Acevedo */
3676 STRNCPY(IObuff, ptr, len);
3677 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3678 tmp_ptr = ptr = skipwhite(ptr);
3679 /* Find start of next word. */
3680 tmp_ptr = find_word_start(tmp_ptr);
3681 /* Find end of next word. */
3682 tmp_ptr = find_word_end(tmp_ptr);
3683 if (tmp_ptr > ptr)
3684 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003685 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003687 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 IObuff[len++] = ' ';
3689 /* IObuf =~ "\k.* ", thus len >= 2 */
3690 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003691 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 || (vim_strchr(p_cpo, CPO_JOINSP)
3693 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003694 && (IObuff[len - 2] == '?'
3695 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 IObuff[len++] = ' ';
3697 }
3698 /* copy as much as posible of the new word */
3699 if (tmp_ptr - ptr >= IOSIZE - len)
3700 tmp_ptr = ptr + IOSIZE - len - 1;
3701 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3702 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003703 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705 IObuff[len] = NUL;
3706 ptr = IObuff;
3707 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003708 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 continue;
3710 }
3711 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003712 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003713 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003714 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
3716 found_new_match = OK;
3717 break;
3718 }
3719 }
3720 p_scs = save_p_scs;
3721 p_ws = save_p_ws;
3722 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003723
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003724 /* check if compl_curr_match has changed, (e.g. other type of
3725 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003726 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 found_new_match = OK;
3728
3729 /* break the loop for specialized modes (use 'complete' just for the
3730 * generic ctrl_x_mode == 0) or when we've found a new match */
3731 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003732 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003733 {
3734 if (got_int)
3735 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003736 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003737 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003738 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003739
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003740 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3741 || compl_interrupted)
3742 break;
3743 compl_started = TRUE;
3744 }
3745 else
3746 {
3747 /* Mark a buffer scanned when it has been scanned completely */
3748 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3749 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003751 compl_started = FALSE;
3752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003754 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755
3756 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3757 && *e_cpt == NUL) /* Got to end of 'complete' */
3758 found_new_match = FAIL;
3759
3760 i = -1; /* total of matches, unknown */
3761 if (found_new_match == FAIL
3762 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3763 i = ins_compl_make_cyclic();
3764
3765 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003766 * just been made cyclic then we have to move compl_curr_match to the next
3767 * or previous entry (if any) -- Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003768 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003769 if (compl_curr_match == NULL)
3770 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 return i;
3772}
3773
3774/* Delete the old text being completed. */
3775 static void
3776ins_compl_delete()
3777{
3778 int i;
3779
3780 /*
3781 * In insert mode: Delete the typed part.
3782 * In replace mode: Put the old characters back, if any.
3783 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003784 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 backspace_until_column(i);
3786 changed_cline_bef_curs();
3787}
3788
3789/* Insert the new text being completed. */
3790 static void
3791ins_compl_insert()
3792{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003793 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003794 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3795 compl_used_match = FALSE;
3796 else
3797 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798}
3799
3800/*
3801 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003802 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3803 * get more completions. If it is FALSE, then we just do nothing when there
3804 * are no more completions in a given direction. The latter case is used when
3805 * we are still in the middle of finding completions, to allow browsing
3806 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 * Return the total number of matches, or -1 if still unknown -- webb.
3808 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003809 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3810 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 *
3812 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003813 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3814 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 */
3816 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003817ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003819 int count; /* repeat completion this many times; should
3820 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003821 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822{
3823 int num_matches = -1;
3824 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003825 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003826 compl_T *found_compl = NULL;
3827 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003829 if (compl_leader != NULL
3830 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003832 /* Set "compl_shown_match" to the actually shown match, it may differ
3833 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003834 while (!ins_compl_equal(compl_shown_match,
3835 compl_leader, STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003836 && compl_shown_match->cp_next != NULL
3837 && compl_shown_match->cp_next != compl_first_match)
3838 compl_shown_match = compl_shown_match->cp_next;
3839 }
3840
3841 if (allow_get_expansion && insert_match
3842 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 /* Delete old text to be replaced */
3844 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003845
Bram Moolenaare3226be2005-12-18 22:10:00 +00003846 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3847 * around. */
3848 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003850 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003852 if (compl_pending != 0)
3853 --compl_pending;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003854 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003855 found_end = (compl_first_match != NULL
3856 && (compl_shown_match->cp_next == compl_first_match
3857 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003858 }
3859 else if (compl_shows_dir == BACKWARD
3860 && compl_shown_match->cp_prev != NULL)
3861 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003862 if (compl_pending != 0)
3863 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00003864 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003865 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003866 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 }
3868 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003869 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003870 if (compl_shows_dir == BACKWARD)
3871 --compl_pending;
3872 else
3873 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00003874 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003875 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003876
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003877 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003878 if (compl_pending != 0 && compl_direction == compl_shows_dir)
Bram Moolenaara6557602006-02-04 22:43:20 +00003879 compl_shown_match = compl_curr_match;
3880 found_end = FALSE;
3881 }
3882 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
3883 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003884 && !ins_compl_equal(compl_shown_match,
3885 compl_leader, STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00003886 ++todo;
3887 else
3888 /* Remember a matching item. */
3889 found_compl = compl_shown_match;
3890
3891 /* Stop at the end of the list when we found a usable match. */
3892 if (found_end)
3893 {
3894 if (found_compl != NULL)
3895 {
3896 compl_shown_match = found_compl;
3897 break;
3898 }
3899 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003903 /* Insert the text of the new completion, or the compl_leader. */
3904 if (insert_match)
3905 {
3906 if (!compl_get_longest || compl_used_match)
3907 ins_compl_insert();
3908 else
3909 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3910 }
3911 else
3912 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
3914 if (!allow_get_expansion)
3915 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003916 /* may undisplay the popup menu first */
3917 ins_compl_upd_pum();
3918
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003919 /* redraw to show the user what was inserted */
3920 update_screen(0);
3921
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003922 /* display the updated popup menu */
3923 ins_compl_show_pum();
3924
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 /* Delete old text to be replaced, since we're still searching and
3926 * don't want to match ourselves! */
3927 ins_compl_delete();
3928 }
3929
3930 /*
3931 * Show the file name for the match (if any)
3932 * Truncate the file name to avoid a wait for return.
3933 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003934 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
3936 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003937 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 if (i <= 0)
3939 i = 0;
3940 else
3941 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003942 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 msg(IObuff);
3944 redraw_cmdline = FALSE; /* don't overwrite! */
3945 }
3946
3947 return num_matches;
3948}
3949
3950/*
3951 * Call this while finding completions, to check whether the user has hit a key
3952 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003953 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00003955 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 */
3957 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00003958ins_compl_check_keys(frequency)
3959 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960{
3961 static int count = 0;
3962
3963 int c;
3964
3965 /* Don't check when reading keys from a script. That would break the test
3966 * scripts */
3967 if (using_script())
3968 return;
3969
3970 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003971 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 return;
3973 count = 0;
3974
3975 ++no_mapping;
3976 c = vpeekc_any();
3977 --no_mapping;
3978 if (c != NUL)
3979 {
3980 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3981 {
3982 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003983 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003984 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
3985 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
3987 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003988 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003990 if (compl_pending != 0 && !got_int)
3991 (void)ins_compl_next(FALSE, compl_pending > 0
3992 ? compl_pending : -compl_pending, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003993}
3994
3995/*
3996 * Decide the direction of Insert mode complete from the key typed.
3997 * Returns BACKWARD or FORWARD.
3998 */
3999 static int
4000ins_compl_key2dir(c)
4001 int c;
4002{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004003 if (c == Ctrl_P || c == Ctrl_L
4004 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4005 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004006 return BACKWARD;
4007 return FORWARD;
4008}
4009
4010/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004011 * Return TRUE for keys that are used for completion only when the popup menu
4012 * is visible.
4013 */
4014 static int
4015ins_compl_pum_key(c)
4016 int c;
4017{
4018 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004019 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4020 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004021}
4022
4023/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004024 * Decide the number of completions to move forward.
4025 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4026 */
4027 static int
4028ins_compl_key2count(c)
4029 int c;
4030{
4031 int h;
4032
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004033 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004034 {
4035 h = pum_get_height();
4036 if (h > 3)
4037 h -= 2; /* keep some context */
4038 return h;
4039 }
4040 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041}
4042
4043/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004044 * Return TRUE if completion with "c" should insert the match, FALSE if only
4045 * to change the currently selected completion.
4046 */
4047 static int
4048ins_compl_use_match(c)
4049 int c;
4050{
4051 switch (c)
4052 {
4053 case K_UP:
4054 case K_DOWN:
4055 case K_PAGEDOWN:
4056 case K_KPAGEDOWN:
4057 case K_S_DOWN:
4058 case K_PAGEUP:
4059 case K_KPAGEUP:
4060 case K_S_UP:
4061 return FALSE;
4062 }
4063 return TRUE;
4064}
4065
4066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 * Do Insert mode completion.
4068 * Called when character "c" was typed, which has a meaning for completion.
4069 * Returns OK if completion was done, FAIL if something failed (out of mem).
4070 */
4071 static int
4072ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004073 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004075 char_u *line;
4076 int startcol = 0; /* column where searched text starts */
4077 colnr_T curs_col; /* cursor column */
4078 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079
Bram Moolenaare3226be2005-12-18 22:10:00 +00004080 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004081 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 {
4083 /* First time we hit ^N or ^P (in a row, I mean) */
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 did_ai = FALSE;
4086#ifdef FEAT_SMARTINDENT
4087 did_si = FALSE;
4088 can_si = FALSE;
4089 can_si_back = FALSE;
4090#endif
4091 if (stop_arrow() == FAIL)
4092 return FAIL;
4093
4094 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004095 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004096 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
4098 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004099 * "compl_startpos" to the cursor as a pattern to add a new word
4100 * instead of expand the one before the cursor, in word-wise if
4101 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 * is not in the same line as the cursor then fix it (the line has
4103 * been split because it was longer than 'tw'). if SOL is set then
4104 * skip the previous pattern, a word at the beginning of the line has
4105 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004106 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4107 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 {
4109 /*
4110 * it is a continued search
4111 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004112 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4114 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4115 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004116 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004118 /* line (probably) wrapped, set compl_startpos to the
4119 * first non_blank in the line, if it is not a wordchar
4120 * include it to get a better pattern, but then we don't
4121 * want the "\\<" prefix, check it bellow */
4122 compl_col = (colnr_T)(skipwhite(line) - line);
4123 compl_startpos.col = compl_col;
4124 compl_startpos.lnum = curwin->w_cursor.lnum;
4125 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
4127 else
4128 {
4129 /* S_IPOS was set when we inserted a word that was at the
4130 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004131 * mode but first we need to redefine compl_startpos */
4132 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004134 compl_cont_status |= CONT_SOL;
4135 compl_startpos.col = (colnr_T)(skipwhite(
4136 line + compl_length
4137 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004139 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004141 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004142 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 * have enough space? just being paranoic */
4144#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004145 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004147 compl_cont_status &= ~CONT_SOL;
4148 compl_length = (IOSIZE - MIN_SPACE);
4149 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004151 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4152 if (compl_length < 1)
4153 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 }
4155 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004156 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004158 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 }
4160 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004161 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004163 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004165 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004167 compl_cont_status = 0;
4168 compl_cont_status |= CONT_N_ADDS;
4169 compl_startpos = curwin->w_cursor;
4170 startcol = (int)curs_col;
4171 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173
4174 /* Work out completion pattern and original text -- webb */
4175 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4176 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004177 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4179 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004180 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004182 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004184 compl_col += ++startcol;
4185 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004188 compl_pattern = str_foldcase(line + compl_col,
4189 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004191 compl_pattern = vim_strnsave(line + compl_col,
4192 compl_length);
4193 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 return FAIL;
4195 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004196 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 {
4198 char_u *prefix = (char_u *)"\\<";
4199
4200 /* we need 3 extra chars, 1 for the NUL and
4201 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004202 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4203 compl_length) + 3);
4204 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004206 if (!vim_iswordp(line + compl_col)
4207 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 && (
4209#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004210 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004212 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213#endif
4214 )))
4215 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004216 STRCPY((char *)compl_pattern, prefix);
4217 (void)quote_meta(compl_pattern + STRLEN(prefix),
4218 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004220 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004222 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004224 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225#endif
4226 )
4227 {
4228 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004229 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4230 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004232 compl_col += curs_col;
4233 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
4235 else
4236 {
4237#ifdef FEAT_MBYTE
4238 /* Search the point of change class of multibyte character
4239 * or not a word single byte character backward. */
4240 if (has_mbyte)
4241 {
4242 int base_class;
4243 int head_off;
4244
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004245 startcol -= (*mb_head_off)(line, line + startcol);
4246 base_class = mb_get_class(line + startcol);
4247 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004249 head_off = (*mb_head_off)(line, line + startcol);
4250 if (base_class != mb_get_class(line + startcol
4251 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004253 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
4255 }
4256 else
4257#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004258 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004260 compl_col += ++startcol;
4261 compl_length = (int)curs_col - startcol;
4262 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 {
4264 /* Only match word with at least two chars -- webb
4265 * there's no need to call quote_meta,
4266 * alloc(7) is enough -- Acevedo
4267 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004268 compl_pattern = alloc(7);
4269 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004271 STRCPY((char *)compl_pattern, "\\<");
4272 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4273 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 else
4276 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004277 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4278 compl_length) + 3);
4279 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004281 STRCPY((char *)compl_pattern, "\\<");
4282 (void)quote_meta(compl_pattern + 2, line + compl_col,
4283 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285 }
4286 }
4287 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4288 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004289 compl_col = skipwhite(line) - line;
4290 compl_length = (int)curs_col - (int)compl_col;
4291 if (compl_length < 0) /* cursor in indent: empty pattern */
4292 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004294 compl_pattern = str_foldcase(line + compl_col, compl_length,
4295 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004297 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4298 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 return FAIL;
4300 }
4301 else if (ctrl_x_mode == CTRL_X_FILES)
4302 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004303 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004305 compl_col += ++startcol;
4306 compl_length = (int)curs_col - startcol;
4307 compl_pattern = addstar(line + compl_col, compl_length,
4308 EXPAND_FILES);
4309 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 return FAIL;
4311 }
4312 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4313 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004314 compl_pattern = vim_strnsave(line, curs_col);
4315 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004317 set_cmd_context(&compl_xp, compl_pattern,
4318 (int)STRLEN(compl_pattern), curs_col);
4319 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4320 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004322 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4323 compl_col = startcol;
4324 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004326 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004327 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004328#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004329 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004330 * Call user defined function 'completefunc' with "a:findstart"
4331 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004332 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004333 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004334 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004335 char_u *funcname;
4336 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004337
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004338 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004339 * string */
4340 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4341 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4342 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004343 {
4344 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4345 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004346 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004347 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004348
4349 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004350 args[1] = NULL;
4351 pos = curwin->w_cursor;
4352 col = call_func_retnr(funcname, 2, args, FALSE);
4353 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004354
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004355 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004356 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004357 compl_col = col;
4358 if ((colnr_T)compl_col > curs_col)
4359 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004360
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004361 /* Setup variables for completion. Need to obtain "line" again,
4362 * it may have become invalid. */
4363 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004364 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004365 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4366 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004367#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004368 return FAIL;
4369 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004370 else if (ctrl_x_mode == CTRL_X_SPELL)
4371 {
4372#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004373 if (spell_bad_len > 0)
4374 compl_col = curs_col - spell_bad_len;
4375 else
4376 compl_col = spell_word_start(startcol);
4377 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004378 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004379 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004380 compl_length = (int)curs_col - compl_col;
4381 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4382 if (compl_pattern == NULL)
4383#endif
4384 return FAIL;
4385 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004386 else
4387 {
4388 EMSG2(_(e_intern2), "ins_complete()");
4389 return FAIL;
4390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 {
4394 edit_submode_pre = (char_u *)_(" Adding");
4395 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4396 {
4397 /* Insert a new line, keep indentation but ignore 'comments' */
4398#ifdef FEAT_COMMENTS
4399 char_u *old = curbuf->b_p_com;
4400
4401 curbuf->b_p_com = (char_u *)"";
4402#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004403 compl_startpos.lnum = curwin->w_cursor.lnum;
4404 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 ins_eol('\r');
4406#ifdef FEAT_COMMENTS
4407 curbuf->b_p_com = old;
4408#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004409 compl_length = 0;
4410 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 }
4412 }
4413 else
4414 {
4415 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004416 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
4418
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004419 if (compl_cont_status & CONT_LOCAL)
4420 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 else
4422 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4423
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004424 /* Always add completion for the original text. */
4425 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004426 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4427 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004428 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430 vim_free(compl_pattern);
4431 compl_pattern = NULL;
4432 vim_free(compl_orig_text);
4433 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 return FAIL;
4435 }
4436
4437 /* showmode might reset the internal line pointers, so it must
4438 * be called before line = ml_get(), or when this address is no
4439 * longer needed. -- Acevedo.
4440 */
4441 edit_submode_extra = (char_u *)_("-- Searching...");
4442 edit_submode_highl = HLF_COUNT;
4443 showmode();
4444 edit_submode_extra = NULL;
4445 out_flush();
4446 }
4447
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004448 compl_shown_match = compl_curr_match;
4449 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
4451 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004452 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004454 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004456 /* may undisplay the popup menu */
4457 ins_compl_upd_pum();
4458
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004459 if (n > 1) /* all matches have been found */
4460 compl_matches = n;
4461 compl_curr_match = compl_shown_match;
4462 compl_direction = compl_shows_dir;
4463 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
4465 /* eat the ESC to avoid leaving insert mode */
4466 if (got_int && !global_busy)
4467 {
4468 (void)vgetc();
4469 got_int = FALSE;
4470 }
4471
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004472 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004473 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004475 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4476 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4478 edit_submode_highl = HLF_E;
4479 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4480 * because we couldn't expand anything at first place, but if we used
4481 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4482 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004483 if ( compl_length > 1
4484 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 || (ctrl_x_mode != 0
4486 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4487 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004488 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490
Bram Moolenaar572cb562005-08-05 21:35:02 +00004491 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004492 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004494 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495
4496 if (edit_submode_extra == NULL)
4497 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004498 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 {
4500 edit_submode_extra = (char_u *)_("Back at original");
4501 edit_submode_highl = HLF_W;
4502 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004503 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
4505 edit_submode_extra = (char_u *)_("Word from other line");
4506 edit_submode_highl = HLF_COUNT;
4507 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004508 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
4510 edit_submode_extra = (char_u *)_("The only match");
4511 edit_submode_highl = HLF_COUNT;
4512 }
4513 else
4514 {
4515 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004516 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004518 int number = 0;
4519 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004521 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 {
4523 /* search backwards for the first valid (!= -1) number.
4524 * This should normally succeed already at the first loop
4525 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004526 for (match = compl_curr_match->cp_prev; match != NULL
4527 && match != compl_first_match;
4528 match = match->cp_prev)
4529 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004531 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 break;
4533 }
4534 if (match != NULL)
4535 /* go up and assign all numbers which are not assigned
4536 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004537 for (match = match->cp_next;
4538 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004539 match = match->cp_next)
4540 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 }
4542 else /* BACKWARD */
4543 {
4544 /* search forwards (upwards) for the first valid (!= -1)
4545 * number. This should normally succeed already at the
4546 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004547 for (match = compl_curr_match->cp_next; match != NULL
4548 && match != compl_first_match;
4549 match = match->cp_next)
4550 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004552 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 break;
4554 }
4555 if (match != NULL)
4556 /* go down and assign all numbers which are not
4557 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004558 for (match = match->cp_prev; match
4559 && match->cp_number == -1;
4560 match = match->cp_prev)
4561 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 }
4563 }
4564
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004565 /* The match should always have a sequence number now, this is
4566 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004567 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
4569 /* Space for 10 text chars. + 2x10-digit no.s */
4570 static char_u match_ref[31];
4571
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004572 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004574 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004576 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004577 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004578 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 edit_submode_extra = match_ref;
4580 edit_submode_highl = HLF_R;
4581 if (dollar_vcol)
4582 curs_columns(FALSE);
4583 }
4584 }
4585 }
4586
4587 /* Show a message about what (completion) mode we're in. */
4588 showmode();
4589 if (edit_submode_extra != NULL)
4590 {
4591 if (!p_smd)
4592 msg_attr(edit_submode_extra,
4593 edit_submode_highl < HLF_COUNT
4594 ? hl_attr(edit_submode_highl) : 0);
4595 }
4596 else
4597 msg_clr_cmdline(); /* necessary for "noshowmode" */
4598
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004599 ins_compl_show_pum();
4600
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 return OK;
4602}
4603
4604/*
4605 * Looks in the first "len" chars. of "src" for search-metachars.
4606 * If dest is not NULL the chars. are copied there quoting (with
4607 * a backslash) the metachars, and dest would be NUL terminated.
4608 * Returns the length (needed) of dest
4609 */
4610 static int
4611quote_meta(dest, src, len)
4612 char_u *dest;
4613 char_u *src;
4614 int len;
4615{
4616 int m;
4617
4618 for (m = len; --len >= 0; src++)
4619 {
4620 switch (*src)
4621 {
4622 case '.':
4623 case '*':
4624 case '[':
4625 if (ctrl_x_mode == CTRL_X_DICTIONARY
4626 || ctrl_x_mode == CTRL_X_THESAURUS)
4627 break;
4628 case '~':
4629 if (!p_magic) /* quote these only if magic is set */
4630 break;
4631 case '\\':
4632 if (ctrl_x_mode == CTRL_X_DICTIONARY
4633 || ctrl_x_mode == CTRL_X_THESAURUS)
4634 break;
4635 case '^': /* currently it's not needed. */
4636 case '$':
4637 m++;
4638 if (dest != NULL)
4639 *dest++ = '\\';
4640 break;
4641 }
4642 if (dest != NULL)
4643 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004644# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 /* Copy remaining bytes of a multibyte character. */
4646 if (has_mbyte)
4647 {
4648 int i, mb_len;
4649
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004650 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 if (mb_len > 0 && len >= mb_len)
4652 for (i = 0; i < mb_len; ++i)
4653 {
4654 --len;
4655 ++src;
4656 if (dest != NULL)
4657 *dest++ = *src;
4658 }
4659 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004660# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 }
4662 if (dest != NULL)
4663 *dest = NUL;
4664
4665 return m;
4666}
4667#endif /* FEAT_INS_EXPAND */
4668
4669/*
4670 * Next character is interpreted literally.
4671 * A one, two or three digit decimal number is interpreted as its byte value.
4672 * If one or two digits are entered, the next character is given to vungetc().
4673 * For Unicode a character > 255 may be returned.
4674 */
4675 int
4676get_literal()
4677{
4678 int cc;
4679 int nc;
4680 int i;
4681 int hex = FALSE;
4682 int octal = FALSE;
4683#ifdef FEAT_MBYTE
4684 int unicode = 0;
4685#endif
4686
4687 if (got_int)
4688 return Ctrl_C;
4689
4690#ifdef FEAT_GUI
4691 /*
4692 * In GUI there is no point inserting the internal code for a special key.
4693 * It is more useful to insert the string "<KEY>" instead. This would
4694 * probably be useful in a text window too, but it would not be
4695 * vi-compatible (maybe there should be an option for it?) -- webb
4696 */
4697 if (gui.in_use)
4698 ++allow_keys;
4699#endif
4700#ifdef USE_ON_FLY_SCROLL
4701 dont_scroll = TRUE; /* disallow scrolling here */
4702#endif
4703 ++no_mapping; /* don't map the next key hits */
4704 cc = 0;
4705 i = 0;
4706 for (;;)
4707 {
4708 do
4709 nc = safe_vgetc();
4710 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4711 || nc == K_HOR_SCROLLBAR);
4712#ifdef FEAT_CMDL_INFO
4713 if (!(State & CMDLINE)
4714# ifdef FEAT_MBYTE
4715 && MB_BYTE2LEN_CHECK(nc) == 1
4716# endif
4717 )
4718 add_to_showcmd(nc);
4719#endif
4720 if (nc == 'x' || nc == 'X')
4721 hex = TRUE;
4722 else if (nc == 'o' || nc == 'O')
4723 octal = TRUE;
4724#ifdef FEAT_MBYTE
4725 else if (nc == 'u' || nc == 'U')
4726 unicode = nc;
4727#endif
4728 else
4729 {
4730 if (hex
4731#ifdef FEAT_MBYTE
4732 || unicode != 0
4733#endif
4734 )
4735 {
4736 if (!vim_isxdigit(nc))
4737 break;
4738 cc = cc * 16 + hex2nr(nc);
4739 }
4740 else if (octal)
4741 {
4742 if (nc < '0' || nc > '7')
4743 break;
4744 cc = cc * 8 + nc - '0';
4745 }
4746 else
4747 {
4748 if (!VIM_ISDIGIT(nc))
4749 break;
4750 cc = cc * 10 + nc - '0';
4751 }
4752
4753 ++i;
4754 }
4755
4756 if (cc > 255
4757#ifdef FEAT_MBYTE
4758 && unicode == 0
4759#endif
4760 )
4761 cc = 255; /* limit range to 0-255 */
4762 nc = 0;
4763
4764 if (hex) /* hex: up to two chars */
4765 {
4766 if (i >= 2)
4767 break;
4768 }
4769#ifdef FEAT_MBYTE
4770 else if (unicode) /* Unicode: up to four or eight chars */
4771 {
4772 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4773 break;
4774 }
4775#endif
4776 else if (i >= 3) /* decimal or octal: up to three chars */
4777 break;
4778 }
4779 if (i == 0) /* no number entered */
4780 {
4781 if (nc == K_ZERO) /* NUL is stored as NL */
4782 {
4783 cc = '\n';
4784 nc = 0;
4785 }
4786 else
4787 {
4788 cc = nc;
4789 nc = 0;
4790 }
4791 }
4792
4793 if (cc == 0) /* NUL is stored as NL */
4794 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004795#ifdef FEAT_MBYTE
4796 if (enc_dbcs && (cc & 0xff) == 0)
4797 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4798 second byte will cause trouble! */
4799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
4801 --no_mapping;
4802#ifdef FEAT_GUI
4803 if (gui.in_use)
4804 --allow_keys;
4805#endif
4806 if (nc)
4807 vungetc(nc);
4808 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4809 return cc;
4810}
4811
4812/*
4813 * Insert character, taking care of special keys and mod_mask
4814 */
4815 static void
4816insert_special(c, allow_modmask, ctrlv)
4817 int c;
4818 int allow_modmask;
4819 int ctrlv; /* c was typed after CTRL-V */
4820{
4821 char_u *p;
4822 int len;
4823
4824 /*
4825 * Special function key, translate into "<Key>". Up to the last '>' is
4826 * inserted with ins_str(), so as not to replace characters in replace
4827 * mode.
4828 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4829 * unless 'allow_modmask' is TRUE.
4830 */
4831#ifdef MACOS
4832 /* Command-key never produces a normal key */
4833 if (mod_mask & MOD_MASK_CMD)
4834 allow_modmask = TRUE;
4835#endif
4836 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4837 {
4838 p = get_special_key_name(c, mod_mask);
4839 len = (int)STRLEN(p);
4840 c = p[len - 1];
4841 if (len > 2)
4842 {
4843 if (stop_arrow() == FAIL)
4844 return;
4845 p[len - 1] = NUL;
4846 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004847 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 ctrlv = FALSE;
4849 }
4850 }
4851 if (stop_arrow() == OK)
4852 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4853}
4854
4855/*
4856 * Special characters in this context are those that need processing other
4857 * than the simple insertion that can be performed here. This includes ESC
4858 * which terminates the insert, and CR/NL which need special processing to
4859 * open up a new line. This routine tries to optimize insertions performed by
4860 * the "redo", "undo" or "put" commands, so it needs to know when it should
4861 * stop and defer processing to the "normal" mechanism.
4862 * '0' and '^' are special, because they can be followed by CTRL-D.
4863 */
4864#ifdef EBCDIC
4865# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4866#else
4867# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4868#endif
4869
4870#ifdef FEAT_MBYTE
4871# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4872#else
4873# define WHITECHAR(cc) vim_iswhite(cc)
4874#endif
4875
4876 void
4877insertchar(c, flags, second_indent)
4878 int c; /* character to insert or NUL */
4879 int flags; /* INSCHAR_FORMAT, etc. */
4880 int second_indent; /* indent for second line if >= 0 */
4881{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 int textwidth;
4883#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887
4888 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4889 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890
4891 /*
4892 * Try to break the line in two or more pieces when:
4893 * - Always do this if we have been called to do formatting only.
4894 * - Always do this when 'formatoptions' has the 'a' flag and the line
4895 * ends in white space.
4896 * - Otherwise:
4897 * - Don't do this if inserting a blank
4898 * - Don't do this if an existing character is being replaced, unless
4899 * we're in VREPLACE mode.
4900 * - Do this if the cursor is not on the line where insert started
4901 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4902 * before the insert.
4903 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4904 * before 'textwidth'
4905 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004906 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 && ((flags & INSCHAR_FORMAT)
4908 || (!vim_iswhite(c)
4909 && !((State & REPLACE_FLAG)
4910#ifdef FEAT_VREPLACE
4911 && !(State & VREPLACE_FLAG)
4912#endif
4913 && *ml_get_cursor() != NUL)
4914 && (curwin->w_cursor.lnum != Insstart.lnum
4915 || ((!has_format_option(FO_INS_LONG)
4916 || Insstart_textlen <= (colnr_T)textwidth)
4917 && (!fo_ins_blank
4918 || Insstart_blank_vcol <= (colnr_T)textwidth
4919 ))))))
4920 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004921 /* Format with 'formatexpr' when it's set. Use internal formatting
4922 * when 'formatexpr' isn't set or it returns non-zero. */
4923#if defined(FEAT_EVAL)
4924 if (*curbuf->b_p_fex == NUL
4925 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004927 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004929
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 if (c == NUL) /* only formatting was wanted */
4931 return;
4932
4933#ifdef FEAT_COMMENTS
4934 /* Check whether this character should end a comment. */
4935 if (did_ai && (int)c == end_comment_pending)
4936 {
4937 char_u *line;
4938 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4939 int middle_len, end_len;
4940 int i;
4941
4942 /*
4943 * Need to remove existing (middle) comment leader and insert end
4944 * comment leader. First, check what comment leader we can find.
4945 */
4946 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4947 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4948 {
4949 /* Skip middle-comment string */
4950 while (*p && p[-1] != ':') /* find end of middle flags */
4951 ++p;
4952 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4953 /* Don't count trailing white space for middle_len */
4954 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4955 --middle_len;
4956
4957 /* Find the end-comment string */
4958 while (*p && p[-1] != ':') /* find end of end flags */
4959 ++p;
4960 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4961
4962 /* Skip white space before the cursor */
4963 i = curwin->w_cursor.col;
4964 while (--i >= 0 && vim_iswhite(line[i]))
4965 ;
4966 i++;
4967
4968 /* Skip to before the middle leader */
4969 i -= middle_len;
4970
4971 /* Check some expected things before we go on */
4972 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4973 {
4974 /* Backspace over all the stuff we want to replace */
4975 backspace_until_column(i);
4976
4977 /*
4978 * Insert the end-comment string, except for the last
4979 * character, which will get inserted as normal later.
4980 */
4981 ins_bytes_len(lead_end, end_len - 1);
4982 }
4983 }
4984 }
4985 end_comment_pending = NUL;
4986#endif
4987
4988 did_ai = FALSE;
4989#ifdef FEAT_SMARTINDENT
4990 did_si = FALSE;
4991 can_si = FALSE;
4992 can_si_back = FALSE;
4993#endif
4994
4995 /*
4996 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4997 * This speeds up normal text input considerably.
4998 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4999 * need to re-indent at a ':', or any other character (but not what
5000 * 'paste' is set)..
5001 */
5002#ifdef USE_ON_FLY_SCROLL
5003 dont_scroll = FALSE; /* allow scrolling here */
5004#endif
5005
5006 if ( !ISSPECIAL(c)
5007#ifdef FEAT_MBYTE
5008 && (!has_mbyte || (*mb_char2len)(c) == 1)
5009#endif
5010 && vpeekc() != NUL
5011 && !(State & REPLACE_FLAG)
5012#ifdef FEAT_CINDENT
5013 && !cindent_on()
5014#endif
5015#ifdef FEAT_RIGHTLEFT
5016 && !p_ri
5017#endif
5018 )
5019 {
5020#define INPUT_BUFLEN 100
5021 char_u buf[INPUT_BUFLEN + 1];
5022 int i;
5023 colnr_T virtcol = 0;
5024
5025 buf[0] = c;
5026 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005027 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 virtcol = get_nolist_virtcol();
5029 /*
5030 * Stop the string when:
5031 * - no more chars available
5032 * - finding a special character (command key)
5033 * - buffer is full
5034 * - running into the 'textwidth' boundary
5035 * - need to check for abbreviation: A non-word char after a word-char
5036 */
5037 while ( (c = vpeekc()) != NUL
5038 && !ISSPECIAL(c)
5039#ifdef FEAT_MBYTE
5040 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5041#endif
5042 && i < INPUT_BUFLEN
5043 && (textwidth == 0
5044 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5045 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5046 {
5047#ifdef FEAT_RIGHTLEFT
5048 c = vgetc();
5049 if (p_hkmap && KeyTyped)
5050 c = hkmap(c); /* Hebrew mode mapping */
5051# ifdef FEAT_FKMAP
5052 if (p_fkmap && KeyTyped)
5053 c = fkmap(c); /* Farsi mode mapping */
5054# endif
5055 buf[i++] = c;
5056#else
5057 buf[i++] = vgetc();
5058#endif
5059 }
5060
5061#ifdef FEAT_DIGRAPHS
5062 do_digraph(-1); /* clear digraphs */
5063 do_digraph(buf[i-1]); /* may be the start of a digraph */
5064#endif
5065 buf[i] = NUL;
5066 ins_str(buf);
5067 if (flags & INSCHAR_CTRLV)
5068 {
5069 redo_literal(*buf);
5070 i = 1;
5071 }
5072 else
5073 i = 0;
5074 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005075 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077 else
5078 {
5079#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005080 int cc;
5081
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5083 {
5084 char_u buf[MB_MAXBYTES + 1];
5085
5086 (*mb_char2bytes)(c, buf);
5087 buf[cc] = NUL;
5088 ins_char_bytes(buf, cc);
5089 AppendCharToRedobuff(c);
5090 }
5091 else
5092#endif
5093 {
5094 ins_char(c);
5095 if (flags & INSCHAR_CTRLV)
5096 redo_literal(c);
5097 else
5098 AppendCharToRedobuff(c);
5099 }
5100 }
5101}
5102
5103/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005104 * Format text at the current insert position.
5105 */
5106 static void
5107internal_format(textwidth, second_indent, flags, format_only)
5108 int textwidth;
5109 int second_indent;
5110 int flags;
5111 int format_only;
5112{
5113 int cc;
5114 int save_char = NUL;
5115 int haveto_redraw = FALSE;
5116 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5117#ifdef FEAT_MBYTE
5118 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5119#endif
5120 int fo_white_par = has_format_option(FO_WHITE_PAR);
5121 int first_line = TRUE;
5122#ifdef FEAT_COMMENTS
5123 colnr_T leader_len;
5124 int no_leader = FALSE;
5125 int do_comments = (flags & INSCHAR_DO_COM);
5126#endif
5127
5128 /*
5129 * When 'ai' is off we don't want a space under the cursor to be
5130 * deleted. Replace it with an 'x' temporarily.
5131 */
5132 if (!curbuf->b_p_ai)
5133 {
5134 cc = gchar_cursor();
5135 if (vim_iswhite(cc))
5136 {
5137 save_char = cc;
5138 pchar_cursor('x');
5139 }
5140 }
5141
5142 /*
5143 * Repeat breaking lines, until the current line is not too long.
5144 */
5145 while (!got_int)
5146 {
5147 int startcol; /* Cursor column at entry */
5148 int wantcol; /* column at textwidth border */
5149 int foundcol; /* column for start of spaces */
5150 int end_foundcol = 0; /* column for start of word */
5151 colnr_T len;
5152 colnr_T virtcol;
5153#ifdef FEAT_VREPLACE
5154 int orig_col = 0;
5155 char_u *saved_text = NULL;
5156#endif
5157 colnr_T col;
5158
5159 virtcol = get_nolist_virtcol();
5160 if (virtcol < (colnr_T)textwidth)
5161 break;
5162
5163#ifdef FEAT_COMMENTS
5164 if (no_leader)
5165 do_comments = FALSE;
5166 else if (!(flags & INSCHAR_FORMAT)
5167 && has_format_option(FO_WRAP_COMS))
5168 do_comments = TRUE;
5169
5170 /* Don't break until after the comment leader */
5171 if (do_comments)
5172 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5173 else
5174 leader_len = 0;
5175
5176 /* If the line doesn't start with a comment leader, then don't
5177 * start one in a following broken line. Avoids that a %word
5178 * moved to the start of the next line causes all following lines
5179 * to start with %. */
5180 if (leader_len == 0)
5181 no_leader = TRUE;
5182#endif
5183 if (!(flags & INSCHAR_FORMAT)
5184#ifdef FEAT_COMMENTS
5185 && leader_len == 0
5186#endif
5187 && !has_format_option(FO_WRAP))
5188
5189 {
5190 textwidth = 0;
5191 break;
5192 }
5193 if ((startcol = curwin->w_cursor.col) == 0)
5194 break;
5195
5196 /* find column of textwidth border */
5197 coladvance((colnr_T)textwidth);
5198 wantcol = curwin->w_cursor.col;
5199
5200 curwin->w_cursor.col = startcol - 1;
5201#ifdef FEAT_MBYTE
5202 /* Correct cursor for multi-byte character. */
5203 if (has_mbyte)
5204 mb_adjust_cursor();
5205#endif
5206 foundcol = 0;
5207
5208 /*
5209 * Find position to break at.
5210 * Stop at first entered white when 'formatoptions' has 'v'
5211 */
5212 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5213 || curwin->w_cursor.lnum != Insstart.lnum
5214 || curwin->w_cursor.col >= Insstart.col)
5215 {
5216 cc = gchar_cursor();
5217 if (WHITECHAR(cc))
5218 {
5219 /* remember position of blank just before text */
5220 end_foundcol = curwin->w_cursor.col;
5221
5222 /* find start of sequence of blanks */
5223 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5224 {
5225 dec_cursor();
5226 cc = gchar_cursor();
5227 }
5228 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5229 break; /* only spaces in front of text */
5230#ifdef FEAT_COMMENTS
5231 /* Don't break until after the comment leader */
5232 if (curwin->w_cursor.col < leader_len)
5233 break;
5234#endif
5235 if (has_format_option(FO_ONE_LETTER))
5236 {
5237 /* do not break after one-letter words */
5238 if (curwin->w_cursor.col == 0)
5239 break; /* one-letter word at begin */
5240
5241 col = curwin->w_cursor.col;
5242 dec_cursor();
5243 cc = gchar_cursor();
5244
5245 if (WHITECHAR(cc))
5246 continue; /* one-letter, continue */
5247 curwin->w_cursor.col = col;
5248 }
5249#ifdef FEAT_MBYTE
5250 if (has_mbyte)
5251 foundcol = curwin->w_cursor.col
5252 + (*mb_ptr2len)(ml_get_cursor());
5253 else
5254#endif
5255 foundcol = curwin->w_cursor.col + 1;
5256 if (curwin->w_cursor.col < (colnr_T)wantcol)
5257 break;
5258 }
5259#ifdef FEAT_MBYTE
5260 else if (cc >= 0x100 && fo_multibyte
5261 && curwin->w_cursor.col <= (colnr_T)wantcol)
5262 {
5263 /* Break after or before a multi-byte character. */
5264 foundcol = curwin->w_cursor.col;
5265 if (curwin->w_cursor.col < (colnr_T)wantcol)
5266 foundcol += (*mb_char2len)(cc);
5267 end_foundcol = foundcol;
5268 break;
5269 }
5270#endif
5271 if (curwin->w_cursor.col == 0)
5272 break;
5273 dec_cursor();
5274 }
5275
5276 if (foundcol == 0) /* no spaces, cannot break line */
5277 {
5278 curwin->w_cursor.col = startcol;
5279 break;
5280 }
5281
5282 /* Going to break the line, remove any "$" now. */
5283 undisplay_dollar();
5284
5285 /*
5286 * Offset between cursor position and line break is used by replace
5287 * stack functions. VREPLACE does not use this, and backspaces
5288 * over the text instead.
5289 */
5290#ifdef FEAT_VREPLACE
5291 if (State & VREPLACE_FLAG)
5292 orig_col = startcol; /* Will start backspacing from here */
5293 else
5294#endif
5295 replace_offset = startcol - end_foundcol - 1;
5296
5297 /*
5298 * adjust startcol for spaces that will be deleted and
5299 * characters that will remain on top line
5300 */
5301 curwin->w_cursor.col = foundcol;
5302 while (cc = gchar_cursor(), WHITECHAR(cc))
5303 inc_cursor();
5304 startcol -= curwin->w_cursor.col;
5305 if (startcol < 0)
5306 startcol = 0;
5307
5308#ifdef FEAT_VREPLACE
5309 if (State & VREPLACE_FLAG)
5310 {
5311 /*
5312 * In VREPLACE mode, we will backspace over the text to be
5313 * wrapped, so save a copy now to put on the next line.
5314 */
5315 saved_text = vim_strsave(ml_get_cursor());
5316 curwin->w_cursor.col = orig_col;
5317 if (saved_text == NULL)
5318 break; /* Can't do it, out of memory */
5319 saved_text[startcol] = NUL;
5320
5321 /* Backspace over characters that will move to the next line */
5322 if (!fo_white_par)
5323 backspace_until_column(foundcol);
5324 }
5325 else
5326#endif
5327 {
5328 /* put cursor after pos. to break line */
5329 if (!fo_white_par)
5330 curwin->w_cursor.col = foundcol;
5331 }
5332
5333 /*
5334 * Split the line just before the margin.
5335 * Only insert/delete lines, but don't really redraw the window.
5336 */
5337 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5338 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5339#ifdef FEAT_COMMENTS
5340 + (do_comments ? OPENLINE_DO_COM : 0)
5341#endif
5342 , old_indent);
5343 old_indent = 0;
5344
5345 replace_offset = 0;
5346 if (first_line)
5347 {
5348 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5349 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5350 if (second_indent >= 0)
5351 {
5352#ifdef FEAT_VREPLACE
5353 if (State & VREPLACE_FLAG)
5354 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5355 else
5356#endif
5357 (void)set_indent(second_indent, SIN_CHANGED);
5358 }
5359 first_line = FALSE;
5360 }
5361
5362#ifdef FEAT_VREPLACE
5363 if (State & VREPLACE_FLAG)
5364 {
5365 /*
5366 * In VREPLACE mode we have backspaced over the text to be
5367 * moved, now we re-insert it into the new line.
5368 */
5369 ins_bytes(saved_text);
5370 vim_free(saved_text);
5371 }
5372 else
5373#endif
5374 {
5375 /*
5376 * Check if cursor is not past the NUL off the line, cindent
5377 * may have added or removed indent.
5378 */
5379 curwin->w_cursor.col += startcol;
5380 len = (colnr_T)STRLEN(ml_get_curline());
5381 if (curwin->w_cursor.col > len)
5382 curwin->w_cursor.col = len;
5383 }
5384
5385 haveto_redraw = TRUE;
5386#ifdef FEAT_CINDENT
5387 can_cindent = TRUE;
5388#endif
5389 /* moved the cursor, don't autoindent or cindent now */
5390 did_ai = FALSE;
5391#ifdef FEAT_SMARTINDENT
5392 did_si = FALSE;
5393 can_si = FALSE;
5394 can_si_back = FALSE;
5395#endif
5396 line_breakcheck();
5397 }
5398
5399 if (save_char != NUL) /* put back space after cursor */
5400 pchar_cursor(save_char);
5401
5402 if (!format_only && haveto_redraw)
5403 {
5404 update_topline();
5405 redraw_curbuf_later(VALID);
5406 }
5407}
5408
5409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 * Called after inserting or deleting text: When 'formatoptions' includes the
5411 * 'a' flag format from the current line until the end of the paragraph.
5412 * Keep the cursor at the same position relative to the text.
5413 * The caller must have saved the cursor line for undo, following ones will be
5414 * saved here.
5415 */
5416 void
5417auto_format(trailblank, prev_line)
5418 int trailblank; /* when TRUE also format with trailing blank */
5419 int prev_line; /* may start in previous line */
5420{
5421 pos_T pos;
5422 colnr_T len;
5423 char_u *old;
5424 char_u *new, *pnew;
5425 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005426 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427
5428 if (!has_format_option(FO_AUTO))
5429 return;
5430
5431 pos = curwin->w_cursor;
5432 old = ml_get_curline();
5433
5434 /* may remove added space */
5435 check_auto_format(FALSE);
5436
5437 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5438 * user might insert normal text next. Also skip formatting when "1" is
5439 * in 'formatoptions' and there is a single character before the cursor.
5440 * Otherwise the line would be broken and when typing another non-white
5441 * next they are not joined back together. */
5442 wasatend = (pos.col == STRLEN(old));
5443 if (*old != NUL && !trailblank && wasatend)
5444 {
5445 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005446 cc = gchar_cursor();
5447 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5448 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005450 cc = gchar_cursor();
5451 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 {
5453 curwin->w_cursor = pos;
5454 return;
5455 }
5456 curwin->w_cursor = pos;
5457 }
5458
5459#ifdef FEAT_COMMENTS
5460 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5461 * comments. */
5462 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5463 && get_leader_len(old, NULL, FALSE) == 0)
5464 return;
5465#endif
5466
5467 /*
5468 * May start formatting in a previous line, so that after "x" a word is
5469 * moved to the previous line if it fits there now. Only when this is not
5470 * the start of a paragraph.
5471 */
5472 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5473 {
5474 --curwin->w_cursor.lnum;
5475 if (u_save_cursor() == FAIL)
5476 return;
5477 }
5478
5479 /*
5480 * Do the formatting and restore the cursor position. "saved_cursor" will
5481 * be adjusted for the text formatting.
5482 */
5483 saved_cursor = pos;
5484 format_lines((linenr_T)-1);
5485 curwin->w_cursor = saved_cursor;
5486 saved_cursor.lnum = 0;
5487
5488 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5489 {
5490 /* "cannot happen" */
5491 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5492 coladvance((colnr_T)MAXCOL);
5493 }
5494 else
5495 check_cursor_col();
5496
5497 /* Insert mode: If the cursor is now after the end of the line while it
5498 * previously wasn't, the line was broken. Because of the rule above we
5499 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5500 * formatted. */
5501 if (!wasatend && has_format_option(FO_WHITE_PAR))
5502 {
5503 new = ml_get_curline();
5504 len = STRLEN(new);
5505 if (curwin->w_cursor.col == len)
5506 {
5507 pnew = vim_strnsave(new, len + 2);
5508 pnew[len] = ' ';
5509 pnew[len + 1] = NUL;
5510 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5511 /* remove the space later */
5512 did_add_space = TRUE;
5513 }
5514 else
5515 /* may remove added space */
5516 check_auto_format(FALSE);
5517 }
5518
5519 check_cursor();
5520}
5521
5522/*
5523 * When an extra space was added to continue a paragraph for auto-formatting,
5524 * delete it now. The space must be under the cursor, just after the insert
5525 * position.
5526 */
5527 static void
5528check_auto_format(end_insert)
5529 int end_insert; /* TRUE when ending Insert mode */
5530{
5531 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005532 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
5534 if (did_add_space)
5535 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005536 cc = gchar_cursor();
5537 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 /* Somehow the space was removed already. */
5539 did_add_space = FALSE;
5540 else
5541 {
5542 if (!end_insert)
5543 {
5544 inc_cursor();
5545 c = gchar_cursor();
5546 dec_cursor();
5547 }
5548 if (c != NUL)
5549 {
5550 /* The space is no longer at the end of the line, delete it. */
5551 del_char(FALSE);
5552 did_add_space = FALSE;
5553 }
5554 }
5555 }
5556}
5557
5558/*
5559 * Find out textwidth to be used for formatting:
5560 * if 'textwidth' option is set, use it
5561 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5562 * if invalid value, use 0.
5563 * Set default to window width (maximum 79) for "gq" operator.
5564 */
5565 int
5566comp_textwidth(ff)
5567 int ff; /* force formatting (for "Q" command) */
5568{
5569 int textwidth;
5570
5571 textwidth = curbuf->b_p_tw;
5572 if (textwidth == 0 && curbuf->b_p_wm)
5573 {
5574 /* The width is the window width minus 'wrapmargin' minus all the
5575 * things that add to the margin. */
5576 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5577#ifdef FEAT_CMDWIN
5578 if (cmdwin_type != 0)
5579 textwidth -= 1;
5580#endif
5581#ifdef FEAT_FOLDING
5582 textwidth -= curwin->w_p_fdc;
5583#endif
5584#ifdef FEAT_SIGNS
5585 if (curwin->w_buffer->b_signlist != NULL
5586# ifdef FEAT_NETBEANS_INTG
5587 || usingNetbeans
5588# endif
5589 )
5590 textwidth -= 1;
5591#endif
5592 if (curwin->w_p_nu)
5593 textwidth -= 8;
5594 }
5595 if (textwidth < 0)
5596 textwidth = 0;
5597 if (ff && textwidth == 0)
5598 {
5599 textwidth = W_WIDTH(curwin) - 1;
5600 if (textwidth > 79)
5601 textwidth = 79;
5602 }
5603 return textwidth;
5604}
5605
5606/*
5607 * Put a character in the redo buffer, for when just after a CTRL-V.
5608 */
5609 static void
5610redo_literal(c)
5611 int c;
5612{
5613 char_u buf[10];
5614
5615 /* Only digits need special treatment. Translate them into a string of
5616 * three digits. */
5617 if (VIM_ISDIGIT(c))
5618 {
5619 sprintf((char *)buf, "%03d", c);
5620 AppendToRedobuff(buf);
5621 }
5622 else
5623 AppendCharToRedobuff(c);
5624}
5625
5626/*
5627 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005628 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 */
5630 static void
5631start_arrow(end_insert_pos)
5632 pos_T *end_insert_pos;
5633{
5634 if (!arrow_used) /* something has been inserted */
5635 {
5636 AppendToRedobuff(ESC_STR);
5637 stop_insert(end_insert_pos, FALSE);
5638 arrow_used = TRUE; /* this means we stopped the current insert */
5639 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005640#ifdef FEAT_SYN_HL
5641 check_spell_redraw();
5642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643}
5644
Bram Moolenaar217ad922005-03-20 22:37:15 +00005645#ifdef FEAT_SYN_HL
5646/*
5647 * If we skipped highlighting word at cursor, do it now.
5648 * It may be skipped again, thus reset spell_redraw_lnum first.
5649 */
5650 static void
5651check_spell_redraw()
5652{
5653 if (spell_redraw_lnum != 0)
5654 {
5655 linenr_T lnum = spell_redraw_lnum;
5656
5657 spell_redraw_lnum = 0;
5658 redrawWinline(lnum, FALSE);
5659 }
5660}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005661
5662/*
5663 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5664 * spelled word, if there is one.
5665 */
5666 static void
5667spell_back_to_badword()
5668{
5669 pos_T tpos = curwin->w_cursor;
5670
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005671 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005672 if (curwin->w_cursor.col != tpos.col)
5673 start_arrow(&tpos);
5674}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005675#endif
5676
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677/*
5678 * stop_arrow() is called before a change is made in insert mode.
5679 * If an arrow key has been used, start a new insertion.
5680 * Returns FAIL if undo is impossible, shouldn't insert then.
5681 */
5682 int
5683stop_arrow()
5684{
5685 if (arrow_used)
5686 {
5687 if (u_save_cursor() == OK)
5688 {
5689 arrow_used = FALSE;
5690 ins_need_undo = FALSE;
5691 }
5692 Insstart = curwin->w_cursor; /* new insertion starts here */
5693 Insstart_textlen = linetabsize(ml_get_curline());
5694 ai_col = 0;
5695#ifdef FEAT_VREPLACE
5696 if (State & VREPLACE_FLAG)
5697 {
5698 orig_line_count = curbuf->b_ml.ml_line_count;
5699 vr_lines_changed = 1;
5700 }
5701#endif
5702 ResetRedobuff();
5703 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005704 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
5706 else if (ins_need_undo)
5707 {
5708 if (u_save_cursor() == OK)
5709 ins_need_undo = FALSE;
5710 }
5711
5712#ifdef FEAT_FOLDING
5713 /* Always open fold at the cursor line when inserting something. */
5714 foldOpenCursor();
5715#endif
5716
5717 return (arrow_used || ins_need_undo ? FAIL : OK);
5718}
5719
5720/*
5721 * do a few things to stop inserting
5722 */
5723 static void
5724stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005725 pos_T *end_insert_pos; /* where insert ended */
5726 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005728 int cc;
5729 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730
5731 stop_redo_ins();
5732 replace_flush(); /* abandon replace stack */
5733
5734 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005735 * Save the inserted text for later redo with ^@ and CTRL-A.
5736 * Don't do it when "restart_edit" was set and nothing was inserted,
5737 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005739 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005740 if (did_restart_edit == 0 || (ptr != NULL
5741 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005742 {
5743 vim_free(last_insert);
5744 last_insert = ptr;
5745 last_insert_skip = new_insert_skip;
5746 }
5747 else
5748 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749
5750 if (!arrow_used)
5751 {
5752 /* Auto-format now. It may seem strange to do this when stopping an
5753 * insertion (or moving the cursor), but it's required when appending
5754 * a line and having it end in a space. But only do it when something
5755 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005756 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005758 pos_T tpos = curwin->w_cursor;
5759
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 /* When the cursor is at the end of the line after a space the
5761 * formatting will move it to the following word. Avoid that by
5762 * moving the cursor onto the space. */
5763 cc = 'x';
5764 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5765 {
5766 dec_cursor();
5767 cc = gchar_cursor();
5768 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005769 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 }
5771
5772 auto_format(TRUE, FALSE);
5773
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005774 if (vim_iswhite(cc))
5775 {
5776 if (gchar_cursor() != NUL)
5777 inc_cursor();
5778#ifdef FEAT_VIRTUALEDIT
5779 /* If the cursor is still at the same character, also keep
5780 * the "coladd". */
5781 if (gchar_cursor() == NUL
5782 && curwin->w_cursor.lnum == tpos.lnum
5783 && curwin->w_cursor.col == tpos.col)
5784 curwin->w_cursor.coladd = tpos.coladd;
5785#endif
5786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 }
5788
5789 /* If a space was inserted for auto-formatting, remove it now. */
5790 check_auto_format(TRUE);
5791
5792 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005793 * of the line, and put the cursor back.
5794 * Do this when ESC was used or moving the cursor up/down. */
5795 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5796 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005798 pos_T tpos = curwin->w_cursor;
5799
5800 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5802 --curwin->w_cursor.col;
5803 while (cc = gchar_cursor(), vim_iswhite(cc))
5804 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005805 if (curwin->w_cursor.lnum != tpos.lnum)
5806 curwin->w_cursor = tpos;
5807 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5809
5810#ifdef FEAT_VISUAL
5811 /* <C-S-Right> may have started Visual mode, adjust the position for
5812 * deleted characters. */
5813 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5814 {
5815 cc = STRLEN(ml_get_curline());
5816 if (VIsual.col > (colnr_T)cc)
5817 {
5818 VIsual.col = cc;
5819# ifdef FEAT_VIRTUALEDIT
5820 VIsual.coladd = 0;
5821# endif
5822 }
5823 }
5824#endif
5825 }
5826 }
5827 did_ai = FALSE;
5828#ifdef FEAT_SMARTINDENT
5829 did_si = FALSE;
5830 can_si = FALSE;
5831 can_si_back = FALSE;
5832#endif
5833
5834 /* set '[ and '] to the inserted text */
5835 curbuf->b_op_start = Insstart;
5836 curbuf->b_op_end = *end_insert_pos;
5837}
5838
5839/*
5840 * Set the last inserted text to a single character.
5841 * Used for the replace command.
5842 */
5843 void
5844set_last_insert(c)
5845 int c;
5846{
5847 char_u *s;
5848
5849 vim_free(last_insert);
5850#ifdef FEAT_MBYTE
5851 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5852#else
5853 last_insert = alloc(6);
5854#endif
5855 if (last_insert != NULL)
5856 {
5857 s = last_insert;
5858 /* Use the CTRL-V only when entering a special char */
5859 if (c < ' ' || c == DEL)
5860 *s++ = Ctrl_V;
5861 s = add_char2buf(c, s);
5862 *s++ = ESC;
5863 *s++ = NUL;
5864 last_insert_skip = 0;
5865 }
5866}
5867
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005868#if defined(EXITFREE) || defined(PROTO)
5869 void
5870free_last_insert()
5871{
5872 vim_free(last_insert);
5873 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005874 vim_free(compl_orig_text);
5875 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005876}
5877#endif
5878
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879/*
5880 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5881 * and CSI. Handle multi-byte characters.
5882 * Returns a pointer to after the added bytes.
5883 */
5884 char_u *
5885add_char2buf(c, s)
5886 int c;
5887 char_u *s;
5888{
5889#ifdef FEAT_MBYTE
5890 char_u temp[MB_MAXBYTES];
5891 int i;
5892 int len;
5893
5894 len = (*mb_char2bytes)(c, temp);
5895 for (i = 0; i < len; ++i)
5896 {
5897 c = temp[i];
5898#endif
5899 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5900 if (c == K_SPECIAL)
5901 {
5902 *s++ = K_SPECIAL;
5903 *s++ = KS_SPECIAL;
5904 *s++ = KE_FILLER;
5905 }
5906#ifdef FEAT_GUI
5907 else if (c == CSI)
5908 {
5909 *s++ = CSI;
5910 *s++ = KS_EXTRA;
5911 *s++ = (int)KE_CSI;
5912 }
5913#endif
5914 else
5915 *s++ = c;
5916#ifdef FEAT_MBYTE
5917 }
5918#endif
5919 return s;
5920}
5921
5922/*
5923 * move cursor to start of line
5924 * if flags & BL_WHITE move to first non-white
5925 * if flags & BL_SOL move to first non-white if startofline is set,
5926 * otherwise keep "curswant" column
5927 * if flags & BL_FIX don't leave the cursor on a NUL.
5928 */
5929 void
5930beginline(flags)
5931 int flags;
5932{
5933 if ((flags & BL_SOL) && !p_sol)
5934 coladvance(curwin->w_curswant);
5935 else
5936 {
5937 curwin->w_cursor.col = 0;
5938#ifdef FEAT_VIRTUALEDIT
5939 curwin->w_cursor.coladd = 0;
5940#endif
5941
5942 if (flags & (BL_WHITE | BL_SOL))
5943 {
5944 char_u *ptr;
5945
5946 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5947 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5948 ++curwin->w_cursor.col;
5949 }
5950 curwin->w_set_curswant = TRUE;
5951 }
5952}
5953
5954/*
5955 * oneright oneleft cursor_down cursor_up
5956 *
5957 * Move one char {right,left,down,up}.
5958 * Doesn't move onto the NUL past the end of the line.
5959 * Return OK when successful, FAIL when we hit a line of file boundary.
5960 */
5961
5962 int
5963oneright()
5964{
5965 char_u *ptr;
5966#ifdef FEAT_MBYTE
5967 int l;
5968#endif
5969
5970#ifdef FEAT_VIRTUALEDIT
5971 if (virtual_active())
5972 {
5973 pos_T prevpos = curwin->w_cursor;
5974
5975 /* Adjust for multi-wide char (excluding TAB) */
5976 ptr = ml_get_cursor();
5977 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5978#ifdef FEAT_MBYTE
5979 (*mb_ptr2char)(ptr)
5980#else
5981 *ptr
5982#endif
5983 ))
5984 ? ptr2cells(ptr) : 1));
5985 curwin->w_set_curswant = TRUE;
5986 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5987 return (prevpos.col != curwin->w_cursor.col
5988 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5989 }
5990#endif
5991
5992 ptr = ml_get_cursor();
5993#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005994 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 {
5996 /* The character under the cursor is a multi-byte character, move
5997 * several bytes right, but don't end up on the NUL. */
5998 if (ptr[l] == NUL)
5999 return FAIL;
6000 curwin->w_cursor.col += l;
6001 }
6002 else
6003#endif
6004 {
6005 if (*ptr++ == NUL || *ptr == NUL)
6006 return FAIL;
6007 ++curwin->w_cursor.col;
6008 }
6009
6010 curwin->w_set_curswant = TRUE;
6011 return OK;
6012}
6013
6014 int
6015oneleft()
6016{
6017#ifdef FEAT_VIRTUALEDIT
6018 if (virtual_active())
6019 {
6020 int width;
6021 int v = getviscol();
6022
6023 if (v == 0)
6024 return FAIL;
6025
6026# ifdef FEAT_LINEBREAK
6027 /* We might get stuck on 'showbreak', skip over it. */
6028 width = 1;
6029 for (;;)
6030 {
6031 coladvance(v - width);
6032 /* getviscol() is slow, skip it when 'showbreak' is empty and
6033 * there are no multi-byte characters */
6034 if ((*p_sbr == NUL
6035# ifdef FEAT_MBYTE
6036 && !has_mbyte
6037# endif
6038 ) || getviscol() < v)
6039 break;
6040 ++width;
6041 }
6042# else
6043 coladvance(v - 1);
6044# endif
6045
6046 if (curwin->w_cursor.coladd == 1)
6047 {
6048 char_u *ptr;
6049
6050 /* Adjust for multi-wide char (not a TAB) */
6051 ptr = ml_get_cursor();
6052 if (*ptr != TAB && vim_isprintc(
6053# ifdef FEAT_MBYTE
6054 (*mb_ptr2char)(ptr)
6055# else
6056 *ptr
6057# endif
6058 ) && ptr2cells(ptr) > 1)
6059 curwin->w_cursor.coladd = 0;
6060 }
6061
6062 curwin->w_set_curswant = TRUE;
6063 return OK;
6064 }
6065#endif
6066
6067 if (curwin->w_cursor.col == 0)
6068 return FAIL;
6069
6070 curwin->w_set_curswant = TRUE;
6071 --curwin->w_cursor.col;
6072
6073#ifdef FEAT_MBYTE
6074 /* if the character on the left of the current cursor is a multi-byte
6075 * character, move to its first byte */
6076 if (has_mbyte)
6077 mb_adjust_cursor();
6078#endif
6079 return OK;
6080}
6081
6082 int
6083cursor_up(n, upd_topline)
6084 long n;
6085 int upd_topline; /* When TRUE: update topline */
6086{
6087 linenr_T lnum;
6088
6089 if (n > 0)
6090 {
6091 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006092 /* This fails if the cursor is already in the first line or the count
6093 * is larger than the line number and '-' is in 'cpoptions' */
6094 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 return FAIL;
6096 if (n >= lnum)
6097 lnum = 1;
6098 else
6099#ifdef FEAT_FOLDING
6100 if (hasAnyFolding(curwin))
6101 {
6102 /*
6103 * Count each sequence of folded lines as one logical line.
6104 */
6105 /* go to the the start of the current fold */
6106 (void)hasFolding(lnum, &lnum, NULL);
6107
6108 while (n--)
6109 {
6110 /* move up one line */
6111 --lnum;
6112 if (lnum <= 1)
6113 break;
6114 /* If we entered a fold, move to the beginning, unless in
6115 * Insert mode or when 'foldopen' contains "all": it will open
6116 * in a moment. */
6117 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6118 (void)hasFolding(lnum, &lnum, NULL);
6119 }
6120 if (lnum < 1)
6121 lnum = 1;
6122 }
6123 else
6124#endif
6125 lnum -= n;
6126 curwin->w_cursor.lnum = lnum;
6127 }
6128
6129 /* try to advance to the column we want to be at */
6130 coladvance(curwin->w_curswant);
6131
6132 if (upd_topline)
6133 update_topline(); /* make sure curwin->w_topline is valid */
6134
6135 return OK;
6136}
6137
6138/*
6139 * Cursor down a number of logical lines.
6140 */
6141 int
6142cursor_down(n, upd_topline)
6143 long n;
6144 int upd_topline; /* When TRUE: update topline */
6145{
6146 linenr_T lnum;
6147
6148 if (n > 0)
6149 {
6150 lnum = curwin->w_cursor.lnum;
6151#ifdef FEAT_FOLDING
6152 /* Move to last line of fold, will fail if it's the end-of-file. */
6153 (void)hasFolding(lnum, NULL, &lnum);
6154#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006155 /* This fails if the cursor is already in the last line or would move
6156 * beyound the last line and '-' is in 'cpoptions' */
6157 if (lnum >= curbuf->b_ml.ml_line_count
6158 || (lnum + n > curbuf->b_ml.ml_line_count
6159 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 return FAIL;
6161 if (lnum + n >= curbuf->b_ml.ml_line_count)
6162 lnum = curbuf->b_ml.ml_line_count;
6163 else
6164#ifdef FEAT_FOLDING
6165 if (hasAnyFolding(curwin))
6166 {
6167 linenr_T last;
6168
6169 /* count each sequence of folded lines as one logical line */
6170 while (n--)
6171 {
6172 if (hasFolding(lnum, NULL, &last))
6173 lnum = last + 1;
6174 else
6175 ++lnum;
6176 if (lnum >= curbuf->b_ml.ml_line_count)
6177 break;
6178 }
6179 if (lnum > curbuf->b_ml.ml_line_count)
6180 lnum = curbuf->b_ml.ml_line_count;
6181 }
6182 else
6183#endif
6184 lnum += n;
6185 curwin->w_cursor.lnum = lnum;
6186 }
6187
6188 /* try to advance to the column we want to be at */
6189 coladvance(curwin->w_curswant);
6190
6191 if (upd_topline)
6192 update_topline(); /* make sure curwin->w_topline is valid */
6193
6194 return OK;
6195}
6196
6197/*
6198 * Stuff the last inserted text in the read buffer.
6199 * Last_insert actually is a copy of the redo buffer, so we
6200 * first have to remove the command.
6201 */
6202 int
6203stuff_inserted(c, count, no_esc)
6204 int c; /* Command character to be inserted */
6205 long count; /* Repeat this many times */
6206 int no_esc; /* Don't add an ESC at the end */
6207{
6208 char_u *esc_ptr;
6209 char_u *ptr;
6210 char_u *last_ptr;
6211 char_u last = NUL;
6212
6213 ptr = get_last_insert();
6214 if (ptr == NULL)
6215 {
6216 EMSG(_(e_noinstext));
6217 return FAIL;
6218 }
6219
6220 /* may want to stuff the command character, to start Insert mode */
6221 if (c != NUL)
6222 stuffcharReadbuff(c);
6223 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6224 *esc_ptr = NUL; /* remove the ESC */
6225
6226 /* when the last char is either "0" or "^" it will be quoted if no ESC
6227 * comes after it OR if it will inserted more than once and "ptr"
6228 * starts with ^D. -- Acevedo
6229 */
6230 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6231 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6232 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6233 {
6234 last = *last_ptr;
6235 *last_ptr = NUL;
6236 }
6237
6238 do
6239 {
6240 stuffReadbuff(ptr);
6241 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6242 if (last)
6243 stuffReadbuff((char_u *)(last == '0'
6244 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6245 : IF_EB("\026^", CTRL_V_STR "^")));
6246 }
6247 while (--count > 0);
6248
6249 if (last)
6250 *last_ptr = last;
6251
6252 if (esc_ptr != NULL)
6253 *esc_ptr = ESC; /* put the ESC back */
6254
6255 /* may want to stuff a trailing ESC, to get out of Insert mode */
6256 if (!no_esc)
6257 stuffcharReadbuff(ESC);
6258
6259 return OK;
6260}
6261
6262 char_u *
6263get_last_insert()
6264{
6265 if (last_insert == NULL)
6266 return NULL;
6267 return last_insert + last_insert_skip;
6268}
6269
6270/*
6271 * Get last inserted string, and remove trailing <Esc>.
6272 * Returns pointer to allocated memory (must be freed) or NULL.
6273 */
6274 char_u *
6275get_last_insert_save()
6276{
6277 char_u *s;
6278 int len;
6279
6280 if (last_insert == NULL)
6281 return NULL;
6282 s = vim_strsave(last_insert + last_insert_skip);
6283 if (s != NULL)
6284 {
6285 len = (int)STRLEN(s);
6286 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6287 s[len - 1] = NUL;
6288 }
6289 return s;
6290}
6291
6292/*
6293 * Check the word in front of the cursor for an abbreviation.
6294 * Called when the non-id character "c" has been entered.
6295 * When an abbreviation is recognized it is removed from the text and
6296 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6297 */
6298 static int
6299echeck_abbr(c)
6300 int c;
6301{
6302 /* Don't check for abbreviation in paste mode, when disabled and just
6303 * after moving around with cursor keys. */
6304 if (p_paste || no_abbr || arrow_used)
6305 return FALSE;
6306
6307 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6308 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6309}
6310
6311/*
6312 * replace-stack functions
6313 *
6314 * When replacing characters, the replaced characters are remembered for each
6315 * new character. This is used to re-insert the old text when backspacing.
6316 *
6317 * There is a NUL headed list of characters for each character that is
6318 * currently in the file after the insertion point. When BS is used, one NUL
6319 * headed list is put back for the deleted character.
6320 *
6321 * For a newline, there are two NUL headed lists. One contains the characters
6322 * that the NL replaced. The extra one stores the characters after the cursor
6323 * that were deleted (always white space).
6324 *
6325 * Replace_offset is normally 0, in which case replace_push will add a new
6326 * character at the end of the stack. If replace_offset is not 0, that many
6327 * characters will be left on the stack above the newly inserted character.
6328 */
6329
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006330static char_u *replace_stack = NULL;
6331static long replace_stack_nr = 0; /* next entry in replace stack */
6332static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333
6334 void
6335replace_push(c)
6336 int c; /* character that is replaced (NUL is none) */
6337{
6338 char_u *p;
6339
6340 if (replace_stack_nr < replace_offset) /* nothing to do */
6341 return;
6342 if (replace_stack_len <= replace_stack_nr)
6343 {
6344 replace_stack_len += 50;
6345 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6346 if (p == NULL) /* out of memory */
6347 {
6348 replace_stack_len -= 50;
6349 return;
6350 }
6351 if (replace_stack != NULL)
6352 {
6353 mch_memmove(p, replace_stack,
6354 (size_t)(replace_stack_nr * sizeof(char_u)));
6355 vim_free(replace_stack);
6356 }
6357 replace_stack = p;
6358 }
6359 p = replace_stack + replace_stack_nr - replace_offset;
6360 if (replace_offset)
6361 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6362 *p = c;
6363 ++replace_stack_nr;
6364}
6365
6366/*
6367 * call replace_push(c) with replace_offset set to the first NUL.
6368 */
6369 static void
6370replace_push_off(c)
6371 int c;
6372{
6373 char_u *p;
6374
6375 p = replace_stack + replace_stack_nr;
6376 for (replace_offset = 1; replace_offset < replace_stack_nr;
6377 ++replace_offset)
6378 if (*--p == NUL)
6379 break;
6380 replace_push(c);
6381 replace_offset = 0;
6382}
6383
6384/*
6385 * Pop one item from the replace stack.
6386 * return -1 if stack empty
6387 * return replaced character or NUL otherwise
6388 */
6389 static int
6390replace_pop()
6391{
6392 if (replace_stack_nr == 0)
6393 return -1;
6394 return (int)replace_stack[--replace_stack_nr];
6395}
6396
6397/*
6398 * Join the top two items on the replace stack. This removes to "off"'th NUL
6399 * encountered.
6400 */
6401 static void
6402replace_join(off)
6403 int off; /* offset for which NUL to remove */
6404{
6405 int i;
6406
6407 for (i = replace_stack_nr; --i >= 0; )
6408 if (replace_stack[i] == NUL && off-- <= 0)
6409 {
6410 --replace_stack_nr;
6411 mch_memmove(replace_stack + i, replace_stack + i + 1,
6412 (size_t)(replace_stack_nr - i));
6413 return;
6414 }
6415}
6416
6417/*
6418 * Pop bytes from the replace stack until a NUL is found, and insert them
6419 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6420 */
6421 static void
6422replace_pop_ins()
6423{
6424 int cc;
6425 int oldState = State;
6426
6427 State = NORMAL; /* don't want REPLACE here */
6428 while ((cc = replace_pop()) > 0)
6429 {
6430#ifdef FEAT_MBYTE
6431 mb_replace_pop_ins(cc);
6432#else
6433 ins_char(cc);
6434#endif
6435 dec_cursor();
6436 }
6437 State = oldState;
6438}
6439
6440#ifdef FEAT_MBYTE
6441/*
6442 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6443 * indicates a multi-byte char, pop the other bytes too.
6444 */
6445 static void
6446mb_replace_pop_ins(cc)
6447 int cc;
6448{
6449 int n;
6450 char_u buf[MB_MAXBYTES];
6451 int i;
6452 int c;
6453
6454 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6455 {
6456 buf[0] = cc;
6457 for (i = 1; i < n; ++i)
6458 buf[i] = replace_pop();
6459 ins_bytes_len(buf, n);
6460 }
6461 else
6462 ins_char(cc);
6463
6464 if (enc_utf8)
6465 /* Handle composing chars. */
6466 for (;;)
6467 {
6468 c = replace_pop();
6469 if (c == -1) /* stack empty */
6470 break;
6471 if ((n = MB_BYTE2LEN(c)) == 1)
6472 {
6473 /* Not a multi-byte char, put it back. */
6474 replace_push(c);
6475 break;
6476 }
6477 else
6478 {
6479 buf[0] = c;
6480 for (i = 1; i < n; ++i)
6481 buf[i] = replace_pop();
6482 if (utf_iscomposing(utf_ptr2char(buf)))
6483 ins_bytes_len(buf, n);
6484 else
6485 {
6486 /* Not a composing char, put it back. */
6487 for (i = n - 1; i >= 0; --i)
6488 replace_push(buf[i]);
6489 break;
6490 }
6491 }
6492 }
6493}
6494#endif
6495
6496/*
6497 * make the replace stack empty
6498 * (called when exiting replace mode)
6499 */
6500 static void
6501replace_flush()
6502{
6503 vim_free(replace_stack);
6504 replace_stack = NULL;
6505 replace_stack_len = 0;
6506 replace_stack_nr = 0;
6507}
6508
6509/*
6510 * Handle doing a BS for one character.
6511 * cc < 0: replace stack empty, just move cursor
6512 * cc == 0: character was inserted, delete it
6513 * cc > 0: character was replaced, put cc (first byte of original char) back
6514 * and check for more characters to be put back
6515 */
6516 static void
6517replace_do_bs()
6518{
6519 int cc;
6520#ifdef FEAT_VREPLACE
6521 int orig_len = 0;
6522 int ins_len;
6523 int orig_vcols = 0;
6524 colnr_T start_vcol;
6525 char_u *p;
6526 int i;
6527 int vcol;
6528#endif
6529
6530 cc = replace_pop();
6531 if (cc > 0)
6532 {
6533#ifdef FEAT_VREPLACE
6534 if (State & VREPLACE_FLAG)
6535 {
6536 /* Get the number of screen cells used by the character we are
6537 * going to delete. */
6538 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6539 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6540 }
6541#endif
6542#ifdef FEAT_MBYTE
6543 if (has_mbyte)
6544 {
6545 del_char(FALSE);
6546# ifdef FEAT_VREPLACE
6547 if (State & VREPLACE_FLAG)
6548 orig_len = STRLEN(ml_get_cursor());
6549# endif
6550 replace_push(cc);
6551 }
6552 else
6553#endif
6554 {
6555 pchar_cursor(cc);
6556#ifdef FEAT_VREPLACE
6557 if (State & VREPLACE_FLAG)
6558 orig_len = STRLEN(ml_get_cursor()) - 1;
6559#endif
6560 }
6561 replace_pop_ins();
6562
6563#ifdef FEAT_VREPLACE
6564 if (State & VREPLACE_FLAG)
6565 {
6566 /* Get the number of screen cells used by the inserted characters */
6567 p = ml_get_cursor();
6568 ins_len = STRLEN(p) - orig_len;
6569 vcol = start_vcol;
6570 for (i = 0; i < ins_len; ++i)
6571 {
6572 vcol += chartabsize(p + i, vcol);
6573#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006574 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575#endif
6576 }
6577 vcol -= start_vcol;
6578
6579 /* Delete spaces that were inserted after the cursor to keep the
6580 * text aligned. */
6581 curwin->w_cursor.col += ins_len;
6582 while (vcol > orig_vcols && gchar_cursor() == ' ')
6583 {
6584 del_char(FALSE);
6585 ++orig_vcols;
6586 }
6587 curwin->w_cursor.col -= ins_len;
6588 }
6589#endif
6590
6591 /* mark the buffer as changed and prepare for displaying */
6592 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6593 }
6594 else if (cc == 0)
6595 (void)del_char(FALSE);
6596}
6597
6598#ifdef FEAT_CINDENT
6599/*
6600 * Return TRUE if C-indenting is on.
6601 */
6602 static int
6603cindent_on()
6604{
6605 return (!p_paste && (curbuf->b_p_cin
6606# ifdef FEAT_EVAL
6607 || *curbuf->b_p_inde != NUL
6608# endif
6609 ));
6610}
6611#endif
6612
6613#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6614/*
6615 * Re-indent the current line, based on the current contents of it and the
6616 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6617 * confused what all the part that handles Control-T is doing that I'm not.
6618 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6619 */
6620
6621 void
6622fixthisline(get_the_indent)
6623 int (*get_the_indent) __ARGS((void));
6624{
6625 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6626 if (linewhite(curwin->w_cursor.lnum))
6627 did_ai = TRUE; /* delete the indent if the line stays empty */
6628}
6629
6630 void
6631fix_indent()
6632{
6633 if (p_paste)
6634 return;
6635# ifdef FEAT_LISP
6636 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6637 fixthisline(get_lisp_indent);
6638# endif
6639# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6640 else
6641# endif
6642# ifdef FEAT_CINDENT
6643 if (cindent_on())
6644 do_c_expr_indent();
6645# endif
6646}
6647
6648#endif
6649
6650#ifdef FEAT_CINDENT
6651/*
6652 * return TRUE if 'cinkeys' contains the key "keytyped",
6653 * when == '*': Only if key is preceded with '*' (indent before insert)
6654 * when == '!': Only if key is prededed with '!' (don't insert)
6655 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6656 *
6657 * "keytyped" can have a few special values:
6658 * KEY_OPEN_FORW
6659 * KEY_OPEN_BACK
6660 * KEY_COMPLETE just finished completion.
6661 *
6662 * If line_is_empty is TRUE accept keys with '0' before them.
6663 */
6664 int
6665in_cinkeys(keytyped, when, line_is_empty)
6666 int keytyped;
6667 int when;
6668 int line_is_empty;
6669{
6670 char_u *look;
6671 int try_match;
6672 int try_match_word;
6673 char_u *p;
6674 char_u *line;
6675 int icase;
6676 int i;
6677
6678#ifdef FEAT_EVAL
6679 if (*curbuf->b_p_inde != NUL)
6680 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6681 else
6682#endif
6683 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6684 while (*look)
6685 {
6686 /*
6687 * Find out if we want to try a match with this key, depending on
6688 * 'when' and a '*' or '!' before the key.
6689 */
6690 switch (when)
6691 {
6692 case '*': try_match = (*look == '*'); break;
6693 case '!': try_match = (*look == '!'); break;
6694 default: try_match = (*look != '*'); break;
6695 }
6696 if (*look == '*' || *look == '!')
6697 ++look;
6698
6699 /*
6700 * If there is a '0', only accept a match if the line is empty.
6701 * But may still match when typing last char of a word.
6702 */
6703 if (*look == '0')
6704 {
6705 try_match_word = try_match;
6706 if (!line_is_empty)
6707 try_match = FALSE;
6708 ++look;
6709 }
6710 else
6711 try_match_word = FALSE;
6712
6713 /*
6714 * does it look like a control character?
6715 */
6716 if (*look == '^'
6717#ifdef EBCDIC
6718 && (Ctrl_chr(look[1]) != 0)
6719#else
6720 && look[1] >= '?' && look[1] <= '_'
6721#endif
6722 )
6723 {
6724 if (try_match && keytyped == Ctrl_chr(look[1]))
6725 return TRUE;
6726 look += 2;
6727 }
6728 /*
6729 * 'o' means "o" command, open forward.
6730 * 'O' means "O" command, open backward.
6731 */
6732 else if (*look == 'o')
6733 {
6734 if (try_match && keytyped == KEY_OPEN_FORW)
6735 return TRUE;
6736 ++look;
6737 }
6738 else if (*look == 'O')
6739 {
6740 if (try_match && keytyped == KEY_OPEN_BACK)
6741 return TRUE;
6742 ++look;
6743 }
6744
6745 /*
6746 * 'e' means to check for "else" at start of line and just before the
6747 * cursor.
6748 */
6749 else if (*look == 'e')
6750 {
6751 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6752 {
6753 p = ml_get_curline();
6754 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6755 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6756 return TRUE;
6757 }
6758 ++look;
6759 }
6760
6761 /*
6762 * ':' only causes an indent if it is at the end of a label or case
6763 * statement, or when it was before typing the ':' (to fix
6764 * class::method for C++).
6765 */
6766 else if (*look == ':')
6767 {
6768 if (try_match && keytyped == ':')
6769 {
6770 p = ml_get_curline();
6771 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6772 return TRUE;
6773 if (curwin->w_cursor.col > 2
6774 && p[curwin->w_cursor.col - 1] == ':'
6775 && p[curwin->w_cursor.col - 2] == ':')
6776 {
6777 p[curwin->w_cursor.col - 1] = ' ';
6778 i = (cin_iscase(p) || cin_isscopedecl(p)
6779 || cin_islabel(30));
6780 p = ml_get_curline();
6781 p[curwin->w_cursor.col - 1] = ':';
6782 if (i)
6783 return TRUE;
6784 }
6785 }
6786 ++look;
6787 }
6788
6789
6790 /*
6791 * Is it a key in <>, maybe?
6792 */
6793 else if (*look == '<')
6794 {
6795 if (try_match)
6796 {
6797 /*
6798 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6799 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6800 * >, *, : and ! keys if they really really want to.
6801 */
6802 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6803 && keytyped == look[1])
6804 return TRUE;
6805
6806 if (keytyped == get_special_key_code(look + 1))
6807 return TRUE;
6808 }
6809 while (*look && *look != '>')
6810 look++;
6811 while (*look == '>')
6812 look++;
6813 }
6814
6815 /*
6816 * Is it a word: "=word"?
6817 */
6818 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6819 {
6820 ++look;
6821 if (*look == '~')
6822 {
6823 icase = TRUE;
6824 ++look;
6825 }
6826 else
6827 icase = FALSE;
6828 p = vim_strchr(look, ',');
6829 if (p == NULL)
6830 p = look + STRLEN(look);
6831 if ((try_match || try_match_word)
6832 && curwin->w_cursor.col >= (colnr_T)(p - look))
6833 {
6834 int match = FALSE;
6835
6836#ifdef FEAT_INS_EXPAND
6837 if (keytyped == KEY_COMPLETE)
6838 {
6839 char_u *s;
6840
6841 /* Just completed a word, check if it starts with "look".
6842 * search back for the start of a word. */
6843 line = ml_get_curline();
6844# ifdef FEAT_MBYTE
6845 if (has_mbyte)
6846 {
6847 char_u *n;
6848
6849 for (s = line + curwin->w_cursor.col; s > line; s = n)
6850 {
6851 n = mb_prevptr(line, s);
6852 if (!vim_iswordp(n))
6853 break;
6854 }
6855 }
6856 else
6857# endif
6858 for (s = line + curwin->w_cursor.col; s > line; --s)
6859 if (!vim_iswordc(s[-1]))
6860 break;
6861 if (s + (p - look) <= line + curwin->w_cursor.col
6862 && (icase
6863 ? MB_STRNICMP(s, look, p - look)
6864 : STRNCMP(s, look, p - look)) == 0)
6865 match = TRUE;
6866 }
6867 else
6868#endif
6869 /* TODO: multi-byte */
6870 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6871 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6872 {
6873 line = ml_get_cursor();
6874 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6875 || !vim_iswordc(line[-(p - look) - 1]))
6876 && (icase
6877 ? MB_STRNICMP(line - (p - look), look, p - look)
6878 : STRNCMP(line - (p - look), look, p - look))
6879 == 0)
6880 match = TRUE;
6881 }
6882 if (match && try_match_word && !try_match)
6883 {
6884 /* "0=word": Check if there are only blanks before the
6885 * word. */
6886 line = ml_get_curline();
6887 if ((int)(skipwhite(line) - line) !=
6888 (int)(curwin->w_cursor.col - (p - look)))
6889 match = FALSE;
6890 }
6891 if (match)
6892 return TRUE;
6893 }
6894 look = p;
6895 }
6896
6897 /*
6898 * ok, it's a boring generic character.
6899 */
6900 else
6901 {
6902 if (try_match && *look == keytyped)
6903 return TRUE;
6904 ++look;
6905 }
6906
6907 /*
6908 * Skip over ", ".
6909 */
6910 look = skip_to_option_part(look);
6911 }
6912 return FALSE;
6913}
6914#endif /* FEAT_CINDENT */
6915
6916#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6917/*
6918 * Map Hebrew keyboard when in hkmap mode.
6919 */
6920 int
6921hkmap(c)
6922 int c;
6923{
6924 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6925 {
6926 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6927 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6928 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6929 static char_u map[26] =
6930 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6931 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6932 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6933 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6934 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6935 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6936 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6937 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6938 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6939
6940 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6941 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6942 /* '-1'='sofit' */
6943 else if (c == 'x')
6944 return 'X';
6945 else if (c == 'q')
6946 return '\''; /* {geresh}={'} */
6947 else if (c == 246)
6948 return ' '; /* \"o --> ' ' for a german keyboard */
6949 else if (c == 228)
6950 return ' '; /* \"a --> ' ' -- / -- */
6951 else if (c == 252)
6952 return ' '; /* \"u --> ' ' -- / -- */
6953#ifdef EBCDIC
6954 else if (islower(c))
6955#else
6956 /* NOTE: islower() does not do the right thing for us on Linux so we
6957 * do this the same was as 5.7 and previous, so it works correctly on
6958 * all systems. Specifically, the e.g. Delete and Arrow keys are
6959 * munged and won't work if e.g. searching for Hebrew text.
6960 */
6961 else if (c >= 'a' && c <= 'z')
6962#endif
6963 return (int)(map[CharOrdLow(c)] + p_aleph);
6964 else
6965 return c;
6966 }
6967 else
6968 {
6969 switch (c)
6970 {
6971 case '`': return ';';
6972 case '/': return '.';
6973 case '\'': return ',';
6974 case 'q': return '/';
6975 case 'w': return '\'';
6976
6977 /* Hebrew letters - set offset from 'a' */
6978 case ',': c = '{'; break;
6979 case '.': c = 'v'; break;
6980 case ';': c = 't'; break;
6981 default: {
6982 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6983
6984#ifdef EBCDIC
6985 /* see note about islower() above */
6986 if (!islower(c))
6987#else
6988 if (c < 'a' || c > 'z')
6989#endif
6990 return c;
6991 c = str[CharOrdLow(c)];
6992 break;
6993 }
6994 }
6995
6996 return (int)(CharOrdLow(c) + p_aleph);
6997 }
6998}
6999#endif
7000
7001 static void
7002ins_reg()
7003{
7004 int need_redraw = FALSE;
7005 int regname;
7006 int literally = 0;
7007
7008 /*
7009 * If we are going to wait for a character, show a '"'.
7010 */
7011 pc_status = PC_STATUS_UNSET;
7012 if (redrawing() && !char_avail())
7013 {
7014 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007015 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016
7017 edit_putchar('"', TRUE);
7018#ifdef FEAT_CMDL_INFO
7019 add_to_showcmd_c(Ctrl_R);
7020#endif
7021 }
7022
7023#ifdef USE_ON_FLY_SCROLL
7024 dont_scroll = TRUE; /* disallow scrolling here */
7025#endif
7026
7027 /*
7028 * Don't map the register name. This also prevents the mode message to be
7029 * deleted when ESC is hit.
7030 */
7031 ++no_mapping;
7032 regname = safe_vgetc();
7033#ifdef FEAT_LANGMAP
7034 LANGMAP_ADJUST(regname, TRUE);
7035#endif
7036 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7037 {
7038 /* Get a third key for literal register insertion */
7039 literally = regname;
7040#ifdef FEAT_CMDL_INFO
7041 add_to_showcmd_c(literally);
7042#endif
7043 regname = safe_vgetc();
7044#ifdef FEAT_LANGMAP
7045 LANGMAP_ADJUST(regname, TRUE);
7046#endif
7047 }
7048 --no_mapping;
7049
7050#ifdef FEAT_EVAL
7051 /*
7052 * Don't call u_sync() while getting the expression,
7053 * evaluating it or giving an error message for it!
7054 */
7055 ++no_u_sync;
7056 if (regname == '=')
7057 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007058# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007062# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 /* Restore the Input Method. */
7064 if (im_on)
7065 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007066# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007068 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7069 {
7070 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 else
7074 {
7075#endif
7076 if (literally == Ctrl_O || literally == Ctrl_P)
7077 {
7078 /* Append the command to the redo buffer. */
7079 AppendCharToRedobuff(Ctrl_R);
7080 AppendCharToRedobuff(literally);
7081 AppendCharToRedobuff(regname);
7082
7083 do_put(regname, BACKWARD, 1L,
7084 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7085 }
7086 else if (insert_reg(regname, literally) == FAIL)
7087 {
7088 vim_beep();
7089 need_redraw = TRUE; /* remove the '"' */
7090 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007091 else if (stop_insert_mode)
7092 /* When the '=' register was used and a function was invoked that
7093 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7094 * insert anything, need to remove the '"' */
7095 need_redraw = TRUE;
7096
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097#ifdef FEAT_EVAL
7098 }
7099 --no_u_sync;
7100#endif
7101#ifdef FEAT_CMDL_INFO
7102 clear_showcmd();
7103#endif
7104
7105 /* If the inserted register is empty, we need to remove the '"' */
7106 if (need_redraw || stuff_empty())
7107 edit_unputchar();
7108}
7109
7110/*
7111 * CTRL-G commands in Insert mode.
7112 */
7113 static void
7114ins_ctrl_g()
7115{
7116 int c;
7117
7118#ifdef FEAT_INS_EXPAND
7119 /* Right after CTRL-X the cursor will be after the ruler. */
7120 setcursor();
7121#endif
7122
7123 /*
7124 * Don't map the second key. This also prevents the mode message to be
7125 * deleted when ESC is hit.
7126 */
7127 ++no_mapping;
7128 c = safe_vgetc();
7129 --no_mapping;
7130 switch (c)
7131 {
7132 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7133 case K_UP:
7134 case Ctrl_K:
7135 case 'k': ins_up(TRUE);
7136 break;
7137
7138 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7139 case K_DOWN:
7140 case Ctrl_J:
7141 case 'j': ins_down(TRUE);
7142 break;
7143
7144 /* CTRL-G u: start new undoable edit */
7145 case 'u': u_sync();
7146 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007147
7148 /* Need to reset Insstart, esp. because a BS that joins
7149 * aline to the previous one must save for undo. */
7150 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 break;
7152
7153 /* Unknown CTRL-G command, reserved for future expansion. */
7154 default: vim_beep();
7155 }
7156}
7157
7158/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007159 * CTRL-^ in Insert mode.
7160 */
7161 static void
7162ins_ctrl_hat()
7163{
7164 if (map_to_exists_mode((char_u *)"", LANGMAP))
7165 {
7166 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7167 if (State & LANGMAP)
7168 {
7169 curbuf->b_p_iminsert = B_IMODE_NONE;
7170 State &= ~LANGMAP;
7171 }
7172 else
7173 {
7174 curbuf->b_p_iminsert = B_IMODE_LMAP;
7175 State |= LANGMAP;
7176#ifdef USE_IM_CONTROL
7177 im_set_active(FALSE);
7178#endif
7179 }
7180 }
7181#ifdef USE_IM_CONTROL
7182 else
7183 {
7184 /* There are no ":lmap" mappings, toggle IM */
7185 if (im_get_status())
7186 {
7187 curbuf->b_p_iminsert = B_IMODE_NONE;
7188 im_set_active(FALSE);
7189 }
7190 else
7191 {
7192 curbuf->b_p_iminsert = B_IMODE_IM;
7193 State &= ~LANGMAP;
7194 im_set_active(TRUE);
7195 }
7196 }
7197#endif
7198 set_iminsert_global();
7199 showmode();
7200#ifdef FEAT_GUI
7201 /* may show different cursor shape or color */
7202 if (gui.in_use)
7203 gui_update_cursor(TRUE, FALSE);
7204#endif
7205#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7206 /* Show/unshow value of 'keymap' in status lines. */
7207 status_redraw_curbuf();
7208#endif
7209}
7210
7211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 * Handle ESC in insert mode.
7213 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7214 * insert.
7215 */
7216 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007217ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 long *count;
7219 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007220 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
7222 int temp;
7223 static int disabled_redraw = FALSE;
7224
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007225#ifdef FEAT_SYN_HL
7226 check_spell_redraw();
7227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228#if defined(FEAT_HANGULIN)
7229# if defined(ESC_CHG_TO_ENG_MODE)
7230 hangul_input_state_set(0);
7231# endif
7232 if (composing_hangul)
7233 {
7234 push_raw_key(composing_hangul_buffer, 2);
7235 composing_hangul = 0;
7236 }
7237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238
7239 temp = curwin->w_cursor.col;
7240 if (disabled_redraw)
7241 {
7242 --RedrawingDisabled;
7243 disabled_redraw = FALSE;
7244 }
7245 if (!arrow_used)
7246 {
7247 /*
7248 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007249 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7250 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 */
7252 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007253 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254
7255 /*
7256 * Repeating insert may take a long time. Check for
7257 * interrupt now and then.
7258 */
7259 if (*count > 0)
7260 {
7261 line_breakcheck();
7262 if (got_int)
7263 *count = 0;
7264 }
7265
7266 if (--*count > 0) /* repeat what was typed */
7267 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007268 /* Vi repeats the insert without replacing characters. */
7269 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7270 State &= ~REPLACE_FLAG;
7271
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 (void)start_redo_ins();
7273 if (cmdchar == 'r' || cmdchar == 'v')
7274 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7275 ++RedrawingDisabled;
7276 disabled_redraw = TRUE;
7277 return FALSE; /* repeat the insert */
7278 }
7279 stop_insert(&curwin->w_cursor, TRUE);
7280 undisplay_dollar();
7281 }
7282
7283 /* When an autoindent was removed, curswant stays after the
7284 * indent */
7285 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7286 curwin->w_set_curswant = TRUE;
7287
7288 /* Remember the last Insert position in the '^ mark. */
7289 if (!cmdmod.keepjumps)
7290 curbuf->b_last_insert = curwin->w_cursor;
7291
7292 /*
7293 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007294 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007296 if (!nomove
7297 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298#ifdef FEAT_VIRTUALEDIT
7299 || curwin->w_cursor.coladd > 0
7300#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007301 )
7302 && (restart_edit == NUL
7303 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007305 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007307 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308#ifdef FEAT_RIGHTLEFT
7309 && !revins_on
7310#endif
7311 )
7312 {
7313#ifdef FEAT_VIRTUALEDIT
7314 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7315 {
7316 oneleft();
7317 if (restart_edit != NUL)
7318 ++curwin->w_cursor.coladd;
7319 }
7320 else
7321#endif
7322 {
7323 --curwin->w_cursor.col;
7324#ifdef FEAT_MBYTE
7325 /* Correct cursor for multi-byte character. */
7326 if (has_mbyte)
7327 mb_adjust_cursor();
7328#endif
7329 }
7330 }
7331
7332#ifdef USE_IM_CONTROL
7333 /* Disable IM to allow typing English directly for Normal mode commands.
7334 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7335 * well). */
7336 if (!(State & LANGMAP))
7337 im_save_status(&curbuf->b_p_iminsert);
7338 im_set_active(FALSE);
7339#endif
7340
7341 State = NORMAL;
7342 /* need to position cursor again (e.g. when on a TAB ) */
7343 changed_cline_bef_curs();
7344
7345#ifdef FEAT_MOUSE
7346 setmouse();
7347#endif
7348#ifdef CURSOR_SHAPE
7349 ui_cursor_shape(); /* may show different cursor shape */
7350#endif
7351
7352 /*
7353 * When recording or for CTRL-O, need to display the new mode.
7354 * Otherwise remove the mode message.
7355 */
7356 if (Recording || restart_edit != NUL)
7357 showmode();
7358 else if (p_smd)
7359 MSG("");
7360
7361 return TRUE; /* exit Insert mode */
7362}
7363
7364#ifdef FEAT_RIGHTLEFT
7365/*
7366 * Toggle language: hkmap and revins_on.
7367 * Move to end of reverse inserted text.
7368 */
7369 static void
7370ins_ctrl_()
7371{
7372 if (revins_on && revins_chars && revins_scol >= 0)
7373 {
7374 while (gchar_cursor() != NUL && revins_chars--)
7375 ++curwin->w_cursor.col;
7376 }
7377 p_ri = !p_ri;
7378 revins_on = (State == INSERT && p_ri);
7379 if (revins_on)
7380 {
7381 revins_scol = curwin->w_cursor.col;
7382 revins_legal++;
7383 revins_chars = 0;
7384 undisplay_dollar();
7385 }
7386 else
7387 revins_scol = -1;
7388#ifdef FEAT_FKMAP
7389 if (p_altkeymap)
7390 {
7391 /*
7392 * to be consistent also for redo command, using '.'
7393 * set arrow_used to true and stop it - causing to redo
7394 * characters entered in one mode (normal/reverse insert).
7395 */
7396 arrow_used = TRUE;
7397 (void)stop_arrow();
7398 p_fkmap = curwin->w_p_rl ^ p_ri;
7399 if (p_fkmap && p_ri)
7400 State = INSERT;
7401 }
7402 else
7403#endif
7404 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7405 showmode();
7406}
7407#endif
7408
7409#ifdef FEAT_VISUAL
7410/*
7411 * If 'keymodel' contains "startsel", may start selection.
7412 * Returns TRUE when a CTRL-O and other keys stuffed.
7413 */
7414 static int
7415ins_start_select(c)
7416 int c;
7417{
7418 if (km_startsel)
7419 switch (c)
7420 {
7421 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 case K_PAGEUP:
7424 case K_KPAGEUP:
7425 case K_PAGEDOWN:
7426 case K_KPAGEDOWN:
7427# ifdef MACOS
7428 case K_LEFT:
7429 case K_RIGHT:
7430 case K_UP:
7431 case K_DOWN:
7432 case K_END:
7433 case K_HOME:
7434# endif
7435 if (!(mod_mask & MOD_MASK_SHIFT))
7436 break;
7437 /* FALLTHROUGH */
7438 case K_S_LEFT:
7439 case K_S_RIGHT:
7440 case K_S_UP:
7441 case K_S_DOWN:
7442 case K_S_END:
7443 case K_S_HOME:
7444 /* Start selection right away, the cursor can move with
7445 * CTRL-O when beyond the end of the line. */
7446 start_selection();
7447
7448 /* Execute the key in (insert) Select mode. */
7449 stuffcharReadbuff(Ctrl_O);
7450 if (mod_mask)
7451 {
7452 char_u buf[4];
7453
7454 buf[0] = K_SPECIAL;
7455 buf[1] = KS_MODIFIER;
7456 buf[2] = mod_mask;
7457 buf[3] = NUL;
7458 stuffReadbuff(buf);
7459 }
7460 stuffcharReadbuff(c);
7461 return TRUE;
7462 }
7463 return FALSE;
7464}
7465#endif
7466
7467/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007468 * <Insert> key in Insert mode: toggle insert/remplace mode.
7469 */
7470 static void
7471ins_insert(replaceState)
7472 int replaceState;
7473{
7474#ifdef FEAT_FKMAP
7475 if (p_fkmap && p_ri)
7476 {
7477 beep_flush();
7478 EMSG(farsi_text_3); /* encoded in Farsi */
7479 return;
7480 }
7481#endif
7482
7483#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007484# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007485 set_vim_var_string(VV_INSERTMODE,
7486 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007487# ifdef FEAT_VREPLACE
7488 replaceState == VREPLACE ? "v" :
7489# endif
7490 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007491# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007492 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7493#endif
7494 if (State & REPLACE_FLAG)
7495 State = INSERT | (State & LANGMAP);
7496 else
7497 State = replaceState | (State & LANGMAP);
7498 AppendCharToRedobuff(K_INS);
7499 showmode();
7500#ifdef CURSOR_SHAPE
7501 ui_cursor_shape(); /* may show different cursor shape */
7502#endif
7503}
7504
7505/*
7506 * Pressed CTRL-O in Insert mode.
7507 */
7508 static void
7509ins_ctrl_o()
7510{
7511#ifdef FEAT_VREPLACE
7512 if (State & VREPLACE_FLAG)
7513 restart_edit = 'V';
7514 else
7515#endif
7516 if (State & REPLACE_FLAG)
7517 restart_edit = 'R';
7518 else
7519 restart_edit = 'I';
7520#ifdef FEAT_VIRTUALEDIT
7521 if (virtual_active())
7522 ins_at_eol = FALSE; /* cursor always keeps its column */
7523 else
7524#endif
7525 ins_at_eol = (gchar_cursor() == NUL);
7526}
7527
7528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 * If the cursor is on an indent, ^T/^D insert/delete one
7530 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7531 * Always round the indent to 'shiftwith', this is compatible
7532 * with vi. But vi only supports ^T and ^D after an
7533 * autoindent, we support it everywhere.
7534 */
7535 static void
7536ins_shift(c, lastc)
7537 int c;
7538 int lastc;
7539{
7540 if (stop_arrow() == FAIL)
7541 return;
7542 AppendCharToRedobuff(c);
7543
7544 /*
7545 * 0^D and ^^D: remove all indent.
7546 */
7547 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7548 {
7549 --curwin->w_cursor.col;
7550 (void)del_char(FALSE); /* delete the '^' or '0' */
7551 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7552 if (State & REPLACE_FLAG)
7553 replace_pop_ins();
7554 if (lastc == '^')
7555 old_indent = get_indent(); /* remember curr. indent */
7556 change_indent(INDENT_SET, 0, TRUE, 0);
7557 }
7558 else
7559 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7560
7561 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7562 did_ai = FALSE;
7563#ifdef FEAT_SMARTINDENT
7564 did_si = FALSE;
7565 can_si = FALSE;
7566 can_si_back = FALSE;
7567#endif
7568#ifdef FEAT_CINDENT
7569 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7570#endif
7571}
7572
7573 static void
7574ins_del()
7575{
7576 int temp;
7577
7578 if (stop_arrow() == FAIL)
7579 return;
7580 if (gchar_cursor() == NUL) /* delete newline */
7581 {
7582 temp = curwin->w_cursor.col;
7583 if (!can_bs(BS_EOL) /* only if "eol" included */
7584 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7585 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7586 || do_join(FALSE) == FAIL)
7587 vim_beep();
7588 else
7589 curwin->w_cursor.col = temp;
7590 }
7591 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7592 vim_beep();
7593 did_ai = FALSE;
7594#ifdef FEAT_SMARTINDENT
7595 did_si = FALSE;
7596 can_si = FALSE;
7597 can_si_back = FALSE;
7598#endif
7599 AppendCharToRedobuff(K_DEL);
7600}
7601
7602/*
7603 * Handle Backspace, delete-word and delete-line in Insert mode.
7604 * Return TRUE when backspace was actually used.
7605 */
7606 static int
7607ins_bs(c, mode, inserted_space_p)
7608 int c;
7609 int mode;
7610 int *inserted_space_p;
7611{
7612 linenr_T lnum;
7613 int cc;
7614 int temp = 0; /* init for GCC */
7615 colnr_T mincol;
7616 int did_backspace = FALSE;
7617 int in_indent;
7618 int oldState;
7619#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007620 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621#endif
7622
7623 /*
7624 * can't delete anything in an empty file
7625 * can't backup past first character in buffer
7626 * can't backup past starting point unless 'backspace' > 1
7627 * can backup to a previous line if 'backspace' == 0
7628 */
7629 if ( bufempty()
7630 || (
7631#ifdef FEAT_RIGHTLEFT
7632 !revins_on &&
7633#endif
7634 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7635 || (!can_bs(BS_START)
7636 && (arrow_used
7637 || (curwin->w_cursor.lnum == Insstart.lnum
7638 && curwin->w_cursor.col <= Insstart.col)))
7639 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7640 && curwin->w_cursor.col <= ai_col)
7641 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7642 {
7643 vim_beep();
7644 return FALSE;
7645 }
7646
7647 if (stop_arrow() == FAIL)
7648 return FALSE;
7649 in_indent = inindent(0);
7650#ifdef FEAT_CINDENT
7651 if (in_indent)
7652 can_cindent = FALSE;
7653#endif
7654#ifdef FEAT_COMMENTS
7655 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7656#endif
7657#ifdef FEAT_RIGHTLEFT
7658 if (revins_on) /* put cursor after last inserted char */
7659 inc_cursor();
7660#endif
7661
7662#ifdef FEAT_VIRTUALEDIT
7663 /* Virtualedit:
7664 * BACKSPACE_CHAR eats a virtual space
7665 * BACKSPACE_WORD eats all coladd
7666 * BACKSPACE_LINE eats all coladd and keeps going
7667 */
7668 if (curwin->w_cursor.coladd > 0)
7669 {
7670 if (mode == BACKSPACE_CHAR)
7671 {
7672 --curwin->w_cursor.coladd;
7673 return TRUE;
7674 }
7675 if (mode == BACKSPACE_WORD)
7676 {
7677 curwin->w_cursor.coladd = 0;
7678 return TRUE;
7679 }
7680 curwin->w_cursor.coladd = 0;
7681 }
7682#endif
7683
7684 /*
7685 * delete newline!
7686 */
7687 if (curwin->w_cursor.col == 0)
7688 {
7689 lnum = Insstart.lnum;
7690 if (curwin->w_cursor.lnum == Insstart.lnum
7691#ifdef FEAT_RIGHTLEFT
7692 || revins_on
7693#endif
7694 )
7695 {
7696 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7697 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7698 return FALSE;
7699 --Insstart.lnum;
7700 Insstart.col = MAXCOL;
7701 }
7702 /*
7703 * In replace mode:
7704 * cc < 0: NL was inserted, delete it
7705 * cc >= 0: NL was replaced, put original characters back
7706 */
7707 cc = -1;
7708 if (State & REPLACE_FLAG)
7709 cc = replace_pop(); /* returns -1 if NL was inserted */
7710 /*
7711 * In replace mode, in the line we started replacing, we only move the
7712 * cursor.
7713 */
7714 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7715 {
7716 dec_cursor();
7717 }
7718 else
7719 {
7720#ifdef FEAT_VREPLACE
7721 if (!(State & VREPLACE_FLAG)
7722 || curwin->w_cursor.lnum > orig_line_count)
7723#endif
7724 {
7725 temp = gchar_cursor(); /* remember current char */
7726 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007727
7728 /* When "aw" is in 'formatoptions' we must delete the space at
7729 * the end of the line, otherwise the line will be broken
7730 * again when auto-formatting. */
7731 if (has_format_option(FO_AUTO)
7732 && has_format_option(FO_WHITE_PAR))
7733 {
7734 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7735 TRUE);
7736 int len;
7737
7738 len = STRLEN(ptr);
7739 if (len > 0 && ptr[len - 1] == ' ')
7740 ptr[len - 1] = NUL;
7741 }
7742
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 (void)do_join(FALSE);
7744 if (temp == NUL && gchar_cursor() != NUL)
7745 inc_cursor();
7746 }
7747#ifdef FEAT_VREPLACE
7748 else
7749 dec_cursor();
7750#endif
7751
7752 /*
7753 * In REPLACE mode we have to put back the text that was replaced
7754 * by the NL. On the replace stack is first a NUL-terminated
7755 * sequence of characters that were deleted and then the
7756 * characters that NL replaced.
7757 */
7758 if (State & REPLACE_FLAG)
7759 {
7760 /*
7761 * Do the next ins_char() in NORMAL state, to
7762 * prevent ins_char() from replacing characters and
7763 * avoiding showmatch().
7764 */
7765 oldState = State;
7766 State = NORMAL;
7767 /*
7768 * restore characters (blanks) deleted after cursor
7769 */
7770 while (cc > 0)
7771 {
7772 temp = curwin->w_cursor.col;
7773#ifdef FEAT_MBYTE
7774 mb_replace_pop_ins(cc);
7775#else
7776 ins_char(cc);
7777#endif
7778 curwin->w_cursor.col = temp;
7779 cc = replace_pop();
7780 }
7781 /* restore the characters that NL replaced */
7782 replace_pop_ins();
7783 State = oldState;
7784 }
7785 }
7786 did_ai = FALSE;
7787 }
7788 else
7789 {
7790 /*
7791 * Delete character(s) before the cursor.
7792 */
7793#ifdef FEAT_RIGHTLEFT
7794 if (revins_on) /* put cursor on last inserted char */
7795 dec_cursor();
7796#endif
7797 mincol = 0;
7798 /* keep indent */
7799 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7800#ifdef FEAT_RIGHTLEFT
7801 && !revins_on
7802#endif
7803 )
7804 {
7805 temp = curwin->w_cursor.col;
7806 beginline(BL_WHITE);
7807 if (curwin->w_cursor.col < (colnr_T)temp)
7808 mincol = curwin->w_cursor.col;
7809 curwin->w_cursor.col = temp;
7810 }
7811
7812 /*
7813 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7814 */
7815 if ( mode == BACKSPACE_CHAR
7816 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007817 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 && (*(ml_get_cursor() - 1) == TAB
7819 || (*(ml_get_cursor() - 1) == ' '
7820 && (!*inserted_space_p
7821 || arrow_used))))))
7822 {
7823 int ts;
7824 colnr_T vcol;
7825 colnr_T want_vcol;
7826 int extra = 0;
7827
7828 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007829 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 ts = curbuf->b_p_sw;
7831 else
7832 ts = curbuf->b_p_sts;
7833 /* Compute the virtual column where we want to be. Since
7834 * 'showbreak' may get in the way, need to get the last column of
7835 * the previous character. */
7836 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7837 dec_cursor();
7838 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7839 inc_cursor();
7840 want_vcol = (want_vcol / ts) * ts;
7841
7842 /* delete characters until we are at or before want_vcol */
7843 while (vcol > want_vcol
7844 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7845 {
7846 dec_cursor();
7847 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7848 if (State & REPLACE_FLAG)
7849 {
7850 /* Don't delete characters before the insert point when in
7851 * Replace mode */
7852 if (curwin->w_cursor.lnum != Insstart.lnum
7853 || curwin->w_cursor.col >= Insstart.col)
7854 {
7855#if 0 /* what was this for? It causes problems when sw != ts. */
7856 if (State == REPLACE && (int)vcol < want_vcol)
7857 {
7858 (void)del_char(FALSE);
7859 extra = 2; /* don't pop too much */
7860 }
7861 else
7862#endif
7863 replace_do_bs();
7864 }
7865 }
7866 else
7867 (void)del_char(FALSE);
7868 }
7869
7870 /* insert extra spaces until we are at want_vcol */
7871 while (vcol < want_vcol)
7872 {
7873 /* Remember the first char we inserted */
7874 if (curwin->w_cursor.lnum == Insstart.lnum
7875 && curwin->w_cursor.col < Insstart.col)
7876 Insstart.col = curwin->w_cursor.col;
7877
7878#ifdef FEAT_VREPLACE
7879 if (State & VREPLACE_FLAG)
7880 ins_char(' ');
7881 else
7882#endif
7883 {
7884 ins_str((char_u *)" ");
7885 if ((State & REPLACE_FLAG) && extra <= 1)
7886 {
7887 if (extra)
7888 replace_push_off(NUL);
7889 else
7890 replace_push(NUL);
7891 }
7892 if (extra == 2)
7893 extra = 1;
7894 }
7895 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7896 }
7897 }
7898
7899 /*
7900 * Delete upto starting point, start of line or previous word.
7901 */
7902 else do
7903 {
7904#ifdef FEAT_RIGHTLEFT
7905 if (!revins_on) /* put cursor on char to be deleted */
7906#endif
7907 dec_cursor();
7908
7909 /* start of word? */
7910 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7911 {
7912 mode = BACKSPACE_WORD_NOT_SPACE;
7913 temp = vim_iswordc(gchar_cursor());
7914 }
7915 /* end of word? */
7916 else if (mode == BACKSPACE_WORD_NOT_SPACE
7917 && (vim_isspace(cc = gchar_cursor())
7918 || vim_iswordc(cc) != temp))
7919 {
7920#ifdef FEAT_RIGHTLEFT
7921 if (!revins_on)
7922#endif
7923 inc_cursor();
7924#ifdef FEAT_RIGHTLEFT
7925 else if (State & REPLACE_FLAG)
7926 dec_cursor();
7927#endif
7928 break;
7929 }
7930 if (State & REPLACE_FLAG)
7931 replace_do_bs();
7932 else
7933 {
7934#ifdef FEAT_MBYTE
7935 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007936 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937#endif
7938 (void)del_char(FALSE);
7939#ifdef FEAT_MBYTE
7940 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007941 * If there are combining characters and 'delcombine' is set
7942 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 * character.
7944 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007945 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 inc_cursor();
7947#endif
7948#ifdef FEAT_RIGHTLEFT
7949 if (revins_chars)
7950 {
7951 revins_chars--;
7952 revins_legal++;
7953 }
7954 if (revins_on && gchar_cursor() == NUL)
7955 break;
7956#endif
7957 }
7958 /* Just a single backspace?: */
7959 if (mode == BACKSPACE_CHAR)
7960 break;
7961 } while (
7962#ifdef FEAT_RIGHTLEFT
7963 revins_on ||
7964#endif
7965 (curwin->w_cursor.col > mincol
7966 && (curwin->w_cursor.lnum != Insstart.lnum
7967 || curwin->w_cursor.col != Insstart.col)));
7968 did_backspace = TRUE;
7969 }
7970#ifdef FEAT_SMARTINDENT
7971 did_si = FALSE;
7972 can_si = FALSE;
7973 can_si_back = FALSE;
7974#endif
7975 if (curwin->w_cursor.col <= 1)
7976 did_ai = FALSE;
7977 /*
7978 * It's a little strange to put backspaces into the redo
7979 * buffer, but it makes auto-indent a lot easier to deal
7980 * with.
7981 */
7982 AppendCharToRedobuff(c);
7983
7984 /* If deleted before the insertion point, adjust it */
7985 if (curwin->w_cursor.lnum == Insstart.lnum
7986 && curwin->w_cursor.col < Insstart.col)
7987 Insstart.col = curwin->w_cursor.col;
7988
7989 /* vi behaviour: the cursor moves backward but the character that
7990 * was there remains visible
7991 * Vim behaviour: the cursor moves backward and the character that
7992 * was there is erased from the screen.
7993 * We can emulate the vi behaviour by pretending there is a dollar
7994 * displayed even when there isn't.
7995 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7996 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7997 dollar_vcol = curwin->w_virtcol;
7998
7999 return did_backspace;
8000}
8001
8002#ifdef FEAT_MOUSE
8003 static void
8004ins_mouse(c)
8005 int c;
8006{
8007 pos_T tpos;
8008
8009# ifdef FEAT_GUI
8010 /* When GUI is active, also move/paste when 'mouse' is empty */
8011 if (!gui.in_use)
8012# endif
8013 if (!mouse_has(MOUSE_INSERT))
8014 return;
8015
8016 undisplay_dollar();
8017 tpos = curwin->w_cursor;
8018 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8019 {
8020 start_arrow(&tpos);
8021# ifdef FEAT_CINDENT
8022 can_cindent = TRUE;
8023# endif
8024 }
8025
8026#ifdef FEAT_WINDOWS
8027 /* redraw status lines (in case another window became active) */
8028 redraw_statuslines();
8029#endif
8030}
8031
8032 static void
8033ins_mousescroll(up)
8034 int up;
8035{
8036 pos_T tpos;
8037# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8038 win_T *old_curwin;
8039# endif
8040
8041 tpos = curwin->w_cursor;
8042
8043# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8044 old_curwin = curwin;
8045
8046 /* Currently the mouse coordinates are only known in the GUI. */
8047 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8048 {
8049 int row, col;
8050
8051 row = mouse_row;
8052 col = mouse_col;
8053
8054 /* find the window at the pointer coordinates */
8055 curwin = mouse_find_win(&row, &col);
8056 curbuf = curwin->w_buffer;
8057 }
8058 if (curwin == old_curwin)
8059# endif
8060 undisplay_dollar();
8061
8062 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8063 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8064 else
8065 scroll_redraw(up, 3L);
8066
8067# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8068 curwin->w_redr_status = TRUE;
8069
8070 curwin = old_curwin;
8071 curbuf = curwin->w_buffer;
8072# endif
8073
8074 if (!equalpos(curwin->w_cursor, tpos))
8075 {
8076 start_arrow(&tpos);
8077# ifdef FEAT_CINDENT
8078 can_cindent = TRUE;
8079# endif
8080 }
8081}
8082#endif
8083
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008084#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8085 void
8086ins_tabline(c)
8087 int c;
8088{
8089 /* We will be leaving the current window, unless closing another tab. */
8090 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8091 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8092 {
8093 undisplay_dollar();
8094 start_arrow(&curwin->w_cursor);
8095# ifdef FEAT_CINDENT
8096 can_cindent = TRUE;
8097# endif
8098 }
8099
8100 if (c == K_TABLINE)
8101 goto_tabpage(current_tab);
8102 else
8103 handle_tabmenu();
8104
8105}
8106#endif
8107
8108#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 void
8110ins_scroll()
8111{
8112 pos_T tpos;
8113
8114 undisplay_dollar();
8115 tpos = curwin->w_cursor;
8116 if (gui_do_scroll())
8117 {
8118 start_arrow(&tpos);
8119# ifdef FEAT_CINDENT
8120 can_cindent = TRUE;
8121# endif
8122 }
8123}
8124
8125 void
8126ins_horscroll()
8127{
8128 pos_T tpos;
8129
8130 undisplay_dollar();
8131 tpos = curwin->w_cursor;
8132 if (gui_do_horiz_scroll())
8133 {
8134 start_arrow(&tpos);
8135# ifdef FEAT_CINDENT
8136 can_cindent = TRUE;
8137# endif
8138 }
8139}
8140#endif
8141
8142 static void
8143ins_left()
8144{
8145 pos_T tpos;
8146
8147#ifdef FEAT_FOLDING
8148 if ((fdo_flags & FDO_HOR) && KeyTyped)
8149 foldOpenCursor();
8150#endif
8151 undisplay_dollar();
8152 tpos = curwin->w_cursor;
8153 if (oneleft() == OK)
8154 {
8155 start_arrow(&tpos);
8156#ifdef FEAT_RIGHTLEFT
8157 /* If exit reversed string, position is fixed */
8158 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8159 revins_legal++;
8160 revins_chars++;
8161#endif
8162 }
8163
8164 /*
8165 * if 'whichwrap' set for cursor in insert mode may go to
8166 * previous line
8167 */
8168 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8169 {
8170 start_arrow(&tpos);
8171 --(curwin->w_cursor.lnum);
8172 coladvance((colnr_T)MAXCOL);
8173 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8174 }
8175 else
8176 vim_beep();
8177}
8178
8179 static void
8180ins_home(c)
8181 int c;
8182{
8183 pos_T tpos;
8184
8185#ifdef FEAT_FOLDING
8186 if ((fdo_flags & FDO_HOR) && KeyTyped)
8187 foldOpenCursor();
8188#endif
8189 undisplay_dollar();
8190 tpos = curwin->w_cursor;
8191 if (c == K_C_HOME)
8192 curwin->w_cursor.lnum = 1;
8193 curwin->w_cursor.col = 0;
8194#ifdef FEAT_VIRTUALEDIT
8195 curwin->w_cursor.coladd = 0;
8196#endif
8197 curwin->w_curswant = 0;
8198 start_arrow(&tpos);
8199}
8200
8201 static void
8202ins_end(c)
8203 int c;
8204{
8205 pos_T tpos;
8206
8207#ifdef FEAT_FOLDING
8208 if ((fdo_flags & FDO_HOR) && KeyTyped)
8209 foldOpenCursor();
8210#endif
8211 undisplay_dollar();
8212 tpos = curwin->w_cursor;
8213 if (c == K_C_END)
8214 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8215 coladvance((colnr_T)MAXCOL);
8216 curwin->w_curswant = MAXCOL;
8217
8218 start_arrow(&tpos);
8219}
8220
8221 static void
8222ins_s_left()
8223{
8224#ifdef FEAT_FOLDING
8225 if ((fdo_flags & FDO_HOR) && KeyTyped)
8226 foldOpenCursor();
8227#endif
8228 undisplay_dollar();
8229 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8230 {
8231 start_arrow(&curwin->w_cursor);
8232 (void)bck_word(1L, FALSE, FALSE);
8233 curwin->w_set_curswant = TRUE;
8234 }
8235 else
8236 vim_beep();
8237}
8238
8239 static void
8240ins_right()
8241{
8242#ifdef FEAT_FOLDING
8243 if ((fdo_flags & FDO_HOR) && KeyTyped)
8244 foldOpenCursor();
8245#endif
8246 undisplay_dollar();
8247 if (gchar_cursor() != NUL || virtual_active()
8248 )
8249 {
8250 start_arrow(&curwin->w_cursor);
8251 curwin->w_set_curswant = TRUE;
8252#ifdef FEAT_VIRTUALEDIT
8253 if (virtual_active())
8254 oneright();
8255 else
8256#endif
8257 {
8258#ifdef FEAT_MBYTE
8259 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008260 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 else
8262#endif
8263 ++curwin->w_cursor.col;
8264 }
8265
8266#ifdef FEAT_RIGHTLEFT
8267 revins_legal++;
8268 if (revins_chars)
8269 revins_chars--;
8270#endif
8271 }
8272 /* if 'whichwrap' set for cursor in insert mode, may move the
8273 * cursor to the next line */
8274 else if (vim_strchr(p_ww, ']') != NULL
8275 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8276 {
8277 start_arrow(&curwin->w_cursor);
8278 curwin->w_set_curswant = TRUE;
8279 ++curwin->w_cursor.lnum;
8280 curwin->w_cursor.col = 0;
8281 }
8282 else
8283 vim_beep();
8284}
8285
8286 static void
8287ins_s_right()
8288{
8289#ifdef FEAT_FOLDING
8290 if ((fdo_flags & FDO_HOR) && KeyTyped)
8291 foldOpenCursor();
8292#endif
8293 undisplay_dollar();
8294 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8295 || gchar_cursor() != NUL)
8296 {
8297 start_arrow(&curwin->w_cursor);
8298 (void)fwd_word(1L, FALSE, 0);
8299 curwin->w_set_curswant = TRUE;
8300 }
8301 else
8302 vim_beep();
8303}
8304
8305 static void
8306ins_up(startcol)
8307 int startcol; /* when TRUE move to Insstart.col */
8308{
8309 pos_T tpos;
8310 linenr_T old_topline = curwin->w_topline;
8311#ifdef FEAT_DIFF
8312 int old_topfill = curwin->w_topfill;
8313#endif
8314
8315 undisplay_dollar();
8316 tpos = curwin->w_cursor;
8317 if (cursor_up(1L, TRUE) == OK)
8318 {
8319 if (startcol)
8320 coladvance(getvcol_nolist(&Insstart));
8321 if (old_topline != curwin->w_topline
8322#ifdef FEAT_DIFF
8323 || old_topfill != curwin->w_topfill
8324#endif
8325 )
8326 redraw_later(VALID);
8327 start_arrow(&tpos);
8328#ifdef FEAT_CINDENT
8329 can_cindent = TRUE;
8330#endif
8331 }
8332 else
8333 vim_beep();
8334}
8335
8336 static void
8337ins_pageup()
8338{
8339 pos_T tpos;
8340
8341 undisplay_dollar();
8342 tpos = curwin->w_cursor;
8343 if (onepage(BACKWARD, 1L) == OK)
8344 {
8345 start_arrow(&tpos);
8346#ifdef FEAT_CINDENT
8347 can_cindent = TRUE;
8348#endif
8349 }
8350 else
8351 vim_beep();
8352}
8353
8354 static void
8355ins_down(startcol)
8356 int startcol; /* when TRUE move to Insstart.col */
8357{
8358 pos_T tpos;
8359 linenr_T old_topline = curwin->w_topline;
8360#ifdef FEAT_DIFF
8361 int old_topfill = curwin->w_topfill;
8362#endif
8363
8364 undisplay_dollar();
8365 tpos = curwin->w_cursor;
8366 if (cursor_down(1L, TRUE) == OK)
8367 {
8368 if (startcol)
8369 coladvance(getvcol_nolist(&Insstart));
8370 if (old_topline != curwin->w_topline
8371#ifdef FEAT_DIFF
8372 || old_topfill != curwin->w_topfill
8373#endif
8374 )
8375 redraw_later(VALID);
8376 start_arrow(&tpos);
8377#ifdef FEAT_CINDENT
8378 can_cindent = TRUE;
8379#endif
8380 }
8381 else
8382 vim_beep();
8383}
8384
8385 static void
8386ins_pagedown()
8387{
8388 pos_T tpos;
8389
8390 undisplay_dollar();
8391 tpos = curwin->w_cursor;
8392 if (onepage(FORWARD, 1L) == OK)
8393 {
8394 start_arrow(&tpos);
8395#ifdef FEAT_CINDENT
8396 can_cindent = TRUE;
8397#endif
8398 }
8399 else
8400 vim_beep();
8401}
8402
8403#ifdef FEAT_DND
8404 static void
8405ins_drop()
8406{
8407 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8408}
8409#endif
8410
8411/*
8412 * Handle TAB in Insert or Replace mode.
8413 * Return TRUE when the TAB needs to be inserted like a normal character.
8414 */
8415 static int
8416ins_tab()
8417{
8418 int ind;
8419 int i;
8420 int temp;
8421
8422 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8423 Insstart_blank_vcol = get_nolist_virtcol();
8424 if (echeck_abbr(TAB + ABBR_OFF))
8425 return FALSE;
8426
8427 ind = inindent(0);
8428#ifdef FEAT_CINDENT
8429 if (ind)
8430 can_cindent = FALSE;
8431#endif
8432
8433 /*
8434 * When nothing special, insert TAB like a normal character
8435 */
8436 if (!curbuf->b_p_et
8437 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8438 && curbuf->b_p_sts == 0)
8439 return TRUE;
8440
8441 if (stop_arrow() == FAIL)
8442 return TRUE;
8443
8444 did_ai = FALSE;
8445#ifdef FEAT_SMARTINDENT
8446 did_si = FALSE;
8447 can_si = FALSE;
8448 can_si_back = FALSE;
8449#endif
8450 AppendToRedobuff((char_u *)"\t");
8451
8452 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8453 temp = (int)curbuf->b_p_sw;
8454 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8455 temp = (int)curbuf->b_p_sts;
8456 else /* otherwise use 'tabstop' */
8457 temp = (int)curbuf->b_p_ts;
8458 temp -= get_nolist_virtcol() % temp;
8459
8460 /*
8461 * Insert the first space with ins_char(). It will delete one char in
8462 * replace mode. Insert the rest with ins_str(); it will not delete any
8463 * chars. For VREPLACE mode, we use ins_char() for all characters.
8464 */
8465 ins_char(' ');
8466 while (--temp > 0)
8467 {
8468#ifdef FEAT_VREPLACE
8469 if (State & VREPLACE_FLAG)
8470 ins_char(' ');
8471 else
8472#endif
8473 {
8474 ins_str((char_u *)" ");
8475 if (State & REPLACE_FLAG) /* no char replaced */
8476 replace_push(NUL);
8477 }
8478 }
8479
8480 /*
8481 * When 'expandtab' not set: Replace spaces by TABs where possible.
8482 */
8483 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8484 {
8485 char_u *ptr;
8486#ifdef FEAT_VREPLACE
8487 char_u *saved_line = NULL; /* init for GCC */
8488 pos_T pos;
8489#endif
8490 pos_T fpos;
8491 pos_T *cursor;
8492 colnr_T want_vcol, vcol;
8493 int change_col = -1;
8494 int save_list = curwin->w_p_list;
8495
8496 /*
8497 * Get the current line. For VREPLACE mode, don't make real changes
8498 * yet, just work on a copy of the line.
8499 */
8500#ifdef FEAT_VREPLACE
8501 if (State & VREPLACE_FLAG)
8502 {
8503 pos = curwin->w_cursor;
8504 cursor = &pos;
8505 saved_line = vim_strsave(ml_get_curline());
8506 if (saved_line == NULL)
8507 return FALSE;
8508 ptr = saved_line + pos.col;
8509 }
8510 else
8511#endif
8512 {
8513 ptr = ml_get_cursor();
8514 cursor = &curwin->w_cursor;
8515 }
8516
8517 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8518 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8519 curwin->w_p_list = FALSE;
8520
8521 /* Find first white before the cursor */
8522 fpos = curwin->w_cursor;
8523 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8524 {
8525 --fpos.col;
8526 --ptr;
8527 }
8528
8529 /* In Replace mode, don't change characters before the insert point. */
8530 if ((State & REPLACE_FLAG)
8531 && fpos.lnum == Insstart.lnum
8532 && fpos.col < Insstart.col)
8533 {
8534 ptr += Insstart.col - fpos.col;
8535 fpos.col = Insstart.col;
8536 }
8537
8538 /* compute virtual column numbers of first white and cursor */
8539 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8540 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8541
8542 /* Use as many TABs as possible. Beware of 'showbreak' and
8543 * 'linebreak' adding extra virtual columns. */
8544 while (vim_iswhite(*ptr))
8545 {
8546 i = lbr_chartabsize((char_u *)"\t", vcol);
8547 if (vcol + i > want_vcol)
8548 break;
8549 if (*ptr != TAB)
8550 {
8551 *ptr = TAB;
8552 if (change_col < 0)
8553 {
8554 change_col = fpos.col; /* Column of first change */
8555 /* May have to adjust Insstart */
8556 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8557 Insstart.col = fpos.col;
8558 }
8559 }
8560 ++fpos.col;
8561 ++ptr;
8562 vcol += i;
8563 }
8564
8565 if (change_col >= 0)
8566 {
8567 int repl_off = 0;
8568
8569 /* Skip over the spaces we need. */
8570 while (vcol < want_vcol && *ptr == ' ')
8571 {
8572 vcol += lbr_chartabsize(ptr, vcol);
8573 ++ptr;
8574 ++repl_off;
8575 }
8576 if (vcol > want_vcol)
8577 {
8578 /* Must have a char with 'showbreak' just before it. */
8579 --ptr;
8580 --repl_off;
8581 }
8582 fpos.col += repl_off;
8583
8584 /* Delete following spaces. */
8585 i = cursor->col - fpos.col;
8586 if (i > 0)
8587 {
8588 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8589 /* correct replace stack. */
8590 if ((State & REPLACE_FLAG)
8591#ifdef FEAT_VREPLACE
8592 && !(State & VREPLACE_FLAG)
8593#endif
8594 )
8595 for (temp = i; --temp >= 0; )
8596 replace_join(repl_off);
8597 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008598#ifdef FEAT_NETBEANS_INTG
8599 if (usingNetbeans)
8600 {
8601 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8602 (long)(i + 1));
8603 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8604 (char_u *)"\t", 1);
8605 }
8606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 cursor->col -= i;
8608
8609#ifdef FEAT_VREPLACE
8610 /*
8611 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8612 * backspacing over the changed spacing and then inserting the new
8613 * spacing.
8614 */
8615 if (State & VREPLACE_FLAG)
8616 {
8617 /* Backspace from real cursor to change_col */
8618 backspace_until_column(change_col);
8619
8620 /* Insert each char in saved_line from changed_col to
8621 * ptr-cursor */
8622 ins_bytes_len(saved_line + change_col,
8623 cursor->col - change_col);
8624 }
8625#endif
8626 }
8627
8628#ifdef FEAT_VREPLACE
8629 if (State & VREPLACE_FLAG)
8630 vim_free(saved_line);
8631#endif
8632 curwin->w_p_list = save_list;
8633 }
8634
8635 return FALSE;
8636}
8637
8638/*
8639 * Handle CR or NL in insert mode.
8640 * Return TRUE when out of memory or can't undo.
8641 */
8642 static int
8643ins_eol(c)
8644 int c;
8645{
8646 int i;
8647
8648 if (echeck_abbr(c + ABBR_OFF))
8649 return FALSE;
8650 if (stop_arrow() == FAIL)
8651 return TRUE;
8652 undisplay_dollar();
8653
8654 /*
8655 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8656 * character under the cursor. Only push a NUL on the replace stack,
8657 * nothing to put back when the NL is deleted.
8658 */
8659 if ((State & REPLACE_FLAG)
8660#ifdef FEAT_VREPLACE
8661 && !(State & VREPLACE_FLAG)
8662#endif
8663 )
8664 replace_push(NUL);
8665
8666 /*
8667 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8668 * replacing the next line, so we push all of the characters left on the
8669 * line onto the replace stack. This is not done here though, it is done
8670 * in open_line().
8671 */
8672
8673#ifdef FEAT_RIGHTLEFT
8674# ifdef FEAT_FKMAP
8675 if (p_altkeymap && p_fkmap)
8676 fkmap(NL);
8677# endif
8678 /* NL in reverse insert will always start in the end of
8679 * current line. */
8680 if (revins_on)
8681 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8682#endif
8683
8684 AppendToRedobuff(NL_STR);
8685 i = open_line(FORWARD,
8686#ifdef FEAT_COMMENTS
8687 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8688#endif
8689 0, old_indent);
8690 old_indent = 0;
8691#ifdef FEAT_CINDENT
8692 can_cindent = TRUE;
8693#endif
8694
8695 return (!i);
8696}
8697
8698#ifdef FEAT_DIGRAPHS
8699/*
8700 * Handle digraph in insert mode.
8701 * Returns character still to be inserted, or NUL when nothing remaining to be
8702 * done.
8703 */
8704 static int
8705ins_digraph()
8706{
8707 int c;
8708 int cc;
8709
8710 pc_status = PC_STATUS_UNSET;
8711 if (redrawing() && !char_avail())
8712 {
8713 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008714 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715
8716 edit_putchar('?', TRUE);
8717#ifdef FEAT_CMDL_INFO
8718 add_to_showcmd_c(Ctrl_K);
8719#endif
8720 }
8721
8722#ifdef USE_ON_FLY_SCROLL
8723 dont_scroll = TRUE; /* disallow scrolling here */
8724#endif
8725
8726 /* don't map the digraph chars. This also prevents the
8727 * mode message to be deleted when ESC is hit */
8728 ++no_mapping;
8729 ++allow_keys;
8730 c = safe_vgetc();
8731 --no_mapping;
8732 --allow_keys;
8733 if (IS_SPECIAL(c) || mod_mask) /* special key */
8734 {
8735#ifdef FEAT_CMDL_INFO
8736 clear_showcmd();
8737#endif
8738 insert_special(c, TRUE, FALSE);
8739 return NUL;
8740 }
8741 if (c != ESC)
8742 {
8743 if (redrawing() && !char_avail())
8744 {
8745 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008746 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747
8748 if (char2cells(c) == 1)
8749 {
8750 /* first remove the '?', otherwise it's restored when typing
8751 * an ESC next */
8752 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008753 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 edit_putchar(c, TRUE);
8755 }
8756#ifdef FEAT_CMDL_INFO
8757 add_to_showcmd_c(c);
8758#endif
8759 }
8760 ++no_mapping;
8761 ++allow_keys;
8762 cc = safe_vgetc();
8763 --no_mapping;
8764 --allow_keys;
8765 if (cc != ESC)
8766 {
8767 AppendToRedobuff((char_u *)CTRL_V_STR);
8768 c = getdigraph(c, cc, TRUE);
8769#ifdef FEAT_CMDL_INFO
8770 clear_showcmd();
8771#endif
8772 return c;
8773 }
8774 }
8775 edit_unputchar();
8776#ifdef FEAT_CMDL_INFO
8777 clear_showcmd();
8778#endif
8779 return NUL;
8780}
8781#endif
8782
8783/*
8784 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8785 * Returns the char to be inserted, or NUL if none found.
8786 */
8787 static int
8788ins_copychar(lnum)
8789 linenr_T lnum;
8790{
8791 int c;
8792 int temp;
8793 char_u *ptr, *prev_ptr;
8794
8795 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8796 {
8797 vim_beep();
8798 return NUL;
8799 }
8800
8801 /* try to advance to the cursor column */
8802 temp = 0;
8803 ptr = ml_get(lnum);
8804 prev_ptr = ptr;
8805 validate_virtcol();
8806 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8807 {
8808 prev_ptr = ptr;
8809 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8810 }
8811 if ((colnr_T)temp > curwin->w_virtcol)
8812 ptr = prev_ptr;
8813
8814#ifdef FEAT_MBYTE
8815 c = (*mb_ptr2char)(ptr);
8816#else
8817 c = *ptr;
8818#endif
8819 if (c == NUL)
8820 vim_beep();
8821 return c;
8822}
8823
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008824/*
8825 * CTRL-Y or CTRL-E typed in Insert mode.
8826 */
8827 static int
8828ins_ctrl_ey(tc)
8829 int tc;
8830{
8831 int c = tc;
8832
8833#ifdef FEAT_INS_EXPAND
8834 if (ctrl_x_mode == CTRL_X_SCROLL)
8835 {
8836 if (c == Ctrl_Y)
8837 scrolldown_clamp();
8838 else
8839 scrollup_clamp();
8840 redraw_later(VALID);
8841 }
8842 else
8843#endif
8844 {
8845 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8846 if (c != NUL)
8847 {
8848 long tw_save;
8849
8850 /* The character must be taken literally, insert like it
8851 * was typed after a CTRL-V, and pretend 'textwidth'
8852 * wasn't set. Digits, 'o' and 'x' are special after a
8853 * CTRL-V, don't use it for these. */
8854 if (c < 256 && !isalnum(c))
8855 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8856 tw_save = curbuf->b_p_tw;
8857 curbuf->b_p_tw = -1;
8858 insert_special(c, TRUE, FALSE);
8859 curbuf->b_p_tw = tw_save;
8860#ifdef FEAT_RIGHTLEFT
8861 revins_chars++;
8862 revins_legal++;
8863#endif
8864 c = Ctrl_V; /* pretend CTRL-V is last character */
8865 auto_format(FALSE, TRUE);
8866 }
8867 }
8868 return c;
8869}
8870
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871#ifdef FEAT_SMARTINDENT
8872/*
8873 * Try to do some very smart auto-indenting.
8874 * Used when inserting a "normal" character.
8875 */
8876 static void
8877ins_try_si(c)
8878 int c;
8879{
8880 pos_T *pos, old_pos;
8881 char_u *ptr;
8882 int i;
8883 int temp;
8884
8885 /*
8886 * do some very smart indenting when entering '{' or '}'
8887 */
8888 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8889 {
8890 /*
8891 * for '}' set indent equal to indent of line containing matching '{'
8892 */
8893 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8894 {
8895 old_pos = curwin->w_cursor;
8896 /*
8897 * If the matching '{' has a ')' immediately before it (ignoring
8898 * white-space), then line up with the start of the line
8899 * containing the matching '(' if there is one. This handles the
8900 * case where an "if (..\n..) {" statement continues over multiple
8901 * lines -- webb
8902 */
8903 ptr = ml_get(pos->lnum);
8904 i = pos->col;
8905 if (i > 0) /* skip blanks before '{' */
8906 while (--i > 0 && vim_iswhite(ptr[i]))
8907 ;
8908 curwin->w_cursor.lnum = pos->lnum;
8909 curwin->w_cursor.col = i;
8910 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8911 curwin->w_cursor = *pos;
8912 i = get_indent();
8913 curwin->w_cursor = old_pos;
8914#ifdef FEAT_VREPLACE
8915 if (State & VREPLACE_FLAG)
8916 change_indent(INDENT_SET, i, FALSE, NUL);
8917 else
8918#endif
8919 (void)set_indent(i, SIN_CHANGED);
8920 }
8921 else if (curwin->w_cursor.col > 0)
8922 {
8923 /*
8924 * when inserting '{' after "O" reduce indent, but not
8925 * more than indent of previous line
8926 */
8927 temp = TRUE;
8928 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8929 {
8930 old_pos = curwin->w_cursor;
8931 i = get_indent();
8932 while (curwin->w_cursor.lnum > 1)
8933 {
8934 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
8935
8936 /* ignore empty lines and lines starting with '#'. */
8937 if (*ptr != '#' && *ptr != NUL)
8938 break;
8939 }
8940 if (get_indent() >= i)
8941 temp = FALSE;
8942 curwin->w_cursor = old_pos;
8943 }
8944 if (temp)
8945 shift_line(TRUE, FALSE, 1);
8946 }
8947 }
8948
8949 /*
8950 * set indent of '#' always to 0
8951 */
8952 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8953 {
8954 /* remember current indent for next line */
8955 old_indent = get_indent();
8956 (void)set_indent(0, SIN_CHANGED);
8957 }
8958
8959 /* Adjust ai_col, the char at this position can be deleted. */
8960 if (ai_col > curwin->w_cursor.col)
8961 ai_col = curwin->w_cursor.col;
8962}
8963#endif
8964
8965/*
8966 * Get the value that w_virtcol would have when 'list' is off.
8967 * Unless 'cpo' contains the 'L' flag.
8968 */
8969 static colnr_T
8970get_nolist_virtcol()
8971{
8972 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8973 return getvcol_nolist(&curwin->w_cursor);
8974 validate_virtcol();
8975 return curwin->w_virtcol;
8976}