blob: 087ce31322af53871283c85e9d31d417b03c068d [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 Moolenaara94bc432006-03-10 21:42:59 +0000144#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
145static void ins_compl_add_list __ARGS((list_T *list));
146#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000147static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148static void ins_compl_delete __ARGS((void));
149static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000150static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000151static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000152static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000153static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000154static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static int ins_complete __ARGS((int c));
156static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
157#endif /* FEAT_INS_EXPAND */
158
159#define BACKSPACE_CHAR 1
160#define BACKSPACE_WORD 2
161#define BACKSPACE_WORD_NOT_SPACE 3
162#define BACKSPACE_LINE 4
163
Bram Moolenaar754b5602006-02-09 23:53:20 +0000164static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165static void ins_ctrl_v __ARGS((void));
166static void undisplay_dollar __ARGS((void));
167static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000168static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169static void check_auto_format __ARGS((int));
170static void redo_literal __ARGS((int c));
171static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000172#ifdef FEAT_SYN_HL
173static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000174static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000175static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
178static int echeck_abbr __ARGS((int));
179static void replace_push_off __ARGS((int c));
180static int replace_pop __ARGS((void));
181static void replace_join __ARGS((int off));
182static void replace_pop_ins __ARGS((void));
183#ifdef FEAT_MBYTE
184static void mb_replace_pop_ins __ARGS((int cc));
185#endif
186static void replace_flush __ARGS((void));
187static void replace_do_bs __ARGS((void));
188#ifdef FEAT_CINDENT
189static int cindent_on __ARGS((void));
190#endif
191static void ins_reg __ARGS((void));
192static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000193static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000194static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#ifdef FEAT_RIGHTLEFT
196static void ins_ctrl_ __ARGS((void));
197#endif
198#ifdef FEAT_VISUAL
199static int ins_start_select __ARGS((int c));
200#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000201static void ins_insert __ARGS((int replaceState));
202static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203static void ins_shift __ARGS((int c, int lastc));
204static void ins_del __ARGS((void));
205static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
206#ifdef FEAT_MOUSE
207static void ins_mouse __ARGS((int c));
208static void ins_mousescroll __ARGS((int up));
209#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000210#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
211static void ins_tabline __ARGS((int c));
212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213static void ins_left __ARGS((void));
214static void ins_home __ARGS((int c));
215static void ins_end __ARGS((int c));
216static void ins_s_left __ARGS((void));
217static void ins_right __ARGS((void));
218static void ins_s_right __ARGS((void));
219static void ins_up __ARGS((int startcol));
220static void ins_pageup __ARGS((void));
221static void ins_down __ARGS((int startcol));
222static void ins_pagedown __ARGS((void));
223#ifdef FEAT_DND
224static void ins_drop __ARGS((void));
225#endif
226static int ins_tab __ARGS((void));
227static int ins_eol __ARGS((int c));
228#ifdef FEAT_DIGRAPHS
229static int ins_digraph __ARGS((void));
230#endif
231static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000232static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#ifdef FEAT_SMARTINDENT
234static void ins_try_si __ARGS((int c));
235#endif
236static colnr_T get_nolist_virtcol __ARGS((void));
237
238static colnr_T Insstart_textlen; /* length of line when insert started */
239static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
240
241static char_u *last_insert = NULL; /* the text of the previous insert,
242 K_SPECIAL and CSI are escaped */
243static int last_insert_skip; /* nr of chars in front of previous insert */
244static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000245static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247#ifdef FEAT_CINDENT
248static int can_cindent; /* may do cindenting on this line */
249#endif
250
251static int old_indent = 0; /* for ^^D command in insert mode */
252
253#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000254static int revins_on; /* reverse insert mode on */
255static int revins_chars; /* how much to skip after edit */
256static int revins_legal; /* was the last char 'legal'? */
257static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#endif
259
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260static int ins_need_undo; /* call u_save() before inserting a
261 char. Set when edit() is called.
262 after that arrow_used is used. */
263
264static int did_add_space = FALSE; /* auto_format() added an extra space
265 under the cursor */
266
267/*
268 * edit(): Start inserting text.
269 *
270 * "cmdchar" can be:
271 * 'i' normal insert command
272 * 'a' normal append command
273 * 'R' replace command
274 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
275 * but still only one <CR> is inserted. The <Esc> is not used for redo.
276 * 'g' "gI" command.
277 * 'V' "gR" command for Virtual Replace mode.
278 * 'v' "gr" command for single character Virtual Replace mode.
279 *
280 * This function is not called recursively. For CTRL-O commands, it returns
281 * and lets the caller handle the Normal-mode command.
282 *
283 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
284 */
285 int
286edit(cmdchar, startln, count)
287 int cmdchar;
288 int startln; /* if set, insert at start of line */
289 long count;
290{
291 int c = 0;
292 char_u *ptr;
293 int lastc;
294 colnr_T mincol;
295 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 int i;
297 int did_backspace = TRUE; /* previous char was backspace */
298#ifdef FEAT_CINDENT
299 int line_is_white = FALSE; /* line is empty before insert */
300#endif
301 linenr_T old_topline = 0; /* topline before insertion */
302#ifdef FEAT_DIFF
303 int old_topfill = -1;
304#endif
305 int inserted_space = FALSE; /* just inserted a space */
306 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000307 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000309 /* Remember whether editing was restarted after CTRL-O. */
310 did_restart_edit = restart_edit;
311
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 /* sleep before redrawing, needed for "CTRL-O :" that results in an
313 * error message */
314 check_for_delay(TRUE);
315
316#ifdef HAVE_SANDBOX
317 /* Don't allow inserting in the sandbox. */
318 if (sandbox != 0)
319 {
320 EMSG(_(e_sandbox));
321 return FALSE;
322 }
323#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000324 /* Don't allow changes in the buffer while editing the cmdline. The
325 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000326 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000327 {
328 EMSG(_(e_secure));
329 return FALSE;
330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331
332#ifdef FEAT_INS_EXPAND
333 ins_compl_clear(); /* clear stuff for CTRL-X mode */
334#endif
335
Bram Moolenaar843ee412004-06-30 16:16:41 +0000336#ifdef FEAT_AUTOCMD
337 /*
338 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
339 */
340 if (cmdchar != 'r' && cmdchar != 'v')
341 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000342# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000343 if (cmdchar == 'R')
344 ptr = (char_u *)"r";
345 else if (cmdchar == 'V')
346 ptr = (char_u *)"v";
347 else
348 ptr = (char_u *)"i";
349 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000350# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000351 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
352 }
353#endif
354
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355#ifdef FEAT_MOUSE
356 /*
357 * When doing a paste with the middle mouse button, Insstart is set to
358 * where the paste started.
359 */
360 if (where_paste_started.lnum != 0)
361 Insstart = where_paste_started;
362 else
363#endif
364 {
365 Insstart = curwin->w_cursor;
366 if (startln)
367 Insstart.col = 0;
368 }
369 Insstart_textlen = linetabsize(ml_get_curline());
370 Insstart_blank_vcol = MAXCOL;
371 if (!did_ai)
372 ai_col = 0;
373
374 if (cmdchar != NUL && restart_edit == 0)
375 {
376 ResetRedobuff();
377 AppendNumberToRedobuff(count);
378#ifdef FEAT_VREPLACE
379 if (cmdchar == 'V' || cmdchar == 'v')
380 {
381 /* "gR" or "gr" command */
382 AppendCharToRedobuff('g');
383 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
384 }
385 else
386#endif
387 {
388 AppendCharToRedobuff(cmdchar);
389 if (cmdchar == 'g') /* "gI" command */
390 AppendCharToRedobuff('I');
391 else if (cmdchar == 'r') /* "r<CR>" command */
392 count = 1; /* insert only one <CR> */
393 }
394 }
395
396 if (cmdchar == 'R')
397 {
398#ifdef FEAT_FKMAP
399 if (p_fkmap && p_ri)
400 {
401 beep_flush();
402 EMSG(farsi_text_3); /* encoded in Farsi */
403 State = INSERT;
404 }
405 else
406#endif
407 State = REPLACE;
408 }
409#ifdef FEAT_VREPLACE
410 else if (cmdchar == 'V' || cmdchar == 'v')
411 {
412 State = VREPLACE;
413 replaceState = VREPLACE;
414 orig_line_count = curbuf->b_ml.ml_line_count;
415 vr_lines_changed = 1;
416 }
417#endif
418 else
419 State = INSERT;
420
421 stop_insert_mode = FALSE;
422
423 /*
424 * Need to recompute the cursor position, it might move when the cursor is
425 * on a TAB or special character.
426 */
427 curs_columns(TRUE);
428
429 /*
430 * Enable langmap or IME, indicated by 'iminsert'.
431 * Note that IME may enabled/disabled without us noticing here, thus the
432 * 'iminsert' value may not reflect what is actually used. It is updated
433 * when hitting <Esc>.
434 */
435 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
436 State |= LANGMAP;
437#ifdef USE_IM_CONTROL
438 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
439#endif
440
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441#ifdef FEAT_MOUSE
442 setmouse();
443#endif
444#ifdef FEAT_CMDL_INFO
445 clear_showcmd();
446#endif
447#ifdef FEAT_RIGHTLEFT
448 /* there is no reverse replace mode */
449 revins_on = (State == INSERT && p_ri);
450 if (revins_on)
451 undisplay_dollar();
452 revins_chars = 0;
453 revins_legal = 0;
454 revins_scol = -1;
455#endif
456
457 /*
458 * Handle restarting Insert mode.
459 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
460 * restart_edit non-zero, and something in the stuff buffer.
461 */
462 if (restart_edit != 0 && stuff_empty())
463 {
464#ifdef FEAT_MOUSE
465 /*
466 * After a paste we consider text typed to be part of the insert for
467 * the pasted text. You can backspace over the pasted text too.
468 */
469 if (where_paste_started.lnum)
470 arrow_used = FALSE;
471 else
472#endif
473 arrow_used = TRUE;
474 restart_edit = 0;
475
476 /*
477 * If the cursor was after the end-of-line before the CTRL-O and it is
478 * now at the end-of-line, put it after the end-of-line (this is not
479 * correct in very rare cases).
480 * Also do this if curswant is greater than the current virtual
481 * column. Eg after "^O$" or "^O80|".
482 */
483 validate_virtcol();
484 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000485 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 || curwin->w_curswant > curwin->w_virtcol)
487 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
488 {
489 if (ptr[1] == NUL)
490 ++curwin->w_cursor.col;
491#ifdef FEAT_MBYTE
492 else if (has_mbyte)
493 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000494 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 if (ptr[i] == NUL)
496 curwin->w_cursor.col += i;
497 }
498#endif
499 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000500 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 }
502 else
503 arrow_used = FALSE;
504
505 /* we are in insert mode now, don't need to start it anymore */
506 need_start_insertmode = FALSE;
507
508 /* Need to save the line for undo before inserting the first char. */
509 ins_need_undo = TRUE;
510
511#ifdef FEAT_MOUSE
512 where_paste_started.lnum = 0;
513#endif
514#ifdef FEAT_CINDENT
515 can_cindent = TRUE;
516#endif
517#ifdef FEAT_FOLDING
518 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
519 * restarting. */
520 if (!p_im && did_restart_edit == 0)
521 foldOpenCursor();
522#endif
523
524 /*
525 * If 'showmode' is set, show the current (insert/replace/..) mode.
526 * A warning message for changing a readonly file is given here, before
527 * actually changing anything. It's put after the mode, if any.
528 */
529 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000530 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 i = showmode();
532
533 if (!p_im && did_restart_edit == 0)
534 change_warning(i + 1);
535
536#ifdef CURSOR_SHAPE
537 ui_cursor_shape(); /* may show different cursor shape */
538#endif
539#ifdef FEAT_DIGRAPHS
540 do_digraph(-1); /* clear digraphs */
541#endif
542
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000543 /*
544 * Get the current length of the redo buffer, those characters have to be
545 * skipped if we want to get to the inserted characters.
546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 ptr = get_inserted();
548 if (ptr == NULL)
549 new_insert_skip = 0;
550 else
551 {
552 new_insert_skip = (int)STRLEN(ptr);
553 vim_free(ptr);
554 }
555
556 old_indent = 0;
557
558 /*
559 * Main loop in Insert mode: repeat until Insert mode is left.
560 */
561 for (;;)
562 {
563#ifdef FEAT_RIGHTLEFT
564 if (!revins_legal)
565 revins_scol = -1; /* reset on illegal motions */
566 else
567 revins_legal = 0;
568#endif
569 if (arrow_used) /* don't repeat insert when arrow key used */
570 count = 0;
571
572 if (stop_insert_mode)
573 {
574 /* ":stopinsert" used or 'insertmode' reset */
575 count = 0;
576 goto doESCkey;
577 }
578
579 /* set curwin->w_curswant for next K_DOWN or K_UP */
580 if (!arrow_used)
581 curwin->w_set_curswant = TRUE;
582
583 /* If there is no typeahead may check for timestamps (e.g., for when a
584 * menu invoked a shell command). */
585 if (stuff_empty())
586 {
587 did_check_timestamps = FALSE;
588 if (need_check_timestamps)
589 check_timestamps(FALSE);
590 }
591
592 /*
593 * When emsg() was called msg_scroll will have been set.
594 */
595 msg_scroll = FALSE;
596
597#ifdef FEAT_GUI
598 /* When 'mousefocus' is set a mouse movement may have taken us to
599 * another window. "need_mouse_correct" may then be set because of an
600 * autocommand. */
601 if (need_mouse_correct)
602 gui_mouse_correct();
603#endif
604
605#ifdef FEAT_FOLDING
606 /* Open fold at the cursor line, according to 'foldopen'. */
607 if (fdo_flags & FDO_INSERT)
608 foldOpenCursor();
609 /* Close folds where the cursor isn't, according to 'foldclose' */
610 if (!char_avail())
611 foldCheckClose();
612#endif
613
614 /*
615 * If we inserted a character at the last position of the last line in
616 * the window, scroll the window one line up. This avoids an extra
617 * redraw.
618 * This is detected when the cursor column is smaller after inserting
619 * something.
620 * Don't do this when the topline changed already, it has
621 * already been adjusted (by insertchar() calling open_line())).
622 */
623 if (curbuf->b_mod_set
624 && curwin->w_p_wrap
625 && !did_backspace
626 && curwin->w_topline == old_topline
627#ifdef FEAT_DIFF
628 && curwin->w_topfill == old_topfill
629#endif
630 )
631 {
632 mincol = curwin->w_wcol;
633 validate_cursor_col();
634
635 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
636 && curwin->w_wrow == W_WINROW(curwin)
637 + curwin->w_height - 1 - p_so
638 && (curwin->w_cursor.lnum != curwin->w_topline
639#ifdef FEAT_DIFF
640 || curwin->w_topfill > 0
641#endif
642 ))
643 {
644#ifdef FEAT_DIFF
645 if (curwin->w_topfill > 0)
646 --curwin->w_topfill;
647 else
648#endif
649#ifdef FEAT_FOLDING
650 if (hasFolding(curwin->w_topline, NULL, &old_topline))
651 set_topline(curwin, old_topline + 1);
652 else
653#endif
654 set_topline(curwin, curwin->w_topline + 1);
655 }
656 }
657
658 /* May need to adjust w_topline to show the cursor. */
659 update_topline();
660
661 did_backspace = FALSE;
662
663 validate_cursor(); /* may set must_redraw */
664
665 /*
666 * Redraw the display when no characters are waiting.
667 * Also shows mode, ruler and positions cursor.
668 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000669 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
671#ifdef FEAT_SCROLLBIND
672 if (curwin->w_p_scb)
673 do_check_scrollbind(TRUE);
674#endif
675
676 update_curswant();
677 old_topline = curwin->w_topline;
678#ifdef FEAT_DIFF
679 old_topfill = curwin->w_topfill;
680#endif
681
682#ifdef USE_ON_FLY_SCROLL
683 dont_scroll = FALSE; /* allow scrolling here */
684#endif
685
686 /*
687 * Get a character for Insert mode.
688 */
689 lastc = c; /* remember previous char for CTRL-D */
690 c = safe_vgetc();
691
692#ifdef FEAT_RIGHTLEFT
693 if (p_hkmap && KeyTyped)
694 c = hkmap(c); /* Hebrew mode mapping */
695#endif
696#ifdef FEAT_FKMAP
697 if (p_fkmap && KeyTyped)
698 c = fkmap(c); /* Farsi mode mapping */
699#endif
700
701#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000702 /*
703 * Special handling of keys while the popup menu is visible or wanted
704 * and the cursor is still in the completed word.
705 */
706 if (compl_started && pum_wanted() && curwin->w_cursor.col >= compl_col)
Bram Moolenaara6557602006-02-04 22:43:20 +0000707 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000708 /* BS: Delete one character from "compl_leader". */
709 if ((c == K_BS || c == Ctrl_H)
710 && curwin->w_cursor.col > compl_col && ins_compl_bs())
Bram Moolenaara6557602006-02-04 22:43:20 +0000711 continue;
712
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000713 /* When no match was selected or it was edited. */
714 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000715 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000716 /* CTRL-L: Add one character from the current match to
717 * "compl_leader". */
718 if (c == Ctrl_L)
719 {
720 ins_compl_addfrommatch();
721 continue;
722 }
723
Bram Moolenaardf1bdc92006-02-23 21:32:16 +0000724 /* A printable, non-white character: Add to "compl_leader". */
725 if (vim_isprintc(c) && !vim_iswhite(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000726 {
727 ins_compl_addleader(c);
728 continue;
729 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000730
731 /* Pressing Enter selects the current match. */
732 if (c == CAR || c == K_KENTER || c == NL)
733 {
734 ins_compl_delete();
735 ins_compl_insert();
736 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000737 }
738 }
739
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
741 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000742 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000743 if (c != K_IGNORE && ins_compl_prep(c))
744 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745#endif
746
Bram Moolenaar488c6512005-08-11 20:09:58 +0000747 /* CTRL-\ CTRL-N goes to Normal mode,
748 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
749 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 if (c == Ctrl_BSL)
751 {
752 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000753 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 ++no_mapping;
755 ++allow_keys;
756 c = safe_vgetc();
757 --no_mapping;
758 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000759 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000761 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 vungetc(c);
763 c = Ctrl_BSL;
764 }
765 else if (c == Ctrl_G && p_im)
766 continue;
767 else
768 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000769 if (c == Ctrl_O)
770 {
771 ins_ctrl_o();
772 ins_at_eol = FALSE; /* cursor keeps its column */
773 nomove = TRUE;
774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 count = 0;
776 goto doESCkey;
777 }
778 }
779
780#ifdef FEAT_DIGRAPHS
781 c = do_digraph(c);
782#endif
783
784#ifdef FEAT_INS_EXPAND
785 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
786 goto docomplete;
787#endif
788 if (c == Ctrl_V || c == Ctrl_Q)
789 {
790 ins_ctrl_v();
791 c = Ctrl_V; /* pretend CTRL-V is last typed character */
792 continue;
793 }
794
795#ifdef FEAT_CINDENT
796 if (cindent_on()
797# ifdef FEAT_INS_EXPAND
798 && ctrl_x_mode == 0
799# endif
800 )
801 {
802 /* A key name preceded by a bang means this key is not to be
803 * inserted. Skip ahead to the re-indenting below.
804 * A key name preceded by a star means that indenting has to be
805 * done before inserting the key. */
806 line_is_white = inindent(0);
807 if (in_cinkeys(c, '!', line_is_white))
808 goto force_cindent;
809 if (can_cindent && in_cinkeys(c, '*', line_is_white)
810 && stop_arrow() == OK)
811 do_c_expr_indent();
812 }
813#endif
814
815#ifdef FEAT_RIGHTLEFT
816 if (curwin->w_p_rl)
817 switch (c)
818 {
819 case K_LEFT: c = K_RIGHT; break;
820 case K_S_LEFT: c = K_S_RIGHT; break;
821 case K_C_LEFT: c = K_C_RIGHT; break;
822 case K_RIGHT: c = K_LEFT; break;
823 case K_S_RIGHT: c = K_S_LEFT; break;
824 case K_C_RIGHT: c = K_C_LEFT; break;
825 }
826#endif
827
828#ifdef FEAT_VISUAL
829 /*
830 * If 'keymodel' contains "startsel", may start selection. If it
831 * does, a CTRL-O and c will be stuffed, we need to get these
832 * characters.
833 */
834 if (ins_start_select(c))
835 continue;
836#endif
837
838 /*
839 * The big switch to handle a character in insert mode.
840 */
841 switch (c)
842 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000843 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 if (echeck_abbr(ESC + ABBR_OFF))
845 break;
846 /*FALLTHROUGH*/
847
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000848 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849#ifdef FEAT_CMDWIN
850 if (c == Ctrl_C && cmdwin_type != 0)
851 {
852 /* Close the cmdline window. */
853 cmdwin_result = K_IGNORE;
854 got_int = FALSE; /* don't stop executing autocommands et al. */
855 goto doESCkey;
856 }
857#endif
858
859#ifdef UNIX
860do_intr:
861#endif
862 /* when 'insertmode' set, and not halfway a mapping, don't leave
863 * Insert mode */
864 if (goto_im())
865 {
866 if (got_int)
867 {
868 (void)vgetc(); /* flush all buffers */
869 got_int = FALSE;
870 }
871 else
872 vim_beep();
873 break;
874 }
875doESCkey:
876 /*
877 * This is the ONLY return from edit()!
878 */
879 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
880 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000881 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 o_lnum = curwin->w_cursor.lnum;
883
Bram Moolenaar488c6512005-08-11 20:09:58 +0000884 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000885 {
886#ifdef FEAT_AUTOCMD
887 if (cmdchar != 'r' && cmdchar != 'v')
888 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
889 FALSE, curbuf);
890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 continue;
894
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000895 case Ctrl_Z: /* suspend when 'insertmode' set */
896 if (!p_im)
897 goto normalchar; /* insert CTRL-Z as normal char */
898 stuffReadbuff((char_u *)":st\r");
899 c = Ctrl_O;
900 /*FALLTHROUGH*/
901
902 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000903#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000904 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000905 goto docomplete;
906#endif
907 if (echeck_abbr(Ctrl_O + ABBR_OFF))
908 break;
909 ins_ctrl_o();
910 count = 0;
911 goto doESCkey;
912
Bram Moolenaar572cb562005-08-05 21:35:02 +0000913 case K_INS: /* toggle insert/replace mode */
914 case K_KINS:
915 ins_insert(replaceState);
916 break;
917
918 case K_SELECT: /* end of Select mode mapping - ignore */
919 break;
920
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000921#ifdef FEAT_SNIFF
922 case K_SNIFF: /* Sniff command received */
923 stuffcharReadbuff(K_SNIFF);
924 goto doESCkey;
925#endif
926
927 case K_HELP: /* Help key works like <ESC> <Help> */
928 case K_F1:
929 case K_XF1:
930 stuffcharReadbuff(K_HELP);
931 if (p_im)
932 need_start_insertmode = TRUE;
933 goto doESCkey;
934
935#ifdef FEAT_NETBEANS_INTG
936 case K_F21: /* NetBeans command */
937 ++no_mapping; /* don't map the next key hits */
938 i = safe_vgetc();
939 --no_mapping;
940 netbeans_keycommand(i);
941 break;
942#endif
943
944 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 case NUL:
946 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000947 /* For ^@ the trailing ESC will end the insert, unless there is an
948 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
950 && c != Ctrl_A && !p_im)
951 goto doESCkey; /* quit insert mode */
952 inserted_space = FALSE;
953 break;
954
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000955 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 ins_reg();
957 auto_format(FALSE, TRUE);
958 inserted_space = FALSE;
959 break;
960
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000961 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 ins_ctrl_g();
963 break;
964
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000965 case Ctrl_HAT: /* switch input mode and/or langmap */
966 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 break;
968
969#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000970 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 if (!p_ari)
972 goto normalchar;
973 ins_ctrl_();
974 break;
975#endif
976
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000977 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
979 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
980 goto docomplete;
981#endif
982 /* FALLTHROUGH */
983
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000984 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985# ifdef FEAT_INS_EXPAND
986 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
987 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000988 if (has_compl_option(FALSE))
989 goto docomplete;
990 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 }
992# endif
993 ins_shift(c, lastc);
994 auto_format(FALSE, TRUE);
995 inserted_space = FALSE;
996 break;
997
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000998 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 case K_KDEL:
1000 ins_del();
1001 auto_format(FALSE, TRUE);
1002 break;
1003
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001004 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 case Ctrl_H:
1006 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1007 auto_format(FALSE, TRUE);
1008 break;
1009
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001010 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1012 auto_format(FALSE, TRUE);
1013 break;
1014
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001015 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001016# ifdef FEAT_COMPL_FUNC
1017 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001018 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001019 goto docomplete;
1020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1022 auto_format(FALSE, TRUE);
1023 inserted_space = FALSE;
1024 break;
1025
1026#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001027 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 case K_LEFTMOUSE_NM:
1029 case K_LEFTDRAG:
1030 case K_LEFTRELEASE:
1031 case K_LEFTRELEASE_NM:
1032 case K_MIDDLEMOUSE:
1033 case K_MIDDLEDRAG:
1034 case K_MIDDLERELEASE:
1035 case K_RIGHTMOUSE:
1036 case K_RIGHTDRAG:
1037 case K_RIGHTRELEASE:
1038 case K_X1MOUSE:
1039 case K_X1DRAG:
1040 case K_X1RELEASE:
1041 case K_X2MOUSE:
1042 case K_X2DRAG:
1043 case K_X2RELEASE:
1044 ins_mouse(c);
1045 break;
1046
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001047 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 ins_mousescroll(FALSE);
1049 break;
1050
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001051 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 ins_mousescroll(TRUE);
1053 break;
1054#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001055#ifdef FEAT_GUI_TABLINE
1056 case K_TABLINE:
1057 case K_TABMENU:
1058 ins_tabline(c);
1059 break;
1060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001062 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 break;
1064
Bram Moolenaar754b5602006-02-09 23:53:20 +00001065#ifdef FEAT_AUTOCMD
1066 case K_CURSORHOLD: /* Didn't type something for a while. */
1067 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1068 did_cursorhold = TRUE;
1069 break;
1070#endif
1071
Bram Moolenaar4770d092006-01-12 23:22:24 +00001072#ifdef FEAT_GUI_W32
1073 /* On Win32 ignore <M-F4>, we get it when closing the window was
1074 * cancelled. */
1075 case K_F4:
1076 if (mod_mask != MOD_MASK_ALT)
1077 goto normalchar;
1078 break;
1079#endif
1080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081#ifdef FEAT_GUI
1082 case K_VER_SCROLLBAR:
1083 ins_scroll();
1084 break;
1085
1086 case K_HOR_SCROLLBAR:
1087 ins_horscroll();
1088 break;
1089#endif
1090
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001091 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 case K_S_HOME:
1094 case K_C_HOME:
1095 ins_home(c);
1096 break;
1097
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001098 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 case K_S_END:
1101 case K_C_END:
1102 ins_end(c);
1103 break;
1104
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001105 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001106 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1107 ins_s_left();
1108 else
1109 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 break;
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 case K_C_LEFT:
1114 ins_s_left();
1115 break;
1116
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001117 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001118 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1119 ins_s_right();
1120 else
1121 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 break;
1123
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001124 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 case K_C_RIGHT:
1126 ins_s_right();
1127 break;
1128
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001129 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001130#ifdef FEAT_INS_EXPAND
1131 if (pum_visible())
1132 goto docomplete;
1133#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001134 if (mod_mask & MOD_MASK_SHIFT)
1135 ins_pageup();
1136 else
1137 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 break;
1139
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001140 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 case K_PAGEUP:
1142 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001143#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001144 if (pum_visible())
1145 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 ins_pageup();
1148 break;
1149
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001150 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001151#ifdef FEAT_INS_EXPAND
1152 if (pum_visible())
1153 goto docomplete;
1154#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001155 if (mod_mask & MOD_MASK_SHIFT)
1156 ins_pagedown();
1157 else
1158 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 break;
1160
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001161 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 case K_PAGEDOWN:
1163 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001164#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001165 if (pum_visible())
1166 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 ins_pagedown();
1169 break;
1170
1171#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001172 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 ins_drop();
1174 break;
1175#endif
1176
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001177 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 c = TAB;
1179 /* FALLTHROUGH */
1180
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001181 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1183 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1184 goto docomplete;
1185#endif
1186 inserted_space = FALSE;
1187 if (ins_tab())
1188 goto normalchar; /* insert TAB as a normal char */
1189 auto_format(FALSE, TRUE);
1190 break;
1191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001192 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 c = CAR;
1194 /* FALLTHROUGH */
1195 case CAR:
1196 case NL:
1197#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1198 /* In a quickfix window a <CR> jumps to the error under the
1199 * cursor. */
1200 if (bt_quickfix(curbuf) && c == CAR)
1201 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001202 if (curwin->w_llist_ref == NULL) /* quickfix window */
1203 do_cmdline_cmd((char_u *)".cc");
1204 else /* location list window */
1205 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 break;
1207 }
1208#endif
1209#ifdef FEAT_CMDWIN
1210 if (cmdwin_type != 0)
1211 {
1212 /* Execute the command in the cmdline window. */
1213 cmdwin_result = CAR;
1214 goto doESCkey;
1215 }
1216#endif
1217 if (ins_eol(c) && !p_im)
1218 goto doESCkey; /* out of memory */
1219 auto_format(FALSE, FALSE);
1220 inserted_space = FALSE;
1221 break;
1222
1223#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001224 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225# ifdef FEAT_INS_EXPAND
1226 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1227 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001228 if (has_compl_option(TRUE))
1229 goto docomplete;
1230 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 }
1232# endif
1233# ifdef FEAT_DIGRAPHS
1234 c = ins_digraph();
1235 if (c == NUL)
1236 break;
1237# endif
1238 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
1241#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001242 case Ctrl_X: /* Enter CTRL-X mode */
1243 ins_ctrl_x();
1244 break;
1245
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001246 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (ctrl_x_mode != CTRL_X_TAGS)
1248 goto normalchar;
1249 goto docomplete;
1250
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001251 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 if (ctrl_x_mode != CTRL_X_FILES)
1253 goto normalchar;
1254 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001255
1256 case 's': /* Spelling completion after ^X */
1257 case Ctrl_S:
1258 if (ctrl_x_mode != CTRL_X_SPELL)
1259 goto normalchar;
1260 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261#endif
1262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264#ifdef FEAT_INS_EXPAND
1265 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1266#endif
1267 {
1268 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1269 if (p_im)
1270 {
1271 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1272 break;
1273 goto doESCkey;
1274 }
1275 goto normalchar;
1276 }
1277#ifdef FEAT_INS_EXPAND
1278 /* FALLTHROUGH */
1279
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001280 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 case Ctrl_N:
1282 /* if 'complete' is empty then plain ^P is no longer special,
1283 * but it is under other ^X modes */
1284 if (*curbuf->b_p_cpt == NUL
1285 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001286 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 goto normalchar;
1288
1289docomplete:
1290 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001291 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 break;
1293#endif /* FEAT_INS_EXPAND */
1294
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001295 case Ctrl_Y: /* copy from previous line or scroll down */
1296 case Ctrl_E: /* copy from next line or scroll up */
1297 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 break;
1299
1300 default:
1301#ifdef UNIX
1302 if (c == intr_char) /* special interrupt char */
1303 goto do_intr;
1304#endif
1305
1306 /*
1307 * Insert a nomal character.
1308 */
1309normalchar:
1310#ifdef FEAT_SMARTINDENT
1311 /* Try to perform smart-indenting. */
1312 ins_try_si(c);
1313#endif
1314
1315 if (c == ' ')
1316 {
1317 inserted_space = TRUE;
1318#ifdef FEAT_CINDENT
1319 if (inindent(0))
1320 can_cindent = FALSE;
1321#endif
1322 if (Insstart_blank_vcol == MAXCOL
1323 && curwin->w_cursor.lnum == Insstart.lnum)
1324 Insstart_blank_vcol = get_nolist_virtcol();
1325 }
1326
1327 if (vim_iswordc(c) || !echeck_abbr(
1328#ifdef FEAT_MBYTE
1329 /* Add ABBR_OFF for characters above 0x100, this is
1330 * what check_abbr() expects. */
1331 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1332#endif
1333 c))
1334 {
1335 insert_special(c, FALSE, FALSE);
1336#ifdef FEAT_RIGHTLEFT
1337 revins_legal++;
1338 revins_chars++;
1339#endif
1340 }
1341
1342 auto_format(FALSE, TRUE);
1343
1344#ifdef FEAT_FOLDING
1345 /* When inserting a character the cursor line must never be in a
1346 * closed fold. */
1347 foldOpenCursor();
1348#endif
1349 break;
1350 } /* end of switch (c) */
1351
1352 /* If the cursor was moved we didn't just insert a space */
1353 if (arrow_used)
1354 inserted_space = FALSE;
1355
1356#ifdef FEAT_CINDENT
1357 if (can_cindent && cindent_on()
1358# ifdef FEAT_INS_EXPAND
1359 && ctrl_x_mode == 0
1360# endif
1361 )
1362 {
1363force_cindent:
1364 /*
1365 * Indent now if a key was typed that is in 'cinkeys'.
1366 */
1367 if (in_cinkeys(c, ' ', line_is_white))
1368 {
1369 if (stop_arrow() == OK)
1370 /* re-indent the current line */
1371 do_c_expr_indent();
1372 }
1373 }
1374#endif /* FEAT_CINDENT */
1375
1376 } /* for (;;) */
1377 /* NOTREACHED */
1378}
1379
1380/*
1381 * Redraw for Insert mode.
1382 * This is postponed until getting the next character to make '$' in the 'cpo'
1383 * option work correctly.
1384 * Only redraw when there are no characters available. This speeds up
1385 * inserting sequences of characters (e.g., for CTRL-R).
1386 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001387/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001389ins_redraw(ready)
1390 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 if (!char_avail())
1393 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001394#ifdef FEAT_AUTOCMD
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001395 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001396 if (ready && has_cursormovedI()
1397 && !equalpos(last_cursormoved, curwin->w_cursor))
1398 {
1399 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1400 last_cursormoved = curwin->w_cursor;
1401 }
1402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 if (must_redraw)
1404 update_screen(0);
1405 else if (clear_cmdline || redraw_cmdline)
1406 showmode(); /* clear cmdline and show mode */
1407 showruler(FALSE);
1408 setcursor();
1409 emsg_on_display = FALSE; /* may remove error message now */
1410 }
1411}
1412
1413/*
1414 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1415 */
1416 static void
1417ins_ctrl_v()
1418{
1419 int c;
1420
1421 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001422 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 if (redrawing() && !char_avail())
1425 edit_putchar('^', TRUE);
1426 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1427
1428#ifdef FEAT_CMDL_INFO
1429 add_to_showcmd_c(Ctrl_V);
1430#endif
1431
1432 c = get_literal();
1433#ifdef FEAT_CMDL_INFO
1434 clear_showcmd();
1435#endif
1436 insert_special(c, FALSE, TRUE);
1437#ifdef FEAT_RIGHTLEFT
1438 revins_chars++;
1439 revins_legal++;
1440#endif
1441}
1442
1443/*
1444 * Put a character directly onto the screen. It's not stored in a buffer.
1445 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1446 */
1447static int pc_status;
1448#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1449#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1450#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1451#define PC_STATUS_SET 3 /* pc_bytes was filled */
1452#ifdef FEAT_MBYTE
1453static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1454#else
1455static char_u pc_bytes[2]; /* saved bytes */
1456#endif
1457static int pc_attr;
1458static int pc_row;
1459static int pc_col;
1460
1461 void
1462edit_putchar(c, highlight)
1463 int c;
1464 int highlight;
1465{
1466 int attr;
1467
1468 if (ScreenLines != NULL)
1469 {
1470 update_topline(); /* just in case w_topline isn't valid */
1471 validate_cursor();
1472 if (highlight)
1473 attr = hl_attr(HLF_8);
1474 else
1475 attr = 0;
1476 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1477 pc_col = W_WINCOL(curwin);
1478#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1479 pc_status = PC_STATUS_UNSET;
1480#endif
1481#ifdef FEAT_RIGHTLEFT
1482 if (curwin->w_p_rl)
1483 {
1484 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1485# ifdef FEAT_MBYTE
1486 if (has_mbyte)
1487 {
1488 int fix_col = mb_fix_col(pc_col, pc_row);
1489
1490 if (fix_col != pc_col)
1491 {
1492 screen_putchar(' ', pc_row, fix_col, attr);
1493 --curwin->w_wcol;
1494 pc_status = PC_STATUS_RIGHT;
1495 }
1496 }
1497# endif
1498 }
1499 else
1500#endif
1501 {
1502 pc_col += curwin->w_wcol;
1503#ifdef FEAT_MBYTE
1504 if (mb_lefthalve(pc_row, pc_col))
1505 pc_status = PC_STATUS_LEFT;
1506#endif
1507 }
1508
1509 /* save the character to be able to put it back */
1510#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1511 if (pc_status == PC_STATUS_UNSET)
1512#endif
1513 {
1514 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1515 pc_status = PC_STATUS_SET;
1516 }
1517 screen_putchar(c, pc_row, pc_col, attr);
1518 }
1519}
1520
1521/*
1522 * Undo the previous edit_putchar().
1523 */
1524 void
1525edit_unputchar()
1526{
1527 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1528 {
1529#if defined(FEAT_MBYTE)
1530 if (pc_status == PC_STATUS_RIGHT)
1531 ++curwin->w_wcol;
1532 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1533 redrawWinline(curwin->w_cursor.lnum, FALSE);
1534 else
1535#endif
1536 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1537 }
1538}
1539
1540/*
1541 * Called when p_dollar is set: display a '$' at the end of the changed text
1542 * Only works when cursor is in the line that changes.
1543 */
1544 void
1545display_dollar(col)
1546 colnr_T col;
1547{
1548 colnr_T save_col;
1549
1550 if (!redrawing())
1551 return;
1552
1553 cursor_off();
1554 save_col = curwin->w_cursor.col;
1555 curwin->w_cursor.col = col;
1556#ifdef FEAT_MBYTE
1557 if (has_mbyte)
1558 {
1559 char_u *p;
1560
1561 /* If on the last byte of a multi-byte move to the first byte. */
1562 p = ml_get_curline();
1563 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1564 }
1565#endif
1566 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1567 if (curwin->w_wcol < W_WIDTH(curwin))
1568 {
1569 edit_putchar('$', FALSE);
1570 dollar_vcol = curwin->w_virtcol;
1571 }
1572 curwin->w_cursor.col = save_col;
1573}
1574
1575/*
1576 * Call this function before moving the cursor from the normal insert position
1577 * in insert mode.
1578 */
1579 static void
1580undisplay_dollar()
1581{
1582 if (dollar_vcol)
1583 {
1584 dollar_vcol = 0;
1585 redrawWinline(curwin->w_cursor.lnum, FALSE);
1586 }
1587}
1588
1589/*
1590 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1591 * Keep the cursor on the same character.
1592 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1593 * type == INDENT_DEC decrease indent (for CTRL-D)
1594 * type == INDENT_SET set indent to "amount"
1595 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1596 */
1597 void
1598change_indent(type, amount, round, replaced)
1599 int type;
1600 int amount;
1601 int round;
1602 int replaced; /* replaced character, put on replace stack */
1603{
1604 int vcol;
1605 int last_vcol;
1606 int insstart_less; /* reduction for Insstart.col */
1607 int new_cursor_col;
1608 int i;
1609 char_u *ptr;
1610 int save_p_list;
1611 int start_col;
1612 colnr_T vc;
1613#ifdef FEAT_VREPLACE
1614 colnr_T orig_col = 0; /* init for GCC */
1615 char_u *new_line, *orig_line = NULL; /* init for GCC */
1616
1617 /* VREPLACE mode needs to know what the line was like before changing */
1618 if (State & VREPLACE_FLAG)
1619 {
1620 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1621 orig_col = curwin->w_cursor.col;
1622 }
1623#endif
1624
1625 /* for the following tricks we don't want list mode */
1626 save_p_list = curwin->w_p_list;
1627 curwin->w_p_list = FALSE;
1628 vc = getvcol_nolist(&curwin->w_cursor);
1629 vcol = vc;
1630
1631 /*
1632 * For Replace mode we need to fix the replace stack later, which is only
1633 * possible when the cursor is in the indent. Remember the number of
1634 * characters before the cursor if it's possible.
1635 */
1636 start_col = curwin->w_cursor.col;
1637
1638 /* determine offset from first non-blank */
1639 new_cursor_col = curwin->w_cursor.col;
1640 beginline(BL_WHITE);
1641 new_cursor_col -= curwin->w_cursor.col;
1642
1643 insstart_less = curwin->w_cursor.col;
1644
1645 /*
1646 * If the cursor is in the indent, compute how many screen columns the
1647 * cursor is to the left of the first non-blank.
1648 */
1649 if (new_cursor_col < 0)
1650 vcol = get_indent() - vcol;
1651
1652 if (new_cursor_col > 0) /* can't fix replace stack */
1653 start_col = -1;
1654
1655 /*
1656 * Set the new indent. The cursor will be put on the first non-blank.
1657 */
1658 if (type == INDENT_SET)
1659 (void)set_indent(amount, SIN_CHANGED);
1660 else
1661 {
1662#ifdef FEAT_VREPLACE
1663 int save_State = State;
1664
1665 /* Avoid being called recursively. */
1666 if (State & VREPLACE_FLAG)
1667 State = INSERT;
1668#endif
1669 shift_line(type == INDENT_DEC, round, 1);
1670#ifdef FEAT_VREPLACE
1671 State = save_State;
1672#endif
1673 }
1674 insstart_less -= curwin->w_cursor.col;
1675
1676 /*
1677 * Try to put cursor on same character.
1678 * If the cursor is at or after the first non-blank in the line,
1679 * compute the cursor column relative to the column of the first
1680 * non-blank character.
1681 * If we are not in insert mode, leave the cursor on the first non-blank.
1682 * If the cursor is before the first non-blank, position it relative
1683 * to the first non-blank, counted in screen columns.
1684 */
1685 if (new_cursor_col >= 0)
1686 {
1687 /*
1688 * When changing the indent while the cursor is touching it, reset
1689 * Insstart_col to 0.
1690 */
1691 if (new_cursor_col == 0)
1692 insstart_less = MAXCOL;
1693 new_cursor_col += curwin->w_cursor.col;
1694 }
1695 else if (!(State & INSERT))
1696 new_cursor_col = curwin->w_cursor.col;
1697 else
1698 {
1699 /*
1700 * Compute the screen column where the cursor should be.
1701 */
1702 vcol = get_indent() - vcol;
1703 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1704
1705 /*
1706 * Advance the cursor until we reach the right screen column.
1707 */
1708 vcol = last_vcol = 0;
1709 new_cursor_col = -1;
1710 ptr = ml_get_curline();
1711 while (vcol <= (int)curwin->w_virtcol)
1712 {
1713 last_vcol = vcol;
1714#ifdef FEAT_MBYTE
1715 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001716 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 else
1718#endif
1719 ++new_cursor_col;
1720 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1721 }
1722 vcol = last_vcol;
1723
1724 /*
1725 * May need to insert spaces to be able to position the cursor on
1726 * the right screen column.
1727 */
1728 if (vcol != (int)curwin->w_virtcol)
1729 {
1730 curwin->w_cursor.col = new_cursor_col;
1731 i = (int)curwin->w_virtcol - vcol;
1732 ptr = alloc(i + 1);
1733 if (ptr != NULL)
1734 {
1735 new_cursor_col += i;
1736 ptr[i] = NUL;
1737 while (--i >= 0)
1738 ptr[i] = ' ';
1739 ins_str(ptr);
1740 vim_free(ptr);
1741 }
1742 }
1743
1744 /*
1745 * When changing the indent while the cursor is in it, reset
1746 * Insstart_col to 0.
1747 */
1748 insstart_less = MAXCOL;
1749 }
1750
1751 curwin->w_p_list = save_p_list;
1752
1753 if (new_cursor_col <= 0)
1754 curwin->w_cursor.col = 0;
1755 else
1756 curwin->w_cursor.col = new_cursor_col;
1757 curwin->w_set_curswant = TRUE;
1758 changed_cline_bef_curs();
1759
1760 /*
1761 * May have to adjust the start of the insert.
1762 */
1763 if (State & INSERT)
1764 {
1765 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1766 {
1767 if ((int)Insstart.col <= insstart_less)
1768 Insstart.col = 0;
1769 else
1770 Insstart.col -= insstart_less;
1771 }
1772 if ((int)ai_col <= insstart_less)
1773 ai_col = 0;
1774 else
1775 ai_col -= insstart_less;
1776 }
1777
1778 /*
1779 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1780 * If the number of characters before the cursor decreased, need to pop a
1781 * few characters from the replace stack.
1782 * If the number of characters before the cursor increased, need to push a
1783 * few NULs onto the replace stack.
1784 */
1785 if (REPLACE_NORMAL(State) && start_col >= 0)
1786 {
1787 while (start_col > (int)curwin->w_cursor.col)
1788 {
1789 replace_join(0); /* remove a NUL from the replace stack */
1790 --start_col;
1791 }
1792 while (start_col < (int)curwin->w_cursor.col || replaced)
1793 {
1794 replace_push(NUL);
1795 if (replaced)
1796 {
1797 replace_push(replaced);
1798 replaced = NUL;
1799 }
1800 ++start_col;
1801 }
1802 }
1803
1804#ifdef FEAT_VREPLACE
1805 /*
1806 * For VREPLACE mode, we also have to fix the replace stack. In this case
1807 * it is always possible because we backspace over the whole line and then
1808 * put it back again the way we wanted it.
1809 */
1810 if (State & VREPLACE_FLAG)
1811 {
1812 /* If orig_line didn't allocate, just return. At least we did the job,
1813 * even if you can't backspace. */
1814 if (orig_line == NULL)
1815 return;
1816
1817 /* Save new line */
1818 new_line = vim_strsave(ml_get_curline());
1819 if (new_line == NULL)
1820 return;
1821
1822 /* We only put back the new line up to the cursor */
1823 new_line[curwin->w_cursor.col] = NUL;
1824
1825 /* Put back original line */
1826 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1827 curwin->w_cursor.col = orig_col;
1828
1829 /* Backspace from cursor to start of line */
1830 backspace_until_column(0);
1831
1832 /* Insert new stuff into line again */
1833 ins_bytes(new_line);
1834
1835 vim_free(new_line);
1836 }
1837#endif
1838}
1839
1840/*
1841 * Truncate the space at the end of a line. This is to be used only in an
1842 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1843 * modes.
1844 */
1845 void
1846truncate_spaces(line)
1847 char_u *line;
1848{
1849 int i;
1850
1851 /* find start of trailing white space */
1852 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1853 {
1854 if (State & REPLACE_FLAG)
1855 replace_join(0); /* remove a NUL from the replace stack */
1856 }
1857 line[i + 1] = NUL;
1858}
1859
1860#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1861 || defined(FEAT_COMMENTS) || defined(PROTO)
1862/*
1863 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1864 * modes correctly. May also be used when not in insert mode at all.
1865 */
1866 void
1867backspace_until_column(col)
1868 int col;
1869{
1870 while ((int)curwin->w_cursor.col > col)
1871 {
1872 curwin->w_cursor.col--;
1873 if (State & REPLACE_FLAG)
1874 replace_do_bs();
1875 else
1876 (void)del_char(FALSE);
1877 }
1878}
1879#endif
1880
1881#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1882/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001883 * CTRL-X pressed in Insert mode.
1884 */
1885 static void
1886ins_ctrl_x()
1887{
1888 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1889 * CTRL-V works like CTRL-N */
1890 if (ctrl_x_mode != CTRL_X_CMDLINE)
1891 {
1892 /* if the next ^X<> won't ADD nothing, then reset
1893 * compl_cont_status */
1894 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001895 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001896 else
1897 compl_cont_status = 0;
1898 /* We're not sure which CTRL-X mode it will be yet */
1899 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1900 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1901 edit_submode_pre = NULL;
1902 showmode();
1903 }
1904}
1905
1906/*
1907 * Return TRUE if the 'dict' or 'tsr' option can be used.
1908 */
1909 static int
1910has_compl_option(dict_opt)
1911 int dict_opt;
1912{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001913 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
1914 && !curwin->w_p_spell)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001915 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1916 {
1917 ctrl_x_mode = 0;
1918 edit_submode = NULL;
1919 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1920 : (char_u *)_("'thesaurus' option is empty"),
1921 hl_attr(HLF_E));
1922 if (emsg_silent == 0)
1923 {
1924 vim_beep();
1925 setcursor();
1926 out_flush();
1927 ui_delay(2000L, FALSE);
1928 }
1929 return FALSE;
1930 }
1931 return TRUE;
1932}
1933
1934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1936 * This depends on the current mode.
1937 */
1938 int
1939vim_is_ctrl_x_key(c)
1940 int c;
1941{
1942 /* Always allow ^R - let it's results then be checked */
1943 if (c == Ctrl_R)
1944 return TRUE;
1945
Bram Moolenaare3226be2005-12-18 22:10:00 +00001946 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001947 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001948 return TRUE;
1949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 switch (ctrl_x_mode)
1951 {
1952 case 0: /* Not in any CTRL-X mode */
1953 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1954 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001955 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1957 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1958 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001959 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1960 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 case CTRL_X_SCROLL:
1962 return (c == Ctrl_Y || c == Ctrl_E);
1963 case CTRL_X_WHOLE_LINE:
1964 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1965 case CTRL_X_FILES:
1966 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1967 case CTRL_X_DICTIONARY:
1968 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1969 case CTRL_X_THESAURUS:
1970 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1971 case CTRL_X_TAGS:
1972 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1973#ifdef FEAT_FIND_ID
1974 case CTRL_X_PATH_PATTERNS:
1975 return (c == Ctrl_P || c == Ctrl_N);
1976 case CTRL_X_PATH_DEFINES:
1977 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1978#endif
1979 case CTRL_X_CMDLINE:
1980 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1981 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001982#ifdef FEAT_COMPL_FUNC
1983 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001984 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001985 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001986 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001987#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001988 case CTRL_X_SPELL:
1989 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
1991 EMSG(_(e_internal));
1992 return FALSE;
1993}
1994
1995/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001996 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 * case of the originally typed text is used, and the case of the completed
1998 * text is infered, ie this tries to work out what case you probably wanted
1999 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002000 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 */
2002 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002003ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 char_u *str;
2005 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002006 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 char_u *fname;
2008 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002009 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010{
2011 int has_lower = FALSE;
2012 int was_letter = FALSE;
2013 int idx;
2014
2015 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2016 {
2017 /* Infer case of completed part -- webb */
2018 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002019 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002022 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002024 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
2026 has_lower = TRUE;
2027 if (isupper(IObuff[idx]))
2028 {
2029 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002030 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2032 break;
2033 }
2034 }
2035 }
2036
2037 /*
2038 * Rule 2: No lower case, 2nd consecutive letter converted to
2039 * upper case.
2040 */
2041 if (!has_lower)
2042 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002043 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002045 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 && islower(IObuff[idx]))
2047 {
2048 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002049 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2051 break;
2052 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002053 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 }
2055 }
2056
2057 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002058 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002060 return ins_compl_add(IObuff, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002062 return ins_compl_add(str, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063}
2064
2065/*
2066 * Add a match to the list of matches.
2067 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002068 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002069 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002071 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002072ins_compl_add(str, len, icase, fname, extra, cdir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 char_u *str;
2074 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002075 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 char_u *fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002077 char_u *extra; /* extra text for popup menu or NULL */
2078 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002079 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002081 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002082 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083
2084 ui_breakcheck();
2085 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002086 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 if (len < 0)
2088 len = (int)STRLEN(str);
2089
2090 /*
2091 * If the same match is already present, don't add it.
2092 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002093 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002095 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 do
2097 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002098 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002099 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002100 && match->cp_str[len] == NUL)
2101 return NOTDONE;
2102 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002103 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
2105
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002106 /* Remove any popup menu before changing the list of matches. */
2107 ins_compl_del_pum();
2108
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 /*
2110 * Allocate a new match structure.
2111 * Copy the values to the new match structure.
2112 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002113 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002115 return FAIL;
2116 match->cp_number = -1;
2117 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002118 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002119 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
2121 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002122 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002124 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002125
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002127 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2129 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002130 if (fname != NULL
2131 && compl_curr_match
2132 && compl_curr_match->cp_fname != NULL
2133 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002134 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002135 else if (fname != NULL)
2136 {
2137 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002138 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002141 match->cp_fname = NULL;
2142 match->cp_flags = flags;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002143 if (extra != NULL)
2144 match->cp_extra = vim_strsave(extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145
2146 /*
2147 * Link the new match structure in the list of matches.
2148 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002149 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002150 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 else if (dir == FORWARD)
2152 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002153 match->cp_next = compl_curr_match->cp_next;
2154 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 else /* BACKWARD */
2157 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002158 match->cp_next = compl_curr_match;
2159 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002161 if (match->cp_next)
2162 match->cp_next->cp_prev = match;
2163 if (match->cp_prev)
2164 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002166 compl_first_match = match;
2167 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002169 /*
2170 * Find the longest common string if still doing that.
2171 */
2172 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2173 ins_compl_longest_match(match);
2174
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 return OK;
2176}
2177
2178/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002179 * Return TRUE if "str[len]" matches with match->cp_str, considering
2180 * match->cp_icase.
2181 */
2182 static int
2183ins_compl_equal(match, str, len)
2184 compl_T *match;
2185 char_u *str;
2186 int len;
2187{
2188 if (match->cp_icase)
2189 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2190 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2191}
2192
2193/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002194 * Reduce the longest common string for match "match".
2195 */
2196 static void
2197ins_compl_longest_match(match)
2198 compl_T *match;
2199{
2200 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002201 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002202 int had_match;
2203
2204 if (compl_leader == NULL)
2205 /* First match, use it as a whole. */
2206 compl_leader = vim_strsave(match->cp_str);
2207 else
2208 {
2209 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002210 p = compl_leader;
2211 s = match->cp_str;
2212 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002213 {
2214#ifdef FEAT_MBYTE
2215 if (has_mbyte)
2216 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002217 c1 = mb_ptr2char(p);
2218 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002219 }
2220 else
2221#endif
2222 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002223 c1 = *p;
2224 c2 = *s;
2225 }
2226 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2227 : (c1 != c2))
2228 break;
2229#ifdef FEAT_MBYTE
2230 if (has_mbyte)
2231 {
2232 mb_ptr_adv(p);
2233 mb_ptr_adv(s);
2234 }
2235 else
2236#endif
2237 {
2238 ++p;
2239 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002240 }
2241 }
2242
2243 if (*p != NUL)
2244 {
2245 /* Leader was shortened, need to change the inserted text. */
2246 *p = NUL;
2247 had_match = (curwin->w_cursor.col > compl_col);
2248 ins_compl_delete();
2249 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2250 ins_redraw(FALSE);
2251
2252 /* When the match isn't there (to avoid matching itself) remove it
2253 * again after redrawing. */
2254 if (!had_match)
2255 ins_compl_delete();
2256 }
2257
2258 compl_used_match = FALSE;
2259 }
2260}
2261
2262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 * Add an array of matches to the list of matches.
2264 * Frees matches[].
2265 */
2266 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002267ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 int num_matches;
2269 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002270 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
2272 int i;
2273 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002274 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275
Bram Moolenaar572cb562005-08-05 21:35:02 +00002276 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002277 if ((add_r = ins_compl_add(matches[i], -1, icase,
2278 NULL, NULL, dir, 0)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002280 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 FreeWild(num_matches, matches);
2282}
2283
2284/* Make the completion list cyclic.
2285 * Return the number of matches (excluding the original).
2286 */
2287 static int
2288ins_compl_make_cyclic()
2289{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002290 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 int count = 0;
2292
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002293 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
2295 /*
2296 * Find the end of the list.
2297 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002298 match = compl_first_match;
2299 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002300 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002302 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 ++count;
2304 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002305 match->cp_next = compl_first_match;
2306 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
2308 return count;
2309}
2310
Bram Moolenaara94bc432006-03-10 21:42:59 +00002311/*
2312 * Start completion for the complete() function.
2313 * "startcol" is where the matched text starts (1 is first column).
2314 * "list" is the list of matches.
2315 */
2316 void
2317set_completion(startcol, list)
2318 int startcol;
2319 list_T *list;
2320{
2321 /* If already doing completions stop it. */
2322 if (ctrl_x_mode != 0)
2323 ins_compl_prep(' ');
2324 ins_compl_clear();
2325
2326 if (stop_arrow() == FAIL)
2327 return;
2328
2329 if (startcol > curwin->w_cursor.col)
2330 startcol = curwin->w_cursor.col;
2331 compl_col = startcol;
2332 compl_length = curwin->w_cursor.col - startcol;
2333 /* compl_pattern doesn't need to be set */
2334 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2335 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
2336 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
2337 return;
2338
2339 /* Handle like dictionary completion. */
2340 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2341
2342 ins_compl_add_list(list);
2343 compl_matches = ins_compl_make_cyclic();
2344 compl_started = TRUE;
2345 compl_used_match = TRUE;
2346
2347 compl_curr_match = compl_first_match;
2348 ins_complete(Ctrl_N);
2349 out_flush();
2350}
2351
2352
Bram Moolenaar9372a112005-12-06 19:59:18 +00002353/* "compl_match_array" points the currently displayed list of entries in the
2354 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002355static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002356static int compl_match_arraysize;
2357
2358/*
2359 * Update the screen and when there is any scrolling remove the popup menu.
2360 */
2361 static void
2362ins_compl_upd_pum()
2363{
2364 int h;
2365
2366 if (compl_match_array != NULL)
2367 {
2368 h = curwin->w_cline_height;
2369 update_screen(0);
2370 if (h != curwin->w_cline_height)
2371 ins_compl_del_pum();
2372 }
2373}
2374
2375/*
2376 * Remove any popup menu.
2377 */
2378 static void
2379ins_compl_del_pum()
2380{
2381 if (compl_match_array != NULL)
2382 {
2383 pum_undisplay();
2384 vim_free(compl_match_array);
2385 compl_match_array = NULL;
2386 }
2387}
2388
2389/*
2390 * Return TRUE if the popup menu should be displayed.
2391 */
2392 static int
2393pum_wanted()
2394{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002395 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002396 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002397 return FALSE;
2398
2399 /* The display looks bad on a B&W display. */
2400 if (t_colors < 8
2401#ifdef FEAT_GUI
2402 && !gui.in_use
2403#endif
2404 )
2405 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002406 return TRUE;
2407}
2408
2409/*
2410 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002411 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002412 */
2413 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002414pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002415{
2416 compl_T *compl;
2417 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002418
2419 /* Don't display the popup menu if there are no matches or there is only
2420 * one (ignoring the original text). */
2421 compl = compl_first_match;
2422 i = 0;
2423 do
2424 {
2425 if (compl == NULL
2426 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2427 break;
2428 compl = compl->cp_next;
2429 } while (compl != compl_first_match);
2430
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002431 if (strstr((char *)p_cot, "menuone") != NULL)
2432 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002433 return (i >= 2);
2434}
2435
2436/*
2437 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002438 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002439 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002440 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002441ins_compl_show_pum()
2442{
2443 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002444 compl_T *shown_compl = NULL;
2445 int did_find_shown_match = FALSE;
2446 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002447 int i;
2448 int cur = -1;
2449 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002450 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002451
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002452 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002453 return;
2454
2455 /* Update the screen before drawing the popup menu over it. */
2456 update_screen(0);
2457
2458 if (compl_match_array == NULL)
2459 {
2460 /* Need to build the popup menu list. */
2461 compl_match_arraysize = 0;
2462 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002463 if (compl_leader != NULL)
2464 lead_len = STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002465 do
2466 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002467 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2468 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002469 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002470 ++compl_match_arraysize;
2471 compl = compl->cp_next;
2472 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002473 if (compl_match_arraysize == 0)
2474 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002475 compl_match_array = (pumitem_T *)alloc_clear(
2476 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002477 * compl_match_arraysize));
2478 if (compl_match_array != NULL)
2479 {
2480 i = 0;
2481 compl = compl_first_match;
2482 do
2483 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002484 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2485 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002486 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002487 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002488 if (!shown_match_ok)
2489 {
2490 if (compl == compl_shown_match || did_find_shown_match)
2491 {
2492 /* This item is the shown match or this is the
2493 * first displayed item after the shown match. */
2494 compl_shown_match = compl;
2495 did_find_shown_match = TRUE;
2496 shown_match_ok = TRUE;
2497 }
2498 else
2499 /* Remember this displayed match for when the
2500 * shown match is just below it. */
2501 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002502 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002503 }
2504 compl_match_array[i].pum_text = compl->cp_str;
2505 if (compl->cp_extra != NULL)
2506 compl_match_array[i++].pum_extra = compl->cp_extra;
2507 else
2508 compl_match_array[i++].pum_extra = compl->cp_fname;
2509 }
2510
2511 if (compl == compl_shown_match)
2512 {
2513 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002514
2515 /* When the original text is the shown match don't set
2516 * compl_shown_match. */
2517 if (compl->cp_flags & ORIGINAL_TEXT)
2518 shown_match_ok = TRUE;
2519
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002520 if (!shown_match_ok && shown_compl != NULL)
2521 {
2522 /* The shown match isn't displayed, set it to the
2523 * previously displayed match. */
2524 compl_shown_match = shown_compl;
2525 shown_match_ok = TRUE;
2526 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002527 }
2528 compl = compl->cp_next;
2529 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002530
2531 if (!shown_match_ok) /* no displayed match at all */
2532 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002533 }
2534 }
2535 else
2536 {
2537 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002538 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002539 if (compl_match_array[i].pum_text == compl_shown_match->cp_str)
Bram Moolenaara6557602006-02-04 22:43:20 +00002540 break;
2541 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002542 }
2543
2544 if (compl_match_array != NULL)
2545 {
2546 /* Compute the screen column of the start of the completed text.
2547 * Use the cursor to get all wrapping and other settings right. */
2548 col = curwin->w_cursor.col;
2549 curwin->w_cursor.col = compl_col;
2550 validate_cursor_col();
2551 pum_display(compl_match_array, compl_match_arraysize, cur,
2552 curwin->w_cline_row + W_WINROW(curwin),
2553 curwin->w_cline_height,
Bram Moolenaar280f1262006-01-30 00:14:18 +00002554 curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002555 curwin->w_cursor.col = col;
2556 }
2557}
2558
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559#define DICT_FIRST (1) /* use just first element in "dict" */
2560#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002561
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002563 * Add any identifiers that match the given pattern in the list of dictionary
2564 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 */
2566 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002567ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2568 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002570 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002571 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002573 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 char_u *ptr;
2575 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 char_u **files;
2578 int count;
2579 int i;
2580 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002581 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
Bram Moolenaar0b238792006-03-02 22:49:12 +00002583 if (*dict == NUL)
2584 {
2585#ifdef FEAT_SYN_HL
2586 /* When 'dictionary' is empty and spell checking is enabled use
2587 * "spell". */
2588 if (!thesaurus && curwin->w_p_spell)
2589 dict = (char_u *)"spell";
2590 else
2591#endif
2592 return;
2593 }
2594
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002596 if (buf == NULL)
2597 return;
2598
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 /* If 'infercase' is set, don't use 'smartcase' here */
2600 save_p_scs = p_scs;
2601 if (curbuf->b_p_inf)
2602 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002603
2604 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2605 * to only match at the start of a line. Otherwise just match the
2606 * pattern. */
2607 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2608 {
2609 i = STRLEN(pat) + 8;
2610 ptr = alloc(i);
2611 if (ptr == NULL)
2612 return;
2613 vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat);
2614 regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0);
2615 vim_free(ptr);
2616 }
2617 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002618 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002619 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002620 if (regmatch.regprog == NULL)
2621 goto theend;
2622 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002623
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2625 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002626 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 {
2628 /* copy one dictionary file name into buf */
2629 if (flags == DICT_EXACT)
2630 {
2631 count = 1;
2632 files = &dict;
2633 }
2634 else
2635 {
2636 /* Expand wildcards in the dictionary name, but do not allow
2637 * backticks (for security, the 'dict' option may have been set in
2638 * a modeline). */
2639 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaar0b238792006-03-02 22:49:12 +00002640 if (!thesaurus && STRCMP(buf, "spell") == 0)
2641 count = -1;
2642 else if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 || expand_wildcards(1, &buf, &count, &files,
2644 EW_FILE|EW_SILENT) != OK)
2645 count = 0;
2646 }
2647
Bram Moolenaar0b238792006-03-02 22:49:12 +00002648 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002650 /* Complete from active spelling. Skip "\<" in the pattern, we
2651 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002652 if (pat[0] == '\\' && pat[1] == '<')
2653 ptr = pat + 2;
2654 else
2655 ptr = pat;
2656 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002658 else
2659 {
2660 ins_compl_files(count, files, thesaurus, flags,
2661 &regmatch, buf, &dir);
2662 if (flags != DICT_EXACT)
2663 FreeWild(count, files);
2664 }
2665 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 break;
2667 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002668
2669theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 p_scs = save_p_scs;
2671 vim_free(regmatch.regprog);
2672 vim_free(buf);
2673}
2674
Bram Moolenaar0b238792006-03-02 22:49:12 +00002675 static void
2676ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2677 int count;
2678 char_u **files;
2679 int thesaurus;
2680 int flags;
2681 regmatch_T *regmatch;
2682 char_u *buf;
2683 int *dir;
2684{
2685 char_u *ptr;
2686 int i;
2687 FILE *fp;
2688 int add_r;
2689
2690 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2691 {
2692 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2693 if (flags != DICT_EXACT)
2694 {
2695 vim_snprintf((char *)IObuff, IOSIZE,
2696 _("Scanning dictionary: %s"), (char *)files[i]);
2697 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2698 }
2699
2700 if (fp != NULL)
2701 {
2702 /*
2703 * Read dictionary file line by line.
2704 * Check each line for a match.
2705 */
2706 while (!got_int && !compl_interrupted
2707 && !vim_fgets(buf, LSIZE, fp))
2708 {
2709 ptr = buf;
2710 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2711 {
2712 ptr = regmatch->startp[0];
2713 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2714 ptr = find_line_end(ptr);
2715 else
2716 ptr = find_word_end(ptr);
2717 add_r = ins_compl_add_infercase(regmatch->startp[0],
2718 (int)(ptr - regmatch->startp[0]),
2719 p_ic, files[i], *dir, 0);
2720 if (thesaurus)
2721 {
2722 char_u *wstart;
2723
2724 /*
2725 * Add the other matches on the line
2726 */
2727 while (!got_int)
2728 {
2729 /* Find start of the next word. Skip white
2730 * space and punctuation. */
2731 ptr = find_word_start(ptr);
2732 if (*ptr == NUL || *ptr == NL)
2733 break;
2734 wstart = ptr;
2735
2736 /* Find end of the word and add it. */
2737#ifdef FEAT_MBYTE
2738 if (has_mbyte)
2739 /* Japanese words may have characters in
2740 * different classes, only separate words
2741 * with single-byte non-word characters. */
2742 while (*ptr != NUL)
2743 {
2744 int l = (*mb_ptr2len)(ptr);
2745
2746 if (l < 2 && !vim_iswordc(*ptr))
2747 break;
2748 ptr += l;
2749 }
2750 else
2751#endif
2752 ptr = find_word_end(ptr);
2753 add_r = ins_compl_add_infercase(wstart,
2754 (int)(ptr - wstart),
2755 p_ic, files[i], *dir, 0);
2756 }
2757 }
2758 if (add_r == OK)
2759 /* if dir was BACKWARD then honor it just once */
2760 *dir = FORWARD;
2761 else if (add_r == FAIL)
2762 break;
2763 /* avoid expensive call to vim_regexec() when at end
2764 * of line */
2765 if (*ptr == '\n' || got_int)
2766 break;
2767 }
2768 line_breakcheck();
2769 ins_compl_check_keys(50);
2770 }
2771 fclose(fp);
2772 }
2773 }
2774}
2775
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776/*
2777 * Find the start of the next word.
2778 * Returns a pointer to the first char of the word. Also stops at a NUL.
2779 */
2780 char_u *
2781find_word_start(ptr)
2782 char_u *ptr;
2783{
2784#ifdef FEAT_MBYTE
2785 if (has_mbyte)
2786 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002787 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 else
2789#endif
2790 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2791 ++ptr;
2792 return ptr;
2793}
2794
2795/*
2796 * Find the end of the word. Assumes it starts inside a word.
2797 * Returns a pointer to just after the word.
2798 */
2799 char_u *
2800find_word_end(ptr)
2801 char_u *ptr;
2802{
2803#ifdef FEAT_MBYTE
2804 int start_class;
2805
2806 if (has_mbyte)
2807 {
2808 start_class = mb_get_class(ptr);
2809 if (start_class > 1)
2810 while (*ptr != NUL)
2811 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002812 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 if (mb_get_class(ptr) != start_class)
2814 break;
2815 }
2816 }
2817 else
2818#endif
2819 while (vim_iswordc(*ptr))
2820 ++ptr;
2821 return ptr;
2822}
2823
2824/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002825 * Find the end of the line, omitting CR and NL at the end.
2826 * Returns a pointer to just after the line.
2827 */
2828 static char_u *
2829find_line_end(ptr)
2830 char_u *ptr;
2831{
2832 char_u *s;
2833
2834 s = ptr + STRLEN(ptr);
2835 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2836 --s;
2837 return s;
2838}
2839
2840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 * Free the list of completions
2842 */
2843 static void
2844ins_compl_free()
2845{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002846 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002848 vim_free(compl_pattern);
2849 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002850 vim_free(compl_leader);
2851 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002853 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002855
2856 ins_compl_del_pum();
2857 pum_clear();
2858
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002859 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 do
2861 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002862 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002863 compl_curr_match = compl_curr_match->cp_next;
2864 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002866 if (match->cp_flags & FREE_FNAME)
2867 vim_free(match->cp_fname);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002868 vim_free(match->cp_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002870 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2871 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872}
2873
2874 static void
2875ins_compl_clear()
2876{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002877 compl_cont_status = 0;
2878 compl_started = FALSE;
2879 compl_matches = 0;
2880 vim_free(compl_pattern);
2881 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002882 vim_free(compl_leader);
2883 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002885 vim_free(compl_orig_text);
2886 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887}
2888
2889/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002890 * Return TRUE when Insert completion is active.
2891 */
2892 int
2893ins_compl_active()
2894{
2895 return compl_started;
2896}
2897
2898/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002899 * Delete one character before the cursor and show the subset of the matches
2900 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002901 * Returns TRUE if the work is done and another char to be got from the user.
2902 */
2903 static int
2904ins_compl_bs()
2905{
2906 char_u *line;
2907 char_u *p;
2908
2909 if (curwin->w_cursor.col <= compl_col + compl_length)
2910 {
2911 /* Deleted more than what was used to find matches, need to look for
2912 * matches all over again. */
2913 ins_compl_free();
2914 compl_started = FALSE;
2915 compl_matches = 0;
2916 }
2917
2918 line = ml_get_curline();
2919 p = line + curwin->w_cursor.col;
2920 mb_ptr_back(line, p);
2921
2922 vim_free(compl_leader);
2923 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2924 if (compl_leader != NULL)
2925 {
2926 ins_compl_del_pum();
2927 ins_compl_delete();
2928 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2929
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002930 if (compl_started)
2931 ins_compl_set_original_text(compl_leader);
2932 else
Bram Moolenaara6557602006-02-04 22:43:20 +00002933 {
2934 /* Matches were cleared, need to search for them now. */
2935 if (ins_complete(Ctrl_N) == FAIL)
2936 compl_cont_status = 0;
2937 else
2938 {
2939 /* Remove the completed word again. */
2940 ins_compl_delete();
2941 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2942 }
2943 }
2944
2945 /* Show the popup menu with a different set of matches. */
2946 ins_compl_show_pum();
2947 compl_used_match = FALSE;
2948
2949 return TRUE;
2950 }
2951 return FALSE;
2952}
2953
2954/*
2955 * Append one character to the match leader. May reduce the number of
2956 * matches.
2957 */
2958 static void
2959ins_compl_addleader(c)
2960 int c;
2961{
2962#ifdef FEAT_MBYTE
2963 int cc;
2964
2965 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2966 {
2967 char_u buf[MB_MAXBYTES + 1];
2968
2969 (*mb_char2bytes)(c, buf);
2970 buf[cc] = NUL;
2971 ins_char_bytes(buf, cc);
2972 }
2973 else
2974#endif
2975 ins_char(c);
2976
2977 vim_free(compl_leader);
2978 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
2979 curwin->w_cursor.col - compl_col);
2980 if (compl_leader != NULL)
2981 {
2982 /* Show the popup menu with a different set of matches. */
2983 ins_compl_del_pum();
2984 ins_compl_show_pum();
2985 compl_used_match = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002986 ins_compl_set_original_text(compl_leader);
2987 }
2988}
2989
2990/*
2991 * Set the first match, the original text.
2992 */
2993 static void
2994ins_compl_set_original_text(str)
2995 char_u *str;
2996{
2997 char_u *p;
2998
2999 /* Replace the original text entry. */
3000 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3001 {
3002 p = vim_strsave(str);
3003 if (p != NULL)
3004 {
3005 vim_free(compl_first_match->cp_str);
3006 compl_first_match->cp_str = p;
3007 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003008 }
3009}
3010
3011/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003012 * Append one character to the match leader. May reduce the number of
3013 * matches.
3014 */
3015 static void
3016ins_compl_addfrommatch()
3017{
3018 char_u *p;
3019 int len = curwin->w_cursor.col - compl_col;
3020 int c;
3021
3022 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003023 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003024 return;
3025 p += len;
3026#ifdef FEAT_MBYTE
3027 c = mb_ptr2char(p);
3028#else
3029 c = *p;
3030#endif
3031 ins_compl_addleader(c);
3032}
3033
3034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003036 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003037 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003039 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040ins_compl_prep(c)
3041 int c;
3042{
3043 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 int temp;
3045 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003046 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047
3048 /* Forget any previous 'special' messages if this is actually
3049 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3050 */
3051 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3052 edit_submode_extra = NULL;
3053
3054 /* Ignore end of Select mode mapping */
3055 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003056 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003058 /* Set "compl_get_longest" when finding the first matches. */
3059 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3060 || (ctrl_x_mode == 0 && !compl_started))
3061 {
3062 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3063 compl_used_match = TRUE;
3064 }
3065
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3067 {
3068 /*
3069 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3070 * it will be yet. Now we decide.
3071 */
3072 switch (c)
3073 {
3074 case Ctrl_E:
3075 case Ctrl_Y:
3076 ctrl_x_mode = CTRL_X_SCROLL;
3077 if (!(State & REPLACE_FLAG))
3078 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3079 else
3080 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3081 edit_submode_pre = NULL;
3082 showmode();
3083 break;
3084 case Ctrl_L:
3085 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3086 break;
3087 case Ctrl_F:
3088 ctrl_x_mode = CTRL_X_FILES;
3089 break;
3090 case Ctrl_K:
3091 ctrl_x_mode = CTRL_X_DICTIONARY;
3092 break;
3093 case Ctrl_R:
3094 /* Simply allow ^R to happen without affecting ^X mode */
3095 break;
3096 case Ctrl_T:
3097 ctrl_x_mode = CTRL_X_THESAURUS;
3098 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003099#ifdef FEAT_COMPL_FUNC
3100 case Ctrl_U:
3101 ctrl_x_mode = CTRL_X_FUNCTION;
3102 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003103 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003104 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003105 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003106#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003107 case 's':
3108 case Ctrl_S:
3109 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003110#ifdef FEAT_SYN_HL
3111 spell_back_to_badword();
3112#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003113 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 case Ctrl_RSB:
3115 ctrl_x_mode = CTRL_X_TAGS;
3116 break;
3117#ifdef FEAT_FIND_ID
3118 case Ctrl_I:
3119 case K_S_TAB:
3120 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3121 break;
3122 case Ctrl_D:
3123 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3124 break;
3125#endif
3126 case Ctrl_V:
3127 case Ctrl_Q:
3128 ctrl_x_mode = CTRL_X_CMDLINE;
3129 break;
3130 case Ctrl_P:
3131 case Ctrl_N:
3132 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3133 * just started ^X mode, or there were enough ^X's to cancel
3134 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3135 * do normal expansion when interrupting a different mode (say
3136 * ^X^F^X^P or ^P^X^X^P, see below)
3137 * nothing changes if interrupting mode 0, (eg, the flag
3138 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003139 if (!(compl_cont_status & CONT_INTRPT))
3140 compl_cont_status |= CONT_LOCAL;
3141 else if (compl_cont_mode != 0)
3142 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 /* FALLTHROUGH */
3144 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003145 /* If we have typed at least 2 ^X's... for modes != 0, we set
3146 * compl_cont_status = 0 (eg, as if we had just started ^X
3147 * mode).
3148 * For mode 0, we set "compl_cont_mode" to an impossible
3149 * value, in both cases ^X^X can be used to restart the same
3150 * mode (avoiding ADDING mode).
3151 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3152 * 'complete' and local ^P expansions respectively.
3153 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3154 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 if (c == Ctrl_X)
3156 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003157 if (compl_cont_mode != 0)
3158 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003160 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 }
3162 ctrl_x_mode = 0;
3163 edit_submode = NULL;
3164 showmode();
3165 break;
3166 }
3167 }
3168 else if (ctrl_x_mode != 0)
3169 {
3170 /* We're already in CTRL-X mode, do we stay in it? */
3171 if (!vim_is_ctrl_x_key(c))
3172 {
3173 if (ctrl_x_mode == CTRL_X_SCROLL)
3174 ctrl_x_mode = 0;
3175 else
3176 ctrl_x_mode = CTRL_X_FINISHED;
3177 edit_submode = NULL;
3178 }
3179 showmode();
3180 }
3181
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003182 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 {
3184 /* Show error message from attempted keyword completion (probably
3185 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003186 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003188 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3189 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 || ctrl_x_mode == CTRL_X_FINISHED)
3191 {
3192 /* Get here when we have finished typing a sequence of ^N and
3193 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003194 * memory that was used, and make sure we can redo the insert. */
3195 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003197 char_u *p;
3198
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 /*
3200 * If any of the original typed text has been changed,
3201 * eg when ignorecase is set, we must add back-spaces to
3202 * the redo buffer. We add as few as necessary to delete
3203 * just the part of the original text that has changed.
3204 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003205 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003206 p = compl_orig_text;
3207 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003209 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 ++ptr;
3211 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003212 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00003214 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 }
3216
3217#ifdef FEAT_CINDENT
3218 want_cindent = (can_cindent && cindent_on());
3219#endif
3220 /*
3221 * When completing whole lines: fix indent for 'cindent'.
3222 * Otherwise, break line if it's too long.
3223 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003224 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 {
3226#ifdef FEAT_CINDENT
3227 /* re-indent the current line */
3228 if (want_cindent)
3229 {
3230 do_c_expr_indent();
3231 want_cindent = FALSE; /* don't do it again */
3232 }
3233#endif
3234 }
3235 else
3236 {
3237 /* put the cursor on the last char, for 'tw' formatting */
3238 curwin->w_cursor.col--;
3239 if (stop_arrow() == OK)
3240 insertchar(NUL, 0, -1);
3241 curwin->w_cursor.col++;
3242 }
3243
3244 auto_format(FALSE, TRUE);
3245
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003246 /* if the popup menu is displayed hitting Enter means accepting
3247 * the selection without inserting anything. */
3248 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
3249 retval = TRUE;
3250
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003252 compl_started = FALSE;
3253 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 msg_clr_cmdline(); /* necessary for "noshowmode" */
3255 ctrl_x_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 if (edit_submode != NULL)
3257 {
3258 edit_submode = NULL;
3259 showmode();
3260 }
3261
3262#ifdef FEAT_CINDENT
3263 /*
3264 * Indent now if a key was typed that is in 'cinkeys'.
3265 */
3266 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3267 do_c_expr_indent();
3268#endif
3269 }
3270 }
3271
3272 /* reset continue_* if we left expansion-mode, if we stay they'll be
3273 * (re)set properly in ins_complete() */
3274 if (!vim_is_ctrl_x_key(c))
3275 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003276 compl_cont_status = 0;
3277 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003279
3280 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281}
3282
3283/*
3284 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3285 * (depending on flag) starting from buf and looking for a non-scanned
3286 * buffer (other than curbuf). curbuf is special, if it is called with
3287 * buf=curbuf then it has to be the first call for a given flag/expansion.
3288 *
3289 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3290 */
3291 static buf_T *
3292ins_compl_next_buf(buf, flag)
3293 buf_T *buf;
3294 int flag;
3295{
3296#ifdef FEAT_WINDOWS
3297 static win_T *wp;
3298#endif
3299
3300 if (flag == 'w') /* just windows */
3301 {
3302#ifdef FEAT_WINDOWS
3303 if (buf == curbuf) /* first call for this flag/expansion */
3304 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003305 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 && wp->w_buffer->b_scanned)
3307 ;
3308 buf = wp->w_buffer;
3309#else
3310 buf = curbuf;
3311#endif
3312 }
3313 else
3314 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3315 * (unlisted buffers)
3316 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003317 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 && ((flag == 'U'
3319 ? buf->b_p_bl
3320 : (!buf->b_p_bl
3321 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003322 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 ;
3324 return buf;
3325}
3326
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003327#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003328static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003329
3330/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003331 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003332 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003333 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003334 static void
3335expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003336 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003337 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003338{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003339 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003340 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003341 char_u *funcname;
3342 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003343
Bram Moolenaare344bea2005-09-01 20:46:49 +00003344 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3345 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003346 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003347
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003348 /* Call 'completefunc' to obtain the list of matches. */
3349 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003350 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003351
Bram Moolenaare344bea2005-09-01 20:46:49 +00003352 pos = curwin->w_cursor;
3353 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3354 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003355 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003356 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003357
Bram Moolenaara94bc432006-03-10 21:42:59 +00003358 ins_compl_add_list(matchlist);
3359 list_unref(matchlist);
3360}
3361#endif /* FEAT_COMPL_FUNC */
3362
3363#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
3364/*
3365 * Add completions from a list.
3366 * Unreferences the list.
3367 */
3368 static void
3369ins_compl_add_list(list)
3370 list_T *list;
3371{
3372 listitem_T *li;
3373 int icase;
3374 char_u *p;
3375 char_u *x;
3376 int dir = compl_direction;
3377
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003378 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003379 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003380 {
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003381 icase = p_ic;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003382 if (li->li_tv.v_type == VAR_DICT && li->li_tv.vval.v_dict != NULL)
3383 {
3384 p = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"word", FALSE);
3385 x = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"menu", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003386 if (get_dict_string(li->li_tv.vval.v_dict, (char_u *)"icase",
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003387 FALSE) != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003388 icase = get_dict_number(li->li_tv.vval.v_dict,
3389 (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003390 }
3391 else
3392 {
3393 p = get_tv_string_chk(&li->li_tv);
3394 x = NULL;
3395 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003396 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003397 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003398 if (ins_compl_add(p, -1, icase, NULL, x, dir, 0) == OK)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003399 /* if dir was BACKWARD then honor it just once */
3400 dir = FORWARD;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003401 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003402 else if (did_emsg)
3403 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003404 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003405}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003406#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003407
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003408/*
3409 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003410 * The search starts at position "ini" in curbuf and in the direction
3411 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003412 * When "compl_started" is FALSE start at that position, otherwise continue
3413 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003414 * This may return before finding all the matches.
3415 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 */
3417 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003418ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420{
3421 static pos_T first_match_pos;
3422 static pos_T last_match_pos;
3423 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003424 static int found_all = FALSE; /* Found all matches of a
3425 certain type. */
3426 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
Bram Moolenaar572cb562005-08-05 21:35:02 +00003428 pos_T *pos;
3429 char_u **matches;
3430 int save_p_scs;
3431 int save_p_ws;
3432 int save_p_ic;
3433 int i;
3434 int num_matches;
3435 int len;
3436 int found_new_match;
3437 int type = ctrl_x_mode;
3438 char_u *ptr;
3439 char_u *dict = NULL;
3440 int dict_f = 0;
3441 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003443 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 {
3445 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3446 ins_buf->b_scanned = 0;
3447 found_all = FALSE;
3448 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003449 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003450 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 last_match_pos = first_match_pos = *ini;
3452 }
3453
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003454 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003455 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3457 for (;;)
3458 {
3459 found_new_match = FAIL;
3460
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003461 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 * or if found_all says this entry is done. For ^X^L only use the
3463 * entries from 'complete' that look in loaded buffers. */
3464 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003465 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
3467 found_all = FALSE;
3468 while (*e_cpt == ',' || *e_cpt == ' ')
3469 e_cpt++;
3470 if (*e_cpt == '.' && !curbuf->b_scanned)
3471 {
3472 ins_buf = curbuf;
3473 first_match_pos = *ini;
3474 /* So that ^N can match word immediately after cursor */
3475 if (ctrl_x_mode == 0)
3476 dec(&first_match_pos);
3477 last_match_pos = first_match_pos;
3478 type = 0;
3479 }
3480 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3481 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3482 {
3483 /* Scan a buffer, but not the current one. */
3484 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3485 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003486 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 first_match_pos.col = last_match_pos.col = 0;
3488 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3489 last_match_pos.lnum = 0;
3490 type = 0;
3491 }
3492 else /* unloaded buffer, scan like dictionary */
3493 {
3494 found_all = TRUE;
3495 if (ins_buf->b_fname == NULL)
3496 continue;
3497 type = CTRL_X_DICTIONARY;
3498 dict = ins_buf->b_fname;
3499 dict_f = DICT_EXACT;
3500 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003501 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 ins_buf->b_fname == NULL
3503 ? buf_spname(ins_buf)
3504 : ins_buf->b_sfname == NULL
3505 ? (char *)ins_buf->b_fname
3506 : (char *)ins_buf->b_sfname);
3507 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3508 }
3509 else if (*e_cpt == NUL)
3510 break;
3511 else
3512 {
3513 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3514 type = -1;
3515 else if (*e_cpt == 'k' || *e_cpt == 's')
3516 {
3517 if (*e_cpt == 'k')
3518 type = CTRL_X_DICTIONARY;
3519 else
3520 type = CTRL_X_THESAURUS;
3521 if (*++e_cpt != ',' && *e_cpt != NUL)
3522 {
3523 dict = e_cpt;
3524 dict_f = DICT_FIRST;
3525 }
3526 }
3527#ifdef FEAT_FIND_ID
3528 else if (*e_cpt == 'i')
3529 type = CTRL_X_PATH_PATTERNS;
3530 else if (*e_cpt == 'd')
3531 type = CTRL_X_PATH_DEFINES;
3532#endif
3533 else if (*e_cpt == ']' || *e_cpt == 't')
3534 {
3535 type = CTRL_X_TAGS;
3536 sprintf((char*)IObuff, _("Scanning tags."));
3537 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3538 }
3539 else
3540 type = -1;
3541
3542 /* in any case e_cpt is advanced to the next entry */
3543 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3544
3545 found_all = TRUE;
3546 if (type == -1)
3547 continue;
3548 }
3549 }
3550
3551 switch (type)
3552 {
3553 case -1:
3554 break;
3555#ifdef FEAT_FIND_ID
3556 case CTRL_X_PATH_PATTERNS:
3557 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003558 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003559 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003561 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3563 (linenr_T)1, (linenr_T)MAXLNUM);
3564 break;
3565#endif
3566
3567 case CTRL_X_DICTIONARY:
3568 case CTRL_X_THESAURUS:
3569 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003570 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 : (type == CTRL_X_THESAURUS
3572 ? (*curbuf->b_p_tsr == NUL
3573 ? p_tsr
3574 : curbuf->b_p_tsr)
3575 : (*curbuf->b_p_dict == NUL
3576 ? p_dict
3577 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003578 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003579 dict != NULL ? dict_f
3580 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 dict = NULL;
3582 break;
3583
3584 case CTRL_X_TAGS:
3585 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3586 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003587 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
3589 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003590 * of matches is found when compl_pattern is empty */
3591 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3593 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3594 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3595 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003596 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598 p_ic = save_p_ic;
3599 break;
3600
3601 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003602 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3604 {
3605
3606 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003607 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003608 ins_compl_add_matches(num_matches, matches,
3609#ifdef CASE_INSENSITIVE_FILENAME
3610 TRUE
3611#else
3612 FALSE
3613#endif
3614 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
3616 break;
3617
3618 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003619 if (expand_cmdline(&compl_xp, compl_pattern,
3620 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003622 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 break;
3624
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003625#ifdef FEAT_COMPL_FUNC
3626 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003627 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003628 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003629 break;
3630#endif
3631
Bram Moolenaar488c6512005-08-11 20:09:58 +00003632 case CTRL_X_SPELL:
3633#ifdef FEAT_SYN_HL
3634 num_matches = expand_spelling(first_match_pos.lnum,
3635 first_match_pos.col, compl_pattern, &matches);
3636 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003637 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003638#endif
3639 break;
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 default: /* normal ^P/^N and ^X^L */
3642 /*
3643 * If 'infercase' is set, don't use 'smartcase' here
3644 */
3645 save_p_scs = p_scs;
3646 if (ins_buf->b_p_inf)
3647 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003648
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 /* buffers other than curbuf are scanned from the beginning or the
3650 * end but never from the middle, thus setting nowrapscan in this
3651 * buffers is a good idea, on the other hand, we always set
3652 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3653 save_p_ws = p_ws;
3654 if (ins_buf != curbuf)
3655 p_ws = FALSE;
3656 else if (*e_cpt == '.')
3657 p_ws = TRUE;
3658 for (;;)
3659 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003660 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003662 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3663 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003665 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003667 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003669 found_new_match = searchit(NULL, ins_buf, pos,
3670 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003671 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003672 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003673 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003675 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003676 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 first_match_pos = *pos;
3678 last_match_pos = *pos;
3679 }
3680 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003681 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 found_new_match = FAIL;
3683 if (found_new_match == FAIL)
3684 {
3685 if (ins_buf == curbuf)
3686 found_all = TRUE;
3687 break;
3688 }
3689
3690 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003691 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 && ini->lnum == pos->lnum
3693 && ini->col == pos->col)
3694 continue;
3695 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3696 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3697 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003698 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
3700 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3701 continue;
3702 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3703 if (!p_paste)
3704 ptr = skipwhite(ptr);
3705 }
3706 len = (int)STRLEN(ptr);
3707 }
3708 else
3709 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003710 char_u *tmp_ptr = ptr;
3711
3712 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003714 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 /* Skip if already inside a word. */
3716 if (vim_iswordp(tmp_ptr))
3717 continue;
3718 /* Find start of next word. */
3719 tmp_ptr = find_word_start(tmp_ptr);
3720 }
3721 /* Find end of this word. */
3722 tmp_ptr = find_word_end(tmp_ptr);
3723 len = (int)(tmp_ptr - ptr);
3724
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003725 if ((compl_cont_status & CONT_ADDING)
3726 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 {
3728 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3729 {
3730 /* Try next line, if any. the new word will be
3731 * "join" as if the normal command "J" was used.
3732 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003733 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 * works -- Acevedo */
3735 STRNCPY(IObuff, ptr, len);
3736 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3737 tmp_ptr = ptr = skipwhite(ptr);
3738 /* Find start of next word. */
3739 tmp_ptr = find_word_start(tmp_ptr);
3740 /* Find end of next word. */
3741 tmp_ptr = find_word_end(tmp_ptr);
3742 if (tmp_ptr > ptr)
3743 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003744 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003746 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 IObuff[len++] = ' ';
3748 /* IObuf =~ "\k.* ", thus len >= 2 */
3749 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003750 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 || (vim_strchr(p_cpo, CPO_JOINSP)
3752 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003753 && (IObuff[len - 2] == '?'
3754 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 IObuff[len++] = ' ';
3756 }
3757 /* copy as much as posible of the new word */
3758 if (tmp_ptr - ptr >= IOSIZE - len)
3759 tmp_ptr = ptr + IOSIZE - len - 1;
3760 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3761 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003762 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
3764 IObuff[len] = NUL;
3765 ptr = IObuff;
3766 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003767 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 continue;
3769 }
3770 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003771 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003772 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003773 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 {
3775 found_new_match = OK;
3776 break;
3777 }
3778 }
3779 p_scs = save_p_scs;
3780 p_ws = save_p_ws;
3781 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003782
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003783 /* check if compl_curr_match has changed, (e.g. other type of
3784 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003785 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 found_new_match = OK;
3787
3788 /* break the loop for specialized modes (use 'complete' just for the
3789 * generic ctrl_x_mode == 0) or when we've found a new match */
3790 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003791 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003792 {
3793 if (got_int)
3794 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003795 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003796 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003797 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003798
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003799 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3800 || compl_interrupted)
3801 break;
3802 compl_started = TRUE;
3803 }
3804 else
3805 {
3806 /* Mark a buffer scanned when it has been scanned completely */
3807 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3808 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003810 compl_started = FALSE;
3811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003813 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
3815 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3816 && *e_cpt == NUL) /* Got to end of 'complete' */
3817 found_new_match = FAIL;
3818
3819 i = -1; /* total of matches, unknown */
3820 if (found_new_match == FAIL
3821 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3822 i = ins_compl_make_cyclic();
3823
3824 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003825 * just been made cyclic then we have to move compl_curr_match to the next
3826 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003827 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
3828 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003829 if (compl_curr_match == NULL)
3830 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 return i;
3832}
3833
3834/* Delete the old text being completed. */
3835 static void
3836ins_compl_delete()
3837{
3838 int i;
3839
3840 /*
3841 * In insert mode: Delete the typed part.
3842 * In replace mode: Put the old characters back, if any.
3843 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003844 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 backspace_until_column(i);
3846 changed_cline_bef_curs();
3847}
3848
3849/* Insert the new text being completed. */
3850 static void
3851ins_compl_insert()
3852{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003853 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003854 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3855 compl_used_match = FALSE;
3856 else
3857 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858}
3859
3860/*
3861 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003862 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3863 * get more completions. If it is FALSE, then we just do nothing when there
3864 * are no more completions in a given direction. The latter case is used when
3865 * we are still in the middle of finding completions, to allow browsing
3866 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 * Return the total number of matches, or -1 if still unknown -- webb.
3868 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003869 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3870 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 *
3872 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003873 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3874 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 */
3876 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003877ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003879 int count; /* repeat completion this many times; should
3880 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003881 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882{
3883 int num_matches = -1;
3884 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003885 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003886 compl_T *found_compl = NULL;
3887 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003889 if (compl_leader != NULL
3890 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003892 /* Set "compl_shown_match" to the actually shown match, it may differ
3893 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003894 while (!ins_compl_equal(compl_shown_match,
3895 compl_leader, STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003896 && compl_shown_match->cp_next != NULL
3897 && compl_shown_match->cp_next != compl_first_match)
3898 compl_shown_match = compl_shown_match->cp_next;
3899 }
3900
3901 if (allow_get_expansion && insert_match
3902 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 /* Delete old text to be replaced */
3904 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003905
Bram Moolenaare3226be2005-12-18 22:10:00 +00003906 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3907 * around. */
3908 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003910 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003912 if (compl_pending != 0)
3913 --compl_pending;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003914 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003915 found_end = (compl_first_match != NULL
3916 && (compl_shown_match->cp_next == compl_first_match
3917 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003918 }
3919 else if (compl_shows_dir == BACKWARD
3920 && compl_shown_match->cp_prev != NULL)
3921 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003922 if (compl_pending != 0)
3923 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00003924 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003925 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003926 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
3928 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003929 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003930 if (compl_shows_dir == BACKWARD)
3931 --compl_pending;
3932 else
3933 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00003934 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003935 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003936
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003937 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003938 if (compl_pending != 0 && compl_direction == compl_shows_dir)
Bram Moolenaara6557602006-02-04 22:43:20 +00003939 compl_shown_match = compl_curr_match;
3940 found_end = FALSE;
3941 }
3942 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
3943 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003944 && !ins_compl_equal(compl_shown_match,
3945 compl_leader, STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00003946 ++todo;
3947 else
3948 /* Remember a matching item. */
3949 found_compl = compl_shown_match;
3950
3951 /* Stop at the end of the list when we found a usable match. */
3952 if (found_end)
3953 {
3954 if (found_compl != NULL)
3955 {
3956 compl_shown_match = found_compl;
3957 break;
3958 }
3959 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
3962
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003963 /* Insert the text of the new completion, or the compl_leader. */
3964 if (insert_match)
3965 {
3966 if (!compl_get_longest || compl_used_match)
3967 ins_compl_insert();
3968 else
3969 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3970 }
3971 else
3972 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973
3974 if (!allow_get_expansion)
3975 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003976 /* may undisplay the popup menu first */
3977 ins_compl_upd_pum();
3978
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003979 /* redraw to show the user what was inserted */
3980 update_screen(0);
3981
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003982 /* display the updated popup menu */
3983 ins_compl_show_pum();
3984
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 /* Delete old text to be replaced, since we're still searching and
3986 * don't want to match ourselves! */
3987 ins_compl_delete();
3988 }
3989
3990 /*
3991 * Show the file name for the match (if any)
3992 * Truncate the file name to avoid a wait for return.
3993 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003994 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 {
3996 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00003997 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (i <= 0)
3999 i = 0;
4000 else
4001 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004002 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 msg(IObuff);
4004 redraw_cmdline = FALSE; /* don't overwrite! */
4005 }
4006
4007 return num_matches;
4008}
4009
4010/*
4011 * Call this while finding completions, to check whether the user has hit a key
4012 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004013 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004015 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 */
4017 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004018ins_compl_check_keys(frequency)
4019 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020{
4021 static int count = 0;
4022
4023 int c;
4024
4025 /* Don't check when reading keys from a script. That would break the test
4026 * scripts */
4027 if (using_script())
4028 return;
4029
4030 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004031 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 return;
4033 count = 0;
4034
4035 ++no_mapping;
4036 c = vpeekc_any();
4037 --no_mapping;
4038 if (c != NUL)
4039 {
4040 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4041 {
4042 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004043 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004044 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4045 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
4047 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004048 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004050 if (compl_pending != 0 && !got_int)
4051 (void)ins_compl_next(FALSE, compl_pending > 0
4052 ? compl_pending : -compl_pending, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004053}
4054
4055/*
4056 * Decide the direction of Insert mode complete from the key typed.
4057 * Returns BACKWARD or FORWARD.
4058 */
4059 static int
4060ins_compl_key2dir(c)
4061 int c;
4062{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004063 if (c == Ctrl_P || c == Ctrl_L
4064 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4065 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004066 return BACKWARD;
4067 return FORWARD;
4068}
4069
4070/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004071 * Return TRUE for keys that are used for completion only when the popup menu
4072 * is visible.
4073 */
4074 static int
4075ins_compl_pum_key(c)
4076 int c;
4077{
4078 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004079 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4080 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004081}
4082
4083/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004084 * Decide the number of completions to move forward.
4085 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4086 */
4087 static int
4088ins_compl_key2count(c)
4089 int c;
4090{
4091 int h;
4092
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004093 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004094 {
4095 h = pum_get_height();
4096 if (h > 3)
4097 h -= 2; /* keep some context */
4098 return h;
4099 }
4100 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101}
4102
4103/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004104 * Return TRUE if completion with "c" should insert the match, FALSE if only
4105 * to change the currently selected completion.
4106 */
4107 static int
4108ins_compl_use_match(c)
4109 int c;
4110{
4111 switch (c)
4112 {
4113 case K_UP:
4114 case K_DOWN:
4115 case K_PAGEDOWN:
4116 case K_KPAGEDOWN:
4117 case K_S_DOWN:
4118 case K_PAGEUP:
4119 case K_KPAGEUP:
4120 case K_S_UP:
4121 return FALSE;
4122 }
4123 return TRUE;
4124}
4125
4126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 * Do Insert mode completion.
4128 * Called when character "c" was typed, which has a meaning for completion.
4129 * Returns OK if completion was done, FAIL if something failed (out of mem).
4130 */
4131 static int
4132ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004133 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004135 char_u *line;
4136 int startcol = 0; /* column where searched text starts */
4137 colnr_T curs_col; /* cursor column */
4138 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
Bram Moolenaare3226be2005-12-18 22:10:00 +00004140 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004141 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 {
4143 /* First time we hit ^N or ^P (in a row, I mean) */
4144
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 did_ai = FALSE;
4146#ifdef FEAT_SMARTINDENT
4147 did_si = FALSE;
4148 can_si = FALSE;
4149 can_si_back = FALSE;
4150#endif
4151 if (stop_arrow() == FAIL)
4152 return FAIL;
4153
4154 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004155 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004156 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157
4158 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004159 * "compl_startpos" to the cursor as a pattern to add a new word
4160 * instead of expand the one before the cursor, in word-wise if
4161 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 * is not in the same line as the cursor then fix it (the line has
4163 * been split because it was longer than 'tw'). if SOL is set then
4164 * skip the previous pattern, a word at the beginning of the line has
4165 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004166 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4167 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {
4169 /*
4170 * it is a continued search
4171 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004172 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4174 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4175 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004176 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004178 /* line (probably) wrapped, set compl_startpos to the
4179 * first non_blank in the line, if it is not a wordchar
4180 * include it to get a better pattern, but then we don't
4181 * want the "\\<" prefix, check it bellow */
4182 compl_col = (colnr_T)(skipwhite(line) - line);
4183 compl_startpos.col = compl_col;
4184 compl_startpos.lnum = curwin->w_cursor.lnum;
4185 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187 else
4188 {
4189 /* S_IPOS was set when we inserted a word that was at the
4190 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004191 * mode but first we need to redefine compl_startpos */
4192 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004194 compl_cont_status |= CONT_SOL;
4195 compl_startpos.col = (colnr_T)(skipwhite(
4196 line + compl_length
4197 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004199 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004201 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004202 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 * have enough space? just being paranoic */
4204#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004205 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004207 compl_cont_status &= ~CONT_SOL;
4208 compl_length = (IOSIZE - MIN_SPACE);
4209 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004211 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4212 if (compl_length < 1)
4213 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004216 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004218 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
4220 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004221 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004223 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004225 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004227 compl_cont_status = 0;
4228 compl_cont_status |= CONT_N_ADDS;
4229 compl_startpos = curwin->w_cursor;
4230 startcol = (int)curs_col;
4231 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233
4234 /* Work out completion pattern and original text -- webb */
4235 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4236 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004237 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4239 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004240 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004242 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004244 compl_col += ++startcol;
4245 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004248 compl_pattern = str_foldcase(line + compl_col,
4249 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251 compl_pattern = vim_strnsave(line + compl_col,
4252 compl_length);
4253 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 return FAIL;
4255 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004256 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 {
4258 char_u *prefix = (char_u *)"\\<";
4259
4260 /* we need 3 extra chars, 1 for the NUL and
4261 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004262 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4263 compl_length) + 3);
4264 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004266 if (!vim_iswordp(line + compl_col)
4267 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 && (
4269#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004270 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004272 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273#endif
4274 )))
4275 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004276 STRCPY((char *)compl_pattern, prefix);
4277 (void)quote_meta(compl_pattern + STRLEN(prefix),
4278 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004280 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004282 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004284 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285#endif
4286 )
4287 {
4288 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004289 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4290 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004292 compl_col += curs_col;
4293 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
4295 else
4296 {
4297#ifdef FEAT_MBYTE
4298 /* Search the point of change class of multibyte character
4299 * or not a word single byte character backward. */
4300 if (has_mbyte)
4301 {
4302 int base_class;
4303 int head_off;
4304
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004305 startcol -= (*mb_head_off)(line, line + startcol);
4306 base_class = mb_get_class(line + startcol);
4307 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004309 head_off = (*mb_head_off)(line, line + startcol);
4310 if (base_class != mb_get_class(line + startcol
4311 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004313 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315 }
4316 else
4317#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004318 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004320 compl_col += ++startcol;
4321 compl_length = (int)curs_col - startcol;
4322 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 {
4324 /* Only match word with at least two chars -- webb
4325 * there's no need to call quote_meta,
4326 * alloc(7) is enough -- Acevedo
4327 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004328 compl_pattern = alloc(7);
4329 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004331 STRCPY((char *)compl_pattern, "\\<");
4332 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4333 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 }
4335 else
4336 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004337 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4338 compl_length) + 3);
4339 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004341 STRCPY((char *)compl_pattern, "\\<");
4342 (void)quote_meta(compl_pattern + 2, line + compl_col,
4343 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 }
4346 }
4347 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4348 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004349 compl_col = skipwhite(line) - line;
4350 compl_length = (int)curs_col - (int)compl_col;
4351 if (compl_length < 0) /* cursor in indent: empty pattern */
4352 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004354 compl_pattern = str_foldcase(line + compl_col, compl_length,
4355 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004357 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4358 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 return FAIL;
4360 }
4361 else if (ctrl_x_mode == CTRL_X_FILES)
4362 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004363 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004365 compl_col += ++startcol;
4366 compl_length = (int)curs_col - startcol;
4367 compl_pattern = addstar(line + compl_col, compl_length,
4368 EXPAND_FILES);
4369 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 return FAIL;
4371 }
4372 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4373 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004374 compl_pattern = vim_strnsave(line, curs_col);
4375 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004377 set_cmd_context(&compl_xp, compl_pattern,
4378 (int)STRLEN(compl_pattern), curs_col);
4379 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4380 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004382 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4383 compl_col = startcol;
4384 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004386 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004387 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004388#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004389 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004390 * Call user defined function 'completefunc' with "a:findstart"
4391 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004392 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004393 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004394 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004395 char_u *funcname;
4396 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004397
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004398 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004399 * string */
4400 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4401 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4402 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004403 {
4404 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4405 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004406 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004407 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004408
4409 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004410 args[1] = NULL;
4411 pos = curwin->w_cursor;
4412 col = call_func_retnr(funcname, 2, args, FALSE);
4413 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004414
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004415 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004416 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004417 compl_col = col;
4418 if ((colnr_T)compl_col > curs_col)
4419 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004420
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004421 /* Setup variables for completion. Need to obtain "line" again,
4422 * it may have become invalid. */
4423 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004424 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4426 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004427#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004428 return FAIL;
4429 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004430 else if (ctrl_x_mode == CTRL_X_SPELL)
4431 {
4432#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004433 if (spell_bad_len > 0)
4434 compl_col = curs_col - spell_bad_len;
4435 else
4436 compl_col = spell_word_start(startcol);
4437 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004438 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004439 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004440 compl_length = (int)curs_col - compl_col;
4441 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4442 if (compl_pattern == NULL)
4443#endif
4444 return FAIL;
4445 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004446 else
4447 {
4448 EMSG2(_(e_intern2), "ins_complete()");
4449 return FAIL;
4450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004452 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 {
4454 edit_submode_pre = (char_u *)_(" Adding");
4455 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4456 {
4457 /* Insert a new line, keep indentation but ignore 'comments' */
4458#ifdef FEAT_COMMENTS
4459 char_u *old = curbuf->b_p_com;
4460
4461 curbuf->b_p_com = (char_u *)"";
4462#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004463 compl_startpos.lnum = curwin->w_cursor.lnum;
4464 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 ins_eol('\r');
4466#ifdef FEAT_COMMENTS
4467 curbuf->b_p_com = old;
4468#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004469 compl_length = 0;
4470 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472 }
4473 else
4474 {
4475 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004476 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004479 if (compl_cont_status & CONT_LOCAL)
4480 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 else
4482 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4483
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004484 /* Always add completion for the original text. */
4485 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004486 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4487 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004488 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004490 vim_free(compl_pattern);
4491 compl_pattern = NULL;
4492 vim_free(compl_orig_text);
4493 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 return FAIL;
4495 }
4496
4497 /* showmode might reset the internal line pointers, so it must
4498 * be called before line = ml_get(), or when this address is no
4499 * longer needed. -- Acevedo.
4500 */
4501 edit_submode_extra = (char_u *)_("-- Searching...");
4502 edit_submode_highl = HLF_COUNT;
4503 showmode();
4504 edit_submode_extra = NULL;
4505 out_flush();
4506 }
4507
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004508 compl_shown_match = compl_curr_match;
4509 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
4511 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004512 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004514 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004516 /* may undisplay the popup menu */
4517 ins_compl_upd_pum();
4518
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004519 if (n > 1) /* all matches have been found */
4520 compl_matches = n;
4521 compl_curr_match = compl_shown_match;
4522 compl_direction = compl_shows_dir;
4523 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524
4525 /* eat the ESC to avoid leaving insert mode */
4526 if (got_int && !global_busy)
4527 {
4528 (void)vgetc();
4529 got_int = FALSE;
4530 }
4531
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004532 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004533 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004535 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4536 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4538 edit_submode_highl = HLF_E;
4539 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4540 * because we couldn't expand anything at first place, but if we used
4541 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4542 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004543 if ( compl_length > 1
4544 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 || (ctrl_x_mode != 0
4546 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4547 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004548 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 }
4550
Bram Moolenaar572cb562005-08-05 21:35:02 +00004551 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004552 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004554 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555
4556 if (edit_submode_extra == NULL)
4557 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004558 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 {
4560 edit_submode_extra = (char_u *)_("Back at original");
4561 edit_submode_highl = HLF_W;
4562 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004563 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 {
4565 edit_submode_extra = (char_u *)_("Word from other line");
4566 edit_submode_highl = HLF_COUNT;
4567 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004568 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 {
4570 edit_submode_extra = (char_u *)_("The only match");
4571 edit_submode_highl = HLF_COUNT;
4572 }
4573 else
4574 {
4575 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004576 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004578 int number = 0;
4579 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004581 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 {
4583 /* search backwards for the first valid (!= -1) number.
4584 * This should normally succeed already at the first loop
4585 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004586 for (match = compl_curr_match->cp_prev; match != NULL
4587 && match != compl_first_match;
4588 match = match->cp_prev)
4589 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004591 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 break;
4593 }
4594 if (match != NULL)
4595 /* go up and assign all numbers which are not assigned
4596 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004597 for (match = match->cp_next;
4598 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004599 match = match->cp_next)
4600 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 }
4602 else /* BACKWARD */
4603 {
4604 /* search forwards (upwards) for the first valid (!= -1)
4605 * number. This should normally succeed already at the
4606 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004607 for (match = compl_curr_match->cp_next; match != NULL
4608 && match != compl_first_match;
4609 match = match->cp_next)
4610 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004612 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 break;
4614 }
4615 if (match != NULL)
4616 /* go down and assign all numbers which are not
4617 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004618 for (match = match->cp_prev; match
4619 && match->cp_number == -1;
4620 match = match->cp_prev)
4621 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 }
4623 }
4624
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004625 /* The match should always have a sequence number now, this is
4626 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004627 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
4629 /* Space for 10 text chars. + 2x10-digit no.s */
4630 static char_u match_ref[31];
4631
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004632 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004634 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004636 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004637 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004638 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 edit_submode_extra = match_ref;
4640 edit_submode_highl = HLF_R;
4641 if (dollar_vcol)
4642 curs_columns(FALSE);
4643 }
4644 }
4645 }
4646
4647 /* Show a message about what (completion) mode we're in. */
4648 showmode();
4649 if (edit_submode_extra != NULL)
4650 {
4651 if (!p_smd)
4652 msg_attr(edit_submode_extra,
4653 edit_submode_highl < HLF_COUNT
4654 ? hl_attr(edit_submode_highl) : 0);
4655 }
4656 else
4657 msg_clr_cmdline(); /* necessary for "noshowmode" */
4658
Bram Moolenaara94bc432006-03-10 21:42:59 +00004659 /* RedrawingDisabled may be set when invoked through complete(). */
4660 n = RedrawingDisabled;
4661 RedrawingDisabled = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004662 ins_compl_show_pum();
Bram Moolenaara94bc432006-03-10 21:42:59 +00004663 setcursor();
4664 RedrawingDisabled = n;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004665
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 return OK;
4667}
4668
4669/*
4670 * Looks in the first "len" chars. of "src" for search-metachars.
4671 * If dest is not NULL the chars. are copied there quoting (with
4672 * a backslash) the metachars, and dest would be NUL terminated.
4673 * Returns the length (needed) of dest
4674 */
4675 static int
4676quote_meta(dest, src, len)
4677 char_u *dest;
4678 char_u *src;
4679 int len;
4680{
4681 int m;
4682
4683 for (m = len; --len >= 0; src++)
4684 {
4685 switch (*src)
4686 {
4687 case '.':
4688 case '*':
4689 case '[':
4690 if (ctrl_x_mode == CTRL_X_DICTIONARY
4691 || ctrl_x_mode == CTRL_X_THESAURUS)
4692 break;
4693 case '~':
4694 if (!p_magic) /* quote these only if magic is set */
4695 break;
4696 case '\\':
4697 if (ctrl_x_mode == CTRL_X_DICTIONARY
4698 || ctrl_x_mode == CTRL_X_THESAURUS)
4699 break;
4700 case '^': /* currently it's not needed. */
4701 case '$':
4702 m++;
4703 if (dest != NULL)
4704 *dest++ = '\\';
4705 break;
4706 }
4707 if (dest != NULL)
4708 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004709# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 /* Copy remaining bytes of a multibyte character. */
4711 if (has_mbyte)
4712 {
4713 int i, mb_len;
4714
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004715 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 if (mb_len > 0 && len >= mb_len)
4717 for (i = 0; i < mb_len; ++i)
4718 {
4719 --len;
4720 ++src;
4721 if (dest != NULL)
4722 *dest++ = *src;
4723 }
4724 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004725# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 }
4727 if (dest != NULL)
4728 *dest = NUL;
4729
4730 return m;
4731}
4732#endif /* FEAT_INS_EXPAND */
4733
4734/*
4735 * Next character is interpreted literally.
4736 * A one, two or three digit decimal number is interpreted as its byte value.
4737 * If one or two digits are entered, the next character is given to vungetc().
4738 * For Unicode a character > 255 may be returned.
4739 */
4740 int
4741get_literal()
4742{
4743 int cc;
4744 int nc;
4745 int i;
4746 int hex = FALSE;
4747 int octal = FALSE;
4748#ifdef FEAT_MBYTE
4749 int unicode = 0;
4750#endif
4751
4752 if (got_int)
4753 return Ctrl_C;
4754
4755#ifdef FEAT_GUI
4756 /*
4757 * In GUI there is no point inserting the internal code for a special key.
4758 * It is more useful to insert the string "<KEY>" instead. This would
4759 * probably be useful in a text window too, but it would not be
4760 * vi-compatible (maybe there should be an option for it?) -- webb
4761 */
4762 if (gui.in_use)
4763 ++allow_keys;
4764#endif
4765#ifdef USE_ON_FLY_SCROLL
4766 dont_scroll = TRUE; /* disallow scrolling here */
4767#endif
4768 ++no_mapping; /* don't map the next key hits */
4769 cc = 0;
4770 i = 0;
4771 for (;;)
4772 {
4773 do
4774 nc = safe_vgetc();
4775 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4776 || nc == K_HOR_SCROLLBAR);
4777#ifdef FEAT_CMDL_INFO
4778 if (!(State & CMDLINE)
4779# ifdef FEAT_MBYTE
4780 && MB_BYTE2LEN_CHECK(nc) == 1
4781# endif
4782 )
4783 add_to_showcmd(nc);
4784#endif
4785 if (nc == 'x' || nc == 'X')
4786 hex = TRUE;
4787 else if (nc == 'o' || nc == 'O')
4788 octal = TRUE;
4789#ifdef FEAT_MBYTE
4790 else if (nc == 'u' || nc == 'U')
4791 unicode = nc;
4792#endif
4793 else
4794 {
4795 if (hex
4796#ifdef FEAT_MBYTE
4797 || unicode != 0
4798#endif
4799 )
4800 {
4801 if (!vim_isxdigit(nc))
4802 break;
4803 cc = cc * 16 + hex2nr(nc);
4804 }
4805 else if (octal)
4806 {
4807 if (nc < '0' || nc > '7')
4808 break;
4809 cc = cc * 8 + nc - '0';
4810 }
4811 else
4812 {
4813 if (!VIM_ISDIGIT(nc))
4814 break;
4815 cc = cc * 10 + nc - '0';
4816 }
4817
4818 ++i;
4819 }
4820
4821 if (cc > 255
4822#ifdef FEAT_MBYTE
4823 && unicode == 0
4824#endif
4825 )
4826 cc = 255; /* limit range to 0-255 */
4827 nc = 0;
4828
4829 if (hex) /* hex: up to two chars */
4830 {
4831 if (i >= 2)
4832 break;
4833 }
4834#ifdef FEAT_MBYTE
4835 else if (unicode) /* Unicode: up to four or eight chars */
4836 {
4837 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4838 break;
4839 }
4840#endif
4841 else if (i >= 3) /* decimal or octal: up to three chars */
4842 break;
4843 }
4844 if (i == 0) /* no number entered */
4845 {
4846 if (nc == K_ZERO) /* NUL is stored as NL */
4847 {
4848 cc = '\n';
4849 nc = 0;
4850 }
4851 else
4852 {
4853 cc = nc;
4854 nc = 0;
4855 }
4856 }
4857
4858 if (cc == 0) /* NUL is stored as NL */
4859 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004860#ifdef FEAT_MBYTE
4861 if (enc_dbcs && (cc & 0xff) == 0)
4862 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4863 second byte will cause trouble! */
4864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
4866 --no_mapping;
4867#ifdef FEAT_GUI
4868 if (gui.in_use)
4869 --allow_keys;
4870#endif
4871 if (nc)
4872 vungetc(nc);
4873 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4874 return cc;
4875}
4876
4877/*
4878 * Insert character, taking care of special keys and mod_mask
4879 */
4880 static void
4881insert_special(c, allow_modmask, ctrlv)
4882 int c;
4883 int allow_modmask;
4884 int ctrlv; /* c was typed after CTRL-V */
4885{
4886 char_u *p;
4887 int len;
4888
4889 /*
4890 * Special function key, translate into "<Key>". Up to the last '>' is
4891 * inserted with ins_str(), so as not to replace characters in replace
4892 * mode.
4893 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4894 * unless 'allow_modmask' is TRUE.
4895 */
4896#ifdef MACOS
4897 /* Command-key never produces a normal key */
4898 if (mod_mask & MOD_MASK_CMD)
4899 allow_modmask = TRUE;
4900#endif
4901 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4902 {
4903 p = get_special_key_name(c, mod_mask);
4904 len = (int)STRLEN(p);
4905 c = p[len - 1];
4906 if (len > 2)
4907 {
4908 if (stop_arrow() == FAIL)
4909 return;
4910 p[len - 1] = NUL;
4911 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004912 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 ctrlv = FALSE;
4914 }
4915 }
4916 if (stop_arrow() == OK)
4917 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4918}
4919
4920/*
4921 * Special characters in this context are those that need processing other
4922 * than the simple insertion that can be performed here. This includes ESC
4923 * which terminates the insert, and CR/NL which need special processing to
4924 * open up a new line. This routine tries to optimize insertions performed by
4925 * the "redo", "undo" or "put" commands, so it needs to know when it should
4926 * stop and defer processing to the "normal" mechanism.
4927 * '0' and '^' are special, because they can be followed by CTRL-D.
4928 */
4929#ifdef EBCDIC
4930# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4931#else
4932# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4933#endif
4934
4935#ifdef FEAT_MBYTE
4936# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4937#else
4938# define WHITECHAR(cc) vim_iswhite(cc)
4939#endif
4940
4941 void
4942insertchar(c, flags, second_indent)
4943 int c; /* character to insert or NUL */
4944 int flags; /* INSCHAR_FORMAT, etc. */
4945 int second_indent; /* indent for second line if >= 0 */
4946{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 int textwidth;
4948#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952
4953 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4954 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955
4956 /*
4957 * Try to break the line in two or more pieces when:
4958 * - Always do this if we have been called to do formatting only.
4959 * - Always do this when 'formatoptions' has the 'a' flag and the line
4960 * ends in white space.
4961 * - Otherwise:
4962 * - Don't do this if inserting a blank
4963 * - Don't do this if an existing character is being replaced, unless
4964 * we're in VREPLACE mode.
4965 * - Do this if the cursor is not on the line where insert started
4966 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4967 * before the insert.
4968 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4969 * before 'textwidth'
4970 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004971 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 && ((flags & INSCHAR_FORMAT)
4973 || (!vim_iswhite(c)
4974 && !((State & REPLACE_FLAG)
4975#ifdef FEAT_VREPLACE
4976 && !(State & VREPLACE_FLAG)
4977#endif
4978 && *ml_get_cursor() != NUL)
4979 && (curwin->w_cursor.lnum != Insstart.lnum
4980 || ((!has_format_option(FO_INS_LONG)
4981 || Insstart_textlen <= (colnr_T)textwidth)
4982 && (!fo_ins_blank
4983 || Insstart_blank_vcol <= (colnr_T)textwidth
4984 ))))))
4985 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004986 /* Format with 'formatexpr' when it's set. Use internal formatting
4987 * when 'formatexpr' isn't set or it returns non-zero. */
4988#if defined(FEAT_EVAL)
4989 if (*curbuf->b_p_fex == NUL
4990 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004992 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004994
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 if (c == NUL) /* only formatting was wanted */
4996 return;
4997
4998#ifdef FEAT_COMMENTS
4999 /* Check whether this character should end a comment. */
5000 if (did_ai && (int)c == end_comment_pending)
5001 {
5002 char_u *line;
5003 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5004 int middle_len, end_len;
5005 int i;
5006
5007 /*
5008 * Need to remove existing (middle) comment leader and insert end
5009 * comment leader. First, check what comment leader we can find.
5010 */
5011 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5012 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5013 {
5014 /* Skip middle-comment string */
5015 while (*p && p[-1] != ':') /* find end of middle flags */
5016 ++p;
5017 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5018 /* Don't count trailing white space for middle_len */
5019 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5020 --middle_len;
5021
5022 /* Find the end-comment string */
5023 while (*p && p[-1] != ':') /* find end of end flags */
5024 ++p;
5025 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5026
5027 /* Skip white space before the cursor */
5028 i = curwin->w_cursor.col;
5029 while (--i >= 0 && vim_iswhite(line[i]))
5030 ;
5031 i++;
5032
5033 /* Skip to before the middle leader */
5034 i -= middle_len;
5035
5036 /* Check some expected things before we go on */
5037 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5038 {
5039 /* Backspace over all the stuff we want to replace */
5040 backspace_until_column(i);
5041
5042 /*
5043 * Insert the end-comment string, except for the last
5044 * character, which will get inserted as normal later.
5045 */
5046 ins_bytes_len(lead_end, end_len - 1);
5047 }
5048 }
5049 }
5050 end_comment_pending = NUL;
5051#endif
5052
5053 did_ai = FALSE;
5054#ifdef FEAT_SMARTINDENT
5055 did_si = FALSE;
5056 can_si = FALSE;
5057 can_si_back = FALSE;
5058#endif
5059
5060 /*
5061 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5062 * This speeds up normal text input considerably.
5063 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5064 * need to re-indent at a ':', or any other character (but not what
5065 * 'paste' is set)..
5066 */
5067#ifdef USE_ON_FLY_SCROLL
5068 dont_scroll = FALSE; /* allow scrolling here */
5069#endif
5070
5071 if ( !ISSPECIAL(c)
5072#ifdef FEAT_MBYTE
5073 && (!has_mbyte || (*mb_char2len)(c) == 1)
5074#endif
5075 && vpeekc() != NUL
5076 && !(State & REPLACE_FLAG)
5077#ifdef FEAT_CINDENT
5078 && !cindent_on()
5079#endif
5080#ifdef FEAT_RIGHTLEFT
5081 && !p_ri
5082#endif
5083 )
5084 {
5085#define INPUT_BUFLEN 100
5086 char_u buf[INPUT_BUFLEN + 1];
5087 int i;
5088 colnr_T virtcol = 0;
5089
5090 buf[0] = c;
5091 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005092 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 virtcol = get_nolist_virtcol();
5094 /*
5095 * Stop the string when:
5096 * - no more chars available
5097 * - finding a special character (command key)
5098 * - buffer is full
5099 * - running into the 'textwidth' boundary
5100 * - need to check for abbreviation: A non-word char after a word-char
5101 */
5102 while ( (c = vpeekc()) != NUL
5103 && !ISSPECIAL(c)
5104#ifdef FEAT_MBYTE
5105 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5106#endif
5107 && i < INPUT_BUFLEN
5108 && (textwidth == 0
5109 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5110 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5111 {
5112#ifdef FEAT_RIGHTLEFT
5113 c = vgetc();
5114 if (p_hkmap && KeyTyped)
5115 c = hkmap(c); /* Hebrew mode mapping */
5116# ifdef FEAT_FKMAP
5117 if (p_fkmap && KeyTyped)
5118 c = fkmap(c); /* Farsi mode mapping */
5119# endif
5120 buf[i++] = c;
5121#else
5122 buf[i++] = vgetc();
5123#endif
5124 }
5125
5126#ifdef FEAT_DIGRAPHS
5127 do_digraph(-1); /* clear digraphs */
5128 do_digraph(buf[i-1]); /* may be the start of a digraph */
5129#endif
5130 buf[i] = NUL;
5131 ins_str(buf);
5132 if (flags & INSCHAR_CTRLV)
5133 {
5134 redo_literal(*buf);
5135 i = 1;
5136 }
5137 else
5138 i = 0;
5139 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005140 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142 else
5143 {
5144#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005145 int cc;
5146
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5148 {
5149 char_u buf[MB_MAXBYTES + 1];
5150
5151 (*mb_char2bytes)(c, buf);
5152 buf[cc] = NUL;
5153 ins_char_bytes(buf, cc);
5154 AppendCharToRedobuff(c);
5155 }
5156 else
5157#endif
5158 {
5159 ins_char(c);
5160 if (flags & INSCHAR_CTRLV)
5161 redo_literal(c);
5162 else
5163 AppendCharToRedobuff(c);
5164 }
5165 }
5166}
5167
5168/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005169 * Format text at the current insert position.
5170 */
5171 static void
5172internal_format(textwidth, second_indent, flags, format_only)
5173 int textwidth;
5174 int second_indent;
5175 int flags;
5176 int format_only;
5177{
5178 int cc;
5179 int save_char = NUL;
5180 int haveto_redraw = FALSE;
5181 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5182#ifdef FEAT_MBYTE
5183 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5184#endif
5185 int fo_white_par = has_format_option(FO_WHITE_PAR);
5186 int first_line = TRUE;
5187#ifdef FEAT_COMMENTS
5188 colnr_T leader_len;
5189 int no_leader = FALSE;
5190 int do_comments = (flags & INSCHAR_DO_COM);
5191#endif
5192
5193 /*
5194 * When 'ai' is off we don't want a space under the cursor to be
5195 * deleted. Replace it with an 'x' temporarily.
5196 */
5197 if (!curbuf->b_p_ai)
5198 {
5199 cc = gchar_cursor();
5200 if (vim_iswhite(cc))
5201 {
5202 save_char = cc;
5203 pchar_cursor('x');
5204 }
5205 }
5206
5207 /*
5208 * Repeat breaking lines, until the current line is not too long.
5209 */
5210 while (!got_int)
5211 {
5212 int startcol; /* Cursor column at entry */
5213 int wantcol; /* column at textwidth border */
5214 int foundcol; /* column for start of spaces */
5215 int end_foundcol = 0; /* column for start of word */
5216 colnr_T len;
5217 colnr_T virtcol;
5218#ifdef FEAT_VREPLACE
5219 int orig_col = 0;
5220 char_u *saved_text = NULL;
5221#endif
5222 colnr_T col;
5223
5224 virtcol = get_nolist_virtcol();
5225 if (virtcol < (colnr_T)textwidth)
5226 break;
5227
5228#ifdef FEAT_COMMENTS
5229 if (no_leader)
5230 do_comments = FALSE;
5231 else if (!(flags & INSCHAR_FORMAT)
5232 && has_format_option(FO_WRAP_COMS))
5233 do_comments = TRUE;
5234
5235 /* Don't break until after the comment leader */
5236 if (do_comments)
5237 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5238 else
5239 leader_len = 0;
5240
5241 /* If the line doesn't start with a comment leader, then don't
5242 * start one in a following broken line. Avoids that a %word
5243 * moved to the start of the next line causes all following lines
5244 * to start with %. */
5245 if (leader_len == 0)
5246 no_leader = TRUE;
5247#endif
5248 if (!(flags & INSCHAR_FORMAT)
5249#ifdef FEAT_COMMENTS
5250 && leader_len == 0
5251#endif
5252 && !has_format_option(FO_WRAP))
5253
5254 {
5255 textwidth = 0;
5256 break;
5257 }
5258 if ((startcol = curwin->w_cursor.col) == 0)
5259 break;
5260
5261 /* find column of textwidth border */
5262 coladvance((colnr_T)textwidth);
5263 wantcol = curwin->w_cursor.col;
5264
5265 curwin->w_cursor.col = startcol - 1;
5266#ifdef FEAT_MBYTE
5267 /* Correct cursor for multi-byte character. */
5268 if (has_mbyte)
5269 mb_adjust_cursor();
5270#endif
5271 foundcol = 0;
5272
5273 /*
5274 * Find position to break at.
5275 * Stop at first entered white when 'formatoptions' has 'v'
5276 */
5277 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5278 || curwin->w_cursor.lnum != Insstart.lnum
5279 || curwin->w_cursor.col >= Insstart.col)
5280 {
5281 cc = gchar_cursor();
5282 if (WHITECHAR(cc))
5283 {
5284 /* remember position of blank just before text */
5285 end_foundcol = curwin->w_cursor.col;
5286
5287 /* find start of sequence of blanks */
5288 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5289 {
5290 dec_cursor();
5291 cc = gchar_cursor();
5292 }
5293 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5294 break; /* only spaces in front of text */
5295#ifdef FEAT_COMMENTS
5296 /* Don't break until after the comment leader */
5297 if (curwin->w_cursor.col < leader_len)
5298 break;
5299#endif
5300 if (has_format_option(FO_ONE_LETTER))
5301 {
5302 /* do not break after one-letter words */
5303 if (curwin->w_cursor.col == 0)
5304 break; /* one-letter word at begin */
5305
5306 col = curwin->w_cursor.col;
5307 dec_cursor();
5308 cc = gchar_cursor();
5309
5310 if (WHITECHAR(cc))
5311 continue; /* one-letter, continue */
5312 curwin->w_cursor.col = col;
5313 }
5314#ifdef FEAT_MBYTE
5315 if (has_mbyte)
5316 foundcol = curwin->w_cursor.col
5317 + (*mb_ptr2len)(ml_get_cursor());
5318 else
5319#endif
5320 foundcol = curwin->w_cursor.col + 1;
5321 if (curwin->w_cursor.col < (colnr_T)wantcol)
5322 break;
5323 }
5324#ifdef FEAT_MBYTE
5325 else if (cc >= 0x100 && fo_multibyte
5326 && curwin->w_cursor.col <= (colnr_T)wantcol)
5327 {
5328 /* Break after or before a multi-byte character. */
5329 foundcol = curwin->w_cursor.col;
5330 if (curwin->w_cursor.col < (colnr_T)wantcol)
5331 foundcol += (*mb_char2len)(cc);
5332 end_foundcol = foundcol;
5333 break;
5334 }
5335#endif
5336 if (curwin->w_cursor.col == 0)
5337 break;
5338 dec_cursor();
5339 }
5340
5341 if (foundcol == 0) /* no spaces, cannot break line */
5342 {
5343 curwin->w_cursor.col = startcol;
5344 break;
5345 }
5346
5347 /* Going to break the line, remove any "$" now. */
5348 undisplay_dollar();
5349
5350 /*
5351 * Offset between cursor position and line break is used by replace
5352 * stack functions. VREPLACE does not use this, and backspaces
5353 * over the text instead.
5354 */
5355#ifdef FEAT_VREPLACE
5356 if (State & VREPLACE_FLAG)
5357 orig_col = startcol; /* Will start backspacing from here */
5358 else
5359#endif
5360 replace_offset = startcol - end_foundcol - 1;
5361
5362 /*
5363 * adjust startcol for spaces that will be deleted and
5364 * characters that will remain on top line
5365 */
5366 curwin->w_cursor.col = foundcol;
5367 while (cc = gchar_cursor(), WHITECHAR(cc))
5368 inc_cursor();
5369 startcol -= curwin->w_cursor.col;
5370 if (startcol < 0)
5371 startcol = 0;
5372
5373#ifdef FEAT_VREPLACE
5374 if (State & VREPLACE_FLAG)
5375 {
5376 /*
5377 * In VREPLACE mode, we will backspace over the text to be
5378 * wrapped, so save a copy now to put on the next line.
5379 */
5380 saved_text = vim_strsave(ml_get_cursor());
5381 curwin->w_cursor.col = orig_col;
5382 if (saved_text == NULL)
5383 break; /* Can't do it, out of memory */
5384 saved_text[startcol] = NUL;
5385
5386 /* Backspace over characters that will move to the next line */
5387 if (!fo_white_par)
5388 backspace_until_column(foundcol);
5389 }
5390 else
5391#endif
5392 {
5393 /* put cursor after pos. to break line */
5394 if (!fo_white_par)
5395 curwin->w_cursor.col = foundcol;
5396 }
5397
5398 /*
5399 * Split the line just before the margin.
5400 * Only insert/delete lines, but don't really redraw the window.
5401 */
5402 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5403 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5404#ifdef FEAT_COMMENTS
5405 + (do_comments ? OPENLINE_DO_COM : 0)
5406#endif
5407 , old_indent);
5408 old_indent = 0;
5409
5410 replace_offset = 0;
5411 if (first_line)
5412 {
5413 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5414 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5415 if (second_indent >= 0)
5416 {
5417#ifdef FEAT_VREPLACE
5418 if (State & VREPLACE_FLAG)
5419 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5420 else
5421#endif
5422 (void)set_indent(second_indent, SIN_CHANGED);
5423 }
5424 first_line = FALSE;
5425 }
5426
5427#ifdef FEAT_VREPLACE
5428 if (State & VREPLACE_FLAG)
5429 {
5430 /*
5431 * In VREPLACE mode we have backspaced over the text to be
5432 * moved, now we re-insert it into the new line.
5433 */
5434 ins_bytes(saved_text);
5435 vim_free(saved_text);
5436 }
5437 else
5438#endif
5439 {
5440 /*
5441 * Check if cursor is not past the NUL off the line, cindent
5442 * may have added or removed indent.
5443 */
5444 curwin->w_cursor.col += startcol;
5445 len = (colnr_T)STRLEN(ml_get_curline());
5446 if (curwin->w_cursor.col > len)
5447 curwin->w_cursor.col = len;
5448 }
5449
5450 haveto_redraw = TRUE;
5451#ifdef FEAT_CINDENT
5452 can_cindent = TRUE;
5453#endif
5454 /* moved the cursor, don't autoindent or cindent now */
5455 did_ai = FALSE;
5456#ifdef FEAT_SMARTINDENT
5457 did_si = FALSE;
5458 can_si = FALSE;
5459 can_si_back = FALSE;
5460#endif
5461 line_breakcheck();
5462 }
5463
5464 if (save_char != NUL) /* put back space after cursor */
5465 pchar_cursor(save_char);
5466
5467 if (!format_only && haveto_redraw)
5468 {
5469 update_topline();
5470 redraw_curbuf_later(VALID);
5471 }
5472}
5473
5474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 * Called after inserting or deleting text: When 'formatoptions' includes the
5476 * 'a' flag format from the current line until the end of the paragraph.
5477 * Keep the cursor at the same position relative to the text.
5478 * The caller must have saved the cursor line for undo, following ones will be
5479 * saved here.
5480 */
5481 void
5482auto_format(trailblank, prev_line)
5483 int trailblank; /* when TRUE also format with trailing blank */
5484 int prev_line; /* may start in previous line */
5485{
5486 pos_T pos;
5487 colnr_T len;
5488 char_u *old;
5489 char_u *new, *pnew;
5490 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005491 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
5493 if (!has_format_option(FO_AUTO))
5494 return;
5495
5496 pos = curwin->w_cursor;
5497 old = ml_get_curline();
5498
5499 /* may remove added space */
5500 check_auto_format(FALSE);
5501
5502 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5503 * user might insert normal text next. Also skip formatting when "1" is
5504 * in 'formatoptions' and there is a single character before the cursor.
5505 * Otherwise the line would be broken and when typing another non-white
5506 * next they are not joined back together. */
5507 wasatend = (pos.col == STRLEN(old));
5508 if (*old != NUL && !trailblank && wasatend)
5509 {
5510 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005511 cc = gchar_cursor();
5512 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5513 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005515 cc = gchar_cursor();
5516 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 {
5518 curwin->w_cursor = pos;
5519 return;
5520 }
5521 curwin->w_cursor = pos;
5522 }
5523
5524#ifdef FEAT_COMMENTS
5525 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5526 * comments. */
5527 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5528 && get_leader_len(old, NULL, FALSE) == 0)
5529 return;
5530#endif
5531
5532 /*
5533 * May start formatting in a previous line, so that after "x" a word is
5534 * moved to the previous line if it fits there now. Only when this is not
5535 * the start of a paragraph.
5536 */
5537 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5538 {
5539 --curwin->w_cursor.lnum;
5540 if (u_save_cursor() == FAIL)
5541 return;
5542 }
5543
5544 /*
5545 * Do the formatting and restore the cursor position. "saved_cursor" will
5546 * be adjusted for the text formatting.
5547 */
5548 saved_cursor = pos;
5549 format_lines((linenr_T)-1);
5550 curwin->w_cursor = saved_cursor;
5551 saved_cursor.lnum = 0;
5552
5553 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5554 {
5555 /* "cannot happen" */
5556 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5557 coladvance((colnr_T)MAXCOL);
5558 }
5559 else
5560 check_cursor_col();
5561
5562 /* Insert mode: If the cursor is now after the end of the line while it
5563 * previously wasn't, the line was broken. Because of the rule above we
5564 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5565 * formatted. */
5566 if (!wasatend && has_format_option(FO_WHITE_PAR))
5567 {
5568 new = ml_get_curline();
5569 len = STRLEN(new);
5570 if (curwin->w_cursor.col == len)
5571 {
5572 pnew = vim_strnsave(new, len + 2);
5573 pnew[len] = ' ';
5574 pnew[len + 1] = NUL;
5575 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5576 /* remove the space later */
5577 did_add_space = TRUE;
5578 }
5579 else
5580 /* may remove added space */
5581 check_auto_format(FALSE);
5582 }
5583
5584 check_cursor();
5585}
5586
5587/*
5588 * When an extra space was added to continue a paragraph for auto-formatting,
5589 * delete it now. The space must be under the cursor, just after the insert
5590 * position.
5591 */
5592 static void
5593check_auto_format(end_insert)
5594 int end_insert; /* TRUE when ending Insert mode */
5595{
5596 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005597 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
5599 if (did_add_space)
5600 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005601 cc = gchar_cursor();
5602 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 /* Somehow the space was removed already. */
5604 did_add_space = FALSE;
5605 else
5606 {
5607 if (!end_insert)
5608 {
5609 inc_cursor();
5610 c = gchar_cursor();
5611 dec_cursor();
5612 }
5613 if (c != NUL)
5614 {
5615 /* The space is no longer at the end of the line, delete it. */
5616 del_char(FALSE);
5617 did_add_space = FALSE;
5618 }
5619 }
5620 }
5621}
5622
5623/*
5624 * Find out textwidth to be used for formatting:
5625 * if 'textwidth' option is set, use it
5626 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5627 * if invalid value, use 0.
5628 * Set default to window width (maximum 79) for "gq" operator.
5629 */
5630 int
5631comp_textwidth(ff)
5632 int ff; /* force formatting (for "Q" command) */
5633{
5634 int textwidth;
5635
5636 textwidth = curbuf->b_p_tw;
5637 if (textwidth == 0 && curbuf->b_p_wm)
5638 {
5639 /* The width is the window width minus 'wrapmargin' minus all the
5640 * things that add to the margin. */
5641 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5642#ifdef FEAT_CMDWIN
5643 if (cmdwin_type != 0)
5644 textwidth -= 1;
5645#endif
5646#ifdef FEAT_FOLDING
5647 textwidth -= curwin->w_p_fdc;
5648#endif
5649#ifdef FEAT_SIGNS
5650 if (curwin->w_buffer->b_signlist != NULL
5651# ifdef FEAT_NETBEANS_INTG
5652 || usingNetbeans
5653# endif
5654 )
5655 textwidth -= 1;
5656#endif
5657 if (curwin->w_p_nu)
5658 textwidth -= 8;
5659 }
5660 if (textwidth < 0)
5661 textwidth = 0;
5662 if (ff && textwidth == 0)
5663 {
5664 textwidth = W_WIDTH(curwin) - 1;
5665 if (textwidth > 79)
5666 textwidth = 79;
5667 }
5668 return textwidth;
5669}
5670
5671/*
5672 * Put a character in the redo buffer, for when just after a CTRL-V.
5673 */
5674 static void
5675redo_literal(c)
5676 int c;
5677{
5678 char_u buf[10];
5679
5680 /* Only digits need special treatment. Translate them into a string of
5681 * three digits. */
5682 if (VIM_ISDIGIT(c))
5683 {
5684 sprintf((char *)buf, "%03d", c);
5685 AppendToRedobuff(buf);
5686 }
5687 else
5688 AppendCharToRedobuff(c);
5689}
5690
5691/*
5692 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005693 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 */
5695 static void
5696start_arrow(end_insert_pos)
5697 pos_T *end_insert_pos;
5698{
5699 if (!arrow_used) /* something has been inserted */
5700 {
5701 AppendToRedobuff(ESC_STR);
5702 stop_insert(end_insert_pos, FALSE);
5703 arrow_used = TRUE; /* this means we stopped the current insert */
5704 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005705#ifdef FEAT_SYN_HL
5706 check_spell_redraw();
5707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708}
5709
Bram Moolenaar217ad922005-03-20 22:37:15 +00005710#ifdef FEAT_SYN_HL
5711/*
5712 * If we skipped highlighting word at cursor, do it now.
5713 * It may be skipped again, thus reset spell_redraw_lnum first.
5714 */
5715 static void
5716check_spell_redraw()
5717{
5718 if (spell_redraw_lnum != 0)
5719 {
5720 linenr_T lnum = spell_redraw_lnum;
5721
5722 spell_redraw_lnum = 0;
5723 redrawWinline(lnum, FALSE);
5724 }
5725}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005726
5727/*
5728 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5729 * spelled word, if there is one.
5730 */
5731 static void
5732spell_back_to_badword()
5733{
5734 pos_T tpos = curwin->w_cursor;
5735
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005736 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005737 if (curwin->w_cursor.col != tpos.col)
5738 start_arrow(&tpos);
5739}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005740#endif
5741
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742/*
5743 * stop_arrow() is called before a change is made in insert mode.
5744 * If an arrow key has been used, start a new insertion.
5745 * Returns FAIL if undo is impossible, shouldn't insert then.
5746 */
5747 int
5748stop_arrow()
5749{
5750 if (arrow_used)
5751 {
5752 if (u_save_cursor() == OK)
5753 {
5754 arrow_used = FALSE;
5755 ins_need_undo = FALSE;
5756 }
5757 Insstart = curwin->w_cursor; /* new insertion starts here */
5758 Insstart_textlen = linetabsize(ml_get_curline());
5759 ai_col = 0;
5760#ifdef FEAT_VREPLACE
5761 if (State & VREPLACE_FLAG)
5762 {
5763 orig_line_count = curbuf->b_ml.ml_line_count;
5764 vr_lines_changed = 1;
5765 }
5766#endif
5767 ResetRedobuff();
5768 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005769 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 }
5771 else if (ins_need_undo)
5772 {
5773 if (u_save_cursor() == OK)
5774 ins_need_undo = FALSE;
5775 }
5776
5777#ifdef FEAT_FOLDING
5778 /* Always open fold at the cursor line when inserting something. */
5779 foldOpenCursor();
5780#endif
5781
5782 return (arrow_used || ins_need_undo ? FAIL : OK);
5783}
5784
5785/*
5786 * do a few things to stop inserting
5787 */
5788 static void
5789stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005790 pos_T *end_insert_pos; /* where insert ended */
5791 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005793 int cc;
5794 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
5796 stop_redo_ins();
5797 replace_flush(); /* abandon replace stack */
5798
5799 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005800 * Save the inserted text for later redo with ^@ and CTRL-A.
5801 * Don't do it when "restart_edit" was set and nothing was inserted,
5802 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005804 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005805 if (did_restart_edit == 0 || (ptr != NULL
5806 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005807 {
5808 vim_free(last_insert);
5809 last_insert = ptr;
5810 last_insert_skip = new_insert_skip;
5811 }
5812 else
5813 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814
5815 if (!arrow_used)
5816 {
5817 /* Auto-format now. It may seem strange to do this when stopping an
5818 * insertion (or moving the cursor), but it's required when appending
5819 * a line and having it end in a space. But only do it when something
5820 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005821 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005823 pos_T tpos = curwin->w_cursor;
5824
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 /* When the cursor is at the end of the line after a space the
5826 * formatting will move it to the following word. Avoid that by
5827 * moving the cursor onto the space. */
5828 cc = 'x';
5829 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5830 {
5831 dec_cursor();
5832 cc = gchar_cursor();
5833 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005834 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 }
5836
5837 auto_format(TRUE, FALSE);
5838
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005839 if (vim_iswhite(cc))
5840 {
5841 if (gchar_cursor() != NUL)
5842 inc_cursor();
5843#ifdef FEAT_VIRTUALEDIT
5844 /* If the cursor is still at the same character, also keep
5845 * the "coladd". */
5846 if (gchar_cursor() == NUL
5847 && curwin->w_cursor.lnum == tpos.lnum
5848 && curwin->w_cursor.col == tpos.col)
5849 curwin->w_cursor.coladd = tpos.coladd;
5850#endif
5851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 }
5853
5854 /* If a space was inserted for auto-formatting, remove it now. */
5855 check_auto_format(TRUE);
5856
5857 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005858 * of the line, and put the cursor back.
5859 * Do this when ESC was used or moving the cursor up/down. */
5860 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5861 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005863 pos_T tpos = curwin->w_cursor;
5864
5865 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5867 --curwin->w_cursor.col;
5868 while (cc = gchar_cursor(), vim_iswhite(cc))
5869 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005870 if (curwin->w_cursor.lnum != tpos.lnum)
5871 curwin->w_cursor = tpos;
5872 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5874
5875#ifdef FEAT_VISUAL
5876 /* <C-S-Right> may have started Visual mode, adjust the position for
5877 * deleted characters. */
5878 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5879 {
5880 cc = STRLEN(ml_get_curline());
5881 if (VIsual.col > (colnr_T)cc)
5882 {
5883 VIsual.col = cc;
5884# ifdef FEAT_VIRTUALEDIT
5885 VIsual.coladd = 0;
5886# endif
5887 }
5888 }
5889#endif
5890 }
5891 }
5892 did_ai = FALSE;
5893#ifdef FEAT_SMARTINDENT
5894 did_si = FALSE;
5895 can_si = FALSE;
5896 can_si_back = FALSE;
5897#endif
5898
5899 /* set '[ and '] to the inserted text */
5900 curbuf->b_op_start = Insstart;
5901 curbuf->b_op_end = *end_insert_pos;
5902}
5903
5904/*
5905 * Set the last inserted text to a single character.
5906 * Used for the replace command.
5907 */
5908 void
5909set_last_insert(c)
5910 int c;
5911{
5912 char_u *s;
5913
5914 vim_free(last_insert);
5915#ifdef FEAT_MBYTE
5916 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5917#else
5918 last_insert = alloc(6);
5919#endif
5920 if (last_insert != NULL)
5921 {
5922 s = last_insert;
5923 /* Use the CTRL-V only when entering a special char */
5924 if (c < ' ' || c == DEL)
5925 *s++ = Ctrl_V;
5926 s = add_char2buf(c, s);
5927 *s++ = ESC;
5928 *s++ = NUL;
5929 last_insert_skip = 0;
5930 }
5931}
5932
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005933#if defined(EXITFREE) || defined(PROTO)
5934 void
5935free_last_insert()
5936{
5937 vim_free(last_insert);
5938 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005939 vim_free(compl_orig_text);
5940 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005941}
5942#endif
5943
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944/*
5945 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5946 * and CSI. Handle multi-byte characters.
5947 * Returns a pointer to after the added bytes.
5948 */
5949 char_u *
5950add_char2buf(c, s)
5951 int c;
5952 char_u *s;
5953{
5954#ifdef FEAT_MBYTE
5955 char_u temp[MB_MAXBYTES];
5956 int i;
5957 int len;
5958
5959 len = (*mb_char2bytes)(c, temp);
5960 for (i = 0; i < len; ++i)
5961 {
5962 c = temp[i];
5963#endif
5964 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5965 if (c == K_SPECIAL)
5966 {
5967 *s++ = K_SPECIAL;
5968 *s++ = KS_SPECIAL;
5969 *s++ = KE_FILLER;
5970 }
5971#ifdef FEAT_GUI
5972 else if (c == CSI)
5973 {
5974 *s++ = CSI;
5975 *s++ = KS_EXTRA;
5976 *s++ = (int)KE_CSI;
5977 }
5978#endif
5979 else
5980 *s++ = c;
5981#ifdef FEAT_MBYTE
5982 }
5983#endif
5984 return s;
5985}
5986
5987/*
5988 * move cursor to start of line
5989 * if flags & BL_WHITE move to first non-white
5990 * if flags & BL_SOL move to first non-white if startofline is set,
5991 * otherwise keep "curswant" column
5992 * if flags & BL_FIX don't leave the cursor on a NUL.
5993 */
5994 void
5995beginline(flags)
5996 int flags;
5997{
5998 if ((flags & BL_SOL) && !p_sol)
5999 coladvance(curwin->w_curswant);
6000 else
6001 {
6002 curwin->w_cursor.col = 0;
6003#ifdef FEAT_VIRTUALEDIT
6004 curwin->w_cursor.coladd = 0;
6005#endif
6006
6007 if (flags & (BL_WHITE | BL_SOL))
6008 {
6009 char_u *ptr;
6010
6011 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6012 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6013 ++curwin->w_cursor.col;
6014 }
6015 curwin->w_set_curswant = TRUE;
6016 }
6017}
6018
6019/*
6020 * oneright oneleft cursor_down cursor_up
6021 *
6022 * Move one char {right,left,down,up}.
6023 * Doesn't move onto the NUL past the end of the line.
6024 * Return OK when successful, FAIL when we hit a line of file boundary.
6025 */
6026
6027 int
6028oneright()
6029{
6030 char_u *ptr;
6031#ifdef FEAT_MBYTE
6032 int l;
6033#endif
6034
6035#ifdef FEAT_VIRTUALEDIT
6036 if (virtual_active())
6037 {
6038 pos_T prevpos = curwin->w_cursor;
6039
6040 /* Adjust for multi-wide char (excluding TAB) */
6041 ptr = ml_get_cursor();
6042 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
6043#ifdef FEAT_MBYTE
6044 (*mb_ptr2char)(ptr)
6045#else
6046 *ptr
6047#endif
6048 ))
6049 ? ptr2cells(ptr) : 1));
6050 curwin->w_set_curswant = TRUE;
6051 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6052 return (prevpos.col != curwin->w_cursor.col
6053 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6054 }
6055#endif
6056
6057 ptr = ml_get_cursor();
6058#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006059 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 {
6061 /* The character under the cursor is a multi-byte character, move
6062 * several bytes right, but don't end up on the NUL. */
6063 if (ptr[l] == NUL)
6064 return FAIL;
6065 curwin->w_cursor.col += l;
6066 }
6067 else
6068#endif
6069 {
6070 if (*ptr++ == NUL || *ptr == NUL)
6071 return FAIL;
6072 ++curwin->w_cursor.col;
6073 }
6074
6075 curwin->w_set_curswant = TRUE;
6076 return OK;
6077}
6078
6079 int
6080oneleft()
6081{
6082#ifdef FEAT_VIRTUALEDIT
6083 if (virtual_active())
6084 {
6085 int width;
6086 int v = getviscol();
6087
6088 if (v == 0)
6089 return FAIL;
6090
6091# ifdef FEAT_LINEBREAK
6092 /* We might get stuck on 'showbreak', skip over it. */
6093 width = 1;
6094 for (;;)
6095 {
6096 coladvance(v - width);
6097 /* getviscol() is slow, skip it when 'showbreak' is empty and
6098 * there are no multi-byte characters */
6099 if ((*p_sbr == NUL
6100# ifdef FEAT_MBYTE
6101 && !has_mbyte
6102# endif
6103 ) || getviscol() < v)
6104 break;
6105 ++width;
6106 }
6107# else
6108 coladvance(v - 1);
6109# endif
6110
6111 if (curwin->w_cursor.coladd == 1)
6112 {
6113 char_u *ptr;
6114
6115 /* Adjust for multi-wide char (not a TAB) */
6116 ptr = ml_get_cursor();
6117 if (*ptr != TAB && vim_isprintc(
6118# ifdef FEAT_MBYTE
6119 (*mb_ptr2char)(ptr)
6120# else
6121 *ptr
6122# endif
6123 ) && ptr2cells(ptr) > 1)
6124 curwin->w_cursor.coladd = 0;
6125 }
6126
6127 curwin->w_set_curswant = TRUE;
6128 return OK;
6129 }
6130#endif
6131
6132 if (curwin->w_cursor.col == 0)
6133 return FAIL;
6134
6135 curwin->w_set_curswant = TRUE;
6136 --curwin->w_cursor.col;
6137
6138#ifdef FEAT_MBYTE
6139 /* if the character on the left of the current cursor is a multi-byte
6140 * character, move to its first byte */
6141 if (has_mbyte)
6142 mb_adjust_cursor();
6143#endif
6144 return OK;
6145}
6146
6147 int
6148cursor_up(n, upd_topline)
6149 long n;
6150 int upd_topline; /* When TRUE: update topline */
6151{
6152 linenr_T lnum;
6153
6154 if (n > 0)
6155 {
6156 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006157 /* This fails if the cursor is already in the first line or the count
6158 * is larger than the line number and '-' is in 'cpoptions' */
6159 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 return FAIL;
6161 if (n >= lnum)
6162 lnum = 1;
6163 else
6164#ifdef FEAT_FOLDING
6165 if (hasAnyFolding(curwin))
6166 {
6167 /*
6168 * Count each sequence of folded lines as one logical line.
6169 */
6170 /* go to the the start of the current fold */
6171 (void)hasFolding(lnum, &lnum, NULL);
6172
6173 while (n--)
6174 {
6175 /* move up one line */
6176 --lnum;
6177 if (lnum <= 1)
6178 break;
6179 /* If we entered a fold, move to the beginning, unless in
6180 * Insert mode or when 'foldopen' contains "all": it will open
6181 * in a moment. */
6182 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6183 (void)hasFolding(lnum, &lnum, NULL);
6184 }
6185 if (lnum < 1)
6186 lnum = 1;
6187 }
6188 else
6189#endif
6190 lnum -= n;
6191 curwin->w_cursor.lnum = lnum;
6192 }
6193
6194 /* try to advance to the column we want to be at */
6195 coladvance(curwin->w_curswant);
6196
6197 if (upd_topline)
6198 update_topline(); /* make sure curwin->w_topline is valid */
6199
6200 return OK;
6201}
6202
6203/*
6204 * Cursor down a number of logical lines.
6205 */
6206 int
6207cursor_down(n, upd_topline)
6208 long n;
6209 int upd_topline; /* When TRUE: update topline */
6210{
6211 linenr_T lnum;
6212
6213 if (n > 0)
6214 {
6215 lnum = curwin->w_cursor.lnum;
6216#ifdef FEAT_FOLDING
6217 /* Move to last line of fold, will fail if it's the end-of-file. */
6218 (void)hasFolding(lnum, NULL, &lnum);
6219#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006220 /* This fails if the cursor is already in the last line or would move
6221 * beyound the last line and '-' is in 'cpoptions' */
6222 if (lnum >= curbuf->b_ml.ml_line_count
6223 || (lnum + n > curbuf->b_ml.ml_line_count
6224 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 return FAIL;
6226 if (lnum + n >= curbuf->b_ml.ml_line_count)
6227 lnum = curbuf->b_ml.ml_line_count;
6228 else
6229#ifdef FEAT_FOLDING
6230 if (hasAnyFolding(curwin))
6231 {
6232 linenr_T last;
6233
6234 /* count each sequence of folded lines as one logical line */
6235 while (n--)
6236 {
6237 if (hasFolding(lnum, NULL, &last))
6238 lnum = last + 1;
6239 else
6240 ++lnum;
6241 if (lnum >= curbuf->b_ml.ml_line_count)
6242 break;
6243 }
6244 if (lnum > curbuf->b_ml.ml_line_count)
6245 lnum = curbuf->b_ml.ml_line_count;
6246 }
6247 else
6248#endif
6249 lnum += n;
6250 curwin->w_cursor.lnum = lnum;
6251 }
6252
6253 /* try to advance to the column we want to be at */
6254 coladvance(curwin->w_curswant);
6255
6256 if (upd_topline)
6257 update_topline(); /* make sure curwin->w_topline is valid */
6258
6259 return OK;
6260}
6261
6262/*
6263 * Stuff the last inserted text in the read buffer.
6264 * Last_insert actually is a copy of the redo buffer, so we
6265 * first have to remove the command.
6266 */
6267 int
6268stuff_inserted(c, count, no_esc)
6269 int c; /* Command character to be inserted */
6270 long count; /* Repeat this many times */
6271 int no_esc; /* Don't add an ESC at the end */
6272{
6273 char_u *esc_ptr;
6274 char_u *ptr;
6275 char_u *last_ptr;
6276 char_u last = NUL;
6277
6278 ptr = get_last_insert();
6279 if (ptr == NULL)
6280 {
6281 EMSG(_(e_noinstext));
6282 return FAIL;
6283 }
6284
6285 /* may want to stuff the command character, to start Insert mode */
6286 if (c != NUL)
6287 stuffcharReadbuff(c);
6288 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6289 *esc_ptr = NUL; /* remove the ESC */
6290
6291 /* when the last char is either "0" or "^" it will be quoted if no ESC
6292 * comes after it OR if it will inserted more than once and "ptr"
6293 * starts with ^D. -- Acevedo
6294 */
6295 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6296 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6297 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6298 {
6299 last = *last_ptr;
6300 *last_ptr = NUL;
6301 }
6302
6303 do
6304 {
6305 stuffReadbuff(ptr);
6306 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6307 if (last)
6308 stuffReadbuff((char_u *)(last == '0'
6309 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6310 : IF_EB("\026^", CTRL_V_STR "^")));
6311 }
6312 while (--count > 0);
6313
6314 if (last)
6315 *last_ptr = last;
6316
6317 if (esc_ptr != NULL)
6318 *esc_ptr = ESC; /* put the ESC back */
6319
6320 /* may want to stuff a trailing ESC, to get out of Insert mode */
6321 if (!no_esc)
6322 stuffcharReadbuff(ESC);
6323
6324 return OK;
6325}
6326
6327 char_u *
6328get_last_insert()
6329{
6330 if (last_insert == NULL)
6331 return NULL;
6332 return last_insert + last_insert_skip;
6333}
6334
6335/*
6336 * Get last inserted string, and remove trailing <Esc>.
6337 * Returns pointer to allocated memory (must be freed) or NULL.
6338 */
6339 char_u *
6340get_last_insert_save()
6341{
6342 char_u *s;
6343 int len;
6344
6345 if (last_insert == NULL)
6346 return NULL;
6347 s = vim_strsave(last_insert + last_insert_skip);
6348 if (s != NULL)
6349 {
6350 len = (int)STRLEN(s);
6351 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6352 s[len - 1] = NUL;
6353 }
6354 return s;
6355}
6356
6357/*
6358 * Check the word in front of the cursor for an abbreviation.
6359 * Called when the non-id character "c" has been entered.
6360 * When an abbreviation is recognized it is removed from the text and
6361 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6362 */
6363 static int
6364echeck_abbr(c)
6365 int c;
6366{
6367 /* Don't check for abbreviation in paste mode, when disabled and just
6368 * after moving around with cursor keys. */
6369 if (p_paste || no_abbr || arrow_used)
6370 return FALSE;
6371
6372 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6373 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6374}
6375
6376/*
6377 * replace-stack functions
6378 *
6379 * When replacing characters, the replaced characters are remembered for each
6380 * new character. This is used to re-insert the old text when backspacing.
6381 *
6382 * There is a NUL headed list of characters for each character that is
6383 * currently in the file after the insertion point. When BS is used, one NUL
6384 * headed list is put back for the deleted character.
6385 *
6386 * For a newline, there are two NUL headed lists. One contains the characters
6387 * that the NL replaced. The extra one stores the characters after the cursor
6388 * that were deleted (always white space).
6389 *
6390 * Replace_offset is normally 0, in which case replace_push will add a new
6391 * character at the end of the stack. If replace_offset is not 0, that many
6392 * characters will be left on the stack above the newly inserted character.
6393 */
6394
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006395static char_u *replace_stack = NULL;
6396static long replace_stack_nr = 0; /* next entry in replace stack */
6397static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398
6399 void
6400replace_push(c)
6401 int c; /* character that is replaced (NUL is none) */
6402{
6403 char_u *p;
6404
6405 if (replace_stack_nr < replace_offset) /* nothing to do */
6406 return;
6407 if (replace_stack_len <= replace_stack_nr)
6408 {
6409 replace_stack_len += 50;
6410 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6411 if (p == NULL) /* out of memory */
6412 {
6413 replace_stack_len -= 50;
6414 return;
6415 }
6416 if (replace_stack != NULL)
6417 {
6418 mch_memmove(p, replace_stack,
6419 (size_t)(replace_stack_nr * sizeof(char_u)));
6420 vim_free(replace_stack);
6421 }
6422 replace_stack = p;
6423 }
6424 p = replace_stack + replace_stack_nr - replace_offset;
6425 if (replace_offset)
6426 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6427 *p = c;
6428 ++replace_stack_nr;
6429}
6430
6431/*
6432 * call replace_push(c) with replace_offset set to the first NUL.
6433 */
6434 static void
6435replace_push_off(c)
6436 int c;
6437{
6438 char_u *p;
6439
6440 p = replace_stack + replace_stack_nr;
6441 for (replace_offset = 1; replace_offset < replace_stack_nr;
6442 ++replace_offset)
6443 if (*--p == NUL)
6444 break;
6445 replace_push(c);
6446 replace_offset = 0;
6447}
6448
6449/*
6450 * Pop one item from the replace stack.
6451 * return -1 if stack empty
6452 * return replaced character or NUL otherwise
6453 */
6454 static int
6455replace_pop()
6456{
6457 if (replace_stack_nr == 0)
6458 return -1;
6459 return (int)replace_stack[--replace_stack_nr];
6460}
6461
6462/*
6463 * Join the top two items on the replace stack. This removes to "off"'th NUL
6464 * encountered.
6465 */
6466 static void
6467replace_join(off)
6468 int off; /* offset for which NUL to remove */
6469{
6470 int i;
6471
6472 for (i = replace_stack_nr; --i >= 0; )
6473 if (replace_stack[i] == NUL && off-- <= 0)
6474 {
6475 --replace_stack_nr;
6476 mch_memmove(replace_stack + i, replace_stack + i + 1,
6477 (size_t)(replace_stack_nr - i));
6478 return;
6479 }
6480}
6481
6482/*
6483 * Pop bytes from the replace stack until a NUL is found, and insert them
6484 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6485 */
6486 static void
6487replace_pop_ins()
6488{
6489 int cc;
6490 int oldState = State;
6491
6492 State = NORMAL; /* don't want REPLACE here */
6493 while ((cc = replace_pop()) > 0)
6494 {
6495#ifdef FEAT_MBYTE
6496 mb_replace_pop_ins(cc);
6497#else
6498 ins_char(cc);
6499#endif
6500 dec_cursor();
6501 }
6502 State = oldState;
6503}
6504
6505#ifdef FEAT_MBYTE
6506/*
6507 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6508 * indicates a multi-byte char, pop the other bytes too.
6509 */
6510 static void
6511mb_replace_pop_ins(cc)
6512 int cc;
6513{
6514 int n;
6515 char_u buf[MB_MAXBYTES];
6516 int i;
6517 int c;
6518
6519 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6520 {
6521 buf[0] = cc;
6522 for (i = 1; i < n; ++i)
6523 buf[i] = replace_pop();
6524 ins_bytes_len(buf, n);
6525 }
6526 else
6527 ins_char(cc);
6528
6529 if (enc_utf8)
6530 /* Handle composing chars. */
6531 for (;;)
6532 {
6533 c = replace_pop();
6534 if (c == -1) /* stack empty */
6535 break;
6536 if ((n = MB_BYTE2LEN(c)) == 1)
6537 {
6538 /* Not a multi-byte char, put it back. */
6539 replace_push(c);
6540 break;
6541 }
6542 else
6543 {
6544 buf[0] = c;
6545 for (i = 1; i < n; ++i)
6546 buf[i] = replace_pop();
6547 if (utf_iscomposing(utf_ptr2char(buf)))
6548 ins_bytes_len(buf, n);
6549 else
6550 {
6551 /* Not a composing char, put it back. */
6552 for (i = n - 1; i >= 0; --i)
6553 replace_push(buf[i]);
6554 break;
6555 }
6556 }
6557 }
6558}
6559#endif
6560
6561/*
6562 * make the replace stack empty
6563 * (called when exiting replace mode)
6564 */
6565 static void
6566replace_flush()
6567{
6568 vim_free(replace_stack);
6569 replace_stack = NULL;
6570 replace_stack_len = 0;
6571 replace_stack_nr = 0;
6572}
6573
6574/*
6575 * Handle doing a BS for one character.
6576 * cc < 0: replace stack empty, just move cursor
6577 * cc == 0: character was inserted, delete it
6578 * cc > 0: character was replaced, put cc (first byte of original char) back
6579 * and check for more characters to be put back
6580 */
6581 static void
6582replace_do_bs()
6583{
6584 int cc;
6585#ifdef FEAT_VREPLACE
6586 int orig_len = 0;
6587 int ins_len;
6588 int orig_vcols = 0;
6589 colnr_T start_vcol;
6590 char_u *p;
6591 int i;
6592 int vcol;
6593#endif
6594
6595 cc = replace_pop();
6596 if (cc > 0)
6597 {
6598#ifdef FEAT_VREPLACE
6599 if (State & VREPLACE_FLAG)
6600 {
6601 /* Get the number of screen cells used by the character we are
6602 * going to delete. */
6603 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6604 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6605 }
6606#endif
6607#ifdef FEAT_MBYTE
6608 if (has_mbyte)
6609 {
6610 del_char(FALSE);
6611# ifdef FEAT_VREPLACE
6612 if (State & VREPLACE_FLAG)
6613 orig_len = STRLEN(ml_get_cursor());
6614# endif
6615 replace_push(cc);
6616 }
6617 else
6618#endif
6619 {
6620 pchar_cursor(cc);
6621#ifdef FEAT_VREPLACE
6622 if (State & VREPLACE_FLAG)
6623 orig_len = STRLEN(ml_get_cursor()) - 1;
6624#endif
6625 }
6626 replace_pop_ins();
6627
6628#ifdef FEAT_VREPLACE
6629 if (State & VREPLACE_FLAG)
6630 {
6631 /* Get the number of screen cells used by the inserted characters */
6632 p = ml_get_cursor();
6633 ins_len = STRLEN(p) - orig_len;
6634 vcol = start_vcol;
6635 for (i = 0; i < ins_len; ++i)
6636 {
6637 vcol += chartabsize(p + i, vcol);
6638#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006639 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640#endif
6641 }
6642 vcol -= start_vcol;
6643
6644 /* Delete spaces that were inserted after the cursor to keep the
6645 * text aligned. */
6646 curwin->w_cursor.col += ins_len;
6647 while (vcol > orig_vcols && gchar_cursor() == ' ')
6648 {
6649 del_char(FALSE);
6650 ++orig_vcols;
6651 }
6652 curwin->w_cursor.col -= ins_len;
6653 }
6654#endif
6655
6656 /* mark the buffer as changed and prepare for displaying */
6657 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6658 }
6659 else if (cc == 0)
6660 (void)del_char(FALSE);
6661}
6662
6663#ifdef FEAT_CINDENT
6664/*
6665 * Return TRUE if C-indenting is on.
6666 */
6667 static int
6668cindent_on()
6669{
6670 return (!p_paste && (curbuf->b_p_cin
6671# ifdef FEAT_EVAL
6672 || *curbuf->b_p_inde != NUL
6673# endif
6674 ));
6675}
6676#endif
6677
6678#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6679/*
6680 * Re-indent the current line, based on the current contents of it and the
6681 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6682 * confused what all the part that handles Control-T is doing that I'm not.
6683 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6684 */
6685
6686 void
6687fixthisline(get_the_indent)
6688 int (*get_the_indent) __ARGS((void));
6689{
6690 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6691 if (linewhite(curwin->w_cursor.lnum))
6692 did_ai = TRUE; /* delete the indent if the line stays empty */
6693}
6694
6695 void
6696fix_indent()
6697{
6698 if (p_paste)
6699 return;
6700# ifdef FEAT_LISP
6701 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6702 fixthisline(get_lisp_indent);
6703# endif
6704# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6705 else
6706# endif
6707# ifdef FEAT_CINDENT
6708 if (cindent_on())
6709 do_c_expr_indent();
6710# endif
6711}
6712
6713#endif
6714
6715#ifdef FEAT_CINDENT
6716/*
6717 * return TRUE if 'cinkeys' contains the key "keytyped",
6718 * when == '*': Only if key is preceded with '*' (indent before insert)
6719 * when == '!': Only if key is prededed with '!' (don't insert)
6720 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6721 *
6722 * "keytyped" can have a few special values:
6723 * KEY_OPEN_FORW
6724 * KEY_OPEN_BACK
6725 * KEY_COMPLETE just finished completion.
6726 *
6727 * If line_is_empty is TRUE accept keys with '0' before them.
6728 */
6729 int
6730in_cinkeys(keytyped, when, line_is_empty)
6731 int keytyped;
6732 int when;
6733 int line_is_empty;
6734{
6735 char_u *look;
6736 int try_match;
6737 int try_match_word;
6738 char_u *p;
6739 char_u *line;
6740 int icase;
6741 int i;
6742
6743#ifdef FEAT_EVAL
6744 if (*curbuf->b_p_inde != NUL)
6745 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6746 else
6747#endif
6748 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6749 while (*look)
6750 {
6751 /*
6752 * Find out if we want to try a match with this key, depending on
6753 * 'when' and a '*' or '!' before the key.
6754 */
6755 switch (when)
6756 {
6757 case '*': try_match = (*look == '*'); break;
6758 case '!': try_match = (*look == '!'); break;
6759 default: try_match = (*look != '*'); break;
6760 }
6761 if (*look == '*' || *look == '!')
6762 ++look;
6763
6764 /*
6765 * If there is a '0', only accept a match if the line is empty.
6766 * But may still match when typing last char of a word.
6767 */
6768 if (*look == '0')
6769 {
6770 try_match_word = try_match;
6771 if (!line_is_empty)
6772 try_match = FALSE;
6773 ++look;
6774 }
6775 else
6776 try_match_word = FALSE;
6777
6778 /*
6779 * does it look like a control character?
6780 */
6781 if (*look == '^'
6782#ifdef EBCDIC
6783 && (Ctrl_chr(look[1]) != 0)
6784#else
6785 && look[1] >= '?' && look[1] <= '_'
6786#endif
6787 )
6788 {
6789 if (try_match && keytyped == Ctrl_chr(look[1]))
6790 return TRUE;
6791 look += 2;
6792 }
6793 /*
6794 * 'o' means "o" command, open forward.
6795 * 'O' means "O" command, open backward.
6796 */
6797 else if (*look == 'o')
6798 {
6799 if (try_match && keytyped == KEY_OPEN_FORW)
6800 return TRUE;
6801 ++look;
6802 }
6803 else if (*look == 'O')
6804 {
6805 if (try_match && keytyped == KEY_OPEN_BACK)
6806 return TRUE;
6807 ++look;
6808 }
6809
6810 /*
6811 * 'e' means to check for "else" at start of line and just before the
6812 * cursor.
6813 */
6814 else if (*look == 'e')
6815 {
6816 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6817 {
6818 p = ml_get_curline();
6819 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6820 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6821 return TRUE;
6822 }
6823 ++look;
6824 }
6825
6826 /*
6827 * ':' only causes an indent if it is at the end of a label or case
6828 * statement, or when it was before typing the ':' (to fix
6829 * class::method for C++).
6830 */
6831 else if (*look == ':')
6832 {
6833 if (try_match && keytyped == ':')
6834 {
6835 p = ml_get_curline();
6836 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6837 return TRUE;
6838 if (curwin->w_cursor.col > 2
6839 && p[curwin->w_cursor.col - 1] == ':'
6840 && p[curwin->w_cursor.col - 2] == ':')
6841 {
6842 p[curwin->w_cursor.col - 1] = ' ';
6843 i = (cin_iscase(p) || cin_isscopedecl(p)
6844 || cin_islabel(30));
6845 p = ml_get_curline();
6846 p[curwin->w_cursor.col - 1] = ':';
6847 if (i)
6848 return TRUE;
6849 }
6850 }
6851 ++look;
6852 }
6853
6854
6855 /*
6856 * Is it a key in <>, maybe?
6857 */
6858 else if (*look == '<')
6859 {
6860 if (try_match)
6861 {
6862 /*
6863 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6864 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6865 * >, *, : and ! keys if they really really want to.
6866 */
6867 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6868 && keytyped == look[1])
6869 return TRUE;
6870
6871 if (keytyped == get_special_key_code(look + 1))
6872 return TRUE;
6873 }
6874 while (*look && *look != '>')
6875 look++;
6876 while (*look == '>')
6877 look++;
6878 }
6879
6880 /*
6881 * Is it a word: "=word"?
6882 */
6883 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6884 {
6885 ++look;
6886 if (*look == '~')
6887 {
6888 icase = TRUE;
6889 ++look;
6890 }
6891 else
6892 icase = FALSE;
6893 p = vim_strchr(look, ',');
6894 if (p == NULL)
6895 p = look + STRLEN(look);
6896 if ((try_match || try_match_word)
6897 && curwin->w_cursor.col >= (colnr_T)(p - look))
6898 {
6899 int match = FALSE;
6900
6901#ifdef FEAT_INS_EXPAND
6902 if (keytyped == KEY_COMPLETE)
6903 {
6904 char_u *s;
6905
6906 /* Just completed a word, check if it starts with "look".
6907 * search back for the start of a word. */
6908 line = ml_get_curline();
6909# ifdef FEAT_MBYTE
6910 if (has_mbyte)
6911 {
6912 char_u *n;
6913
6914 for (s = line + curwin->w_cursor.col; s > line; s = n)
6915 {
6916 n = mb_prevptr(line, s);
6917 if (!vim_iswordp(n))
6918 break;
6919 }
6920 }
6921 else
6922# endif
6923 for (s = line + curwin->w_cursor.col; s > line; --s)
6924 if (!vim_iswordc(s[-1]))
6925 break;
6926 if (s + (p - look) <= line + curwin->w_cursor.col
6927 && (icase
6928 ? MB_STRNICMP(s, look, p - look)
6929 : STRNCMP(s, look, p - look)) == 0)
6930 match = TRUE;
6931 }
6932 else
6933#endif
6934 /* TODO: multi-byte */
6935 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6936 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6937 {
6938 line = ml_get_cursor();
6939 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6940 || !vim_iswordc(line[-(p - look) - 1]))
6941 && (icase
6942 ? MB_STRNICMP(line - (p - look), look, p - look)
6943 : STRNCMP(line - (p - look), look, p - look))
6944 == 0)
6945 match = TRUE;
6946 }
6947 if (match && try_match_word && !try_match)
6948 {
6949 /* "0=word": Check if there are only blanks before the
6950 * word. */
6951 line = ml_get_curline();
6952 if ((int)(skipwhite(line) - line) !=
6953 (int)(curwin->w_cursor.col - (p - look)))
6954 match = FALSE;
6955 }
6956 if (match)
6957 return TRUE;
6958 }
6959 look = p;
6960 }
6961
6962 /*
6963 * ok, it's a boring generic character.
6964 */
6965 else
6966 {
6967 if (try_match && *look == keytyped)
6968 return TRUE;
6969 ++look;
6970 }
6971
6972 /*
6973 * Skip over ", ".
6974 */
6975 look = skip_to_option_part(look);
6976 }
6977 return FALSE;
6978}
6979#endif /* FEAT_CINDENT */
6980
6981#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6982/*
6983 * Map Hebrew keyboard when in hkmap mode.
6984 */
6985 int
6986hkmap(c)
6987 int c;
6988{
6989 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6990 {
6991 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6992 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6993 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6994 static char_u map[26] =
6995 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6996 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6997 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6998 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6999 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7000 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7001 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7002 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7003 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7004
7005 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7006 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7007 /* '-1'='sofit' */
7008 else if (c == 'x')
7009 return 'X';
7010 else if (c == 'q')
7011 return '\''; /* {geresh}={'} */
7012 else if (c == 246)
7013 return ' '; /* \"o --> ' ' for a german keyboard */
7014 else if (c == 228)
7015 return ' '; /* \"a --> ' ' -- / -- */
7016 else if (c == 252)
7017 return ' '; /* \"u --> ' ' -- / -- */
7018#ifdef EBCDIC
7019 else if (islower(c))
7020#else
7021 /* NOTE: islower() does not do the right thing for us on Linux so we
7022 * do this the same was as 5.7 and previous, so it works correctly on
7023 * all systems. Specifically, the e.g. Delete and Arrow keys are
7024 * munged and won't work if e.g. searching for Hebrew text.
7025 */
7026 else if (c >= 'a' && c <= 'z')
7027#endif
7028 return (int)(map[CharOrdLow(c)] + p_aleph);
7029 else
7030 return c;
7031 }
7032 else
7033 {
7034 switch (c)
7035 {
7036 case '`': return ';';
7037 case '/': return '.';
7038 case '\'': return ',';
7039 case 'q': return '/';
7040 case 'w': return '\'';
7041
7042 /* Hebrew letters - set offset from 'a' */
7043 case ',': c = '{'; break;
7044 case '.': c = 'v'; break;
7045 case ';': c = 't'; break;
7046 default: {
7047 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7048
7049#ifdef EBCDIC
7050 /* see note about islower() above */
7051 if (!islower(c))
7052#else
7053 if (c < 'a' || c > 'z')
7054#endif
7055 return c;
7056 c = str[CharOrdLow(c)];
7057 break;
7058 }
7059 }
7060
7061 return (int)(CharOrdLow(c) + p_aleph);
7062 }
7063}
7064#endif
7065
7066 static void
7067ins_reg()
7068{
7069 int need_redraw = FALSE;
7070 int regname;
7071 int literally = 0;
7072
7073 /*
7074 * If we are going to wait for a character, show a '"'.
7075 */
7076 pc_status = PC_STATUS_UNSET;
7077 if (redrawing() && !char_avail())
7078 {
7079 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007080 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081
7082 edit_putchar('"', TRUE);
7083#ifdef FEAT_CMDL_INFO
7084 add_to_showcmd_c(Ctrl_R);
7085#endif
7086 }
7087
7088#ifdef USE_ON_FLY_SCROLL
7089 dont_scroll = TRUE; /* disallow scrolling here */
7090#endif
7091
7092 /*
7093 * Don't map the register name. This also prevents the mode message to be
7094 * deleted when ESC is hit.
7095 */
7096 ++no_mapping;
7097 regname = safe_vgetc();
7098#ifdef FEAT_LANGMAP
7099 LANGMAP_ADJUST(regname, TRUE);
7100#endif
7101 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7102 {
7103 /* Get a third key for literal register insertion */
7104 literally = regname;
7105#ifdef FEAT_CMDL_INFO
7106 add_to_showcmd_c(literally);
7107#endif
7108 regname = safe_vgetc();
7109#ifdef FEAT_LANGMAP
7110 LANGMAP_ADJUST(regname, TRUE);
7111#endif
7112 }
7113 --no_mapping;
7114
7115#ifdef FEAT_EVAL
7116 /*
7117 * Don't call u_sync() while getting the expression,
7118 * evaluating it or giving an error message for it!
7119 */
7120 ++no_u_sync;
7121 if (regname == '=')
7122 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007123# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007127# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 /* Restore the Input Method. */
7129 if (im_on)
7130 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007131# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007133 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7134 {
7135 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 else
7139 {
7140#endif
7141 if (literally == Ctrl_O || literally == Ctrl_P)
7142 {
7143 /* Append the command to the redo buffer. */
7144 AppendCharToRedobuff(Ctrl_R);
7145 AppendCharToRedobuff(literally);
7146 AppendCharToRedobuff(regname);
7147
7148 do_put(regname, BACKWARD, 1L,
7149 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7150 }
7151 else if (insert_reg(regname, literally) == FAIL)
7152 {
7153 vim_beep();
7154 need_redraw = TRUE; /* remove the '"' */
7155 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007156 else if (stop_insert_mode)
7157 /* When the '=' register was used and a function was invoked that
7158 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7159 * insert anything, need to remove the '"' */
7160 need_redraw = TRUE;
7161
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162#ifdef FEAT_EVAL
7163 }
7164 --no_u_sync;
7165#endif
7166#ifdef FEAT_CMDL_INFO
7167 clear_showcmd();
7168#endif
7169
7170 /* If the inserted register is empty, we need to remove the '"' */
7171 if (need_redraw || stuff_empty())
7172 edit_unputchar();
7173}
7174
7175/*
7176 * CTRL-G commands in Insert mode.
7177 */
7178 static void
7179ins_ctrl_g()
7180{
7181 int c;
7182
7183#ifdef FEAT_INS_EXPAND
7184 /* Right after CTRL-X the cursor will be after the ruler. */
7185 setcursor();
7186#endif
7187
7188 /*
7189 * Don't map the second key. This also prevents the mode message to be
7190 * deleted when ESC is hit.
7191 */
7192 ++no_mapping;
7193 c = safe_vgetc();
7194 --no_mapping;
7195 switch (c)
7196 {
7197 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7198 case K_UP:
7199 case Ctrl_K:
7200 case 'k': ins_up(TRUE);
7201 break;
7202
7203 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7204 case K_DOWN:
7205 case Ctrl_J:
7206 case 'j': ins_down(TRUE);
7207 break;
7208
7209 /* CTRL-G u: start new undoable edit */
7210 case 'u': u_sync();
7211 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007212
7213 /* Need to reset Insstart, esp. because a BS that joins
7214 * aline to the previous one must save for undo. */
7215 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 break;
7217
7218 /* Unknown CTRL-G command, reserved for future expansion. */
7219 default: vim_beep();
7220 }
7221}
7222
7223/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007224 * CTRL-^ in Insert mode.
7225 */
7226 static void
7227ins_ctrl_hat()
7228{
7229 if (map_to_exists_mode((char_u *)"", LANGMAP))
7230 {
7231 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7232 if (State & LANGMAP)
7233 {
7234 curbuf->b_p_iminsert = B_IMODE_NONE;
7235 State &= ~LANGMAP;
7236 }
7237 else
7238 {
7239 curbuf->b_p_iminsert = B_IMODE_LMAP;
7240 State |= LANGMAP;
7241#ifdef USE_IM_CONTROL
7242 im_set_active(FALSE);
7243#endif
7244 }
7245 }
7246#ifdef USE_IM_CONTROL
7247 else
7248 {
7249 /* There are no ":lmap" mappings, toggle IM */
7250 if (im_get_status())
7251 {
7252 curbuf->b_p_iminsert = B_IMODE_NONE;
7253 im_set_active(FALSE);
7254 }
7255 else
7256 {
7257 curbuf->b_p_iminsert = B_IMODE_IM;
7258 State &= ~LANGMAP;
7259 im_set_active(TRUE);
7260 }
7261 }
7262#endif
7263 set_iminsert_global();
7264 showmode();
7265#ifdef FEAT_GUI
7266 /* may show different cursor shape or color */
7267 if (gui.in_use)
7268 gui_update_cursor(TRUE, FALSE);
7269#endif
7270#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7271 /* Show/unshow value of 'keymap' in status lines. */
7272 status_redraw_curbuf();
7273#endif
7274}
7275
7276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 * Handle ESC in insert mode.
7278 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7279 * insert.
7280 */
7281 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007282ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 long *count;
7284 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007285 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286{
7287 int temp;
7288 static int disabled_redraw = FALSE;
7289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007290#ifdef FEAT_SYN_HL
7291 check_spell_redraw();
7292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293#if defined(FEAT_HANGULIN)
7294# if defined(ESC_CHG_TO_ENG_MODE)
7295 hangul_input_state_set(0);
7296# endif
7297 if (composing_hangul)
7298 {
7299 push_raw_key(composing_hangul_buffer, 2);
7300 composing_hangul = 0;
7301 }
7302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303
7304 temp = curwin->w_cursor.col;
7305 if (disabled_redraw)
7306 {
7307 --RedrawingDisabled;
7308 disabled_redraw = FALSE;
7309 }
7310 if (!arrow_used)
7311 {
7312 /*
7313 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007314 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7315 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 */
7317 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007318 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
7320 /*
7321 * Repeating insert may take a long time. Check for
7322 * interrupt now and then.
7323 */
7324 if (*count > 0)
7325 {
7326 line_breakcheck();
7327 if (got_int)
7328 *count = 0;
7329 }
7330
7331 if (--*count > 0) /* repeat what was typed */
7332 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007333 /* Vi repeats the insert without replacing characters. */
7334 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7335 State &= ~REPLACE_FLAG;
7336
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 (void)start_redo_ins();
7338 if (cmdchar == 'r' || cmdchar == 'v')
7339 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7340 ++RedrawingDisabled;
7341 disabled_redraw = TRUE;
7342 return FALSE; /* repeat the insert */
7343 }
7344 stop_insert(&curwin->w_cursor, TRUE);
7345 undisplay_dollar();
7346 }
7347
7348 /* When an autoindent was removed, curswant stays after the
7349 * indent */
7350 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7351 curwin->w_set_curswant = TRUE;
7352
7353 /* Remember the last Insert position in the '^ mark. */
7354 if (!cmdmod.keepjumps)
7355 curbuf->b_last_insert = curwin->w_cursor;
7356
7357 /*
7358 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007359 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007361 if (!nomove
7362 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363#ifdef FEAT_VIRTUALEDIT
7364 || curwin->w_cursor.coladd > 0
7365#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007366 )
7367 && (restart_edit == NUL
7368 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007370 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007372 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373#ifdef FEAT_RIGHTLEFT
7374 && !revins_on
7375#endif
7376 )
7377 {
7378#ifdef FEAT_VIRTUALEDIT
7379 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7380 {
7381 oneleft();
7382 if (restart_edit != NUL)
7383 ++curwin->w_cursor.coladd;
7384 }
7385 else
7386#endif
7387 {
7388 --curwin->w_cursor.col;
7389#ifdef FEAT_MBYTE
7390 /* Correct cursor for multi-byte character. */
7391 if (has_mbyte)
7392 mb_adjust_cursor();
7393#endif
7394 }
7395 }
7396
7397#ifdef USE_IM_CONTROL
7398 /* Disable IM to allow typing English directly for Normal mode commands.
7399 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7400 * well). */
7401 if (!(State & LANGMAP))
7402 im_save_status(&curbuf->b_p_iminsert);
7403 im_set_active(FALSE);
7404#endif
7405
7406 State = NORMAL;
7407 /* need to position cursor again (e.g. when on a TAB ) */
7408 changed_cline_bef_curs();
7409
7410#ifdef FEAT_MOUSE
7411 setmouse();
7412#endif
7413#ifdef CURSOR_SHAPE
7414 ui_cursor_shape(); /* may show different cursor shape */
7415#endif
7416
7417 /*
7418 * When recording or for CTRL-O, need to display the new mode.
7419 * Otherwise remove the mode message.
7420 */
7421 if (Recording || restart_edit != NUL)
7422 showmode();
7423 else if (p_smd)
7424 MSG("");
7425
7426 return TRUE; /* exit Insert mode */
7427}
7428
7429#ifdef FEAT_RIGHTLEFT
7430/*
7431 * Toggle language: hkmap and revins_on.
7432 * Move to end of reverse inserted text.
7433 */
7434 static void
7435ins_ctrl_()
7436{
7437 if (revins_on && revins_chars && revins_scol >= 0)
7438 {
7439 while (gchar_cursor() != NUL && revins_chars--)
7440 ++curwin->w_cursor.col;
7441 }
7442 p_ri = !p_ri;
7443 revins_on = (State == INSERT && p_ri);
7444 if (revins_on)
7445 {
7446 revins_scol = curwin->w_cursor.col;
7447 revins_legal++;
7448 revins_chars = 0;
7449 undisplay_dollar();
7450 }
7451 else
7452 revins_scol = -1;
7453#ifdef FEAT_FKMAP
7454 if (p_altkeymap)
7455 {
7456 /*
7457 * to be consistent also for redo command, using '.'
7458 * set arrow_used to true and stop it - causing to redo
7459 * characters entered in one mode (normal/reverse insert).
7460 */
7461 arrow_used = TRUE;
7462 (void)stop_arrow();
7463 p_fkmap = curwin->w_p_rl ^ p_ri;
7464 if (p_fkmap && p_ri)
7465 State = INSERT;
7466 }
7467 else
7468#endif
7469 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7470 showmode();
7471}
7472#endif
7473
7474#ifdef FEAT_VISUAL
7475/*
7476 * If 'keymodel' contains "startsel", may start selection.
7477 * Returns TRUE when a CTRL-O and other keys stuffed.
7478 */
7479 static int
7480ins_start_select(c)
7481 int c;
7482{
7483 if (km_startsel)
7484 switch (c)
7485 {
7486 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 case K_PAGEUP:
7489 case K_KPAGEUP:
7490 case K_PAGEDOWN:
7491 case K_KPAGEDOWN:
7492# ifdef MACOS
7493 case K_LEFT:
7494 case K_RIGHT:
7495 case K_UP:
7496 case K_DOWN:
7497 case K_END:
7498 case K_HOME:
7499# endif
7500 if (!(mod_mask & MOD_MASK_SHIFT))
7501 break;
7502 /* FALLTHROUGH */
7503 case K_S_LEFT:
7504 case K_S_RIGHT:
7505 case K_S_UP:
7506 case K_S_DOWN:
7507 case K_S_END:
7508 case K_S_HOME:
7509 /* Start selection right away, the cursor can move with
7510 * CTRL-O when beyond the end of the line. */
7511 start_selection();
7512
7513 /* Execute the key in (insert) Select mode. */
7514 stuffcharReadbuff(Ctrl_O);
7515 if (mod_mask)
7516 {
7517 char_u buf[4];
7518
7519 buf[0] = K_SPECIAL;
7520 buf[1] = KS_MODIFIER;
7521 buf[2] = mod_mask;
7522 buf[3] = NUL;
7523 stuffReadbuff(buf);
7524 }
7525 stuffcharReadbuff(c);
7526 return TRUE;
7527 }
7528 return FALSE;
7529}
7530#endif
7531
7532/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007533 * <Insert> key in Insert mode: toggle insert/remplace mode.
7534 */
7535 static void
7536ins_insert(replaceState)
7537 int replaceState;
7538{
7539#ifdef FEAT_FKMAP
7540 if (p_fkmap && p_ri)
7541 {
7542 beep_flush();
7543 EMSG(farsi_text_3); /* encoded in Farsi */
7544 return;
7545 }
7546#endif
7547
7548#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007549# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007550 set_vim_var_string(VV_INSERTMODE,
7551 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007552# ifdef FEAT_VREPLACE
7553 replaceState == VREPLACE ? "v" :
7554# endif
7555 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007556# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007557 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7558#endif
7559 if (State & REPLACE_FLAG)
7560 State = INSERT | (State & LANGMAP);
7561 else
7562 State = replaceState | (State & LANGMAP);
7563 AppendCharToRedobuff(K_INS);
7564 showmode();
7565#ifdef CURSOR_SHAPE
7566 ui_cursor_shape(); /* may show different cursor shape */
7567#endif
7568}
7569
7570/*
7571 * Pressed CTRL-O in Insert mode.
7572 */
7573 static void
7574ins_ctrl_o()
7575{
7576#ifdef FEAT_VREPLACE
7577 if (State & VREPLACE_FLAG)
7578 restart_edit = 'V';
7579 else
7580#endif
7581 if (State & REPLACE_FLAG)
7582 restart_edit = 'R';
7583 else
7584 restart_edit = 'I';
7585#ifdef FEAT_VIRTUALEDIT
7586 if (virtual_active())
7587 ins_at_eol = FALSE; /* cursor always keeps its column */
7588 else
7589#endif
7590 ins_at_eol = (gchar_cursor() == NUL);
7591}
7592
7593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 * If the cursor is on an indent, ^T/^D insert/delete one
7595 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7596 * Always round the indent to 'shiftwith', this is compatible
7597 * with vi. But vi only supports ^T and ^D after an
7598 * autoindent, we support it everywhere.
7599 */
7600 static void
7601ins_shift(c, lastc)
7602 int c;
7603 int lastc;
7604{
7605 if (stop_arrow() == FAIL)
7606 return;
7607 AppendCharToRedobuff(c);
7608
7609 /*
7610 * 0^D and ^^D: remove all indent.
7611 */
7612 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7613 {
7614 --curwin->w_cursor.col;
7615 (void)del_char(FALSE); /* delete the '^' or '0' */
7616 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7617 if (State & REPLACE_FLAG)
7618 replace_pop_ins();
7619 if (lastc == '^')
7620 old_indent = get_indent(); /* remember curr. indent */
7621 change_indent(INDENT_SET, 0, TRUE, 0);
7622 }
7623 else
7624 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7625
7626 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7627 did_ai = FALSE;
7628#ifdef FEAT_SMARTINDENT
7629 did_si = FALSE;
7630 can_si = FALSE;
7631 can_si_back = FALSE;
7632#endif
7633#ifdef FEAT_CINDENT
7634 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7635#endif
7636}
7637
7638 static void
7639ins_del()
7640{
7641 int temp;
7642
7643 if (stop_arrow() == FAIL)
7644 return;
7645 if (gchar_cursor() == NUL) /* delete newline */
7646 {
7647 temp = curwin->w_cursor.col;
7648 if (!can_bs(BS_EOL) /* only if "eol" included */
7649 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7650 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7651 || do_join(FALSE) == FAIL)
7652 vim_beep();
7653 else
7654 curwin->w_cursor.col = temp;
7655 }
7656 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7657 vim_beep();
7658 did_ai = FALSE;
7659#ifdef FEAT_SMARTINDENT
7660 did_si = FALSE;
7661 can_si = FALSE;
7662 can_si_back = FALSE;
7663#endif
7664 AppendCharToRedobuff(K_DEL);
7665}
7666
7667/*
7668 * Handle Backspace, delete-word and delete-line in Insert mode.
7669 * Return TRUE when backspace was actually used.
7670 */
7671 static int
7672ins_bs(c, mode, inserted_space_p)
7673 int c;
7674 int mode;
7675 int *inserted_space_p;
7676{
7677 linenr_T lnum;
7678 int cc;
7679 int temp = 0; /* init for GCC */
7680 colnr_T mincol;
7681 int did_backspace = FALSE;
7682 int in_indent;
7683 int oldState;
7684#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007685 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686#endif
7687
7688 /*
7689 * can't delete anything in an empty file
7690 * can't backup past first character in buffer
7691 * can't backup past starting point unless 'backspace' > 1
7692 * can backup to a previous line if 'backspace' == 0
7693 */
7694 if ( bufempty()
7695 || (
7696#ifdef FEAT_RIGHTLEFT
7697 !revins_on &&
7698#endif
7699 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7700 || (!can_bs(BS_START)
7701 && (arrow_used
7702 || (curwin->w_cursor.lnum == Insstart.lnum
7703 && curwin->w_cursor.col <= Insstart.col)))
7704 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7705 && curwin->w_cursor.col <= ai_col)
7706 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7707 {
7708 vim_beep();
7709 return FALSE;
7710 }
7711
7712 if (stop_arrow() == FAIL)
7713 return FALSE;
7714 in_indent = inindent(0);
7715#ifdef FEAT_CINDENT
7716 if (in_indent)
7717 can_cindent = FALSE;
7718#endif
7719#ifdef FEAT_COMMENTS
7720 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7721#endif
7722#ifdef FEAT_RIGHTLEFT
7723 if (revins_on) /* put cursor after last inserted char */
7724 inc_cursor();
7725#endif
7726
7727#ifdef FEAT_VIRTUALEDIT
7728 /* Virtualedit:
7729 * BACKSPACE_CHAR eats a virtual space
7730 * BACKSPACE_WORD eats all coladd
7731 * BACKSPACE_LINE eats all coladd and keeps going
7732 */
7733 if (curwin->w_cursor.coladd > 0)
7734 {
7735 if (mode == BACKSPACE_CHAR)
7736 {
7737 --curwin->w_cursor.coladd;
7738 return TRUE;
7739 }
7740 if (mode == BACKSPACE_WORD)
7741 {
7742 curwin->w_cursor.coladd = 0;
7743 return TRUE;
7744 }
7745 curwin->w_cursor.coladd = 0;
7746 }
7747#endif
7748
7749 /*
7750 * delete newline!
7751 */
7752 if (curwin->w_cursor.col == 0)
7753 {
7754 lnum = Insstart.lnum;
7755 if (curwin->w_cursor.lnum == Insstart.lnum
7756#ifdef FEAT_RIGHTLEFT
7757 || revins_on
7758#endif
7759 )
7760 {
7761 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7762 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7763 return FALSE;
7764 --Insstart.lnum;
7765 Insstart.col = MAXCOL;
7766 }
7767 /*
7768 * In replace mode:
7769 * cc < 0: NL was inserted, delete it
7770 * cc >= 0: NL was replaced, put original characters back
7771 */
7772 cc = -1;
7773 if (State & REPLACE_FLAG)
7774 cc = replace_pop(); /* returns -1 if NL was inserted */
7775 /*
7776 * In replace mode, in the line we started replacing, we only move the
7777 * cursor.
7778 */
7779 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7780 {
7781 dec_cursor();
7782 }
7783 else
7784 {
7785#ifdef FEAT_VREPLACE
7786 if (!(State & VREPLACE_FLAG)
7787 || curwin->w_cursor.lnum > orig_line_count)
7788#endif
7789 {
7790 temp = gchar_cursor(); /* remember current char */
7791 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007792
7793 /* When "aw" is in 'formatoptions' we must delete the space at
7794 * the end of the line, otherwise the line will be broken
7795 * again when auto-formatting. */
7796 if (has_format_option(FO_AUTO)
7797 && has_format_option(FO_WHITE_PAR))
7798 {
7799 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7800 TRUE);
7801 int len;
7802
7803 len = STRLEN(ptr);
7804 if (len > 0 && ptr[len - 1] == ' ')
7805 ptr[len - 1] = NUL;
7806 }
7807
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 (void)do_join(FALSE);
7809 if (temp == NUL && gchar_cursor() != NUL)
7810 inc_cursor();
7811 }
7812#ifdef FEAT_VREPLACE
7813 else
7814 dec_cursor();
7815#endif
7816
7817 /*
7818 * In REPLACE mode we have to put back the text that was replaced
7819 * by the NL. On the replace stack is first a NUL-terminated
7820 * sequence of characters that were deleted and then the
7821 * characters that NL replaced.
7822 */
7823 if (State & REPLACE_FLAG)
7824 {
7825 /*
7826 * Do the next ins_char() in NORMAL state, to
7827 * prevent ins_char() from replacing characters and
7828 * avoiding showmatch().
7829 */
7830 oldState = State;
7831 State = NORMAL;
7832 /*
7833 * restore characters (blanks) deleted after cursor
7834 */
7835 while (cc > 0)
7836 {
7837 temp = curwin->w_cursor.col;
7838#ifdef FEAT_MBYTE
7839 mb_replace_pop_ins(cc);
7840#else
7841 ins_char(cc);
7842#endif
7843 curwin->w_cursor.col = temp;
7844 cc = replace_pop();
7845 }
7846 /* restore the characters that NL replaced */
7847 replace_pop_ins();
7848 State = oldState;
7849 }
7850 }
7851 did_ai = FALSE;
7852 }
7853 else
7854 {
7855 /*
7856 * Delete character(s) before the cursor.
7857 */
7858#ifdef FEAT_RIGHTLEFT
7859 if (revins_on) /* put cursor on last inserted char */
7860 dec_cursor();
7861#endif
7862 mincol = 0;
7863 /* keep indent */
7864 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7865#ifdef FEAT_RIGHTLEFT
7866 && !revins_on
7867#endif
7868 )
7869 {
7870 temp = curwin->w_cursor.col;
7871 beginline(BL_WHITE);
7872 if (curwin->w_cursor.col < (colnr_T)temp)
7873 mincol = curwin->w_cursor.col;
7874 curwin->w_cursor.col = temp;
7875 }
7876
7877 /*
7878 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7879 */
7880 if ( mode == BACKSPACE_CHAR
7881 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007882 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 && (*(ml_get_cursor() - 1) == TAB
7884 || (*(ml_get_cursor() - 1) == ' '
7885 && (!*inserted_space_p
7886 || arrow_used))))))
7887 {
7888 int ts;
7889 colnr_T vcol;
7890 colnr_T want_vcol;
7891 int extra = 0;
7892
7893 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007894 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 ts = curbuf->b_p_sw;
7896 else
7897 ts = curbuf->b_p_sts;
7898 /* Compute the virtual column where we want to be. Since
7899 * 'showbreak' may get in the way, need to get the last column of
7900 * the previous character. */
7901 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7902 dec_cursor();
7903 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7904 inc_cursor();
7905 want_vcol = (want_vcol / ts) * ts;
7906
7907 /* delete characters until we are at or before want_vcol */
7908 while (vcol > want_vcol
7909 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7910 {
7911 dec_cursor();
7912 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7913 if (State & REPLACE_FLAG)
7914 {
7915 /* Don't delete characters before the insert point when in
7916 * Replace mode */
7917 if (curwin->w_cursor.lnum != Insstart.lnum
7918 || curwin->w_cursor.col >= Insstart.col)
7919 {
7920#if 0 /* what was this for? It causes problems when sw != ts. */
7921 if (State == REPLACE && (int)vcol < want_vcol)
7922 {
7923 (void)del_char(FALSE);
7924 extra = 2; /* don't pop too much */
7925 }
7926 else
7927#endif
7928 replace_do_bs();
7929 }
7930 }
7931 else
7932 (void)del_char(FALSE);
7933 }
7934
7935 /* insert extra spaces until we are at want_vcol */
7936 while (vcol < want_vcol)
7937 {
7938 /* Remember the first char we inserted */
7939 if (curwin->w_cursor.lnum == Insstart.lnum
7940 && curwin->w_cursor.col < Insstart.col)
7941 Insstart.col = curwin->w_cursor.col;
7942
7943#ifdef FEAT_VREPLACE
7944 if (State & VREPLACE_FLAG)
7945 ins_char(' ');
7946 else
7947#endif
7948 {
7949 ins_str((char_u *)" ");
7950 if ((State & REPLACE_FLAG) && extra <= 1)
7951 {
7952 if (extra)
7953 replace_push_off(NUL);
7954 else
7955 replace_push(NUL);
7956 }
7957 if (extra == 2)
7958 extra = 1;
7959 }
7960 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7961 }
7962 }
7963
7964 /*
7965 * Delete upto starting point, start of line or previous word.
7966 */
7967 else do
7968 {
7969#ifdef FEAT_RIGHTLEFT
7970 if (!revins_on) /* put cursor on char to be deleted */
7971#endif
7972 dec_cursor();
7973
7974 /* start of word? */
7975 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7976 {
7977 mode = BACKSPACE_WORD_NOT_SPACE;
7978 temp = vim_iswordc(gchar_cursor());
7979 }
7980 /* end of word? */
7981 else if (mode == BACKSPACE_WORD_NOT_SPACE
7982 && (vim_isspace(cc = gchar_cursor())
7983 || vim_iswordc(cc) != temp))
7984 {
7985#ifdef FEAT_RIGHTLEFT
7986 if (!revins_on)
7987#endif
7988 inc_cursor();
7989#ifdef FEAT_RIGHTLEFT
7990 else if (State & REPLACE_FLAG)
7991 dec_cursor();
7992#endif
7993 break;
7994 }
7995 if (State & REPLACE_FLAG)
7996 replace_do_bs();
7997 else
7998 {
7999#ifdef FEAT_MBYTE
8000 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008001 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002#endif
8003 (void)del_char(FALSE);
8004#ifdef FEAT_MBYTE
8005 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008006 * If there are combining characters and 'delcombine' is set
8007 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 * character.
8009 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008010 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 inc_cursor();
8012#endif
8013#ifdef FEAT_RIGHTLEFT
8014 if (revins_chars)
8015 {
8016 revins_chars--;
8017 revins_legal++;
8018 }
8019 if (revins_on && gchar_cursor() == NUL)
8020 break;
8021#endif
8022 }
8023 /* Just a single backspace?: */
8024 if (mode == BACKSPACE_CHAR)
8025 break;
8026 } while (
8027#ifdef FEAT_RIGHTLEFT
8028 revins_on ||
8029#endif
8030 (curwin->w_cursor.col > mincol
8031 && (curwin->w_cursor.lnum != Insstart.lnum
8032 || curwin->w_cursor.col != Insstart.col)));
8033 did_backspace = TRUE;
8034 }
8035#ifdef FEAT_SMARTINDENT
8036 did_si = FALSE;
8037 can_si = FALSE;
8038 can_si_back = FALSE;
8039#endif
8040 if (curwin->w_cursor.col <= 1)
8041 did_ai = FALSE;
8042 /*
8043 * It's a little strange to put backspaces into the redo
8044 * buffer, but it makes auto-indent a lot easier to deal
8045 * with.
8046 */
8047 AppendCharToRedobuff(c);
8048
8049 /* If deleted before the insertion point, adjust it */
8050 if (curwin->w_cursor.lnum == Insstart.lnum
8051 && curwin->w_cursor.col < Insstart.col)
8052 Insstart.col = curwin->w_cursor.col;
8053
8054 /* vi behaviour: the cursor moves backward but the character that
8055 * was there remains visible
8056 * Vim behaviour: the cursor moves backward and the character that
8057 * was there is erased from the screen.
8058 * We can emulate the vi behaviour by pretending there is a dollar
8059 * displayed even when there isn't.
8060 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8061 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8062 dollar_vcol = curwin->w_virtcol;
8063
8064 return did_backspace;
8065}
8066
8067#ifdef FEAT_MOUSE
8068 static void
8069ins_mouse(c)
8070 int c;
8071{
8072 pos_T tpos;
8073
8074# ifdef FEAT_GUI
8075 /* When GUI is active, also move/paste when 'mouse' is empty */
8076 if (!gui.in_use)
8077# endif
8078 if (!mouse_has(MOUSE_INSERT))
8079 return;
8080
8081 undisplay_dollar();
8082 tpos = curwin->w_cursor;
8083 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8084 {
8085 start_arrow(&tpos);
8086# ifdef FEAT_CINDENT
8087 can_cindent = TRUE;
8088# endif
8089 }
8090
8091#ifdef FEAT_WINDOWS
8092 /* redraw status lines (in case another window became active) */
8093 redraw_statuslines();
8094#endif
8095}
8096
8097 static void
8098ins_mousescroll(up)
8099 int up;
8100{
8101 pos_T tpos;
8102# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8103 win_T *old_curwin;
8104# endif
8105
8106 tpos = curwin->w_cursor;
8107
8108# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8109 old_curwin = curwin;
8110
8111 /* Currently the mouse coordinates are only known in the GUI. */
8112 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8113 {
8114 int row, col;
8115
8116 row = mouse_row;
8117 col = mouse_col;
8118
8119 /* find the window at the pointer coordinates */
8120 curwin = mouse_find_win(&row, &col);
8121 curbuf = curwin->w_buffer;
8122 }
8123 if (curwin == old_curwin)
8124# endif
8125 undisplay_dollar();
8126
8127 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8128 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8129 else
8130 scroll_redraw(up, 3L);
8131
8132# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8133 curwin->w_redr_status = TRUE;
8134
8135 curwin = old_curwin;
8136 curbuf = curwin->w_buffer;
8137# endif
8138
8139 if (!equalpos(curwin->w_cursor, tpos))
8140 {
8141 start_arrow(&tpos);
8142# ifdef FEAT_CINDENT
8143 can_cindent = TRUE;
8144# endif
8145 }
8146}
8147#endif
8148
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008149#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008150 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008151ins_tabline(c)
8152 int c;
8153{
8154 /* We will be leaving the current window, unless closing another tab. */
8155 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8156 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8157 {
8158 undisplay_dollar();
8159 start_arrow(&curwin->w_cursor);
8160# ifdef FEAT_CINDENT
8161 can_cindent = TRUE;
8162# endif
8163 }
8164
8165 if (c == K_TABLINE)
8166 goto_tabpage(current_tab);
8167 else
8168 handle_tabmenu();
8169
8170}
8171#endif
8172
8173#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 void
8175ins_scroll()
8176{
8177 pos_T tpos;
8178
8179 undisplay_dollar();
8180 tpos = curwin->w_cursor;
8181 if (gui_do_scroll())
8182 {
8183 start_arrow(&tpos);
8184# ifdef FEAT_CINDENT
8185 can_cindent = TRUE;
8186# endif
8187 }
8188}
8189
8190 void
8191ins_horscroll()
8192{
8193 pos_T tpos;
8194
8195 undisplay_dollar();
8196 tpos = curwin->w_cursor;
8197 if (gui_do_horiz_scroll())
8198 {
8199 start_arrow(&tpos);
8200# ifdef FEAT_CINDENT
8201 can_cindent = TRUE;
8202# endif
8203 }
8204}
8205#endif
8206
8207 static void
8208ins_left()
8209{
8210 pos_T tpos;
8211
8212#ifdef FEAT_FOLDING
8213 if ((fdo_flags & FDO_HOR) && KeyTyped)
8214 foldOpenCursor();
8215#endif
8216 undisplay_dollar();
8217 tpos = curwin->w_cursor;
8218 if (oneleft() == OK)
8219 {
8220 start_arrow(&tpos);
8221#ifdef FEAT_RIGHTLEFT
8222 /* If exit reversed string, position is fixed */
8223 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8224 revins_legal++;
8225 revins_chars++;
8226#endif
8227 }
8228
8229 /*
8230 * if 'whichwrap' set for cursor in insert mode may go to
8231 * previous line
8232 */
8233 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8234 {
8235 start_arrow(&tpos);
8236 --(curwin->w_cursor.lnum);
8237 coladvance((colnr_T)MAXCOL);
8238 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8239 }
8240 else
8241 vim_beep();
8242}
8243
8244 static void
8245ins_home(c)
8246 int c;
8247{
8248 pos_T tpos;
8249
8250#ifdef FEAT_FOLDING
8251 if ((fdo_flags & FDO_HOR) && KeyTyped)
8252 foldOpenCursor();
8253#endif
8254 undisplay_dollar();
8255 tpos = curwin->w_cursor;
8256 if (c == K_C_HOME)
8257 curwin->w_cursor.lnum = 1;
8258 curwin->w_cursor.col = 0;
8259#ifdef FEAT_VIRTUALEDIT
8260 curwin->w_cursor.coladd = 0;
8261#endif
8262 curwin->w_curswant = 0;
8263 start_arrow(&tpos);
8264}
8265
8266 static void
8267ins_end(c)
8268 int c;
8269{
8270 pos_T tpos;
8271
8272#ifdef FEAT_FOLDING
8273 if ((fdo_flags & FDO_HOR) && KeyTyped)
8274 foldOpenCursor();
8275#endif
8276 undisplay_dollar();
8277 tpos = curwin->w_cursor;
8278 if (c == K_C_END)
8279 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8280 coladvance((colnr_T)MAXCOL);
8281 curwin->w_curswant = MAXCOL;
8282
8283 start_arrow(&tpos);
8284}
8285
8286 static void
8287ins_s_left()
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 > 1 || curwin->w_cursor.col > 0)
8295 {
8296 start_arrow(&curwin->w_cursor);
8297 (void)bck_word(1L, FALSE, FALSE);
8298 curwin->w_set_curswant = TRUE;
8299 }
8300 else
8301 vim_beep();
8302}
8303
8304 static void
8305ins_right()
8306{
8307#ifdef FEAT_FOLDING
8308 if ((fdo_flags & FDO_HOR) && KeyTyped)
8309 foldOpenCursor();
8310#endif
8311 undisplay_dollar();
8312 if (gchar_cursor() != NUL || virtual_active()
8313 )
8314 {
8315 start_arrow(&curwin->w_cursor);
8316 curwin->w_set_curswant = TRUE;
8317#ifdef FEAT_VIRTUALEDIT
8318 if (virtual_active())
8319 oneright();
8320 else
8321#endif
8322 {
8323#ifdef FEAT_MBYTE
8324 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008325 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 else
8327#endif
8328 ++curwin->w_cursor.col;
8329 }
8330
8331#ifdef FEAT_RIGHTLEFT
8332 revins_legal++;
8333 if (revins_chars)
8334 revins_chars--;
8335#endif
8336 }
8337 /* if 'whichwrap' set for cursor in insert mode, may move the
8338 * cursor to the next line */
8339 else if (vim_strchr(p_ww, ']') != NULL
8340 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8341 {
8342 start_arrow(&curwin->w_cursor);
8343 curwin->w_set_curswant = TRUE;
8344 ++curwin->w_cursor.lnum;
8345 curwin->w_cursor.col = 0;
8346 }
8347 else
8348 vim_beep();
8349}
8350
8351 static void
8352ins_s_right()
8353{
8354#ifdef FEAT_FOLDING
8355 if ((fdo_flags & FDO_HOR) && KeyTyped)
8356 foldOpenCursor();
8357#endif
8358 undisplay_dollar();
8359 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8360 || gchar_cursor() != NUL)
8361 {
8362 start_arrow(&curwin->w_cursor);
8363 (void)fwd_word(1L, FALSE, 0);
8364 curwin->w_set_curswant = TRUE;
8365 }
8366 else
8367 vim_beep();
8368}
8369
8370 static void
8371ins_up(startcol)
8372 int startcol; /* when TRUE move to Insstart.col */
8373{
8374 pos_T tpos;
8375 linenr_T old_topline = curwin->w_topline;
8376#ifdef FEAT_DIFF
8377 int old_topfill = curwin->w_topfill;
8378#endif
8379
8380 undisplay_dollar();
8381 tpos = curwin->w_cursor;
8382 if (cursor_up(1L, TRUE) == OK)
8383 {
8384 if (startcol)
8385 coladvance(getvcol_nolist(&Insstart));
8386 if (old_topline != curwin->w_topline
8387#ifdef FEAT_DIFF
8388 || old_topfill != curwin->w_topfill
8389#endif
8390 )
8391 redraw_later(VALID);
8392 start_arrow(&tpos);
8393#ifdef FEAT_CINDENT
8394 can_cindent = TRUE;
8395#endif
8396 }
8397 else
8398 vim_beep();
8399}
8400
8401 static void
8402ins_pageup()
8403{
8404 pos_T tpos;
8405
8406 undisplay_dollar();
8407 tpos = curwin->w_cursor;
8408 if (onepage(BACKWARD, 1L) == OK)
8409 {
8410 start_arrow(&tpos);
8411#ifdef FEAT_CINDENT
8412 can_cindent = TRUE;
8413#endif
8414 }
8415 else
8416 vim_beep();
8417}
8418
8419 static void
8420ins_down(startcol)
8421 int startcol; /* when TRUE move to Insstart.col */
8422{
8423 pos_T tpos;
8424 linenr_T old_topline = curwin->w_topline;
8425#ifdef FEAT_DIFF
8426 int old_topfill = curwin->w_topfill;
8427#endif
8428
8429 undisplay_dollar();
8430 tpos = curwin->w_cursor;
8431 if (cursor_down(1L, TRUE) == OK)
8432 {
8433 if (startcol)
8434 coladvance(getvcol_nolist(&Insstart));
8435 if (old_topline != curwin->w_topline
8436#ifdef FEAT_DIFF
8437 || old_topfill != curwin->w_topfill
8438#endif
8439 )
8440 redraw_later(VALID);
8441 start_arrow(&tpos);
8442#ifdef FEAT_CINDENT
8443 can_cindent = TRUE;
8444#endif
8445 }
8446 else
8447 vim_beep();
8448}
8449
8450 static void
8451ins_pagedown()
8452{
8453 pos_T tpos;
8454
8455 undisplay_dollar();
8456 tpos = curwin->w_cursor;
8457 if (onepage(FORWARD, 1L) == OK)
8458 {
8459 start_arrow(&tpos);
8460#ifdef FEAT_CINDENT
8461 can_cindent = TRUE;
8462#endif
8463 }
8464 else
8465 vim_beep();
8466}
8467
8468#ifdef FEAT_DND
8469 static void
8470ins_drop()
8471{
8472 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8473}
8474#endif
8475
8476/*
8477 * Handle TAB in Insert or Replace mode.
8478 * Return TRUE when the TAB needs to be inserted like a normal character.
8479 */
8480 static int
8481ins_tab()
8482{
8483 int ind;
8484 int i;
8485 int temp;
8486
8487 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8488 Insstart_blank_vcol = get_nolist_virtcol();
8489 if (echeck_abbr(TAB + ABBR_OFF))
8490 return FALSE;
8491
8492 ind = inindent(0);
8493#ifdef FEAT_CINDENT
8494 if (ind)
8495 can_cindent = FALSE;
8496#endif
8497
8498 /*
8499 * When nothing special, insert TAB like a normal character
8500 */
8501 if (!curbuf->b_p_et
8502 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8503 && curbuf->b_p_sts == 0)
8504 return TRUE;
8505
8506 if (stop_arrow() == FAIL)
8507 return TRUE;
8508
8509 did_ai = FALSE;
8510#ifdef FEAT_SMARTINDENT
8511 did_si = FALSE;
8512 can_si = FALSE;
8513 can_si_back = FALSE;
8514#endif
8515 AppendToRedobuff((char_u *)"\t");
8516
8517 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8518 temp = (int)curbuf->b_p_sw;
8519 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8520 temp = (int)curbuf->b_p_sts;
8521 else /* otherwise use 'tabstop' */
8522 temp = (int)curbuf->b_p_ts;
8523 temp -= get_nolist_virtcol() % temp;
8524
8525 /*
8526 * Insert the first space with ins_char(). It will delete one char in
8527 * replace mode. Insert the rest with ins_str(); it will not delete any
8528 * chars. For VREPLACE mode, we use ins_char() for all characters.
8529 */
8530 ins_char(' ');
8531 while (--temp > 0)
8532 {
8533#ifdef FEAT_VREPLACE
8534 if (State & VREPLACE_FLAG)
8535 ins_char(' ');
8536 else
8537#endif
8538 {
8539 ins_str((char_u *)" ");
8540 if (State & REPLACE_FLAG) /* no char replaced */
8541 replace_push(NUL);
8542 }
8543 }
8544
8545 /*
8546 * When 'expandtab' not set: Replace spaces by TABs where possible.
8547 */
8548 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8549 {
8550 char_u *ptr;
8551#ifdef FEAT_VREPLACE
8552 char_u *saved_line = NULL; /* init for GCC */
8553 pos_T pos;
8554#endif
8555 pos_T fpos;
8556 pos_T *cursor;
8557 colnr_T want_vcol, vcol;
8558 int change_col = -1;
8559 int save_list = curwin->w_p_list;
8560
8561 /*
8562 * Get the current line. For VREPLACE mode, don't make real changes
8563 * yet, just work on a copy of the line.
8564 */
8565#ifdef FEAT_VREPLACE
8566 if (State & VREPLACE_FLAG)
8567 {
8568 pos = curwin->w_cursor;
8569 cursor = &pos;
8570 saved_line = vim_strsave(ml_get_curline());
8571 if (saved_line == NULL)
8572 return FALSE;
8573 ptr = saved_line + pos.col;
8574 }
8575 else
8576#endif
8577 {
8578 ptr = ml_get_cursor();
8579 cursor = &curwin->w_cursor;
8580 }
8581
8582 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8583 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8584 curwin->w_p_list = FALSE;
8585
8586 /* Find first white before the cursor */
8587 fpos = curwin->w_cursor;
8588 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8589 {
8590 --fpos.col;
8591 --ptr;
8592 }
8593
8594 /* In Replace mode, don't change characters before the insert point. */
8595 if ((State & REPLACE_FLAG)
8596 && fpos.lnum == Insstart.lnum
8597 && fpos.col < Insstart.col)
8598 {
8599 ptr += Insstart.col - fpos.col;
8600 fpos.col = Insstart.col;
8601 }
8602
8603 /* compute virtual column numbers of first white and cursor */
8604 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8605 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8606
8607 /* Use as many TABs as possible. Beware of 'showbreak' and
8608 * 'linebreak' adding extra virtual columns. */
8609 while (vim_iswhite(*ptr))
8610 {
8611 i = lbr_chartabsize((char_u *)"\t", vcol);
8612 if (vcol + i > want_vcol)
8613 break;
8614 if (*ptr != TAB)
8615 {
8616 *ptr = TAB;
8617 if (change_col < 0)
8618 {
8619 change_col = fpos.col; /* Column of first change */
8620 /* May have to adjust Insstart */
8621 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8622 Insstart.col = fpos.col;
8623 }
8624 }
8625 ++fpos.col;
8626 ++ptr;
8627 vcol += i;
8628 }
8629
8630 if (change_col >= 0)
8631 {
8632 int repl_off = 0;
8633
8634 /* Skip over the spaces we need. */
8635 while (vcol < want_vcol && *ptr == ' ')
8636 {
8637 vcol += lbr_chartabsize(ptr, vcol);
8638 ++ptr;
8639 ++repl_off;
8640 }
8641 if (vcol > want_vcol)
8642 {
8643 /* Must have a char with 'showbreak' just before it. */
8644 --ptr;
8645 --repl_off;
8646 }
8647 fpos.col += repl_off;
8648
8649 /* Delete following spaces. */
8650 i = cursor->col - fpos.col;
8651 if (i > 0)
8652 {
8653 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8654 /* correct replace stack. */
8655 if ((State & REPLACE_FLAG)
8656#ifdef FEAT_VREPLACE
8657 && !(State & VREPLACE_FLAG)
8658#endif
8659 )
8660 for (temp = i; --temp >= 0; )
8661 replace_join(repl_off);
8662 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008663#ifdef FEAT_NETBEANS_INTG
8664 if (usingNetbeans)
8665 {
8666 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8667 (long)(i + 1));
8668 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8669 (char_u *)"\t", 1);
8670 }
8671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 cursor->col -= i;
8673
8674#ifdef FEAT_VREPLACE
8675 /*
8676 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8677 * backspacing over the changed spacing and then inserting the new
8678 * spacing.
8679 */
8680 if (State & VREPLACE_FLAG)
8681 {
8682 /* Backspace from real cursor to change_col */
8683 backspace_until_column(change_col);
8684
8685 /* Insert each char in saved_line from changed_col to
8686 * ptr-cursor */
8687 ins_bytes_len(saved_line + change_col,
8688 cursor->col - change_col);
8689 }
8690#endif
8691 }
8692
8693#ifdef FEAT_VREPLACE
8694 if (State & VREPLACE_FLAG)
8695 vim_free(saved_line);
8696#endif
8697 curwin->w_p_list = save_list;
8698 }
8699
8700 return FALSE;
8701}
8702
8703/*
8704 * Handle CR or NL in insert mode.
8705 * Return TRUE when out of memory or can't undo.
8706 */
8707 static int
8708ins_eol(c)
8709 int c;
8710{
8711 int i;
8712
8713 if (echeck_abbr(c + ABBR_OFF))
8714 return FALSE;
8715 if (stop_arrow() == FAIL)
8716 return TRUE;
8717 undisplay_dollar();
8718
8719 /*
8720 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8721 * character under the cursor. Only push a NUL on the replace stack,
8722 * nothing to put back when the NL is deleted.
8723 */
8724 if ((State & REPLACE_FLAG)
8725#ifdef FEAT_VREPLACE
8726 && !(State & VREPLACE_FLAG)
8727#endif
8728 )
8729 replace_push(NUL);
8730
8731 /*
8732 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8733 * replacing the next line, so we push all of the characters left on the
8734 * line onto the replace stack. This is not done here though, it is done
8735 * in open_line().
8736 */
8737
8738#ifdef FEAT_RIGHTLEFT
8739# ifdef FEAT_FKMAP
8740 if (p_altkeymap && p_fkmap)
8741 fkmap(NL);
8742# endif
8743 /* NL in reverse insert will always start in the end of
8744 * current line. */
8745 if (revins_on)
8746 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8747#endif
8748
8749 AppendToRedobuff(NL_STR);
8750 i = open_line(FORWARD,
8751#ifdef FEAT_COMMENTS
8752 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8753#endif
8754 0, old_indent);
8755 old_indent = 0;
8756#ifdef FEAT_CINDENT
8757 can_cindent = TRUE;
8758#endif
8759
8760 return (!i);
8761}
8762
8763#ifdef FEAT_DIGRAPHS
8764/*
8765 * Handle digraph in insert mode.
8766 * Returns character still to be inserted, or NUL when nothing remaining to be
8767 * done.
8768 */
8769 static int
8770ins_digraph()
8771{
8772 int c;
8773 int cc;
8774
8775 pc_status = PC_STATUS_UNSET;
8776 if (redrawing() && !char_avail())
8777 {
8778 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008779 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780
8781 edit_putchar('?', TRUE);
8782#ifdef FEAT_CMDL_INFO
8783 add_to_showcmd_c(Ctrl_K);
8784#endif
8785 }
8786
8787#ifdef USE_ON_FLY_SCROLL
8788 dont_scroll = TRUE; /* disallow scrolling here */
8789#endif
8790
8791 /* don't map the digraph chars. This also prevents the
8792 * mode message to be deleted when ESC is hit */
8793 ++no_mapping;
8794 ++allow_keys;
8795 c = safe_vgetc();
8796 --no_mapping;
8797 --allow_keys;
8798 if (IS_SPECIAL(c) || mod_mask) /* special key */
8799 {
8800#ifdef FEAT_CMDL_INFO
8801 clear_showcmd();
8802#endif
8803 insert_special(c, TRUE, FALSE);
8804 return NUL;
8805 }
8806 if (c != ESC)
8807 {
8808 if (redrawing() && !char_avail())
8809 {
8810 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008811 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812
8813 if (char2cells(c) == 1)
8814 {
8815 /* first remove the '?', otherwise it's restored when typing
8816 * an ESC next */
8817 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008818 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 edit_putchar(c, TRUE);
8820 }
8821#ifdef FEAT_CMDL_INFO
8822 add_to_showcmd_c(c);
8823#endif
8824 }
8825 ++no_mapping;
8826 ++allow_keys;
8827 cc = safe_vgetc();
8828 --no_mapping;
8829 --allow_keys;
8830 if (cc != ESC)
8831 {
8832 AppendToRedobuff((char_u *)CTRL_V_STR);
8833 c = getdigraph(c, cc, TRUE);
8834#ifdef FEAT_CMDL_INFO
8835 clear_showcmd();
8836#endif
8837 return c;
8838 }
8839 }
8840 edit_unputchar();
8841#ifdef FEAT_CMDL_INFO
8842 clear_showcmd();
8843#endif
8844 return NUL;
8845}
8846#endif
8847
8848/*
8849 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8850 * Returns the char to be inserted, or NUL if none found.
8851 */
8852 static int
8853ins_copychar(lnum)
8854 linenr_T lnum;
8855{
8856 int c;
8857 int temp;
8858 char_u *ptr, *prev_ptr;
8859
8860 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8861 {
8862 vim_beep();
8863 return NUL;
8864 }
8865
8866 /* try to advance to the cursor column */
8867 temp = 0;
8868 ptr = ml_get(lnum);
8869 prev_ptr = ptr;
8870 validate_virtcol();
8871 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8872 {
8873 prev_ptr = ptr;
8874 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8875 }
8876 if ((colnr_T)temp > curwin->w_virtcol)
8877 ptr = prev_ptr;
8878
8879#ifdef FEAT_MBYTE
8880 c = (*mb_ptr2char)(ptr);
8881#else
8882 c = *ptr;
8883#endif
8884 if (c == NUL)
8885 vim_beep();
8886 return c;
8887}
8888
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008889/*
8890 * CTRL-Y or CTRL-E typed in Insert mode.
8891 */
8892 static int
8893ins_ctrl_ey(tc)
8894 int tc;
8895{
8896 int c = tc;
8897
8898#ifdef FEAT_INS_EXPAND
8899 if (ctrl_x_mode == CTRL_X_SCROLL)
8900 {
8901 if (c == Ctrl_Y)
8902 scrolldown_clamp();
8903 else
8904 scrollup_clamp();
8905 redraw_later(VALID);
8906 }
8907 else
8908#endif
8909 {
8910 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8911 if (c != NUL)
8912 {
8913 long tw_save;
8914
8915 /* The character must be taken literally, insert like it
8916 * was typed after a CTRL-V, and pretend 'textwidth'
8917 * wasn't set. Digits, 'o' and 'x' are special after a
8918 * CTRL-V, don't use it for these. */
8919 if (c < 256 && !isalnum(c))
8920 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8921 tw_save = curbuf->b_p_tw;
8922 curbuf->b_p_tw = -1;
8923 insert_special(c, TRUE, FALSE);
8924 curbuf->b_p_tw = tw_save;
8925#ifdef FEAT_RIGHTLEFT
8926 revins_chars++;
8927 revins_legal++;
8928#endif
8929 c = Ctrl_V; /* pretend CTRL-V is last character */
8930 auto_format(FALSE, TRUE);
8931 }
8932 }
8933 return c;
8934}
8935
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936#ifdef FEAT_SMARTINDENT
8937/*
8938 * Try to do some very smart auto-indenting.
8939 * Used when inserting a "normal" character.
8940 */
8941 static void
8942ins_try_si(c)
8943 int c;
8944{
8945 pos_T *pos, old_pos;
8946 char_u *ptr;
8947 int i;
8948 int temp;
8949
8950 /*
8951 * do some very smart indenting when entering '{' or '}'
8952 */
8953 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8954 {
8955 /*
8956 * for '}' set indent equal to indent of line containing matching '{'
8957 */
8958 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8959 {
8960 old_pos = curwin->w_cursor;
8961 /*
8962 * If the matching '{' has a ')' immediately before it (ignoring
8963 * white-space), then line up with the start of the line
8964 * containing the matching '(' if there is one. This handles the
8965 * case where an "if (..\n..) {" statement continues over multiple
8966 * lines -- webb
8967 */
8968 ptr = ml_get(pos->lnum);
8969 i = pos->col;
8970 if (i > 0) /* skip blanks before '{' */
8971 while (--i > 0 && vim_iswhite(ptr[i]))
8972 ;
8973 curwin->w_cursor.lnum = pos->lnum;
8974 curwin->w_cursor.col = i;
8975 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8976 curwin->w_cursor = *pos;
8977 i = get_indent();
8978 curwin->w_cursor = old_pos;
8979#ifdef FEAT_VREPLACE
8980 if (State & VREPLACE_FLAG)
8981 change_indent(INDENT_SET, i, FALSE, NUL);
8982 else
8983#endif
8984 (void)set_indent(i, SIN_CHANGED);
8985 }
8986 else if (curwin->w_cursor.col > 0)
8987 {
8988 /*
8989 * when inserting '{' after "O" reduce indent, but not
8990 * more than indent of previous line
8991 */
8992 temp = TRUE;
8993 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
8994 {
8995 old_pos = curwin->w_cursor;
8996 i = get_indent();
8997 while (curwin->w_cursor.lnum > 1)
8998 {
8999 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9000
9001 /* ignore empty lines and lines starting with '#'. */
9002 if (*ptr != '#' && *ptr != NUL)
9003 break;
9004 }
9005 if (get_indent() >= i)
9006 temp = FALSE;
9007 curwin->w_cursor = old_pos;
9008 }
9009 if (temp)
9010 shift_line(TRUE, FALSE, 1);
9011 }
9012 }
9013
9014 /*
9015 * set indent of '#' always to 0
9016 */
9017 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9018 {
9019 /* remember current indent for next line */
9020 old_indent = get_indent();
9021 (void)set_indent(0, SIN_CHANGED);
9022 }
9023
9024 /* Adjust ai_col, the char at this position can be deleted. */
9025 if (ai_col > curwin->w_cursor.col)
9026 ai_col = curwin->w_cursor.col;
9027}
9028#endif
9029
9030/*
9031 * Get the value that w_virtcol would have when 'list' is off.
9032 * Unless 'cpo' contains the 'L' flag.
9033 */
9034 static colnr_T
9035get_nolist_virtcol()
9036{
9037 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9038 return getvcol_nolist(&curwin->w_cursor);
9039 validate_virtcol();
9040 return curwin->w_virtcol;
9041}