blob: 60fd65256b4c1387784e86b36fa354ef918b277f [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 Moolenaar910f66f2006-04-05 20:41:53 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^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 Moolenaar910f66f2006-04-05 20:41:53 +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 Moolenaar39f05632006-03-19 22:15:26 +000072 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000073 char_u *cp_fname; /* file containing the match, allocated when
74 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
76 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077};
78
Bram Moolenaar572cb562005-08-05 21:35:02 +000079#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#define FREE_FNAME (2)
81
82/*
83 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000084 * "compl_first_match" points to the start of the list.
85 * "compl_curr_match" points to the currently selected entry.
86 * "compl_shown_match" is different from compl_curr_match during
87 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000089static compl_T *compl_first_match = NULL;
90static compl_T *compl_curr_match = NULL;
91static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
Bram Moolenaar779b74b2006-04-10 14:55:34 +000093/* After using a cursor key <Enter> selects a match in the popup menu,
94 * otherwise it inserts a line break. */
95static int compl_enter_selects = FALSE;
96
Bram Moolenaara6557602006-02-04 22:43:20 +000097/* When "compl_leader" is not NULL only matches that start with this string
98 * are used. */
99static char_u *compl_leader = NULL;
100
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000101static int compl_get_longest = FALSE; /* put longest common string
102 in compl_leader */
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104static int compl_used_match; /* Selected one of the matches. When
105 FALSE the match was edited or using
106 the longest common string. */
107
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000108static int compl_was_interrupted = FALSE; /* didn't finish finding
109 completions. */
110
111static int compl_restarting = FALSE; /* don't insert match */
112
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000113/* When the first completion is done "compl_started" is set. When it's
114 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000115static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000116
Bram Moolenaar572cb562005-08-05 21:35:02 +0000117static int compl_matches = 0;
118static char_u *compl_pattern = NULL;
119static int compl_direction = FORWARD;
120static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000121static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000122static pos_T compl_startpos;
123static colnr_T compl_col = 0; /* column where the text starts
124 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000125static char_u *compl_orig_text = NULL; /* text as it was before
126 * completion started */
127static int compl_cont_mode = 0;
128static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000130static void ins_ctrl_x __ARGS((void));
131static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000132static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000133static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000134static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000135static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000136static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000138static void ins_compl_upd_pum __ARGS((void));
139static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000140static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000141static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000142static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000143static 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 +0000144static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145static void ins_compl_free __ARGS((void));
146static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000147static int ins_compl_bs __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000148static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000149static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000150static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000151static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000152static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000153static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000155#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
156static void ins_compl_add_list __ARGS((list_T *list));
157#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000158static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159static void ins_compl_delete __ARGS((void));
160static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000161static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000162static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000163static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000164static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000165static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166static int ins_complete __ARGS((int c));
167static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
168#endif /* FEAT_INS_EXPAND */
169
170#define BACKSPACE_CHAR 1
171#define BACKSPACE_WORD 2
172#define BACKSPACE_WORD_NOT_SPACE 3
173#define BACKSPACE_LINE 4
174
Bram Moolenaar754b5602006-02-09 23:53:20 +0000175static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176static void ins_ctrl_v __ARGS((void));
177static void undisplay_dollar __ARGS((void));
178static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000179static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180static void check_auto_format __ARGS((int));
181static void redo_literal __ARGS((int c));
182static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000183#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000184static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000185static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000186static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
189static int echeck_abbr __ARGS((int));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000190#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191static void replace_push_off __ARGS((int c));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193static int replace_pop __ARGS((void));
194static void replace_join __ARGS((int off));
195static void replace_pop_ins __ARGS((void));
196#ifdef FEAT_MBYTE
197static void mb_replace_pop_ins __ARGS((int cc));
198#endif
199static void replace_flush __ARGS((void));
200static void replace_do_bs __ARGS((void));
201#ifdef FEAT_CINDENT
202static int cindent_on __ARGS((void));
203#endif
204static void ins_reg __ARGS((void));
205static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000206static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000207static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208#ifdef FEAT_RIGHTLEFT
209static void ins_ctrl_ __ARGS((void));
210#endif
211#ifdef FEAT_VISUAL
212static int ins_start_select __ARGS((int c));
213#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000214static void ins_insert __ARGS((int replaceState));
215static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216static void ins_shift __ARGS((int c, int lastc));
217static void ins_del __ARGS((void));
218static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
219#ifdef FEAT_MOUSE
220static void ins_mouse __ARGS((int c));
221static void ins_mousescroll __ARGS((int up));
222#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000223#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
224static void ins_tabline __ARGS((int c));
225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226static void ins_left __ARGS((void));
227static void ins_home __ARGS((int c));
228static void ins_end __ARGS((int c));
229static void ins_s_left __ARGS((void));
230static void ins_right __ARGS((void));
231static void ins_s_right __ARGS((void));
232static void ins_up __ARGS((int startcol));
233static void ins_pageup __ARGS((void));
234static void ins_down __ARGS((int startcol));
235static void ins_pagedown __ARGS((void));
236#ifdef FEAT_DND
237static void ins_drop __ARGS((void));
238#endif
239static int ins_tab __ARGS((void));
240static int ins_eol __ARGS((int c));
241#ifdef FEAT_DIGRAPHS
242static int ins_digraph __ARGS((void));
243#endif
244static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000245static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246#ifdef FEAT_SMARTINDENT
247static void ins_try_si __ARGS((int c));
248#endif
249static colnr_T get_nolist_virtcol __ARGS((void));
250
251static colnr_T Insstart_textlen; /* length of line when insert started */
252static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
253
254static char_u *last_insert = NULL; /* the text of the previous insert,
255 K_SPECIAL and CSI are escaped */
256static int last_insert_skip; /* nr of chars in front of previous insert */
257static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000258static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260#ifdef FEAT_CINDENT
261static int can_cindent; /* may do cindenting on this line */
262#endif
263
264static int old_indent = 0; /* for ^^D command in insert mode */
265
266#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000267static int revins_on; /* reverse insert mode on */
268static int revins_chars; /* how much to skip after edit */
269static int revins_legal; /* was the last char 'legal'? */
270static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271#endif
272
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273static int ins_need_undo; /* call u_save() before inserting a
274 char. Set when edit() is called.
275 after that arrow_used is used. */
276
277static int did_add_space = FALSE; /* auto_format() added an extra space
278 under the cursor */
279
280/*
281 * edit(): Start inserting text.
282 *
283 * "cmdchar" can be:
284 * 'i' normal insert command
285 * 'a' normal append command
286 * 'R' replace command
287 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
288 * but still only one <CR> is inserted. The <Esc> is not used for redo.
289 * 'g' "gI" command.
290 * 'V' "gR" command for Virtual Replace mode.
291 * 'v' "gr" command for single character Virtual Replace mode.
292 *
293 * This function is not called recursively. For CTRL-O commands, it returns
294 * and lets the caller handle the Normal-mode command.
295 *
296 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
297 */
298 int
299edit(cmdchar, startln, count)
300 int cmdchar;
301 int startln; /* if set, insert at start of line */
302 long count;
303{
304 int c = 0;
305 char_u *ptr;
306 int lastc;
307 colnr_T mincol;
308 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 int i;
310 int did_backspace = TRUE; /* previous char was backspace */
311#ifdef FEAT_CINDENT
312 int line_is_white = FALSE; /* line is empty before insert */
313#endif
314 linenr_T old_topline = 0; /* topline before insertion */
315#ifdef FEAT_DIFF
316 int old_topfill = -1;
317#endif
318 int inserted_space = FALSE; /* just inserted a space */
319 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000320 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000322 /* Remember whether editing was restarted after CTRL-O. */
323 did_restart_edit = restart_edit;
324
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 /* sleep before redrawing, needed for "CTRL-O :" that results in an
326 * error message */
327 check_for_delay(TRUE);
328
329#ifdef HAVE_SANDBOX
330 /* Don't allow inserting in the sandbox. */
331 if (sandbox != 0)
332 {
333 EMSG(_(e_sandbox));
334 return FALSE;
335 }
336#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000337 /* Don't allow changes in the buffer while editing the cmdline. The
338 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000339 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000340 {
341 EMSG(_(e_secure));
342 return FALSE;
343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344
345#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000346 /* Don't allow recursive insert mode when busy with completion. */
347 if (compl_started || pum_visible())
348 {
349 EMSG(_(e_secure));
350 return FALSE;
351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 ins_compl_clear(); /* clear stuff for CTRL-X mode */
353#endif
354
Bram Moolenaar843ee412004-06-30 16:16:41 +0000355#ifdef FEAT_AUTOCMD
356 /*
357 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
358 */
359 if (cmdchar != 'r' && cmdchar != 'v')
360 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000361# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000362 if (cmdchar == 'R')
363 ptr = (char_u *)"r";
364 else if (cmdchar == 'V')
365 ptr = (char_u *)"v";
366 else
367 ptr = (char_u *)"i";
368 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000369# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000370 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
371 }
372#endif
373
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374#ifdef FEAT_MOUSE
375 /*
376 * When doing a paste with the middle mouse button, Insstart is set to
377 * where the paste started.
378 */
379 if (where_paste_started.lnum != 0)
380 Insstart = where_paste_started;
381 else
382#endif
383 {
384 Insstart = curwin->w_cursor;
385 if (startln)
386 Insstart.col = 0;
387 }
388 Insstart_textlen = linetabsize(ml_get_curline());
389 Insstart_blank_vcol = MAXCOL;
390 if (!did_ai)
391 ai_col = 0;
392
393 if (cmdchar != NUL && restart_edit == 0)
394 {
395 ResetRedobuff();
396 AppendNumberToRedobuff(count);
397#ifdef FEAT_VREPLACE
398 if (cmdchar == 'V' || cmdchar == 'v')
399 {
400 /* "gR" or "gr" command */
401 AppendCharToRedobuff('g');
402 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
403 }
404 else
405#endif
406 {
407 AppendCharToRedobuff(cmdchar);
408 if (cmdchar == 'g') /* "gI" command */
409 AppendCharToRedobuff('I');
410 else if (cmdchar == 'r') /* "r<CR>" command */
411 count = 1; /* insert only one <CR> */
412 }
413 }
414
415 if (cmdchar == 'R')
416 {
417#ifdef FEAT_FKMAP
418 if (p_fkmap && p_ri)
419 {
420 beep_flush();
421 EMSG(farsi_text_3); /* encoded in Farsi */
422 State = INSERT;
423 }
424 else
425#endif
426 State = REPLACE;
427 }
428#ifdef FEAT_VREPLACE
429 else if (cmdchar == 'V' || cmdchar == 'v')
430 {
431 State = VREPLACE;
432 replaceState = VREPLACE;
433 orig_line_count = curbuf->b_ml.ml_line_count;
434 vr_lines_changed = 1;
435 }
436#endif
437 else
438 State = INSERT;
439
440 stop_insert_mode = FALSE;
441
442 /*
443 * Need to recompute the cursor position, it might move when the cursor is
444 * on a TAB or special character.
445 */
446 curs_columns(TRUE);
447
448 /*
449 * Enable langmap or IME, indicated by 'iminsert'.
450 * Note that IME may enabled/disabled without us noticing here, thus the
451 * 'iminsert' value may not reflect what is actually used. It is updated
452 * when hitting <Esc>.
453 */
454 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
455 State |= LANGMAP;
456#ifdef USE_IM_CONTROL
457 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
458#endif
459
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460#ifdef FEAT_MOUSE
461 setmouse();
462#endif
463#ifdef FEAT_CMDL_INFO
464 clear_showcmd();
465#endif
466#ifdef FEAT_RIGHTLEFT
467 /* there is no reverse replace mode */
468 revins_on = (State == INSERT && p_ri);
469 if (revins_on)
470 undisplay_dollar();
471 revins_chars = 0;
472 revins_legal = 0;
473 revins_scol = -1;
474#endif
475
476 /*
477 * Handle restarting Insert mode.
478 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
479 * restart_edit non-zero, and something in the stuff buffer.
480 */
481 if (restart_edit != 0 && stuff_empty())
482 {
483#ifdef FEAT_MOUSE
484 /*
485 * After a paste we consider text typed to be part of the insert for
486 * the pasted text. You can backspace over the pasted text too.
487 */
488 if (where_paste_started.lnum)
489 arrow_used = FALSE;
490 else
491#endif
492 arrow_used = TRUE;
493 restart_edit = 0;
494
495 /*
496 * If the cursor was after the end-of-line before the CTRL-O and it is
497 * now at the end-of-line, put it after the end-of-line (this is not
498 * correct in very rare cases).
499 * Also do this if curswant is greater than the current virtual
500 * column. Eg after "^O$" or "^O80|".
501 */
502 validate_virtcol();
503 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000504 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 || curwin->w_curswant > curwin->w_virtcol)
506 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
507 {
508 if (ptr[1] == NUL)
509 ++curwin->w_cursor.col;
510#ifdef FEAT_MBYTE
511 else if (has_mbyte)
512 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000513 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (ptr[i] == NUL)
515 curwin->w_cursor.col += i;
516 }
517#endif
518 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000519 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 }
521 else
522 arrow_used = FALSE;
523
524 /* we are in insert mode now, don't need to start it anymore */
525 need_start_insertmode = FALSE;
526
527 /* Need to save the line for undo before inserting the first char. */
528 ins_need_undo = TRUE;
529
530#ifdef FEAT_MOUSE
531 where_paste_started.lnum = 0;
532#endif
533#ifdef FEAT_CINDENT
534 can_cindent = TRUE;
535#endif
536#ifdef FEAT_FOLDING
537 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
538 * restarting. */
539 if (!p_im && did_restart_edit == 0)
540 foldOpenCursor();
541#endif
542
543 /*
544 * If 'showmode' is set, show the current (insert/replace/..) mode.
545 * A warning message for changing a readonly file is given here, before
546 * actually changing anything. It's put after the mode, if any.
547 */
548 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000549 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 i = showmode();
551
552 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000553 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
555#ifdef CURSOR_SHAPE
556 ui_cursor_shape(); /* may show different cursor shape */
557#endif
558#ifdef FEAT_DIGRAPHS
559 do_digraph(-1); /* clear digraphs */
560#endif
561
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000562 /*
563 * Get the current length of the redo buffer, those characters have to be
564 * skipped if we want to get to the inserted characters.
565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 ptr = get_inserted();
567 if (ptr == NULL)
568 new_insert_skip = 0;
569 else
570 {
571 new_insert_skip = (int)STRLEN(ptr);
572 vim_free(ptr);
573 }
574
575 old_indent = 0;
576
577 /*
578 * Main loop in Insert mode: repeat until Insert mode is left.
579 */
580 for (;;)
581 {
582#ifdef FEAT_RIGHTLEFT
583 if (!revins_legal)
584 revins_scol = -1; /* reset on illegal motions */
585 else
586 revins_legal = 0;
587#endif
588 if (arrow_used) /* don't repeat insert when arrow key used */
589 count = 0;
590
591 if (stop_insert_mode)
592 {
593 /* ":stopinsert" used or 'insertmode' reset */
594 count = 0;
595 goto doESCkey;
596 }
597
598 /* set curwin->w_curswant for next K_DOWN or K_UP */
599 if (!arrow_used)
600 curwin->w_set_curswant = TRUE;
601
602 /* If there is no typeahead may check for timestamps (e.g., for when a
603 * menu invoked a shell command). */
604 if (stuff_empty())
605 {
606 did_check_timestamps = FALSE;
607 if (need_check_timestamps)
608 check_timestamps(FALSE);
609 }
610
611 /*
612 * When emsg() was called msg_scroll will have been set.
613 */
614 msg_scroll = FALSE;
615
616#ifdef FEAT_GUI
617 /* When 'mousefocus' is set a mouse movement may have taken us to
618 * another window. "need_mouse_correct" may then be set because of an
619 * autocommand. */
620 if (need_mouse_correct)
621 gui_mouse_correct();
622#endif
623
624#ifdef FEAT_FOLDING
625 /* Open fold at the cursor line, according to 'foldopen'. */
626 if (fdo_flags & FDO_INSERT)
627 foldOpenCursor();
628 /* Close folds where the cursor isn't, according to 'foldclose' */
629 if (!char_avail())
630 foldCheckClose();
631#endif
632
633 /*
634 * If we inserted a character at the last position of the last line in
635 * the window, scroll the window one line up. This avoids an extra
636 * redraw.
637 * This is detected when the cursor column is smaller after inserting
638 * something.
639 * Don't do this when the topline changed already, it has
640 * already been adjusted (by insertchar() calling open_line())).
641 */
642 if (curbuf->b_mod_set
643 && curwin->w_p_wrap
644 && !did_backspace
645 && curwin->w_topline == old_topline
646#ifdef FEAT_DIFF
647 && curwin->w_topfill == old_topfill
648#endif
649 )
650 {
651 mincol = curwin->w_wcol;
652 validate_cursor_col();
653
654 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
655 && curwin->w_wrow == W_WINROW(curwin)
656 + curwin->w_height - 1 - p_so
657 && (curwin->w_cursor.lnum != curwin->w_topline
658#ifdef FEAT_DIFF
659 || curwin->w_topfill > 0
660#endif
661 ))
662 {
663#ifdef FEAT_DIFF
664 if (curwin->w_topfill > 0)
665 --curwin->w_topfill;
666 else
667#endif
668#ifdef FEAT_FOLDING
669 if (hasFolding(curwin->w_topline, NULL, &old_topline))
670 set_topline(curwin, old_topline + 1);
671 else
672#endif
673 set_topline(curwin, curwin->w_topline + 1);
674 }
675 }
676
677 /* May need to adjust w_topline to show the cursor. */
678 update_topline();
679
680 did_backspace = FALSE;
681
682 validate_cursor(); /* may set must_redraw */
683
684 /*
685 * Redraw the display when no characters are waiting.
686 * Also shows mode, ruler and positions cursor.
687 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000688 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689
690#ifdef FEAT_SCROLLBIND
691 if (curwin->w_p_scb)
692 do_check_scrollbind(TRUE);
693#endif
694
695 update_curswant();
696 old_topline = curwin->w_topline;
697#ifdef FEAT_DIFF
698 old_topfill = curwin->w_topfill;
699#endif
700
701#ifdef USE_ON_FLY_SCROLL
702 dont_scroll = FALSE; /* allow scrolling here */
703#endif
704
705 /*
706 * Get a character for Insert mode.
707 */
708 lastc = c; /* remember previous char for CTRL-D */
709 c = safe_vgetc();
710
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000711#ifdef FEAT_AUTOCMD
712 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
713 did_cursorhold = TRUE;
714#endif
715
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716#ifdef FEAT_RIGHTLEFT
717 if (p_hkmap && KeyTyped)
718 c = hkmap(c); /* Hebrew mode mapping */
719#endif
720#ifdef FEAT_FKMAP
721 if (p_fkmap && KeyTyped)
722 c = fkmap(c); /* Farsi mode mapping */
723#endif
724
725#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000726 /*
727 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000728 * and the cursor is still in the completed word. Only when there is
729 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000730 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000731 if (compl_started
732 && pum_wanted()
733 && curwin->w_cursor.col >= compl_col
734 && (compl_shown_match == NULL
735 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000736 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000737 /* BS: Delete one character from "compl_leader". */
738 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000739 && curwin->w_cursor.col > compl_col
740 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000741 continue;
742
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000743 /* When no match was selected or it was edited. */
744 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000745 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000746 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000747 * "compl_leader". Except when at the original match and
748 * there is nothing to add, CTRL-L works like CTRL-P then. */
749 if (c == Ctrl_L
750 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
751 || STRLEN(compl_shown_match->cp_str)
752 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000753 {
754 ins_compl_addfrommatch();
755 continue;
756 }
757
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000758 /* A non-white character that fits in with the current
759 * completion: Add to "compl_leader". */
760 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000761 {
762 ins_compl_addleader(c);
763 continue;
764 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000765
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000766 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000767 * compl_enter_selects is set the Enter key does the same. */
768 if (c == Ctrl_Y || (compl_enter_selects
769 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000770 {
771 ins_compl_delete();
772 ins_compl_insert();
773 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000774 }
775 }
776
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
778 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000779 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000780 if (c != K_IGNORE && ins_compl_prep(c))
781 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#endif
783
Bram Moolenaar488c6512005-08-11 20:09:58 +0000784 /* CTRL-\ CTRL-N goes to Normal mode,
785 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
786 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 if (c == Ctrl_BSL)
788 {
789 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000790 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 ++no_mapping;
792 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000793 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 --no_mapping;
795 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000796 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000798 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 vungetc(c);
800 c = Ctrl_BSL;
801 }
802 else if (c == Ctrl_G && p_im)
803 continue;
804 else
805 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000806 if (c == Ctrl_O)
807 {
808 ins_ctrl_o();
809 ins_at_eol = FALSE; /* cursor keeps its column */
810 nomove = TRUE;
811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 count = 0;
813 goto doESCkey;
814 }
815 }
816
817#ifdef FEAT_DIGRAPHS
818 c = do_digraph(c);
819#endif
820
821#ifdef FEAT_INS_EXPAND
822 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
823 goto docomplete;
824#endif
825 if (c == Ctrl_V || c == Ctrl_Q)
826 {
827 ins_ctrl_v();
828 c = Ctrl_V; /* pretend CTRL-V is last typed character */
829 continue;
830 }
831
832#ifdef FEAT_CINDENT
833 if (cindent_on()
834# ifdef FEAT_INS_EXPAND
835 && ctrl_x_mode == 0
836# endif
837 )
838 {
839 /* A key name preceded by a bang means this key is not to be
840 * inserted. Skip ahead to the re-indenting below.
841 * A key name preceded by a star means that indenting has to be
842 * done before inserting the key. */
843 line_is_white = inindent(0);
844 if (in_cinkeys(c, '!', line_is_white))
845 goto force_cindent;
846 if (can_cindent && in_cinkeys(c, '*', line_is_white)
847 && stop_arrow() == OK)
848 do_c_expr_indent();
849 }
850#endif
851
852#ifdef FEAT_RIGHTLEFT
853 if (curwin->w_p_rl)
854 switch (c)
855 {
856 case K_LEFT: c = K_RIGHT; break;
857 case K_S_LEFT: c = K_S_RIGHT; break;
858 case K_C_LEFT: c = K_C_RIGHT; break;
859 case K_RIGHT: c = K_LEFT; break;
860 case K_S_RIGHT: c = K_S_LEFT; break;
861 case K_C_RIGHT: c = K_C_LEFT; break;
862 }
863#endif
864
865#ifdef FEAT_VISUAL
866 /*
867 * If 'keymodel' contains "startsel", may start selection. If it
868 * does, a CTRL-O and c will be stuffed, we need to get these
869 * characters.
870 */
871 if (ins_start_select(c))
872 continue;
873#endif
874
875 /*
876 * The big switch to handle a character in insert mode.
877 */
878 switch (c)
879 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000880 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (echeck_abbr(ESC + ABBR_OFF))
882 break;
883 /*FALLTHROUGH*/
884
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000885 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886#ifdef FEAT_CMDWIN
887 if (c == Ctrl_C && cmdwin_type != 0)
888 {
889 /* Close the cmdline window. */
890 cmdwin_result = K_IGNORE;
891 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000892 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 goto doESCkey;
894 }
895#endif
896
897#ifdef UNIX
898do_intr:
899#endif
900 /* when 'insertmode' set, and not halfway a mapping, don't leave
901 * Insert mode */
902 if (goto_im())
903 {
904 if (got_int)
905 {
906 (void)vgetc(); /* flush all buffers */
907 got_int = FALSE;
908 }
909 else
910 vim_beep();
911 break;
912 }
913doESCkey:
914 /*
915 * This is the ONLY return from edit()!
916 */
917 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
918 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000919 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 o_lnum = curwin->w_cursor.lnum;
921
Bram Moolenaar488c6512005-08-11 20:09:58 +0000922 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000923 {
924#ifdef FEAT_AUTOCMD
925 if (cmdchar != 'r' && cmdchar != 'v')
926 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
927 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000928 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 continue;
933
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000934 case Ctrl_Z: /* suspend when 'insertmode' set */
935 if (!p_im)
936 goto normalchar; /* insert CTRL-Z as normal char */
937 stuffReadbuff((char_u *)":st\r");
938 c = Ctrl_O;
939 /*FALLTHROUGH*/
940
941 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000942#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000943 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000944 goto docomplete;
945#endif
946 if (echeck_abbr(Ctrl_O + ABBR_OFF))
947 break;
948 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000949
950#ifdef FEAT_VIRTUALEDIT
951 /* don't move the cursor left when 'virtualedit' has "onemore". */
952 if (ve_flags & VE_ONEMORE)
953 {
954 ins_at_eol = FALSE;
955 nomove = TRUE;
956 }
957#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000958 count = 0;
959 goto doESCkey;
960
Bram Moolenaar572cb562005-08-05 21:35:02 +0000961 case K_INS: /* toggle insert/replace mode */
962 case K_KINS:
963 ins_insert(replaceState);
964 break;
965
966 case K_SELECT: /* end of Select mode mapping - ignore */
967 break;
968
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000969#ifdef FEAT_SNIFF
970 case K_SNIFF: /* Sniff command received */
971 stuffcharReadbuff(K_SNIFF);
972 goto doESCkey;
973#endif
974
975 case K_HELP: /* Help key works like <ESC> <Help> */
976 case K_F1:
977 case K_XF1:
978 stuffcharReadbuff(K_HELP);
979 if (p_im)
980 need_start_insertmode = TRUE;
981 goto doESCkey;
982
983#ifdef FEAT_NETBEANS_INTG
984 case K_F21: /* NetBeans command */
985 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000986 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000987 --no_mapping;
988 netbeans_keycommand(i);
989 break;
990#endif
991
992 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 case NUL:
994 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000995 /* For ^@ the trailing ESC will end the insert, unless there is an
996 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
998 && c != Ctrl_A && !p_im)
999 goto doESCkey; /* quit insert mode */
1000 inserted_space = FALSE;
1001 break;
1002
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001003 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 ins_reg();
1005 auto_format(FALSE, TRUE);
1006 inserted_space = FALSE;
1007 break;
1008
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001009 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 ins_ctrl_g();
1011 break;
1012
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001013 case Ctrl_HAT: /* switch input mode and/or langmap */
1014 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 break;
1016
1017#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001018 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 if (!p_ari)
1020 goto normalchar;
1021 ins_ctrl_();
1022 break;
1023#endif
1024
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001025 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1027 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1028 goto docomplete;
1029#endif
1030 /* FALLTHROUGH */
1031
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001032 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033# ifdef FEAT_INS_EXPAND
1034 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1035 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001036 if (has_compl_option(FALSE))
1037 goto docomplete;
1038 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 }
1040# endif
1041 ins_shift(c, lastc);
1042 auto_format(FALSE, TRUE);
1043 inserted_space = FALSE;
1044 break;
1045
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001046 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 case K_KDEL:
1048 ins_del();
1049 auto_format(FALSE, TRUE);
1050 break;
1051
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001052 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 case Ctrl_H:
1054 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1055 auto_format(FALSE, TRUE);
1056 break;
1057
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001058 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1060 auto_format(FALSE, TRUE);
1061 break;
1062
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001063 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001064# ifdef FEAT_COMPL_FUNC
1065 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001066 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001067 goto docomplete;
1068# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1070 auto_format(FALSE, TRUE);
1071 inserted_space = FALSE;
1072 break;
1073
1074#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001075 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 case K_LEFTMOUSE_NM:
1077 case K_LEFTDRAG:
1078 case K_LEFTRELEASE:
1079 case K_LEFTRELEASE_NM:
1080 case K_MIDDLEMOUSE:
1081 case K_MIDDLEDRAG:
1082 case K_MIDDLERELEASE:
1083 case K_RIGHTMOUSE:
1084 case K_RIGHTDRAG:
1085 case K_RIGHTRELEASE:
1086 case K_X1MOUSE:
1087 case K_X1DRAG:
1088 case K_X1RELEASE:
1089 case K_X2MOUSE:
1090 case K_X2DRAG:
1091 case K_X2RELEASE:
1092 ins_mouse(c);
1093 break;
1094
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001095 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 ins_mousescroll(FALSE);
1097 break;
1098
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001099 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 ins_mousescroll(TRUE);
1101 break;
1102#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001103#ifdef FEAT_GUI_TABLINE
1104 case K_TABLINE:
1105 case K_TABMENU:
1106 ins_tabline(c);
1107 break;
1108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001110 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 break;
1112
Bram Moolenaar754b5602006-02-09 23:53:20 +00001113#ifdef FEAT_AUTOCMD
1114 case K_CURSORHOLD: /* Didn't type something for a while. */
1115 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1116 did_cursorhold = TRUE;
1117 break;
1118#endif
1119
Bram Moolenaar4770d092006-01-12 23:22:24 +00001120#ifdef FEAT_GUI_W32
1121 /* On Win32 ignore <M-F4>, we get it when closing the window was
1122 * cancelled. */
1123 case K_F4:
1124 if (mod_mask != MOD_MASK_ALT)
1125 goto normalchar;
1126 break;
1127#endif
1128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129#ifdef FEAT_GUI
1130 case K_VER_SCROLLBAR:
1131 ins_scroll();
1132 break;
1133
1134 case K_HOR_SCROLLBAR:
1135 ins_horscroll();
1136 break;
1137#endif
1138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001139 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 case K_S_HOME:
1142 case K_C_HOME:
1143 ins_home(c);
1144 break;
1145
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 case K_S_END:
1149 case K_C_END:
1150 ins_end(c);
1151 break;
1152
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001153 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001154 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1155 ins_s_left();
1156 else
1157 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 break;
1159
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001160 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 case K_C_LEFT:
1162 ins_s_left();
1163 break;
1164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001166 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1167 ins_s_right();
1168 else
1169 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 break;
1171
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001172 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 case K_C_RIGHT:
1174 ins_s_right();
1175 break;
1176
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001177 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001178#ifdef FEAT_INS_EXPAND
1179 if (pum_visible())
1180 goto docomplete;
1181#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001182 if (mod_mask & MOD_MASK_SHIFT)
1183 ins_pageup();
1184 else
1185 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 break;
1187
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001188 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 case K_PAGEUP:
1190 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001191#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001192 if (pum_visible())
1193 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 ins_pageup();
1196 break;
1197
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001198 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001199#ifdef FEAT_INS_EXPAND
1200 if (pum_visible())
1201 goto docomplete;
1202#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001203 if (mod_mask & MOD_MASK_SHIFT)
1204 ins_pagedown();
1205 else
1206 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 break;
1208
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001209 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 case K_PAGEDOWN:
1211 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001212#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001213 if (pum_visible())
1214 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 ins_pagedown();
1217 break;
1218
1219#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001220 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 ins_drop();
1222 break;
1223#endif
1224
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001225 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 c = TAB;
1227 /* FALLTHROUGH */
1228
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001229 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1231 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1232 goto docomplete;
1233#endif
1234 inserted_space = FALSE;
1235 if (ins_tab())
1236 goto normalchar; /* insert TAB as a normal char */
1237 auto_format(FALSE, TRUE);
1238 break;
1239
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001240 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 c = CAR;
1242 /* FALLTHROUGH */
1243 case CAR:
1244 case NL:
1245#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1246 /* In a quickfix window a <CR> jumps to the error under the
1247 * cursor. */
1248 if (bt_quickfix(curbuf) && c == CAR)
1249 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001250 if (curwin->w_llist_ref == NULL) /* quickfix window */
1251 do_cmdline_cmd((char_u *)".cc");
1252 else /* location list window */
1253 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 break;
1255 }
1256#endif
1257#ifdef FEAT_CMDWIN
1258 if (cmdwin_type != 0)
1259 {
1260 /* Execute the command in the cmdline window. */
1261 cmdwin_result = CAR;
1262 goto doESCkey;
1263 }
1264#endif
1265 if (ins_eol(c) && !p_im)
1266 goto doESCkey; /* out of memory */
1267 auto_format(FALSE, FALSE);
1268 inserted_space = FALSE;
1269 break;
1270
1271#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001272 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273# ifdef FEAT_INS_EXPAND
1274 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1275 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001276 if (has_compl_option(TRUE))
1277 goto docomplete;
1278 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 }
1280# endif
1281# ifdef FEAT_DIGRAPHS
1282 c = ins_digraph();
1283 if (c == NUL)
1284 break;
1285# endif
1286 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288
1289#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001290 case Ctrl_X: /* Enter CTRL-X mode */
1291 ins_ctrl_x();
1292 break;
1293
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001294 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 if (ctrl_x_mode != CTRL_X_TAGS)
1296 goto normalchar;
1297 goto docomplete;
1298
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001299 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (ctrl_x_mode != CTRL_X_FILES)
1301 goto normalchar;
1302 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001303
1304 case 's': /* Spelling completion after ^X */
1305 case Ctrl_S:
1306 if (ctrl_x_mode != CTRL_X_SPELL)
1307 goto normalchar;
1308 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309#endif
1310
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001311 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312#ifdef FEAT_INS_EXPAND
1313 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1314#endif
1315 {
1316 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1317 if (p_im)
1318 {
1319 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1320 break;
1321 goto doESCkey;
1322 }
1323 goto normalchar;
1324 }
1325#ifdef FEAT_INS_EXPAND
1326 /* FALLTHROUGH */
1327
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001328 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 case Ctrl_N:
1330 /* if 'complete' is empty then plain ^P is no longer special,
1331 * but it is under other ^X modes */
1332 if (*curbuf->b_p_cpt == NUL
1333 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001334 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 goto normalchar;
1336
1337docomplete:
1338 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001339 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 break;
1341#endif /* FEAT_INS_EXPAND */
1342
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001343 case Ctrl_Y: /* copy from previous line or scroll down */
1344 case Ctrl_E: /* copy from next line or scroll up */
1345 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 break;
1347
1348 default:
1349#ifdef UNIX
1350 if (c == intr_char) /* special interrupt char */
1351 goto do_intr;
1352#endif
1353
1354 /*
1355 * Insert a nomal character.
1356 */
1357normalchar:
1358#ifdef FEAT_SMARTINDENT
1359 /* Try to perform smart-indenting. */
1360 ins_try_si(c);
1361#endif
1362
1363 if (c == ' ')
1364 {
1365 inserted_space = TRUE;
1366#ifdef FEAT_CINDENT
1367 if (inindent(0))
1368 can_cindent = FALSE;
1369#endif
1370 if (Insstart_blank_vcol == MAXCOL
1371 && curwin->w_cursor.lnum == Insstart.lnum)
1372 Insstart_blank_vcol = get_nolist_virtcol();
1373 }
1374
1375 if (vim_iswordc(c) || !echeck_abbr(
1376#ifdef FEAT_MBYTE
1377 /* Add ABBR_OFF for characters above 0x100, this is
1378 * what check_abbr() expects. */
1379 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1380#endif
1381 c))
1382 {
1383 insert_special(c, FALSE, FALSE);
1384#ifdef FEAT_RIGHTLEFT
1385 revins_legal++;
1386 revins_chars++;
1387#endif
1388 }
1389
1390 auto_format(FALSE, TRUE);
1391
1392#ifdef FEAT_FOLDING
1393 /* When inserting a character the cursor line must never be in a
1394 * closed fold. */
1395 foldOpenCursor();
1396#endif
1397 break;
1398 } /* end of switch (c) */
1399
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001400#ifdef FEAT_AUTOCMD
1401 /* If typed something may trigger CursorHoldI again. */
1402 if (c != K_CURSORHOLD)
1403 did_cursorhold = FALSE;
1404#endif
1405
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 /* If the cursor was moved we didn't just insert a space */
1407 if (arrow_used)
1408 inserted_space = FALSE;
1409
1410#ifdef FEAT_CINDENT
1411 if (can_cindent && cindent_on()
1412# ifdef FEAT_INS_EXPAND
1413 && ctrl_x_mode == 0
1414# endif
1415 )
1416 {
1417force_cindent:
1418 /*
1419 * Indent now if a key was typed that is in 'cinkeys'.
1420 */
1421 if (in_cinkeys(c, ' ', line_is_white))
1422 {
1423 if (stop_arrow() == OK)
1424 /* re-indent the current line */
1425 do_c_expr_indent();
1426 }
1427 }
1428#endif /* FEAT_CINDENT */
1429
1430 } /* for (;;) */
1431 /* NOTREACHED */
1432}
1433
1434/*
1435 * Redraw for Insert mode.
1436 * This is postponed until getting the next character to make '$' in the 'cpo'
1437 * option work correctly.
1438 * Only redraw when there are no characters available. This speeds up
1439 * inserting sequences of characters (e.g., for CTRL-R).
1440 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001441/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001443ins_redraw(ready)
1444 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445{
1446 if (!char_avail())
1447 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001448#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001449 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1450 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001451 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001452 && !equalpos(last_cursormoved, curwin->w_cursor)
1453# ifdef FEAT_INS_EXPAND
1454 && !pum_visible()
1455# endif
1456 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001457 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001458# ifdef FEAT_SYN_HL
1459 /* Need to update the screen first, to make sure syntax
1460 * highlighting is correct after making a change (e.g., inserting
1461 * a "(". The autocommand may also require a redraw, so it's done
1462 * again below, unfortunately. */
1463 if (syntax_present(curbuf) && must_redraw)
1464 update_screen(0);
1465# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001466 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1467 last_cursormoved = curwin->w_cursor;
1468 }
1469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 if (must_redraw)
1471 update_screen(0);
1472 else if (clear_cmdline || redraw_cmdline)
1473 showmode(); /* clear cmdline and show mode */
1474 showruler(FALSE);
1475 setcursor();
1476 emsg_on_display = FALSE; /* may remove error message now */
1477 }
1478}
1479
1480/*
1481 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1482 */
1483 static void
1484ins_ctrl_v()
1485{
1486 int c;
1487
1488 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001489 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
1491 if (redrawing() && !char_avail())
1492 edit_putchar('^', TRUE);
1493 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1494
1495#ifdef FEAT_CMDL_INFO
1496 add_to_showcmd_c(Ctrl_V);
1497#endif
1498
1499 c = get_literal();
1500#ifdef FEAT_CMDL_INFO
1501 clear_showcmd();
1502#endif
1503 insert_special(c, FALSE, TRUE);
1504#ifdef FEAT_RIGHTLEFT
1505 revins_chars++;
1506 revins_legal++;
1507#endif
1508}
1509
1510/*
1511 * Put a character directly onto the screen. It's not stored in a buffer.
1512 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1513 */
1514static int pc_status;
1515#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1516#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1517#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1518#define PC_STATUS_SET 3 /* pc_bytes was filled */
1519#ifdef FEAT_MBYTE
1520static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1521#else
1522static char_u pc_bytes[2]; /* saved bytes */
1523#endif
1524static int pc_attr;
1525static int pc_row;
1526static int pc_col;
1527
1528 void
1529edit_putchar(c, highlight)
1530 int c;
1531 int highlight;
1532{
1533 int attr;
1534
1535 if (ScreenLines != NULL)
1536 {
1537 update_topline(); /* just in case w_topline isn't valid */
1538 validate_cursor();
1539 if (highlight)
1540 attr = hl_attr(HLF_8);
1541 else
1542 attr = 0;
1543 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1544 pc_col = W_WINCOL(curwin);
1545#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1546 pc_status = PC_STATUS_UNSET;
1547#endif
1548#ifdef FEAT_RIGHTLEFT
1549 if (curwin->w_p_rl)
1550 {
1551 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1552# ifdef FEAT_MBYTE
1553 if (has_mbyte)
1554 {
1555 int fix_col = mb_fix_col(pc_col, pc_row);
1556
1557 if (fix_col != pc_col)
1558 {
1559 screen_putchar(' ', pc_row, fix_col, attr);
1560 --curwin->w_wcol;
1561 pc_status = PC_STATUS_RIGHT;
1562 }
1563 }
1564# endif
1565 }
1566 else
1567#endif
1568 {
1569 pc_col += curwin->w_wcol;
1570#ifdef FEAT_MBYTE
1571 if (mb_lefthalve(pc_row, pc_col))
1572 pc_status = PC_STATUS_LEFT;
1573#endif
1574 }
1575
1576 /* save the character to be able to put it back */
1577#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1578 if (pc_status == PC_STATUS_UNSET)
1579#endif
1580 {
1581 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1582 pc_status = PC_STATUS_SET;
1583 }
1584 screen_putchar(c, pc_row, pc_col, attr);
1585 }
1586}
1587
1588/*
1589 * Undo the previous edit_putchar().
1590 */
1591 void
1592edit_unputchar()
1593{
1594 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1595 {
1596#if defined(FEAT_MBYTE)
1597 if (pc_status == PC_STATUS_RIGHT)
1598 ++curwin->w_wcol;
1599 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1600 redrawWinline(curwin->w_cursor.lnum, FALSE);
1601 else
1602#endif
1603 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1604 }
1605}
1606
1607/*
1608 * Called when p_dollar is set: display a '$' at the end of the changed text
1609 * Only works when cursor is in the line that changes.
1610 */
1611 void
1612display_dollar(col)
1613 colnr_T col;
1614{
1615 colnr_T save_col;
1616
1617 if (!redrawing())
1618 return;
1619
1620 cursor_off();
1621 save_col = curwin->w_cursor.col;
1622 curwin->w_cursor.col = col;
1623#ifdef FEAT_MBYTE
1624 if (has_mbyte)
1625 {
1626 char_u *p;
1627
1628 /* If on the last byte of a multi-byte move to the first byte. */
1629 p = ml_get_curline();
1630 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1631 }
1632#endif
1633 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1634 if (curwin->w_wcol < W_WIDTH(curwin))
1635 {
1636 edit_putchar('$', FALSE);
1637 dollar_vcol = curwin->w_virtcol;
1638 }
1639 curwin->w_cursor.col = save_col;
1640}
1641
1642/*
1643 * Call this function before moving the cursor from the normal insert position
1644 * in insert mode.
1645 */
1646 static void
1647undisplay_dollar()
1648{
1649 if (dollar_vcol)
1650 {
1651 dollar_vcol = 0;
1652 redrawWinline(curwin->w_cursor.lnum, FALSE);
1653 }
1654}
1655
1656/*
1657 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1658 * Keep the cursor on the same character.
1659 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1660 * type == INDENT_DEC decrease indent (for CTRL-D)
1661 * type == INDENT_SET set indent to "amount"
1662 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1663 */
1664 void
1665change_indent(type, amount, round, replaced)
1666 int type;
1667 int amount;
1668 int round;
1669 int replaced; /* replaced character, put on replace stack */
1670{
1671 int vcol;
1672 int last_vcol;
1673 int insstart_less; /* reduction for Insstart.col */
1674 int new_cursor_col;
1675 int i;
1676 char_u *ptr;
1677 int save_p_list;
1678 int start_col;
1679 colnr_T vc;
1680#ifdef FEAT_VREPLACE
1681 colnr_T orig_col = 0; /* init for GCC */
1682 char_u *new_line, *orig_line = NULL; /* init for GCC */
1683
1684 /* VREPLACE mode needs to know what the line was like before changing */
1685 if (State & VREPLACE_FLAG)
1686 {
1687 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1688 orig_col = curwin->w_cursor.col;
1689 }
1690#endif
1691
1692 /* for the following tricks we don't want list mode */
1693 save_p_list = curwin->w_p_list;
1694 curwin->w_p_list = FALSE;
1695 vc = getvcol_nolist(&curwin->w_cursor);
1696 vcol = vc;
1697
1698 /*
1699 * For Replace mode we need to fix the replace stack later, which is only
1700 * possible when the cursor is in the indent. Remember the number of
1701 * characters before the cursor if it's possible.
1702 */
1703 start_col = curwin->w_cursor.col;
1704
1705 /* determine offset from first non-blank */
1706 new_cursor_col = curwin->w_cursor.col;
1707 beginline(BL_WHITE);
1708 new_cursor_col -= curwin->w_cursor.col;
1709
1710 insstart_less = curwin->w_cursor.col;
1711
1712 /*
1713 * If the cursor is in the indent, compute how many screen columns the
1714 * cursor is to the left of the first non-blank.
1715 */
1716 if (new_cursor_col < 0)
1717 vcol = get_indent() - vcol;
1718
1719 if (new_cursor_col > 0) /* can't fix replace stack */
1720 start_col = -1;
1721
1722 /*
1723 * Set the new indent. The cursor will be put on the first non-blank.
1724 */
1725 if (type == INDENT_SET)
1726 (void)set_indent(amount, SIN_CHANGED);
1727 else
1728 {
1729#ifdef FEAT_VREPLACE
1730 int save_State = State;
1731
1732 /* Avoid being called recursively. */
1733 if (State & VREPLACE_FLAG)
1734 State = INSERT;
1735#endif
1736 shift_line(type == INDENT_DEC, round, 1);
1737#ifdef FEAT_VREPLACE
1738 State = save_State;
1739#endif
1740 }
1741 insstart_less -= curwin->w_cursor.col;
1742
1743 /*
1744 * Try to put cursor on same character.
1745 * If the cursor is at or after the first non-blank in the line,
1746 * compute the cursor column relative to the column of the first
1747 * non-blank character.
1748 * If we are not in insert mode, leave the cursor on the first non-blank.
1749 * If the cursor is before the first non-blank, position it relative
1750 * to the first non-blank, counted in screen columns.
1751 */
1752 if (new_cursor_col >= 0)
1753 {
1754 /*
1755 * When changing the indent while the cursor is touching it, reset
1756 * Insstart_col to 0.
1757 */
1758 if (new_cursor_col == 0)
1759 insstart_less = MAXCOL;
1760 new_cursor_col += curwin->w_cursor.col;
1761 }
1762 else if (!(State & INSERT))
1763 new_cursor_col = curwin->w_cursor.col;
1764 else
1765 {
1766 /*
1767 * Compute the screen column where the cursor should be.
1768 */
1769 vcol = get_indent() - vcol;
1770 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1771
1772 /*
1773 * Advance the cursor until we reach the right screen column.
1774 */
1775 vcol = last_vcol = 0;
1776 new_cursor_col = -1;
1777 ptr = ml_get_curline();
1778 while (vcol <= (int)curwin->w_virtcol)
1779 {
1780 last_vcol = vcol;
1781#ifdef FEAT_MBYTE
1782 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001783 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 else
1785#endif
1786 ++new_cursor_col;
1787 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1788 }
1789 vcol = last_vcol;
1790
1791 /*
1792 * May need to insert spaces to be able to position the cursor on
1793 * the right screen column.
1794 */
1795 if (vcol != (int)curwin->w_virtcol)
1796 {
1797 curwin->w_cursor.col = new_cursor_col;
1798 i = (int)curwin->w_virtcol - vcol;
1799 ptr = alloc(i + 1);
1800 if (ptr != NULL)
1801 {
1802 new_cursor_col += i;
1803 ptr[i] = NUL;
1804 while (--i >= 0)
1805 ptr[i] = ' ';
1806 ins_str(ptr);
1807 vim_free(ptr);
1808 }
1809 }
1810
1811 /*
1812 * When changing the indent while the cursor is in it, reset
1813 * Insstart_col to 0.
1814 */
1815 insstart_less = MAXCOL;
1816 }
1817
1818 curwin->w_p_list = save_p_list;
1819
1820 if (new_cursor_col <= 0)
1821 curwin->w_cursor.col = 0;
1822 else
1823 curwin->w_cursor.col = new_cursor_col;
1824 curwin->w_set_curswant = TRUE;
1825 changed_cline_bef_curs();
1826
1827 /*
1828 * May have to adjust the start of the insert.
1829 */
1830 if (State & INSERT)
1831 {
1832 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1833 {
1834 if ((int)Insstart.col <= insstart_less)
1835 Insstart.col = 0;
1836 else
1837 Insstart.col -= insstart_less;
1838 }
1839 if ((int)ai_col <= insstart_less)
1840 ai_col = 0;
1841 else
1842 ai_col -= insstart_less;
1843 }
1844
1845 /*
1846 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1847 * If the number of characters before the cursor decreased, need to pop a
1848 * few characters from the replace stack.
1849 * If the number of characters before the cursor increased, need to push a
1850 * few NULs onto the replace stack.
1851 */
1852 if (REPLACE_NORMAL(State) && start_col >= 0)
1853 {
1854 while (start_col > (int)curwin->w_cursor.col)
1855 {
1856 replace_join(0); /* remove a NUL from the replace stack */
1857 --start_col;
1858 }
1859 while (start_col < (int)curwin->w_cursor.col || replaced)
1860 {
1861 replace_push(NUL);
1862 if (replaced)
1863 {
1864 replace_push(replaced);
1865 replaced = NUL;
1866 }
1867 ++start_col;
1868 }
1869 }
1870
1871#ifdef FEAT_VREPLACE
1872 /*
1873 * For VREPLACE mode, we also have to fix the replace stack. In this case
1874 * it is always possible because we backspace over the whole line and then
1875 * put it back again the way we wanted it.
1876 */
1877 if (State & VREPLACE_FLAG)
1878 {
1879 /* If orig_line didn't allocate, just return. At least we did the job,
1880 * even if you can't backspace. */
1881 if (orig_line == NULL)
1882 return;
1883
1884 /* Save new line */
1885 new_line = vim_strsave(ml_get_curline());
1886 if (new_line == NULL)
1887 return;
1888
1889 /* We only put back the new line up to the cursor */
1890 new_line[curwin->w_cursor.col] = NUL;
1891
1892 /* Put back original line */
1893 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1894 curwin->w_cursor.col = orig_col;
1895
1896 /* Backspace from cursor to start of line */
1897 backspace_until_column(0);
1898
1899 /* Insert new stuff into line again */
1900 ins_bytes(new_line);
1901
1902 vim_free(new_line);
1903 }
1904#endif
1905}
1906
1907/*
1908 * Truncate the space at the end of a line. This is to be used only in an
1909 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1910 * modes.
1911 */
1912 void
1913truncate_spaces(line)
1914 char_u *line;
1915{
1916 int i;
1917
1918 /* find start of trailing white space */
1919 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1920 {
1921 if (State & REPLACE_FLAG)
1922 replace_join(0); /* remove a NUL from the replace stack */
1923 }
1924 line[i + 1] = NUL;
1925}
1926
1927#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1928 || defined(FEAT_COMMENTS) || defined(PROTO)
1929/*
1930 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1931 * modes correctly. May also be used when not in insert mode at all.
1932 */
1933 void
1934backspace_until_column(col)
1935 int col;
1936{
1937 while ((int)curwin->w_cursor.col > col)
1938 {
1939 curwin->w_cursor.col--;
1940 if (State & REPLACE_FLAG)
1941 replace_do_bs();
1942 else
1943 (void)del_char(FALSE);
1944 }
1945}
1946#endif
1947
1948#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1949/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001950 * CTRL-X pressed in Insert mode.
1951 */
1952 static void
1953ins_ctrl_x()
1954{
1955 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1956 * CTRL-V works like CTRL-N */
1957 if (ctrl_x_mode != CTRL_X_CMDLINE)
1958 {
1959 /* if the next ^X<> won't ADD nothing, then reset
1960 * compl_cont_status */
1961 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001962 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001963 else
1964 compl_cont_status = 0;
1965 /* We're not sure which CTRL-X mode it will be yet */
1966 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1967 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1968 edit_submode_pre = NULL;
1969 showmode();
1970 }
1971}
1972
1973/*
1974 * Return TRUE if the 'dict' or 'tsr' option can be used.
1975 */
1976 static int
1977has_compl_option(dict_opt)
1978 int dict_opt;
1979{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001980 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00001981# ifdef FEAT_SPELL
1982 && !curwin->w_p_spell
1983# endif
1984 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001985 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1986 {
1987 ctrl_x_mode = 0;
1988 edit_submode = NULL;
1989 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1990 : (char_u *)_("'thesaurus' option is empty"),
1991 hl_attr(HLF_E));
1992 if (emsg_silent == 0)
1993 {
1994 vim_beep();
1995 setcursor();
1996 out_flush();
1997 ui_delay(2000L, FALSE);
1998 }
1999 return FALSE;
2000 }
2001 return TRUE;
2002}
2003
2004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2006 * This depends on the current mode.
2007 */
2008 int
2009vim_is_ctrl_x_key(c)
2010 int c;
2011{
2012 /* Always allow ^R - let it's results then be checked */
2013 if (c == Ctrl_R)
2014 return TRUE;
2015
Bram Moolenaare3226be2005-12-18 22:10:00 +00002016 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002017 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002018 return TRUE;
2019
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 switch (ctrl_x_mode)
2021 {
2022 case 0: /* Not in any CTRL-X mode */
2023 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2024 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002025 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2027 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2028 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002029 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2030 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 case CTRL_X_SCROLL:
2032 return (c == Ctrl_Y || c == Ctrl_E);
2033 case CTRL_X_WHOLE_LINE:
2034 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2035 case CTRL_X_FILES:
2036 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2037 case CTRL_X_DICTIONARY:
2038 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2039 case CTRL_X_THESAURUS:
2040 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2041 case CTRL_X_TAGS:
2042 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2043#ifdef FEAT_FIND_ID
2044 case CTRL_X_PATH_PATTERNS:
2045 return (c == Ctrl_P || c == Ctrl_N);
2046 case CTRL_X_PATH_DEFINES:
2047 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2048#endif
2049 case CTRL_X_CMDLINE:
2050 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2051 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002052#ifdef FEAT_COMPL_FUNC
2053 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002054 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002055 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002056 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002057#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002058 case CTRL_X_SPELL:
2059 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 }
2061 EMSG(_(e_internal));
2062 return FALSE;
2063}
2064
2065/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002066 * Return TRUE when character "c" is part of the item currently being
2067 * completed. Used to decide whether to abandon complete mode when the menu
2068 * is visible.
2069 */
2070 static int
2071ins_compl_accept_char(c)
2072 int c;
2073{
2074 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2075 /* When expanding an identifier only accept identifier chars. */
2076 return vim_isIDc(c);
2077
2078 switch (ctrl_x_mode)
2079 {
2080 case CTRL_X_FILES:
2081 /* When expanding file name only accept file name chars. But not
2082 * path separators, so that "proto/<Tab>" expands files in
2083 * "proto", not "proto/" as a whole */
2084 return vim_isfilec(c) && !vim_ispathsep(c);
2085
2086 case CTRL_X_CMDLINE:
2087 case CTRL_X_OMNI:
2088 /* Command line and Omni completion can work with just about any
2089 * printable character, but do stop at white space. */
2090 return vim_isprintc(c) && !vim_iswhite(c);
2091
2092 case CTRL_X_WHOLE_LINE:
2093 /* For while line completion a space can be part of the line. */
2094 return vim_isprintc(c);
2095 }
2096 return vim_iswordc(c);
2097}
2098
2099/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002100 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002102 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 * the rest of the word to be in -- webb
2104 */
2105 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002106ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 char_u *str;
2108 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002109 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 char_u *fname;
2111 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002112 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002114 char_u *p;
2115 int i, c;
2116 int actual_len; /* Take multi-byte characters */
2117 int actual_compl_length; /* into account. */
2118 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 int has_lower = FALSE;
2120 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002122 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002124 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125
Bram Moolenaara2993e12007-08-12 14:38:46 +00002126 /* Find actual length of completion. */
2127#ifdef FEAT_MBYTE
2128 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002130 p = str;
2131 actual_len = 0;
2132 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002134 mb_ptr_adv(p);
2135 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 }
2137 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002138 else
2139#endif
2140 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
Bram Moolenaara2993e12007-08-12 14:38:46 +00002142 /* Find actual length of original text. */
2143#ifdef FEAT_MBYTE
2144 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002146 p = compl_orig_text;
2147 actual_compl_length = 0;
2148 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002150 mb_ptr_adv(p);
2151 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
2153 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002154 else
2155#endif
2156 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
Bram Moolenaara2993e12007-08-12 14:38:46 +00002158 /* Allocate wide character array for the completion and fill it. */
2159 wca = (int *)alloc(actual_len * sizeof(int));
2160 if (wca != NULL)
2161 {
2162 p = str;
2163 for (i = 0; i < actual_len; ++i)
2164#ifdef FEAT_MBYTE
2165 if (has_mbyte)
2166 wca[i] = mb_ptr2char_adv(&p);
2167 else
2168#endif
2169 wca[i] = *(p++);
2170
2171 /* Rule 1: Were any chars converted to lower? */
2172 p = compl_orig_text;
2173 for (i = 0; i < actual_compl_length; ++i)
2174 {
2175#ifdef FEAT_MBYTE
2176 if (has_mbyte)
2177 c = mb_ptr2char_adv(&p);
2178 else
2179#endif
2180 c = *(p++);
2181 if (MB_ISLOWER(c))
2182 {
2183 has_lower = TRUE;
2184 if (MB_ISUPPER(wca[i]))
2185 {
2186 /* Rule 1 is satisfied. */
2187 for (i = actual_compl_length; i < actual_len; ++i)
2188 wca[i] = MB_TOLOWER(wca[i]);
2189 break;
2190 }
2191 }
2192 }
2193
2194 /*
2195 * Rule 2: No lower case, 2nd consecutive letter converted to
2196 * upper case.
2197 */
2198 if (!has_lower)
2199 {
2200 p = compl_orig_text;
2201 for (i = 0; i < actual_compl_length; ++i)
2202 {
2203#ifdef FEAT_MBYTE
2204 if (has_mbyte)
2205 c = mb_ptr2char_adv(&p);
2206 else
2207#endif
2208 c = *(p++);
2209 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2210 {
2211 /* Rule 2 is satisfied. */
2212 for (i = actual_compl_length; i < actual_len; ++i)
2213 wca[i] = MB_TOUPPER(wca[i]);
2214 break;
2215 }
2216 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2217 }
2218 }
2219
2220 /* Copy the original case of the part we typed. */
2221 p = compl_orig_text;
2222 for (i = 0; i < actual_compl_length; ++i)
2223 {
2224#ifdef FEAT_MBYTE
2225 if (has_mbyte)
2226 c = mb_ptr2char_adv(&p);
2227 else
2228#endif
2229 c = *(p++);
2230 if (MB_ISLOWER(c))
2231 wca[i] = MB_TOLOWER(wca[i]);
2232 else if (MB_ISUPPER(c))
2233 wca[i] = MB_TOUPPER(wca[i]);
2234 }
2235
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002236 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002237 * Generate encoding specific output from wide character array.
2238 * Multi-byte characters can occupy up to five bytes more than
2239 * ASCII characters, and we also need one byte for NUL, so stay
2240 * six bytes away from the edge of IObuff.
2241 */
2242 p = IObuff;
2243 i = 0;
2244 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2245#ifdef FEAT_MBYTE
2246 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002247 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002248 else
2249#endif
2250 *(p++) = wca[i++];
2251 *p = NUL;
2252
2253 vim_free(wca);
2254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002256 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2257 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002259 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260}
2261
2262/*
2263 * Add a match to the list of matches.
2264 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002265 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002266 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002268 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002269ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 char_u *str;
2271 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002272 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002274 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002275 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002276 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002277 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002279 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002280 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281
2282 ui_breakcheck();
2283 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002284 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (len < 0)
2286 len = (int)STRLEN(str);
2287
2288 /*
2289 * If the same match is already present, don't add it.
2290 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002291 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002293 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 do
2295 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002296 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002297 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002298 && match->cp_str[len] == NUL)
2299 return NOTDONE;
2300 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002301 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002304 /* Remove any popup menu before changing the list of matches. */
2305 ins_compl_del_pum();
2306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 /*
2308 * Allocate a new match structure.
2309 * Copy the values to the new match structure.
2310 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002311 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002313 return FAIL;
2314 match->cp_number = -1;
2315 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002316 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002317 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
2319 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002320 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002322 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002325 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2327 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002328 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002329 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002330 && compl_curr_match->cp_fname != NULL
2331 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002332 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002333 else if (fname != NULL)
2334 {
2335 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002336 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002339 match->cp_fname = NULL;
2340 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002341
2342 if (cptext != NULL)
2343 {
2344 int i;
2345
2346 for (i = 0; i < CPT_COUNT; ++i)
2347 if (cptext[i] != NULL && *cptext[i] != NUL)
2348 match->cp_text[i] = vim_strsave(cptext[i]);
2349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
2351 /*
2352 * Link the new match structure in the list of matches.
2353 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002354 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002355 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 else if (dir == FORWARD)
2357 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002358 match->cp_next = compl_curr_match->cp_next;
2359 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 }
2361 else /* BACKWARD */
2362 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002363 match->cp_next = compl_curr_match;
2364 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002366 if (match->cp_next)
2367 match->cp_next->cp_prev = match;
2368 if (match->cp_prev)
2369 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002371 compl_first_match = match;
2372 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002374 /*
2375 * Find the longest common string if still doing that.
2376 */
2377 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2378 ins_compl_longest_match(match);
2379
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 return OK;
2381}
2382
2383/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002384 * Return TRUE if "str[len]" matches with match->cp_str, considering
2385 * match->cp_icase.
2386 */
2387 static int
2388ins_compl_equal(match, str, len)
2389 compl_T *match;
2390 char_u *str;
2391 int len;
2392{
2393 if (match->cp_icase)
2394 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2395 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2396}
2397
2398/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002399 * Reduce the longest common string for match "match".
2400 */
2401 static void
2402ins_compl_longest_match(match)
2403 compl_T *match;
2404{
2405 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002406 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002407 int had_match;
2408
2409 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002410 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002411 /* First match, use it as a whole. */
2412 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002413 if (compl_leader != NULL)
2414 {
2415 had_match = (curwin->w_cursor.col > compl_col);
2416 ins_compl_delete();
2417 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2418 ins_redraw(FALSE);
2419
2420 /* When the match isn't there (to avoid matching itself) remove it
2421 * again after redrawing. */
2422 if (!had_match)
2423 ins_compl_delete();
2424 compl_used_match = FALSE;
2425 }
2426 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002427 else
2428 {
2429 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002430 p = compl_leader;
2431 s = match->cp_str;
2432 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002433 {
2434#ifdef FEAT_MBYTE
2435 if (has_mbyte)
2436 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002437 c1 = mb_ptr2char(p);
2438 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002439 }
2440 else
2441#endif
2442 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002443 c1 = *p;
2444 c2 = *s;
2445 }
2446 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2447 : (c1 != c2))
2448 break;
2449#ifdef FEAT_MBYTE
2450 if (has_mbyte)
2451 {
2452 mb_ptr_adv(p);
2453 mb_ptr_adv(s);
2454 }
2455 else
2456#endif
2457 {
2458 ++p;
2459 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002460 }
2461 }
2462
2463 if (*p != NUL)
2464 {
2465 /* Leader was shortened, need to change the inserted text. */
2466 *p = NUL;
2467 had_match = (curwin->w_cursor.col > compl_col);
2468 ins_compl_delete();
2469 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2470 ins_redraw(FALSE);
2471
2472 /* When the match isn't there (to avoid matching itself) remove it
2473 * again after redrawing. */
2474 if (!had_match)
2475 ins_compl_delete();
2476 }
2477
2478 compl_used_match = FALSE;
2479 }
2480}
2481
2482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 * Add an array of matches to the list of matches.
2484 * Frees matches[].
2485 */
2486 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002487ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 int num_matches;
2489 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002490 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491{
2492 int i;
2493 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002494 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
Bram Moolenaar572cb562005-08-05 21:35:02 +00002496 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002497 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002498 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002500 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 FreeWild(num_matches, matches);
2502}
2503
2504/* Make the completion list cyclic.
2505 * Return the number of matches (excluding the original).
2506 */
2507 static int
2508ins_compl_make_cyclic()
2509{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002510 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 int count = 0;
2512
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002513 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
2515 /*
2516 * Find the end of the list.
2517 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002518 match = compl_first_match;
2519 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002520 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002522 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 ++count;
2524 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002525 match->cp_next = compl_first_match;
2526 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 }
2528 return count;
2529}
2530
Bram Moolenaara94bc432006-03-10 21:42:59 +00002531/*
2532 * Start completion for the complete() function.
2533 * "startcol" is where the matched text starts (1 is first column).
2534 * "list" is the list of matches.
2535 */
2536 void
2537set_completion(startcol, list)
2538 int startcol;
2539 list_T *list;
2540{
2541 /* If already doing completions stop it. */
2542 if (ctrl_x_mode != 0)
2543 ins_compl_prep(' ');
2544 ins_compl_clear();
2545
2546 if (stop_arrow() == FAIL)
2547 return;
2548
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002549 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002550 startcol = curwin->w_cursor.col;
2551 compl_col = startcol;
2552 compl_length = curwin->w_cursor.col - startcol;
2553 /* compl_pattern doesn't need to be set */
2554 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2555 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002556 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002557 return;
2558
2559 /* Handle like dictionary completion. */
2560 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2561
2562 ins_compl_add_list(list);
2563 compl_matches = ins_compl_make_cyclic();
2564 compl_started = TRUE;
2565 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002566 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002567
2568 compl_curr_match = compl_first_match;
2569 ins_complete(Ctrl_N);
2570 out_flush();
2571}
2572
2573
Bram Moolenaar9372a112005-12-06 19:59:18 +00002574/* "compl_match_array" points the currently displayed list of entries in the
2575 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002576static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002577static int compl_match_arraysize;
2578
2579/*
2580 * Update the screen and when there is any scrolling remove the popup menu.
2581 */
2582 static void
2583ins_compl_upd_pum()
2584{
2585 int h;
2586
2587 if (compl_match_array != NULL)
2588 {
2589 h = curwin->w_cline_height;
2590 update_screen(0);
2591 if (h != curwin->w_cline_height)
2592 ins_compl_del_pum();
2593 }
2594}
2595
2596/*
2597 * Remove any popup menu.
2598 */
2599 static void
2600ins_compl_del_pum()
2601{
2602 if (compl_match_array != NULL)
2603 {
2604 pum_undisplay();
2605 vim_free(compl_match_array);
2606 compl_match_array = NULL;
2607 }
2608}
2609
2610/*
2611 * Return TRUE if the popup menu should be displayed.
2612 */
2613 static int
2614pum_wanted()
2615{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002616 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002617 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002618 return FALSE;
2619
2620 /* The display looks bad on a B&W display. */
2621 if (t_colors < 8
2622#ifdef FEAT_GUI
2623 && !gui.in_use
2624#endif
2625 )
2626 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002627 return TRUE;
2628}
2629
2630/*
2631 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002632 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002633 */
2634 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002635pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002636{
2637 compl_T *compl;
2638 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002639
2640 /* Don't display the popup menu if there are no matches or there is only
2641 * one (ignoring the original text). */
2642 compl = compl_first_match;
2643 i = 0;
2644 do
2645 {
2646 if (compl == NULL
2647 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2648 break;
2649 compl = compl->cp_next;
2650 } while (compl != compl_first_match);
2651
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002652 if (strstr((char *)p_cot, "menuone") != NULL)
2653 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002654 return (i >= 2);
2655}
2656
2657/*
2658 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002659 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002660 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002661 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002662ins_compl_show_pum()
2663{
2664 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002665 compl_T *shown_compl = NULL;
2666 int did_find_shown_match = FALSE;
2667 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002668 int i;
2669 int cur = -1;
2670 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002671 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002672
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002673 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002674 return;
2675
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002676#if defined(FEAT_EVAL)
2677 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2678 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2679#endif
2680
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002681 /* Update the screen before drawing the popup menu over it. */
2682 update_screen(0);
2683
2684 if (compl_match_array == NULL)
2685 {
2686 /* Need to build the popup menu list. */
2687 compl_match_arraysize = 0;
2688 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002689 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002690 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002691 do
2692 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002693 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2694 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002695 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002696 ++compl_match_arraysize;
2697 compl = compl->cp_next;
2698 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002699 if (compl_match_arraysize == 0)
2700 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002701 compl_match_array = (pumitem_T *)alloc_clear(
2702 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002703 * compl_match_arraysize));
2704 if (compl_match_array != NULL)
2705 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002706 /* If the current match is the original text don't find the first
2707 * match after it, don't highlight anything. */
2708 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2709 shown_match_ok = TRUE;
2710
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002711 i = 0;
2712 compl = compl_first_match;
2713 do
2714 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002715 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2716 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002717 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002718 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002719 if (!shown_match_ok)
2720 {
2721 if (compl == compl_shown_match || did_find_shown_match)
2722 {
2723 /* This item is the shown match or this is the
2724 * first displayed item after the shown match. */
2725 compl_shown_match = compl;
2726 did_find_shown_match = TRUE;
2727 shown_match_ok = TRUE;
2728 }
2729 else
2730 /* Remember this displayed match for when the
2731 * shown match is just below it. */
2732 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002733 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002734 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002735
2736 if (compl->cp_text[CPT_ABBR] != NULL)
2737 compl_match_array[i].pum_text =
2738 compl->cp_text[CPT_ABBR];
2739 else
2740 compl_match_array[i].pum_text = compl->cp_str;
2741 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2742 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2743 if (compl->cp_text[CPT_MENU] != NULL)
2744 compl_match_array[i++].pum_extra =
2745 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002746 else
2747 compl_match_array[i++].pum_extra = compl->cp_fname;
2748 }
2749
2750 if (compl == compl_shown_match)
2751 {
2752 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002753
2754 /* When the original text is the shown match don't set
2755 * compl_shown_match. */
2756 if (compl->cp_flags & ORIGINAL_TEXT)
2757 shown_match_ok = TRUE;
2758
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002759 if (!shown_match_ok && shown_compl != NULL)
2760 {
2761 /* The shown match isn't displayed, set it to the
2762 * previously displayed match. */
2763 compl_shown_match = shown_compl;
2764 shown_match_ok = TRUE;
2765 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002766 }
2767 compl = compl->cp_next;
2768 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002769
2770 if (!shown_match_ok) /* no displayed match at all */
2771 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002772 }
2773 }
2774 else
2775 {
2776 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002777 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002778 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2779 || compl_match_array[i].pum_text
2780 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002781 {
2782 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002783 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002784 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002785 }
2786
2787 if (compl_match_array != NULL)
2788 {
2789 /* Compute the screen column of the start of the completed text.
2790 * Use the cursor to get all wrapping and other settings right. */
2791 col = curwin->w_cursor.col;
2792 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002793 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002794 curwin->w_cursor.col = col;
2795 }
2796}
2797
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798#define DICT_FIRST (1) /* use just first element in "dict" */
2799#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002800
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002802 * Add any identifiers that match the given pattern in the list of dictionary
2803 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 */
2805 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002806ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2807 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002809 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002810 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002812 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 char_u *ptr;
2814 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 char_u **files;
2817 int count;
2818 int i;
2819 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002820 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821
Bram Moolenaar0b238792006-03-02 22:49:12 +00002822 if (*dict == NUL)
2823 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002824#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002825 /* When 'dictionary' is empty and spell checking is enabled use
2826 * "spell". */
2827 if (!thesaurus && curwin->w_p_spell)
2828 dict = (char_u *)"spell";
2829 else
2830#endif
2831 return;
2832 }
2833
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002835 if (buf == NULL)
2836 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002837 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002838
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 /* If 'infercase' is set, don't use 'smartcase' here */
2840 save_p_scs = p_scs;
2841 if (curbuf->b_p_inf)
2842 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002843
2844 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2845 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002846 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002847 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2848 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002849 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
2850
2851 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00002852 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002853 i = (int)STRLEN(pat_esc) + 10;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002854 ptr = alloc(i);
2855 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002856 {
2857 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002858 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002859 }
2860 vim_snprintf((char *)ptr, i, "^\\s*\\zs\\V%s", pat_esc);
2861 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2862 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002863 vim_free(ptr);
2864 }
2865 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002866 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002867 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002868 if (regmatch.regprog == NULL)
2869 goto theend;
2870 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002871
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2873 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002874 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 {
2876 /* copy one dictionary file name into buf */
2877 if (flags == DICT_EXACT)
2878 {
2879 count = 1;
2880 files = &dict;
2881 }
2882 else
2883 {
2884 /* Expand wildcards in the dictionary name, but do not allow
2885 * backticks (for security, the 'dict' option may have been set in
2886 * a modeline). */
2887 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002888# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002889 if (!thesaurus && STRCMP(buf, "spell") == 0)
2890 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002891 else
2892# endif
2893 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 || expand_wildcards(1, &buf, &count, &files,
2895 EW_FILE|EW_SILENT) != OK)
2896 count = 0;
2897 }
2898
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002899# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002900 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002902 /* Complete from active spelling. Skip "\<" in the pattern, we
2903 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002904 if (pat[0] == '\\' && pat[1] == '<')
2905 ptr = pat + 2;
2906 else
2907 ptr = pat;
2908 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002910 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002911# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00002912 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002913 {
2914 ins_compl_files(count, files, thesaurus, flags,
2915 &regmatch, buf, &dir);
2916 if (flags != DICT_EXACT)
2917 FreeWild(count, files);
2918 }
2919 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 break;
2921 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002922
2923theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 p_scs = save_p_scs;
2925 vim_free(regmatch.regprog);
2926 vim_free(buf);
2927}
2928
Bram Moolenaar0b238792006-03-02 22:49:12 +00002929 static void
2930ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2931 int count;
2932 char_u **files;
2933 int thesaurus;
2934 int flags;
2935 regmatch_T *regmatch;
2936 char_u *buf;
2937 int *dir;
2938{
2939 char_u *ptr;
2940 int i;
2941 FILE *fp;
2942 int add_r;
2943
2944 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2945 {
2946 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2947 if (flags != DICT_EXACT)
2948 {
2949 vim_snprintf((char *)IObuff, IOSIZE,
2950 _("Scanning dictionary: %s"), (char *)files[i]);
2951 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2952 }
2953
2954 if (fp != NULL)
2955 {
2956 /*
2957 * Read dictionary file line by line.
2958 * Check each line for a match.
2959 */
2960 while (!got_int && !compl_interrupted
2961 && !vim_fgets(buf, LSIZE, fp))
2962 {
2963 ptr = buf;
2964 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2965 {
2966 ptr = regmatch->startp[0];
2967 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2968 ptr = find_line_end(ptr);
2969 else
2970 ptr = find_word_end(ptr);
2971 add_r = ins_compl_add_infercase(regmatch->startp[0],
2972 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002973 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002974 if (thesaurus)
2975 {
2976 char_u *wstart;
2977
2978 /*
2979 * Add the other matches on the line
2980 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00002981 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00002982 while (!got_int)
2983 {
2984 /* Find start of the next word. Skip white
2985 * space and punctuation. */
2986 ptr = find_word_start(ptr);
2987 if (*ptr == NUL || *ptr == NL)
2988 break;
2989 wstart = ptr;
2990
Bram Moolenaara2993e12007-08-12 14:38:46 +00002991 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002992#ifdef FEAT_MBYTE
2993 if (has_mbyte)
2994 /* Japanese words may have characters in
2995 * different classes, only separate words
2996 * with single-byte non-word characters. */
2997 while (*ptr != NUL)
2998 {
2999 int l = (*mb_ptr2len)(ptr);
3000
3001 if (l < 2 && !vim_iswordc(*ptr))
3002 break;
3003 ptr += l;
3004 }
3005 else
3006#endif
3007 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003008
3009 /* Add the word. Skip the regexp match. */
3010 if (wstart != regmatch->startp[0])
3011 add_r = ins_compl_add_infercase(wstart,
3012 (int)(ptr - wstart),
3013 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003014 }
3015 }
3016 if (add_r == OK)
3017 /* if dir was BACKWARD then honor it just once */
3018 *dir = FORWARD;
3019 else if (add_r == FAIL)
3020 break;
3021 /* avoid expensive call to vim_regexec() when at end
3022 * of line */
3023 if (*ptr == '\n' || got_int)
3024 break;
3025 }
3026 line_breakcheck();
3027 ins_compl_check_keys(50);
3028 }
3029 fclose(fp);
3030 }
3031 }
3032}
3033
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034/*
3035 * Find the start of the next word.
3036 * Returns a pointer to the first char of the word. Also stops at a NUL.
3037 */
3038 char_u *
3039find_word_start(ptr)
3040 char_u *ptr;
3041{
3042#ifdef FEAT_MBYTE
3043 if (has_mbyte)
3044 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003045 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 else
3047#endif
3048 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3049 ++ptr;
3050 return ptr;
3051}
3052
3053/*
3054 * Find the end of the word. Assumes it starts inside a word.
3055 * Returns a pointer to just after the word.
3056 */
3057 char_u *
3058find_word_end(ptr)
3059 char_u *ptr;
3060{
3061#ifdef FEAT_MBYTE
3062 int start_class;
3063
3064 if (has_mbyte)
3065 {
3066 start_class = mb_get_class(ptr);
3067 if (start_class > 1)
3068 while (*ptr != NUL)
3069 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003070 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 if (mb_get_class(ptr) != start_class)
3072 break;
3073 }
3074 }
3075 else
3076#endif
3077 while (vim_iswordc(*ptr))
3078 ++ptr;
3079 return ptr;
3080}
3081
3082/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003083 * Find the end of the line, omitting CR and NL at the end.
3084 * Returns a pointer to just after the line.
3085 */
3086 static char_u *
3087find_line_end(ptr)
3088 char_u *ptr;
3089{
3090 char_u *s;
3091
3092 s = ptr + STRLEN(ptr);
3093 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3094 --s;
3095 return s;
3096}
3097
3098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 * Free the list of completions
3100 */
3101 static void
3102ins_compl_free()
3103{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003104 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003105 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003107 vim_free(compl_pattern);
3108 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003109 vim_free(compl_leader);
3110 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003112 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003114
3115 ins_compl_del_pum();
3116 pum_clear();
3117
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003118 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 do
3120 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003121 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003122 compl_curr_match = compl_curr_match->cp_next;
3123 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003125 if (match->cp_flags & FREE_FNAME)
3126 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003127 for (i = 0; i < CPT_COUNT; ++i)
3128 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003130 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3131 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132}
3133
3134 static void
3135ins_compl_clear()
3136{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003137 compl_cont_status = 0;
3138 compl_started = FALSE;
3139 compl_matches = 0;
3140 vim_free(compl_pattern);
3141 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003142 vim_free(compl_leader);
3143 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003145 vim_free(compl_orig_text);
3146 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003147 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148}
3149
3150/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003151 * Return TRUE when Insert completion is active.
3152 */
3153 int
3154ins_compl_active()
3155{
3156 return compl_started;
3157}
3158
3159/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003160 * Delete one character before the cursor and show the subset of the matches
3161 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003162 * Returns the character to be used, NUL if the work is done and another char
3163 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003164 */
3165 static int
3166ins_compl_bs()
3167{
3168 char_u *line;
3169 char_u *p;
3170
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003171 line = ml_get_curline();
3172 p = line + curwin->w_cursor.col;
3173 mb_ptr_back(line, p);
3174
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003175 /* Stop completion when the whole word was deleted. For Omni completion
3176 * allow the word to be deleted, we won't match everything. */
3177 if ((int)(p - line) - (int)compl_col < 0
3178 || ((int)(p - line) - (int)compl_col == 0
3179 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003180 return K_BS;
3181
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003182 /* Deleted more than what was used to find matches or didn't finish
3183 * finding all matches: need to look for matches all over again. */
3184 if (curwin->w_cursor.col <= compl_col + compl_length
3185 || compl_was_interrupted)
3186 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003187
Bram Moolenaara6557602006-02-04 22:43:20 +00003188 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003189 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003190 if (compl_leader != NULL)
3191 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003192 ins_compl_new_leader();
3193 return NUL;
3194 }
3195 return K_BS;
3196}
Bram Moolenaara6557602006-02-04 22:43:20 +00003197
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003198/*
3199 * Called after changing "compl_leader".
3200 * Show the popup menu with a different set of matches.
3201 * May also search for matches again if the previous search was interrupted.
3202 */
3203 static void
3204ins_compl_new_leader()
3205{
3206 ins_compl_del_pum();
3207 ins_compl_delete();
3208 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3209 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003210
3211 if (compl_started)
3212 ins_compl_set_original_text(compl_leader);
3213 else
3214 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003215#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003216 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003217#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003218 /*
3219 * Matches were cleared, need to search for them now. First display
3220 * the changed text before the cursor. Set "compl_restarting" to
3221 * avoid that the first match is inserted.
3222 */
3223 update_screen(0);
3224#ifdef FEAT_GUI
3225 if (gui.in_use)
3226 {
3227 /* Show the cursor after the match, not after the redrawn text. */
3228 setcursor();
3229 out_flush();
3230 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003231 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003232#endif
3233 compl_restarting = TRUE;
3234 if (ins_complete(Ctrl_N) == FAIL)
3235 compl_cont_status = 0;
3236 compl_restarting = FALSE;
3237 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003238
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003239#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003240 if (!compl_used_match)
3241 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003242 /* Go to the original text, since none of the matches is inserted. */
3243 if (compl_first_match->cp_prev != NULL
3244 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3245 compl_shown_match = compl_first_match->cp_prev;
3246 else
3247 compl_shown_match = compl_first_match;
3248 compl_curr_match = compl_shown_match;
3249 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003250 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003251#endif
3252 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003253
3254 /* Show the popup menu with a different set of matches. */
3255 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003256
3257 /* Don't let Enter select the original text when there is no popup menu. */
3258 if (compl_match_array == NULL)
3259 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003260}
3261
3262/*
3263 * Append one character to the match leader. May reduce the number of
3264 * matches.
3265 */
3266 static void
3267ins_compl_addleader(c)
3268 int c;
3269{
3270#ifdef FEAT_MBYTE
3271 int cc;
3272
3273 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3274 {
3275 char_u buf[MB_MAXBYTES + 1];
3276
3277 (*mb_char2bytes)(c, buf);
3278 buf[cc] = NUL;
3279 ins_char_bytes(buf, cc);
3280 }
3281 else
3282#endif
3283 ins_char(c);
3284
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003285 /* If we didn't complete finding matches we must search again. */
3286 if (compl_was_interrupted)
3287 ins_compl_restart();
3288
Bram Moolenaara6557602006-02-04 22:43:20 +00003289 vim_free(compl_leader);
3290 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3291 curwin->w_cursor.col - compl_col);
3292 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003293 ins_compl_new_leader();
3294}
3295
3296/*
3297 * Setup for finding completions again without leaving CTRL-X mode. Used when
3298 * BS or a key was typed while still searching for matches.
3299 */
3300 static void
3301ins_compl_restart()
3302{
3303 ins_compl_free();
3304 compl_started = FALSE;
3305 compl_matches = 0;
3306 compl_cont_status = 0;
3307 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003308}
3309
3310/*
3311 * Set the first match, the original text.
3312 */
3313 static void
3314ins_compl_set_original_text(str)
3315 char_u *str;
3316{
3317 char_u *p;
3318
3319 /* Replace the original text entry. */
3320 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3321 {
3322 p = vim_strsave(str);
3323 if (p != NULL)
3324 {
3325 vim_free(compl_first_match->cp_str);
3326 compl_first_match->cp_str = p;
3327 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003328 }
3329}
3330
3331/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003332 * Append one character to the match leader. May reduce the number of
3333 * matches.
3334 */
3335 static void
3336ins_compl_addfrommatch()
3337{
3338 char_u *p;
3339 int len = curwin->w_cursor.col - compl_col;
3340 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003341 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003342
3343 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003344 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003345 {
3346 /* When still at the original match use the first entry that matches
3347 * the leader. */
3348 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3349 {
3350 p = NULL;
3351 for (cp = compl_shown_match->cp_next; cp != NULL
3352 && cp != compl_first_match; cp = cp->cp_next)
3353 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003354 if (compl_leader == NULL
3355 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003356 (int)STRLEN(compl_leader)))
3357 {
3358 p = cp->cp_str;
3359 break;
3360 }
3361 }
3362 if (p == NULL || (int)STRLEN(p) <= len)
3363 return;
3364 }
3365 else
3366 return;
3367 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003368 p += len;
3369#ifdef FEAT_MBYTE
3370 c = mb_ptr2char(p);
3371#else
3372 c = *p;
3373#endif
3374 ins_compl_addleader(c);
3375}
3376
3377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003379 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003380 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003382 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383ins_compl_prep(c)
3384 int c;
3385{
3386 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003388 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
3390 /* Forget any previous 'special' messages if this is actually
3391 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3392 */
3393 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3394 edit_submode_extra = NULL;
3395
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003396 /* Ignore end of Select mode mapping and mouse scroll buttons. */
3397 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003398 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003400 /* Set "compl_get_longest" when finding the first matches. */
3401 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3402 || (ctrl_x_mode == 0 && !compl_started))
3403 {
3404 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3405 compl_used_match = TRUE;
3406 }
3407
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3409 {
3410 /*
3411 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3412 * it will be yet. Now we decide.
3413 */
3414 switch (c)
3415 {
3416 case Ctrl_E:
3417 case Ctrl_Y:
3418 ctrl_x_mode = CTRL_X_SCROLL;
3419 if (!(State & REPLACE_FLAG))
3420 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3421 else
3422 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3423 edit_submode_pre = NULL;
3424 showmode();
3425 break;
3426 case Ctrl_L:
3427 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3428 break;
3429 case Ctrl_F:
3430 ctrl_x_mode = CTRL_X_FILES;
3431 break;
3432 case Ctrl_K:
3433 ctrl_x_mode = CTRL_X_DICTIONARY;
3434 break;
3435 case Ctrl_R:
3436 /* Simply allow ^R to happen without affecting ^X mode */
3437 break;
3438 case Ctrl_T:
3439 ctrl_x_mode = CTRL_X_THESAURUS;
3440 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003441#ifdef FEAT_COMPL_FUNC
3442 case Ctrl_U:
3443 ctrl_x_mode = CTRL_X_FUNCTION;
3444 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003445 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003446 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003447 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003448#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003449 case 's':
3450 case Ctrl_S:
3451 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003452#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003453 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003454 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003455 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003456#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003457 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 case Ctrl_RSB:
3459 ctrl_x_mode = CTRL_X_TAGS;
3460 break;
3461#ifdef FEAT_FIND_ID
3462 case Ctrl_I:
3463 case K_S_TAB:
3464 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3465 break;
3466 case Ctrl_D:
3467 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3468 break;
3469#endif
3470 case Ctrl_V:
3471 case Ctrl_Q:
3472 ctrl_x_mode = CTRL_X_CMDLINE;
3473 break;
3474 case Ctrl_P:
3475 case Ctrl_N:
3476 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3477 * just started ^X mode, or there were enough ^X's to cancel
3478 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3479 * do normal expansion when interrupting a different mode (say
3480 * ^X^F^X^P or ^P^X^X^P, see below)
3481 * nothing changes if interrupting mode 0, (eg, the flag
3482 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003483 if (!(compl_cont_status & CONT_INTRPT))
3484 compl_cont_status |= CONT_LOCAL;
3485 else if (compl_cont_mode != 0)
3486 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 /* FALLTHROUGH */
3488 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003489 /* If we have typed at least 2 ^X's... for modes != 0, we set
3490 * compl_cont_status = 0 (eg, as if we had just started ^X
3491 * mode).
3492 * For mode 0, we set "compl_cont_mode" to an impossible
3493 * value, in both cases ^X^X can be used to restart the same
3494 * mode (avoiding ADDING mode).
3495 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3496 * 'complete' and local ^P expansions respectively.
3497 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3498 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 if (c == Ctrl_X)
3500 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003501 if (compl_cont_mode != 0)
3502 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003504 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
3506 ctrl_x_mode = 0;
3507 edit_submode = NULL;
3508 showmode();
3509 break;
3510 }
3511 }
3512 else if (ctrl_x_mode != 0)
3513 {
3514 /* We're already in CTRL-X mode, do we stay in it? */
3515 if (!vim_is_ctrl_x_key(c))
3516 {
3517 if (ctrl_x_mode == CTRL_X_SCROLL)
3518 ctrl_x_mode = 0;
3519 else
3520 ctrl_x_mode = CTRL_X_FINISHED;
3521 edit_submode = NULL;
3522 }
3523 showmode();
3524 }
3525
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003526 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
3528 /* Show error message from attempted keyword completion (probably
3529 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003530 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003532 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3533 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 || ctrl_x_mode == CTRL_X_FINISHED)
3535 {
3536 /* Get here when we have finished typing a sequence of ^N and
3537 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003538 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003539 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003541 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003542 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003543
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003545 * If any of the original typed text has been changed, eg when
3546 * ignorecase is set, we must add back-spaces to the redo
3547 * buffer. We add as few as necessary to delete just the part
3548 * of the original text that has changed.
3549 * When using the longest match, edited the match or used
3550 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003552 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3553 ptr = compl_curr_match->cp_str;
3554 else if (compl_leader != NULL)
3555 ptr = compl_leader;
3556 else
3557 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003558 if (compl_orig_text != NULL)
3559 {
3560 p = compl_orig_text;
3561 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3562 ++temp)
3563 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003564#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003565 if (temp > 0)
3566 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003567#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003568 for (p += temp; *p != NUL; mb_ptr_adv(p))
3569 AppendCharToRedobuff(K_BS);
3570 }
3571 if (ptr != NULL)
3572 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
3574
3575#ifdef FEAT_CINDENT
3576 want_cindent = (can_cindent && cindent_on());
3577#endif
3578 /*
3579 * When completing whole lines: fix indent for 'cindent'.
3580 * Otherwise, break line if it's too long.
3581 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003582 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584#ifdef FEAT_CINDENT
3585 /* re-indent the current line */
3586 if (want_cindent)
3587 {
3588 do_c_expr_indent();
3589 want_cindent = FALSE; /* don't do it again */
3590 }
3591#endif
3592 }
3593 else
3594 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003595 int prev_col = curwin->w_cursor.col;
3596
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003598 if (prev_col > 0)
3599 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (stop_arrow() == OK)
3601 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003602 if (prev_col > 0
3603 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3604 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 }
3606
3607 auto_format(FALSE, TRUE);
3608
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003609 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003610 * the selection without inserting anything. When
3611 * compl_enter_selects is set the Enter key does the same. */
3612 if ((c == Ctrl_Y || (compl_enter_selects
3613 && (c == CAR || c == K_KENTER || c == NL)))
3614 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003615 retval = TRUE;
3616
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003617 /* CTRL-E means completion is Ended, go back to the typed text. */
3618 if (c == Ctrl_E)
3619 {
3620 ins_compl_delete();
3621 if (compl_leader != NULL)
3622 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3623 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003624 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003625 + curwin->w_cursor.col - compl_col);
3626 retval = TRUE;
3627 }
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003630 compl_started = FALSE;
3631 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 msg_clr_cmdline(); /* necessary for "noshowmode" */
3633 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003634 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 if (edit_submode != NULL)
3636 {
3637 edit_submode = NULL;
3638 showmode();
3639 }
3640
3641#ifdef FEAT_CINDENT
3642 /*
3643 * Indent now if a key was typed that is in 'cinkeys'.
3644 */
3645 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3646 do_c_expr_indent();
3647#endif
3648 }
3649 }
3650
3651 /* reset continue_* if we left expansion-mode, if we stay they'll be
3652 * (re)set properly in ins_complete() */
3653 if (!vim_is_ctrl_x_key(c))
3654 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003655 compl_cont_status = 0;
3656 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003658
3659 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660}
3661
3662/*
3663 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3664 * (depending on flag) starting from buf and looking for a non-scanned
3665 * buffer (other than curbuf). curbuf is special, if it is called with
3666 * buf=curbuf then it has to be the first call for a given flag/expansion.
3667 *
3668 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3669 */
3670 static buf_T *
3671ins_compl_next_buf(buf, flag)
3672 buf_T *buf;
3673 int flag;
3674{
3675#ifdef FEAT_WINDOWS
3676 static win_T *wp;
3677#endif
3678
3679 if (flag == 'w') /* just windows */
3680 {
3681#ifdef FEAT_WINDOWS
3682 if (buf == curbuf) /* first call for this flag/expansion */
3683 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003684 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 && wp->w_buffer->b_scanned)
3686 ;
3687 buf = wp->w_buffer;
3688#else
3689 buf = curbuf;
3690#endif
3691 }
3692 else
3693 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3694 * (unlisted buffers)
3695 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003696 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 && ((flag == 'U'
3698 ? buf->b_p_bl
3699 : (!buf->b_p_bl
3700 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003701 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 ;
3703 return buf;
3704}
3705
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003706#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003707static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003708
3709/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003710 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003711 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003712 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003713 static void
3714expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003715 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003716 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003717{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003718 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003719 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003720 char_u *funcname;
3721 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003722
Bram Moolenaare344bea2005-09-01 20:46:49 +00003723 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3724 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003725 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003726
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003727 /* Call 'completefunc' to obtain the list of matches. */
3728 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003729 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003730
Bram Moolenaare344bea2005-09-01 20:46:49 +00003731 pos = curwin->w_cursor;
3732 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3733 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003734 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003735 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003736
Bram Moolenaara94bc432006-03-10 21:42:59 +00003737 ins_compl_add_list(matchlist);
3738 list_unref(matchlist);
3739}
3740#endif /* FEAT_COMPL_FUNC */
3741
Bram Moolenaar39f05632006-03-19 22:15:26 +00003742#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003743/*
3744 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003745 */
3746 static void
3747ins_compl_add_list(list)
3748 list_T *list;
3749{
3750 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003751 int dir = compl_direction;
3752
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003753 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003754 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003755 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003756 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3757 /* if dir was BACKWARD then honor it just once */
3758 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003759 else if (did_emsg)
3760 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003761 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003762}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003763
3764/*
3765 * Add a match to the list of matches from a typeval_T.
3766 * If the given string is already in the list of completions, then return
3767 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3768 * maybe because alloc() returns NULL, then FAIL is returned.
3769 */
3770 int
3771ins_compl_add_tv(tv, dir)
3772 typval_T *tv;
3773 int dir;
3774{
3775 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003776 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003777 int adup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003778 char_u *(cptext[CPT_COUNT]);
3779
3780 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3781 {
3782 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3783 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3784 (char_u *)"abbr", FALSE);
3785 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3786 (char_u *)"menu", FALSE);
3787 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3788 (char_u *)"kind", FALSE);
3789 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3790 (char_u *)"info", FALSE);
3791 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3792 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003793 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003794 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003795 }
3796 else
3797 {
3798 word = get_tv_string_chk(tv);
3799 vim_memset(cptext, 0, sizeof(cptext));
3800 }
3801 if (word == NULL || *word == NUL)
3802 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003803 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003804}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003805#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003806
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003807/*
3808 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003809 * The search starts at position "ini" in curbuf and in the direction
3810 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003811 * When "compl_started" is FALSE start at that position, otherwise continue
3812 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003813 * This may return before finding all the matches.
3814 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 */
3816 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003817ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819{
3820 static pos_T first_match_pos;
3821 static pos_T last_match_pos;
3822 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003823 static int found_all = FALSE; /* Found all matches of a
3824 certain type. */
3825 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
Bram Moolenaar572cb562005-08-05 21:35:02 +00003827 pos_T *pos;
3828 char_u **matches;
3829 int save_p_scs;
3830 int save_p_ws;
3831 int save_p_ic;
3832 int i;
3833 int num_matches;
3834 int len;
3835 int found_new_match;
3836 int type = ctrl_x_mode;
3837 char_u *ptr;
3838 char_u *dict = NULL;
3839 int dict_f = 0;
3840 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003842 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 {
3844 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3845 ins_buf->b_scanned = 0;
3846 found_all = FALSE;
3847 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003848 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003849 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 last_match_pos = first_match_pos = *ini;
3851 }
3852
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003853 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003854 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3856 for (;;)
3857 {
3858 found_new_match = FAIL;
3859
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003860 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 * or if found_all says this entry is done. For ^X^L only use the
3862 * entries from 'complete' that look in loaded buffers. */
3863 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003864 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
3866 found_all = FALSE;
3867 while (*e_cpt == ',' || *e_cpt == ' ')
3868 e_cpt++;
3869 if (*e_cpt == '.' && !curbuf->b_scanned)
3870 {
3871 ins_buf = curbuf;
3872 first_match_pos = *ini;
3873 /* So that ^N can match word immediately after cursor */
3874 if (ctrl_x_mode == 0)
3875 dec(&first_match_pos);
3876 last_match_pos = first_match_pos;
3877 type = 0;
3878 }
3879 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3880 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3881 {
3882 /* Scan a buffer, but not the current one. */
3883 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3884 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003885 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 first_match_pos.col = last_match_pos.col = 0;
3887 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3888 last_match_pos.lnum = 0;
3889 type = 0;
3890 }
3891 else /* unloaded buffer, scan like dictionary */
3892 {
3893 found_all = TRUE;
3894 if (ins_buf->b_fname == NULL)
3895 continue;
3896 type = CTRL_X_DICTIONARY;
3897 dict = ins_buf->b_fname;
3898 dict_f = DICT_EXACT;
3899 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003900 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 ins_buf->b_fname == NULL
3902 ? buf_spname(ins_buf)
3903 : ins_buf->b_sfname == NULL
3904 ? (char *)ins_buf->b_fname
3905 : (char *)ins_buf->b_sfname);
3906 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3907 }
3908 else if (*e_cpt == NUL)
3909 break;
3910 else
3911 {
3912 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3913 type = -1;
3914 else if (*e_cpt == 'k' || *e_cpt == 's')
3915 {
3916 if (*e_cpt == 'k')
3917 type = CTRL_X_DICTIONARY;
3918 else
3919 type = CTRL_X_THESAURUS;
3920 if (*++e_cpt != ',' && *e_cpt != NUL)
3921 {
3922 dict = e_cpt;
3923 dict_f = DICT_FIRST;
3924 }
3925 }
3926#ifdef FEAT_FIND_ID
3927 else if (*e_cpt == 'i')
3928 type = CTRL_X_PATH_PATTERNS;
3929 else if (*e_cpt == 'd')
3930 type = CTRL_X_PATH_DEFINES;
3931#endif
3932 else if (*e_cpt == ']' || *e_cpt == 't')
3933 {
3934 type = CTRL_X_TAGS;
3935 sprintf((char*)IObuff, _("Scanning tags."));
3936 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3937 }
3938 else
3939 type = -1;
3940
3941 /* in any case e_cpt is advanced to the next entry */
3942 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3943
3944 found_all = TRUE;
3945 if (type == -1)
3946 continue;
3947 }
3948 }
3949
3950 switch (type)
3951 {
3952 case -1:
3953 break;
3954#ifdef FEAT_FIND_ID
3955 case CTRL_X_PATH_PATTERNS:
3956 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003957 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003958 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003960 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3962 (linenr_T)1, (linenr_T)MAXLNUM);
3963 break;
3964#endif
3965
3966 case CTRL_X_DICTIONARY:
3967 case CTRL_X_THESAURUS:
3968 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003969 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 : (type == CTRL_X_THESAURUS
3971 ? (*curbuf->b_p_tsr == NUL
3972 ? p_tsr
3973 : curbuf->b_p_tsr)
3974 : (*curbuf->b_p_dict == NUL
3975 ? p_dict
3976 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003977 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003978 dict != NULL ? dict_f
3979 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 dict = NULL;
3981 break;
3982
3983 case CTRL_X_TAGS:
3984 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3985 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003986 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987
3988 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003989 * of matches is found when compl_pattern is empty */
3990 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3992 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3993 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3994 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003995 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
3997 p_ic = save_p_ic;
3998 break;
3999
4000 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004001 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4003 {
4004
4005 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004006 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004007 ins_compl_add_matches(num_matches, matches,
4008#ifdef CASE_INSENSITIVE_FILENAME
4009 TRUE
4010#else
4011 FALSE
4012#endif
4013 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
4015 break;
4016
4017 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004018 if (expand_cmdline(&compl_xp, compl_pattern,
4019 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004021 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 break;
4023
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004024#ifdef FEAT_COMPL_FUNC
4025 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004026 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004027 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004028 break;
4029#endif
4030
Bram Moolenaar488c6512005-08-11 20:09:58 +00004031 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004032#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004033 num_matches = expand_spelling(first_match_pos.lnum,
4034 first_match_pos.col, compl_pattern, &matches);
4035 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004036 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004037#endif
4038 break;
4039
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 default: /* normal ^P/^N and ^X^L */
4041 /*
4042 * If 'infercase' is set, don't use 'smartcase' here
4043 */
4044 save_p_scs = p_scs;
4045 if (ins_buf->b_p_inf)
4046 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004047
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 /* buffers other than curbuf are scanned from the beginning or the
4049 * end but never from the middle, thus setting nowrapscan in this
4050 * buffers is a good idea, on the other hand, we always set
4051 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4052 save_p_ws = p_ws;
4053 if (ins_buf != curbuf)
4054 p_ws = FALSE;
4055 else if (*e_cpt == '.')
4056 p_ws = TRUE;
4057 for (;;)
4058 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004059 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004061 ++msg_silent; /* Don't want messages for wrapscan. */
4062
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004063 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4064 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004066 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004068 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004070 found_new_match = searchit(NULL, ins_buf, pos,
4071 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004072 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004073 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004074 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004075 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004077 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004078 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 first_match_pos = *pos;
4080 last_match_pos = *pos;
4081 }
4082 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004083 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 found_new_match = FAIL;
4085 if (found_new_match == FAIL)
4086 {
4087 if (ins_buf == curbuf)
4088 found_all = TRUE;
4089 break;
4090 }
4091
4092 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004093 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 && ini->lnum == pos->lnum
4095 && ini->col == pos->col)
4096 continue;
4097 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4098 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4099 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004100 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
4102 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4103 continue;
4104 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4105 if (!p_paste)
4106 ptr = skipwhite(ptr);
4107 }
4108 len = (int)STRLEN(ptr);
4109 }
4110 else
4111 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004112 char_u *tmp_ptr = ptr;
4113
4114 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004116 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 /* Skip if already inside a word. */
4118 if (vim_iswordp(tmp_ptr))
4119 continue;
4120 /* Find start of next word. */
4121 tmp_ptr = find_word_start(tmp_ptr);
4122 }
4123 /* Find end of this word. */
4124 tmp_ptr = find_word_end(tmp_ptr);
4125 len = (int)(tmp_ptr - ptr);
4126
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004127 if ((compl_cont_status & CONT_ADDING)
4128 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
4130 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4131 {
4132 /* Try next line, if any. the new word will be
4133 * "join" as if the normal command "J" was used.
4134 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004135 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 * works -- Acevedo */
4137 STRNCPY(IObuff, ptr, len);
4138 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4139 tmp_ptr = ptr = skipwhite(ptr);
4140 /* Find start of next word. */
4141 tmp_ptr = find_word_start(tmp_ptr);
4142 /* Find end of next word. */
4143 tmp_ptr = find_word_end(tmp_ptr);
4144 if (tmp_ptr > ptr)
4145 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004146 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004148 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 IObuff[len++] = ' ';
4150 /* IObuf =~ "\k.* ", thus len >= 2 */
4151 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004152 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 || (vim_strchr(p_cpo, CPO_JOINSP)
4154 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004155 && (IObuff[len - 2] == '?'
4156 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 IObuff[len++] = ' ';
4158 }
4159 /* copy as much as posible of the new word */
4160 if (tmp_ptr - ptr >= IOSIZE - len)
4161 tmp_ptr = ptr + IOSIZE - len - 1;
4162 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4163 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004164 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 }
4166 IObuff[len] = NUL;
4167 ptr = IObuff;
4168 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004169 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 continue;
4171 }
4172 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004173 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004174 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004175 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
4177 found_new_match = OK;
4178 break;
4179 }
4180 }
4181 p_scs = save_p_scs;
4182 p_ws = save_p_ws;
4183 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004184
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004185 /* check if compl_curr_match has changed, (e.g. other type of
4186 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004187 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 found_new_match = OK;
4189
4190 /* break the loop for specialized modes (use 'complete' just for the
4191 * generic ctrl_x_mode == 0) or when we've found a new match */
4192 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004193 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004194 {
4195 if (got_int)
4196 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004197 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004198 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004199 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004200
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004201 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4202 || compl_interrupted)
4203 break;
4204 compl_started = TRUE;
4205 }
4206 else
4207 {
4208 /* Mark a buffer scanned when it has been scanned completely */
4209 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4210 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004212 compl_started = FALSE;
4213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004215 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
4217 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4218 && *e_cpt == NUL) /* Got to end of 'complete' */
4219 found_new_match = FAIL;
4220
4221 i = -1; /* total of matches, unknown */
4222 if (found_new_match == FAIL
4223 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4224 i = ins_compl_make_cyclic();
4225
4226 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004227 * just been made cyclic then we have to move compl_curr_match to the next
4228 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004229 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4230 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004231 if (compl_curr_match == NULL)
4232 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 return i;
4234}
4235
4236/* Delete the old text being completed. */
4237 static void
4238ins_compl_delete()
4239{
4240 int i;
4241
4242 /*
4243 * In insert mode: Delete the typed part.
4244 * In replace mode: Put the old characters back, if any.
4245 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004246 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 backspace_until_column(i);
4248 changed_cline_bef_curs();
4249}
4250
4251/* Insert the new text being completed. */
4252 static void
4253ins_compl_insert()
4254{
Bram Moolenaar572cb562005-08-05 21:35:02 +00004255 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004256 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4257 compl_used_match = FALSE;
4258 else
4259 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260}
4261
4262/*
4263 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004264 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4265 * get more completions. If it is FALSE, then we just do nothing when there
4266 * are no more completions in a given direction. The latter case is used when
4267 * we are still in the middle of finding completions, to allow browsing
4268 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 * Return the total number of matches, or -1 if still unknown -- webb.
4270 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004271 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4272 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 *
4274 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004275 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4276 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 */
4278 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004279ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004281 int count; /* repeat completion this many times; should
4282 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004283 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284{
4285 int num_matches = -1;
4286 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004287 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004288 compl_T *found_compl = NULL;
4289 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004290 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004292 if (compl_leader != NULL
4293 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004295 /* Set "compl_shown_match" to the actually shown match, it may differ
4296 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004297 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004298 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004299 && compl_shown_match->cp_next != NULL
4300 && compl_shown_match->cp_next != compl_first_match)
4301 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004302
4303 /* If we didn't find it searching forward, and compl_shows_dir is
4304 * backward, find the last match. */
4305 if (compl_shows_dir == BACKWARD
4306 && !ins_compl_equal(compl_shown_match,
4307 compl_leader, (int)STRLEN(compl_leader))
4308 && (compl_shown_match->cp_next == NULL
4309 || compl_shown_match->cp_next == compl_first_match))
4310 {
4311 while (!ins_compl_equal(compl_shown_match,
4312 compl_leader, (int)STRLEN(compl_leader))
4313 && compl_shown_match->cp_prev != NULL
4314 && compl_shown_match->cp_prev != compl_first_match)
4315 compl_shown_match = compl_shown_match->cp_prev;
4316 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004317 }
4318
4319 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004320 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 /* Delete old text to be replaced */
4322 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004323
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004324 /* When finding the longest common text we stick at the original text,
4325 * don't let CTRL-N or CTRL-P move to the first match. */
4326 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4327
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004328 /* When restarting the search don't insert the first match either. */
4329 if (compl_restarting)
4330 {
4331 advance = FALSE;
4332 compl_restarting = FALSE;
4333 }
4334
Bram Moolenaare3226be2005-12-18 22:10:00 +00004335 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4336 * around. */
4337 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004339 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004341 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004342 found_end = (compl_first_match != NULL
4343 && (compl_shown_match->cp_next == compl_first_match
4344 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004345 }
4346 else if (compl_shows_dir == BACKWARD
4347 && compl_shown_match->cp_prev != NULL)
4348 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004349 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004350 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004351 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004354 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004355 if (!allow_get_expansion)
4356 {
4357 if (advance)
4358 {
4359 if (compl_shows_dir == BACKWARD)
4360 compl_pending -= todo + 1;
4361 else
4362 compl_pending += todo + 1;
4363 }
4364 return -1;
4365 }
4366
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004367 if (advance)
4368 {
4369 if (compl_shows_dir == BACKWARD)
4370 --compl_pending;
4371 else
4372 ++compl_pending;
4373 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004374
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004375 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004376 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004377
4378 /* handle any pending completions */
4379 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004380 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004381 {
4382 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4383 {
4384 compl_shown_match = compl_shown_match->cp_next;
4385 --compl_pending;
4386 }
4387 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4388 {
4389 compl_shown_match = compl_shown_match->cp_prev;
4390 ++compl_pending;
4391 }
4392 else
4393 break;
4394 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004395 found_end = FALSE;
4396 }
4397 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4398 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004399 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004400 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004401 ++todo;
4402 else
4403 /* Remember a matching item. */
4404 found_compl = compl_shown_match;
4405
4406 /* Stop at the end of the list when we found a usable match. */
4407 if (found_end)
4408 {
4409 if (found_compl != NULL)
4410 {
4411 compl_shown_match = found_compl;
4412 break;
4413 }
4414 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
4417
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004418 /* Insert the text of the new completion, or the compl_leader. */
4419 if (insert_match)
4420 {
4421 if (!compl_get_longest || compl_used_match)
4422 ins_compl_insert();
4423 else
4424 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4425 }
4426 else
4427 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
4429 if (!allow_get_expansion)
4430 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004431 /* may undisplay the popup menu first */
4432 ins_compl_upd_pum();
4433
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004434 /* redraw to show the user what was inserted */
4435 update_screen(0);
4436
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004437 /* display the updated popup menu */
4438 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004439#ifdef FEAT_GUI
4440 if (gui.in_use)
4441 {
4442 /* Show the cursor after the match, not after the redrawn text. */
4443 setcursor();
4444 out_flush();
4445 gui_update_cursor(FALSE, FALSE);
4446 }
4447#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004448
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 /* Delete old text to be replaced, since we're still searching and
4450 * don't want to match ourselves! */
4451 ins_compl_delete();
4452 }
4453
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004454 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004455 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004456 compl_enter_selects = !insert_match && compl_match_array != NULL;
4457
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 /*
4459 * Show the file name for the match (if any)
4460 * Truncate the file name to avoid a wait for return.
4461 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004462 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 {
4464 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004465 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 if (i <= 0)
4467 i = 0;
4468 else
4469 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004470 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 msg(IObuff);
4472 redraw_cmdline = FALSE; /* don't overwrite! */
4473 }
4474
4475 return num_matches;
4476}
4477
4478/*
4479 * Call this while finding completions, to check whether the user has hit a key
4480 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004481 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004483 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 */
4485 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004486ins_compl_check_keys(frequency)
4487 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488{
4489 static int count = 0;
4490
4491 int c;
4492
4493 /* Don't check when reading keys from a script. That would break the test
4494 * scripts */
4495 if (using_script())
4496 return;
4497
4498 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004499 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 return;
4501 count = 0;
4502
Bram Moolenaara260a972006-06-23 19:36:29 +00004503 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4504 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 if (c != NUL)
4507 {
4508 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4509 {
4510 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004511 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004512 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4513 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004515 else
4516 {
4517 /* Need to get the character to have KeyTyped set. We'll put it
4518 * back with vungetc() below. */
4519 c = safe_vgetc();
4520
4521 /* Don't interrupt completion when the character wasn't typed,
4522 * e.g., when doing @q to replay keys. */
4523 if (c != Ctrl_R && KeyTyped)
4524 compl_interrupted = TRUE;
4525
4526 vungetc(c);
4527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004529 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004530 {
4531 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4532
4533 compl_pending = 0;
4534 (void)ins_compl_next(FALSE, todo, TRUE);
4535 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004536}
4537
4538/*
4539 * Decide the direction of Insert mode complete from the key typed.
4540 * Returns BACKWARD or FORWARD.
4541 */
4542 static int
4543ins_compl_key2dir(c)
4544 int c;
4545{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004546 if (c == Ctrl_P || c == Ctrl_L
4547 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4548 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004549 return BACKWARD;
4550 return FORWARD;
4551}
4552
4553/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004554 * Return TRUE for keys that are used for completion only when the popup menu
4555 * is visible.
4556 */
4557 static int
4558ins_compl_pum_key(c)
4559 int c;
4560{
4561 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004562 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4563 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004564}
4565
4566/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004567 * Decide the number of completions to move forward.
4568 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4569 */
4570 static int
4571ins_compl_key2count(c)
4572 int c;
4573{
4574 int h;
4575
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004576 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004577 {
4578 h = pum_get_height();
4579 if (h > 3)
4580 h -= 2; /* keep some context */
4581 return h;
4582 }
4583 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584}
4585
4586/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004587 * Return TRUE if completion with "c" should insert the match, FALSE if only
4588 * to change the currently selected completion.
4589 */
4590 static int
4591ins_compl_use_match(c)
4592 int c;
4593{
4594 switch (c)
4595 {
4596 case K_UP:
4597 case K_DOWN:
4598 case K_PAGEDOWN:
4599 case K_KPAGEDOWN:
4600 case K_S_DOWN:
4601 case K_PAGEUP:
4602 case K_KPAGEUP:
4603 case K_S_UP:
4604 return FALSE;
4605 }
4606 return TRUE;
4607}
4608
4609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 * Do Insert mode completion.
4611 * Called when character "c" was typed, which has a meaning for completion.
4612 * Returns OK if completion was done, FAIL if something failed (out of mem).
4613 */
4614 static int
4615ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004616 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004618 char_u *line;
4619 int startcol = 0; /* column where searched text starts */
4620 colnr_T curs_col; /* cursor column */
4621 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
Bram Moolenaare3226be2005-12-18 22:10:00 +00004623 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004624 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
4626 /* First time we hit ^N or ^P (in a row, I mean) */
4627
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 did_ai = FALSE;
4629#ifdef FEAT_SMARTINDENT
4630 did_si = FALSE;
4631 can_si = FALSE;
4632 can_si_back = FALSE;
4633#endif
4634 if (stop_arrow() == FAIL)
4635 return FAIL;
4636
4637 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004638 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004639 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004641 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004642 * "compl_startpos" to the cursor as a pattern to add a new word
4643 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004644 * "compl_startpos" is not in the same line as the cursor then fix it
4645 * (the line has been split because it was longer than 'tw'). if SOL
4646 * is set then skip the previous pattern, a word at the beginning of
4647 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004648 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4649 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
4651 /*
4652 * it is a continued search
4653 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004654 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4656 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4657 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004658 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004660 /* line (probably) wrapped, set compl_startpos to the
4661 * first non_blank in the line, if it is not a wordchar
4662 * include it to get a better pattern, but then we don't
4663 * want the "\\<" prefix, check it bellow */
4664 compl_col = (colnr_T)(skipwhite(line) - line);
4665 compl_startpos.col = compl_col;
4666 compl_startpos.lnum = curwin->w_cursor.lnum;
4667 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
4669 else
4670 {
4671 /* S_IPOS was set when we inserted a word that was at the
4672 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004673 * mode but first we need to redefine compl_startpos */
4674 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004676 compl_cont_status |= CONT_SOL;
4677 compl_startpos.col = (colnr_T)(skipwhite(
4678 line + compl_length
4679 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004681 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004683 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004684 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 * have enough space? just being paranoic */
4686#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004687 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004689 compl_cont_status &= ~CONT_SOL;
4690 compl_length = (IOSIZE - MIN_SPACE);
4691 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004693 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4694 if (compl_length < 1)
4695 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 }
4697 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004698 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004700 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 }
4702 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004703 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004705 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004707 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004709 compl_cont_status = 0;
4710 compl_cont_status |= CONT_N_ADDS;
4711 compl_startpos = curwin->w_cursor;
4712 startcol = (int)curs_col;
4713 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 }
4715
4716 /* Work out completion pattern and original text -- webb */
4717 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4718 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004719 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4721 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004722 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004724 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004726 compl_col += ++startcol;
4727 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 }
4729 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004730 compl_pattern = str_foldcase(line + compl_col,
4731 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004733 compl_pattern = vim_strnsave(line + compl_col,
4734 compl_length);
4735 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 return FAIL;
4737 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004738 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
4740 char_u *prefix = (char_u *)"\\<";
4741
4742 /* we need 3 extra chars, 1 for the NUL and
4743 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004744 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4745 compl_length) + 3);
4746 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004748 if (!vim_iswordp(line + compl_col)
4749 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 && (
4751#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004752 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004754 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755#endif
4756 )))
4757 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004758 STRCPY((char *)compl_pattern, prefix);
4759 (void)quote_meta(compl_pattern + STRLEN(prefix),
4760 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004762 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004764 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004766 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767#endif
4768 )
4769 {
4770 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004771 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4772 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004774 compl_col += curs_col;
4775 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 }
4777 else
4778 {
4779#ifdef FEAT_MBYTE
4780 /* Search the point of change class of multibyte character
4781 * or not a word single byte character backward. */
4782 if (has_mbyte)
4783 {
4784 int base_class;
4785 int head_off;
4786
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004787 startcol -= (*mb_head_off)(line, line + startcol);
4788 base_class = mb_get_class(line + startcol);
4789 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004791 head_off = (*mb_head_off)(line, line + startcol);
4792 if (base_class != mb_get_class(line + startcol
4793 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004795 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797 }
4798 else
4799#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004800 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004802 compl_col += ++startcol;
4803 compl_length = (int)curs_col - startcol;
4804 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
4806 /* Only match word with at least two chars -- webb
4807 * there's no need to call quote_meta,
4808 * alloc(7) is enough -- Acevedo
4809 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004810 compl_pattern = alloc(7);
4811 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004813 STRCPY((char *)compl_pattern, "\\<");
4814 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4815 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 }
4817 else
4818 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004819 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4820 compl_length) + 3);
4821 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004823 STRCPY((char *)compl_pattern, "\\<");
4824 (void)quote_meta(compl_pattern + 2, line + compl_col,
4825 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 }
4827 }
4828 }
4829 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4830 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004831 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004832 compl_length = (int)curs_col - (int)compl_col;
4833 if (compl_length < 0) /* cursor in indent: empty pattern */
4834 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004836 compl_pattern = str_foldcase(line + compl_col, compl_length,
4837 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004839 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4840 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 return FAIL;
4842 }
4843 else if (ctrl_x_mode == CTRL_X_FILES)
4844 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004845 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004847 compl_col += ++startcol;
4848 compl_length = (int)curs_col - startcol;
4849 compl_pattern = addstar(line + compl_col, compl_length,
4850 EXPAND_FILES);
4851 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 return FAIL;
4853 }
4854 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4855 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004856 compl_pattern = vim_strnsave(line, curs_col);
4857 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004859 set_cmd_context(&compl_xp, compl_pattern,
4860 (int)STRLEN(compl_pattern), curs_col);
4861 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4862 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004863 /* No completion possible, use an empty pattern to get a
4864 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004865 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004866 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004867 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4868 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004870 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004871 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004872#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004873 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004874 * Call user defined function 'completefunc' with "a:findstart"
4875 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004876 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004877 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004878 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004879 char_u *funcname;
4880 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004881
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004882 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004883 * string */
4884 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4885 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4886 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004887 {
4888 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4889 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004890 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004891 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004892
4893 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004894 args[1] = NULL;
4895 pos = curwin->w_cursor;
4896 col = call_func_retnr(funcname, 2, args, FALSE);
4897 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004898
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004899 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004900 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004901 compl_col = col;
4902 if ((colnr_T)compl_col > curs_col)
4903 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004904
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004905 /* Setup variables for completion. Need to obtain "line" again,
4906 * it may have become invalid. */
4907 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004908 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004909 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4910 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004911#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004912 return FAIL;
4913 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004914 else if (ctrl_x_mode == CTRL_X_SPELL)
4915 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004916#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004917 if (spell_bad_len > 0)
4918 compl_col = curs_col - spell_bad_len;
4919 else
4920 compl_col = spell_word_start(startcol);
4921 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004922 {
4923 compl_length = 0;
4924 compl_col = curs_col;
4925 }
4926 else
4927 {
4928 spell_expand_check_cap(compl_col);
4929 compl_length = (int)curs_col - compl_col;
4930 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004931 /* Need to obtain "line" again, it may have become invalid. */
4932 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004933 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4934 if (compl_pattern == NULL)
4935#endif
4936 return FAIL;
4937 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004938 else
4939 {
4940 EMSG2(_(e_intern2), "ins_complete()");
4941 return FAIL;
4942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004944 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
4946 edit_submode_pre = (char_u *)_(" Adding");
4947 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4948 {
4949 /* Insert a new line, keep indentation but ignore 'comments' */
4950#ifdef FEAT_COMMENTS
4951 char_u *old = curbuf->b_p_com;
4952
4953 curbuf->b_p_com = (char_u *)"";
4954#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004955 compl_startpos.lnum = curwin->w_cursor.lnum;
4956 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 ins_eol('\r');
4958#ifdef FEAT_COMMENTS
4959 curbuf->b_p_com = old;
4960#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004961 compl_length = 0;
4962 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
4964 }
4965 else
4966 {
4967 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004968 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 }
4970
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004971 if (compl_cont_status & CONT_LOCAL)
4972 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 else
4974 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4975
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004976 /* Always add completion for the original text. */
4977 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004978 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4979 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004980 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004982 vim_free(compl_pattern);
4983 compl_pattern = NULL;
4984 vim_free(compl_orig_text);
4985 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 return FAIL;
4987 }
4988
4989 /* showmode might reset the internal line pointers, so it must
4990 * be called before line = ml_get(), or when this address is no
4991 * longer needed. -- Acevedo.
4992 */
4993 edit_submode_extra = (char_u *)_("-- Searching...");
4994 edit_submode_highl = HLF_COUNT;
4995 showmode();
4996 edit_submode_extra = NULL;
4997 out_flush();
4998 }
4999
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005000 compl_shown_match = compl_curr_match;
5001 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002
5003 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005004 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005006 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005008 /* may undisplay the popup menu */
5009 ins_compl_upd_pum();
5010
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005011 if (n > 1) /* all matches have been found */
5012 compl_matches = n;
5013 compl_curr_match = compl_shown_match;
5014 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015
Bram Moolenaard68071d2006-05-02 22:08:30 +00005016 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5017 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 if (got_int && !global_busy)
5019 {
5020 (void)vgetc();
5021 got_int = FALSE;
5022 }
5023
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005024 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005025 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005027 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5028 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5030 edit_submode_highl = HLF_E;
5031 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5032 * because we couldn't expand anything at first place, but if we used
5033 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5034 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005035 if ( compl_length > 1
5036 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 || (ctrl_x_mode != 0
5038 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5039 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005040 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
5042
Bram Moolenaar572cb562005-08-05 21:35:02 +00005043 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005044 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005046 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048 if (edit_submode_extra == NULL)
5049 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005050 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 {
5052 edit_submode_extra = (char_u *)_("Back at original");
5053 edit_submode_highl = HLF_W;
5054 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005055 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
5057 edit_submode_extra = (char_u *)_("Word from other line");
5058 edit_submode_highl = HLF_COUNT;
5059 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005060 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
5062 edit_submode_extra = (char_u *)_("The only match");
5063 edit_submode_highl = HLF_COUNT;
5064 }
5065 else
5066 {
5067 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005068 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005070 int number = 0;
5071 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005073 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 {
5075 /* search backwards for the first valid (!= -1) number.
5076 * This should normally succeed already at the first loop
5077 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005078 for (match = compl_curr_match->cp_prev; match != NULL
5079 && match != compl_first_match;
5080 match = match->cp_prev)
5081 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005083 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 break;
5085 }
5086 if (match != NULL)
5087 /* go up and assign all numbers which are not assigned
5088 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005089 for (match = match->cp_next;
5090 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005091 match = match->cp_next)
5092 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
5094 else /* BACKWARD */
5095 {
5096 /* search forwards (upwards) for the first valid (!= -1)
5097 * number. This should normally succeed already at the
5098 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005099 for (match = compl_curr_match->cp_next; match != NULL
5100 && match != compl_first_match;
5101 match = match->cp_next)
5102 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005104 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 break;
5106 }
5107 if (match != NULL)
5108 /* go down and assign all numbers which are not
5109 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005110 for (match = match->cp_prev; match
5111 && match->cp_number == -1;
5112 match = match->cp_prev)
5113 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115 }
5116
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005117 /* The match should always have a sequence number now, this is
5118 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005119 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005121 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5122 * Translations may need more than twice that. */
5123 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005125 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005126 vim_snprintf((char *)match_ref, sizeof(match_ref),
5127 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005128 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005130 vim_snprintf((char *)match_ref, sizeof(match_ref),
5131 _("match %d"),
5132 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 edit_submode_extra = match_ref;
5134 edit_submode_highl = HLF_R;
5135 if (dollar_vcol)
5136 curs_columns(FALSE);
5137 }
5138 }
5139 }
5140
5141 /* Show a message about what (completion) mode we're in. */
5142 showmode();
5143 if (edit_submode_extra != NULL)
5144 {
5145 if (!p_smd)
5146 msg_attr(edit_submode_extra,
5147 edit_submode_highl < HLF_COUNT
5148 ? hl_attr(edit_submode_highl) : 0);
5149 }
5150 else
5151 msg_clr_cmdline(); /* necessary for "noshowmode" */
5152
Bram Moolenaard68071d2006-05-02 22:08:30 +00005153 /* Show the popup menu, unless we got interrupted. */
5154 if (!compl_interrupted)
5155 {
5156 /* RedrawingDisabled may be set when invoked through complete(). */
5157 n = RedrawingDisabled;
5158 RedrawingDisabled = 0;
5159 ins_compl_show_pum();
5160 setcursor();
5161 RedrawingDisabled = n;
5162 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005163 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005164 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005165
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 return OK;
5167}
5168
5169/*
5170 * Looks in the first "len" chars. of "src" for search-metachars.
5171 * If dest is not NULL the chars. are copied there quoting (with
5172 * a backslash) the metachars, and dest would be NUL terminated.
5173 * Returns the length (needed) of dest
5174 */
5175 static int
5176quote_meta(dest, src, len)
5177 char_u *dest;
5178 char_u *src;
5179 int len;
5180{
5181 int m;
5182
5183 for (m = len; --len >= 0; src++)
5184 {
5185 switch (*src)
5186 {
5187 case '.':
5188 case '*':
5189 case '[':
5190 if (ctrl_x_mode == CTRL_X_DICTIONARY
5191 || ctrl_x_mode == CTRL_X_THESAURUS)
5192 break;
5193 case '~':
5194 if (!p_magic) /* quote these only if magic is set */
5195 break;
5196 case '\\':
5197 if (ctrl_x_mode == CTRL_X_DICTIONARY
5198 || ctrl_x_mode == CTRL_X_THESAURUS)
5199 break;
5200 case '^': /* currently it's not needed. */
5201 case '$':
5202 m++;
5203 if (dest != NULL)
5204 *dest++ = '\\';
5205 break;
5206 }
5207 if (dest != NULL)
5208 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005209# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 /* Copy remaining bytes of a multibyte character. */
5211 if (has_mbyte)
5212 {
5213 int i, mb_len;
5214
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005215 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 if (mb_len > 0 && len >= mb_len)
5217 for (i = 0; i < mb_len; ++i)
5218 {
5219 --len;
5220 ++src;
5221 if (dest != NULL)
5222 *dest++ = *src;
5223 }
5224 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005225# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
5227 if (dest != NULL)
5228 *dest = NUL;
5229
5230 return m;
5231}
5232#endif /* FEAT_INS_EXPAND */
5233
5234/*
5235 * Next character is interpreted literally.
5236 * A one, two or three digit decimal number is interpreted as its byte value.
5237 * If one or two digits are entered, the next character is given to vungetc().
5238 * For Unicode a character > 255 may be returned.
5239 */
5240 int
5241get_literal()
5242{
5243 int cc;
5244 int nc;
5245 int i;
5246 int hex = FALSE;
5247 int octal = FALSE;
5248#ifdef FEAT_MBYTE
5249 int unicode = 0;
5250#endif
5251
5252 if (got_int)
5253 return Ctrl_C;
5254
5255#ifdef FEAT_GUI
5256 /*
5257 * In GUI there is no point inserting the internal code for a special key.
5258 * It is more useful to insert the string "<KEY>" instead. This would
5259 * probably be useful in a text window too, but it would not be
5260 * vi-compatible (maybe there should be an option for it?) -- webb
5261 */
5262 if (gui.in_use)
5263 ++allow_keys;
5264#endif
5265#ifdef USE_ON_FLY_SCROLL
5266 dont_scroll = TRUE; /* disallow scrolling here */
5267#endif
5268 ++no_mapping; /* don't map the next key hits */
5269 cc = 0;
5270 i = 0;
5271 for (;;)
5272 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005273 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274#ifdef FEAT_CMDL_INFO
5275 if (!(State & CMDLINE)
5276# ifdef FEAT_MBYTE
5277 && MB_BYTE2LEN_CHECK(nc) == 1
5278# endif
5279 )
5280 add_to_showcmd(nc);
5281#endif
5282 if (nc == 'x' || nc == 'X')
5283 hex = TRUE;
5284 else if (nc == 'o' || nc == 'O')
5285 octal = TRUE;
5286#ifdef FEAT_MBYTE
5287 else if (nc == 'u' || nc == 'U')
5288 unicode = nc;
5289#endif
5290 else
5291 {
5292 if (hex
5293#ifdef FEAT_MBYTE
5294 || unicode != 0
5295#endif
5296 )
5297 {
5298 if (!vim_isxdigit(nc))
5299 break;
5300 cc = cc * 16 + hex2nr(nc);
5301 }
5302 else if (octal)
5303 {
5304 if (nc < '0' || nc > '7')
5305 break;
5306 cc = cc * 8 + nc - '0';
5307 }
5308 else
5309 {
5310 if (!VIM_ISDIGIT(nc))
5311 break;
5312 cc = cc * 10 + nc - '0';
5313 }
5314
5315 ++i;
5316 }
5317
5318 if (cc > 255
5319#ifdef FEAT_MBYTE
5320 && unicode == 0
5321#endif
5322 )
5323 cc = 255; /* limit range to 0-255 */
5324 nc = 0;
5325
5326 if (hex) /* hex: up to two chars */
5327 {
5328 if (i >= 2)
5329 break;
5330 }
5331#ifdef FEAT_MBYTE
5332 else if (unicode) /* Unicode: up to four or eight chars */
5333 {
5334 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5335 break;
5336 }
5337#endif
5338 else if (i >= 3) /* decimal or octal: up to three chars */
5339 break;
5340 }
5341 if (i == 0) /* no number entered */
5342 {
5343 if (nc == K_ZERO) /* NUL is stored as NL */
5344 {
5345 cc = '\n';
5346 nc = 0;
5347 }
5348 else
5349 {
5350 cc = nc;
5351 nc = 0;
5352 }
5353 }
5354
5355 if (cc == 0) /* NUL is stored as NL */
5356 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005357#ifdef FEAT_MBYTE
5358 if (enc_dbcs && (cc & 0xff) == 0)
5359 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5360 second byte will cause trouble! */
5361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
5363 --no_mapping;
5364#ifdef FEAT_GUI
5365 if (gui.in_use)
5366 --allow_keys;
5367#endif
5368 if (nc)
5369 vungetc(nc);
5370 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5371 return cc;
5372}
5373
5374/*
5375 * Insert character, taking care of special keys and mod_mask
5376 */
5377 static void
5378insert_special(c, allow_modmask, ctrlv)
5379 int c;
5380 int allow_modmask;
5381 int ctrlv; /* c was typed after CTRL-V */
5382{
5383 char_u *p;
5384 int len;
5385
5386 /*
5387 * Special function key, translate into "<Key>". Up to the last '>' is
5388 * inserted with ins_str(), so as not to replace characters in replace
5389 * mode.
5390 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5391 * unless 'allow_modmask' is TRUE.
5392 */
5393#ifdef MACOS
5394 /* Command-key never produces a normal key */
5395 if (mod_mask & MOD_MASK_CMD)
5396 allow_modmask = TRUE;
5397#endif
5398 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5399 {
5400 p = get_special_key_name(c, mod_mask);
5401 len = (int)STRLEN(p);
5402 c = p[len - 1];
5403 if (len > 2)
5404 {
5405 if (stop_arrow() == FAIL)
5406 return;
5407 p[len - 1] = NUL;
5408 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005409 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 ctrlv = FALSE;
5411 }
5412 }
5413 if (stop_arrow() == OK)
5414 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5415}
5416
5417/*
5418 * Special characters in this context are those that need processing other
5419 * than the simple insertion that can be performed here. This includes ESC
5420 * which terminates the insert, and CR/NL which need special processing to
5421 * open up a new line. This routine tries to optimize insertions performed by
5422 * the "redo", "undo" or "put" commands, so it needs to know when it should
5423 * stop and defer processing to the "normal" mechanism.
5424 * '0' and '^' are special, because they can be followed by CTRL-D.
5425 */
5426#ifdef EBCDIC
5427# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5428#else
5429# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5430#endif
5431
5432#ifdef FEAT_MBYTE
5433# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5434#else
5435# define WHITECHAR(cc) vim_iswhite(cc)
5436#endif
5437
5438 void
5439insertchar(c, flags, second_indent)
5440 int c; /* character to insert or NUL */
5441 int flags; /* INSCHAR_FORMAT, etc. */
5442 int second_indent; /* indent for second line if >= 0 */
5443{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 int textwidth;
5445#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
5450 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5451 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452
5453 /*
5454 * Try to break the line in two or more pieces when:
5455 * - Always do this if we have been called to do formatting only.
5456 * - Always do this when 'formatoptions' has the 'a' flag and the line
5457 * ends in white space.
5458 * - Otherwise:
5459 * - Don't do this if inserting a blank
5460 * - Don't do this if an existing character is being replaced, unless
5461 * we're in VREPLACE mode.
5462 * - Do this if the cursor is not on the line where insert started
5463 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5464 * before the insert.
5465 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5466 * before 'textwidth'
5467 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005468 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 && ((flags & INSCHAR_FORMAT)
5470 || (!vim_iswhite(c)
5471 && !((State & REPLACE_FLAG)
5472#ifdef FEAT_VREPLACE
5473 && !(State & VREPLACE_FLAG)
5474#endif
5475 && *ml_get_cursor() != NUL)
5476 && (curwin->w_cursor.lnum != Insstart.lnum
5477 || ((!has_format_option(FO_INS_LONG)
5478 || Insstart_textlen <= (colnr_T)textwidth)
5479 && (!fo_ins_blank
5480 || Insstart_blank_vcol <= (colnr_T)textwidth
5481 ))))))
5482 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005483 /* Format with 'formatexpr' when it's set. Use internal formatting
5484 * when 'formatexpr' isn't set or it returns non-zero. */
5485#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005486 int do_internal = TRUE;
5487
5488 if (*curbuf->b_p_fex != NUL)
5489 {
5490 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5491 /* It may be required to save for undo again, e.g. when setline()
5492 * was called. */
5493 ins_need_undo = TRUE;
5494 }
5495 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005497 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005499
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 if (c == NUL) /* only formatting was wanted */
5501 return;
5502
5503#ifdef FEAT_COMMENTS
5504 /* Check whether this character should end a comment. */
5505 if (did_ai && (int)c == end_comment_pending)
5506 {
5507 char_u *line;
5508 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5509 int middle_len, end_len;
5510 int i;
5511
5512 /*
5513 * Need to remove existing (middle) comment leader and insert end
5514 * comment leader. First, check what comment leader we can find.
5515 */
5516 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5517 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5518 {
5519 /* Skip middle-comment string */
5520 while (*p && p[-1] != ':') /* find end of middle flags */
5521 ++p;
5522 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5523 /* Don't count trailing white space for middle_len */
5524 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5525 --middle_len;
5526
5527 /* Find the end-comment string */
5528 while (*p && p[-1] != ':') /* find end of end flags */
5529 ++p;
5530 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5531
5532 /* Skip white space before the cursor */
5533 i = curwin->w_cursor.col;
5534 while (--i >= 0 && vim_iswhite(line[i]))
5535 ;
5536 i++;
5537
5538 /* Skip to before the middle leader */
5539 i -= middle_len;
5540
5541 /* Check some expected things before we go on */
5542 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5543 {
5544 /* Backspace over all the stuff we want to replace */
5545 backspace_until_column(i);
5546
5547 /*
5548 * Insert the end-comment string, except for the last
5549 * character, which will get inserted as normal later.
5550 */
5551 ins_bytes_len(lead_end, end_len - 1);
5552 }
5553 }
5554 }
5555 end_comment_pending = NUL;
5556#endif
5557
5558 did_ai = FALSE;
5559#ifdef FEAT_SMARTINDENT
5560 did_si = FALSE;
5561 can_si = FALSE;
5562 can_si_back = FALSE;
5563#endif
5564
5565 /*
5566 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5567 * This speeds up normal text input considerably.
5568 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5569 * need to re-indent at a ':', or any other character (but not what
5570 * 'paste' is set)..
5571 */
5572#ifdef USE_ON_FLY_SCROLL
5573 dont_scroll = FALSE; /* allow scrolling here */
5574#endif
5575
5576 if ( !ISSPECIAL(c)
5577#ifdef FEAT_MBYTE
5578 && (!has_mbyte || (*mb_char2len)(c) == 1)
5579#endif
5580 && vpeekc() != NUL
5581 && !(State & REPLACE_FLAG)
5582#ifdef FEAT_CINDENT
5583 && !cindent_on()
5584#endif
5585#ifdef FEAT_RIGHTLEFT
5586 && !p_ri
5587#endif
5588 )
5589 {
5590#define INPUT_BUFLEN 100
5591 char_u buf[INPUT_BUFLEN + 1];
5592 int i;
5593 colnr_T virtcol = 0;
5594
5595 buf[0] = c;
5596 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005597 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 virtcol = get_nolist_virtcol();
5599 /*
5600 * Stop the string when:
5601 * - no more chars available
5602 * - finding a special character (command key)
5603 * - buffer is full
5604 * - running into the 'textwidth' boundary
5605 * - need to check for abbreviation: A non-word char after a word-char
5606 */
5607 while ( (c = vpeekc()) != NUL
5608 && !ISSPECIAL(c)
5609#ifdef FEAT_MBYTE
5610 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5611#endif
5612 && i < INPUT_BUFLEN
5613 && (textwidth == 0
5614 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5615 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5616 {
5617#ifdef FEAT_RIGHTLEFT
5618 c = vgetc();
5619 if (p_hkmap && KeyTyped)
5620 c = hkmap(c); /* Hebrew mode mapping */
5621# ifdef FEAT_FKMAP
5622 if (p_fkmap && KeyTyped)
5623 c = fkmap(c); /* Farsi mode mapping */
5624# endif
5625 buf[i++] = c;
5626#else
5627 buf[i++] = vgetc();
5628#endif
5629 }
5630
5631#ifdef FEAT_DIGRAPHS
5632 do_digraph(-1); /* clear digraphs */
5633 do_digraph(buf[i-1]); /* may be the start of a digraph */
5634#endif
5635 buf[i] = NUL;
5636 ins_str(buf);
5637 if (flags & INSCHAR_CTRLV)
5638 {
5639 redo_literal(*buf);
5640 i = 1;
5641 }
5642 else
5643 i = 0;
5644 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005645 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 }
5647 else
5648 {
5649#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005650 int cc;
5651
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5653 {
5654 char_u buf[MB_MAXBYTES + 1];
5655
5656 (*mb_char2bytes)(c, buf);
5657 buf[cc] = NUL;
5658 ins_char_bytes(buf, cc);
5659 AppendCharToRedobuff(c);
5660 }
5661 else
5662#endif
5663 {
5664 ins_char(c);
5665 if (flags & INSCHAR_CTRLV)
5666 redo_literal(c);
5667 else
5668 AppendCharToRedobuff(c);
5669 }
5670 }
5671}
5672
5673/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005674 * Format text at the current insert position.
5675 */
5676 static void
5677internal_format(textwidth, second_indent, flags, format_only)
5678 int textwidth;
5679 int second_indent;
5680 int flags;
5681 int format_only;
5682{
5683 int cc;
5684 int save_char = NUL;
5685 int haveto_redraw = FALSE;
5686 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5687#ifdef FEAT_MBYTE
5688 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5689#endif
5690 int fo_white_par = has_format_option(FO_WHITE_PAR);
5691 int first_line = TRUE;
5692#ifdef FEAT_COMMENTS
5693 colnr_T leader_len;
5694 int no_leader = FALSE;
5695 int do_comments = (flags & INSCHAR_DO_COM);
5696#endif
5697
5698 /*
5699 * When 'ai' is off we don't want a space under the cursor to be
5700 * deleted. Replace it with an 'x' temporarily.
5701 */
5702 if (!curbuf->b_p_ai)
5703 {
5704 cc = gchar_cursor();
5705 if (vim_iswhite(cc))
5706 {
5707 save_char = cc;
5708 pchar_cursor('x');
5709 }
5710 }
5711
5712 /*
5713 * Repeat breaking lines, until the current line is not too long.
5714 */
5715 while (!got_int)
5716 {
5717 int startcol; /* Cursor column at entry */
5718 int wantcol; /* column at textwidth border */
5719 int foundcol; /* column for start of spaces */
5720 int end_foundcol = 0; /* column for start of word */
5721 colnr_T len;
5722 colnr_T virtcol;
5723#ifdef FEAT_VREPLACE
5724 int orig_col = 0;
5725 char_u *saved_text = NULL;
5726#endif
5727 colnr_T col;
5728
5729 virtcol = get_nolist_virtcol();
5730 if (virtcol < (colnr_T)textwidth)
5731 break;
5732
5733#ifdef FEAT_COMMENTS
5734 if (no_leader)
5735 do_comments = FALSE;
5736 else if (!(flags & INSCHAR_FORMAT)
5737 && has_format_option(FO_WRAP_COMS))
5738 do_comments = TRUE;
5739
5740 /* Don't break until after the comment leader */
5741 if (do_comments)
5742 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5743 else
5744 leader_len = 0;
5745
5746 /* If the line doesn't start with a comment leader, then don't
5747 * start one in a following broken line. Avoids that a %word
5748 * moved to the start of the next line causes all following lines
5749 * to start with %. */
5750 if (leader_len == 0)
5751 no_leader = TRUE;
5752#endif
5753 if (!(flags & INSCHAR_FORMAT)
5754#ifdef FEAT_COMMENTS
5755 && leader_len == 0
5756#endif
5757 && !has_format_option(FO_WRAP))
5758
5759 {
5760 textwidth = 0;
5761 break;
5762 }
5763 if ((startcol = curwin->w_cursor.col) == 0)
5764 break;
5765
5766 /* find column of textwidth border */
5767 coladvance((colnr_T)textwidth);
5768 wantcol = curwin->w_cursor.col;
5769
5770 curwin->w_cursor.col = startcol - 1;
5771#ifdef FEAT_MBYTE
5772 /* Correct cursor for multi-byte character. */
5773 if (has_mbyte)
5774 mb_adjust_cursor();
5775#endif
5776 foundcol = 0;
5777
5778 /*
5779 * Find position to break at.
5780 * Stop at first entered white when 'formatoptions' has 'v'
5781 */
5782 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5783 || curwin->w_cursor.lnum != Insstart.lnum
5784 || curwin->w_cursor.col >= Insstart.col)
5785 {
5786 cc = gchar_cursor();
5787 if (WHITECHAR(cc))
5788 {
5789 /* remember position of blank just before text */
5790 end_foundcol = curwin->w_cursor.col;
5791
5792 /* find start of sequence of blanks */
5793 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5794 {
5795 dec_cursor();
5796 cc = gchar_cursor();
5797 }
5798 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5799 break; /* only spaces in front of text */
5800#ifdef FEAT_COMMENTS
5801 /* Don't break until after the comment leader */
5802 if (curwin->w_cursor.col < leader_len)
5803 break;
5804#endif
5805 if (has_format_option(FO_ONE_LETTER))
5806 {
5807 /* do not break after one-letter words */
5808 if (curwin->w_cursor.col == 0)
5809 break; /* one-letter word at begin */
5810
5811 col = curwin->w_cursor.col;
5812 dec_cursor();
5813 cc = gchar_cursor();
5814
5815 if (WHITECHAR(cc))
5816 continue; /* one-letter, continue */
5817 curwin->w_cursor.col = col;
5818 }
5819#ifdef FEAT_MBYTE
5820 if (has_mbyte)
5821 foundcol = curwin->w_cursor.col
5822 + (*mb_ptr2len)(ml_get_cursor());
5823 else
5824#endif
5825 foundcol = curwin->w_cursor.col + 1;
5826 if (curwin->w_cursor.col < (colnr_T)wantcol)
5827 break;
5828 }
5829#ifdef FEAT_MBYTE
5830 else if (cc >= 0x100 && fo_multibyte
5831 && curwin->w_cursor.col <= (colnr_T)wantcol)
5832 {
5833 /* Break after or before a multi-byte character. */
5834 foundcol = curwin->w_cursor.col;
5835 if (curwin->w_cursor.col < (colnr_T)wantcol)
5836 foundcol += (*mb_char2len)(cc);
5837 end_foundcol = foundcol;
5838 break;
5839 }
5840#endif
5841 if (curwin->w_cursor.col == 0)
5842 break;
5843 dec_cursor();
5844 }
5845
5846 if (foundcol == 0) /* no spaces, cannot break line */
5847 {
5848 curwin->w_cursor.col = startcol;
5849 break;
5850 }
5851
5852 /* Going to break the line, remove any "$" now. */
5853 undisplay_dollar();
5854
5855 /*
5856 * Offset between cursor position and line break is used by replace
5857 * stack functions. VREPLACE does not use this, and backspaces
5858 * over the text instead.
5859 */
5860#ifdef FEAT_VREPLACE
5861 if (State & VREPLACE_FLAG)
5862 orig_col = startcol; /* Will start backspacing from here */
5863 else
5864#endif
5865 replace_offset = startcol - end_foundcol - 1;
5866
5867 /*
5868 * adjust startcol for spaces that will be deleted and
5869 * characters that will remain on top line
5870 */
5871 curwin->w_cursor.col = foundcol;
5872 while (cc = gchar_cursor(), WHITECHAR(cc))
5873 inc_cursor();
5874 startcol -= curwin->w_cursor.col;
5875 if (startcol < 0)
5876 startcol = 0;
5877
5878#ifdef FEAT_VREPLACE
5879 if (State & VREPLACE_FLAG)
5880 {
5881 /*
5882 * In VREPLACE mode, we will backspace over the text to be
5883 * wrapped, so save a copy now to put on the next line.
5884 */
5885 saved_text = vim_strsave(ml_get_cursor());
5886 curwin->w_cursor.col = orig_col;
5887 if (saved_text == NULL)
5888 break; /* Can't do it, out of memory */
5889 saved_text[startcol] = NUL;
5890
5891 /* Backspace over characters that will move to the next line */
5892 if (!fo_white_par)
5893 backspace_until_column(foundcol);
5894 }
5895 else
5896#endif
5897 {
5898 /* put cursor after pos. to break line */
5899 if (!fo_white_par)
5900 curwin->w_cursor.col = foundcol;
5901 }
5902
5903 /*
5904 * Split the line just before the margin.
5905 * Only insert/delete lines, but don't really redraw the window.
5906 */
5907 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5908 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5909#ifdef FEAT_COMMENTS
5910 + (do_comments ? OPENLINE_DO_COM : 0)
5911#endif
5912 , old_indent);
5913 old_indent = 0;
5914
5915 replace_offset = 0;
5916 if (first_line)
5917 {
5918 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5919 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5920 if (second_indent >= 0)
5921 {
5922#ifdef FEAT_VREPLACE
5923 if (State & VREPLACE_FLAG)
5924 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5925 else
5926#endif
5927 (void)set_indent(second_indent, SIN_CHANGED);
5928 }
5929 first_line = FALSE;
5930 }
5931
5932#ifdef FEAT_VREPLACE
5933 if (State & VREPLACE_FLAG)
5934 {
5935 /*
5936 * In VREPLACE mode we have backspaced over the text to be
5937 * moved, now we re-insert it into the new line.
5938 */
5939 ins_bytes(saved_text);
5940 vim_free(saved_text);
5941 }
5942 else
5943#endif
5944 {
5945 /*
5946 * Check if cursor is not past the NUL off the line, cindent
5947 * may have added or removed indent.
5948 */
5949 curwin->w_cursor.col += startcol;
5950 len = (colnr_T)STRLEN(ml_get_curline());
5951 if (curwin->w_cursor.col > len)
5952 curwin->w_cursor.col = len;
5953 }
5954
5955 haveto_redraw = TRUE;
5956#ifdef FEAT_CINDENT
5957 can_cindent = TRUE;
5958#endif
5959 /* moved the cursor, don't autoindent or cindent now */
5960 did_ai = FALSE;
5961#ifdef FEAT_SMARTINDENT
5962 did_si = FALSE;
5963 can_si = FALSE;
5964 can_si_back = FALSE;
5965#endif
5966 line_breakcheck();
5967 }
5968
5969 if (save_char != NUL) /* put back space after cursor */
5970 pchar_cursor(save_char);
5971
5972 if (!format_only && haveto_redraw)
5973 {
5974 update_topline();
5975 redraw_curbuf_later(VALID);
5976 }
5977}
5978
5979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 * Called after inserting or deleting text: When 'formatoptions' includes the
5981 * 'a' flag format from the current line until the end of the paragraph.
5982 * Keep the cursor at the same position relative to the text.
5983 * The caller must have saved the cursor line for undo, following ones will be
5984 * saved here.
5985 */
5986 void
5987auto_format(trailblank, prev_line)
5988 int trailblank; /* when TRUE also format with trailing blank */
5989 int prev_line; /* may start in previous line */
5990{
5991 pos_T pos;
5992 colnr_T len;
5993 char_u *old;
5994 char_u *new, *pnew;
5995 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005996 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997
5998 if (!has_format_option(FO_AUTO))
5999 return;
6000
6001 pos = curwin->w_cursor;
6002 old = ml_get_curline();
6003
6004 /* may remove added space */
6005 check_auto_format(FALSE);
6006
6007 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6008 * user might insert normal text next. Also skip formatting when "1" is
6009 * in 'formatoptions' and there is a single character before the cursor.
6010 * Otherwise the line would be broken and when typing another non-white
6011 * next they are not joined back together. */
6012 wasatend = (pos.col == STRLEN(old));
6013 if (*old != NUL && !trailblank && wasatend)
6014 {
6015 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006016 cc = gchar_cursor();
6017 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6018 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006020 cc = gchar_cursor();
6021 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 {
6023 curwin->w_cursor = pos;
6024 return;
6025 }
6026 curwin->w_cursor = pos;
6027 }
6028
6029#ifdef FEAT_COMMENTS
6030 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6031 * comments. */
6032 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6033 && get_leader_len(old, NULL, FALSE) == 0)
6034 return;
6035#endif
6036
6037 /*
6038 * May start formatting in a previous line, so that after "x" a word is
6039 * moved to the previous line if it fits there now. Only when this is not
6040 * the start of a paragraph.
6041 */
6042 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6043 {
6044 --curwin->w_cursor.lnum;
6045 if (u_save_cursor() == FAIL)
6046 return;
6047 }
6048
6049 /*
6050 * Do the formatting and restore the cursor position. "saved_cursor" will
6051 * be adjusted for the text formatting.
6052 */
6053 saved_cursor = pos;
6054 format_lines((linenr_T)-1);
6055 curwin->w_cursor = saved_cursor;
6056 saved_cursor.lnum = 0;
6057
6058 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6059 {
6060 /* "cannot happen" */
6061 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6062 coladvance((colnr_T)MAXCOL);
6063 }
6064 else
6065 check_cursor_col();
6066
6067 /* Insert mode: If the cursor is now after the end of the line while it
6068 * previously wasn't, the line was broken. Because of the rule above we
6069 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6070 * formatted. */
6071 if (!wasatend && has_format_option(FO_WHITE_PAR))
6072 {
6073 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006074 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 if (curwin->w_cursor.col == len)
6076 {
6077 pnew = vim_strnsave(new, len + 2);
6078 pnew[len] = ' ';
6079 pnew[len + 1] = NUL;
6080 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6081 /* remove the space later */
6082 did_add_space = TRUE;
6083 }
6084 else
6085 /* may remove added space */
6086 check_auto_format(FALSE);
6087 }
6088
6089 check_cursor();
6090}
6091
6092/*
6093 * When an extra space was added to continue a paragraph for auto-formatting,
6094 * delete it now. The space must be under the cursor, just after the insert
6095 * position.
6096 */
6097 static void
6098check_auto_format(end_insert)
6099 int end_insert; /* TRUE when ending Insert mode */
6100{
6101 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006102 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103
6104 if (did_add_space)
6105 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006106 cc = gchar_cursor();
6107 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 /* Somehow the space was removed already. */
6109 did_add_space = FALSE;
6110 else
6111 {
6112 if (!end_insert)
6113 {
6114 inc_cursor();
6115 c = gchar_cursor();
6116 dec_cursor();
6117 }
6118 if (c != NUL)
6119 {
6120 /* The space is no longer at the end of the line, delete it. */
6121 del_char(FALSE);
6122 did_add_space = FALSE;
6123 }
6124 }
6125 }
6126}
6127
6128/*
6129 * Find out textwidth to be used for formatting:
6130 * if 'textwidth' option is set, use it
6131 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6132 * if invalid value, use 0.
6133 * Set default to window width (maximum 79) for "gq" operator.
6134 */
6135 int
6136comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006137 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138{
6139 int textwidth;
6140
6141 textwidth = curbuf->b_p_tw;
6142 if (textwidth == 0 && curbuf->b_p_wm)
6143 {
6144 /* The width is the window width minus 'wrapmargin' minus all the
6145 * things that add to the margin. */
6146 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6147#ifdef FEAT_CMDWIN
6148 if (cmdwin_type != 0)
6149 textwidth -= 1;
6150#endif
6151#ifdef FEAT_FOLDING
6152 textwidth -= curwin->w_p_fdc;
6153#endif
6154#ifdef FEAT_SIGNS
6155 if (curwin->w_buffer->b_signlist != NULL
6156# ifdef FEAT_NETBEANS_INTG
6157 || usingNetbeans
6158# endif
6159 )
6160 textwidth -= 1;
6161#endif
6162 if (curwin->w_p_nu)
6163 textwidth -= 8;
6164 }
6165 if (textwidth < 0)
6166 textwidth = 0;
6167 if (ff && textwidth == 0)
6168 {
6169 textwidth = W_WIDTH(curwin) - 1;
6170 if (textwidth > 79)
6171 textwidth = 79;
6172 }
6173 return textwidth;
6174}
6175
6176/*
6177 * Put a character in the redo buffer, for when just after a CTRL-V.
6178 */
6179 static void
6180redo_literal(c)
6181 int c;
6182{
6183 char_u buf[10];
6184
6185 /* Only digits need special treatment. Translate them into a string of
6186 * three digits. */
6187 if (VIM_ISDIGIT(c))
6188 {
6189 sprintf((char *)buf, "%03d", c);
6190 AppendToRedobuff(buf);
6191 }
6192 else
6193 AppendCharToRedobuff(c);
6194}
6195
6196/*
6197 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006198 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 */
6200 static void
6201start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006202 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203{
6204 if (!arrow_used) /* something has been inserted */
6205 {
6206 AppendToRedobuff(ESC_STR);
6207 stop_insert(end_insert_pos, FALSE);
6208 arrow_used = TRUE; /* this means we stopped the current insert */
6209 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006210#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006211 check_spell_redraw();
6212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213}
6214
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006215#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006216/*
6217 * If we skipped highlighting word at cursor, do it now.
6218 * It may be skipped again, thus reset spell_redraw_lnum first.
6219 */
6220 static void
6221check_spell_redraw()
6222{
6223 if (spell_redraw_lnum != 0)
6224 {
6225 linenr_T lnum = spell_redraw_lnum;
6226
6227 spell_redraw_lnum = 0;
6228 redrawWinline(lnum, FALSE);
6229 }
6230}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006231
6232/*
6233 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6234 * spelled word, if there is one.
6235 */
6236 static void
6237spell_back_to_badword()
6238{
6239 pos_T tpos = curwin->w_cursor;
6240
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006241 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006242 if (curwin->w_cursor.col != tpos.col)
6243 start_arrow(&tpos);
6244}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006245#endif
6246
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247/*
6248 * stop_arrow() is called before a change is made in insert mode.
6249 * If an arrow key has been used, start a new insertion.
6250 * Returns FAIL if undo is impossible, shouldn't insert then.
6251 */
6252 int
6253stop_arrow()
6254{
6255 if (arrow_used)
6256 {
6257 if (u_save_cursor() == OK)
6258 {
6259 arrow_used = FALSE;
6260 ins_need_undo = FALSE;
6261 }
6262 Insstart = curwin->w_cursor; /* new insertion starts here */
6263 Insstart_textlen = linetabsize(ml_get_curline());
6264 ai_col = 0;
6265#ifdef FEAT_VREPLACE
6266 if (State & VREPLACE_FLAG)
6267 {
6268 orig_line_count = curbuf->b_ml.ml_line_count;
6269 vr_lines_changed = 1;
6270 }
6271#endif
6272 ResetRedobuff();
6273 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006274 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 }
6276 else if (ins_need_undo)
6277 {
6278 if (u_save_cursor() == OK)
6279 ins_need_undo = FALSE;
6280 }
6281
6282#ifdef FEAT_FOLDING
6283 /* Always open fold at the cursor line when inserting something. */
6284 foldOpenCursor();
6285#endif
6286
6287 return (arrow_used || ins_need_undo ? FAIL : OK);
6288}
6289
6290/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006291 * Do a few things to stop inserting.
6292 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6293 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 */
6295 static void
6296stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006297 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006298 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006300 int cc;
6301 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302
6303 stop_redo_ins();
6304 replace_flush(); /* abandon replace stack */
6305
6306 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006307 * Save the inserted text for later redo with ^@ and CTRL-A.
6308 * Don't do it when "restart_edit" was set and nothing was inserted,
6309 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006311 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006312 if (did_restart_edit == 0 || (ptr != NULL
6313 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006314 {
6315 vim_free(last_insert);
6316 last_insert = ptr;
6317 last_insert_skip = new_insert_skip;
6318 }
6319 else
6320 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006322 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 {
6324 /* Auto-format now. It may seem strange to do this when stopping an
6325 * insertion (or moving the cursor), but it's required when appending
6326 * a line and having it end in a space. But only do it when something
6327 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006328 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006330 pos_T tpos = curwin->w_cursor;
6331
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 /* When the cursor is at the end of the line after a space the
6333 * formatting will move it to the following word. Avoid that by
6334 * moving the cursor onto the space. */
6335 cc = 'x';
6336 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6337 {
6338 dec_cursor();
6339 cc = gchar_cursor();
6340 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006341 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 }
6343
6344 auto_format(TRUE, FALSE);
6345
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006346 if (vim_iswhite(cc))
6347 {
6348 if (gchar_cursor() != NUL)
6349 inc_cursor();
6350#ifdef FEAT_VIRTUALEDIT
6351 /* If the cursor is still at the same character, also keep
6352 * the "coladd". */
6353 if (gchar_cursor() == NUL
6354 && curwin->w_cursor.lnum == tpos.lnum
6355 && curwin->w_cursor.col == tpos.col)
6356 curwin->w_cursor.coladd = tpos.coladd;
6357#endif
6358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 }
6360
6361 /* If a space was inserted for auto-formatting, remove it now. */
6362 check_auto_format(TRUE);
6363
6364 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006365 * of the line, and put the cursor back.
6366 * Do this when ESC was used or moving the cursor up/down. */
6367 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006368 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006370 pos_T tpos = curwin->w_cursor;
6371
6372 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006373 for (;;)
6374 {
6375 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6376 --curwin->w_cursor.col;
6377 cc = gchar_cursor();
6378 if (!vim_iswhite(cc))
6379 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006381 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006382 if (curwin->w_cursor.lnum != tpos.lnum)
6383 curwin->w_cursor = tpos;
6384 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6386
6387#ifdef FEAT_VISUAL
6388 /* <C-S-Right> may have started Visual mode, adjust the position for
6389 * deleted characters. */
6390 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6391 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006392 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 if (VIsual.col > (colnr_T)cc)
6394 {
6395 VIsual.col = cc;
6396# ifdef FEAT_VIRTUALEDIT
6397 VIsual.coladd = 0;
6398# endif
6399 }
6400 }
6401#endif
6402 }
6403 }
6404 did_ai = FALSE;
6405#ifdef FEAT_SMARTINDENT
6406 did_si = FALSE;
6407 can_si = FALSE;
6408 can_si_back = FALSE;
6409#endif
6410
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006411 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6412 * now in a different buffer. */
6413 if (end_insert_pos != NULL)
6414 {
6415 curbuf->b_op_start = Insstart;
6416 curbuf->b_op_end = *end_insert_pos;
6417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418}
6419
6420/*
6421 * Set the last inserted text to a single character.
6422 * Used for the replace command.
6423 */
6424 void
6425set_last_insert(c)
6426 int c;
6427{
6428 char_u *s;
6429
6430 vim_free(last_insert);
6431#ifdef FEAT_MBYTE
6432 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6433#else
6434 last_insert = alloc(6);
6435#endif
6436 if (last_insert != NULL)
6437 {
6438 s = last_insert;
6439 /* Use the CTRL-V only when entering a special char */
6440 if (c < ' ' || c == DEL)
6441 *s++ = Ctrl_V;
6442 s = add_char2buf(c, s);
6443 *s++ = ESC;
6444 *s++ = NUL;
6445 last_insert_skip = 0;
6446 }
6447}
6448
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006449#if defined(EXITFREE) || defined(PROTO)
6450 void
6451free_last_insert()
6452{
6453 vim_free(last_insert);
6454 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006455# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006456 vim_free(compl_orig_text);
6457 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006458# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006459}
6460#endif
6461
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462/*
6463 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6464 * and CSI. Handle multi-byte characters.
6465 * Returns a pointer to after the added bytes.
6466 */
6467 char_u *
6468add_char2buf(c, s)
6469 int c;
6470 char_u *s;
6471{
6472#ifdef FEAT_MBYTE
6473 char_u temp[MB_MAXBYTES];
6474 int i;
6475 int len;
6476
6477 len = (*mb_char2bytes)(c, temp);
6478 for (i = 0; i < len; ++i)
6479 {
6480 c = temp[i];
6481#endif
6482 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6483 if (c == K_SPECIAL)
6484 {
6485 *s++ = K_SPECIAL;
6486 *s++ = KS_SPECIAL;
6487 *s++ = KE_FILLER;
6488 }
6489#ifdef FEAT_GUI
6490 else if (c == CSI)
6491 {
6492 *s++ = CSI;
6493 *s++ = KS_EXTRA;
6494 *s++ = (int)KE_CSI;
6495 }
6496#endif
6497 else
6498 *s++ = c;
6499#ifdef FEAT_MBYTE
6500 }
6501#endif
6502 return s;
6503}
6504
6505/*
6506 * move cursor to start of line
6507 * if flags & BL_WHITE move to first non-white
6508 * if flags & BL_SOL move to first non-white if startofline is set,
6509 * otherwise keep "curswant" column
6510 * if flags & BL_FIX don't leave the cursor on a NUL.
6511 */
6512 void
6513beginline(flags)
6514 int flags;
6515{
6516 if ((flags & BL_SOL) && !p_sol)
6517 coladvance(curwin->w_curswant);
6518 else
6519 {
6520 curwin->w_cursor.col = 0;
6521#ifdef FEAT_VIRTUALEDIT
6522 curwin->w_cursor.coladd = 0;
6523#endif
6524
6525 if (flags & (BL_WHITE | BL_SOL))
6526 {
6527 char_u *ptr;
6528
6529 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6530 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6531 ++curwin->w_cursor.col;
6532 }
6533 curwin->w_set_curswant = TRUE;
6534 }
6535}
6536
6537/*
6538 * oneright oneleft cursor_down cursor_up
6539 *
6540 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006541 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 * Return OK when successful, FAIL when we hit a line of file boundary.
6543 */
6544
6545 int
6546oneright()
6547{
6548 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550
6551#ifdef FEAT_VIRTUALEDIT
6552 if (virtual_active())
6553 {
6554 pos_T prevpos = curwin->w_cursor;
6555
6556 /* Adjust for multi-wide char (excluding TAB) */
6557 ptr = ml_get_cursor();
6558 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006559# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006561# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006563# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 ))
6565 ? ptr2cells(ptr) : 1));
6566 curwin->w_set_curswant = TRUE;
6567 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6568 return (prevpos.col != curwin->w_cursor.col
6569 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6570 }
6571#endif
6572
6573 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006574 if (*ptr == NUL)
6575 return FAIL; /* already at the very end */
6576
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006578 if (has_mbyte)
6579 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 else
6581#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006582 l = 1;
6583
6584 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6585 * contains "onemore". */
6586 if (ptr[l] == NUL
6587#ifdef FEAT_VIRTUALEDIT
6588 && (ve_flags & VE_ONEMORE) == 0
6589#endif
6590 )
6591 return FAIL;
6592 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593
6594 curwin->w_set_curswant = TRUE;
6595 return OK;
6596}
6597
6598 int
6599oneleft()
6600{
6601#ifdef FEAT_VIRTUALEDIT
6602 if (virtual_active())
6603 {
6604 int width;
6605 int v = getviscol();
6606
6607 if (v == 0)
6608 return FAIL;
6609
6610# ifdef FEAT_LINEBREAK
6611 /* We might get stuck on 'showbreak', skip over it. */
6612 width = 1;
6613 for (;;)
6614 {
6615 coladvance(v - width);
6616 /* getviscol() is slow, skip it when 'showbreak' is empty and
6617 * there are no multi-byte characters */
6618 if ((*p_sbr == NUL
6619# ifdef FEAT_MBYTE
6620 && !has_mbyte
6621# endif
6622 ) || getviscol() < v)
6623 break;
6624 ++width;
6625 }
6626# else
6627 coladvance(v - 1);
6628# endif
6629
6630 if (curwin->w_cursor.coladd == 1)
6631 {
6632 char_u *ptr;
6633
6634 /* Adjust for multi-wide char (not a TAB) */
6635 ptr = ml_get_cursor();
6636 if (*ptr != TAB && vim_isprintc(
6637# ifdef FEAT_MBYTE
6638 (*mb_ptr2char)(ptr)
6639# else
6640 *ptr
6641# endif
6642 ) && ptr2cells(ptr) > 1)
6643 curwin->w_cursor.coladd = 0;
6644 }
6645
6646 curwin->w_set_curswant = TRUE;
6647 return OK;
6648 }
6649#endif
6650
6651 if (curwin->w_cursor.col == 0)
6652 return FAIL;
6653
6654 curwin->w_set_curswant = TRUE;
6655 --curwin->w_cursor.col;
6656
6657#ifdef FEAT_MBYTE
6658 /* if the character on the left of the current cursor is a multi-byte
6659 * character, move to its first byte */
6660 if (has_mbyte)
6661 mb_adjust_cursor();
6662#endif
6663 return OK;
6664}
6665
6666 int
6667cursor_up(n, upd_topline)
6668 long n;
6669 int upd_topline; /* When TRUE: update topline */
6670{
6671 linenr_T lnum;
6672
6673 if (n > 0)
6674 {
6675 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006676 /* This fails if the cursor is already in the first line or the count
6677 * is larger than the line number and '-' is in 'cpoptions' */
6678 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 return FAIL;
6680 if (n >= lnum)
6681 lnum = 1;
6682 else
6683#ifdef FEAT_FOLDING
6684 if (hasAnyFolding(curwin))
6685 {
6686 /*
6687 * Count each sequence of folded lines as one logical line.
6688 */
6689 /* go to the the start of the current fold */
6690 (void)hasFolding(lnum, &lnum, NULL);
6691
6692 while (n--)
6693 {
6694 /* move up one line */
6695 --lnum;
6696 if (lnum <= 1)
6697 break;
6698 /* If we entered a fold, move to the beginning, unless in
6699 * Insert mode or when 'foldopen' contains "all": it will open
6700 * in a moment. */
6701 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6702 (void)hasFolding(lnum, &lnum, NULL);
6703 }
6704 if (lnum < 1)
6705 lnum = 1;
6706 }
6707 else
6708#endif
6709 lnum -= n;
6710 curwin->w_cursor.lnum = lnum;
6711 }
6712
6713 /* try to advance to the column we want to be at */
6714 coladvance(curwin->w_curswant);
6715
6716 if (upd_topline)
6717 update_topline(); /* make sure curwin->w_topline is valid */
6718
6719 return OK;
6720}
6721
6722/*
6723 * Cursor down a number of logical lines.
6724 */
6725 int
6726cursor_down(n, upd_topline)
6727 long n;
6728 int upd_topline; /* When TRUE: update topline */
6729{
6730 linenr_T lnum;
6731
6732 if (n > 0)
6733 {
6734 lnum = curwin->w_cursor.lnum;
6735#ifdef FEAT_FOLDING
6736 /* Move to last line of fold, will fail if it's the end-of-file. */
6737 (void)hasFolding(lnum, NULL, &lnum);
6738#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006739 /* This fails if the cursor is already in the last line or would move
6740 * beyound the last line and '-' is in 'cpoptions' */
6741 if (lnum >= curbuf->b_ml.ml_line_count
6742 || (lnum + n > curbuf->b_ml.ml_line_count
6743 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 return FAIL;
6745 if (lnum + n >= curbuf->b_ml.ml_line_count)
6746 lnum = curbuf->b_ml.ml_line_count;
6747 else
6748#ifdef FEAT_FOLDING
6749 if (hasAnyFolding(curwin))
6750 {
6751 linenr_T last;
6752
6753 /* count each sequence of folded lines as one logical line */
6754 while (n--)
6755 {
6756 if (hasFolding(lnum, NULL, &last))
6757 lnum = last + 1;
6758 else
6759 ++lnum;
6760 if (lnum >= curbuf->b_ml.ml_line_count)
6761 break;
6762 }
6763 if (lnum > curbuf->b_ml.ml_line_count)
6764 lnum = curbuf->b_ml.ml_line_count;
6765 }
6766 else
6767#endif
6768 lnum += n;
6769 curwin->w_cursor.lnum = lnum;
6770 }
6771
6772 /* try to advance to the column we want to be at */
6773 coladvance(curwin->w_curswant);
6774
6775 if (upd_topline)
6776 update_topline(); /* make sure curwin->w_topline is valid */
6777
6778 return OK;
6779}
6780
6781/*
6782 * Stuff the last inserted text in the read buffer.
6783 * Last_insert actually is a copy of the redo buffer, so we
6784 * first have to remove the command.
6785 */
6786 int
6787stuff_inserted(c, count, no_esc)
6788 int c; /* Command character to be inserted */
6789 long count; /* Repeat this many times */
6790 int no_esc; /* Don't add an ESC at the end */
6791{
6792 char_u *esc_ptr;
6793 char_u *ptr;
6794 char_u *last_ptr;
6795 char_u last = NUL;
6796
6797 ptr = get_last_insert();
6798 if (ptr == NULL)
6799 {
6800 EMSG(_(e_noinstext));
6801 return FAIL;
6802 }
6803
6804 /* may want to stuff the command character, to start Insert mode */
6805 if (c != NUL)
6806 stuffcharReadbuff(c);
6807 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6808 *esc_ptr = NUL; /* remove the ESC */
6809
6810 /* when the last char is either "0" or "^" it will be quoted if no ESC
6811 * comes after it OR if it will inserted more than once and "ptr"
6812 * starts with ^D. -- Acevedo
6813 */
6814 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6815 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6816 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6817 {
6818 last = *last_ptr;
6819 *last_ptr = NUL;
6820 }
6821
6822 do
6823 {
6824 stuffReadbuff(ptr);
6825 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6826 if (last)
6827 stuffReadbuff((char_u *)(last == '0'
6828 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6829 : IF_EB("\026^", CTRL_V_STR "^")));
6830 }
6831 while (--count > 0);
6832
6833 if (last)
6834 *last_ptr = last;
6835
6836 if (esc_ptr != NULL)
6837 *esc_ptr = ESC; /* put the ESC back */
6838
6839 /* may want to stuff a trailing ESC, to get out of Insert mode */
6840 if (!no_esc)
6841 stuffcharReadbuff(ESC);
6842
6843 return OK;
6844}
6845
6846 char_u *
6847get_last_insert()
6848{
6849 if (last_insert == NULL)
6850 return NULL;
6851 return last_insert + last_insert_skip;
6852}
6853
6854/*
6855 * Get last inserted string, and remove trailing <Esc>.
6856 * Returns pointer to allocated memory (must be freed) or NULL.
6857 */
6858 char_u *
6859get_last_insert_save()
6860{
6861 char_u *s;
6862 int len;
6863
6864 if (last_insert == NULL)
6865 return NULL;
6866 s = vim_strsave(last_insert + last_insert_skip);
6867 if (s != NULL)
6868 {
6869 len = (int)STRLEN(s);
6870 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6871 s[len - 1] = NUL;
6872 }
6873 return s;
6874}
6875
6876/*
6877 * Check the word in front of the cursor for an abbreviation.
6878 * Called when the non-id character "c" has been entered.
6879 * When an abbreviation is recognized it is removed from the text and
6880 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6881 */
6882 static int
6883echeck_abbr(c)
6884 int c;
6885{
6886 /* Don't check for abbreviation in paste mode, when disabled and just
6887 * after moving around with cursor keys. */
6888 if (p_paste || no_abbr || arrow_used)
6889 return FALSE;
6890
6891 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6892 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6893}
6894
6895/*
6896 * replace-stack functions
6897 *
6898 * When replacing characters, the replaced characters are remembered for each
6899 * new character. This is used to re-insert the old text when backspacing.
6900 *
6901 * There is a NUL headed list of characters for each character that is
6902 * currently in the file after the insertion point. When BS is used, one NUL
6903 * headed list is put back for the deleted character.
6904 *
6905 * For a newline, there are two NUL headed lists. One contains the characters
6906 * that the NL replaced. The extra one stores the characters after the cursor
6907 * that were deleted (always white space).
6908 *
6909 * Replace_offset is normally 0, in which case replace_push will add a new
6910 * character at the end of the stack. If replace_offset is not 0, that many
6911 * characters will be left on the stack above the newly inserted character.
6912 */
6913
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006914static char_u *replace_stack = NULL;
6915static long replace_stack_nr = 0; /* next entry in replace stack */
6916static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917
6918 void
6919replace_push(c)
6920 int c; /* character that is replaced (NUL is none) */
6921{
6922 char_u *p;
6923
6924 if (replace_stack_nr < replace_offset) /* nothing to do */
6925 return;
6926 if (replace_stack_len <= replace_stack_nr)
6927 {
6928 replace_stack_len += 50;
6929 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6930 if (p == NULL) /* out of memory */
6931 {
6932 replace_stack_len -= 50;
6933 return;
6934 }
6935 if (replace_stack != NULL)
6936 {
6937 mch_memmove(p, replace_stack,
6938 (size_t)(replace_stack_nr * sizeof(char_u)));
6939 vim_free(replace_stack);
6940 }
6941 replace_stack = p;
6942 }
6943 p = replace_stack + replace_stack_nr - replace_offset;
6944 if (replace_offset)
6945 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6946 *p = c;
6947 ++replace_stack_nr;
6948}
6949
Bram Moolenaar2c994e82008-01-02 16:49:36 +00006950#if defined(FEAT_MBYTE) || defined(PROTO)
6951/*
6952 * Push a character onto the replace stack. Handles a multi-byte character in
6953 * reverse byte order, so that the first byte is popped off first.
6954 * Return the number of bytes done (includes composing characters).
6955 */
6956 int
6957replace_push_mb(p)
6958 char_u *p;
6959{
6960 int l = (*mb_ptr2len)(p);
6961 int j;
6962
6963 for (j = l - 1; j >= 0; --j)
6964 replace_push(p[j]);
6965 return l;
6966}
6967#endif
6968
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006969#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970/*
6971 * call replace_push(c) with replace_offset set to the first NUL.
6972 */
6973 static void
6974replace_push_off(c)
6975 int c;
6976{
6977 char_u *p;
6978
6979 p = replace_stack + replace_stack_nr;
6980 for (replace_offset = 1; replace_offset < replace_stack_nr;
6981 ++replace_offset)
6982 if (*--p == NUL)
6983 break;
6984 replace_push(c);
6985 replace_offset = 0;
6986}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988
6989/*
6990 * Pop one item from the replace stack.
6991 * return -1 if stack empty
6992 * return replaced character or NUL otherwise
6993 */
6994 static int
6995replace_pop()
6996{
6997 if (replace_stack_nr == 0)
6998 return -1;
6999 return (int)replace_stack[--replace_stack_nr];
7000}
7001
7002/*
7003 * Join the top two items on the replace stack. This removes to "off"'th NUL
7004 * encountered.
7005 */
7006 static void
7007replace_join(off)
7008 int off; /* offset for which NUL to remove */
7009{
7010 int i;
7011
7012 for (i = replace_stack_nr; --i >= 0; )
7013 if (replace_stack[i] == NUL && off-- <= 0)
7014 {
7015 --replace_stack_nr;
7016 mch_memmove(replace_stack + i, replace_stack + i + 1,
7017 (size_t)(replace_stack_nr - i));
7018 return;
7019 }
7020}
7021
7022/*
7023 * Pop bytes from the replace stack until a NUL is found, and insert them
7024 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7025 */
7026 static void
7027replace_pop_ins()
7028{
7029 int cc;
7030 int oldState = State;
7031
7032 State = NORMAL; /* don't want REPLACE here */
7033 while ((cc = replace_pop()) > 0)
7034 {
7035#ifdef FEAT_MBYTE
7036 mb_replace_pop_ins(cc);
7037#else
7038 ins_char(cc);
7039#endif
7040 dec_cursor();
7041 }
7042 State = oldState;
7043}
7044
7045#ifdef FEAT_MBYTE
7046/*
7047 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7048 * indicates a multi-byte char, pop the other bytes too.
7049 */
7050 static void
7051mb_replace_pop_ins(cc)
7052 int cc;
7053{
7054 int n;
7055 char_u buf[MB_MAXBYTES];
7056 int i;
7057 int c;
7058
7059 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7060 {
7061 buf[0] = cc;
7062 for (i = 1; i < n; ++i)
7063 buf[i] = replace_pop();
7064 ins_bytes_len(buf, n);
7065 }
7066 else
7067 ins_char(cc);
7068
7069 if (enc_utf8)
7070 /* Handle composing chars. */
7071 for (;;)
7072 {
7073 c = replace_pop();
7074 if (c == -1) /* stack empty */
7075 break;
7076 if ((n = MB_BYTE2LEN(c)) == 1)
7077 {
7078 /* Not a multi-byte char, put it back. */
7079 replace_push(c);
7080 break;
7081 }
7082 else
7083 {
7084 buf[0] = c;
7085 for (i = 1; i < n; ++i)
7086 buf[i] = replace_pop();
7087 if (utf_iscomposing(utf_ptr2char(buf)))
7088 ins_bytes_len(buf, n);
7089 else
7090 {
7091 /* Not a composing char, put it back. */
7092 for (i = n - 1; i >= 0; --i)
7093 replace_push(buf[i]);
7094 break;
7095 }
7096 }
7097 }
7098}
7099#endif
7100
7101/*
7102 * make the replace stack empty
7103 * (called when exiting replace mode)
7104 */
7105 static void
7106replace_flush()
7107{
7108 vim_free(replace_stack);
7109 replace_stack = NULL;
7110 replace_stack_len = 0;
7111 replace_stack_nr = 0;
7112}
7113
7114/*
7115 * Handle doing a BS for one character.
7116 * cc < 0: replace stack empty, just move cursor
7117 * cc == 0: character was inserted, delete it
7118 * cc > 0: character was replaced, put cc (first byte of original char) back
7119 * and check for more characters to be put back
7120 */
7121 static void
7122replace_do_bs()
7123{
7124 int cc;
7125#ifdef FEAT_VREPLACE
7126 int orig_len = 0;
7127 int ins_len;
7128 int orig_vcols = 0;
7129 colnr_T start_vcol;
7130 char_u *p;
7131 int i;
7132 int vcol;
7133#endif
7134
7135 cc = replace_pop();
7136 if (cc > 0)
7137 {
7138#ifdef FEAT_VREPLACE
7139 if (State & VREPLACE_FLAG)
7140 {
7141 /* Get the number of screen cells used by the character we are
7142 * going to delete. */
7143 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7144 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7145 }
7146#endif
7147#ifdef FEAT_MBYTE
7148 if (has_mbyte)
7149 {
7150 del_char(FALSE);
7151# ifdef FEAT_VREPLACE
7152 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007153 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154# endif
7155 replace_push(cc);
7156 }
7157 else
7158#endif
7159 {
7160 pchar_cursor(cc);
7161#ifdef FEAT_VREPLACE
7162 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007163 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164#endif
7165 }
7166 replace_pop_ins();
7167
7168#ifdef FEAT_VREPLACE
7169 if (State & VREPLACE_FLAG)
7170 {
7171 /* Get the number of screen cells used by the inserted characters */
7172 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007173 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 vcol = start_vcol;
7175 for (i = 0; i < ins_len; ++i)
7176 {
7177 vcol += chartabsize(p + i, vcol);
7178#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007179 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180#endif
7181 }
7182 vcol -= start_vcol;
7183
7184 /* Delete spaces that were inserted after the cursor to keep the
7185 * text aligned. */
7186 curwin->w_cursor.col += ins_len;
7187 while (vcol > orig_vcols && gchar_cursor() == ' ')
7188 {
7189 del_char(FALSE);
7190 ++orig_vcols;
7191 }
7192 curwin->w_cursor.col -= ins_len;
7193 }
7194#endif
7195
7196 /* mark the buffer as changed and prepare for displaying */
7197 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7198 }
7199 else if (cc == 0)
7200 (void)del_char(FALSE);
7201}
7202
7203#ifdef FEAT_CINDENT
7204/*
7205 * Return TRUE if C-indenting is on.
7206 */
7207 static int
7208cindent_on()
7209{
7210 return (!p_paste && (curbuf->b_p_cin
7211# ifdef FEAT_EVAL
7212 || *curbuf->b_p_inde != NUL
7213# endif
7214 ));
7215}
7216#endif
7217
7218#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7219/*
7220 * Re-indent the current line, based on the current contents of it and the
7221 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7222 * confused what all the part that handles Control-T is doing that I'm not.
7223 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7224 */
7225
7226 void
7227fixthisline(get_the_indent)
7228 int (*get_the_indent) __ARGS((void));
7229{
7230 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
7231 if (linewhite(curwin->w_cursor.lnum))
7232 did_ai = TRUE; /* delete the indent if the line stays empty */
7233}
7234
7235 void
7236fix_indent()
7237{
7238 if (p_paste)
7239 return;
7240# ifdef FEAT_LISP
7241 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7242 fixthisline(get_lisp_indent);
7243# endif
7244# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7245 else
7246# endif
7247# ifdef FEAT_CINDENT
7248 if (cindent_on())
7249 do_c_expr_indent();
7250# endif
7251}
7252
7253#endif
7254
7255#ifdef FEAT_CINDENT
7256/*
7257 * return TRUE if 'cinkeys' contains the key "keytyped",
7258 * when == '*': Only if key is preceded with '*' (indent before insert)
7259 * when == '!': Only if key is prededed with '!' (don't insert)
7260 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7261 *
7262 * "keytyped" can have a few special values:
7263 * KEY_OPEN_FORW
7264 * KEY_OPEN_BACK
7265 * KEY_COMPLETE just finished completion.
7266 *
7267 * If line_is_empty is TRUE accept keys with '0' before them.
7268 */
7269 int
7270in_cinkeys(keytyped, when, line_is_empty)
7271 int keytyped;
7272 int when;
7273 int line_is_empty;
7274{
7275 char_u *look;
7276 int try_match;
7277 int try_match_word;
7278 char_u *p;
7279 char_u *line;
7280 int icase;
7281 int i;
7282
7283#ifdef FEAT_EVAL
7284 if (*curbuf->b_p_inde != NUL)
7285 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7286 else
7287#endif
7288 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7289 while (*look)
7290 {
7291 /*
7292 * Find out if we want to try a match with this key, depending on
7293 * 'when' and a '*' or '!' before the key.
7294 */
7295 switch (when)
7296 {
7297 case '*': try_match = (*look == '*'); break;
7298 case '!': try_match = (*look == '!'); break;
7299 default: try_match = (*look != '*'); break;
7300 }
7301 if (*look == '*' || *look == '!')
7302 ++look;
7303
7304 /*
7305 * If there is a '0', only accept a match if the line is empty.
7306 * But may still match when typing last char of a word.
7307 */
7308 if (*look == '0')
7309 {
7310 try_match_word = try_match;
7311 if (!line_is_empty)
7312 try_match = FALSE;
7313 ++look;
7314 }
7315 else
7316 try_match_word = FALSE;
7317
7318 /*
7319 * does it look like a control character?
7320 */
7321 if (*look == '^'
7322#ifdef EBCDIC
7323 && (Ctrl_chr(look[1]) != 0)
7324#else
7325 && look[1] >= '?' && look[1] <= '_'
7326#endif
7327 )
7328 {
7329 if (try_match && keytyped == Ctrl_chr(look[1]))
7330 return TRUE;
7331 look += 2;
7332 }
7333 /*
7334 * 'o' means "o" command, open forward.
7335 * 'O' means "O" command, open backward.
7336 */
7337 else if (*look == 'o')
7338 {
7339 if (try_match && keytyped == KEY_OPEN_FORW)
7340 return TRUE;
7341 ++look;
7342 }
7343 else if (*look == 'O')
7344 {
7345 if (try_match && keytyped == KEY_OPEN_BACK)
7346 return TRUE;
7347 ++look;
7348 }
7349
7350 /*
7351 * 'e' means to check for "else" at start of line and just before the
7352 * cursor.
7353 */
7354 else if (*look == 'e')
7355 {
7356 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7357 {
7358 p = ml_get_curline();
7359 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7360 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7361 return TRUE;
7362 }
7363 ++look;
7364 }
7365
7366 /*
7367 * ':' only causes an indent if it is at the end of a label or case
7368 * statement, or when it was before typing the ':' (to fix
7369 * class::method for C++).
7370 */
7371 else if (*look == ':')
7372 {
7373 if (try_match && keytyped == ':')
7374 {
7375 p = ml_get_curline();
7376 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7377 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007378 /* Need to get the line again after cin_islabel(). */
7379 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 if (curwin->w_cursor.col > 2
7381 && p[curwin->w_cursor.col - 1] == ':'
7382 && p[curwin->w_cursor.col - 2] == ':')
7383 {
7384 p[curwin->w_cursor.col - 1] = ' ';
7385 i = (cin_iscase(p) || cin_isscopedecl(p)
7386 || cin_islabel(30));
7387 p = ml_get_curline();
7388 p[curwin->w_cursor.col - 1] = ':';
7389 if (i)
7390 return TRUE;
7391 }
7392 }
7393 ++look;
7394 }
7395
7396
7397 /*
7398 * Is it a key in <>, maybe?
7399 */
7400 else if (*look == '<')
7401 {
7402 if (try_match)
7403 {
7404 /*
7405 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7406 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7407 * >, *, : and ! keys if they really really want to.
7408 */
7409 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7410 && keytyped == look[1])
7411 return TRUE;
7412
7413 if (keytyped == get_special_key_code(look + 1))
7414 return TRUE;
7415 }
7416 while (*look && *look != '>')
7417 look++;
7418 while (*look == '>')
7419 look++;
7420 }
7421
7422 /*
7423 * Is it a word: "=word"?
7424 */
7425 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7426 {
7427 ++look;
7428 if (*look == '~')
7429 {
7430 icase = TRUE;
7431 ++look;
7432 }
7433 else
7434 icase = FALSE;
7435 p = vim_strchr(look, ',');
7436 if (p == NULL)
7437 p = look + STRLEN(look);
7438 if ((try_match || try_match_word)
7439 && curwin->w_cursor.col >= (colnr_T)(p - look))
7440 {
7441 int match = FALSE;
7442
7443#ifdef FEAT_INS_EXPAND
7444 if (keytyped == KEY_COMPLETE)
7445 {
7446 char_u *s;
7447
7448 /* Just completed a word, check if it starts with "look".
7449 * search back for the start of a word. */
7450 line = ml_get_curline();
7451# ifdef FEAT_MBYTE
7452 if (has_mbyte)
7453 {
7454 char_u *n;
7455
7456 for (s = line + curwin->w_cursor.col; s > line; s = n)
7457 {
7458 n = mb_prevptr(line, s);
7459 if (!vim_iswordp(n))
7460 break;
7461 }
7462 }
7463 else
7464# endif
7465 for (s = line + curwin->w_cursor.col; s > line; --s)
7466 if (!vim_iswordc(s[-1]))
7467 break;
7468 if (s + (p - look) <= line + curwin->w_cursor.col
7469 && (icase
7470 ? MB_STRNICMP(s, look, p - look)
7471 : STRNCMP(s, look, p - look)) == 0)
7472 match = TRUE;
7473 }
7474 else
7475#endif
7476 /* TODO: multi-byte */
7477 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7478 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7479 {
7480 line = ml_get_cursor();
7481 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7482 || !vim_iswordc(line[-(p - look) - 1]))
7483 && (icase
7484 ? MB_STRNICMP(line - (p - look), look, p - look)
7485 : STRNCMP(line - (p - look), look, p - look))
7486 == 0)
7487 match = TRUE;
7488 }
7489 if (match && try_match_word && !try_match)
7490 {
7491 /* "0=word": Check if there are only blanks before the
7492 * word. */
7493 line = ml_get_curline();
7494 if ((int)(skipwhite(line) - line) !=
7495 (int)(curwin->w_cursor.col - (p - look)))
7496 match = FALSE;
7497 }
7498 if (match)
7499 return TRUE;
7500 }
7501 look = p;
7502 }
7503
7504 /*
7505 * ok, it's a boring generic character.
7506 */
7507 else
7508 {
7509 if (try_match && *look == keytyped)
7510 return TRUE;
7511 ++look;
7512 }
7513
7514 /*
7515 * Skip over ", ".
7516 */
7517 look = skip_to_option_part(look);
7518 }
7519 return FALSE;
7520}
7521#endif /* FEAT_CINDENT */
7522
7523#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7524/*
7525 * Map Hebrew keyboard when in hkmap mode.
7526 */
7527 int
7528hkmap(c)
7529 int c;
7530{
7531 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7532 {
7533 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7534 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7535 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7536 static char_u map[26] =
7537 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7538 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7539 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7540 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7541 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7542 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7543 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7544 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7545 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7546
7547 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7548 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7549 /* '-1'='sofit' */
7550 else if (c == 'x')
7551 return 'X';
7552 else if (c == 'q')
7553 return '\''; /* {geresh}={'} */
7554 else if (c == 246)
7555 return ' '; /* \"o --> ' ' for a german keyboard */
7556 else if (c == 228)
7557 return ' '; /* \"a --> ' ' -- / -- */
7558 else if (c == 252)
7559 return ' '; /* \"u --> ' ' -- / -- */
7560#ifdef EBCDIC
7561 else if (islower(c))
7562#else
7563 /* NOTE: islower() does not do the right thing for us on Linux so we
7564 * do this the same was as 5.7 and previous, so it works correctly on
7565 * all systems. Specifically, the e.g. Delete and Arrow keys are
7566 * munged and won't work if e.g. searching for Hebrew text.
7567 */
7568 else if (c >= 'a' && c <= 'z')
7569#endif
7570 return (int)(map[CharOrdLow(c)] + p_aleph);
7571 else
7572 return c;
7573 }
7574 else
7575 {
7576 switch (c)
7577 {
7578 case '`': return ';';
7579 case '/': return '.';
7580 case '\'': return ',';
7581 case 'q': return '/';
7582 case 'w': return '\'';
7583
7584 /* Hebrew letters - set offset from 'a' */
7585 case ',': c = '{'; break;
7586 case '.': c = 'v'; break;
7587 case ';': c = 't'; break;
7588 default: {
7589 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7590
7591#ifdef EBCDIC
7592 /* see note about islower() above */
7593 if (!islower(c))
7594#else
7595 if (c < 'a' || c > 'z')
7596#endif
7597 return c;
7598 c = str[CharOrdLow(c)];
7599 break;
7600 }
7601 }
7602
7603 return (int)(CharOrdLow(c) + p_aleph);
7604 }
7605}
7606#endif
7607
7608 static void
7609ins_reg()
7610{
7611 int need_redraw = FALSE;
7612 int regname;
7613 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007614#ifdef FEAT_VISUAL
7615 int vis_active = VIsual_active;
7616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617
7618 /*
7619 * If we are going to wait for a character, show a '"'.
7620 */
7621 pc_status = PC_STATUS_UNSET;
7622 if (redrawing() && !char_avail())
7623 {
7624 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007625 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626
7627 edit_putchar('"', TRUE);
7628#ifdef FEAT_CMDL_INFO
7629 add_to_showcmd_c(Ctrl_R);
7630#endif
7631 }
7632
7633#ifdef USE_ON_FLY_SCROLL
7634 dont_scroll = TRUE; /* disallow scrolling here */
7635#endif
7636
7637 /*
7638 * Don't map the register name. This also prevents the mode message to be
7639 * deleted when ESC is hit.
7640 */
7641 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007642 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643#ifdef FEAT_LANGMAP
7644 LANGMAP_ADJUST(regname, TRUE);
7645#endif
7646 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7647 {
7648 /* Get a third key for literal register insertion */
7649 literally = regname;
7650#ifdef FEAT_CMDL_INFO
7651 add_to_showcmd_c(literally);
7652#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007653 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654#ifdef FEAT_LANGMAP
7655 LANGMAP_ADJUST(regname, TRUE);
7656#endif
7657 }
7658 --no_mapping;
7659
7660#ifdef FEAT_EVAL
7661 /*
7662 * Don't call u_sync() while getting the expression,
7663 * evaluating it or giving an error message for it!
7664 */
7665 ++no_u_sync;
7666 if (regname == '=')
7667 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007668# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007670# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007672# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 /* Restore the Input Method. */
7674 if (im_on)
7675 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007676# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007678 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7679 {
7680 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 else
7684 {
7685#endif
7686 if (literally == Ctrl_O || literally == Ctrl_P)
7687 {
7688 /* Append the command to the redo buffer. */
7689 AppendCharToRedobuff(Ctrl_R);
7690 AppendCharToRedobuff(literally);
7691 AppendCharToRedobuff(regname);
7692
7693 do_put(regname, BACKWARD, 1L,
7694 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7695 }
7696 else if (insert_reg(regname, literally) == FAIL)
7697 {
7698 vim_beep();
7699 need_redraw = TRUE; /* remove the '"' */
7700 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007701 else if (stop_insert_mode)
7702 /* When the '=' register was used and a function was invoked that
7703 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7704 * insert anything, need to remove the '"' */
7705 need_redraw = TRUE;
7706
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707#ifdef FEAT_EVAL
7708 }
7709 --no_u_sync;
7710#endif
7711#ifdef FEAT_CMDL_INFO
7712 clear_showcmd();
7713#endif
7714
7715 /* If the inserted register is empty, we need to remove the '"' */
7716 if (need_redraw || stuff_empty())
7717 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007718
7719#ifdef FEAT_VISUAL
7720 /* Disallow starting Visual mode here, would get a weird mode. */
7721 if (!vis_active && VIsual_active)
7722 end_visual_mode();
7723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724}
7725
7726/*
7727 * CTRL-G commands in Insert mode.
7728 */
7729 static void
7730ins_ctrl_g()
7731{
7732 int c;
7733
7734#ifdef FEAT_INS_EXPAND
7735 /* Right after CTRL-X the cursor will be after the ruler. */
7736 setcursor();
7737#endif
7738
7739 /*
7740 * Don't map the second key. This also prevents the mode message to be
7741 * deleted when ESC is hit.
7742 */
7743 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007744 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 --no_mapping;
7746 switch (c)
7747 {
7748 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7749 case K_UP:
7750 case Ctrl_K:
7751 case 'k': ins_up(TRUE);
7752 break;
7753
7754 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7755 case K_DOWN:
7756 case Ctrl_J:
7757 case 'j': ins_down(TRUE);
7758 break;
7759
7760 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007761 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007763
7764 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00007765 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007766 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 break;
7768
7769 /* Unknown CTRL-G command, reserved for future expansion. */
7770 default: vim_beep();
7771 }
7772}
7773
7774/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007775 * CTRL-^ in Insert mode.
7776 */
7777 static void
7778ins_ctrl_hat()
7779{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007780 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007781 {
7782 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7783 if (State & LANGMAP)
7784 {
7785 curbuf->b_p_iminsert = B_IMODE_NONE;
7786 State &= ~LANGMAP;
7787 }
7788 else
7789 {
7790 curbuf->b_p_iminsert = B_IMODE_LMAP;
7791 State |= LANGMAP;
7792#ifdef USE_IM_CONTROL
7793 im_set_active(FALSE);
7794#endif
7795 }
7796 }
7797#ifdef USE_IM_CONTROL
7798 else
7799 {
7800 /* There are no ":lmap" mappings, toggle IM */
7801 if (im_get_status())
7802 {
7803 curbuf->b_p_iminsert = B_IMODE_NONE;
7804 im_set_active(FALSE);
7805 }
7806 else
7807 {
7808 curbuf->b_p_iminsert = B_IMODE_IM;
7809 State &= ~LANGMAP;
7810 im_set_active(TRUE);
7811 }
7812 }
7813#endif
7814 set_iminsert_global();
7815 showmode();
7816#ifdef FEAT_GUI
7817 /* may show different cursor shape or color */
7818 if (gui.in_use)
7819 gui_update_cursor(TRUE, FALSE);
7820#endif
7821#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7822 /* Show/unshow value of 'keymap' in status lines. */
7823 status_redraw_curbuf();
7824#endif
7825}
7826
7827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 * Handle ESC in insert mode.
7829 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7830 * insert.
7831 */
7832 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007833ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 long *count;
7835 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007836 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837{
7838 int temp;
7839 static int disabled_redraw = FALSE;
7840
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007841#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007842 check_spell_redraw();
7843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844#if defined(FEAT_HANGULIN)
7845# if defined(ESC_CHG_TO_ENG_MODE)
7846 hangul_input_state_set(0);
7847# endif
7848 if (composing_hangul)
7849 {
7850 push_raw_key(composing_hangul_buffer, 2);
7851 composing_hangul = 0;
7852 }
7853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854
7855 temp = curwin->w_cursor.col;
7856 if (disabled_redraw)
7857 {
7858 --RedrawingDisabled;
7859 disabled_redraw = FALSE;
7860 }
7861 if (!arrow_used)
7862 {
7863 /*
7864 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007865 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7866 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 */
7868 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007869 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870
7871 /*
7872 * Repeating insert may take a long time. Check for
7873 * interrupt now and then.
7874 */
7875 if (*count > 0)
7876 {
7877 line_breakcheck();
7878 if (got_int)
7879 *count = 0;
7880 }
7881
7882 if (--*count > 0) /* repeat what was typed */
7883 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007884 /* Vi repeats the insert without replacing characters. */
7885 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7886 State &= ~REPLACE_FLAG;
7887
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 (void)start_redo_ins();
7889 if (cmdchar == 'r' || cmdchar == 'v')
7890 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7891 ++RedrawingDisabled;
7892 disabled_redraw = TRUE;
7893 return FALSE; /* repeat the insert */
7894 }
7895 stop_insert(&curwin->w_cursor, TRUE);
7896 undisplay_dollar();
7897 }
7898
7899 /* When an autoindent was removed, curswant stays after the
7900 * indent */
7901 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7902 curwin->w_set_curswant = TRUE;
7903
7904 /* Remember the last Insert position in the '^ mark. */
7905 if (!cmdmod.keepjumps)
7906 curbuf->b_last_insert = curwin->w_cursor;
7907
7908 /*
7909 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007910 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007912 if (!nomove
7913 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914#ifdef FEAT_VIRTUALEDIT
7915 || curwin->w_cursor.coladd > 0
7916#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007917 )
7918 && (restart_edit == NUL
7919 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007921 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007923 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924#ifdef FEAT_RIGHTLEFT
7925 && !revins_on
7926#endif
7927 )
7928 {
7929#ifdef FEAT_VIRTUALEDIT
7930 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7931 {
7932 oneleft();
7933 if (restart_edit != NUL)
7934 ++curwin->w_cursor.coladd;
7935 }
7936 else
7937#endif
7938 {
7939 --curwin->w_cursor.col;
7940#ifdef FEAT_MBYTE
7941 /* Correct cursor for multi-byte character. */
7942 if (has_mbyte)
7943 mb_adjust_cursor();
7944#endif
7945 }
7946 }
7947
7948#ifdef USE_IM_CONTROL
7949 /* Disable IM to allow typing English directly for Normal mode commands.
7950 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7951 * well). */
7952 if (!(State & LANGMAP))
7953 im_save_status(&curbuf->b_p_iminsert);
7954 im_set_active(FALSE);
7955#endif
7956
7957 State = NORMAL;
7958 /* need to position cursor again (e.g. when on a TAB ) */
7959 changed_cline_bef_curs();
7960
7961#ifdef FEAT_MOUSE
7962 setmouse();
7963#endif
7964#ifdef CURSOR_SHAPE
7965 ui_cursor_shape(); /* may show different cursor shape */
7966#endif
7967
7968 /*
7969 * When recording or for CTRL-O, need to display the new mode.
7970 * Otherwise remove the mode message.
7971 */
7972 if (Recording || restart_edit != NUL)
7973 showmode();
7974 else if (p_smd)
7975 MSG("");
7976
7977 return TRUE; /* exit Insert mode */
7978}
7979
7980#ifdef FEAT_RIGHTLEFT
7981/*
7982 * Toggle language: hkmap and revins_on.
7983 * Move to end of reverse inserted text.
7984 */
7985 static void
7986ins_ctrl_()
7987{
7988 if (revins_on && revins_chars && revins_scol >= 0)
7989 {
7990 while (gchar_cursor() != NUL && revins_chars--)
7991 ++curwin->w_cursor.col;
7992 }
7993 p_ri = !p_ri;
7994 revins_on = (State == INSERT && p_ri);
7995 if (revins_on)
7996 {
7997 revins_scol = curwin->w_cursor.col;
7998 revins_legal++;
7999 revins_chars = 0;
8000 undisplay_dollar();
8001 }
8002 else
8003 revins_scol = -1;
8004#ifdef FEAT_FKMAP
8005 if (p_altkeymap)
8006 {
8007 /*
8008 * to be consistent also for redo command, using '.'
8009 * set arrow_used to true and stop it - causing to redo
8010 * characters entered in one mode (normal/reverse insert).
8011 */
8012 arrow_used = TRUE;
8013 (void)stop_arrow();
8014 p_fkmap = curwin->w_p_rl ^ p_ri;
8015 if (p_fkmap && p_ri)
8016 State = INSERT;
8017 }
8018 else
8019#endif
8020 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8021 showmode();
8022}
8023#endif
8024
8025#ifdef FEAT_VISUAL
8026/*
8027 * If 'keymodel' contains "startsel", may start selection.
8028 * Returns TRUE when a CTRL-O and other keys stuffed.
8029 */
8030 static int
8031ins_start_select(c)
8032 int c;
8033{
8034 if (km_startsel)
8035 switch (c)
8036 {
8037 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 case K_PAGEUP:
8040 case K_KPAGEUP:
8041 case K_PAGEDOWN:
8042 case K_KPAGEDOWN:
8043# ifdef MACOS
8044 case K_LEFT:
8045 case K_RIGHT:
8046 case K_UP:
8047 case K_DOWN:
8048 case K_END:
8049 case K_HOME:
8050# endif
8051 if (!(mod_mask & MOD_MASK_SHIFT))
8052 break;
8053 /* FALLTHROUGH */
8054 case K_S_LEFT:
8055 case K_S_RIGHT:
8056 case K_S_UP:
8057 case K_S_DOWN:
8058 case K_S_END:
8059 case K_S_HOME:
8060 /* Start selection right away, the cursor can move with
8061 * CTRL-O when beyond the end of the line. */
8062 start_selection();
8063
8064 /* Execute the key in (insert) Select mode. */
8065 stuffcharReadbuff(Ctrl_O);
8066 if (mod_mask)
8067 {
8068 char_u buf[4];
8069
8070 buf[0] = K_SPECIAL;
8071 buf[1] = KS_MODIFIER;
8072 buf[2] = mod_mask;
8073 buf[3] = NUL;
8074 stuffReadbuff(buf);
8075 }
8076 stuffcharReadbuff(c);
8077 return TRUE;
8078 }
8079 return FALSE;
8080}
8081#endif
8082
8083/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008084 * <Insert> key in Insert mode: toggle insert/remplace mode.
8085 */
8086 static void
8087ins_insert(replaceState)
8088 int replaceState;
8089{
8090#ifdef FEAT_FKMAP
8091 if (p_fkmap && p_ri)
8092 {
8093 beep_flush();
8094 EMSG(farsi_text_3); /* encoded in Farsi */
8095 return;
8096 }
8097#endif
8098
8099#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008100# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008101 set_vim_var_string(VV_INSERTMODE,
8102 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008103# ifdef FEAT_VREPLACE
8104 replaceState == VREPLACE ? "v" :
8105# endif
8106 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008107# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008108 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8109#endif
8110 if (State & REPLACE_FLAG)
8111 State = INSERT | (State & LANGMAP);
8112 else
8113 State = replaceState | (State & LANGMAP);
8114 AppendCharToRedobuff(K_INS);
8115 showmode();
8116#ifdef CURSOR_SHAPE
8117 ui_cursor_shape(); /* may show different cursor shape */
8118#endif
8119}
8120
8121/*
8122 * Pressed CTRL-O in Insert mode.
8123 */
8124 static void
8125ins_ctrl_o()
8126{
8127#ifdef FEAT_VREPLACE
8128 if (State & VREPLACE_FLAG)
8129 restart_edit = 'V';
8130 else
8131#endif
8132 if (State & REPLACE_FLAG)
8133 restart_edit = 'R';
8134 else
8135 restart_edit = 'I';
8136#ifdef FEAT_VIRTUALEDIT
8137 if (virtual_active())
8138 ins_at_eol = FALSE; /* cursor always keeps its column */
8139 else
8140#endif
8141 ins_at_eol = (gchar_cursor() == NUL);
8142}
8143
8144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 * If the cursor is on an indent, ^T/^D insert/delete one
8146 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
8147 * Always round the indent to 'shiftwith', this is compatible
8148 * with vi. But vi only supports ^T and ^D after an
8149 * autoindent, we support it everywhere.
8150 */
8151 static void
8152ins_shift(c, lastc)
8153 int c;
8154 int lastc;
8155{
8156 if (stop_arrow() == FAIL)
8157 return;
8158 AppendCharToRedobuff(c);
8159
8160 /*
8161 * 0^D and ^^D: remove all indent.
8162 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008163 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8164 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {
8166 --curwin->w_cursor.col;
8167 (void)del_char(FALSE); /* delete the '^' or '0' */
8168 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8169 if (State & REPLACE_FLAG)
8170 replace_pop_ins();
8171 if (lastc == '^')
8172 old_indent = get_indent(); /* remember curr. indent */
8173 change_indent(INDENT_SET, 0, TRUE, 0);
8174 }
8175 else
8176 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
8177
8178 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8179 did_ai = FALSE;
8180#ifdef FEAT_SMARTINDENT
8181 did_si = FALSE;
8182 can_si = FALSE;
8183 can_si_back = FALSE;
8184#endif
8185#ifdef FEAT_CINDENT
8186 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8187#endif
8188}
8189
8190 static void
8191ins_del()
8192{
8193 int temp;
8194
8195 if (stop_arrow() == FAIL)
8196 return;
8197 if (gchar_cursor() == NUL) /* delete newline */
8198 {
8199 temp = curwin->w_cursor.col;
8200 if (!can_bs(BS_EOL) /* only if "eol" included */
8201 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8202 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8203 || do_join(FALSE) == FAIL)
8204 vim_beep();
8205 else
8206 curwin->w_cursor.col = temp;
8207 }
8208 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8209 vim_beep();
8210 did_ai = FALSE;
8211#ifdef FEAT_SMARTINDENT
8212 did_si = FALSE;
8213 can_si = FALSE;
8214 can_si_back = FALSE;
8215#endif
8216 AppendCharToRedobuff(K_DEL);
8217}
8218
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008219static void ins_bs_one __ARGS((colnr_T *vcolp));
8220
8221/*
8222 * Delete one character for ins_bs().
8223 */
8224 static void
8225ins_bs_one(vcolp)
8226 colnr_T *vcolp;
8227{
8228 dec_cursor();
8229 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8230 if (State & REPLACE_FLAG)
8231 {
8232 /* Don't delete characters before the insert point when in
8233 * Replace mode */
8234 if (curwin->w_cursor.lnum != Insstart.lnum
8235 || curwin->w_cursor.col >= Insstart.col)
8236 replace_do_bs();
8237 }
8238 else
8239 (void)del_char(FALSE);
8240}
8241
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242/*
8243 * Handle Backspace, delete-word and delete-line in Insert mode.
8244 * Return TRUE when backspace was actually used.
8245 */
8246 static int
8247ins_bs(c, mode, inserted_space_p)
8248 int c;
8249 int mode;
8250 int *inserted_space_p;
8251{
8252 linenr_T lnum;
8253 int cc;
8254 int temp = 0; /* init for GCC */
8255 colnr_T mincol;
8256 int did_backspace = FALSE;
8257 int in_indent;
8258 int oldState;
8259#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008260 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261#endif
8262
8263 /*
8264 * can't delete anything in an empty file
8265 * can't backup past first character in buffer
8266 * can't backup past starting point unless 'backspace' > 1
8267 * can backup to a previous line if 'backspace' == 0
8268 */
8269 if ( bufempty()
8270 || (
8271#ifdef FEAT_RIGHTLEFT
8272 !revins_on &&
8273#endif
8274 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8275 || (!can_bs(BS_START)
8276 && (arrow_used
8277 || (curwin->w_cursor.lnum == Insstart.lnum
8278 && curwin->w_cursor.col <= Insstart.col)))
8279 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8280 && curwin->w_cursor.col <= ai_col)
8281 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8282 {
8283 vim_beep();
8284 return FALSE;
8285 }
8286
8287 if (stop_arrow() == FAIL)
8288 return FALSE;
8289 in_indent = inindent(0);
8290#ifdef FEAT_CINDENT
8291 if (in_indent)
8292 can_cindent = FALSE;
8293#endif
8294#ifdef FEAT_COMMENTS
8295 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8296#endif
8297#ifdef FEAT_RIGHTLEFT
8298 if (revins_on) /* put cursor after last inserted char */
8299 inc_cursor();
8300#endif
8301
8302#ifdef FEAT_VIRTUALEDIT
8303 /* Virtualedit:
8304 * BACKSPACE_CHAR eats a virtual space
8305 * BACKSPACE_WORD eats all coladd
8306 * BACKSPACE_LINE eats all coladd and keeps going
8307 */
8308 if (curwin->w_cursor.coladd > 0)
8309 {
8310 if (mode == BACKSPACE_CHAR)
8311 {
8312 --curwin->w_cursor.coladd;
8313 return TRUE;
8314 }
8315 if (mode == BACKSPACE_WORD)
8316 {
8317 curwin->w_cursor.coladd = 0;
8318 return TRUE;
8319 }
8320 curwin->w_cursor.coladd = 0;
8321 }
8322#endif
8323
8324 /*
8325 * delete newline!
8326 */
8327 if (curwin->w_cursor.col == 0)
8328 {
8329 lnum = Insstart.lnum;
8330 if (curwin->w_cursor.lnum == Insstart.lnum
8331#ifdef FEAT_RIGHTLEFT
8332 || revins_on
8333#endif
8334 )
8335 {
8336 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8337 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8338 return FALSE;
8339 --Insstart.lnum;
8340 Insstart.col = MAXCOL;
8341 }
8342 /*
8343 * In replace mode:
8344 * cc < 0: NL was inserted, delete it
8345 * cc >= 0: NL was replaced, put original characters back
8346 */
8347 cc = -1;
8348 if (State & REPLACE_FLAG)
8349 cc = replace_pop(); /* returns -1 if NL was inserted */
8350 /*
8351 * In replace mode, in the line we started replacing, we only move the
8352 * cursor.
8353 */
8354 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8355 {
8356 dec_cursor();
8357 }
8358 else
8359 {
8360#ifdef FEAT_VREPLACE
8361 if (!(State & VREPLACE_FLAG)
8362 || curwin->w_cursor.lnum > orig_line_count)
8363#endif
8364 {
8365 temp = gchar_cursor(); /* remember current char */
8366 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008367
8368 /* When "aw" is in 'formatoptions' we must delete the space at
8369 * the end of the line, otherwise the line will be broken
8370 * again when auto-formatting. */
8371 if (has_format_option(FO_AUTO)
8372 && has_format_option(FO_WHITE_PAR))
8373 {
8374 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8375 TRUE);
8376 int len;
8377
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008378 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008379 if (len > 0 && ptr[len - 1] == ' ')
8380 ptr[len - 1] = NUL;
8381 }
8382
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 (void)do_join(FALSE);
8384 if (temp == NUL && gchar_cursor() != NUL)
8385 inc_cursor();
8386 }
8387#ifdef FEAT_VREPLACE
8388 else
8389 dec_cursor();
8390#endif
8391
8392 /*
8393 * In REPLACE mode we have to put back the text that was replaced
8394 * by the NL. On the replace stack is first a NUL-terminated
8395 * sequence of characters that were deleted and then the
8396 * characters that NL replaced.
8397 */
8398 if (State & REPLACE_FLAG)
8399 {
8400 /*
8401 * Do the next ins_char() in NORMAL state, to
8402 * prevent ins_char() from replacing characters and
8403 * avoiding showmatch().
8404 */
8405 oldState = State;
8406 State = NORMAL;
8407 /*
8408 * restore characters (blanks) deleted after cursor
8409 */
8410 while (cc > 0)
8411 {
8412 temp = curwin->w_cursor.col;
8413#ifdef FEAT_MBYTE
8414 mb_replace_pop_ins(cc);
8415#else
8416 ins_char(cc);
8417#endif
8418 curwin->w_cursor.col = temp;
8419 cc = replace_pop();
8420 }
8421 /* restore the characters that NL replaced */
8422 replace_pop_ins();
8423 State = oldState;
8424 }
8425 }
8426 did_ai = FALSE;
8427 }
8428 else
8429 {
8430 /*
8431 * Delete character(s) before the cursor.
8432 */
8433#ifdef FEAT_RIGHTLEFT
8434 if (revins_on) /* put cursor on last inserted char */
8435 dec_cursor();
8436#endif
8437 mincol = 0;
8438 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008439 if (mode == BACKSPACE_LINE
8440 && (curbuf->b_p_ai
8441#ifdef FEAT_CINDENT
8442 || cindent_on()
8443#endif
8444 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445#ifdef FEAT_RIGHTLEFT
8446 && !revins_on
8447#endif
8448 )
8449 {
8450 temp = curwin->w_cursor.col;
8451 beginline(BL_WHITE);
8452 if (curwin->w_cursor.col < (colnr_T)temp)
8453 mincol = curwin->w_cursor.col;
8454 curwin->w_cursor.col = temp;
8455 }
8456
8457 /*
8458 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8459 */
8460 if ( mode == BACKSPACE_CHAR
8461 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008462 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008463 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 && (*(ml_get_cursor() - 1) == TAB
8465 || (*(ml_get_cursor() - 1) == ' '
8466 && (!*inserted_space_p
8467 || arrow_used))))))
8468 {
8469 int ts;
8470 colnr_T vcol;
8471 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008472 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473
8474 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008475 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 ts = curbuf->b_p_sw;
8477 else
8478 ts = curbuf->b_p_sts;
8479 /* Compute the virtual column where we want to be. Since
8480 * 'showbreak' may get in the way, need to get the last column of
8481 * the previous character. */
8482 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008483 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 dec_cursor();
8485 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8486 inc_cursor();
8487 want_vcol = (want_vcol / ts) * ts;
8488
8489 /* delete characters until we are at or before want_vcol */
8490 while (vcol > want_vcol
8491 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008492 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493
8494 /* insert extra spaces until we are at want_vcol */
8495 while (vcol < want_vcol)
8496 {
8497 /* Remember the first char we inserted */
8498 if (curwin->w_cursor.lnum == Insstart.lnum
8499 && curwin->w_cursor.col < Insstart.col)
8500 Insstart.col = curwin->w_cursor.col;
8501
8502#ifdef FEAT_VREPLACE
8503 if (State & VREPLACE_FLAG)
8504 ins_char(' ');
8505 else
8506#endif
8507 {
8508 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008509 if ((State & REPLACE_FLAG))
8510 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 }
8512 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8513 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008514
8515 /* If we are now back where we started delete one character. Can
8516 * happen when using 'sts' and 'linebreak'. */
8517 if (vcol >= start_vcol)
8518 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 }
8520
8521 /*
8522 * Delete upto starting point, start of line or previous word.
8523 */
8524 else do
8525 {
8526#ifdef FEAT_RIGHTLEFT
8527 if (!revins_on) /* put cursor on char to be deleted */
8528#endif
8529 dec_cursor();
8530
8531 /* start of word? */
8532 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8533 {
8534 mode = BACKSPACE_WORD_NOT_SPACE;
8535 temp = vim_iswordc(gchar_cursor());
8536 }
8537 /* end of word? */
8538 else if (mode == BACKSPACE_WORD_NOT_SPACE
8539 && (vim_isspace(cc = gchar_cursor())
8540 || vim_iswordc(cc) != temp))
8541 {
8542#ifdef FEAT_RIGHTLEFT
8543 if (!revins_on)
8544#endif
8545 inc_cursor();
8546#ifdef FEAT_RIGHTLEFT
8547 else if (State & REPLACE_FLAG)
8548 dec_cursor();
8549#endif
8550 break;
8551 }
8552 if (State & REPLACE_FLAG)
8553 replace_do_bs();
8554 else
8555 {
8556#ifdef FEAT_MBYTE
8557 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008558 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559#endif
8560 (void)del_char(FALSE);
8561#ifdef FEAT_MBYTE
8562 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008563 * If there are combining characters and 'delcombine' is set
8564 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 * character.
8566 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008567 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 inc_cursor();
8569#endif
8570#ifdef FEAT_RIGHTLEFT
8571 if (revins_chars)
8572 {
8573 revins_chars--;
8574 revins_legal++;
8575 }
8576 if (revins_on && gchar_cursor() == NUL)
8577 break;
8578#endif
8579 }
8580 /* Just a single backspace?: */
8581 if (mode == BACKSPACE_CHAR)
8582 break;
8583 } while (
8584#ifdef FEAT_RIGHTLEFT
8585 revins_on ||
8586#endif
8587 (curwin->w_cursor.col > mincol
8588 && (curwin->w_cursor.lnum != Insstart.lnum
8589 || curwin->w_cursor.col != Insstart.col)));
8590 did_backspace = TRUE;
8591 }
8592#ifdef FEAT_SMARTINDENT
8593 did_si = FALSE;
8594 can_si = FALSE;
8595 can_si_back = FALSE;
8596#endif
8597 if (curwin->w_cursor.col <= 1)
8598 did_ai = FALSE;
8599 /*
8600 * It's a little strange to put backspaces into the redo
8601 * buffer, but it makes auto-indent a lot easier to deal
8602 * with.
8603 */
8604 AppendCharToRedobuff(c);
8605
8606 /* If deleted before the insertion point, adjust it */
8607 if (curwin->w_cursor.lnum == Insstart.lnum
8608 && curwin->w_cursor.col < Insstart.col)
8609 Insstart.col = curwin->w_cursor.col;
8610
8611 /* vi behaviour: the cursor moves backward but the character that
8612 * was there remains visible
8613 * Vim behaviour: the cursor moves backward and the character that
8614 * was there is erased from the screen.
8615 * We can emulate the vi behaviour by pretending there is a dollar
8616 * displayed even when there isn't.
8617 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8618 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8619 dollar_vcol = curwin->w_virtcol;
8620
8621 return did_backspace;
8622}
8623
8624#ifdef FEAT_MOUSE
8625 static void
8626ins_mouse(c)
8627 int c;
8628{
8629 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008630 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631
8632# ifdef FEAT_GUI
8633 /* When GUI is active, also move/paste when 'mouse' is empty */
8634 if (!gui.in_use)
8635# endif
8636 if (!mouse_has(MOUSE_INSERT))
8637 return;
8638
8639 undisplay_dollar();
8640 tpos = curwin->w_cursor;
8641 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8642 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008643#ifdef FEAT_WINDOWS
8644 win_T *new_curwin = curwin;
8645
8646 if (curwin != old_curwin && win_valid(old_curwin))
8647 {
8648 /* Mouse took us to another window. We need to go back to the
8649 * previous one to stop insert there properly. */
8650 curwin = old_curwin;
8651 curbuf = curwin->w_buffer;
8652 }
8653#endif
8654 start_arrow(curwin == old_curwin ? &tpos : NULL);
8655#ifdef FEAT_WINDOWS
8656 if (curwin != new_curwin && win_valid(new_curwin))
8657 {
8658 curwin = new_curwin;
8659 curbuf = curwin->w_buffer;
8660 }
8661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662# ifdef FEAT_CINDENT
8663 can_cindent = TRUE;
8664# endif
8665 }
8666
8667#ifdef FEAT_WINDOWS
8668 /* redraw status lines (in case another window became active) */
8669 redraw_statuslines();
8670#endif
8671}
8672
8673 static void
8674ins_mousescroll(up)
8675 int up;
8676{
8677 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008678# if defined(FEAT_WINDOWS)
8679 win_T *old_curwin = curwin;
8680# endif
8681# ifdef FEAT_INS_EXPAND
8682 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683# endif
8684
8685 tpos = curwin->w_cursor;
8686
8687# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 /* Currently the mouse coordinates are only known in the GUI. */
8689 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8690 {
8691 int row, col;
8692
8693 row = mouse_row;
8694 col = mouse_col;
8695
8696 /* find the window at the pointer coordinates */
8697 curwin = mouse_find_win(&row, &col);
8698 curbuf = curwin->w_buffer;
8699 }
8700 if (curwin == old_curwin)
8701# endif
8702 undisplay_dollar();
8703
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008704# ifdef FEAT_INS_EXPAND
8705 /* Don't scroll the window in which completion is being done. */
8706 if (!pum_visible()
8707# if defined(FEAT_WINDOWS)
8708 || curwin != old_curwin
8709# endif
8710 )
8711# endif
8712 {
8713 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8714 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8715 else
8716 scroll_redraw(up, 3L);
8717# ifdef FEAT_INS_EXPAND
8718 did_scroll = TRUE;
8719# endif
8720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721
8722# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8723 curwin->w_redr_status = TRUE;
8724
8725 curwin = old_curwin;
8726 curbuf = curwin->w_buffer;
8727# endif
8728
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008729# ifdef FEAT_INS_EXPAND
8730 /* The popup menu may overlay the window, need to redraw it.
8731 * TODO: Would be more efficient to only redraw the windows that are
8732 * overlapped by the popup menu. */
8733 if (pum_visible() && did_scroll)
8734 {
8735 redraw_all_later(NOT_VALID);
8736 ins_compl_show_pum();
8737 }
8738# endif
8739
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 if (!equalpos(curwin->w_cursor, tpos))
8741 {
8742 start_arrow(&tpos);
8743# ifdef FEAT_CINDENT
8744 can_cindent = TRUE;
8745# endif
8746 }
8747}
8748#endif
8749
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008750#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008751 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008752ins_tabline(c)
8753 int c;
8754{
8755 /* We will be leaving the current window, unless closing another tab. */
8756 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8757 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8758 {
8759 undisplay_dollar();
8760 start_arrow(&curwin->w_cursor);
8761# ifdef FEAT_CINDENT
8762 can_cindent = TRUE;
8763# endif
8764 }
8765
8766 if (c == K_TABLINE)
8767 goto_tabpage(current_tab);
8768 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008769 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008770 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008771 redraw_statuslines(); /* will redraw the tabline when needed */
8772 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008773}
8774#endif
8775
8776#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 void
8778ins_scroll()
8779{
8780 pos_T tpos;
8781
8782 undisplay_dollar();
8783 tpos = curwin->w_cursor;
8784 if (gui_do_scroll())
8785 {
8786 start_arrow(&tpos);
8787# ifdef FEAT_CINDENT
8788 can_cindent = TRUE;
8789# endif
8790 }
8791}
8792
8793 void
8794ins_horscroll()
8795{
8796 pos_T tpos;
8797
8798 undisplay_dollar();
8799 tpos = curwin->w_cursor;
8800 if (gui_do_horiz_scroll())
8801 {
8802 start_arrow(&tpos);
8803# ifdef FEAT_CINDENT
8804 can_cindent = TRUE;
8805# endif
8806 }
8807}
8808#endif
8809
8810 static void
8811ins_left()
8812{
8813 pos_T tpos;
8814
8815#ifdef FEAT_FOLDING
8816 if ((fdo_flags & FDO_HOR) && KeyTyped)
8817 foldOpenCursor();
8818#endif
8819 undisplay_dollar();
8820 tpos = curwin->w_cursor;
8821 if (oneleft() == OK)
8822 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00008823#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8824 /* Only call start_arrow() when not busy with preediting, it will
8825 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8826 if (!im_is_preediting())
8827#endif
8828 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829#ifdef FEAT_RIGHTLEFT
8830 /* If exit reversed string, position is fixed */
8831 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8832 revins_legal++;
8833 revins_chars++;
8834#endif
8835 }
8836
8837 /*
8838 * if 'whichwrap' set for cursor in insert mode may go to
8839 * previous line
8840 */
8841 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8842 {
8843 start_arrow(&tpos);
8844 --(curwin->w_cursor.lnum);
8845 coladvance((colnr_T)MAXCOL);
8846 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8847 }
8848 else
8849 vim_beep();
8850}
8851
8852 static void
8853ins_home(c)
8854 int c;
8855{
8856 pos_T tpos;
8857
8858#ifdef FEAT_FOLDING
8859 if ((fdo_flags & FDO_HOR) && KeyTyped)
8860 foldOpenCursor();
8861#endif
8862 undisplay_dollar();
8863 tpos = curwin->w_cursor;
8864 if (c == K_C_HOME)
8865 curwin->w_cursor.lnum = 1;
8866 curwin->w_cursor.col = 0;
8867#ifdef FEAT_VIRTUALEDIT
8868 curwin->w_cursor.coladd = 0;
8869#endif
8870 curwin->w_curswant = 0;
8871 start_arrow(&tpos);
8872}
8873
8874 static void
8875ins_end(c)
8876 int c;
8877{
8878 pos_T tpos;
8879
8880#ifdef FEAT_FOLDING
8881 if ((fdo_flags & FDO_HOR) && KeyTyped)
8882 foldOpenCursor();
8883#endif
8884 undisplay_dollar();
8885 tpos = curwin->w_cursor;
8886 if (c == K_C_END)
8887 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8888 coladvance((colnr_T)MAXCOL);
8889 curwin->w_curswant = MAXCOL;
8890
8891 start_arrow(&tpos);
8892}
8893
8894 static void
8895ins_s_left()
8896{
8897#ifdef FEAT_FOLDING
8898 if ((fdo_flags & FDO_HOR) && KeyTyped)
8899 foldOpenCursor();
8900#endif
8901 undisplay_dollar();
8902 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8903 {
8904 start_arrow(&curwin->w_cursor);
8905 (void)bck_word(1L, FALSE, FALSE);
8906 curwin->w_set_curswant = TRUE;
8907 }
8908 else
8909 vim_beep();
8910}
8911
8912 static void
8913ins_right()
8914{
8915#ifdef FEAT_FOLDING
8916 if ((fdo_flags & FDO_HOR) && KeyTyped)
8917 foldOpenCursor();
8918#endif
8919 undisplay_dollar();
8920 if (gchar_cursor() != NUL || virtual_active()
8921 )
8922 {
8923 start_arrow(&curwin->w_cursor);
8924 curwin->w_set_curswant = TRUE;
8925#ifdef FEAT_VIRTUALEDIT
8926 if (virtual_active())
8927 oneright();
8928 else
8929#endif
8930 {
8931#ifdef FEAT_MBYTE
8932 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008933 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 else
8935#endif
8936 ++curwin->w_cursor.col;
8937 }
8938
8939#ifdef FEAT_RIGHTLEFT
8940 revins_legal++;
8941 if (revins_chars)
8942 revins_chars--;
8943#endif
8944 }
8945 /* if 'whichwrap' set for cursor in insert mode, may move the
8946 * cursor to the next line */
8947 else if (vim_strchr(p_ww, ']') != NULL
8948 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8949 {
8950 start_arrow(&curwin->w_cursor);
8951 curwin->w_set_curswant = TRUE;
8952 ++curwin->w_cursor.lnum;
8953 curwin->w_cursor.col = 0;
8954 }
8955 else
8956 vim_beep();
8957}
8958
8959 static void
8960ins_s_right()
8961{
8962#ifdef FEAT_FOLDING
8963 if ((fdo_flags & FDO_HOR) && KeyTyped)
8964 foldOpenCursor();
8965#endif
8966 undisplay_dollar();
8967 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8968 || gchar_cursor() != NUL)
8969 {
8970 start_arrow(&curwin->w_cursor);
8971 (void)fwd_word(1L, FALSE, 0);
8972 curwin->w_set_curswant = TRUE;
8973 }
8974 else
8975 vim_beep();
8976}
8977
8978 static void
8979ins_up(startcol)
8980 int startcol; /* when TRUE move to Insstart.col */
8981{
8982 pos_T tpos;
8983 linenr_T old_topline = curwin->w_topline;
8984#ifdef FEAT_DIFF
8985 int old_topfill = curwin->w_topfill;
8986#endif
8987
8988 undisplay_dollar();
8989 tpos = curwin->w_cursor;
8990 if (cursor_up(1L, TRUE) == OK)
8991 {
8992 if (startcol)
8993 coladvance(getvcol_nolist(&Insstart));
8994 if (old_topline != curwin->w_topline
8995#ifdef FEAT_DIFF
8996 || old_topfill != curwin->w_topfill
8997#endif
8998 )
8999 redraw_later(VALID);
9000 start_arrow(&tpos);
9001#ifdef FEAT_CINDENT
9002 can_cindent = TRUE;
9003#endif
9004 }
9005 else
9006 vim_beep();
9007}
9008
9009 static void
9010ins_pageup()
9011{
9012 pos_T tpos;
9013
9014 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009015
9016#ifdef FEAT_WINDOWS
9017 if (mod_mask & MOD_MASK_CTRL)
9018 {
9019 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009020 if (first_tabpage->tp_next != NULL)
9021 {
9022 start_arrow(&curwin->w_cursor);
9023 goto_tabpage(-1);
9024 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009025 return;
9026 }
9027#endif
9028
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 tpos = curwin->w_cursor;
9030 if (onepage(BACKWARD, 1L) == OK)
9031 {
9032 start_arrow(&tpos);
9033#ifdef FEAT_CINDENT
9034 can_cindent = TRUE;
9035#endif
9036 }
9037 else
9038 vim_beep();
9039}
9040
9041 static void
9042ins_down(startcol)
9043 int startcol; /* when TRUE move to Insstart.col */
9044{
9045 pos_T tpos;
9046 linenr_T old_topline = curwin->w_topline;
9047#ifdef FEAT_DIFF
9048 int old_topfill = curwin->w_topfill;
9049#endif
9050
9051 undisplay_dollar();
9052 tpos = curwin->w_cursor;
9053 if (cursor_down(1L, TRUE) == OK)
9054 {
9055 if (startcol)
9056 coladvance(getvcol_nolist(&Insstart));
9057 if (old_topline != curwin->w_topline
9058#ifdef FEAT_DIFF
9059 || old_topfill != curwin->w_topfill
9060#endif
9061 )
9062 redraw_later(VALID);
9063 start_arrow(&tpos);
9064#ifdef FEAT_CINDENT
9065 can_cindent = TRUE;
9066#endif
9067 }
9068 else
9069 vim_beep();
9070}
9071
9072 static void
9073ins_pagedown()
9074{
9075 pos_T tpos;
9076
9077 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009078
9079#ifdef FEAT_WINDOWS
9080 if (mod_mask & MOD_MASK_CTRL)
9081 {
9082 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009083 if (first_tabpage->tp_next != NULL)
9084 {
9085 start_arrow(&curwin->w_cursor);
9086 goto_tabpage(0);
9087 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009088 return;
9089 }
9090#endif
9091
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 tpos = curwin->w_cursor;
9093 if (onepage(FORWARD, 1L) == OK)
9094 {
9095 start_arrow(&tpos);
9096#ifdef FEAT_CINDENT
9097 can_cindent = TRUE;
9098#endif
9099 }
9100 else
9101 vim_beep();
9102}
9103
9104#ifdef FEAT_DND
9105 static void
9106ins_drop()
9107{
9108 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9109}
9110#endif
9111
9112/*
9113 * Handle TAB in Insert or Replace mode.
9114 * Return TRUE when the TAB needs to be inserted like a normal character.
9115 */
9116 static int
9117ins_tab()
9118{
9119 int ind;
9120 int i;
9121 int temp;
9122
9123 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9124 Insstart_blank_vcol = get_nolist_virtcol();
9125 if (echeck_abbr(TAB + ABBR_OFF))
9126 return FALSE;
9127
9128 ind = inindent(0);
9129#ifdef FEAT_CINDENT
9130 if (ind)
9131 can_cindent = FALSE;
9132#endif
9133
9134 /*
9135 * When nothing special, insert TAB like a normal character
9136 */
9137 if (!curbuf->b_p_et
9138 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9139 && curbuf->b_p_sts == 0)
9140 return TRUE;
9141
9142 if (stop_arrow() == FAIL)
9143 return TRUE;
9144
9145 did_ai = FALSE;
9146#ifdef FEAT_SMARTINDENT
9147 did_si = FALSE;
9148 can_si = FALSE;
9149 can_si_back = FALSE;
9150#endif
9151 AppendToRedobuff((char_u *)"\t");
9152
9153 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9154 temp = (int)curbuf->b_p_sw;
9155 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9156 temp = (int)curbuf->b_p_sts;
9157 else /* otherwise use 'tabstop' */
9158 temp = (int)curbuf->b_p_ts;
9159 temp -= get_nolist_virtcol() % temp;
9160
9161 /*
9162 * Insert the first space with ins_char(). It will delete one char in
9163 * replace mode. Insert the rest with ins_str(); it will not delete any
9164 * chars. For VREPLACE mode, we use ins_char() for all characters.
9165 */
9166 ins_char(' ');
9167 while (--temp > 0)
9168 {
9169#ifdef FEAT_VREPLACE
9170 if (State & VREPLACE_FLAG)
9171 ins_char(' ');
9172 else
9173#endif
9174 {
9175 ins_str((char_u *)" ");
9176 if (State & REPLACE_FLAG) /* no char replaced */
9177 replace_push(NUL);
9178 }
9179 }
9180
9181 /*
9182 * When 'expandtab' not set: Replace spaces by TABs where possible.
9183 */
9184 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9185 {
9186 char_u *ptr;
9187#ifdef FEAT_VREPLACE
9188 char_u *saved_line = NULL; /* init for GCC */
9189 pos_T pos;
9190#endif
9191 pos_T fpos;
9192 pos_T *cursor;
9193 colnr_T want_vcol, vcol;
9194 int change_col = -1;
9195 int save_list = curwin->w_p_list;
9196
9197 /*
9198 * Get the current line. For VREPLACE mode, don't make real changes
9199 * yet, just work on a copy of the line.
9200 */
9201#ifdef FEAT_VREPLACE
9202 if (State & VREPLACE_FLAG)
9203 {
9204 pos = curwin->w_cursor;
9205 cursor = &pos;
9206 saved_line = vim_strsave(ml_get_curline());
9207 if (saved_line == NULL)
9208 return FALSE;
9209 ptr = saved_line + pos.col;
9210 }
9211 else
9212#endif
9213 {
9214 ptr = ml_get_cursor();
9215 cursor = &curwin->w_cursor;
9216 }
9217
9218 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9219 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9220 curwin->w_p_list = FALSE;
9221
9222 /* Find first white before the cursor */
9223 fpos = curwin->w_cursor;
9224 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9225 {
9226 --fpos.col;
9227 --ptr;
9228 }
9229
9230 /* In Replace mode, don't change characters before the insert point. */
9231 if ((State & REPLACE_FLAG)
9232 && fpos.lnum == Insstart.lnum
9233 && fpos.col < Insstart.col)
9234 {
9235 ptr += Insstart.col - fpos.col;
9236 fpos.col = Insstart.col;
9237 }
9238
9239 /* compute virtual column numbers of first white and cursor */
9240 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9241 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9242
9243 /* Use as many TABs as possible. Beware of 'showbreak' and
9244 * 'linebreak' adding extra virtual columns. */
9245 while (vim_iswhite(*ptr))
9246 {
9247 i = lbr_chartabsize((char_u *)"\t", vcol);
9248 if (vcol + i > want_vcol)
9249 break;
9250 if (*ptr != TAB)
9251 {
9252 *ptr = TAB;
9253 if (change_col < 0)
9254 {
9255 change_col = fpos.col; /* Column of first change */
9256 /* May have to adjust Insstart */
9257 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9258 Insstart.col = fpos.col;
9259 }
9260 }
9261 ++fpos.col;
9262 ++ptr;
9263 vcol += i;
9264 }
9265
9266 if (change_col >= 0)
9267 {
9268 int repl_off = 0;
9269
9270 /* Skip over the spaces we need. */
9271 while (vcol < want_vcol && *ptr == ' ')
9272 {
9273 vcol += lbr_chartabsize(ptr, vcol);
9274 ++ptr;
9275 ++repl_off;
9276 }
9277 if (vcol > want_vcol)
9278 {
9279 /* Must have a char with 'showbreak' just before it. */
9280 --ptr;
9281 --repl_off;
9282 }
9283 fpos.col += repl_off;
9284
9285 /* Delete following spaces. */
9286 i = cursor->col - fpos.col;
9287 if (i > 0)
9288 {
9289 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
9290 /* correct replace stack. */
9291 if ((State & REPLACE_FLAG)
9292#ifdef FEAT_VREPLACE
9293 && !(State & VREPLACE_FLAG)
9294#endif
9295 )
9296 for (temp = i; --temp >= 0; )
9297 replace_join(repl_off);
9298 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009299#ifdef FEAT_NETBEANS_INTG
9300 if (usingNetbeans)
9301 {
9302 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9303 (long)(i + 1));
9304 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9305 (char_u *)"\t", 1);
9306 }
9307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 cursor->col -= i;
9309
9310#ifdef FEAT_VREPLACE
9311 /*
9312 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9313 * backspacing over the changed spacing and then inserting the new
9314 * spacing.
9315 */
9316 if (State & VREPLACE_FLAG)
9317 {
9318 /* Backspace from real cursor to change_col */
9319 backspace_until_column(change_col);
9320
9321 /* Insert each char in saved_line from changed_col to
9322 * ptr-cursor */
9323 ins_bytes_len(saved_line + change_col,
9324 cursor->col - change_col);
9325 }
9326#endif
9327 }
9328
9329#ifdef FEAT_VREPLACE
9330 if (State & VREPLACE_FLAG)
9331 vim_free(saved_line);
9332#endif
9333 curwin->w_p_list = save_list;
9334 }
9335
9336 return FALSE;
9337}
9338
9339/*
9340 * Handle CR or NL in insert mode.
9341 * Return TRUE when out of memory or can't undo.
9342 */
9343 static int
9344ins_eol(c)
9345 int c;
9346{
9347 int i;
9348
9349 if (echeck_abbr(c + ABBR_OFF))
9350 return FALSE;
9351 if (stop_arrow() == FAIL)
9352 return TRUE;
9353 undisplay_dollar();
9354
9355 /*
9356 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9357 * character under the cursor. Only push a NUL on the replace stack,
9358 * nothing to put back when the NL is deleted.
9359 */
9360 if ((State & REPLACE_FLAG)
9361#ifdef FEAT_VREPLACE
9362 && !(State & VREPLACE_FLAG)
9363#endif
9364 )
9365 replace_push(NUL);
9366
9367 /*
9368 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9369 * replacing the next line, so we push all of the characters left on the
9370 * line onto the replace stack. This is not done here though, it is done
9371 * in open_line().
9372 */
9373
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009374#ifdef FEAT_VIRTUALEDIT
9375 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9376 * CTRL-O). */
9377 if (virtual_active() && curwin->w_cursor.coladd > 0)
9378 coladvance(getviscol());
9379#endif
9380
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381#ifdef FEAT_RIGHTLEFT
9382# ifdef FEAT_FKMAP
9383 if (p_altkeymap && p_fkmap)
9384 fkmap(NL);
9385# endif
9386 /* NL in reverse insert will always start in the end of
9387 * current line. */
9388 if (revins_on)
9389 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9390#endif
9391
9392 AppendToRedobuff(NL_STR);
9393 i = open_line(FORWARD,
9394#ifdef FEAT_COMMENTS
9395 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9396#endif
9397 0, old_indent);
9398 old_indent = 0;
9399#ifdef FEAT_CINDENT
9400 can_cindent = TRUE;
9401#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009402#ifdef FEAT_FOLDING
9403 /* When inserting a line the cursor line must never be in a closed fold. */
9404 foldOpenCursor();
9405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406
9407 return (!i);
9408}
9409
9410#ifdef FEAT_DIGRAPHS
9411/*
9412 * Handle digraph in insert mode.
9413 * Returns character still to be inserted, or NUL when nothing remaining to be
9414 * done.
9415 */
9416 static int
9417ins_digraph()
9418{
9419 int c;
9420 int cc;
9421
9422 pc_status = PC_STATUS_UNSET;
9423 if (redrawing() && !char_avail())
9424 {
9425 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009426 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427
9428 edit_putchar('?', TRUE);
9429#ifdef FEAT_CMDL_INFO
9430 add_to_showcmd_c(Ctrl_K);
9431#endif
9432 }
9433
9434#ifdef USE_ON_FLY_SCROLL
9435 dont_scroll = TRUE; /* disallow scrolling here */
9436#endif
9437
9438 /* don't map the digraph chars. This also prevents the
9439 * mode message to be deleted when ESC is hit */
9440 ++no_mapping;
9441 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009442 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 --no_mapping;
9444 --allow_keys;
9445 if (IS_SPECIAL(c) || mod_mask) /* special key */
9446 {
9447#ifdef FEAT_CMDL_INFO
9448 clear_showcmd();
9449#endif
9450 insert_special(c, TRUE, FALSE);
9451 return NUL;
9452 }
9453 if (c != ESC)
9454 {
9455 if (redrawing() && !char_avail())
9456 {
9457 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009458 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459
9460 if (char2cells(c) == 1)
9461 {
9462 /* first remove the '?', otherwise it's restored when typing
9463 * an ESC next */
9464 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009465 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 edit_putchar(c, TRUE);
9467 }
9468#ifdef FEAT_CMDL_INFO
9469 add_to_showcmd_c(c);
9470#endif
9471 }
9472 ++no_mapping;
9473 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009474 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 --no_mapping;
9476 --allow_keys;
9477 if (cc != ESC)
9478 {
9479 AppendToRedobuff((char_u *)CTRL_V_STR);
9480 c = getdigraph(c, cc, TRUE);
9481#ifdef FEAT_CMDL_INFO
9482 clear_showcmd();
9483#endif
9484 return c;
9485 }
9486 }
9487 edit_unputchar();
9488#ifdef FEAT_CMDL_INFO
9489 clear_showcmd();
9490#endif
9491 return NUL;
9492}
9493#endif
9494
9495/*
9496 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9497 * Returns the char to be inserted, or NUL if none found.
9498 */
9499 static int
9500ins_copychar(lnum)
9501 linenr_T lnum;
9502{
9503 int c;
9504 int temp;
9505 char_u *ptr, *prev_ptr;
9506
9507 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9508 {
9509 vim_beep();
9510 return NUL;
9511 }
9512
9513 /* try to advance to the cursor column */
9514 temp = 0;
9515 ptr = ml_get(lnum);
9516 prev_ptr = ptr;
9517 validate_virtcol();
9518 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9519 {
9520 prev_ptr = ptr;
9521 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9522 }
9523 if ((colnr_T)temp > curwin->w_virtcol)
9524 ptr = prev_ptr;
9525
9526#ifdef FEAT_MBYTE
9527 c = (*mb_ptr2char)(ptr);
9528#else
9529 c = *ptr;
9530#endif
9531 if (c == NUL)
9532 vim_beep();
9533 return c;
9534}
9535
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009536/*
9537 * CTRL-Y or CTRL-E typed in Insert mode.
9538 */
9539 static int
9540ins_ctrl_ey(tc)
9541 int tc;
9542{
9543 int c = tc;
9544
9545#ifdef FEAT_INS_EXPAND
9546 if (ctrl_x_mode == CTRL_X_SCROLL)
9547 {
9548 if (c == Ctrl_Y)
9549 scrolldown_clamp();
9550 else
9551 scrollup_clamp();
9552 redraw_later(VALID);
9553 }
9554 else
9555#endif
9556 {
9557 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9558 if (c != NUL)
9559 {
9560 long tw_save;
9561
9562 /* The character must be taken literally, insert like it
9563 * was typed after a CTRL-V, and pretend 'textwidth'
9564 * wasn't set. Digits, 'o' and 'x' are special after a
9565 * CTRL-V, don't use it for these. */
9566 if (c < 256 && !isalnum(c))
9567 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9568 tw_save = curbuf->b_p_tw;
9569 curbuf->b_p_tw = -1;
9570 insert_special(c, TRUE, FALSE);
9571 curbuf->b_p_tw = tw_save;
9572#ifdef FEAT_RIGHTLEFT
9573 revins_chars++;
9574 revins_legal++;
9575#endif
9576 c = Ctrl_V; /* pretend CTRL-V is last character */
9577 auto_format(FALSE, TRUE);
9578 }
9579 }
9580 return c;
9581}
9582
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583#ifdef FEAT_SMARTINDENT
9584/*
9585 * Try to do some very smart auto-indenting.
9586 * Used when inserting a "normal" character.
9587 */
9588 static void
9589ins_try_si(c)
9590 int c;
9591{
9592 pos_T *pos, old_pos;
9593 char_u *ptr;
9594 int i;
9595 int temp;
9596
9597 /*
9598 * do some very smart indenting when entering '{' or '}'
9599 */
9600 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9601 {
9602 /*
9603 * for '}' set indent equal to indent of line containing matching '{'
9604 */
9605 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9606 {
9607 old_pos = curwin->w_cursor;
9608 /*
9609 * If the matching '{' has a ')' immediately before it (ignoring
9610 * white-space), then line up with the start of the line
9611 * containing the matching '(' if there is one. This handles the
9612 * case where an "if (..\n..) {" statement continues over multiple
9613 * lines -- webb
9614 */
9615 ptr = ml_get(pos->lnum);
9616 i = pos->col;
9617 if (i > 0) /* skip blanks before '{' */
9618 while (--i > 0 && vim_iswhite(ptr[i]))
9619 ;
9620 curwin->w_cursor.lnum = pos->lnum;
9621 curwin->w_cursor.col = i;
9622 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9623 curwin->w_cursor = *pos;
9624 i = get_indent();
9625 curwin->w_cursor = old_pos;
9626#ifdef FEAT_VREPLACE
9627 if (State & VREPLACE_FLAG)
9628 change_indent(INDENT_SET, i, FALSE, NUL);
9629 else
9630#endif
9631 (void)set_indent(i, SIN_CHANGED);
9632 }
9633 else if (curwin->w_cursor.col > 0)
9634 {
9635 /*
9636 * when inserting '{' after "O" reduce indent, but not
9637 * more than indent of previous line
9638 */
9639 temp = TRUE;
9640 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9641 {
9642 old_pos = curwin->w_cursor;
9643 i = get_indent();
9644 while (curwin->w_cursor.lnum > 1)
9645 {
9646 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9647
9648 /* ignore empty lines and lines starting with '#'. */
9649 if (*ptr != '#' && *ptr != NUL)
9650 break;
9651 }
9652 if (get_indent() >= i)
9653 temp = FALSE;
9654 curwin->w_cursor = old_pos;
9655 }
9656 if (temp)
9657 shift_line(TRUE, FALSE, 1);
9658 }
9659 }
9660
9661 /*
9662 * set indent of '#' always to 0
9663 */
9664 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9665 {
9666 /* remember current indent for next line */
9667 old_indent = get_indent();
9668 (void)set_indent(0, SIN_CHANGED);
9669 }
9670
9671 /* Adjust ai_col, the char at this position can be deleted. */
9672 if (ai_col > curwin->w_cursor.col)
9673 ai_col = curwin->w_cursor.col;
9674}
9675#endif
9676
9677/*
9678 * Get the value that w_virtcol would have when 'list' is off.
9679 * Unless 'cpo' contains the 'L' flag.
9680 */
9681 static colnr_T
9682get_nolist_virtcol()
9683{
9684 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9685 return getvcol_nolist(&curwin->w_cursor);
9686 validate_virtcol();
9687 return curwin->w_virtcol;
9688}