blob: 2c97119605264a3314bfeba73c2b3ab2cfc2b6e5 [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 Moolenaar4be06f92005-07-29 22:36:03 +0000108/* When the first completion is done "compl_started" is set. When it's
109 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000110static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000111
Bram Moolenaar572cb562005-08-05 21:35:02 +0000112static int compl_matches = 0;
113static char_u *compl_pattern = NULL;
114static int compl_direction = FORWARD;
115static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000116static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000117static pos_T compl_startpos;
118static colnr_T compl_col = 0; /* column where the text starts
119 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000120static char_u *compl_orig_text = NULL; /* text as it was before
121 * completion started */
122static int compl_cont_mode = 0;
123static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000125static void ins_ctrl_x __ARGS((void));
126static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000127static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000128static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000129static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000131static void ins_compl_upd_pum __ARGS((void));
132static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000133static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000134static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000135static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000136static 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 +0000137static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138static void ins_compl_free __ARGS((void));
139static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000140static int ins_compl_bs __ARGS((void));
141static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000142static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000143static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000144static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000146#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
147static void ins_compl_add_list __ARGS((list_T *list));
148#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000149static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150static void ins_compl_delete __ARGS((void));
151static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000152static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000153static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000154static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000155static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000156static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157static int ins_complete __ARGS((int c));
158static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
159#endif /* FEAT_INS_EXPAND */
160
161#define BACKSPACE_CHAR 1
162#define BACKSPACE_WORD 2
163#define BACKSPACE_WORD_NOT_SPACE 3
164#define BACKSPACE_LINE 4
165
Bram Moolenaar754b5602006-02-09 23:53:20 +0000166static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167static void ins_ctrl_v __ARGS((void));
168static void undisplay_dollar __ARGS((void));
169static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000170static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171static void check_auto_format __ARGS((int));
172static void redo_literal __ARGS((int c));
173static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000174#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000175static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000176static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000177static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
180static int echeck_abbr __ARGS((int));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000181#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182static void replace_push_off __ARGS((int c));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184static int replace_pop __ARGS((void));
185static void replace_join __ARGS((int off));
186static void replace_pop_ins __ARGS((void));
187#ifdef FEAT_MBYTE
188static void mb_replace_pop_ins __ARGS((int cc));
189#endif
190static void replace_flush __ARGS((void));
191static void replace_do_bs __ARGS((void));
192#ifdef FEAT_CINDENT
193static int cindent_on __ARGS((void));
194#endif
195static void ins_reg __ARGS((void));
196static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000197static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000198static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199#ifdef FEAT_RIGHTLEFT
200static void ins_ctrl_ __ARGS((void));
201#endif
202#ifdef FEAT_VISUAL
203static int ins_start_select __ARGS((int c));
204#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000205static void ins_insert __ARGS((int replaceState));
206static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207static void ins_shift __ARGS((int c, int lastc));
208static void ins_del __ARGS((void));
209static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
210#ifdef FEAT_MOUSE
211static void ins_mouse __ARGS((int c));
212static void ins_mousescroll __ARGS((int up));
213#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000214#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
215static void ins_tabline __ARGS((int c));
216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217static void ins_left __ARGS((void));
218static void ins_home __ARGS((int c));
219static void ins_end __ARGS((int c));
220static void ins_s_left __ARGS((void));
221static void ins_right __ARGS((void));
222static void ins_s_right __ARGS((void));
223static void ins_up __ARGS((int startcol));
224static void ins_pageup __ARGS((void));
225static void ins_down __ARGS((int startcol));
226static void ins_pagedown __ARGS((void));
227#ifdef FEAT_DND
228static void ins_drop __ARGS((void));
229#endif
230static int ins_tab __ARGS((void));
231static int ins_eol __ARGS((int c));
232#ifdef FEAT_DIGRAPHS
233static int ins_digraph __ARGS((void));
234#endif
235static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000236static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#ifdef FEAT_SMARTINDENT
238static void ins_try_si __ARGS((int c));
239#endif
240static colnr_T get_nolist_virtcol __ARGS((void));
241
242static colnr_T Insstart_textlen; /* length of line when insert started */
243static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
244
245static char_u *last_insert = NULL; /* the text of the previous insert,
246 K_SPECIAL and CSI are escaped */
247static int last_insert_skip; /* nr of chars in front of previous insert */
248static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000249static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250
251#ifdef FEAT_CINDENT
252static int can_cindent; /* may do cindenting on this line */
253#endif
254
255static int old_indent = 0; /* for ^^D command in insert mode */
256
257#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000258static int revins_on; /* reverse insert mode on */
259static int revins_chars; /* how much to skip after edit */
260static int revins_legal; /* was the last char 'legal'? */
261static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#endif
263
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264static int ins_need_undo; /* call u_save() before inserting a
265 char. Set when edit() is called.
266 after that arrow_used is used. */
267
268static int did_add_space = FALSE; /* auto_format() added an extra space
269 under the cursor */
270
271/*
272 * edit(): Start inserting text.
273 *
274 * "cmdchar" can be:
275 * 'i' normal insert command
276 * 'a' normal append command
277 * 'R' replace command
278 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
279 * but still only one <CR> is inserted. The <Esc> is not used for redo.
280 * 'g' "gI" command.
281 * 'V' "gR" command for Virtual Replace mode.
282 * 'v' "gr" command for single character Virtual Replace mode.
283 *
284 * This function is not called recursively. For CTRL-O commands, it returns
285 * and lets the caller handle the Normal-mode command.
286 *
287 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
288 */
289 int
290edit(cmdchar, startln, count)
291 int cmdchar;
292 int startln; /* if set, insert at start of line */
293 long count;
294{
295 int c = 0;
296 char_u *ptr;
297 int lastc;
298 colnr_T mincol;
299 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 int i;
301 int did_backspace = TRUE; /* previous char was backspace */
302#ifdef FEAT_CINDENT
303 int line_is_white = FALSE; /* line is empty before insert */
304#endif
305 linenr_T old_topline = 0; /* topline before insertion */
306#ifdef FEAT_DIFF
307 int old_topfill = -1;
308#endif
309 int inserted_space = FALSE; /* just inserted a space */
310 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000311 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000313 /* Remember whether editing was restarted after CTRL-O. */
314 did_restart_edit = restart_edit;
315
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 /* sleep before redrawing, needed for "CTRL-O :" that results in an
317 * error message */
318 check_for_delay(TRUE);
319
320#ifdef HAVE_SANDBOX
321 /* Don't allow inserting in the sandbox. */
322 if (sandbox != 0)
323 {
324 EMSG(_(e_sandbox));
325 return FALSE;
326 }
327#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000328 /* Don't allow changes in the buffer while editing the cmdline. The
329 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000330 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000331 {
332 EMSG(_(e_secure));
333 return FALSE;
334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335
336#ifdef FEAT_INS_EXPAND
337 ins_compl_clear(); /* clear stuff for CTRL-X mode */
338#endif
339
Bram Moolenaar843ee412004-06-30 16:16:41 +0000340#ifdef FEAT_AUTOCMD
341 /*
342 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
343 */
344 if (cmdchar != 'r' && cmdchar != 'v')
345 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000346# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000347 if (cmdchar == 'R')
348 ptr = (char_u *)"r";
349 else if (cmdchar == 'V')
350 ptr = (char_u *)"v";
351 else
352 ptr = (char_u *)"i";
353 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000354# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000355 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
356 }
357#endif
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifdef FEAT_MOUSE
360 /*
361 * When doing a paste with the middle mouse button, Insstart is set to
362 * where the paste started.
363 */
364 if (where_paste_started.lnum != 0)
365 Insstart = where_paste_started;
366 else
367#endif
368 {
369 Insstart = curwin->w_cursor;
370 if (startln)
371 Insstart.col = 0;
372 }
373 Insstart_textlen = linetabsize(ml_get_curline());
374 Insstart_blank_vcol = MAXCOL;
375 if (!did_ai)
376 ai_col = 0;
377
378 if (cmdchar != NUL && restart_edit == 0)
379 {
380 ResetRedobuff();
381 AppendNumberToRedobuff(count);
382#ifdef FEAT_VREPLACE
383 if (cmdchar == 'V' || cmdchar == 'v')
384 {
385 /* "gR" or "gr" command */
386 AppendCharToRedobuff('g');
387 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
388 }
389 else
390#endif
391 {
392 AppendCharToRedobuff(cmdchar);
393 if (cmdchar == 'g') /* "gI" command */
394 AppendCharToRedobuff('I');
395 else if (cmdchar == 'r') /* "r<CR>" command */
396 count = 1; /* insert only one <CR> */
397 }
398 }
399
400 if (cmdchar == 'R')
401 {
402#ifdef FEAT_FKMAP
403 if (p_fkmap && p_ri)
404 {
405 beep_flush();
406 EMSG(farsi_text_3); /* encoded in Farsi */
407 State = INSERT;
408 }
409 else
410#endif
411 State = REPLACE;
412 }
413#ifdef FEAT_VREPLACE
414 else if (cmdchar == 'V' || cmdchar == 'v')
415 {
416 State = VREPLACE;
417 replaceState = VREPLACE;
418 orig_line_count = curbuf->b_ml.ml_line_count;
419 vr_lines_changed = 1;
420 }
421#endif
422 else
423 State = INSERT;
424
425 stop_insert_mode = FALSE;
426
427 /*
428 * Need to recompute the cursor position, it might move when the cursor is
429 * on a TAB or special character.
430 */
431 curs_columns(TRUE);
432
433 /*
434 * Enable langmap or IME, indicated by 'iminsert'.
435 * Note that IME may enabled/disabled without us noticing here, thus the
436 * 'iminsert' value may not reflect what is actually used. It is updated
437 * when hitting <Esc>.
438 */
439 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
440 State |= LANGMAP;
441#ifdef USE_IM_CONTROL
442 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
443#endif
444
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445#ifdef FEAT_MOUSE
446 setmouse();
447#endif
448#ifdef FEAT_CMDL_INFO
449 clear_showcmd();
450#endif
451#ifdef FEAT_RIGHTLEFT
452 /* there is no reverse replace mode */
453 revins_on = (State == INSERT && p_ri);
454 if (revins_on)
455 undisplay_dollar();
456 revins_chars = 0;
457 revins_legal = 0;
458 revins_scol = -1;
459#endif
460
461 /*
462 * Handle restarting Insert mode.
463 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
464 * restart_edit non-zero, and something in the stuff buffer.
465 */
466 if (restart_edit != 0 && stuff_empty())
467 {
468#ifdef FEAT_MOUSE
469 /*
470 * After a paste we consider text typed to be part of the insert for
471 * the pasted text. You can backspace over the pasted text too.
472 */
473 if (where_paste_started.lnum)
474 arrow_used = FALSE;
475 else
476#endif
477 arrow_used = TRUE;
478 restart_edit = 0;
479
480 /*
481 * If the cursor was after the end-of-line before the CTRL-O and it is
482 * now at the end-of-line, put it after the end-of-line (this is not
483 * correct in very rare cases).
484 * Also do this if curswant is greater than the current virtual
485 * column. Eg after "^O$" or "^O80|".
486 */
487 validate_virtcol();
488 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000489 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 || curwin->w_curswant > curwin->w_virtcol)
491 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
492 {
493 if (ptr[1] == NUL)
494 ++curwin->w_cursor.col;
495#ifdef FEAT_MBYTE
496 else if (has_mbyte)
497 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000498 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 if (ptr[i] == NUL)
500 curwin->w_cursor.col += i;
501 }
502#endif
503 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000504 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 }
506 else
507 arrow_used = FALSE;
508
509 /* we are in insert mode now, don't need to start it anymore */
510 need_start_insertmode = FALSE;
511
512 /* Need to save the line for undo before inserting the first char. */
513 ins_need_undo = TRUE;
514
515#ifdef FEAT_MOUSE
516 where_paste_started.lnum = 0;
517#endif
518#ifdef FEAT_CINDENT
519 can_cindent = TRUE;
520#endif
521#ifdef FEAT_FOLDING
522 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
523 * restarting. */
524 if (!p_im && did_restart_edit == 0)
525 foldOpenCursor();
526#endif
527
528 /*
529 * If 'showmode' is set, show the current (insert/replace/..) mode.
530 * A warning message for changing a readonly file is given here, before
531 * actually changing anything. It's put after the mode, if any.
532 */
533 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000534 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 i = showmode();
536
537 if (!p_im && did_restart_edit == 0)
538 change_warning(i + 1);
539
540#ifdef CURSOR_SHAPE
541 ui_cursor_shape(); /* may show different cursor shape */
542#endif
543#ifdef FEAT_DIGRAPHS
544 do_digraph(-1); /* clear digraphs */
545#endif
546
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000547 /*
548 * Get the current length of the redo buffer, those characters have to be
549 * skipped if we want to get to the inserted characters.
550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 ptr = get_inserted();
552 if (ptr == NULL)
553 new_insert_skip = 0;
554 else
555 {
556 new_insert_skip = (int)STRLEN(ptr);
557 vim_free(ptr);
558 }
559
560 old_indent = 0;
561
562 /*
563 * Main loop in Insert mode: repeat until Insert mode is left.
564 */
565 for (;;)
566 {
567#ifdef FEAT_RIGHTLEFT
568 if (!revins_legal)
569 revins_scol = -1; /* reset on illegal motions */
570 else
571 revins_legal = 0;
572#endif
573 if (arrow_used) /* don't repeat insert when arrow key used */
574 count = 0;
575
576 if (stop_insert_mode)
577 {
578 /* ":stopinsert" used or 'insertmode' reset */
579 count = 0;
580 goto doESCkey;
581 }
582
583 /* set curwin->w_curswant for next K_DOWN or K_UP */
584 if (!arrow_used)
585 curwin->w_set_curswant = TRUE;
586
587 /* If there is no typeahead may check for timestamps (e.g., for when a
588 * menu invoked a shell command). */
589 if (stuff_empty())
590 {
591 did_check_timestamps = FALSE;
592 if (need_check_timestamps)
593 check_timestamps(FALSE);
594 }
595
596 /*
597 * When emsg() was called msg_scroll will have been set.
598 */
599 msg_scroll = FALSE;
600
601#ifdef FEAT_GUI
602 /* When 'mousefocus' is set a mouse movement may have taken us to
603 * another window. "need_mouse_correct" may then be set because of an
604 * autocommand. */
605 if (need_mouse_correct)
606 gui_mouse_correct();
607#endif
608
609#ifdef FEAT_FOLDING
610 /* Open fold at the cursor line, according to 'foldopen'. */
611 if (fdo_flags & FDO_INSERT)
612 foldOpenCursor();
613 /* Close folds where the cursor isn't, according to 'foldclose' */
614 if (!char_avail())
615 foldCheckClose();
616#endif
617
618 /*
619 * If we inserted a character at the last position of the last line in
620 * the window, scroll the window one line up. This avoids an extra
621 * redraw.
622 * This is detected when the cursor column is smaller after inserting
623 * something.
624 * Don't do this when the topline changed already, it has
625 * already been adjusted (by insertchar() calling open_line())).
626 */
627 if (curbuf->b_mod_set
628 && curwin->w_p_wrap
629 && !did_backspace
630 && curwin->w_topline == old_topline
631#ifdef FEAT_DIFF
632 && curwin->w_topfill == old_topfill
633#endif
634 )
635 {
636 mincol = curwin->w_wcol;
637 validate_cursor_col();
638
639 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
640 && curwin->w_wrow == W_WINROW(curwin)
641 + curwin->w_height - 1 - p_so
642 && (curwin->w_cursor.lnum != curwin->w_topline
643#ifdef FEAT_DIFF
644 || curwin->w_topfill > 0
645#endif
646 ))
647 {
648#ifdef FEAT_DIFF
649 if (curwin->w_topfill > 0)
650 --curwin->w_topfill;
651 else
652#endif
653#ifdef FEAT_FOLDING
654 if (hasFolding(curwin->w_topline, NULL, &old_topline))
655 set_topline(curwin, old_topline + 1);
656 else
657#endif
658 set_topline(curwin, curwin->w_topline + 1);
659 }
660 }
661
662 /* May need to adjust w_topline to show the cursor. */
663 update_topline();
664
665 did_backspace = FALSE;
666
667 validate_cursor(); /* may set must_redraw */
668
669 /*
670 * Redraw the display when no characters are waiting.
671 * Also shows mode, ruler and positions cursor.
672 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000673 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
675#ifdef FEAT_SCROLLBIND
676 if (curwin->w_p_scb)
677 do_check_scrollbind(TRUE);
678#endif
679
680 update_curswant();
681 old_topline = curwin->w_topline;
682#ifdef FEAT_DIFF
683 old_topfill = curwin->w_topfill;
684#endif
685
686#ifdef USE_ON_FLY_SCROLL
687 dont_scroll = FALSE; /* allow scrolling here */
688#endif
689
690 /*
691 * Get a character for Insert mode.
692 */
693 lastc = c; /* remember previous char for CTRL-D */
694 c = safe_vgetc();
695
696#ifdef FEAT_RIGHTLEFT
697 if (p_hkmap && KeyTyped)
698 c = hkmap(c); /* Hebrew mode mapping */
699#endif
700#ifdef FEAT_FKMAP
701 if (p_fkmap && KeyTyped)
702 c = fkmap(c); /* Farsi mode mapping */
703#endif
704
705#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000706 /*
707 * Special handling of keys while the popup menu is visible or wanted
708 * and the cursor is still in the completed word.
709 */
710 if (compl_started && pum_wanted() && curwin->w_cursor.col >= compl_col)
Bram Moolenaara6557602006-02-04 22:43:20 +0000711 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000712 /* BS: Delete one character from "compl_leader". */
713 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000714 && curwin->w_cursor.col > compl_col
715 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000716 continue;
717
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000718 /* When no match was selected or it was edited. */
719 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000720 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000721 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000722 * "compl_leader". Except when at the original match and
723 * there is nothing to add, CTRL-L works like CTRL-P then. */
724 if (c == Ctrl_L
725 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
726 || STRLEN(compl_shown_match->cp_str)
727 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000728 {
729 ins_compl_addfrommatch();
730 continue;
731 }
732
Bram Moolenaardf1bdc92006-02-23 21:32:16 +0000733 /* A printable, non-white character: Add to "compl_leader". */
734 if (vim_isprintc(c) && !vim_iswhite(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000735 {
736 ins_compl_addleader(c);
737 continue;
738 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000739
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000740 /* Pressing CTRL-Y selects the current match. Shen
741 * compl_enter_selects is set the Enter key does the same. */
742 if (c == Ctrl_Y || (compl_enter_selects
743 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000744 {
745 ins_compl_delete();
746 ins_compl_insert();
747 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000748 }
749 }
750
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
752 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000753 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000754 if (c != K_IGNORE && ins_compl_prep(c))
755 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756#endif
757
Bram Moolenaar488c6512005-08-11 20:09:58 +0000758 /* CTRL-\ CTRL-N goes to Normal mode,
759 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
760 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 if (c == Ctrl_BSL)
762 {
763 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000764 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 ++no_mapping;
766 ++allow_keys;
767 c = safe_vgetc();
768 --no_mapping;
769 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000770 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000772 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 vungetc(c);
774 c = Ctrl_BSL;
775 }
776 else if (c == Ctrl_G && p_im)
777 continue;
778 else
779 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000780 if (c == Ctrl_O)
781 {
782 ins_ctrl_o();
783 ins_at_eol = FALSE; /* cursor keeps its column */
784 nomove = TRUE;
785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 count = 0;
787 goto doESCkey;
788 }
789 }
790
791#ifdef FEAT_DIGRAPHS
792 c = do_digraph(c);
793#endif
794
795#ifdef FEAT_INS_EXPAND
796 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
797 goto docomplete;
798#endif
799 if (c == Ctrl_V || c == Ctrl_Q)
800 {
801 ins_ctrl_v();
802 c = Ctrl_V; /* pretend CTRL-V is last typed character */
803 continue;
804 }
805
806#ifdef FEAT_CINDENT
807 if (cindent_on()
808# ifdef FEAT_INS_EXPAND
809 && ctrl_x_mode == 0
810# endif
811 )
812 {
813 /* A key name preceded by a bang means this key is not to be
814 * inserted. Skip ahead to the re-indenting below.
815 * A key name preceded by a star means that indenting has to be
816 * done before inserting the key. */
817 line_is_white = inindent(0);
818 if (in_cinkeys(c, '!', line_is_white))
819 goto force_cindent;
820 if (can_cindent && in_cinkeys(c, '*', line_is_white)
821 && stop_arrow() == OK)
822 do_c_expr_indent();
823 }
824#endif
825
826#ifdef FEAT_RIGHTLEFT
827 if (curwin->w_p_rl)
828 switch (c)
829 {
830 case K_LEFT: c = K_RIGHT; break;
831 case K_S_LEFT: c = K_S_RIGHT; break;
832 case K_C_LEFT: c = K_C_RIGHT; break;
833 case K_RIGHT: c = K_LEFT; break;
834 case K_S_RIGHT: c = K_S_LEFT; break;
835 case K_C_RIGHT: c = K_C_LEFT; break;
836 }
837#endif
838
839#ifdef FEAT_VISUAL
840 /*
841 * If 'keymodel' contains "startsel", may start selection. If it
842 * does, a CTRL-O and c will be stuffed, we need to get these
843 * characters.
844 */
845 if (ins_start_select(c))
846 continue;
847#endif
848
849 /*
850 * The big switch to handle a character in insert mode.
851 */
852 switch (c)
853 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000854 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 if (echeck_abbr(ESC + ABBR_OFF))
856 break;
857 /*FALLTHROUGH*/
858
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000859 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860#ifdef FEAT_CMDWIN
861 if (c == Ctrl_C && cmdwin_type != 0)
862 {
863 /* Close the cmdline window. */
864 cmdwin_result = K_IGNORE;
865 got_int = FALSE; /* don't stop executing autocommands et al. */
866 goto doESCkey;
867 }
868#endif
869
870#ifdef UNIX
871do_intr:
872#endif
873 /* when 'insertmode' set, and not halfway a mapping, don't leave
874 * Insert mode */
875 if (goto_im())
876 {
877 if (got_int)
878 {
879 (void)vgetc(); /* flush all buffers */
880 got_int = FALSE;
881 }
882 else
883 vim_beep();
884 break;
885 }
886doESCkey:
887 /*
888 * This is the ONLY return from edit()!
889 */
890 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
891 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000892 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 o_lnum = curwin->w_cursor.lnum;
894
Bram Moolenaar488c6512005-08-11 20:09:58 +0000895 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000896 {
897#ifdef FEAT_AUTOCMD
898 if (cmdchar != 'r' && cmdchar != 'v')
899 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
900 FALSE, curbuf);
901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 continue;
905
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000906 case Ctrl_Z: /* suspend when 'insertmode' set */
907 if (!p_im)
908 goto normalchar; /* insert CTRL-Z as normal char */
909 stuffReadbuff((char_u *)":st\r");
910 c = Ctrl_O;
911 /*FALLTHROUGH*/
912
913 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000914#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000915 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000916 goto docomplete;
917#endif
918 if (echeck_abbr(Ctrl_O + ABBR_OFF))
919 break;
920 ins_ctrl_o();
921 count = 0;
922 goto doESCkey;
923
Bram Moolenaar572cb562005-08-05 21:35:02 +0000924 case K_INS: /* toggle insert/replace mode */
925 case K_KINS:
926 ins_insert(replaceState);
927 break;
928
929 case K_SELECT: /* end of Select mode mapping - ignore */
930 break;
931
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000932#ifdef FEAT_SNIFF
933 case K_SNIFF: /* Sniff command received */
934 stuffcharReadbuff(K_SNIFF);
935 goto doESCkey;
936#endif
937
938 case K_HELP: /* Help key works like <ESC> <Help> */
939 case K_F1:
940 case K_XF1:
941 stuffcharReadbuff(K_HELP);
942 if (p_im)
943 need_start_insertmode = TRUE;
944 goto doESCkey;
945
946#ifdef FEAT_NETBEANS_INTG
947 case K_F21: /* NetBeans command */
948 ++no_mapping; /* don't map the next key hits */
949 i = safe_vgetc();
950 --no_mapping;
951 netbeans_keycommand(i);
952 break;
953#endif
954
955 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 case NUL:
957 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000958 /* For ^@ the trailing ESC will end the insert, unless there is an
959 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
961 && c != Ctrl_A && !p_im)
962 goto doESCkey; /* quit insert mode */
963 inserted_space = FALSE;
964 break;
965
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000966 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 ins_reg();
968 auto_format(FALSE, TRUE);
969 inserted_space = FALSE;
970 break;
971
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000972 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 ins_ctrl_g();
974 break;
975
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000976 case Ctrl_HAT: /* switch input mode and/or langmap */
977 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 break;
979
980#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000981 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 if (!p_ari)
983 goto normalchar;
984 ins_ctrl_();
985 break;
986#endif
987
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000988 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
990 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
991 goto docomplete;
992#endif
993 /* FALLTHROUGH */
994
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000995 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996# ifdef FEAT_INS_EXPAND
997 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
998 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000999 if (has_compl_option(FALSE))
1000 goto docomplete;
1001 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 }
1003# endif
1004 ins_shift(c, lastc);
1005 auto_format(FALSE, TRUE);
1006 inserted_space = FALSE;
1007 break;
1008
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001009 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 case K_KDEL:
1011 ins_del();
1012 auto_format(FALSE, TRUE);
1013 break;
1014
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001015 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 case Ctrl_H:
1017 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1018 auto_format(FALSE, TRUE);
1019 break;
1020
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001021 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1023 auto_format(FALSE, TRUE);
1024 break;
1025
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001026 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001027# ifdef FEAT_COMPL_FUNC
1028 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001029 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001030 goto docomplete;
1031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1033 auto_format(FALSE, TRUE);
1034 inserted_space = FALSE;
1035 break;
1036
1037#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001038 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 case K_LEFTMOUSE_NM:
1040 case K_LEFTDRAG:
1041 case K_LEFTRELEASE:
1042 case K_LEFTRELEASE_NM:
1043 case K_MIDDLEMOUSE:
1044 case K_MIDDLEDRAG:
1045 case K_MIDDLERELEASE:
1046 case K_RIGHTMOUSE:
1047 case K_RIGHTDRAG:
1048 case K_RIGHTRELEASE:
1049 case K_X1MOUSE:
1050 case K_X1DRAG:
1051 case K_X1RELEASE:
1052 case K_X2MOUSE:
1053 case K_X2DRAG:
1054 case K_X2RELEASE:
1055 ins_mouse(c);
1056 break;
1057
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001058 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 ins_mousescroll(FALSE);
1060 break;
1061
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001062 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 ins_mousescroll(TRUE);
1064 break;
1065#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001066#ifdef FEAT_GUI_TABLINE
1067 case K_TABLINE:
1068 case K_TABMENU:
1069 ins_tabline(c);
1070 break;
1071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001073 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 break;
1075
Bram Moolenaar754b5602006-02-09 23:53:20 +00001076#ifdef FEAT_AUTOCMD
1077 case K_CURSORHOLD: /* Didn't type something for a while. */
1078 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1079 did_cursorhold = TRUE;
1080 break;
1081#endif
1082
Bram Moolenaar4770d092006-01-12 23:22:24 +00001083#ifdef FEAT_GUI_W32
1084 /* On Win32 ignore <M-F4>, we get it when closing the window was
1085 * cancelled. */
1086 case K_F4:
1087 if (mod_mask != MOD_MASK_ALT)
1088 goto normalchar;
1089 break;
1090#endif
1091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092#ifdef FEAT_GUI
1093 case K_VER_SCROLLBAR:
1094 ins_scroll();
1095 break;
1096
1097 case K_HOR_SCROLLBAR:
1098 ins_horscroll();
1099 break;
1100#endif
1101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 case K_S_HOME:
1105 case K_C_HOME:
1106 ins_home(c);
1107 break;
1108
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001109 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 case K_S_END:
1112 case K_C_END:
1113 ins_end(c);
1114 break;
1115
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001116 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001117 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1118 ins_s_left();
1119 else
1120 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 break;
1122
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001123 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 case K_C_LEFT:
1125 ins_s_left();
1126 break;
1127
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001128 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001129 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1130 ins_s_right();
1131 else
1132 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 break;
1134
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001135 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 case K_C_RIGHT:
1137 ins_s_right();
1138 break;
1139
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001140 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001141#ifdef FEAT_INS_EXPAND
1142 if (pum_visible())
1143 goto docomplete;
1144#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001145 if (mod_mask & MOD_MASK_SHIFT)
1146 ins_pageup();
1147 else
1148 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 case K_PAGEUP:
1153 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001154#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001155 if (pum_visible())
1156 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 ins_pageup();
1159 break;
1160
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001161 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001162#ifdef FEAT_INS_EXPAND
1163 if (pum_visible())
1164 goto docomplete;
1165#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001166 if (mod_mask & MOD_MASK_SHIFT)
1167 ins_pagedown();
1168 else
1169 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 break;
1171
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001172 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 case K_PAGEDOWN:
1174 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001175#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001176 if (pum_visible())
1177 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 ins_pagedown();
1180 break;
1181
1182#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001183 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 ins_drop();
1185 break;
1186#endif
1187
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001188 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 c = TAB;
1190 /* FALLTHROUGH */
1191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001192 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1194 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1195 goto docomplete;
1196#endif
1197 inserted_space = FALSE;
1198 if (ins_tab())
1199 goto normalchar; /* insert TAB as a normal char */
1200 auto_format(FALSE, TRUE);
1201 break;
1202
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001203 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 c = CAR;
1205 /* FALLTHROUGH */
1206 case CAR:
1207 case NL:
1208#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1209 /* In a quickfix window a <CR> jumps to the error under the
1210 * cursor. */
1211 if (bt_quickfix(curbuf) && c == CAR)
1212 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001213 if (curwin->w_llist_ref == NULL) /* quickfix window */
1214 do_cmdline_cmd((char_u *)".cc");
1215 else /* location list window */
1216 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 break;
1218 }
1219#endif
1220#ifdef FEAT_CMDWIN
1221 if (cmdwin_type != 0)
1222 {
1223 /* Execute the command in the cmdline window. */
1224 cmdwin_result = CAR;
1225 goto doESCkey;
1226 }
1227#endif
1228 if (ins_eol(c) && !p_im)
1229 goto doESCkey; /* out of memory */
1230 auto_format(FALSE, FALSE);
1231 inserted_space = FALSE;
1232 break;
1233
1234#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001235 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236# ifdef FEAT_INS_EXPAND
1237 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1238 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001239 if (has_compl_option(TRUE))
1240 goto docomplete;
1241 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 }
1243# endif
1244# ifdef FEAT_DIGRAPHS
1245 c = ins_digraph();
1246 if (c == NUL)
1247 break;
1248# endif
1249 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
1252#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001253 case Ctrl_X: /* Enter CTRL-X mode */
1254 ins_ctrl_x();
1255 break;
1256
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001257 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 if (ctrl_x_mode != CTRL_X_TAGS)
1259 goto normalchar;
1260 goto docomplete;
1261
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001262 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 if (ctrl_x_mode != CTRL_X_FILES)
1264 goto normalchar;
1265 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001266
1267 case 's': /* Spelling completion after ^X */
1268 case Ctrl_S:
1269 if (ctrl_x_mode != CTRL_X_SPELL)
1270 goto normalchar;
1271 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272#endif
1273
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001274 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275#ifdef FEAT_INS_EXPAND
1276 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1277#endif
1278 {
1279 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1280 if (p_im)
1281 {
1282 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1283 break;
1284 goto doESCkey;
1285 }
1286 goto normalchar;
1287 }
1288#ifdef FEAT_INS_EXPAND
1289 /* FALLTHROUGH */
1290
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001291 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 case Ctrl_N:
1293 /* if 'complete' is empty then plain ^P is no longer special,
1294 * but it is under other ^X modes */
1295 if (*curbuf->b_p_cpt == NUL
1296 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001297 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 goto normalchar;
1299
1300docomplete:
1301 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001302 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 break;
1304#endif /* FEAT_INS_EXPAND */
1305
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001306 case Ctrl_Y: /* copy from previous line or scroll down */
1307 case Ctrl_E: /* copy from next line or scroll up */
1308 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 break;
1310
1311 default:
1312#ifdef UNIX
1313 if (c == intr_char) /* special interrupt char */
1314 goto do_intr;
1315#endif
1316
1317 /*
1318 * Insert a nomal character.
1319 */
1320normalchar:
1321#ifdef FEAT_SMARTINDENT
1322 /* Try to perform smart-indenting. */
1323 ins_try_si(c);
1324#endif
1325
1326 if (c == ' ')
1327 {
1328 inserted_space = TRUE;
1329#ifdef FEAT_CINDENT
1330 if (inindent(0))
1331 can_cindent = FALSE;
1332#endif
1333 if (Insstart_blank_vcol == MAXCOL
1334 && curwin->w_cursor.lnum == Insstart.lnum)
1335 Insstart_blank_vcol = get_nolist_virtcol();
1336 }
1337
1338 if (vim_iswordc(c) || !echeck_abbr(
1339#ifdef FEAT_MBYTE
1340 /* Add ABBR_OFF for characters above 0x100, this is
1341 * what check_abbr() expects. */
1342 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1343#endif
1344 c))
1345 {
1346 insert_special(c, FALSE, FALSE);
1347#ifdef FEAT_RIGHTLEFT
1348 revins_legal++;
1349 revins_chars++;
1350#endif
1351 }
1352
1353 auto_format(FALSE, TRUE);
1354
1355#ifdef FEAT_FOLDING
1356 /* When inserting a character the cursor line must never be in a
1357 * closed fold. */
1358 foldOpenCursor();
1359#endif
1360 break;
1361 } /* end of switch (c) */
1362
1363 /* If the cursor was moved we didn't just insert a space */
1364 if (arrow_used)
1365 inserted_space = FALSE;
1366
1367#ifdef FEAT_CINDENT
1368 if (can_cindent && cindent_on()
1369# ifdef FEAT_INS_EXPAND
1370 && ctrl_x_mode == 0
1371# endif
1372 )
1373 {
1374force_cindent:
1375 /*
1376 * Indent now if a key was typed that is in 'cinkeys'.
1377 */
1378 if (in_cinkeys(c, ' ', line_is_white))
1379 {
1380 if (stop_arrow() == OK)
1381 /* re-indent the current line */
1382 do_c_expr_indent();
1383 }
1384 }
1385#endif /* FEAT_CINDENT */
1386
1387 } /* for (;;) */
1388 /* NOTREACHED */
1389}
1390
1391/*
1392 * Redraw for Insert mode.
1393 * This is postponed until getting the next character to make '$' in the 'cpo'
1394 * option work correctly.
1395 * Only redraw when there are no characters available. This speeds up
1396 * inserting sequences of characters (e.g., for CTRL-R).
1397 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001398/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001400ins_redraw(ready)
1401 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
1403 if (!char_avail())
1404 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001405#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001406 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1407 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001408 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001409 && !equalpos(last_cursormoved, curwin->w_cursor)
1410# ifdef FEAT_INS_EXPAND
1411 && !pum_visible()
1412# endif
1413 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001414 {
1415 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1416 last_cursormoved = curwin->w_cursor;
1417 }
1418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 if (must_redraw)
1420 update_screen(0);
1421 else if (clear_cmdline || redraw_cmdline)
1422 showmode(); /* clear cmdline and show mode */
1423 showruler(FALSE);
1424 setcursor();
1425 emsg_on_display = FALSE; /* may remove error message now */
1426 }
1427}
1428
1429/*
1430 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1431 */
1432 static void
1433ins_ctrl_v()
1434{
1435 int c;
1436
1437 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001438 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
1440 if (redrawing() && !char_avail())
1441 edit_putchar('^', TRUE);
1442 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1443
1444#ifdef FEAT_CMDL_INFO
1445 add_to_showcmd_c(Ctrl_V);
1446#endif
1447
1448 c = get_literal();
1449#ifdef FEAT_CMDL_INFO
1450 clear_showcmd();
1451#endif
1452 insert_special(c, FALSE, TRUE);
1453#ifdef FEAT_RIGHTLEFT
1454 revins_chars++;
1455 revins_legal++;
1456#endif
1457}
1458
1459/*
1460 * Put a character directly onto the screen. It's not stored in a buffer.
1461 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1462 */
1463static int pc_status;
1464#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1465#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1466#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1467#define PC_STATUS_SET 3 /* pc_bytes was filled */
1468#ifdef FEAT_MBYTE
1469static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1470#else
1471static char_u pc_bytes[2]; /* saved bytes */
1472#endif
1473static int pc_attr;
1474static int pc_row;
1475static int pc_col;
1476
1477 void
1478edit_putchar(c, highlight)
1479 int c;
1480 int highlight;
1481{
1482 int attr;
1483
1484 if (ScreenLines != NULL)
1485 {
1486 update_topline(); /* just in case w_topline isn't valid */
1487 validate_cursor();
1488 if (highlight)
1489 attr = hl_attr(HLF_8);
1490 else
1491 attr = 0;
1492 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1493 pc_col = W_WINCOL(curwin);
1494#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1495 pc_status = PC_STATUS_UNSET;
1496#endif
1497#ifdef FEAT_RIGHTLEFT
1498 if (curwin->w_p_rl)
1499 {
1500 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1501# ifdef FEAT_MBYTE
1502 if (has_mbyte)
1503 {
1504 int fix_col = mb_fix_col(pc_col, pc_row);
1505
1506 if (fix_col != pc_col)
1507 {
1508 screen_putchar(' ', pc_row, fix_col, attr);
1509 --curwin->w_wcol;
1510 pc_status = PC_STATUS_RIGHT;
1511 }
1512 }
1513# endif
1514 }
1515 else
1516#endif
1517 {
1518 pc_col += curwin->w_wcol;
1519#ifdef FEAT_MBYTE
1520 if (mb_lefthalve(pc_row, pc_col))
1521 pc_status = PC_STATUS_LEFT;
1522#endif
1523 }
1524
1525 /* save the character to be able to put it back */
1526#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1527 if (pc_status == PC_STATUS_UNSET)
1528#endif
1529 {
1530 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1531 pc_status = PC_STATUS_SET;
1532 }
1533 screen_putchar(c, pc_row, pc_col, attr);
1534 }
1535}
1536
1537/*
1538 * Undo the previous edit_putchar().
1539 */
1540 void
1541edit_unputchar()
1542{
1543 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1544 {
1545#if defined(FEAT_MBYTE)
1546 if (pc_status == PC_STATUS_RIGHT)
1547 ++curwin->w_wcol;
1548 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1549 redrawWinline(curwin->w_cursor.lnum, FALSE);
1550 else
1551#endif
1552 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1553 }
1554}
1555
1556/*
1557 * Called when p_dollar is set: display a '$' at the end of the changed text
1558 * Only works when cursor is in the line that changes.
1559 */
1560 void
1561display_dollar(col)
1562 colnr_T col;
1563{
1564 colnr_T save_col;
1565
1566 if (!redrawing())
1567 return;
1568
1569 cursor_off();
1570 save_col = curwin->w_cursor.col;
1571 curwin->w_cursor.col = col;
1572#ifdef FEAT_MBYTE
1573 if (has_mbyte)
1574 {
1575 char_u *p;
1576
1577 /* If on the last byte of a multi-byte move to the first byte. */
1578 p = ml_get_curline();
1579 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1580 }
1581#endif
1582 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1583 if (curwin->w_wcol < W_WIDTH(curwin))
1584 {
1585 edit_putchar('$', FALSE);
1586 dollar_vcol = curwin->w_virtcol;
1587 }
1588 curwin->w_cursor.col = save_col;
1589}
1590
1591/*
1592 * Call this function before moving the cursor from the normal insert position
1593 * in insert mode.
1594 */
1595 static void
1596undisplay_dollar()
1597{
1598 if (dollar_vcol)
1599 {
1600 dollar_vcol = 0;
1601 redrawWinline(curwin->w_cursor.lnum, FALSE);
1602 }
1603}
1604
1605/*
1606 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1607 * Keep the cursor on the same character.
1608 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1609 * type == INDENT_DEC decrease indent (for CTRL-D)
1610 * type == INDENT_SET set indent to "amount"
1611 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1612 */
1613 void
1614change_indent(type, amount, round, replaced)
1615 int type;
1616 int amount;
1617 int round;
1618 int replaced; /* replaced character, put on replace stack */
1619{
1620 int vcol;
1621 int last_vcol;
1622 int insstart_less; /* reduction for Insstart.col */
1623 int new_cursor_col;
1624 int i;
1625 char_u *ptr;
1626 int save_p_list;
1627 int start_col;
1628 colnr_T vc;
1629#ifdef FEAT_VREPLACE
1630 colnr_T orig_col = 0; /* init for GCC */
1631 char_u *new_line, *orig_line = NULL; /* init for GCC */
1632
1633 /* VREPLACE mode needs to know what the line was like before changing */
1634 if (State & VREPLACE_FLAG)
1635 {
1636 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1637 orig_col = curwin->w_cursor.col;
1638 }
1639#endif
1640
1641 /* for the following tricks we don't want list mode */
1642 save_p_list = curwin->w_p_list;
1643 curwin->w_p_list = FALSE;
1644 vc = getvcol_nolist(&curwin->w_cursor);
1645 vcol = vc;
1646
1647 /*
1648 * For Replace mode we need to fix the replace stack later, which is only
1649 * possible when the cursor is in the indent. Remember the number of
1650 * characters before the cursor if it's possible.
1651 */
1652 start_col = curwin->w_cursor.col;
1653
1654 /* determine offset from first non-blank */
1655 new_cursor_col = curwin->w_cursor.col;
1656 beginline(BL_WHITE);
1657 new_cursor_col -= curwin->w_cursor.col;
1658
1659 insstart_less = curwin->w_cursor.col;
1660
1661 /*
1662 * If the cursor is in the indent, compute how many screen columns the
1663 * cursor is to the left of the first non-blank.
1664 */
1665 if (new_cursor_col < 0)
1666 vcol = get_indent() - vcol;
1667
1668 if (new_cursor_col > 0) /* can't fix replace stack */
1669 start_col = -1;
1670
1671 /*
1672 * Set the new indent. The cursor will be put on the first non-blank.
1673 */
1674 if (type == INDENT_SET)
1675 (void)set_indent(amount, SIN_CHANGED);
1676 else
1677 {
1678#ifdef FEAT_VREPLACE
1679 int save_State = State;
1680
1681 /* Avoid being called recursively. */
1682 if (State & VREPLACE_FLAG)
1683 State = INSERT;
1684#endif
1685 shift_line(type == INDENT_DEC, round, 1);
1686#ifdef FEAT_VREPLACE
1687 State = save_State;
1688#endif
1689 }
1690 insstart_less -= curwin->w_cursor.col;
1691
1692 /*
1693 * Try to put cursor on same character.
1694 * If the cursor is at or after the first non-blank in the line,
1695 * compute the cursor column relative to the column of the first
1696 * non-blank character.
1697 * If we are not in insert mode, leave the cursor on the first non-blank.
1698 * If the cursor is before the first non-blank, position it relative
1699 * to the first non-blank, counted in screen columns.
1700 */
1701 if (new_cursor_col >= 0)
1702 {
1703 /*
1704 * When changing the indent while the cursor is touching it, reset
1705 * Insstart_col to 0.
1706 */
1707 if (new_cursor_col == 0)
1708 insstart_less = MAXCOL;
1709 new_cursor_col += curwin->w_cursor.col;
1710 }
1711 else if (!(State & INSERT))
1712 new_cursor_col = curwin->w_cursor.col;
1713 else
1714 {
1715 /*
1716 * Compute the screen column where the cursor should be.
1717 */
1718 vcol = get_indent() - vcol;
1719 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1720
1721 /*
1722 * Advance the cursor until we reach the right screen column.
1723 */
1724 vcol = last_vcol = 0;
1725 new_cursor_col = -1;
1726 ptr = ml_get_curline();
1727 while (vcol <= (int)curwin->w_virtcol)
1728 {
1729 last_vcol = vcol;
1730#ifdef FEAT_MBYTE
1731 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001732 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 else
1734#endif
1735 ++new_cursor_col;
1736 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1737 }
1738 vcol = last_vcol;
1739
1740 /*
1741 * May need to insert spaces to be able to position the cursor on
1742 * the right screen column.
1743 */
1744 if (vcol != (int)curwin->w_virtcol)
1745 {
1746 curwin->w_cursor.col = new_cursor_col;
1747 i = (int)curwin->w_virtcol - vcol;
1748 ptr = alloc(i + 1);
1749 if (ptr != NULL)
1750 {
1751 new_cursor_col += i;
1752 ptr[i] = NUL;
1753 while (--i >= 0)
1754 ptr[i] = ' ';
1755 ins_str(ptr);
1756 vim_free(ptr);
1757 }
1758 }
1759
1760 /*
1761 * When changing the indent while the cursor is in it, reset
1762 * Insstart_col to 0.
1763 */
1764 insstart_less = MAXCOL;
1765 }
1766
1767 curwin->w_p_list = save_p_list;
1768
1769 if (new_cursor_col <= 0)
1770 curwin->w_cursor.col = 0;
1771 else
1772 curwin->w_cursor.col = new_cursor_col;
1773 curwin->w_set_curswant = TRUE;
1774 changed_cline_bef_curs();
1775
1776 /*
1777 * May have to adjust the start of the insert.
1778 */
1779 if (State & INSERT)
1780 {
1781 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1782 {
1783 if ((int)Insstart.col <= insstart_less)
1784 Insstart.col = 0;
1785 else
1786 Insstart.col -= insstart_less;
1787 }
1788 if ((int)ai_col <= insstart_less)
1789 ai_col = 0;
1790 else
1791 ai_col -= insstart_less;
1792 }
1793
1794 /*
1795 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1796 * If the number of characters before the cursor decreased, need to pop a
1797 * few characters from the replace stack.
1798 * If the number of characters before the cursor increased, need to push a
1799 * few NULs onto the replace stack.
1800 */
1801 if (REPLACE_NORMAL(State) && start_col >= 0)
1802 {
1803 while (start_col > (int)curwin->w_cursor.col)
1804 {
1805 replace_join(0); /* remove a NUL from the replace stack */
1806 --start_col;
1807 }
1808 while (start_col < (int)curwin->w_cursor.col || replaced)
1809 {
1810 replace_push(NUL);
1811 if (replaced)
1812 {
1813 replace_push(replaced);
1814 replaced = NUL;
1815 }
1816 ++start_col;
1817 }
1818 }
1819
1820#ifdef FEAT_VREPLACE
1821 /*
1822 * For VREPLACE mode, we also have to fix the replace stack. In this case
1823 * it is always possible because we backspace over the whole line and then
1824 * put it back again the way we wanted it.
1825 */
1826 if (State & VREPLACE_FLAG)
1827 {
1828 /* If orig_line didn't allocate, just return. At least we did the job,
1829 * even if you can't backspace. */
1830 if (orig_line == NULL)
1831 return;
1832
1833 /* Save new line */
1834 new_line = vim_strsave(ml_get_curline());
1835 if (new_line == NULL)
1836 return;
1837
1838 /* We only put back the new line up to the cursor */
1839 new_line[curwin->w_cursor.col] = NUL;
1840
1841 /* Put back original line */
1842 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1843 curwin->w_cursor.col = orig_col;
1844
1845 /* Backspace from cursor to start of line */
1846 backspace_until_column(0);
1847
1848 /* Insert new stuff into line again */
1849 ins_bytes(new_line);
1850
1851 vim_free(new_line);
1852 }
1853#endif
1854}
1855
1856/*
1857 * Truncate the space at the end of a line. This is to be used only in an
1858 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1859 * modes.
1860 */
1861 void
1862truncate_spaces(line)
1863 char_u *line;
1864{
1865 int i;
1866
1867 /* find start of trailing white space */
1868 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1869 {
1870 if (State & REPLACE_FLAG)
1871 replace_join(0); /* remove a NUL from the replace stack */
1872 }
1873 line[i + 1] = NUL;
1874}
1875
1876#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1877 || defined(FEAT_COMMENTS) || defined(PROTO)
1878/*
1879 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1880 * modes correctly. May also be used when not in insert mode at all.
1881 */
1882 void
1883backspace_until_column(col)
1884 int col;
1885{
1886 while ((int)curwin->w_cursor.col > col)
1887 {
1888 curwin->w_cursor.col--;
1889 if (State & REPLACE_FLAG)
1890 replace_do_bs();
1891 else
1892 (void)del_char(FALSE);
1893 }
1894}
1895#endif
1896
1897#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1898/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001899 * CTRL-X pressed in Insert mode.
1900 */
1901 static void
1902ins_ctrl_x()
1903{
1904 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1905 * CTRL-V works like CTRL-N */
1906 if (ctrl_x_mode != CTRL_X_CMDLINE)
1907 {
1908 /* if the next ^X<> won't ADD nothing, then reset
1909 * compl_cont_status */
1910 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001911 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001912 else
1913 compl_cont_status = 0;
1914 /* We're not sure which CTRL-X mode it will be yet */
1915 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1916 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1917 edit_submode_pre = NULL;
1918 showmode();
1919 }
1920}
1921
1922/*
1923 * Return TRUE if the 'dict' or 'tsr' option can be used.
1924 */
1925 static int
1926has_compl_option(dict_opt)
1927 int dict_opt;
1928{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001929 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00001930# ifdef FEAT_SPELL
1931 && !curwin->w_p_spell
1932# endif
1933 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001934 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1935 {
1936 ctrl_x_mode = 0;
1937 edit_submode = NULL;
1938 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1939 : (char_u *)_("'thesaurus' option is empty"),
1940 hl_attr(HLF_E));
1941 if (emsg_silent == 0)
1942 {
1943 vim_beep();
1944 setcursor();
1945 out_flush();
1946 ui_delay(2000L, FALSE);
1947 }
1948 return FALSE;
1949 }
1950 return TRUE;
1951}
1952
1953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1955 * This depends on the current mode.
1956 */
1957 int
1958vim_is_ctrl_x_key(c)
1959 int c;
1960{
1961 /* Always allow ^R - let it's results then be checked */
1962 if (c == Ctrl_R)
1963 return TRUE;
1964
Bram Moolenaare3226be2005-12-18 22:10:00 +00001965 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001966 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001967 return TRUE;
1968
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 switch (ctrl_x_mode)
1970 {
1971 case 0: /* Not in any CTRL-X mode */
1972 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1973 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001974 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1976 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1977 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001978 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1979 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 case CTRL_X_SCROLL:
1981 return (c == Ctrl_Y || c == Ctrl_E);
1982 case CTRL_X_WHOLE_LINE:
1983 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1984 case CTRL_X_FILES:
1985 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1986 case CTRL_X_DICTIONARY:
1987 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1988 case CTRL_X_THESAURUS:
1989 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1990 case CTRL_X_TAGS:
1991 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1992#ifdef FEAT_FIND_ID
1993 case CTRL_X_PATH_PATTERNS:
1994 return (c == Ctrl_P || c == Ctrl_N);
1995 case CTRL_X_PATH_DEFINES:
1996 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1997#endif
1998 case CTRL_X_CMDLINE:
1999 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2000 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002001#ifdef FEAT_COMPL_FUNC
2002 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002003 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002004 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002005 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002006#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002007 case CTRL_X_SPELL:
2008 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
2010 EMSG(_(e_internal));
2011 return FALSE;
2012}
2013
2014/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002015 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 * case of the originally typed text is used, and the case of the completed
2017 * text is infered, ie this tries to work out what case you probably wanted
2018 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002019 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 */
2021 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002022ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 char_u *str;
2024 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002025 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 char_u *fname;
2027 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002028 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029{
2030 int has_lower = FALSE;
2031 int was_letter = FALSE;
2032 int idx;
2033
2034 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2035 {
2036 /* Infer case of completed part -- webb */
2037 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002038 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039
2040 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002041 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002043 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
2045 has_lower = TRUE;
2046 if (isupper(IObuff[idx]))
2047 {
2048 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002049 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2051 break;
2052 }
2053 }
2054 }
2055
2056 /*
2057 * Rule 2: No lower case, 2nd consecutive letter converted to
2058 * upper case.
2059 */
2060 if (!has_lower)
2061 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002062 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002064 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 && islower(IObuff[idx]))
2066 {
2067 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002068 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2070 break;
2071 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002072 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 }
2074 }
2075
2076 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002077 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078
Bram Moolenaar39f05632006-03-19 22:15:26 +00002079 return ins_compl_add(IObuff, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002081 return ins_compl_add(str, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082}
2083
2084/*
2085 * Add a match to the list of matches.
2086 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002087 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002088 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002090 int
Bram Moolenaar39f05632006-03-19 22:15:26 +00002091ins_compl_add(str, len, icase, fname, cptext, cdir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 char_u *str;
2093 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002094 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 char_u *fname;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002096 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002097 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002098 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002100 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002101 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102
2103 ui_breakcheck();
2104 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002105 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 if (len < 0)
2107 len = (int)STRLEN(str);
2108
2109 /*
2110 * If the same match is already present, don't add it.
2111 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002112 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002114 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 do
2116 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002117 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002118 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002119 && match->cp_str[len] == NUL)
2120 return NOTDONE;
2121 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002122 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
2124
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002125 /* Remove any popup menu before changing the list of matches. */
2126 ins_compl_del_pum();
2127
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 /*
2129 * Allocate a new match structure.
2130 * Copy the values to the new match structure.
2131 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002132 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002134 return FAIL;
2135 match->cp_number = -1;
2136 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002137 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002138 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
2140 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002141 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002143 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002144
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002146 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2148 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002149 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002150 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002151 && compl_curr_match->cp_fname != NULL
2152 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002153 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002154 else if (fname != NULL)
2155 {
2156 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002157 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002160 match->cp_fname = NULL;
2161 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002162
2163 if (cptext != NULL)
2164 {
2165 int i;
2166
2167 for (i = 0; i < CPT_COUNT; ++i)
2168 if (cptext[i] != NULL && *cptext[i] != NUL)
2169 match->cp_text[i] = vim_strsave(cptext[i]);
2170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
2172 /*
2173 * Link the new match structure in the list of matches.
2174 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002175 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002176 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 else if (dir == FORWARD)
2178 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002179 match->cp_next = compl_curr_match->cp_next;
2180 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 }
2182 else /* BACKWARD */
2183 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002184 match->cp_next = compl_curr_match;
2185 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002187 if (match->cp_next)
2188 match->cp_next->cp_prev = match;
2189 if (match->cp_prev)
2190 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002192 compl_first_match = match;
2193 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002195 /*
2196 * Find the longest common string if still doing that.
2197 */
2198 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2199 ins_compl_longest_match(match);
2200
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 return OK;
2202}
2203
2204/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002205 * Return TRUE if "str[len]" matches with match->cp_str, considering
2206 * match->cp_icase.
2207 */
2208 static int
2209ins_compl_equal(match, str, len)
2210 compl_T *match;
2211 char_u *str;
2212 int len;
2213{
2214 if (match->cp_icase)
2215 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2216 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2217}
2218
2219/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002220 * Reduce the longest common string for match "match".
2221 */
2222 static void
2223ins_compl_longest_match(match)
2224 compl_T *match;
2225{
2226 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002227 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002228 int had_match;
2229
2230 if (compl_leader == NULL)
2231 /* First match, use it as a whole. */
2232 compl_leader = vim_strsave(match->cp_str);
2233 else
2234 {
2235 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002236 p = compl_leader;
2237 s = match->cp_str;
2238 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002239 {
2240#ifdef FEAT_MBYTE
2241 if (has_mbyte)
2242 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002243 c1 = mb_ptr2char(p);
2244 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002245 }
2246 else
2247#endif
2248 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002249 c1 = *p;
2250 c2 = *s;
2251 }
2252 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2253 : (c1 != c2))
2254 break;
2255#ifdef FEAT_MBYTE
2256 if (has_mbyte)
2257 {
2258 mb_ptr_adv(p);
2259 mb_ptr_adv(s);
2260 }
2261 else
2262#endif
2263 {
2264 ++p;
2265 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002266 }
2267 }
2268
2269 if (*p != NUL)
2270 {
2271 /* Leader was shortened, need to change the inserted text. */
2272 *p = NUL;
2273 had_match = (curwin->w_cursor.col > compl_col);
2274 ins_compl_delete();
2275 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2276 ins_redraw(FALSE);
2277
2278 /* When the match isn't there (to avoid matching itself) remove it
2279 * again after redrawing. */
2280 if (!had_match)
2281 ins_compl_delete();
2282 }
2283
2284 compl_used_match = FALSE;
2285 }
2286}
2287
2288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 * Add an array of matches to the list of matches.
2290 * Frees matches[].
2291 */
2292 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002293ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 int num_matches;
2295 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002296 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297{
2298 int i;
2299 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002300 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301
Bram Moolenaar572cb562005-08-05 21:35:02 +00002302 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002303 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar39f05632006-03-19 22:15:26 +00002304 NULL, NULL, dir, 0)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002306 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 FreeWild(num_matches, matches);
2308}
2309
2310/* Make the completion list cyclic.
2311 * Return the number of matches (excluding the original).
2312 */
2313 static int
2314ins_compl_make_cyclic()
2315{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002316 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 int count = 0;
2318
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002319 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
2321 /*
2322 * Find the end of the list.
2323 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002324 match = compl_first_match;
2325 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002326 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002328 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 ++count;
2330 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002331 match->cp_next = compl_first_match;
2332 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 }
2334 return count;
2335}
2336
Bram Moolenaara94bc432006-03-10 21:42:59 +00002337/*
2338 * Start completion for the complete() function.
2339 * "startcol" is where the matched text starts (1 is first column).
2340 * "list" is the list of matches.
2341 */
2342 void
2343set_completion(startcol, list)
2344 int startcol;
2345 list_T *list;
2346{
2347 /* If already doing completions stop it. */
2348 if (ctrl_x_mode != 0)
2349 ins_compl_prep(' ');
2350 ins_compl_clear();
2351
2352 if (stop_arrow() == FAIL)
2353 return;
2354
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002355 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002356 startcol = curwin->w_cursor.col;
2357 compl_col = startcol;
2358 compl_length = curwin->w_cursor.col - startcol;
2359 /* compl_pattern doesn't need to be set */
2360 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2361 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar39f05632006-03-19 22:15:26 +00002362 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002363 return;
2364
2365 /* Handle like dictionary completion. */
2366 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2367
2368 ins_compl_add_list(list);
2369 compl_matches = ins_compl_make_cyclic();
2370 compl_started = TRUE;
2371 compl_used_match = TRUE;
2372
2373 compl_curr_match = compl_first_match;
2374 ins_complete(Ctrl_N);
2375 out_flush();
2376}
2377
2378
Bram Moolenaar9372a112005-12-06 19:59:18 +00002379/* "compl_match_array" points the currently displayed list of entries in the
2380 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002381static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002382static int compl_match_arraysize;
2383
2384/*
2385 * Update the screen and when there is any scrolling remove the popup menu.
2386 */
2387 static void
2388ins_compl_upd_pum()
2389{
2390 int h;
2391
2392 if (compl_match_array != NULL)
2393 {
2394 h = curwin->w_cline_height;
2395 update_screen(0);
2396 if (h != curwin->w_cline_height)
2397 ins_compl_del_pum();
2398 }
2399}
2400
2401/*
2402 * Remove any popup menu.
2403 */
2404 static void
2405ins_compl_del_pum()
2406{
2407 if (compl_match_array != NULL)
2408 {
2409 pum_undisplay();
2410 vim_free(compl_match_array);
2411 compl_match_array = NULL;
2412 }
2413}
2414
2415/*
2416 * Return TRUE if the popup menu should be displayed.
2417 */
2418 static int
2419pum_wanted()
2420{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002421 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002422 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002423 return FALSE;
2424
2425 /* The display looks bad on a B&W display. */
2426 if (t_colors < 8
2427#ifdef FEAT_GUI
2428 && !gui.in_use
2429#endif
2430 )
2431 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002432 return TRUE;
2433}
2434
2435/*
2436 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002437 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002438 */
2439 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002440pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002441{
2442 compl_T *compl;
2443 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002444
2445 /* Don't display the popup menu if there are no matches or there is only
2446 * one (ignoring the original text). */
2447 compl = compl_first_match;
2448 i = 0;
2449 do
2450 {
2451 if (compl == NULL
2452 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2453 break;
2454 compl = compl->cp_next;
2455 } while (compl != compl_first_match);
2456
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002457 if (strstr((char *)p_cot, "menuone") != NULL)
2458 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002459 return (i >= 2);
2460}
2461
2462/*
2463 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002464 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002465 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002466 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002467ins_compl_show_pum()
2468{
2469 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002470 compl_T *shown_compl = NULL;
2471 int did_find_shown_match = FALSE;
2472 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002473 int i;
2474 int cur = -1;
2475 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002476 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002477
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002478 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002479 return;
2480
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002481#if defined(FEAT_EVAL)
2482 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2483 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2484#endif
2485
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002486 /* Update the screen before drawing the popup menu over it. */
2487 update_screen(0);
2488
2489 if (compl_match_array == NULL)
2490 {
2491 /* Need to build the popup menu list. */
2492 compl_match_arraysize = 0;
2493 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002494 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002495 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002496 do
2497 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002498 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2499 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002500 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002501 ++compl_match_arraysize;
2502 compl = compl->cp_next;
2503 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002504 if (compl_match_arraysize == 0)
2505 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002506 compl_match_array = (pumitem_T *)alloc_clear(
2507 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002508 * compl_match_arraysize));
2509 if (compl_match_array != NULL)
2510 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002511 /* If the current match is the original text don't find the first
2512 * match after it, don't highlight anything. */
2513 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2514 shown_match_ok = TRUE;
2515
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002516 i = 0;
2517 compl = compl_first_match;
2518 do
2519 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002520 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2521 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002522 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002523 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002524 if (!shown_match_ok)
2525 {
2526 if (compl == compl_shown_match || did_find_shown_match)
2527 {
2528 /* This item is the shown match or this is the
2529 * first displayed item after the shown match. */
2530 compl_shown_match = compl;
2531 did_find_shown_match = TRUE;
2532 shown_match_ok = TRUE;
2533 }
2534 else
2535 /* Remember this displayed match for when the
2536 * shown match is just below it. */
2537 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002538 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002539 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002540
2541 if (compl->cp_text[CPT_ABBR] != NULL)
2542 compl_match_array[i].pum_text =
2543 compl->cp_text[CPT_ABBR];
2544 else
2545 compl_match_array[i].pum_text = compl->cp_str;
2546 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2547 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2548 if (compl->cp_text[CPT_MENU] != NULL)
2549 compl_match_array[i++].pum_extra =
2550 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002551 else
2552 compl_match_array[i++].pum_extra = compl->cp_fname;
2553 }
2554
2555 if (compl == compl_shown_match)
2556 {
2557 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002558
2559 /* When the original text is the shown match don't set
2560 * compl_shown_match. */
2561 if (compl->cp_flags & ORIGINAL_TEXT)
2562 shown_match_ok = TRUE;
2563
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002564 if (!shown_match_ok && shown_compl != NULL)
2565 {
2566 /* The shown match isn't displayed, set it to the
2567 * previously displayed match. */
2568 compl_shown_match = shown_compl;
2569 shown_match_ok = TRUE;
2570 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002571 }
2572 compl = compl->cp_next;
2573 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002574
2575 if (!shown_match_ok) /* no displayed match at all */
2576 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002577 }
2578 }
2579 else
2580 {
2581 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002582 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002583 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2584 || compl_match_array[i].pum_text
2585 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002586 {
2587 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002588 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002589 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002590 }
2591
2592 if (compl_match_array != NULL)
2593 {
2594 /* Compute the screen column of the start of the completed text.
2595 * Use the cursor to get all wrapping and other settings right. */
2596 col = curwin->w_cursor.col;
2597 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002598 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002599 curwin->w_cursor.col = col;
2600 }
2601}
2602
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603#define DICT_FIRST (1) /* use just first element in "dict" */
2604#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002605
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002607 * Add any identifiers that match the given pattern in the list of dictionary
2608 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 */
2610 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002611ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2612 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002614 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002615 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002617 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 char_u *ptr;
2619 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 char_u **files;
2622 int count;
2623 int i;
2624 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002625 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
Bram Moolenaar0b238792006-03-02 22:49:12 +00002627 if (*dict == NUL)
2628 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002629#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002630 /* When 'dictionary' is empty and spell checking is enabled use
2631 * "spell". */
2632 if (!thesaurus && curwin->w_p_spell)
2633 dict = (char_u *)"spell";
2634 else
2635#endif
2636 return;
2637 }
2638
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002640 if (buf == NULL)
2641 return;
2642
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 /* If 'infercase' is set, don't use 'smartcase' here */
2644 save_p_scs = p_scs;
2645 if (curbuf->b_p_inf)
2646 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002647
2648 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2649 * to only match at the start of a line. Otherwise just match the
2650 * pattern. */
2651 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2652 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002653 i = (int)STRLEN(pat) + 8;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002654 ptr = alloc(i);
2655 if (ptr == NULL)
2656 return;
2657 vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat);
2658 regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0);
2659 vim_free(ptr);
2660 }
2661 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002662 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002663 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002664 if (regmatch.regprog == NULL)
2665 goto theend;
2666 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2669 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002670 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {
2672 /* copy one dictionary file name into buf */
2673 if (flags == DICT_EXACT)
2674 {
2675 count = 1;
2676 files = &dict;
2677 }
2678 else
2679 {
2680 /* Expand wildcards in the dictionary name, but do not allow
2681 * backticks (for security, the 'dict' option may have been set in
2682 * a modeline). */
2683 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002684# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002685 if (!thesaurus && STRCMP(buf, "spell") == 0)
2686 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002687 else
2688# endif
2689 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 || expand_wildcards(1, &buf, &count, &files,
2691 EW_FILE|EW_SILENT) != OK)
2692 count = 0;
2693 }
2694
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002695# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002696 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002698 /* Complete from active spelling. Skip "\<" in the pattern, we
2699 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002700 if (pat[0] == '\\' && pat[1] == '<')
2701 ptr = pat + 2;
2702 else
2703 ptr = pat;
2704 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002706 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002707# endif
Bram Moolenaar0b238792006-03-02 22:49:12 +00002708 {
2709 ins_compl_files(count, files, thesaurus, flags,
2710 &regmatch, buf, &dir);
2711 if (flags != DICT_EXACT)
2712 FreeWild(count, files);
2713 }
2714 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 break;
2716 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002717
2718theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 p_scs = save_p_scs;
2720 vim_free(regmatch.regprog);
2721 vim_free(buf);
2722}
2723
Bram Moolenaar0b238792006-03-02 22:49:12 +00002724 static void
2725ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2726 int count;
2727 char_u **files;
2728 int thesaurus;
2729 int flags;
2730 regmatch_T *regmatch;
2731 char_u *buf;
2732 int *dir;
2733{
2734 char_u *ptr;
2735 int i;
2736 FILE *fp;
2737 int add_r;
2738
2739 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2740 {
2741 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2742 if (flags != DICT_EXACT)
2743 {
2744 vim_snprintf((char *)IObuff, IOSIZE,
2745 _("Scanning dictionary: %s"), (char *)files[i]);
2746 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2747 }
2748
2749 if (fp != NULL)
2750 {
2751 /*
2752 * Read dictionary file line by line.
2753 * Check each line for a match.
2754 */
2755 while (!got_int && !compl_interrupted
2756 && !vim_fgets(buf, LSIZE, fp))
2757 {
2758 ptr = buf;
2759 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2760 {
2761 ptr = regmatch->startp[0];
2762 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2763 ptr = find_line_end(ptr);
2764 else
2765 ptr = find_word_end(ptr);
2766 add_r = ins_compl_add_infercase(regmatch->startp[0],
2767 (int)(ptr - regmatch->startp[0]),
Bram Moolenaard289f132006-03-11 21:30:53 +00002768 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002769 if (thesaurus)
2770 {
2771 char_u *wstart;
2772
2773 /*
2774 * Add the other matches on the line
2775 */
2776 while (!got_int)
2777 {
2778 /* Find start of the next word. Skip white
2779 * space and punctuation. */
2780 ptr = find_word_start(ptr);
2781 if (*ptr == NUL || *ptr == NL)
2782 break;
2783 wstart = ptr;
2784
2785 /* Find end of the word and add it. */
2786#ifdef FEAT_MBYTE
2787 if (has_mbyte)
2788 /* Japanese words may have characters in
2789 * different classes, only separate words
2790 * with single-byte non-word characters. */
2791 while (*ptr != NUL)
2792 {
2793 int l = (*mb_ptr2len)(ptr);
2794
2795 if (l < 2 && !vim_iswordc(*ptr))
2796 break;
2797 ptr += l;
2798 }
2799 else
2800#endif
2801 ptr = find_word_end(ptr);
2802 add_r = ins_compl_add_infercase(wstart,
2803 (int)(ptr - wstart),
2804 p_ic, files[i], *dir, 0);
2805 }
2806 }
2807 if (add_r == OK)
2808 /* if dir was BACKWARD then honor it just once */
2809 *dir = FORWARD;
2810 else if (add_r == FAIL)
2811 break;
2812 /* avoid expensive call to vim_regexec() when at end
2813 * of line */
2814 if (*ptr == '\n' || got_int)
2815 break;
2816 }
2817 line_breakcheck();
2818 ins_compl_check_keys(50);
2819 }
2820 fclose(fp);
2821 }
2822 }
2823}
2824
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825/*
2826 * Find the start of the next word.
2827 * Returns a pointer to the first char of the word. Also stops at a NUL.
2828 */
2829 char_u *
2830find_word_start(ptr)
2831 char_u *ptr;
2832{
2833#ifdef FEAT_MBYTE
2834 if (has_mbyte)
2835 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002836 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 else
2838#endif
2839 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2840 ++ptr;
2841 return ptr;
2842}
2843
2844/*
2845 * Find the end of the word. Assumes it starts inside a word.
2846 * Returns a pointer to just after the word.
2847 */
2848 char_u *
2849find_word_end(ptr)
2850 char_u *ptr;
2851{
2852#ifdef FEAT_MBYTE
2853 int start_class;
2854
2855 if (has_mbyte)
2856 {
2857 start_class = mb_get_class(ptr);
2858 if (start_class > 1)
2859 while (*ptr != NUL)
2860 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002861 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 if (mb_get_class(ptr) != start_class)
2863 break;
2864 }
2865 }
2866 else
2867#endif
2868 while (vim_iswordc(*ptr))
2869 ++ptr;
2870 return ptr;
2871}
2872
2873/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002874 * Find the end of the line, omitting CR and NL at the end.
2875 * Returns a pointer to just after the line.
2876 */
2877 static char_u *
2878find_line_end(ptr)
2879 char_u *ptr;
2880{
2881 char_u *s;
2882
2883 s = ptr + STRLEN(ptr);
2884 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2885 --s;
2886 return s;
2887}
2888
2889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 * Free the list of completions
2891 */
2892 static void
2893ins_compl_free()
2894{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002895 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002896 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002898 vim_free(compl_pattern);
2899 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002900 vim_free(compl_leader);
2901 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002903 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002905
2906 ins_compl_del_pum();
2907 pum_clear();
2908
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002909 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 do
2911 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002912 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002913 compl_curr_match = compl_curr_match->cp_next;
2914 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002916 if (match->cp_flags & FREE_FNAME)
2917 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00002918 for (i = 0; i < CPT_COUNT; ++i)
2919 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002921 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2922 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923}
2924
2925 static void
2926ins_compl_clear()
2927{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002928 compl_cont_status = 0;
2929 compl_started = FALSE;
2930 compl_matches = 0;
2931 vim_free(compl_pattern);
2932 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002933 vim_free(compl_leader);
2934 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002936 vim_free(compl_orig_text);
2937 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002938 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939}
2940
2941/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002942 * Return TRUE when Insert completion is active.
2943 */
2944 int
2945ins_compl_active()
2946{
2947 return compl_started;
2948}
2949
2950/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002951 * Delete one character before the cursor and show the subset of the matches
2952 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00002953 * Returns the character to be used, NUL if the work is done and another char
2954 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00002955 */
2956 static int
2957ins_compl_bs()
2958{
2959 char_u *line;
2960 char_u *p;
2961
Bram Moolenaarc1e37902006-04-18 21:55:01 +00002962 line = ml_get_curline();
2963 p = line + curwin->w_cursor.col;
2964 mb_ptr_back(line, p);
2965
2966 /* Stop completion when the whole word was deleted. */
2967 if ((int)(p - line) - (int)compl_col <= 0)
2968 return K_BS;
2969
Bram Moolenaara6557602006-02-04 22:43:20 +00002970 if (curwin->w_cursor.col <= compl_col + compl_length)
2971 {
2972 /* Deleted more than what was used to find matches, need to look for
2973 * matches all over again. */
2974 ins_compl_free();
2975 compl_started = FALSE;
2976 compl_matches = 0;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002977 compl_cont_status = 0;
2978 compl_cont_mode = 0;
Bram Moolenaara6557602006-02-04 22:43:20 +00002979 }
2980
Bram Moolenaara6557602006-02-04 22:43:20 +00002981 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002982 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00002983 if (compl_leader != NULL)
2984 {
2985 ins_compl_del_pum();
2986 ins_compl_delete();
2987 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2988
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002989 if (compl_started)
2990 ins_compl_set_original_text(compl_leader);
2991 else
Bram Moolenaara6557602006-02-04 22:43:20 +00002992 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00002993#ifdef FEAT_SPELL
2994 spell_bad_len = 0; /* need to redetect bad word */
2995#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00002996 /* Matches were cleared, need to search for them now. */
2997 if (ins_complete(Ctrl_N) == FAIL)
2998 compl_cont_status = 0;
2999 else
3000 {
3001 /* Remove the completed word again. */
3002 ins_compl_delete();
3003 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3004 }
3005 }
3006
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003007 /* Go to the original text, since none of the matches is inserted. */
3008 if (compl_first_match->cp_prev != NULL
3009 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3010 compl_shown_match = compl_first_match->cp_prev;
3011 else
3012 compl_shown_match = compl_first_match;
3013 compl_curr_match = compl_shown_match;
3014 compl_shows_dir = compl_direction;
3015
Bram Moolenaara6557602006-02-04 22:43:20 +00003016 /* Show the popup menu with a different set of matches. */
3017 ins_compl_show_pum();
3018 compl_used_match = FALSE;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003019 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003020
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003021 return NUL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003022 }
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003023 return K_BS;
Bram Moolenaara6557602006-02-04 22:43:20 +00003024}
3025
3026/*
3027 * Append one character to the match leader. May reduce the number of
3028 * matches.
3029 */
3030 static void
3031ins_compl_addleader(c)
3032 int c;
3033{
3034#ifdef FEAT_MBYTE
3035 int cc;
3036
3037 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3038 {
3039 char_u buf[MB_MAXBYTES + 1];
3040
3041 (*mb_char2bytes)(c, buf);
3042 buf[cc] = NUL;
3043 ins_char_bytes(buf, cc);
3044 }
3045 else
3046#endif
3047 ins_char(c);
3048
3049 vim_free(compl_leader);
3050 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3051 curwin->w_cursor.col - compl_col);
3052 if (compl_leader != NULL)
3053 {
3054 /* Show the popup menu with a different set of matches. */
3055 ins_compl_del_pum();
3056 ins_compl_show_pum();
3057 compl_used_match = FALSE;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003058 compl_enter_selects = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003059 ins_compl_set_original_text(compl_leader);
3060 }
3061}
3062
3063/*
3064 * Set the first match, the original text.
3065 */
3066 static void
3067ins_compl_set_original_text(str)
3068 char_u *str;
3069{
3070 char_u *p;
3071
3072 /* Replace the original text entry. */
3073 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3074 {
3075 p = vim_strsave(str);
3076 if (p != NULL)
3077 {
3078 vim_free(compl_first_match->cp_str);
3079 compl_first_match->cp_str = p;
3080 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003081 }
3082}
3083
3084/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003085 * Append one character to the match leader. May reduce the number of
3086 * matches.
3087 */
3088 static void
3089ins_compl_addfrommatch()
3090{
3091 char_u *p;
3092 int len = curwin->w_cursor.col - compl_col;
3093 int c;
3094
3095 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003096 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003097 return;
3098 p += len;
3099#ifdef FEAT_MBYTE
3100 c = mb_ptr2char(p);
3101#else
3102 c = *p;
3103#endif
3104 ins_compl_addleader(c);
3105}
3106
3107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003109 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003110 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003112 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113ins_compl_prep(c)
3114 int c;
3115{
3116 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 int temp;
3118 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003119 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
3121 /* Forget any previous 'special' messages if this is actually
3122 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3123 */
3124 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3125 edit_submode_extra = NULL;
3126
3127 /* Ignore end of Select mode mapping */
3128 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003129 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003131 /* Set "compl_get_longest" when finding the first matches. */
3132 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3133 || (ctrl_x_mode == 0 && !compl_started))
3134 {
3135 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3136 compl_used_match = TRUE;
3137 }
3138
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3140 {
3141 /*
3142 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3143 * it will be yet. Now we decide.
3144 */
3145 switch (c)
3146 {
3147 case Ctrl_E:
3148 case Ctrl_Y:
3149 ctrl_x_mode = CTRL_X_SCROLL;
3150 if (!(State & REPLACE_FLAG))
3151 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3152 else
3153 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3154 edit_submode_pre = NULL;
3155 showmode();
3156 break;
3157 case Ctrl_L:
3158 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3159 break;
3160 case Ctrl_F:
3161 ctrl_x_mode = CTRL_X_FILES;
3162 break;
3163 case Ctrl_K:
3164 ctrl_x_mode = CTRL_X_DICTIONARY;
3165 break;
3166 case Ctrl_R:
3167 /* Simply allow ^R to happen without affecting ^X mode */
3168 break;
3169 case Ctrl_T:
3170 ctrl_x_mode = CTRL_X_THESAURUS;
3171 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003172#ifdef FEAT_COMPL_FUNC
3173 case Ctrl_U:
3174 ctrl_x_mode = CTRL_X_FUNCTION;
3175 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003176 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003177 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003178 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003179#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003180 case 's':
3181 case Ctrl_S:
3182 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003183#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003184 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003185 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003186 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003187#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003188 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 case Ctrl_RSB:
3190 ctrl_x_mode = CTRL_X_TAGS;
3191 break;
3192#ifdef FEAT_FIND_ID
3193 case Ctrl_I:
3194 case K_S_TAB:
3195 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3196 break;
3197 case Ctrl_D:
3198 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3199 break;
3200#endif
3201 case Ctrl_V:
3202 case Ctrl_Q:
3203 ctrl_x_mode = CTRL_X_CMDLINE;
3204 break;
3205 case Ctrl_P:
3206 case Ctrl_N:
3207 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3208 * just started ^X mode, or there were enough ^X's to cancel
3209 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3210 * do normal expansion when interrupting a different mode (say
3211 * ^X^F^X^P or ^P^X^X^P, see below)
3212 * nothing changes if interrupting mode 0, (eg, the flag
3213 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003214 if (!(compl_cont_status & CONT_INTRPT))
3215 compl_cont_status |= CONT_LOCAL;
3216 else if (compl_cont_mode != 0)
3217 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 /* FALLTHROUGH */
3219 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003220 /* If we have typed at least 2 ^X's... for modes != 0, we set
3221 * compl_cont_status = 0 (eg, as if we had just started ^X
3222 * mode).
3223 * For mode 0, we set "compl_cont_mode" to an impossible
3224 * value, in both cases ^X^X can be used to restart the same
3225 * mode (avoiding ADDING mode).
3226 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3227 * 'complete' and local ^P expansions respectively.
3228 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3229 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 if (c == Ctrl_X)
3231 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003232 if (compl_cont_mode != 0)
3233 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003235 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 }
3237 ctrl_x_mode = 0;
3238 edit_submode = NULL;
3239 showmode();
3240 break;
3241 }
3242 }
3243 else if (ctrl_x_mode != 0)
3244 {
3245 /* We're already in CTRL-X mode, do we stay in it? */
3246 if (!vim_is_ctrl_x_key(c))
3247 {
3248 if (ctrl_x_mode == CTRL_X_SCROLL)
3249 ctrl_x_mode = 0;
3250 else
3251 ctrl_x_mode = CTRL_X_FINISHED;
3252 edit_submode = NULL;
3253 }
3254 showmode();
3255 }
3256
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003257 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 {
3259 /* Show error message from attempted keyword completion (probably
3260 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003261 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003263 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3264 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 || ctrl_x_mode == CTRL_X_FINISHED)
3266 {
3267 /* Get here when we have finished typing a sequence of ^N and
3268 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003269 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003270 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003272 char_u *p;
3273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003275 * If any of the original typed text has been changed, eg when
3276 * ignorecase is set, we must add back-spaces to the redo
3277 * buffer. We add as few as necessary to delete just the part
3278 * of the original text that has changed.
3279 * When using the longest match, edited the match or used
3280 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003282 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3283 ptr = compl_curr_match->cp_str;
3284 else if (compl_leader != NULL)
3285 ptr = compl_leader;
3286 else
3287 ptr = compl_orig_text;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003288 p = compl_orig_text;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003289 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp]; ++temp)
3290 ;
3291#ifdef FEAT_MBYTE
3292 if (temp > 0)
3293 temp -= (*mb_head_off)(compl_orig_text, p + temp);
3294#endif
3295 for (p += temp; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 AppendCharToRedobuff(K_BS);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003297 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
3299
3300#ifdef FEAT_CINDENT
3301 want_cindent = (can_cindent && cindent_on());
3302#endif
3303 /*
3304 * When completing whole lines: fix indent for 'cindent'.
3305 * Otherwise, break line if it's too long.
3306 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003307 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
3309#ifdef FEAT_CINDENT
3310 /* re-indent the current line */
3311 if (want_cindent)
3312 {
3313 do_c_expr_indent();
3314 want_cindent = FALSE; /* don't do it again */
3315 }
3316#endif
3317 }
3318 else
3319 {
3320 /* put the cursor on the last char, for 'tw' formatting */
3321 curwin->w_cursor.col--;
3322 if (stop_arrow() == OK)
3323 insertchar(NUL, 0, -1);
3324 curwin->w_cursor.col++;
3325 }
3326
3327 auto_format(FALSE, TRUE);
3328
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003329 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003330 * the selection without inserting anything. When
3331 * compl_enter_selects is set the Enter key does the same. */
3332 if ((c == Ctrl_Y || (compl_enter_selects
3333 && (c == CAR || c == K_KENTER || c == NL)))
3334 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003335 retval = TRUE;
3336
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003337 /* CTRL-E means completion is Ended, go back to the typed text. */
3338 if (c == Ctrl_E)
3339 {
3340 ins_compl_delete();
3341 if (compl_leader != NULL)
3342 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3343 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003344 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003345 + curwin->w_cursor.col - compl_col);
3346 retval = TRUE;
3347 }
3348
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003350 compl_started = FALSE;
3351 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 msg_clr_cmdline(); /* necessary for "noshowmode" */
3353 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003354 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (edit_submode != NULL)
3356 {
3357 edit_submode = NULL;
3358 showmode();
3359 }
3360
3361#ifdef FEAT_CINDENT
3362 /*
3363 * Indent now if a key was typed that is in 'cinkeys'.
3364 */
3365 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3366 do_c_expr_indent();
3367#endif
3368 }
3369 }
3370
3371 /* reset continue_* if we left expansion-mode, if we stay they'll be
3372 * (re)set properly in ins_complete() */
3373 if (!vim_is_ctrl_x_key(c))
3374 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003375 compl_cont_status = 0;
3376 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003378
3379 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380}
3381
3382/*
3383 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3384 * (depending on flag) starting from buf and looking for a non-scanned
3385 * buffer (other than curbuf). curbuf is special, if it is called with
3386 * buf=curbuf then it has to be the first call for a given flag/expansion.
3387 *
3388 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3389 */
3390 static buf_T *
3391ins_compl_next_buf(buf, flag)
3392 buf_T *buf;
3393 int flag;
3394{
3395#ifdef FEAT_WINDOWS
3396 static win_T *wp;
3397#endif
3398
3399 if (flag == 'w') /* just windows */
3400 {
3401#ifdef FEAT_WINDOWS
3402 if (buf == curbuf) /* first call for this flag/expansion */
3403 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003404 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 && wp->w_buffer->b_scanned)
3406 ;
3407 buf = wp->w_buffer;
3408#else
3409 buf = curbuf;
3410#endif
3411 }
3412 else
3413 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3414 * (unlisted buffers)
3415 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003416 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 && ((flag == 'U'
3418 ? buf->b_p_bl
3419 : (!buf->b_p_bl
3420 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003421 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 ;
3423 return buf;
3424}
3425
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003426#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003427static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003428
3429/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003430 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003431 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003432 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003433 static void
3434expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003435 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003436 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003437{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003438 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003439 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003440 char_u *funcname;
3441 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003442
Bram Moolenaare344bea2005-09-01 20:46:49 +00003443 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3444 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003445 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003446
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003447 /* Call 'completefunc' to obtain the list of matches. */
3448 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003449 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003450
Bram Moolenaare344bea2005-09-01 20:46:49 +00003451 pos = curwin->w_cursor;
3452 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3453 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003454 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003455 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003456
Bram Moolenaara94bc432006-03-10 21:42:59 +00003457 ins_compl_add_list(matchlist);
3458 list_unref(matchlist);
3459}
3460#endif /* FEAT_COMPL_FUNC */
3461
Bram Moolenaar39f05632006-03-19 22:15:26 +00003462#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003463/*
3464 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003465 */
3466 static void
3467ins_compl_add_list(list)
3468 list_T *list;
3469{
3470 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003471 int dir = compl_direction;
3472
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003473 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003474 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003475 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003476 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3477 /* if dir was BACKWARD then honor it just once */
3478 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003479 else if (did_emsg)
3480 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003481 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003482}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003483
3484/*
3485 * Add a match to the list of matches from a typeval_T.
3486 * If the given string is already in the list of completions, then return
3487 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3488 * maybe because alloc() returns NULL, then FAIL is returned.
3489 */
3490 int
3491ins_compl_add_tv(tv, dir)
3492 typval_T *tv;
3493 int dir;
3494{
3495 char_u *word;
3496 int icase = p_ic;
3497 char_u *(cptext[CPT_COUNT]);
3498
3499 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3500 {
3501 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3502 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3503 (char_u *)"abbr", FALSE);
3504 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3505 (char_u *)"menu", FALSE);
3506 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3507 (char_u *)"kind", FALSE);
3508 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3509 (char_u *)"info", FALSE);
3510 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3511 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
3512 }
3513 else
3514 {
3515 word = get_tv_string_chk(tv);
3516 vim_memset(cptext, 0, sizeof(cptext));
3517 }
3518 if (word == NULL || *word == NUL)
3519 return FAIL;
3520 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0);
3521}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003522#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003523
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003524/*
3525 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003526 * The search starts at position "ini" in curbuf and in the direction
3527 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003528 * When "compl_started" is FALSE start at that position, otherwise continue
3529 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003530 * This may return before finding all the matches.
3531 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 */
3533 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003534ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536{
3537 static pos_T first_match_pos;
3538 static pos_T last_match_pos;
3539 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003540 static int found_all = FALSE; /* Found all matches of a
3541 certain type. */
3542 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
Bram Moolenaar572cb562005-08-05 21:35:02 +00003544 pos_T *pos;
3545 char_u **matches;
3546 int save_p_scs;
3547 int save_p_ws;
3548 int save_p_ic;
3549 int i;
3550 int num_matches;
3551 int len;
3552 int found_new_match;
3553 int type = ctrl_x_mode;
3554 char_u *ptr;
3555 char_u *dict = NULL;
3556 int dict_f = 0;
3557 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003559 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
3561 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3562 ins_buf->b_scanned = 0;
3563 found_all = FALSE;
3564 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003565 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003566 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 last_match_pos = first_match_pos = *ini;
3568 }
3569
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003570 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003571 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3573 for (;;)
3574 {
3575 found_new_match = FAIL;
3576
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003577 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 * or if found_all says this entry is done. For ^X^L only use the
3579 * entries from 'complete' that look in loaded buffers. */
3580 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003581 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
3583 found_all = FALSE;
3584 while (*e_cpt == ',' || *e_cpt == ' ')
3585 e_cpt++;
3586 if (*e_cpt == '.' && !curbuf->b_scanned)
3587 {
3588 ins_buf = curbuf;
3589 first_match_pos = *ini;
3590 /* So that ^N can match word immediately after cursor */
3591 if (ctrl_x_mode == 0)
3592 dec(&first_match_pos);
3593 last_match_pos = first_match_pos;
3594 type = 0;
3595 }
3596 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3597 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3598 {
3599 /* Scan a buffer, but not the current one. */
3600 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3601 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003602 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 first_match_pos.col = last_match_pos.col = 0;
3604 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3605 last_match_pos.lnum = 0;
3606 type = 0;
3607 }
3608 else /* unloaded buffer, scan like dictionary */
3609 {
3610 found_all = TRUE;
3611 if (ins_buf->b_fname == NULL)
3612 continue;
3613 type = CTRL_X_DICTIONARY;
3614 dict = ins_buf->b_fname;
3615 dict_f = DICT_EXACT;
3616 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003617 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 ins_buf->b_fname == NULL
3619 ? buf_spname(ins_buf)
3620 : ins_buf->b_sfname == NULL
3621 ? (char *)ins_buf->b_fname
3622 : (char *)ins_buf->b_sfname);
3623 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3624 }
3625 else if (*e_cpt == NUL)
3626 break;
3627 else
3628 {
3629 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3630 type = -1;
3631 else if (*e_cpt == 'k' || *e_cpt == 's')
3632 {
3633 if (*e_cpt == 'k')
3634 type = CTRL_X_DICTIONARY;
3635 else
3636 type = CTRL_X_THESAURUS;
3637 if (*++e_cpt != ',' && *e_cpt != NUL)
3638 {
3639 dict = e_cpt;
3640 dict_f = DICT_FIRST;
3641 }
3642 }
3643#ifdef FEAT_FIND_ID
3644 else if (*e_cpt == 'i')
3645 type = CTRL_X_PATH_PATTERNS;
3646 else if (*e_cpt == 'd')
3647 type = CTRL_X_PATH_DEFINES;
3648#endif
3649 else if (*e_cpt == ']' || *e_cpt == 't')
3650 {
3651 type = CTRL_X_TAGS;
3652 sprintf((char*)IObuff, _("Scanning tags."));
3653 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3654 }
3655 else
3656 type = -1;
3657
3658 /* in any case e_cpt is advanced to the next entry */
3659 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3660
3661 found_all = TRUE;
3662 if (type == -1)
3663 continue;
3664 }
3665 }
3666
3667 switch (type)
3668 {
3669 case -1:
3670 break;
3671#ifdef FEAT_FIND_ID
3672 case CTRL_X_PATH_PATTERNS:
3673 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003674 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003675 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003677 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3679 (linenr_T)1, (linenr_T)MAXLNUM);
3680 break;
3681#endif
3682
3683 case CTRL_X_DICTIONARY:
3684 case CTRL_X_THESAURUS:
3685 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003686 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 : (type == CTRL_X_THESAURUS
3688 ? (*curbuf->b_p_tsr == NUL
3689 ? p_tsr
3690 : curbuf->b_p_tsr)
3691 : (*curbuf->b_p_dict == NUL
3692 ? p_dict
3693 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003694 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003695 dict != NULL ? dict_f
3696 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 dict = NULL;
3698 break;
3699
3700 case CTRL_X_TAGS:
3701 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3702 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003703 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003706 * of matches is found when compl_pattern is empty */
3707 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3709 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3710 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3711 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003712 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 }
3714 p_ic = save_p_ic;
3715 break;
3716
3717 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003718 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3720 {
3721
3722 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003723 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003724 ins_compl_add_matches(num_matches, matches,
3725#ifdef CASE_INSENSITIVE_FILENAME
3726 TRUE
3727#else
3728 FALSE
3729#endif
3730 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732 break;
3733
3734 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003735 if (expand_cmdline(&compl_xp, compl_pattern,
3736 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003738 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 break;
3740
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003741#ifdef FEAT_COMPL_FUNC
3742 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003743 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003744 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003745 break;
3746#endif
3747
Bram Moolenaar488c6512005-08-11 20:09:58 +00003748 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003749#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003750 num_matches = expand_spelling(first_match_pos.lnum,
3751 first_match_pos.col, compl_pattern, &matches);
3752 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003753 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003754#endif
3755 break;
3756
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 default: /* normal ^P/^N and ^X^L */
3758 /*
3759 * If 'infercase' is set, don't use 'smartcase' here
3760 */
3761 save_p_scs = p_scs;
3762 if (ins_buf->b_p_inf)
3763 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003764
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 /* buffers other than curbuf are scanned from the beginning or the
3766 * end but never from the middle, thus setting nowrapscan in this
3767 * buffers is a good idea, on the other hand, we always set
3768 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3769 save_p_ws = p_ws;
3770 if (ins_buf != curbuf)
3771 p_ws = FALSE;
3772 else if (*e_cpt == '.')
3773 p_ws = TRUE;
3774 for (;;)
3775 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003776 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003778 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3779 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003781 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003783 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003785 found_new_match = searchit(NULL, ins_buf, pos,
3786 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003787 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003788 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003789 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003791 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003792 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 first_match_pos = *pos;
3794 last_match_pos = *pos;
3795 }
3796 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003797 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 found_new_match = FAIL;
3799 if (found_new_match == FAIL)
3800 {
3801 if (ins_buf == curbuf)
3802 found_all = TRUE;
3803 break;
3804 }
3805
3806 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003807 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 && ini->lnum == pos->lnum
3809 && ini->col == pos->col)
3810 continue;
3811 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3812 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3813 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003814 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 {
3816 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3817 continue;
3818 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3819 if (!p_paste)
3820 ptr = skipwhite(ptr);
3821 }
3822 len = (int)STRLEN(ptr);
3823 }
3824 else
3825 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003826 char_u *tmp_ptr = ptr;
3827
3828 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003830 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 /* Skip if already inside a word. */
3832 if (vim_iswordp(tmp_ptr))
3833 continue;
3834 /* Find start of next word. */
3835 tmp_ptr = find_word_start(tmp_ptr);
3836 }
3837 /* Find end of this word. */
3838 tmp_ptr = find_word_end(tmp_ptr);
3839 len = (int)(tmp_ptr - ptr);
3840
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003841 if ((compl_cont_status & CONT_ADDING)
3842 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 {
3844 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3845 {
3846 /* Try next line, if any. the new word will be
3847 * "join" as if the normal command "J" was used.
3848 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003849 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 * works -- Acevedo */
3851 STRNCPY(IObuff, ptr, len);
3852 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3853 tmp_ptr = ptr = skipwhite(ptr);
3854 /* Find start of next word. */
3855 tmp_ptr = find_word_start(tmp_ptr);
3856 /* Find end of next word. */
3857 tmp_ptr = find_word_end(tmp_ptr);
3858 if (tmp_ptr > ptr)
3859 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003860 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003862 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 IObuff[len++] = ' ';
3864 /* IObuf =~ "\k.* ", thus len >= 2 */
3865 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003866 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 || (vim_strchr(p_cpo, CPO_JOINSP)
3868 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003869 && (IObuff[len - 2] == '?'
3870 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 IObuff[len++] = ' ';
3872 }
3873 /* copy as much as posible of the new word */
3874 if (tmp_ptr - ptr >= IOSIZE - len)
3875 tmp_ptr = ptr + IOSIZE - len - 1;
3876 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3877 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003878 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 }
3880 IObuff[len] = NUL;
3881 ptr = IObuff;
3882 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003883 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 continue;
3885 }
3886 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003887 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003888 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003889 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 {
3891 found_new_match = OK;
3892 break;
3893 }
3894 }
3895 p_scs = save_p_scs;
3896 p_ws = save_p_ws;
3897 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003898
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003899 /* check if compl_curr_match has changed, (e.g. other type of
3900 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003901 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 found_new_match = OK;
3903
3904 /* break the loop for specialized modes (use 'complete' just for the
3905 * generic ctrl_x_mode == 0) or when we've found a new match */
3906 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003907 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003908 {
3909 if (got_int)
3910 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003911 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003912 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003913 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003914
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003915 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3916 || compl_interrupted)
3917 break;
3918 compl_started = TRUE;
3919 }
3920 else
3921 {
3922 /* Mark a buffer scanned when it has been scanned completely */
3923 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3924 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003926 compl_started = FALSE;
3927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003929 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
3931 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3932 && *e_cpt == NUL) /* Got to end of 'complete' */
3933 found_new_match = FAIL;
3934
3935 i = -1; /* total of matches, unknown */
3936 if (found_new_match == FAIL
3937 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3938 i = ins_compl_make_cyclic();
3939
3940 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003941 * just been made cyclic then we have to move compl_curr_match to the next
3942 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003943 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
3944 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003945 if (compl_curr_match == NULL)
3946 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 return i;
3948}
3949
3950/* Delete the old text being completed. */
3951 static void
3952ins_compl_delete()
3953{
3954 int i;
3955
3956 /*
3957 * In insert mode: Delete the typed part.
3958 * In replace mode: Put the old characters back, if any.
3959 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003960 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 backspace_until_column(i);
3962 changed_cline_bef_curs();
3963}
3964
3965/* Insert the new text being completed. */
3966 static void
3967ins_compl_insert()
3968{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003969 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003970 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3971 compl_used_match = FALSE;
3972 else
3973 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974}
3975
3976/*
3977 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003978 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3979 * get more completions. If it is FALSE, then we just do nothing when there
3980 * are no more completions in a given direction. The latter case is used when
3981 * we are still in the middle of finding completions, to allow browsing
3982 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 * Return the total number of matches, or -1 if still unknown -- webb.
3984 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003985 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3986 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 *
3988 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003989 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3990 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 */
3992 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003993ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003995 int count; /* repeat completion this many times; should
3996 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003997 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998{
3999 int num_matches = -1;
4000 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004001 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004002 compl_T *found_compl = NULL;
4003 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004004 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004006 if (compl_leader != NULL
4007 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004009 /* Set "compl_shown_match" to the actually shown match, it may differ
4010 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004011 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004012 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004013 && compl_shown_match->cp_next != NULL
4014 && compl_shown_match->cp_next != compl_first_match)
4015 compl_shown_match = compl_shown_match->cp_next;
4016 }
4017
4018 if (allow_get_expansion && insert_match
4019 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 /* Delete old text to be replaced */
4021 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004022
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004023 /* When finding the longest common text we stick at the original text,
4024 * don't let CTRL-N or CTRL-P move to the first match. */
4025 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4026
Bram Moolenaare3226be2005-12-18 22:10:00 +00004027 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4028 * around. */
4029 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004031 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004033 if (compl_pending != 0)
4034 --compl_pending;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004035 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004036 found_end = (compl_first_match != NULL
4037 && (compl_shown_match->cp_next == compl_first_match
4038 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004039 }
4040 else if (compl_shows_dir == BACKWARD
4041 && compl_shown_match->cp_prev != NULL)
4042 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004043 if (compl_pending != 0)
4044 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00004045 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004046 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004047 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
4049 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004050 {
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004051 if (advance)
4052 {
4053 if (compl_shows_dir == BACKWARD)
4054 --compl_pending;
4055 else
4056 ++compl_pending;
4057 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004058 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004059 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00004060
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004061 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004062 if (compl_pending != 0 && compl_direction == compl_shows_dir
4063 && advance)
Bram Moolenaara6557602006-02-04 22:43:20 +00004064 compl_shown_match = compl_curr_match;
4065 found_end = FALSE;
4066 }
4067 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4068 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004069 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004070 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004071 ++todo;
4072 else
4073 /* Remember a matching item. */
4074 found_compl = compl_shown_match;
4075
4076 /* Stop at the end of the list when we found a usable match. */
4077 if (found_end)
4078 {
4079 if (found_compl != NULL)
4080 {
4081 compl_shown_match = found_compl;
4082 break;
4083 }
4084 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
4087
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004088 /* Insert the text of the new completion, or the compl_leader. */
4089 if (insert_match)
4090 {
4091 if (!compl_get_longest || compl_used_match)
4092 ins_compl_insert();
4093 else
4094 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4095 }
4096 else
4097 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
4099 if (!allow_get_expansion)
4100 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004101 /* may undisplay the popup menu first */
4102 ins_compl_upd_pum();
4103
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004104 /* redraw to show the user what was inserted */
4105 update_screen(0);
4106
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004107 /* display the updated popup menu */
4108 ins_compl_show_pum();
4109
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 /* Delete old text to be replaced, since we're still searching and
4111 * don't want to match ourselves! */
4112 ins_compl_delete();
4113 }
4114
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004115 /* Enter will select a match when the match wasn't inserted and the popup
4116 * menu is visislbe. */
4117 compl_enter_selects = !insert_match && compl_match_array != NULL;
4118
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 /*
4120 * Show the file name for the match (if any)
4121 * Truncate the file name to avoid a wait for return.
4122 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004123 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
4125 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004126 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 if (i <= 0)
4128 i = 0;
4129 else
4130 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004131 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 msg(IObuff);
4133 redraw_cmdline = FALSE; /* don't overwrite! */
4134 }
4135
4136 return num_matches;
4137}
4138
4139/*
4140 * Call this while finding completions, to check whether the user has hit a key
4141 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004142 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004144 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 */
4146 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004147ins_compl_check_keys(frequency)
4148 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149{
4150 static int count = 0;
4151
4152 int c;
4153
4154 /* Don't check when reading keys from a script. That would break the test
4155 * scripts */
4156 if (using_script())
4157 return;
4158
4159 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004160 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 return;
4162 count = 0;
4163
4164 ++no_mapping;
4165 c = vpeekc_any();
4166 --no_mapping;
4167 if (c != NUL)
4168 {
4169 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4170 {
4171 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004172 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004173 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4174 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 }
4176 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004177 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004179 if (compl_pending != 0 && !got_int)
4180 (void)ins_compl_next(FALSE, compl_pending > 0
4181 ? compl_pending : -compl_pending, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004182}
4183
4184/*
4185 * Decide the direction of Insert mode complete from the key typed.
4186 * Returns BACKWARD or FORWARD.
4187 */
4188 static int
4189ins_compl_key2dir(c)
4190 int c;
4191{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004192 if (c == Ctrl_P || c == Ctrl_L
4193 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4194 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004195 return BACKWARD;
4196 return FORWARD;
4197}
4198
4199/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004200 * Return TRUE for keys that are used for completion only when the popup menu
4201 * is visible.
4202 */
4203 static int
4204ins_compl_pum_key(c)
4205 int c;
4206{
4207 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004208 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4209 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004210}
4211
4212/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004213 * Decide the number of completions to move forward.
4214 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4215 */
4216 static int
4217ins_compl_key2count(c)
4218 int c;
4219{
4220 int h;
4221
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004222 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004223 {
4224 h = pum_get_height();
4225 if (h > 3)
4226 h -= 2; /* keep some context */
4227 return h;
4228 }
4229 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230}
4231
4232/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004233 * Return TRUE if completion with "c" should insert the match, FALSE if only
4234 * to change the currently selected completion.
4235 */
4236 static int
4237ins_compl_use_match(c)
4238 int c;
4239{
4240 switch (c)
4241 {
4242 case K_UP:
4243 case K_DOWN:
4244 case K_PAGEDOWN:
4245 case K_KPAGEDOWN:
4246 case K_S_DOWN:
4247 case K_PAGEUP:
4248 case K_KPAGEUP:
4249 case K_S_UP:
4250 return FALSE;
4251 }
4252 return TRUE;
4253}
4254
4255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 * Do Insert mode completion.
4257 * Called when character "c" was typed, which has a meaning for completion.
4258 * Returns OK if completion was done, FAIL if something failed (out of mem).
4259 */
4260 static int
4261ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004262 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004264 char_u *line;
4265 int startcol = 0; /* column where searched text starts */
4266 colnr_T curs_col; /* cursor column */
4267 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268
Bram Moolenaare3226be2005-12-18 22:10:00 +00004269 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004270 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 {
4272 /* First time we hit ^N or ^P (in a row, I mean) */
4273
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 did_ai = FALSE;
4275#ifdef FEAT_SMARTINDENT
4276 did_si = FALSE;
4277 can_si = FALSE;
4278 can_si_back = FALSE;
4279#endif
4280 if (stop_arrow() == FAIL)
4281 return FAIL;
4282
4283 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004284 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004285 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
4287 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004288 * "compl_startpos" to the cursor as a pattern to add a new word
4289 * instead of expand the one before the cursor, in word-wise if
4290 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 * is not in the same line as the cursor then fix it (the line has
4292 * been split because it was longer than 'tw'). if SOL is set then
4293 * skip the previous pattern, a word at the beginning of the line has
4294 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004295 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4296 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 {
4298 /*
4299 * it is a continued search
4300 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004301 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4303 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4304 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004305 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004307 /* line (probably) wrapped, set compl_startpos to the
4308 * first non_blank in the line, if it is not a wordchar
4309 * include it to get a better pattern, but then we don't
4310 * want the "\\<" prefix, check it bellow */
4311 compl_col = (colnr_T)(skipwhite(line) - line);
4312 compl_startpos.col = compl_col;
4313 compl_startpos.lnum = curwin->w_cursor.lnum;
4314 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
4316 else
4317 {
4318 /* S_IPOS was set when we inserted a word that was at the
4319 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004320 * mode but first we need to redefine compl_startpos */
4321 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004323 compl_cont_status |= CONT_SOL;
4324 compl_startpos.col = (colnr_T)(skipwhite(
4325 line + compl_length
4326 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004328 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004330 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004331 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 * have enough space? just being paranoic */
4333#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004334 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004336 compl_cont_status &= ~CONT_SOL;
4337 compl_length = (IOSIZE - MIN_SPACE);
4338 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004340 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4341 if (compl_length < 1)
4342 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004345 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004347 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004350 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004352 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004354 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004356 compl_cont_status = 0;
4357 compl_cont_status |= CONT_N_ADDS;
4358 compl_startpos = curwin->w_cursor;
4359 startcol = (int)curs_col;
4360 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362
4363 /* Work out completion pattern and original text -- webb */
4364 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4365 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004366 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4368 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004369 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004371 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004373 compl_col += ++startcol;
4374 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004377 compl_pattern = str_foldcase(line + compl_col,
4378 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004380 compl_pattern = vim_strnsave(line + compl_col,
4381 compl_length);
4382 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 return FAIL;
4384 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004385 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 {
4387 char_u *prefix = (char_u *)"\\<";
4388
4389 /* we need 3 extra chars, 1 for the NUL and
4390 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004391 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4392 compl_length) + 3);
4393 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004395 if (!vim_iswordp(line + compl_col)
4396 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 && (
4398#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004399 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004401 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402#endif
4403 )))
4404 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004405 STRCPY((char *)compl_pattern, prefix);
4406 (void)quote_meta(compl_pattern + STRLEN(prefix),
4407 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004409 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004411 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004413 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414#endif
4415 )
4416 {
4417 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004418 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4419 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004421 compl_col += curs_col;
4422 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
4424 else
4425 {
4426#ifdef FEAT_MBYTE
4427 /* Search the point of change class of multibyte character
4428 * or not a word single byte character backward. */
4429 if (has_mbyte)
4430 {
4431 int base_class;
4432 int head_off;
4433
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004434 startcol -= (*mb_head_off)(line, line + startcol);
4435 base_class = mb_get_class(line + startcol);
4436 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004438 head_off = (*mb_head_off)(line, line + startcol);
4439 if (base_class != mb_get_class(line + startcol
4440 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004442 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444 }
4445 else
4446#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004447 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004449 compl_col += ++startcol;
4450 compl_length = (int)curs_col - startcol;
4451 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
4453 /* Only match word with at least two chars -- webb
4454 * there's no need to call quote_meta,
4455 * alloc(7) is enough -- Acevedo
4456 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004457 compl_pattern = alloc(7);
4458 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004460 STRCPY((char *)compl_pattern, "\\<");
4461 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4462 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 }
4464 else
4465 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004466 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4467 compl_length) + 3);
4468 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004470 STRCPY((char *)compl_pattern, "\\<");
4471 (void)quote_meta(compl_pattern + 2, line + compl_col,
4472 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 }
4475 }
4476 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4477 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004478 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004479 compl_length = (int)curs_col - (int)compl_col;
4480 if (compl_length < 0) /* cursor in indent: empty pattern */
4481 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004483 compl_pattern = str_foldcase(line + compl_col, compl_length,
4484 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004486 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4487 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 return FAIL;
4489 }
4490 else if (ctrl_x_mode == CTRL_X_FILES)
4491 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004492 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004494 compl_col += ++startcol;
4495 compl_length = (int)curs_col - startcol;
4496 compl_pattern = addstar(line + compl_col, compl_length,
4497 EXPAND_FILES);
4498 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 return FAIL;
4500 }
4501 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4502 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004503 compl_pattern = vim_strnsave(line, curs_col);
4504 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004506 set_cmd_context(&compl_xp, compl_pattern,
4507 (int)STRLEN(compl_pattern), curs_col);
4508 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4509 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004511 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4512 compl_col = startcol;
4513 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004515 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004516 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004517#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004518 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004519 * Call user defined function 'completefunc' with "a:findstart"
4520 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004521 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004522 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004523 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004524 char_u *funcname;
4525 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004526
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004527 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004528 * string */
4529 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4530 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4531 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004532 {
4533 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4534 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004535 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004536 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004537
4538 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004539 args[1] = NULL;
4540 pos = curwin->w_cursor;
4541 col = call_func_retnr(funcname, 2, args, FALSE);
4542 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004543
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004544 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004545 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004546 compl_col = col;
4547 if ((colnr_T)compl_col > curs_col)
4548 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004549
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004550 /* Setup variables for completion. Need to obtain "line" again,
4551 * it may have become invalid. */
4552 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004553 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004554 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4555 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004556#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 return FAIL;
4558 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004559 else if (ctrl_x_mode == CTRL_X_SPELL)
4560 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004561#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004562 if (spell_bad_len > 0)
4563 compl_col = curs_col - spell_bad_len;
4564 else
4565 compl_col = spell_word_start(startcol);
4566 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004567 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004568 spell_expand_check_cap(compl_col);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004569 /* Need to obtain "line" again, it may have become invalid. */
4570 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004571 compl_length = (int)curs_col - compl_col;
4572 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4573 if (compl_pattern == NULL)
4574#endif
4575 return FAIL;
4576 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004577 else
4578 {
4579 EMSG2(_(e_intern2), "ins_complete()");
4580 return FAIL;
4581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004583 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 {
4585 edit_submode_pre = (char_u *)_(" Adding");
4586 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4587 {
4588 /* Insert a new line, keep indentation but ignore 'comments' */
4589#ifdef FEAT_COMMENTS
4590 char_u *old = curbuf->b_p_com;
4591
4592 curbuf->b_p_com = (char_u *)"";
4593#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004594 compl_startpos.lnum = curwin->w_cursor.lnum;
4595 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 ins_eol('\r');
4597#ifdef FEAT_COMMENTS
4598 curbuf->b_p_com = old;
4599#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004600 compl_length = 0;
4601 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
4603 }
4604 else
4605 {
4606 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004607 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610 if (compl_cont_status & CONT_LOCAL)
4611 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 else
4613 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4614
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004615 /* Always add completion for the original text. */
4616 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004617 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4618 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004619 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004621 vim_free(compl_pattern);
4622 compl_pattern = NULL;
4623 vim_free(compl_orig_text);
4624 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 return FAIL;
4626 }
4627
4628 /* showmode might reset the internal line pointers, so it must
4629 * be called before line = ml_get(), or when this address is no
4630 * longer needed. -- Acevedo.
4631 */
4632 edit_submode_extra = (char_u *)_("-- Searching...");
4633 edit_submode_highl = HLF_COUNT;
4634 showmode();
4635 edit_submode_extra = NULL;
4636 out_flush();
4637 }
4638
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004639 compl_shown_match = compl_curr_match;
4640 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
4642 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004643 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004645 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004647 /* may undisplay the popup menu */
4648 ins_compl_upd_pum();
4649
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004650 if (n > 1) /* all matches have been found */
4651 compl_matches = n;
4652 compl_curr_match = compl_shown_match;
4653 compl_direction = compl_shows_dir;
4654 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655
4656 /* eat the ESC to avoid leaving insert mode */
4657 if (got_int && !global_busy)
4658 {
4659 (void)vgetc();
4660 got_int = FALSE;
4661 }
4662
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004663 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004664 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004666 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4667 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4669 edit_submode_highl = HLF_E;
4670 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4671 * because we couldn't expand anything at first place, but if we used
4672 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4673 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004674 if ( compl_length > 1
4675 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 || (ctrl_x_mode != 0
4677 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4678 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004679 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
4681
Bram Moolenaar572cb562005-08-05 21:35:02 +00004682 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004683 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004685 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686
4687 if (edit_submode_extra == NULL)
4688 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004689 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 {
4691 edit_submode_extra = (char_u *)_("Back at original");
4692 edit_submode_highl = HLF_W;
4693 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004694 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
4696 edit_submode_extra = (char_u *)_("Word from other line");
4697 edit_submode_highl = HLF_COUNT;
4698 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004699 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
4701 edit_submode_extra = (char_u *)_("The only match");
4702 edit_submode_highl = HLF_COUNT;
4703 }
4704 else
4705 {
4706 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004707 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004709 int number = 0;
4710 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004712 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
4714 /* search backwards for the first valid (!= -1) number.
4715 * This should normally succeed already at the first loop
4716 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004717 for (match = compl_curr_match->cp_prev; match != NULL
4718 && match != compl_first_match;
4719 match = match->cp_prev)
4720 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004722 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 break;
4724 }
4725 if (match != NULL)
4726 /* go up and assign all numbers which are not assigned
4727 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004728 for (match = match->cp_next;
4729 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004730 match = match->cp_next)
4731 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 }
4733 else /* BACKWARD */
4734 {
4735 /* search forwards (upwards) for the first valid (!= -1)
4736 * number. This should normally succeed already at the
4737 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004738 for (match = compl_curr_match->cp_next; match != NULL
4739 && match != compl_first_match;
4740 match = match->cp_next)
4741 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004743 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 break;
4745 }
4746 if (match != NULL)
4747 /* go down and assign all numbers which are not
4748 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004749 for (match = match->cp_prev; match
4750 && match->cp_number == -1;
4751 match = match->cp_prev)
4752 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 }
4754 }
4755
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004756 /* The match should always have a sequence number now, this is
4757 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004758 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 {
4760 /* Space for 10 text chars. + 2x10-digit no.s */
4761 static char_u match_ref[31];
4762
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004763 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004765 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004767 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004768 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004769 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 edit_submode_extra = match_ref;
4771 edit_submode_highl = HLF_R;
4772 if (dollar_vcol)
4773 curs_columns(FALSE);
4774 }
4775 }
4776 }
4777
4778 /* Show a message about what (completion) mode we're in. */
4779 showmode();
4780 if (edit_submode_extra != NULL)
4781 {
4782 if (!p_smd)
4783 msg_attr(edit_submode_extra,
4784 edit_submode_highl < HLF_COUNT
4785 ? hl_attr(edit_submode_highl) : 0);
4786 }
4787 else
4788 msg_clr_cmdline(); /* necessary for "noshowmode" */
4789
Bram Moolenaara94bc432006-03-10 21:42:59 +00004790 /* RedrawingDisabled may be set when invoked through complete(). */
4791 n = RedrawingDisabled;
4792 RedrawingDisabled = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004793 ins_compl_show_pum();
Bram Moolenaara94bc432006-03-10 21:42:59 +00004794 setcursor();
4795 RedrawingDisabled = n;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004796
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 return OK;
4798}
4799
4800/*
4801 * Looks in the first "len" chars. of "src" for search-metachars.
4802 * If dest is not NULL the chars. are copied there quoting (with
4803 * a backslash) the metachars, and dest would be NUL terminated.
4804 * Returns the length (needed) of dest
4805 */
4806 static int
4807quote_meta(dest, src, len)
4808 char_u *dest;
4809 char_u *src;
4810 int len;
4811{
4812 int m;
4813
4814 for (m = len; --len >= 0; src++)
4815 {
4816 switch (*src)
4817 {
4818 case '.':
4819 case '*':
4820 case '[':
4821 if (ctrl_x_mode == CTRL_X_DICTIONARY
4822 || ctrl_x_mode == CTRL_X_THESAURUS)
4823 break;
4824 case '~':
4825 if (!p_magic) /* quote these only if magic is set */
4826 break;
4827 case '\\':
4828 if (ctrl_x_mode == CTRL_X_DICTIONARY
4829 || ctrl_x_mode == CTRL_X_THESAURUS)
4830 break;
4831 case '^': /* currently it's not needed. */
4832 case '$':
4833 m++;
4834 if (dest != NULL)
4835 *dest++ = '\\';
4836 break;
4837 }
4838 if (dest != NULL)
4839 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004840# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 /* Copy remaining bytes of a multibyte character. */
4842 if (has_mbyte)
4843 {
4844 int i, mb_len;
4845
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004846 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 if (mb_len > 0 && len >= mb_len)
4848 for (i = 0; i < mb_len; ++i)
4849 {
4850 --len;
4851 ++src;
4852 if (dest != NULL)
4853 *dest++ = *src;
4854 }
4855 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 }
4858 if (dest != NULL)
4859 *dest = NUL;
4860
4861 return m;
4862}
4863#endif /* FEAT_INS_EXPAND */
4864
4865/*
4866 * Next character is interpreted literally.
4867 * A one, two or three digit decimal number is interpreted as its byte value.
4868 * If one or two digits are entered, the next character is given to vungetc().
4869 * For Unicode a character > 255 may be returned.
4870 */
4871 int
4872get_literal()
4873{
4874 int cc;
4875 int nc;
4876 int i;
4877 int hex = FALSE;
4878 int octal = FALSE;
4879#ifdef FEAT_MBYTE
4880 int unicode = 0;
4881#endif
4882
4883 if (got_int)
4884 return Ctrl_C;
4885
4886#ifdef FEAT_GUI
4887 /*
4888 * In GUI there is no point inserting the internal code for a special key.
4889 * It is more useful to insert the string "<KEY>" instead. This would
4890 * probably be useful in a text window too, but it would not be
4891 * vi-compatible (maybe there should be an option for it?) -- webb
4892 */
4893 if (gui.in_use)
4894 ++allow_keys;
4895#endif
4896#ifdef USE_ON_FLY_SCROLL
4897 dont_scroll = TRUE; /* disallow scrolling here */
4898#endif
4899 ++no_mapping; /* don't map the next key hits */
4900 cc = 0;
4901 i = 0;
4902 for (;;)
4903 {
4904 do
4905 nc = safe_vgetc();
4906 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4907 || nc == K_HOR_SCROLLBAR);
4908#ifdef FEAT_CMDL_INFO
4909 if (!(State & CMDLINE)
4910# ifdef FEAT_MBYTE
4911 && MB_BYTE2LEN_CHECK(nc) == 1
4912# endif
4913 )
4914 add_to_showcmd(nc);
4915#endif
4916 if (nc == 'x' || nc == 'X')
4917 hex = TRUE;
4918 else if (nc == 'o' || nc == 'O')
4919 octal = TRUE;
4920#ifdef FEAT_MBYTE
4921 else if (nc == 'u' || nc == 'U')
4922 unicode = nc;
4923#endif
4924 else
4925 {
4926 if (hex
4927#ifdef FEAT_MBYTE
4928 || unicode != 0
4929#endif
4930 )
4931 {
4932 if (!vim_isxdigit(nc))
4933 break;
4934 cc = cc * 16 + hex2nr(nc);
4935 }
4936 else if (octal)
4937 {
4938 if (nc < '0' || nc > '7')
4939 break;
4940 cc = cc * 8 + nc - '0';
4941 }
4942 else
4943 {
4944 if (!VIM_ISDIGIT(nc))
4945 break;
4946 cc = cc * 10 + nc - '0';
4947 }
4948
4949 ++i;
4950 }
4951
4952 if (cc > 255
4953#ifdef FEAT_MBYTE
4954 && unicode == 0
4955#endif
4956 )
4957 cc = 255; /* limit range to 0-255 */
4958 nc = 0;
4959
4960 if (hex) /* hex: up to two chars */
4961 {
4962 if (i >= 2)
4963 break;
4964 }
4965#ifdef FEAT_MBYTE
4966 else if (unicode) /* Unicode: up to four or eight chars */
4967 {
4968 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4969 break;
4970 }
4971#endif
4972 else if (i >= 3) /* decimal or octal: up to three chars */
4973 break;
4974 }
4975 if (i == 0) /* no number entered */
4976 {
4977 if (nc == K_ZERO) /* NUL is stored as NL */
4978 {
4979 cc = '\n';
4980 nc = 0;
4981 }
4982 else
4983 {
4984 cc = nc;
4985 nc = 0;
4986 }
4987 }
4988
4989 if (cc == 0) /* NUL is stored as NL */
4990 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004991#ifdef FEAT_MBYTE
4992 if (enc_dbcs && (cc & 0xff) == 0)
4993 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4994 second byte will cause trouble! */
4995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996
4997 --no_mapping;
4998#ifdef FEAT_GUI
4999 if (gui.in_use)
5000 --allow_keys;
5001#endif
5002 if (nc)
5003 vungetc(nc);
5004 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5005 return cc;
5006}
5007
5008/*
5009 * Insert character, taking care of special keys and mod_mask
5010 */
5011 static void
5012insert_special(c, allow_modmask, ctrlv)
5013 int c;
5014 int allow_modmask;
5015 int ctrlv; /* c was typed after CTRL-V */
5016{
5017 char_u *p;
5018 int len;
5019
5020 /*
5021 * Special function key, translate into "<Key>". Up to the last '>' is
5022 * inserted with ins_str(), so as not to replace characters in replace
5023 * mode.
5024 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5025 * unless 'allow_modmask' is TRUE.
5026 */
5027#ifdef MACOS
5028 /* Command-key never produces a normal key */
5029 if (mod_mask & MOD_MASK_CMD)
5030 allow_modmask = TRUE;
5031#endif
5032 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5033 {
5034 p = get_special_key_name(c, mod_mask);
5035 len = (int)STRLEN(p);
5036 c = p[len - 1];
5037 if (len > 2)
5038 {
5039 if (stop_arrow() == FAIL)
5040 return;
5041 p[len - 1] = NUL;
5042 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005043 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 ctrlv = FALSE;
5045 }
5046 }
5047 if (stop_arrow() == OK)
5048 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5049}
5050
5051/*
5052 * Special characters in this context are those that need processing other
5053 * than the simple insertion that can be performed here. This includes ESC
5054 * which terminates the insert, and CR/NL which need special processing to
5055 * open up a new line. This routine tries to optimize insertions performed by
5056 * the "redo", "undo" or "put" commands, so it needs to know when it should
5057 * stop and defer processing to the "normal" mechanism.
5058 * '0' and '^' are special, because they can be followed by CTRL-D.
5059 */
5060#ifdef EBCDIC
5061# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5062#else
5063# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5064#endif
5065
5066#ifdef FEAT_MBYTE
5067# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5068#else
5069# define WHITECHAR(cc) vim_iswhite(cc)
5070#endif
5071
5072 void
5073insertchar(c, flags, second_indent)
5074 int c; /* character to insert or NUL */
5075 int flags; /* INSCHAR_FORMAT, etc. */
5076 int second_indent; /* indent for second line if >= 0 */
5077{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 int textwidth;
5079#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
5084 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5085 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086
5087 /*
5088 * Try to break the line in two or more pieces when:
5089 * - Always do this if we have been called to do formatting only.
5090 * - Always do this when 'formatoptions' has the 'a' flag and the line
5091 * ends in white space.
5092 * - Otherwise:
5093 * - Don't do this if inserting a blank
5094 * - Don't do this if an existing character is being replaced, unless
5095 * we're in VREPLACE mode.
5096 * - Do this if the cursor is not on the line where insert started
5097 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5098 * before the insert.
5099 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5100 * before 'textwidth'
5101 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005102 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 && ((flags & INSCHAR_FORMAT)
5104 || (!vim_iswhite(c)
5105 && !((State & REPLACE_FLAG)
5106#ifdef FEAT_VREPLACE
5107 && !(State & VREPLACE_FLAG)
5108#endif
5109 && *ml_get_cursor() != NUL)
5110 && (curwin->w_cursor.lnum != Insstart.lnum
5111 || ((!has_format_option(FO_INS_LONG)
5112 || Insstart_textlen <= (colnr_T)textwidth)
5113 && (!fo_ins_blank
5114 || Insstart_blank_vcol <= (colnr_T)textwidth
5115 ))))))
5116 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005117 /* Format with 'formatexpr' when it's set. Use internal formatting
5118 * when 'formatexpr' isn't set or it returns non-zero. */
5119#if defined(FEAT_EVAL)
5120 if (*curbuf->b_p_fex == NUL
5121 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005123 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005125
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 if (c == NUL) /* only formatting was wanted */
5127 return;
5128
5129#ifdef FEAT_COMMENTS
5130 /* Check whether this character should end a comment. */
5131 if (did_ai && (int)c == end_comment_pending)
5132 {
5133 char_u *line;
5134 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5135 int middle_len, end_len;
5136 int i;
5137
5138 /*
5139 * Need to remove existing (middle) comment leader and insert end
5140 * comment leader. First, check what comment leader we can find.
5141 */
5142 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5143 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5144 {
5145 /* Skip middle-comment string */
5146 while (*p && p[-1] != ':') /* find end of middle flags */
5147 ++p;
5148 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5149 /* Don't count trailing white space for middle_len */
5150 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5151 --middle_len;
5152
5153 /* Find the end-comment string */
5154 while (*p && p[-1] != ':') /* find end of end flags */
5155 ++p;
5156 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5157
5158 /* Skip white space before the cursor */
5159 i = curwin->w_cursor.col;
5160 while (--i >= 0 && vim_iswhite(line[i]))
5161 ;
5162 i++;
5163
5164 /* Skip to before the middle leader */
5165 i -= middle_len;
5166
5167 /* Check some expected things before we go on */
5168 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5169 {
5170 /* Backspace over all the stuff we want to replace */
5171 backspace_until_column(i);
5172
5173 /*
5174 * Insert the end-comment string, except for the last
5175 * character, which will get inserted as normal later.
5176 */
5177 ins_bytes_len(lead_end, end_len - 1);
5178 }
5179 }
5180 }
5181 end_comment_pending = NUL;
5182#endif
5183
5184 did_ai = FALSE;
5185#ifdef FEAT_SMARTINDENT
5186 did_si = FALSE;
5187 can_si = FALSE;
5188 can_si_back = FALSE;
5189#endif
5190
5191 /*
5192 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5193 * This speeds up normal text input considerably.
5194 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5195 * need to re-indent at a ':', or any other character (but not what
5196 * 'paste' is set)..
5197 */
5198#ifdef USE_ON_FLY_SCROLL
5199 dont_scroll = FALSE; /* allow scrolling here */
5200#endif
5201
5202 if ( !ISSPECIAL(c)
5203#ifdef FEAT_MBYTE
5204 && (!has_mbyte || (*mb_char2len)(c) == 1)
5205#endif
5206 && vpeekc() != NUL
5207 && !(State & REPLACE_FLAG)
5208#ifdef FEAT_CINDENT
5209 && !cindent_on()
5210#endif
5211#ifdef FEAT_RIGHTLEFT
5212 && !p_ri
5213#endif
5214 )
5215 {
5216#define INPUT_BUFLEN 100
5217 char_u buf[INPUT_BUFLEN + 1];
5218 int i;
5219 colnr_T virtcol = 0;
5220
5221 buf[0] = c;
5222 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005223 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 virtcol = get_nolist_virtcol();
5225 /*
5226 * Stop the string when:
5227 * - no more chars available
5228 * - finding a special character (command key)
5229 * - buffer is full
5230 * - running into the 'textwidth' boundary
5231 * - need to check for abbreviation: A non-word char after a word-char
5232 */
5233 while ( (c = vpeekc()) != NUL
5234 && !ISSPECIAL(c)
5235#ifdef FEAT_MBYTE
5236 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5237#endif
5238 && i < INPUT_BUFLEN
5239 && (textwidth == 0
5240 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5241 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5242 {
5243#ifdef FEAT_RIGHTLEFT
5244 c = vgetc();
5245 if (p_hkmap && KeyTyped)
5246 c = hkmap(c); /* Hebrew mode mapping */
5247# ifdef FEAT_FKMAP
5248 if (p_fkmap && KeyTyped)
5249 c = fkmap(c); /* Farsi mode mapping */
5250# endif
5251 buf[i++] = c;
5252#else
5253 buf[i++] = vgetc();
5254#endif
5255 }
5256
5257#ifdef FEAT_DIGRAPHS
5258 do_digraph(-1); /* clear digraphs */
5259 do_digraph(buf[i-1]); /* may be the start of a digraph */
5260#endif
5261 buf[i] = NUL;
5262 ins_str(buf);
5263 if (flags & INSCHAR_CTRLV)
5264 {
5265 redo_literal(*buf);
5266 i = 1;
5267 }
5268 else
5269 i = 0;
5270 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005271 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 }
5273 else
5274 {
5275#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005276 int cc;
5277
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5279 {
5280 char_u buf[MB_MAXBYTES + 1];
5281
5282 (*mb_char2bytes)(c, buf);
5283 buf[cc] = NUL;
5284 ins_char_bytes(buf, cc);
5285 AppendCharToRedobuff(c);
5286 }
5287 else
5288#endif
5289 {
5290 ins_char(c);
5291 if (flags & INSCHAR_CTRLV)
5292 redo_literal(c);
5293 else
5294 AppendCharToRedobuff(c);
5295 }
5296 }
5297}
5298
5299/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005300 * Format text at the current insert position.
5301 */
5302 static void
5303internal_format(textwidth, second_indent, flags, format_only)
5304 int textwidth;
5305 int second_indent;
5306 int flags;
5307 int format_only;
5308{
5309 int cc;
5310 int save_char = NUL;
5311 int haveto_redraw = FALSE;
5312 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5313#ifdef FEAT_MBYTE
5314 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5315#endif
5316 int fo_white_par = has_format_option(FO_WHITE_PAR);
5317 int first_line = TRUE;
5318#ifdef FEAT_COMMENTS
5319 colnr_T leader_len;
5320 int no_leader = FALSE;
5321 int do_comments = (flags & INSCHAR_DO_COM);
5322#endif
5323
5324 /*
5325 * When 'ai' is off we don't want a space under the cursor to be
5326 * deleted. Replace it with an 'x' temporarily.
5327 */
5328 if (!curbuf->b_p_ai)
5329 {
5330 cc = gchar_cursor();
5331 if (vim_iswhite(cc))
5332 {
5333 save_char = cc;
5334 pchar_cursor('x');
5335 }
5336 }
5337
5338 /*
5339 * Repeat breaking lines, until the current line is not too long.
5340 */
5341 while (!got_int)
5342 {
5343 int startcol; /* Cursor column at entry */
5344 int wantcol; /* column at textwidth border */
5345 int foundcol; /* column for start of spaces */
5346 int end_foundcol = 0; /* column for start of word */
5347 colnr_T len;
5348 colnr_T virtcol;
5349#ifdef FEAT_VREPLACE
5350 int orig_col = 0;
5351 char_u *saved_text = NULL;
5352#endif
5353 colnr_T col;
5354
5355 virtcol = get_nolist_virtcol();
5356 if (virtcol < (colnr_T)textwidth)
5357 break;
5358
5359#ifdef FEAT_COMMENTS
5360 if (no_leader)
5361 do_comments = FALSE;
5362 else if (!(flags & INSCHAR_FORMAT)
5363 && has_format_option(FO_WRAP_COMS))
5364 do_comments = TRUE;
5365
5366 /* Don't break until after the comment leader */
5367 if (do_comments)
5368 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5369 else
5370 leader_len = 0;
5371
5372 /* If the line doesn't start with a comment leader, then don't
5373 * start one in a following broken line. Avoids that a %word
5374 * moved to the start of the next line causes all following lines
5375 * to start with %. */
5376 if (leader_len == 0)
5377 no_leader = TRUE;
5378#endif
5379 if (!(flags & INSCHAR_FORMAT)
5380#ifdef FEAT_COMMENTS
5381 && leader_len == 0
5382#endif
5383 && !has_format_option(FO_WRAP))
5384
5385 {
5386 textwidth = 0;
5387 break;
5388 }
5389 if ((startcol = curwin->w_cursor.col) == 0)
5390 break;
5391
5392 /* find column of textwidth border */
5393 coladvance((colnr_T)textwidth);
5394 wantcol = curwin->w_cursor.col;
5395
5396 curwin->w_cursor.col = startcol - 1;
5397#ifdef FEAT_MBYTE
5398 /* Correct cursor for multi-byte character. */
5399 if (has_mbyte)
5400 mb_adjust_cursor();
5401#endif
5402 foundcol = 0;
5403
5404 /*
5405 * Find position to break at.
5406 * Stop at first entered white when 'formatoptions' has 'v'
5407 */
5408 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5409 || curwin->w_cursor.lnum != Insstart.lnum
5410 || curwin->w_cursor.col >= Insstart.col)
5411 {
5412 cc = gchar_cursor();
5413 if (WHITECHAR(cc))
5414 {
5415 /* remember position of blank just before text */
5416 end_foundcol = curwin->w_cursor.col;
5417
5418 /* find start of sequence of blanks */
5419 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5420 {
5421 dec_cursor();
5422 cc = gchar_cursor();
5423 }
5424 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5425 break; /* only spaces in front of text */
5426#ifdef FEAT_COMMENTS
5427 /* Don't break until after the comment leader */
5428 if (curwin->w_cursor.col < leader_len)
5429 break;
5430#endif
5431 if (has_format_option(FO_ONE_LETTER))
5432 {
5433 /* do not break after one-letter words */
5434 if (curwin->w_cursor.col == 0)
5435 break; /* one-letter word at begin */
5436
5437 col = curwin->w_cursor.col;
5438 dec_cursor();
5439 cc = gchar_cursor();
5440
5441 if (WHITECHAR(cc))
5442 continue; /* one-letter, continue */
5443 curwin->w_cursor.col = col;
5444 }
5445#ifdef FEAT_MBYTE
5446 if (has_mbyte)
5447 foundcol = curwin->w_cursor.col
5448 + (*mb_ptr2len)(ml_get_cursor());
5449 else
5450#endif
5451 foundcol = curwin->w_cursor.col + 1;
5452 if (curwin->w_cursor.col < (colnr_T)wantcol)
5453 break;
5454 }
5455#ifdef FEAT_MBYTE
5456 else if (cc >= 0x100 && fo_multibyte
5457 && curwin->w_cursor.col <= (colnr_T)wantcol)
5458 {
5459 /* Break after or before a multi-byte character. */
5460 foundcol = curwin->w_cursor.col;
5461 if (curwin->w_cursor.col < (colnr_T)wantcol)
5462 foundcol += (*mb_char2len)(cc);
5463 end_foundcol = foundcol;
5464 break;
5465 }
5466#endif
5467 if (curwin->w_cursor.col == 0)
5468 break;
5469 dec_cursor();
5470 }
5471
5472 if (foundcol == 0) /* no spaces, cannot break line */
5473 {
5474 curwin->w_cursor.col = startcol;
5475 break;
5476 }
5477
5478 /* Going to break the line, remove any "$" now. */
5479 undisplay_dollar();
5480
5481 /*
5482 * Offset between cursor position and line break is used by replace
5483 * stack functions. VREPLACE does not use this, and backspaces
5484 * over the text instead.
5485 */
5486#ifdef FEAT_VREPLACE
5487 if (State & VREPLACE_FLAG)
5488 orig_col = startcol; /* Will start backspacing from here */
5489 else
5490#endif
5491 replace_offset = startcol - end_foundcol - 1;
5492
5493 /*
5494 * adjust startcol for spaces that will be deleted and
5495 * characters that will remain on top line
5496 */
5497 curwin->w_cursor.col = foundcol;
5498 while (cc = gchar_cursor(), WHITECHAR(cc))
5499 inc_cursor();
5500 startcol -= curwin->w_cursor.col;
5501 if (startcol < 0)
5502 startcol = 0;
5503
5504#ifdef FEAT_VREPLACE
5505 if (State & VREPLACE_FLAG)
5506 {
5507 /*
5508 * In VREPLACE mode, we will backspace over the text to be
5509 * wrapped, so save a copy now to put on the next line.
5510 */
5511 saved_text = vim_strsave(ml_get_cursor());
5512 curwin->w_cursor.col = orig_col;
5513 if (saved_text == NULL)
5514 break; /* Can't do it, out of memory */
5515 saved_text[startcol] = NUL;
5516
5517 /* Backspace over characters that will move to the next line */
5518 if (!fo_white_par)
5519 backspace_until_column(foundcol);
5520 }
5521 else
5522#endif
5523 {
5524 /* put cursor after pos. to break line */
5525 if (!fo_white_par)
5526 curwin->w_cursor.col = foundcol;
5527 }
5528
5529 /*
5530 * Split the line just before the margin.
5531 * Only insert/delete lines, but don't really redraw the window.
5532 */
5533 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5534 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5535#ifdef FEAT_COMMENTS
5536 + (do_comments ? OPENLINE_DO_COM : 0)
5537#endif
5538 , old_indent);
5539 old_indent = 0;
5540
5541 replace_offset = 0;
5542 if (first_line)
5543 {
5544 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5545 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5546 if (second_indent >= 0)
5547 {
5548#ifdef FEAT_VREPLACE
5549 if (State & VREPLACE_FLAG)
5550 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5551 else
5552#endif
5553 (void)set_indent(second_indent, SIN_CHANGED);
5554 }
5555 first_line = FALSE;
5556 }
5557
5558#ifdef FEAT_VREPLACE
5559 if (State & VREPLACE_FLAG)
5560 {
5561 /*
5562 * In VREPLACE mode we have backspaced over the text to be
5563 * moved, now we re-insert it into the new line.
5564 */
5565 ins_bytes(saved_text);
5566 vim_free(saved_text);
5567 }
5568 else
5569#endif
5570 {
5571 /*
5572 * Check if cursor is not past the NUL off the line, cindent
5573 * may have added or removed indent.
5574 */
5575 curwin->w_cursor.col += startcol;
5576 len = (colnr_T)STRLEN(ml_get_curline());
5577 if (curwin->w_cursor.col > len)
5578 curwin->w_cursor.col = len;
5579 }
5580
5581 haveto_redraw = TRUE;
5582#ifdef FEAT_CINDENT
5583 can_cindent = TRUE;
5584#endif
5585 /* moved the cursor, don't autoindent or cindent now */
5586 did_ai = FALSE;
5587#ifdef FEAT_SMARTINDENT
5588 did_si = FALSE;
5589 can_si = FALSE;
5590 can_si_back = FALSE;
5591#endif
5592 line_breakcheck();
5593 }
5594
5595 if (save_char != NUL) /* put back space after cursor */
5596 pchar_cursor(save_char);
5597
5598 if (!format_only && haveto_redraw)
5599 {
5600 update_topline();
5601 redraw_curbuf_later(VALID);
5602 }
5603}
5604
5605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 * Called after inserting or deleting text: When 'formatoptions' includes the
5607 * 'a' flag format from the current line until the end of the paragraph.
5608 * Keep the cursor at the same position relative to the text.
5609 * The caller must have saved the cursor line for undo, following ones will be
5610 * saved here.
5611 */
5612 void
5613auto_format(trailblank, prev_line)
5614 int trailblank; /* when TRUE also format with trailing blank */
5615 int prev_line; /* may start in previous line */
5616{
5617 pos_T pos;
5618 colnr_T len;
5619 char_u *old;
5620 char_u *new, *pnew;
5621 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005622 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
5624 if (!has_format_option(FO_AUTO))
5625 return;
5626
5627 pos = curwin->w_cursor;
5628 old = ml_get_curline();
5629
5630 /* may remove added space */
5631 check_auto_format(FALSE);
5632
5633 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5634 * user might insert normal text next. Also skip formatting when "1" is
5635 * in 'formatoptions' and there is a single character before the cursor.
5636 * Otherwise the line would be broken and when typing another non-white
5637 * next they are not joined back together. */
5638 wasatend = (pos.col == STRLEN(old));
5639 if (*old != NUL && !trailblank && wasatend)
5640 {
5641 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005642 cc = gchar_cursor();
5643 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5644 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005646 cc = gchar_cursor();
5647 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
5649 curwin->w_cursor = pos;
5650 return;
5651 }
5652 curwin->w_cursor = pos;
5653 }
5654
5655#ifdef FEAT_COMMENTS
5656 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5657 * comments. */
5658 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5659 && get_leader_len(old, NULL, FALSE) == 0)
5660 return;
5661#endif
5662
5663 /*
5664 * May start formatting in a previous line, so that after "x" a word is
5665 * moved to the previous line if it fits there now. Only when this is not
5666 * the start of a paragraph.
5667 */
5668 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5669 {
5670 --curwin->w_cursor.lnum;
5671 if (u_save_cursor() == FAIL)
5672 return;
5673 }
5674
5675 /*
5676 * Do the formatting and restore the cursor position. "saved_cursor" will
5677 * be adjusted for the text formatting.
5678 */
5679 saved_cursor = pos;
5680 format_lines((linenr_T)-1);
5681 curwin->w_cursor = saved_cursor;
5682 saved_cursor.lnum = 0;
5683
5684 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5685 {
5686 /* "cannot happen" */
5687 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5688 coladvance((colnr_T)MAXCOL);
5689 }
5690 else
5691 check_cursor_col();
5692
5693 /* Insert mode: If the cursor is now after the end of the line while it
5694 * previously wasn't, the line was broken. Because of the rule above we
5695 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5696 * formatted. */
5697 if (!wasatend && has_format_option(FO_WHITE_PAR))
5698 {
5699 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005700 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 if (curwin->w_cursor.col == len)
5702 {
5703 pnew = vim_strnsave(new, len + 2);
5704 pnew[len] = ' ';
5705 pnew[len + 1] = NUL;
5706 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5707 /* remove the space later */
5708 did_add_space = TRUE;
5709 }
5710 else
5711 /* may remove added space */
5712 check_auto_format(FALSE);
5713 }
5714
5715 check_cursor();
5716}
5717
5718/*
5719 * When an extra space was added to continue a paragraph for auto-formatting,
5720 * delete it now. The space must be under the cursor, just after the insert
5721 * position.
5722 */
5723 static void
5724check_auto_format(end_insert)
5725 int end_insert; /* TRUE when ending Insert mode */
5726{
5727 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005728 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
5730 if (did_add_space)
5731 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005732 cc = gchar_cursor();
5733 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 /* Somehow the space was removed already. */
5735 did_add_space = FALSE;
5736 else
5737 {
5738 if (!end_insert)
5739 {
5740 inc_cursor();
5741 c = gchar_cursor();
5742 dec_cursor();
5743 }
5744 if (c != NUL)
5745 {
5746 /* The space is no longer at the end of the line, delete it. */
5747 del_char(FALSE);
5748 did_add_space = FALSE;
5749 }
5750 }
5751 }
5752}
5753
5754/*
5755 * Find out textwidth to be used for formatting:
5756 * if 'textwidth' option is set, use it
5757 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5758 * if invalid value, use 0.
5759 * Set default to window width (maximum 79) for "gq" operator.
5760 */
5761 int
5762comp_textwidth(ff)
5763 int ff; /* force formatting (for "Q" command) */
5764{
5765 int textwidth;
5766
5767 textwidth = curbuf->b_p_tw;
5768 if (textwidth == 0 && curbuf->b_p_wm)
5769 {
5770 /* The width is the window width minus 'wrapmargin' minus all the
5771 * things that add to the margin. */
5772 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5773#ifdef FEAT_CMDWIN
5774 if (cmdwin_type != 0)
5775 textwidth -= 1;
5776#endif
5777#ifdef FEAT_FOLDING
5778 textwidth -= curwin->w_p_fdc;
5779#endif
5780#ifdef FEAT_SIGNS
5781 if (curwin->w_buffer->b_signlist != NULL
5782# ifdef FEAT_NETBEANS_INTG
5783 || usingNetbeans
5784# endif
5785 )
5786 textwidth -= 1;
5787#endif
5788 if (curwin->w_p_nu)
5789 textwidth -= 8;
5790 }
5791 if (textwidth < 0)
5792 textwidth = 0;
5793 if (ff && textwidth == 0)
5794 {
5795 textwidth = W_WIDTH(curwin) - 1;
5796 if (textwidth > 79)
5797 textwidth = 79;
5798 }
5799 return textwidth;
5800}
5801
5802/*
5803 * Put a character in the redo buffer, for when just after a CTRL-V.
5804 */
5805 static void
5806redo_literal(c)
5807 int c;
5808{
5809 char_u buf[10];
5810
5811 /* Only digits need special treatment. Translate them into a string of
5812 * three digits. */
5813 if (VIM_ISDIGIT(c))
5814 {
5815 sprintf((char *)buf, "%03d", c);
5816 AppendToRedobuff(buf);
5817 }
5818 else
5819 AppendCharToRedobuff(c);
5820}
5821
5822/*
5823 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005824 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 */
5826 static void
5827start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005828 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829{
5830 if (!arrow_used) /* something has been inserted */
5831 {
5832 AppendToRedobuff(ESC_STR);
5833 stop_insert(end_insert_pos, FALSE);
5834 arrow_used = TRUE; /* this means we stopped the current insert */
5835 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005836#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005837 check_spell_redraw();
5838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839}
5840
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005841#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005842/*
5843 * If we skipped highlighting word at cursor, do it now.
5844 * It may be skipped again, thus reset spell_redraw_lnum first.
5845 */
5846 static void
5847check_spell_redraw()
5848{
5849 if (spell_redraw_lnum != 0)
5850 {
5851 linenr_T lnum = spell_redraw_lnum;
5852
5853 spell_redraw_lnum = 0;
5854 redrawWinline(lnum, FALSE);
5855 }
5856}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005857
5858/*
5859 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5860 * spelled word, if there is one.
5861 */
5862 static void
5863spell_back_to_badword()
5864{
5865 pos_T tpos = curwin->w_cursor;
5866
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005867 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005868 if (curwin->w_cursor.col != tpos.col)
5869 start_arrow(&tpos);
5870}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005871#endif
5872
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873/*
5874 * stop_arrow() is called before a change is made in insert mode.
5875 * If an arrow key has been used, start a new insertion.
5876 * Returns FAIL if undo is impossible, shouldn't insert then.
5877 */
5878 int
5879stop_arrow()
5880{
5881 if (arrow_used)
5882 {
5883 if (u_save_cursor() == OK)
5884 {
5885 arrow_used = FALSE;
5886 ins_need_undo = FALSE;
5887 }
5888 Insstart = curwin->w_cursor; /* new insertion starts here */
5889 Insstart_textlen = linetabsize(ml_get_curline());
5890 ai_col = 0;
5891#ifdef FEAT_VREPLACE
5892 if (State & VREPLACE_FLAG)
5893 {
5894 orig_line_count = curbuf->b_ml.ml_line_count;
5895 vr_lines_changed = 1;
5896 }
5897#endif
5898 ResetRedobuff();
5899 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005900 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 }
5902 else if (ins_need_undo)
5903 {
5904 if (u_save_cursor() == OK)
5905 ins_need_undo = FALSE;
5906 }
5907
5908#ifdef FEAT_FOLDING
5909 /* Always open fold at the cursor line when inserting something. */
5910 foldOpenCursor();
5911#endif
5912
5913 return (arrow_used || ins_need_undo ? FAIL : OK);
5914}
5915
5916/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005917 * Do a few things to stop inserting.
5918 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
5919 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 */
5921 static void
5922stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005923 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005924 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005926 int cc;
5927 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928
5929 stop_redo_ins();
5930 replace_flush(); /* abandon replace stack */
5931
5932 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005933 * Save the inserted text for later redo with ^@ and CTRL-A.
5934 * Don't do it when "restart_edit" was set and nothing was inserted,
5935 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005937 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005938 if (did_restart_edit == 0 || (ptr != NULL
5939 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005940 {
5941 vim_free(last_insert);
5942 last_insert = ptr;
5943 last_insert_skip = new_insert_skip;
5944 }
5945 else
5946 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005948 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 {
5950 /* Auto-format now. It may seem strange to do this when stopping an
5951 * insertion (or moving the cursor), but it's required when appending
5952 * a line and having it end in a space. But only do it when something
5953 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005954 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005956 pos_T tpos = curwin->w_cursor;
5957
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 /* When the cursor is at the end of the line after a space the
5959 * formatting will move it to the following word. Avoid that by
5960 * moving the cursor onto the space. */
5961 cc = 'x';
5962 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5963 {
5964 dec_cursor();
5965 cc = gchar_cursor();
5966 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005967 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 }
5969
5970 auto_format(TRUE, FALSE);
5971
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005972 if (vim_iswhite(cc))
5973 {
5974 if (gchar_cursor() != NUL)
5975 inc_cursor();
5976#ifdef FEAT_VIRTUALEDIT
5977 /* If the cursor is still at the same character, also keep
5978 * the "coladd". */
5979 if (gchar_cursor() == NUL
5980 && curwin->w_cursor.lnum == tpos.lnum
5981 && curwin->w_cursor.col == tpos.col)
5982 curwin->w_cursor.coladd = tpos.coladd;
5983#endif
5984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 }
5986
5987 /* If a space was inserted for auto-formatting, remove it now. */
5988 check_auto_format(TRUE);
5989
5990 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005991 * of the line, and put the cursor back.
5992 * Do this when ESC was used or moving the cursor up/down. */
5993 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005994 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005996 pos_T tpos = curwin->w_cursor;
5997
5998 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00005999 for (;;)
6000 {
6001 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6002 --curwin->w_cursor.col;
6003 cc = gchar_cursor();
6004 if (!vim_iswhite(cc))
6005 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006007 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006008 if (curwin->w_cursor.lnum != tpos.lnum)
6009 curwin->w_cursor = tpos;
6010 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6012
6013#ifdef FEAT_VISUAL
6014 /* <C-S-Right> may have started Visual mode, adjust the position for
6015 * deleted characters. */
6016 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6017 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006018 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 if (VIsual.col > (colnr_T)cc)
6020 {
6021 VIsual.col = cc;
6022# ifdef FEAT_VIRTUALEDIT
6023 VIsual.coladd = 0;
6024# endif
6025 }
6026 }
6027#endif
6028 }
6029 }
6030 did_ai = FALSE;
6031#ifdef FEAT_SMARTINDENT
6032 did_si = FALSE;
6033 can_si = FALSE;
6034 can_si_back = FALSE;
6035#endif
6036
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006037 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6038 * now in a different buffer. */
6039 if (end_insert_pos != NULL)
6040 {
6041 curbuf->b_op_start = Insstart;
6042 curbuf->b_op_end = *end_insert_pos;
6043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044}
6045
6046/*
6047 * Set the last inserted text to a single character.
6048 * Used for the replace command.
6049 */
6050 void
6051set_last_insert(c)
6052 int c;
6053{
6054 char_u *s;
6055
6056 vim_free(last_insert);
6057#ifdef FEAT_MBYTE
6058 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6059#else
6060 last_insert = alloc(6);
6061#endif
6062 if (last_insert != NULL)
6063 {
6064 s = last_insert;
6065 /* Use the CTRL-V only when entering a special char */
6066 if (c < ' ' || c == DEL)
6067 *s++ = Ctrl_V;
6068 s = add_char2buf(c, s);
6069 *s++ = ESC;
6070 *s++ = NUL;
6071 last_insert_skip = 0;
6072 }
6073}
6074
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006075#if defined(EXITFREE) || defined(PROTO)
6076 void
6077free_last_insert()
6078{
6079 vim_free(last_insert);
6080 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006081 vim_free(compl_orig_text);
6082 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006083}
6084#endif
6085
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086/*
6087 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6088 * and CSI. Handle multi-byte characters.
6089 * Returns a pointer to after the added bytes.
6090 */
6091 char_u *
6092add_char2buf(c, s)
6093 int c;
6094 char_u *s;
6095{
6096#ifdef FEAT_MBYTE
6097 char_u temp[MB_MAXBYTES];
6098 int i;
6099 int len;
6100
6101 len = (*mb_char2bytes)(c, temp);
6102 for (i = 0; i < len; ++i)
6103 {
6104 c = temp[i];
6105#endif
6106 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6107 if (c == K_SPECIAL)
6108 {
6109 *s++ = K_SPECIAL;
6110 *s++ = KS_SPECIAL;
6111 *s++ = KE_FILLER;
6112 }
6113#ifdef FEAT_GUI
6114 else if (c == CSI)
6115 {
6116 *s++ = CSI;
6117 *s++ = KS_EXTRA;
6118 *s++ = (int)KE_CSI;
6119 }
6120#endif
6121 else
6122 *s++ = c;
6123#ifdef FEAT_MBYTE
6124 }
6125#endif
6126 return s;
6127}
6128
6129/*
6130 * move cursor to start of line
6131 * if flags & BL_WHITE move to first non-white
6132 * if flags & BL_SOL move to first non-white if startofline is set,
6133 * otherwise keep "curswant" column
6134 * if flags & BL_FIX don't leave the cursor on a NUL.
6135 */
6136 void
6137beginline(flags)
6138 int flags;
6139{
6140 if ((flags & BL_SOL) && !p_sol)
6141 coladvance(curwin->w_curswant);
6142 else
6143 {
6144 curwin->w_cursor.col = 0;
6145#ifdef FEAT_VIRTUALEDIT
6146 curwin->w_cursor.coladd = 0;
6147#endif
6148
6149 if (flags & (BL_WHITE | BL_SOL))
6150 {
6151 char_u *ptr;
6152
6153 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6154 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6155 ++curwin->w_cursor.col;
6156 }
6157 curwin->w_set_curswant = TRUE;
6158 }
6159}
6160
6161/*
6162 * oneright oneleft cursor_down cursor_up
6163 *
6164 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006165 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 * Return OK when successful, FAIL when we hit a line of file boundary.
6167 */
6168
6169 int
6170oneright()
6171{
6172 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174
6175#ifdef FEAT_VIRTUALEDIT
6176 if (virtual_active())
6177 {
6178 pos_T prevpos = curwin->w_cursor;
6179
6180 /* Adjust for multi-wide char (excluding TAB) */
6181 ptr = ml_get_cursor();
6182 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006183# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006185# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006187# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 ))
6189 ? ptr2cells(ptr) : 1));
6190 curwin->w_set_curswant = TRUE;
6191 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6192 return (prevpos.col != curwin->w_cursor.col
6193 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6194 }
6195#endif
6196
6197 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006198 if (*ptr == NUL)
6199 return FAIL; /* already at the very end */
6200
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006202 if (has_mbyte)
6203 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 else
6205#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006206 l = 1;
6207
6208 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6209 * contains "onemore". */
6210 if (ptr[l] == NUL
6211#ifdef FEAT_VIRTUALEDIT
6212 && (ve_flags & VE_ONEMORE) == 0
6213#endif
6214 )
6215 return FAIL;
6216 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
6218 curwin->w_set_curswant = TRUE;
6219 return OK;
6220}
6221
6222 int
6223oneleft()
6224{
6225#ifdef FEAT_VIRTUALEDIT
6226 if (virtual_active())
6227 {
6228 int width;
6229 int v = getviscol();
6230
6231 if (v == 0)
6232 return FAIL;
6233
6234# ifdef FEAT_LINEBREAK
6235 /* We might get stuck on 'showbreak', skip over it. */
6236 width = 1;
6237 for (;;)
6238 {
6239 coladvance(v - width);
6240 /* getviscol() is slow, skip it when 'showbreak' is empty and
6241 * there are no multi-byte characters */
6242 if ((*p_sbr == NUL
6243# ifdef FEAT_MBYTE
6244 && !has_mbyte
6245# endif
6246 ) || getviscol() < v)
6247 break;
6248 ++width;
6249 }
6250# else
6251 coladvance(v - 1);
6252# endif
6253
6254 if (curwin->w_cursor.coladd == 1)
6255 {
6256 char_u *ptr;
6257
6258 /* Adjust for multi-wide char (not a TAB) */
6259 ptr = ml_get_cursor();
6260 if (*ptr != TAB && vim_isprintc(
6261# ifdef FEAT_MBYTE
6262 (*mb_ptr2char)(ptr)
6263# else
6264 *ptr
6265# endif
6266 ) && ptr2cells(ptr) > 1)
6267 curwin->w_cursor.coladd = 0;
6268 }
6269
6270 curwin->w_set_curswant = TRUE;
6271 return OK;
6272 }
6273#endif
6274
6275 if (curwin->w_cursor.col == 0)
6276 return FAIL;
6277
6278 curwin->w_set_curswant = TRUE;
6279 --curwin->w_cursor.col;
6280
6281#ifdef FEAT_MBYTE
6282 /* if the character on the left of the current cursor is a multi-byte
6283 * character, move to its first byte */
6284 if (has_mbyte)
6285 mb_adjust_cursor();
6286#endif
6287 return OK;
6288}
6289
6290 int
6291cursor_up(n, upd_topline)
6292 long n;
6293 int upd_topline; /* When TRUE: update topline */
6294{
6295 linenr_T lnum;
6296
6297 if (n > 0)
6298 {
6299 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006300 /* This fails if the cursor is already in the first line or the count
6301 * is larger than the line number and '-' is in 'cpoptions' */
6302 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 return FAIL;
6304 if (n >= lnum)
6305 lnum = 1;
6306 else
6307#ifdef FEAT_FOLDING
6308 if (hasAnyFolding(curwin))
6309 {
6310 /*
6311 * Count each sequence of folded lines as one logical line.
6312 */
6313 /* go to the the start of the current fold */
6314 (void)hasFolding(lnum, &lnum, NULL);
6315
6316 while (n--)
6317 {
6318 /* move up one line */
6319 --lnum;
6320 if (lnum <= 1)
6321 break;
6322 /* If we entered a fold, move to the beginning, unless in
6323 * Insert mode or when 'foldopen' contains "all": it will open
6324 * in a moment. */
6325 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6326 (void)hasFolding(lnum, &lnum, NULL);
6327 }
6328 if (lnum < 1)
6329 lnum = 1;
6330 }
6331 else
6332#endif
6333 lnum -= n;
6334 curwin->w_cursor.lnum = lnum;
6335 }
6336
6337 /* try to advance to the column we want to be at */
6338 coladvance(curwin->w_curswant);
6339
6340 if (upd_topline)
6341 update_topline(); /* make sure curwin->w_topline is valid */
6342
6343 return OK;
6344}
6345
6346/*
6347 * Cursor down a number of logical lines.
6348 */
6349 int
6350cursor_down(n, upd_topline)
6351 long n;
6352 int upd_topline; /* When TRUE: update topline */
6353{
6354 linenr_T lnum;
6355
6356 if (n > 0)
6357 {
6358 lnum = curwin->w_cursor.lnum;
6359#ifdef FEAT_FOLDING
6360 /* Move to last line of fold, will fail if it's the end-of-file. */
6361 (void)hasFolding(lnum, NULL, &lnum);
6362#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006363 /* This fails if the cursor is already in the last line or would move
6364 * beyound the last line and '-' is in 'cpoptions' */
6365 if (lnum >= curbuf->b_ml.ml_line_count
6366 || (lnum + n > curbuf->b_ml.ml_line_count
6367 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 return FAIL;
6369 if (lnum + n >= curbuf->b_ml.ml_line_count)
6370 lnum = curbuf->b_ml.ml_line_count;
6371 else
6372#ifdef FEAT_FOLDING
6373 if (hasAnyFolding(curwin))
6374 {
6375 linenr_T last;
6376
6377 /* count each sequence of folded lines as one logical line */
6378 while (n--)
6379 {
6380 if (hasFolding(lnum, NULL, &last))
6381 lnum = last + 1;
6382 else
6383 ++lnum;
6384 if (lnum >= curbuf->b_ml.ml_line_count)
6385 break;
6386 }
6387 if (lnum > curbuf->b_ml.ml_line_count)
6388 lnum = curbuf->b_ml.ml_line_count;
6389 }
6390 else
6391#endif
6392 lnum += n;
6393 curwin->w_cursor.lnum = lnum;
6394 }
6395
6396 /* try to advance to the column we want to be at */
6397 coladvance(curwin->w_curswant);
6398
6399 if (upd_topline)
6400 update_topline(); /* make sure curwin->w_topline is valid */
6401
6402 return OK;
6403}
6404
6405/*
6406 * Stuff the last inserted text in the read buffer.
6407 * Last_insert actually is a copy of the redo buffer, so we
6408 * first have to remove the command.
6409 */
6410 int
6411stuff_inserted(c, count, no_esc)
6412 int c; /* Command character to be inserted */
6413 long count; /* Repeat this many times */
6414 int no_esc; /* Don't add an ESC at the end */
6415{
6416 char_u *esc_ptr;
6417 char_u *ptr;
6418 char_u *last_ptr;
6419 char_u last = NUL;
6420
6421 ptr = get_last_insert();
6422 if (ptr == NULL)
6423 {
6424 EMSG(_(e_noinstext));
6425 return FAIL;
6426 }
6427
6428 /* may want to stuff the command character, to start Insert mode */
6429 if (c != NUL)
6430 stuffcharReadbuff(c);
6431 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6432 *esc_ptr = NUL; /* remove the ESC */
6433
6434 /* when the last char is either "0" or "^" it will be quoted if no ESC
6435 * comes after it OR if it will inserted more than once and "ptr"
6436 * starts with ^D. -- Acevedo
6437 */
6438 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6439 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6440 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6441 {
6442 last = *last_ptr;
6443 *last_ptr = NUL;
6444 }
6445
6446 do
6447 {
6448 stuffReadbuff(ptr);
6449 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6450 if (last)
6451 stuffReadbuff((char_u *)(last == '0'
6452 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6453 : IF_EB("\026^", CTRL_V_STR "^")));
6454 }
6455 while (--count > 0);
6456
6457 if (last)
6458 *last_ptr = last;
6459
6460 if (esc_ptr != NULL)
6461 *esc_ptr = ESC; /* put the ESC back */
6462
6463 /* may want to stuff a trailing ESC, to get out of Insert mode */
6464 if (!no_esc)
6465 stuffcharReadbuff(ESC);
6466
6467 return OK;
6468}
6469
6470 char_u *
6471get_last_insert()
6472{
6473 if (last_insert == NULL)
6474 return NULL;
6475 return last_insert + last_insert_skip;
6476}
6477
6478/*
6479 * Get last inserted string, and remove trailing <Esc>.
6480 * Returns pointer to allocated memory (must be freed) or NULL.
6481 */
6482 char_u *
6483get_last_insert_save()
6484{
6485 char_u *s;
6486 int len;
6487
6488 if (last_insert == NULL)
6489 return NULL;
6490 s = vim_strsave(last_insert + last_insert_skip);
6491 if (s != NULL)
6492 {
6493 len = (int)STRLEN(s);
6494 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6495 s[len - 1] = NUL;
6496 }
6497 return s;
6498}
6499
6500/*
6501 * Check the word in front of the cursor for an abbreviation.
6502 * Called when the non-id character "c" has been entered.
6503 * When an abbreviation is recognized it is removed from the text and
6504 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6505 */
6506 static int
6507echeck_abbr(c)
6508 int c;
6509{
6510 /* Don't check for abbreviation in paste mode, when disabled and just
6511 * after moving around with cursor keys. */
6512 if (p_paste || no_abbr || arrow_used)
6513 return FALSE;
6514
6515 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6516 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6517}
6518
6519/*
6520 * replace-stack functions
6521 *
6522 * When replacing characters, the replaced characters are remembered for each
6523 * new character. This is used to re-insert the old text when backspacing.
6524 *
6525 * There is a NUL headed list of characters for each character that is
6526 * currently in the file after the insertion point. When BS is used, one NUL
6527 * headed list is put back for the deleted character.
6528 *
6529 * For a newline, there are two NUL headed lists. One contains the characters
6530 * that the NL replaced. The extra one stores the characters after the cursor
6531 * that were deleted (always white space).
6532 *
6533 * Replace_offset is normally 0, in which case replace_push will add a new
6534 * character at the end of the stack. If replace_offset is not 0, that many
6535 * characters will be left on the stack above the newly inserted character.
6536 */
6537
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006538static char_u *replace_stack = NULL;
6539static long replace_stack_nr = 0; /* next entry in replace stack */
6540static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541
6542 void
6543replace_push(c)
6544 int c; /* character that is replaced (NUL is none) */
6545{
6546 char_u *p;
6547
6548 if (replace_stack_nr < replace_offset) /* nothing to do */
6549 return;
6550 if (replace_stack_len <= replace_stack_nr)
6551 {
6552 replace_stack_len += 50;
6553 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6554 if (p == NULL) /* out of memory */
6555 {
6556 replace_stack_len -= 50;
6557 return;
6558 }
6559 if (replace_stack != NULL)
6560 {
6561 mch_memmove(p, replace_stack,
6562 (size_t)(replace_stack_nr * sizeof(char_u)));
6563 vim_free(replace_stack);
6564 }
6565 replace_stack = p;
6566 }
6567 p = replace_stack + replace_stack_nr - replace_offset;
6568 if (replace_offset)
6569 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6570 *p = c;
6571 ++replace_stack_nr;
6572}
6573
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006574#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575/*
6576 * call replace_push(c) with replace_offset set to the first NUL.
6577 */
6578 static void
6579replace_push_off(c)
6580 int c;
6581{
6582 char_u *p;
6583
6584 p = replace_stack + replace_stack_nr;
6585 for (replace_offset = 1; replace_offset < replace_stack_nr;
6586 ++replace_offset)
6587 if (*--p == NUL)
6588 break;
6589 replace_push(c);
6590 replace_offset = 0;
6591}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593
6594/*
6595 * Pop one item from the replace stack.
6596 * return -1 if stack empty
6597 * return replaced character or NUL otherwise
6598 */
6599 static int
6600replace_pop()
6601{
6602 if (replace_stack_nr == 0)
6603 return -1;
6604 return (int)replace_stack[--replace_stack_nr];
6605}
6606
6607/*
6608 * Join the top two items on the replace stack. This removes to "off"'th NUL
6609 * encountered.
6610 */
6611 static void
6612replace_join(off)
6613 int off; /* offset for which NUL to remove */
6614{
6615 int i;
6616
6617 for (i = replace_stack_nr; --i >= 0; )
6618 if (replace_stack[i] == NUL && off-- <= 0)
6619 {
6620 --replace_stack_nr;
6621 mch_memmove(replace_stack + i, replace_stack + i + 1,
6622 (size_t)(replace_stack_nr - i));
6623 return;
6624 }
6625}
6626
6627/*
6628 * Pop bytes from the replace stack until a NUL is found, and insert them
6629 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6630 */
6631 static void
6632replace_pop_ins()
6633{
6634 int cc;
6635 int oldState = State;
6636
6637 State = NORMAL; /* don't want REPLACE here */
6638 while ((cc = replace_pop()) > 0)
6639 {
6640#ifdef FEAT_MBYTE
6641 mb_replace_pop_ins(cc);
6642#else
6643 ins_char(cc);
6644#endif
6645 dec_cursor();
6646 }
6647 State = oldState;
6648}
6649
6650#ifdef FEAT_MBYTE
6651/*
6652 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6653 * indicates a multi-byte char, pop the other bytes too.
6654 */
6655 static void
6656mb_replace_pop_ins(cc)
6657 int cc;
6658{
6659 int n;
6660 char_u buf[MB_MAXBYTES];
6661 int i;
6662 int c;
6663
6664 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6665 {
6666 buf[0] = cc;
6667 for (i = 1; i < n; ++i)
6668 buf[i] = replace_pop();
6669 ins_bytes_len(buf, n);
6670 }
6671 else
6672 ins_char(cc);
6673
6674 if (enc_utf8)
6675 /* Handle composing chars. */
6676 for (;;)
6677 {
6678 c = replace_pop();
6679 if (c == -1) /* stack empty */
6680 break;
6681 if ((n = MB_BYTE2LEN(c)) == 1)
6682 {
6683 /* Not a multi-byte char, put it back. */
6684 replace_push(c);
6685 break;
6686 }
6687 else
6688 {
6689 buf[0] = c;
6690 for (i = 1; i < n; ++i)
6691 buf[i] = replace_pop();
6692 if (utf_iscomposing(utf_ptr2char(buf)))
6693 ins_bytes_len(buf, n);
6694 else
6695 {
6696 /* Not a composing char, put it back. */
6697 for (i = n - 1; i >= 0; --i)
6698 replace_push(buf[i]);
6699 break;
6700 }
6701 }
6702 }
6703}
6704#endif
6705
6706/*
6707 * make the replace stack empty
6708 * (called when exiting replace mode)
6709 */
6710 static void
6711replace_flush()
6712{
6713 vim_free(replace_stack);
6714 replace_stack = NULL;
6715 replace_stack_len = 0;
6716 replace_stack_nr = 0;
6717}
6718
6719/*
6720 * Handle doing a BS for one character.
6721 * cc < 0: replace stack empty, just move cursor
6722 * cc == 0: character was inserted, delete it
6723 * cc > 0: character was replaced, put cc (first byte of original char) back
6724 * and check for more characters to be put back
6725 */
6726 static void
6727replace_do_bs()
6728{
6729 int cc;
6730#ifdef FEAT_VREPLACE
6731 int orig_len = 0;
6732 int ins_len;
6733 int orig_vcols = 0;
6734 colnr_T start_vcol;
6735 char_u *p;
6736 int i;
6737 int vcol;
6738#endif
6739
6740 cc = replace_pop();
6741 if (cc > 0)
6742 {
6743#ifdef FEAT_VREPLACE
6744 if (State & VREPLACE_FLAG)
6745 {
6746 /* Get the number of screen cells used by the character we are
6747 * going to delete. */
6748 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6749 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6750 }
6751#endif
6752#ifdef FEAT_MBYTE
6753 if (has_mbyte)
6754 {
6755 del_char(FALSE);
6756# ifdef FEAT_VREPLACE
6757 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006758 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759# endif
6760 replace_push(cc);
6761 }
6762 else
6763#endif
6764 {
6765 pchar_cursor(cc);
6766#ifdef FEAT_VREPLACE
6767 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006768 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769#endif
6770 }
6771 replace_pop_ins();
6772
6773#ifdef FEAT_VREPLACE
6774 if (State & VREPLACE_FLAG)
6775 {
6776 /* Get the number of screen cells used by the inserted characters */
6777 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006778 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 vcol = start_vcol;
6780 for (i = 0; i < ins_len; ++i)
6781 {
6782 vcol += chartabsize(p + i, vcol);
6783#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006784 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785#endif
6786 }
6787 vcol -= start_vcol;
6788
6789 /* Delete spaces that were inserted after the cursor to keep the
6790 * text aligned. */
6791 curwin->w_cursor.col += ins_len;
6792 while (vcol > orig_vcols && gchar_cursor() == ' ')
6793 {
6794 del_char(FALSE);
6795 ++orig_vcols;
6796 }
6797 curwin->w_cursor.col -= ins_len;
6798 }
6799#endif
6800
6801 /* mark the buffer as changed and prepare for displaying */
6802 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6803 }
6804 else if (cc == 0)
6805 (void)del_char(FALSE);
6806}
6807
6808#ifdef FEAT_CINDENT
6809/*
6810 * Return TRUE if C-indenting is on.
6811 */
6812 static int
6813cindent_on()
6814{
6815 return (!p_paste && (curbuf->b_p_cin
6816# ifdef FEAT_EVAL
6817 || *curbuf->b_p_inde != NUL
6818# endif
6819 ));
6820}
6821#endif
6822
6823#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6824/*
6825 * Re-indent the current line, based on the current contents of it and the
6826 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6827 * confused what all the part that handles Control-T is doing that I'm not.
6828 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6829 */
6830
6831 void
6832fixthisline(get_the_indent)
6833 int (*get_the_indent) __ARGS((void));
6834{
6835 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6836 if (linewhite(curwin->w_cursor.lnum))
6837 did_ai = TRUE; /* delete the indent if the line stays empty */
6838}
6839
6840 void
6841fix_indent()
6842{
6843 if (p_paste)
6844 return;
6845# ifdef FEAT_LISP
6846 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6847 fixthisline(get_lisp_indent);
6848# endif
6849# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6850 else
6851# endif
6852# ifdef FEAT_CINDENT
6853 if (cindent_on())
6854 do_c_expr_indent();
6855# endif
6856}
6857
6858#endif
6859
6860#ifdef FEAT_CINDENT
6861/*
6862 * return TRUE if 'cinkeys' contains the key "keytyped",
6863 * when == '*': Only if key is preceded with '*' (indent before insert)
6864 * when == '!': Only if key is prededed with '!' (don't insert)
6865 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6866 *
6867 * "keytyped" can have a few special values:
6868 * KEY_OPEN_FORW
6869 * KEY_OPEN_BACK
6870 * KEY_COMPLETE just finished completion.
6871 *
6872 * If line_is_empty is TRUE accept keys with '0' before them.
6873 */
6874 int
6875in_cinkeys(keytyped, when, line_is_empty)
6876 int keytyped;
6877 int when;
6878 int line_is_empty;
6879{
6880 char_u *look;
6881 int try_match;
6882 int try_match_word;
6883 char_u *p;
6884 char_u *line;
6885 int icase;
6886 int i;
6887
6888#ifdef FEAT_EVAL
6889 if (*curbuf->b_p_inde != NUL)
6890 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6891 else
6892#endif
6893 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6894 while (*look)
6895 {
6896 /*
6897 * Find out if we want to try a match with this key, depending on
6898 * 'when' and a '*' or '!' before the key.
6899 */
6900 switch (when)
6901 {
6902 case '*': try_match = (*look == '*'); break;
6903 case '!': try_match = (*look == '!'); break;
6904 default: try_match = (*look != '*'); break;
6905 }
6906 if (*look == '*' || *look == '!')
6907 ++look;
6908
6909 /*
6910 * If there is a '0', only accept a match if the line is empty.
6911 * But may still match when typing last char of a word.
6912 */
6913 if (*look == '0')
6914 {
6915 try_match_word = try_match;
6916 if (!line_is_empty)
6917 try_match = FALSE;
6918 ++look;
6919 }
6920 else
6921 try_match_word = FALSE;
6922
6923 /*
6924 * does it look like a control character?
6925 */
6926 if (*look == '^'
6927#ifdef EBCDIC
6928 && (Ctrl_chr(look[1]) != 0)
6929#else
6930 && look[1] >= '?' && look[1] <= '_'
6931#endif
6932 )
6933 {
6934 if (try_match && keytyped == Ctrl_chr(look[1]))
6935 return TRUE;
6936 look += 2;
6937 }
6938 /*
6939 * 'o' means "o" command, open forward.
6940 * 'O' means "O" command, open backward.
6941 */
6942 else if (*look == 'o')
6943 {
6944 if (try_match && keytyped == KEY_OPEN_FORW)
6945 return TRUE;
6946 ++look;
6947 }
6948 else if (*look == 'O')
6949 {
6950 if (try_match && keytyped == KEY_OPEN_BACK)
6951 return TRUE;
6952 ++look;
6953 }
6954
6955 /*
6956 * 'e' means to check for "else" at start of line and just before the
6957 * cursor.
6958 */
6959 else if (*look == 'e')
6960 {
6961 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6962 {
6963 p = ml_get_curline();
6964 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6965 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6966 return TRUE;
6967 }
6968 ++look;
6969 }
6970
6971 /*
6972 * ':' only causes an indent if it is at the end of a label or case
6973 * statement, or when it was before typing the ':' (to fix
6974 * class::method for C++).
6975 */
6976 else if (*look == ':')
6977 {
6978 if (try_match && keytyped == ':')
6979 {
6980 p = ml_get_curline();
6981 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6982 return TRUE;
6983 if (curwin->w_cursor.col > 2
6984 && p[curwin->w_cursor.col - 1] == ':'
6985 && p[curwin->w_cursor.col - 2] == ':')
6986 {
6987 p[curwin->w_cursor.col - 1] = ' ';
6988 i = (cin_iscase(p) || cin_isscopedecl(p)
6989 || cin_islabel(30));
6990 p = ml_get_curline();
6991 p[curwin->w_cursor.col - 1] = ':';
6992 if (i)
6993 return TRUE;
6994 }
6995 }
6996 ++look;
6997 }
6998
6999
7000 /*
7001 * Is it a key in <>, maybe?
7002 */
7003 else if (*look == '<')
7004 {
7005 if (try_match)
7006 {
7007 /*
7008 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7009 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7010 * >, *, : and ! keys if they really really want to.
7011 */
7012 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7013 && keytyped == look[1])
7014 return TRUE;
7015
7016 if (keytyped == get_special_key_code(look + 1))
7017 return TRUE;
7018 }
7019 while (*look && *look != '>')
7020 look++;
7021 while (*look == '>')
7022 look++;
7023 }
7024
7025 /*
7026 * Is it a word: "=word"?
7027 */
7028 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7029 {
7030 ++look;
7031 if (*look == '~')
7032 {
7033 icase = TRUE;
7034 ++look;
7035 }
7036 else
7037 icase = FALSE;
7038 p = vim_strchr(look, ',');
7039 if (p == NULL)
7040 p = look + STRLEN(look);
7041 if ((try_match || try_match_word)
7042 && curwin->w_cursor.col >= (colnr_T)(p - look))
7043 {
7044 int match = FALSE;
7045
7046#ifdef FEAT_INS_EXPAND
7047 if (keytyped == KEY_COMPLETE)
7048 {
7049 char_u *s;
7050
7051 /* Just completed a word, check if it starts with "look".
7052 * search back for the start of a word. */
7053 line = ml_get_curline();
7054# ifdef FEAT_MBYTE
7055 if (has_mbyte)
7056 {
7057 char_u *n;
7058
7059 for (s = line + curwin->w_cursor.col; s > line; s = n)
7060 {
7061 n = mb_prevptr(line, s);
7062 if (!vim_iswordp(n))
7063 break;
7064 }
7065 }
7066 else
7067# endif
7068 for (s = line + curwin->w_cursor.col; s > line; --s)
7069 if (!vim_iswordc(s[-1]))
7070 break;
7071 if (s + (p - look) <= line + curwin->w_cursor.col
7072 && (icase
7073 ? MB_STRNICMP(s, look, p - look)
7074 : STRNCMP(s, look, p - look)) == 0)
7075 match = TRUE;
7076 }
7077 else
7078#endif
7079 /* TODO: multi-byte */
7080 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7081 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7082 {
7083 line = ml_get_cursor();
7084 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7085 || !vim_iswordc(line[-(p - look) - 1]))
7086 && (icase
7087 ? MB_STRNICMP(line - (p - look), look, p - look)
7088 : STRNCMP(line - (p - look), look, p - look))
7089 == 0)
7090 match = TRUE;
7091 }
7092 if (match && try_match_word && !try_match)
7093 {
7094 /* "0=word": Check if there are only blanks before the
7095 * word. */
7096 line = ml_get_curline();
7097 if ((int)(skipwhite(line) - line) !=
7098 (int)(curwin->w_cursor.col - (p - look)))
7099 match = FALSE;
7100 }
7101 if (match)
7102 return TRUE;
7103 }
7104 look = p;
7105 }
7106
7107 /*
7108 * ok, it's a boring generic character.
7109 */
7110 else
7111 {
7112 if (try_match && *look == keytyped)
7113 return TRUE;
7114 ++look;
7115 }
7116
7117 /*
7118 * Skip over ", ".
7119 */
7120 look = skip_to_option_part(look);
7121 }
7122 return FALSE;
7123}
7124#endif /* FEAT_CINDENT */
7125
7126#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7127/*
7128 * Map Hebrew keyboard when in hkmap mode.
7129 */
7130 int
7131hkmap(c)
7132 int c;
7133{
7134 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7135 {
7136 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7137 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7138 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7139 static char_u map[26] =
7140 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7141 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7142 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7143 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7144 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7145 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7146 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7147 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7148 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7149
7150 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7151 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7152 /* '-1'='sofit' */
7153 else if (c == 'x')
7154 return 'X';
7155 else if (c == 'q')
7156 return '\''; /* {geresh}={'} */
7157 else if (c == 246)
7158 return ' '; /* \"o --> ' ' for a german keyboard */
7159 else if (c == 228)
7160 return ' '; /* \"a --> ' ' -- / -- */
7161 else if (c == 252)
7162 return ' '; /* \"u --> ' ' -- / -- */
7163#ifdef EBCDIC
7164 else if (islower(c))
7165#else
7166 /* NOTE: islower() does not do the right thing for us on Linux so we
7167 * do this the same was as 5.7 and previous, so it works correctly on
7168 * all systems. Specifically, the e.g. Delete and Arrow keys are
7169 * munged and won't work if e.g. searching for Hebrew text.
7170 */
7171 else if (c >= 'a' && c <= 'z')
7172#endif
7173 return (int)(map[CharOrdLow(c)] + p_aleph);
7174 else
7175 return c;
7176 }
7177 else
7178 {
7179 switch (c)
7180 {
7181 case '`': return ';';
7182 case '/': return '.';
7183 case '\'': return ',';
7184 case 'q': return '/';
7185 case 'w': return '\'';
7186
7187 /* Hebrew letters - set offset from 'a' */
7188 case ',': c = '{'; break;
7189 case '.': c = 'v'; break;
7190 case ';': c = 't'; break;
7191 default: {
7192 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7193
7194#ifdef EBCDIC
7195 /* see note about islower() above */
7196 if (!islower(c))
7197#else
7198 if (c < 'a' || c > 'z')
7199#endif
7200 return c;
7201 c = str[CharOrdLow(c)];
7202 break;
7203 }
7204 }
7205
7206 return (int)(CharOrdLow(c) + p_aleph);
7207 }
7208}
7209#endif
7210
7211 static void
7212ins_reg()
7213{
7214 int need_redraw = FALSE;
7215 int regname;
7216 int literally = 0;
7217
7218 /*
7219 * If we are going to wait for a character, show a '"'.
7220 */
7221 pc_status = PC_STATUS_UNSET;
7222 if (redrawing() && !char_avail())
7223 {
7224 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007225 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226
7227 edit_putchar('"', TRUE);
7228#ifdef FEAT_CMDL_INFO
7229 add_to_showcmd_c(Ctrl_R);
7230#endif
7231 }
7232
7233#ifdef USE_ON_FLY_SCROLL
7234 dont_scroll = TRUE; /* disallow scrolling here */
7235#endif
7236
7237 /*
7238 * Don't map the register name. This also prevents the mode message to be
7239 * deleted when ESC is hit.
7240 */
7241 ++no_mapping;
7242 regname = safe_vgetc();
7243#ifdef FEAT_LANGMAP
7244 LANGMAP_ADJUST(regname, TRUE);
7245#endif
7246 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7247 {
7248 /* Get a third key for literal register insertion */
7249 literally = regname;
7250#ifdef FEAT_CMDL_INFO
7251 add_to_showcmd_c(literally);
7252#endif
7253 regname = safe_vgetc();
7254#ifdef FEAT_LANGMAP
7255 LANGMAP_ADJUST(regname, TRUE);
7256#endif
7257 }
7258 --no_mapping;
7259
7260#ifdef FEAT_EVAL
7261 /*
7262 * Don't call u_sync() while getting the expression,
7263 * evaluating it or giving an error message for it!
7264 */
7265 ++no_u_sync;
7266 if (regname == '=')
7267 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007268# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007272# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 /* Restore the Input Method. */
7274 if (im_on)
7275 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007278 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7279 {
7280 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 else
7284 {
7285#endif
7286 if (literally == Ctrl_O || literally == Ctrl_P)
7287 {
7288 /* Append the command to the redo buffer. */
7289 AppendCharToRedobuff(Ctrl_R);
7290 AppendCharToRedobuff(literally);
7291 AppendCharToRedobuff(regname);
7292
7293 do_put(regname, BACKWARD, 1L,
7294 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7295 }
7296 else if (insert_reg(regname, literally) == FAIL)
7297 {
7298 vim_beep();
7299 need_redraw = TRUE; /* remove the '"' */
7300 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007301 else if (stop_insert_mode)
7302 /* When the '=' register was used and a function was invoked that
7303 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7304 * insert anything, need to remove the '"' */
7305 need_redraw = TRUE;
7306
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307#ifdef FEAT_EVAL
7308 }
7309 --no_u_sync;
7310#endif
7311#ifdef FEAT_CMDL_INFO
7312 clear_showcmd();
7313#endif
7314
7315 /* If the inserted register is empty, we need to remove the '"' */
7316 if (need_redraw || stuff_empty())
7317 edit_unputchar();
7318}
7319
7320/*
7321 * CTRL-G commands in Insert mode.
7322 */
7323 static void
7324ins_ctrl_g()
7325{
7326 int c;
7327
7328#ifdef FEAT_INS_EXPAND
7329 /* Right after CTRL-X the cursor will be after the ruler. */
7330 setcursor();
7331#endif
7332
7333 /*
7334 * Don't map the second key. This also prevents the mode message to be
7335 * deleted when ESC is hit.
7336 */
7337 ++no_mapping;
7338 c = safe_vgetc();
7339 --no_mapping;
7340 switch (c)
7341 {
7342 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7343 case K_UP:
7344 case Ctrl_K:
7345 case 'k': ins_up(TRUE);
7346 break;
7347
7348 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7349 case K_DOWN:
7350 case Ctrl_J:
7351 case 'j': ins_down(TRUE);
7352 break;
7353
7354 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007355 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007357
7358 /* Need to reset Insstart, esp. because a BS that joins
7359 * aline to the previous one must save for undo. */
7360 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 break;
7362
7363 /* Unknown CTRL-G command, reserved for future expansion. */
7364 default: vim_beep();
7365 }
7366}
7367
7368/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007369 * CTRL-^ in Insert mode.
7370 */
7371 static void
7372ins_ctrl_hat()
7373{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007374 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007375 {
7376 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7377 if (State & LANGMAP)
7378 {
7379 curbuf->b_p_iminsert = B_IMODE_NONE;
7380 State &= ~LANGMAP;
7381 }
7382 else
7383 {
7384 curbuf->b_p_iminsert = B_IMODE_LMAP;
7385 State |= LANGMAP;
7386#ifdef USE_IM_CONTROL
7387 im_set_active(FALSE);
7388#endif
7389 }
7390 }
7391#ifdef USE_IM_CONTROL
7392 else
7393 {
7394 /* There are no ":lmap" mappings, toggle IM */
7395 if (im_get_status())
7396 {
7397 curbuf->b_p_iminsert = B_IMODE_NONE;
7398 im_set_active(FALSE);
7399 }
7400 else
7401 {
7402 curbuf->b_p_iminsert = B_IMODE_IM;
7403 State &= ~LANGMAP;
7404 im_set_active(TRUE);
7405 }
7406 }
7407#endif
7408 set_iminsert_global();
7409 showmode();
7410#ifdef FEAT_GUI
7411 /* may show different cursor shape or color */
7412 if (gui.in_use)
7413 gui_update_cursor(TRUE, FALSE);
7414#endif
7415#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7416 /* Show/unshow value of 'keymap' in status lines. */
7417 status_redraw_curbuf();
7418#endif
7419}
7420
7421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 * Handle ESC in insert mode.
7423 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7424 * insert.
7425 */
7426 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007427ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 long *count;
7429 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007430 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431{
7432 int temp;
7433 static int disabled_redraw = FALSE;
7434
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007435#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007436 check_spell_redraw();
7437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438#if defined(FEAT_HANGULIN)
7439# if defined(ESC_CHG_TO_ENG_MODE)
7440 hangul_input_state_set(0);
7441# endif
7442 if (composing_hangul)
7443 {
7444 push_raw_key(composing_hangul_buffer, 2);
7445 composing_hangul = 0;
7446 }
7447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448
7449 temp = curwin->w_cursor.col;
7450 if (disabled_redraw)
7451 {
7452 --RedrawingDisabled;
7453 disabled_redraw = FALSE;
7454 }
7455 if (!arrow_used)
7456 {
7457 /*
7458 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007459 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7460 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 */
7462 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007463 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464
7465 /*
7466 * Repeating insert may take a long time. Check for
7467 * interrupt now and then.
7468 */
7469 if (*count > 0)
7470 {
7471 line_breakcheck();
7472 if (got_int)
7473 *count = 0;
7474 }
7475
7476 if (--*count > 0) /* repeat what was typed */
7477 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007478 /* Vi repeats the insert without replacing characters. */
7479 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7480 State &= ~REPLACE_FLAG;
7481
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 (void)start_redo_ins();
7483 if (cmdchar == 'r' || cmdchar == 'v')
7484 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7485 ++RedrawingDisabled;
7486 disabled_redraw = TRUE;
7487 return FALSE; /* repeat the insert */
7488 }
7489 stop_insert(&curwin->w_cursor, TRUE);
7490 undisplay_dollar();
7491 }
7492
7493 /* When an autoindent was removed, curswant stays after the
7494 * indent */
7495 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7496 curwin->w_set_curswant = TRUE;
7497
7498 /* Remember the last Insert position in the '^ mark. */
7499 if (!cmdmod.keepjumps)
7500 curbuf->b_last_insert = curwin->w_cursor;
7501
7502 /*
7503 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007504 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007506 if (!nomove
7507 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508#ifdef FEAT_VIRTUALEDIT
7509 || curwin->w_cursor.coladd > 0
7510#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007511 )
7512 && (restart_edit == NUL
7513 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007515 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007517 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518#ifdef FEAT_RIGHTLEFT
7519 && !revins_on
7520#endif
7521 )
7522 {
7523#ifdef FEAT_VIRTUALEDIT
7524 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7525 {
7526 oneleft();
7527 if (restart_edit != NUL)
7528 ++curwin->w_cursor.coladd;
7529 }
7530 else
7531#endif
7532 {
7533 --curwin->w_cursor.col;
7534#ifdef FEAT_MBYTE
7535 /* Correct cursor for multi-byte character. */
7536 if (has_mbyte)
7537 mb_adjust_cursor();
7538#endif
7539 }
7540 }
7541
7542#ifdef USE_IM_CONTROL
7543 /* Disable IM to allow typing English directly for Normal mode commands.
7544 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7545 * well). */
7546 if (!(State & LANGMAP))
7547 im_save_status(&curbuf->b_p_iminsert);
7548 im_set_active(FALSE);
7549#endif
7550
7551 State = NORMAL;
7552 /* need to position cursor again (e.g. when on a TAB ) */
7553 changed_cline_bef_curs();
7554
7555#ifdef FEAT_MOUSE
7556 setmouse();
7557#endif
7558#ifdef CURSOR_SHAPE
7559 ui_cursor_shape(); /* may show different cursor shape */
7560#endif
7561
7562 /*
7563 * When recording or for CTRL-O, need to display the new mode.
7564 * Otherwise remove the mode message.
7565 */
7566 if (Recording || restart_edit != NUL)
7567 showmode();
7568 else if (p_smd)
7569 MSG("");
7570
7571 return TRUE; /* exit Insert mode */
7572}
7573
7574#ifdef FEAT_RIGHTLEFT
7575/*
7576 * Toggle language: hkmap and revins_on.
7577 * Move to end of reverse inserted text.
7578 */
7579 static void
7580ins_ctrl_()
7581{
7582 if (revins_on && revins_chars && revins_scol >= 0)
7583 {
7584 while (gchar_cursor() != NUL && revins_chars--)
7585 ++curwin->w_cursor.col;
7586 }
7587 p_ri = !p_ri;
7588 revins_on = (State == INSERT && p_ri);
7589 if (revins_on)
7590 {
7591 revins_scol = curwin->w_cursor.col;
7592 revins_legal++;
7593 revins_chars = 0;
7594 undisplay_dollar();
7595 }
7596 else
7597 revins_scol = -1;
7598#ifdef FEAT_FKMAP
7599 if (p_altkeymap)
7600 {
7601 /*
7602 * to be consistent also for redo command, using '.'
7603 * set arrow_used to true and stop it - causing to redo
7604 * characters entered in one mode (normal/reverse insert).
7605 */
7606 arrow_used = TRUE;
7607 (void)stop_arrow();
7608 p_fkmap = curwin->w_p_rl ^ p_ri;
7609 if (p_fkmap && p_ri)
7610 State = INSERT;
7611 }
7612 else
7613#endif
7614 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7615 showmode();
7616}
7617#endif
7618
7619#ifdef FEAT_VISUAL
7620/*
7621 * If 'keymodel' contains "startsel", may start selection.
7622 * Returns TRUE when a CTRL-O and other keys stuffed.
7623 */
7624 static int
7625ins_start_select(c)
7626 int c;
7627{
7628 if (km_startsel)
7629 switch (c)
7630 {
7631 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 case K_PAGEUP:
7634 case K_KPAGEUP:
7635 case K_PAGEDOWN:
7636 case K_KPAGEDOWN:
7637# ifdef MACOS
7638 case K_LEFT:
7639 case K_RIGHT:
7640 case K_UP:
7641 case K_DOWN:
7642 case K_END:
7643 case K_HOME:
7644# endif
7645 if (!(mod_mask & MOD_MASK_SHIFT))
7646 break;
7647 /* FALLTHROUGH */
7648 case K_S_LEFT:
7649 case K_S_RIGHT:
7650 case K_S_UP:
7651 case K_S_DOWN:
7652 case K_S_END:
7653 case K_S_HOME:
7654 /* Start selection right away, the cursor can move with
7655 * CTRL-O when beyond the end of the line. */
7656 start_selection();
7657
7658 /* Execute the key in (insert) Select mode. */
7659 stuffcharReadbuff(Ctrl_O);
7660 if (mod_mask)
7661 {
7662 char_u buf[4];
7663
7664 buf[0] = K_SPECIAL;
7665 buf[1] = KS_MODIFIER;
7666 buf[2] = mod_mask;
7667 buf[3] = NUL;
7668 stuffReadbuff(buf);
7669 }
7670 stuffcharReadbuff(c);
7671 return TRUE;
7672 }
7673 return FALSE;
7674}
7675#endif
7676
7677/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007678 * <Insert> key in Insert mode: toggle insert/remplace mode.
7679 */
7680 static void
7681ins_insert(replaceState)
7682 int replaceState;
7683{
7684#ifdef FEAT_FKMAP
7685 if (p_fkmap && p_ri)
7686 {
7687 beep_flush();
7688 EMSG(farsi_text_3); /* encoded in Farsi */
7689 return;
7690 }
7691#endif
7692
7693#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007694# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007695 set_vim_var_string(VV_INSERTMODE,
7696 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007697# ifdef FEAT_VREPLACE
7698 replaceState == VREPLACE ? "v" :
7699# endif
7700 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007701# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007702 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7703#endif
7704 if (State & REPLACE_FLAG)
7705 State = INSERT | (State & LANGMAP);
7706 else
7707 State = replaceState | (State & LANGMAP);
7708 AppendCharToRedobuff(K_INS);
7709 showmode();
7710#ifdef CURSOR_SHAPE
7711 ui_cursor_shape(); /* may show different cursor shape */
7712#endif
7713}
7714
7715/*
7716 * Pressed CTRL-O in Insert mode.
7717 */
7718 static void
7719ins_ctrl_o()
7720{
7721#ifdef FEAT_VREPLACE
7722 if (State & VREPLACE_FLAG)
7723 restart_edit = 'V';
7724 else
7725#endif
7726 if (State & REPLACE_FLAG)
7727 restart_edit = 'R';
7728 else
7729 restart_edit = 'I';
7730#ifdef FEAT_VIRTUALEDIT
7731 if (virtual_active())
7732 ins_at_eol = FALSE; /* cursor always keeps its column */
7733 else
7734#endif
7735 ins_at_eol = (gchar_cursor() == NUL);
7736}
7737
7738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 * If the cursor is on an indent, ^T/^D insert/delete one
7740 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7741 * Always round the indent to 'shiftwith', this is compatible
7742 * with vi. But vi only supports ^T and ^D after an
7743 * autoindent, we support it everywhere.
7744 */
7745 static void
7746ins_shift(c, lastc)
7747 int c;
7748 int lastc;
7749{
7750 if (stop_arrow() == FAIL)
7751 return;
7752 AppendCharToRedobuff(c);
7753
7754 /*
7755 * 0^D and ^^D: remove all indent.
7756 */
7757 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7758 {
7759 --curwin->w_cursor.col;
7760 (void)del_char(FALSE); /* delete the '^' or '0' */
7761 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7762 if (State & REPLACE_FLAG)
7763 replace_pop_ins();
7764 if (lastc == '^')
7765 old_indent = get_indent(); /* remember curr. indent */
7766 change_indent(INDENT_SET, 0, TRUE, 0);
7767 }
7768 else
7769 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7770
7771 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7772 did_ai = FALSE;
7773#ifdef FEAT_SMARTINDENT
7774 did_si = FALSE;
7775 can_si = FALSE;
7776 can_si_back = FALSE;
7777#endif
7778#ifdef FEAT_CINDENT
7779 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7780#endif
7781}
7782
7783 static void
7784ins_del()
7785{
7786 int temp;
7787
7788 if (stop_arrow() == FAIL)
7789 return;
7790 if (gchar_cursor() == NUL) /* delete newline */
7791 {
7792 temp = curwin->w_cursor.col;
7793 if (!can_bs(BS_EOL) /* only if "eol" included */
7794 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7795 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7796 || do_join(FALSE) == FAIL)
7797 vim_beep();
7798 else
7799 curwin->w_cursor.col = temp;
7800 }
7801 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7802 vim_beep();
7803 did_ai = FALSE;
7804#ifdef FEAT_SMARTINDENT
7805 did_si = FALSE;
7806 can_si = FALSE;
7807 can_si_back = FALSE;
7808#endif
7809 AppendCharToRedobuff(K_DEL);
7810}
7811
7812/*
7813 * Handle Backspace, delete-word and delete-line in Insert mode.
7814 * Return TRUE when backspace was actually used.
7815 */
7816 static int
7817ins_bs(c, mode, inserted_space_p)
7818 int c;
7819 int mode;
7820 int *inserted_space_p;
7821{
7822 linenr_T lnum;
7823 int cc;
7824 int temp = 0; /* init for GCC */
7825 colnr_T mincol;
7826 int did_backspace = FALSE;
7827 int in_indent;
7828 int oldState;
7829#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007830 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831#endif
7832
7833 /*
7834 * can't delete anything in an empty file
7835 * can't backup past first character in buffer
7836 * can't backup past starting point unless 'backspace' > 1
7837 * can backup to a previous line if 'backspace' == 0
7838 */
7839 if ( bufempty()
7840 || (
7841#ifdef FEAT_RIGHTLEFT
7842 !revins_on &&
7843#endif
7844 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7845 || (!can_bs(BS_START)
7846 && (arrow_used
7847 || (curwin->w_cursor.lnum == Insstart.lnum
7848 && curwin->w_cursor.col <= Insstart.col)))
7849 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7850 && curwin->w_cursor.col <= ai_col)
7851 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7852 {
7853 vim_beep();
7854 return FALSE;
7855 }
7856
7857 if (stop_arrow() == FAIL)
7858 return FALSE;
7859 in_indent = inindent(0);
7860#ifdef FEAT_CINDENT
7861 if (in_indent)
7862 can_cindent = FALSE;
7863#endif
7864#ifdef FEAT_COMMENTS
7865 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7866#endif
7867#ifdef FEAT_RIGHTLEFT
7868 if (revins_on) /* put cursor after last inserted char */
7869 inc_cursor();
7870#endif
7871
7872#ifdef FEAT_VIRTUALEDIT
7873 /* Virtualedit:
7874 * BACKSPACE_CHAR eats a virtual space
7875 * BACKSPACE_WORD eats all coladd
7876 * BACKSPACE_LINE eats all coladd and keeps going
7877 */
7878 if (curwin->w_cursor.coladd > 0)
7879 {
7880 if (mode == BACKSPACE_CHAR)
7881 {
7882 --curwin->w_cursor.coladd;
7883 return TRUE;
7884 }
7885 if (mode == BACKSPACE_WORD)
7886 {
7887 curwin->w_cursor.coladd = 0;
7888 return TRUE;
7889 }
7890 curwin->w_cursor.coladd = 0;
7891 }
7892#endif
7893
7894 /*
7895 * delete newline!
7896 */
7897 if (curwin->w_cursor.col == 0)
7898 {
7899 lnum = Insstart.lnum;
7900 if (curwin->w_cursor.lnum == Insstart.lnum
7901#ifdef FEAT_RIGHTLEFT
7902 || revins_on
7903#endif
7904 )
7905 {
7906 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7907 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7908 return FALSE;
7909 --Insstart.lnum;
7910 Insstart.col = MAXCOL;
7911 }
7912 /*
7913 * In replace mode:
7914 * cc < 0: NL was inserted, delete it
7915 * cc >= 0: NL was replaced, put original characters back
7916 */
7917 cc = -1;
7918 if (State & REPLACE_FLAG)
7919 cc = replace_pop(); /* returns -1 if NL was inserted */
7920 /*
7921 * In replace mode, in the line we started replacing, we only move the
7922 * cursor.
7923 */
7924 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7925 {
7926 dec_cursor();
7927 }
7928 else
7929 {
7930#ifdef FEAT_VREPLACE
7931 if (!(State & VREPLACE_FLAG)
7932 || curwin->w_cursor.lnum > orig_line_count)
7933#endif
7934 {
7935 temp = gchar_cursor(); /* remember current char */
7936 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007937
7938 /* When "aw" is in 'formatoptions' we must delete the space at
7939 * the end of the line, otherwise the line will be broken
7940 * again when auto-formatting. */
7941 if (has_format_option(FO_AUTO)
7942 && has_format_option(FO_WHITE_PAR))
7943 {
7944 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7945 TRUE);
7946 int len;
7947
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007948 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007949 if (len > 0 && ptr[len - 1] == ' ')
7950 ptr[len - 1] = NUL;
7951 }
7952
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 (void)do_join(FALSE);
7954 if (temp == NUL && gchar_cursor() != NUL)
7955 inc_cursor();
7956 }
7957#ifdef FEAT_VREPLACE
7958 else
7959 dec_cursor();
7960#endif
7961
7962 /*
7963 * In REPLACE mode we have to put back the text that was replaced
7964 * by the NL. On the replace stack is first a NUL-terminated
7965 * sequence of characters that were deleted and then the
7966 * characters that NL replaced.
7967 */
7968 if (State & REPLACE_FLAG)
7969 {
7970 /*
7971 * Do the next ins_char() in NORMAL state, to
7972 * prevent ins_char() from replacing characters and
7973 * avoiding showmatch().
7974 */
7975 oldState = State;
7976 State = NORMAL;
7977 /*
7978 * restore characters (blanks) deleted after cursor
7979 */
7980 while (cc > 0)
7981 {
7982 temp = curwin->w_cursor.col;
7983#ifdef FEAT_MBYTE
7984 mb_replace_pop_ins(cc);
7985#else
7986 ins_char(cc);
7987#endif
7988 curwin->w_cursor.col = temp;
7989 cc = replace_pop();
7990 }
7991 /* restore the characters that NL replaced */
7992 replace_pop_ins();
7993 State = oldState;
7994 }
7995 }
7996 did_ai = FALSE;
7997 }
7998 else
7999 {
8000 /*
8001 * Delete character(s) before the cursor.
8002 */
8003#ifdef FEAT_RIGHTLEFT
8004 if (revins_on) /* put cursor on last inserted char */
8005 dec_cursor();
8006#endif
8007 mincol = 0;
8008 /* keep indent */
8009 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
8010#ifdef FEAT_RIGHTLEFT
8011 && !revins_on
8012#endif
8013 )
8014 {
8015 temp = curwin->w_cursor.col;
8016 beginline(BL_WHITE);
8017 if (curwin->w_cursor.col < (colnr_T)temp)
8018 mincol = curwin->w_cursor.col;
8019 curwin->w_cursor.col = temp;
8020 }
8021
8022 /*
8023 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8024 */
8025 if ( mode == BACKSPACE_CHAR
8026 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008027 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 && (*(ml_get_cursor() - 1) == TAB
8029 || (*(ml_get_cursor() - 1) == ' '
8030 && (!*inserted_space_p
8031 || arrow_used))))))
8032 {
8033 int ts;
8034 colnr_T vcol;
8035 colnr_T want_vcol;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008036#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 int extra = 0;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039
8040 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008041 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 ts = curbuf->b_p_sw;
8043 else
8044 ts = curbuf->b_p_sts;
8045 /* Compute the virtual column where we want to be. Since
8046 * 'showbreak' may get in the way, need to get the last column of
8047 * the previous character. */
8048 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8049 dec_cursor();
8050 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8051 inc_cursor();
8052 want_vcol = (want_vcol / ts) * ts;
8053
8054 /* delete characters until we are at or before want_vcol */
8055 while (vcol > want_vcol
8056 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
8057 {
8058 dec_cursor();
8059 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8060 if (State & REPLACE_FLAG)
8061 {
8062 /* Don't delete characters before the insert point when in
8063 * Replace mode */
8064 if (curwin->w_cursor.lnum != Insstart.lnum
8065 || curwin->w_cursor.col >= Insstart.col)
8066 {
8067#if 0 /* what was this for? It causes problems when sw != ts. */
8068 if (State == REPLACE && (int)vcol < want_vcol)
8069 {
8070 (void)del_char(FALSE);
8071 extra = 2; /* don't pop too much */
8072 }
8073 else
8074#endif
8075 replace_do_bs();
8076 }
8077 }
8078 else
8079 (void)del_char(FALSE);
8080 }
8081
8082 /* insert extra spaces until we are at want_vcol */
8083 while (vcol < want_vcol)
8084 {
8085 /* Remember the first char we inserted */
8086 if (curwin->w_cursor.lnum == Insstart.lnum
8087 && curwin->w_cursor.col < Insstart.col)
8088 Insstart.col = curwin->w_cursor.col;
8089
8090#ifdef FEAT_VREPLACE
8091 if (State & VREPLACE_FLAG)
8092 ins_char(' ');
8093 else
8094#endif
8095 {
8096 ins_str((char_u *)" ");
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008097 if ((State & REPLACE_FLAG) /* && extra <= 1 */)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008099#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 if (extra)
8101 replace_push_off(NUL);
8102 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 replace_push(NUL);
8105 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008106#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 if (extra == 2)
8108 extra = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 }
8111 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8112 }
8113 }
8114
8115 /*
8116 * Delete upto starting point, start of line or previous word.
8117 */
8118 else do
8119 {
8120#ifdef FEAT_RIGHTLEFT
8121 if (!revins_on) /* put cursor on char to be deleted */
8122#endif
8123 dec_cursor();
8124
8125 /* start of word? */
8126 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8127 {
8128 mode = BACKSPACE_WORD_NOT_SPACE;
8129 temp = vim_iswordc(gchar_cursor());
8130 }
8131 /* end of word? */
8132 else if (mode == BACKSPACE_WORD_NOT_SPACE
8133 && (vim_isspace(cc = gchar_cursor())
8134 || vim_iswordc(cc) != temp))
8135 {
8136#ifdef FEAT_RIGHTLEFT
8137 if (!revins_on)
8138#endif
8139 inc_cursor();
8140#ifdef FEAT_RIGHTLEFT
8141 else if (State & REPLACE_FLAG)
8142 dec_cursor();
8143#endif
8144 break;
8145 }
8146 if (State & REPLACE_FLAG)
8147 replace_do_bs();
8148 else
8149 {
8150#ifdef FEAT_MBYTE
8151 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008152 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153#endif
8154 (void)del_char(FALSE);
8155#ifdef FEAT_MBYTE
8156 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008157 * If there are combining characters and 'delcombine' is set
8158 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 * character.
8160 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008161 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 inc_cursor();
8163#endif
8164#ifdef FEAT_RIGHTLEFT
8165 if (revins_chars)
8166 {
8167 revins_chars--;
8168 revins_legal++;
8169 }
8170 if (revins_on && gchar_cursor() == NUL)
8171 break;
8172#endif
8173 }
8174 /* Just a single backspace?: */
8175 if (mode == BACKSPACE_CHAR)
8176 break;
8177 } while (
8178#ifdef FEAT_RIGHTLEFT
8179 revins_on ||
8180#endif
8181 (curwin->w_cursor.col > mincol
8182 && (curwin->w_cursor.lnum != Insstart.lnum
8183 || curwin->w_cursor.col != Insstart.col)));
8184 did_backspace = TRUE;
8185 }
8186#ifdef FEAT_SMARTINDENT
8187 did_si = FALSE;
8188 can_si = FALSE;
8189 can_si_back = FALSE;
8190#endif
8191 if (curwin->w_cursor.col <= 1)
8192 did_ai = FALSE;
8193 /*
8194 * It's a little strange to put backspaces into the redo
8195 * buffer, but it makes auto-indent a lot easier to deal
8196 * with.
8197 */
8198 AppendCharToRedobuff(c);
8199
8200 /* If deleted before the insertion point, adjust it */
8201 if (curwin->w_cursor.lnum == Insstart.lnum
8202 && curwin->w_cursor.col < Insstart.col)
8203 Insstart.col = curwin->w_cursor.col;
8204
8205 /* vi behaviour: the cursor moves backward but the character that
8206 * was there remains visible
8207 * Vim behaviour: the cursor moves backward and the character that
8208 * was there is erased from the screen.
8209 * We can emulate the vi behaviour by pretending there is a dollar
8210 * displayed even when there isn't.
8211 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8212 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8213 dollar_vcol = curwin->w_virtcol;
8214
8215 return did_backspace;
8216}
8217
8218#ifdef FEAT_MOUSE
8219 static void
8220ins_mouse(c)
8221 int c;
8222{
8223 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008224 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225
8226# ifdef FEAT_GUI
8227 /* When GUI is active, also move/paste when 'mouse' is empty */
8228 if (!gui.in_use)
8229# endif
8230 if (!mouse_has(MOUSE_INSERT))
8231 return;
8232
8233 undisplay_dollar();
8234 tpos = curwin->w_cursor;
8235 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8236 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008237#ifdef FEAT_WINDOWS
8238 win_T *new_curwin = curwin;
8239
8240 if (curwin != old_curwin && win_valid(old_curwin))
8241 {
8242 /* Mouse took us to another window. We need to go back to the
8243 * previous one to stop insert there properly. */
8244 curwin = old_curwin;
8245 curbuf = curwin->w_buffer;
8246 }
8247#endif
8248 start_arrow(curwin == old_curwin ? &tpos : NULL);
8249#ifdef FEAT_WINDOWS
8250 if (curwin != new_curwin && win_valid(new_curwin))
8251 {
8252 curwin = new_curwin;
8253 curbuf = curwin->w_buffer;
8254 }
8255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256# ifdef FEAT_CINDENT
8257 can_cindent = TRUE;
8258# endif
8259 }
8260
8261#ifdef FEAT_WINDOWS
8262 /* redraw status lines (in case another window became active) */
8263 redraw_statuslines();
8264#endif
8265}
8266
8267 static void
8268ins_mousescroll(up)
8269 int up;
8270{
8271 pos_T tpos;
8272# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8273 win_T *old_curwin;
8274# endif
8275
8276 tpos = curwin->w_cursor;
8277
8278# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8279 old_curwin = curwin;
8280
8281 /* Currently the mouse coordinates are only known in the GUI. */
8282 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8283 {
8284 int row, col;
8285
8286 row = mouse_row;
8287 col = mouse_col;
8288
8289 /* find the window at the pointer coordinates */
8290 curwin = mouse_find_win(&row, &col);
8291 curbuf = curwin->w_buffer;
8292 }
8293 if (curwin == old_curwin)
8294# endif
8295 undisplay_dollar();
8296
8297 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8298 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8299 else
8300 scroll_redraw(up, 3L);
8301
8302# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8303 curwin->w_redr_status = TRUE;
8304
8305 curwin = old_curwin;
8306 curbuf = curwin->w_buffer;
8307# endif
8308
8309 if (!equalpos(curwin->w_cursor, tpos))
8310 {
8311 start_arrow(&tpos);
8312# ifdef FEAT_CINDENT
8313 can_cindent = TRUE;
8314# endif
8315 }
8316}
8317#endif
8318
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008319#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008320 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008321ins_tabline(c)
8322 int c;
8323{
8324 /* We will be leaving the current window, unless closing another tab. */
8325 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8326 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8327 {
8328 undisplay_dollar();
8329 start_arrow(&curwin->w_cursor);
8330# ifdef FEAT_CINDENT
8331 can_cindent = TRUE;
8332# endif
8333 }
8334
8335 if (c == K_TABLINE)
8336 goto_tabpage(current_tab);
8337 else
8338 handle_tabmenu();
8339
8340}
8341#endif
8342
8343#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 void
8345ins_scroll()
8346{
8347 pos_T tpos;
8348
8349 undisplay_dollar();
8350 tpos = curwin->w_cursor;
8351 if (gui_do_scroll())
8352 {
8353 start_arrow(&tpos);
8354# ifdef FEAT_CINDENT
8355 can_cindent = TRUE;
8356# endif
8357 }
8358}
8359
8360 void
8361ins_horscroll()
8362{
8363 pos_T tpos;
8364
8365 undisplay_dollar();
8366 tpos = curwin->w_cursor;
8367 if (gui_do_horiz_scroll())
8368 {
8369 start_arrow(&tpos);
8370# ifdef FEAT_CINDENT
8371 can_cindent = TRUE;
8372# endif
8373 }
8374}
8375#endif
8376
8377 static void
8378ins_left()
8379{
8380 pos_T tpos;
8381
8382#ifdef FEAT_FOLDING
8383 if ((fdo_flags & FDO_HOR) && KeyTyped)
8384 foldOpenCursor();
8385#endif
8386 undisplay_dollar();
8387 tpos = curwin->w_cursor;
8388 if (oneleft() == OK)
8389 {
8390 start_arrow(&tpos);
8391#ifdef FEAT_RIGHTLEFT
8392 /* If exit reversed string, position is fixed */
8393 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8394 revins_legal++;
8395 revins_chars++;
8396#endif
8397 }
8398
8399 /*
8400 * if 'whichwrap' set for cursor in insert mode may go to
8401 * previous line
8402 */
8403 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8404 {
8405 start_arrow(&tpos);
8406 --(curwin->w_cursor.lnum);
8407 coladvance((colnr_T)MAXCOL);
8408 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8409 }
8410 else
8411 vim_beep();
8412}
8413
8414 static void
8415ins_home(c)
8416 int c;
8417{
8418 pos_T tpos;
8419
8420#ifdef FEAT_FOLDING
8421 if ((fdo_flags & FDO_HOR) && KeyTyped)
8422 foldOpenCursor();
8423#endif
8424 undisplay_dollar();
8425 tpos = curwin->w_cursor;
8426 if (c == K_C_HOME)
8427 curwin->w_cursor.lnum = 1;
8428 curwin->w_cursor.col = 0;
8429#ifdef FEAT_VIRTUALEDIT
8430 curwin->w_cursor.coladd = 0;
8431#endif
8432 curwin->w_curswant = 0;
8433 start_arrow(&tpos);
8434}
8435
8436 static void
8437ins_end(c)
8438 int c;
8439{
8440 pos_T tpos;
8441
8442#ifdef FEAT_FOLDING
8443 if ((fdo_flags & FDO_HOR) && KeyTyped)
8444 foldOpenCursor();
8445#endif
8446 undisplay_dollar();
8447 tpos = curwin->w_cursor;
8448 if (c == K_C_END)
8449 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8450 coladvance((colnr_T)MAXCOL);
8451 curwin->w_curswant = MAXCOL;
8452
8453 start_arrow(&tpos);
8454}
8455
8456 static void
8457ins_s_left()
8458{
8459#ifdef FEAT_FOLDING
8460 if ((fdo_flags & FDO_HOR) && KeyTyped)
8461 foldOpenCursor();
8462#endif
8463 undisplay_dollar();
8464 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8465 {
8466 start_arrow(&curwin->w_cursor);
8467 (void)bck_word(1L, FALSE, FALSE);
8468 curwin->w_set_curswant = TRUE;
8469 }
8470 else
8471 vim_beep();
8472}
8473
8474 static void
8475ins_right()
8476{
8477#ifdef FEAT_FOLDING
8478 if ((fdo_flags & FDO_HOR) && KeyTyped)
8479 foldOpenCursor();
8480#endif
8481 undisplay_dollar();
8482 if (gchar_cursor() != NUL || virtual_active()
8483 )
8484 {
8485 start_arrow(&curwin->w_cursor);
8486 curwin->w_set_curswant = TRUE;
8487#ifdef FEAT_VIRTUALEDIT
8488 if (virtual_active())
8489 oneright();
8490 else
8491#endif
8492 {
8493#ifdef FEAT_MBYTE
8494 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008495 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 else
8497#endif
8498 ++curwin->w_cursor.col;
8499 }
8500
8501#ifdef FEAT_RIGHTLEFT
8502 revins_legal++;
8503 if (revins_chars)
8504 revins_chars--;
8505#endif
8506 }
8507 /* if 'whichwrap' set for cursor in insert mode, may move the
8508 * cursor to the next line */
8509 else if (vim_strchr(p_ww, ']') != NULL
8510 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8511 {
8512 start_arrow(&curwin->w_cursor);
8513 curwin->w_set_curswant = TRUE;
8514 ++curwin->w_cursor.lnum;
8515 curwin->w_cursor.col = 0;
8516 }
8517 else
8518 vim_beep();
8519}
8520
8521 static void
8522ins_s_right()
8523{
8524#ifdef FEAT_FOLDING
8525 if ((fdo_flags & FDO_HOR) && KeyTyped)
8526 foldOpenCursor();
8527#endif
8528 undisplay_dollar();
8529 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8530 || gchar_cursor() != NUL)
8531 {
8532 start_arrow(&curwin->w_cursor);
8533 (void)fwd_word(1L, FALSE, 0);
8534 curwin->w_set_curswant = TRUE;
8535 }
8536 else
8537 vim_beep();
8538}
8539
8540 static void
8541ins_up(startcol)
8542 int startcol; /* when TRUE move to Insstart.col */
8543{
8544 pos_T tpos;
8545 linenr_T old_topline = curwin->w_topline;
8546#ifdef FEAT_DIFF
8547 int old_topfill = curwin->w_topfill;
8548#endif
8549
8550 undisplay_dollar();
8551 tpos = curwin->w_cursor;
8552 if (cursor_up(1L, TRUE) == OK)
8553 {
8554 if (startcol)
8555 coladvance(getvcol_nolist(&Insstart));
8556 if (old_topline != curwin->w_topline
8557#ifdef FEAT_DIFF
8558 || old_topfill != curwin->w_topfill
8559#endif
8560 )
8561 redraw_later(VALID);
8562 start_arrow(&tpos);
8563#ifdef FEAT_CINDENT
8564 can_cindent = TRUE;
8565#endif
8566 }
8567 else
8568 vim_beep();
8569}
8570
8571 static void
8572ins_pageup()
8573{
8574 pos_T tpos;
8575
8576 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008577
8578#ifdef FEAT_WINDOWS
8579 if (mod_mask & MOD_MASK_CTRL)
8580 {
8581 /* <C-PageUp>: tab page back */
8582 goto_tabpage(-1);
8583 return;
8584 }
8585#endif
8586
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 tpos = curwin->w_cursor;
8588 if (onepage(BACKWARD, 1L) == OK)
8589 {
8590 start_arrow(&tpos);
8591#ifdef FEAT_CINDENT
8592 can_cindent = TRUE;
8593#endif
8594 }
8595 else
8596 vim_beep();
8597}
8598
8599 static void
8600ins_down(startcol)
8601 int startcol; /* when TRUE move to Insstart.col */
8602{
8603 pos_T tpos;
8604 linenr_T old_topline = curwin->w_topline;
8605#ifdef FEAT_DIFF
8606 int old_topfill = curwin->w_topfill;
8607#endif
8608
8609 undisplay_dollar();
8610 tpos = curwin->w_cursor;
8611 if (cursor_down(1L, TRUE) == OK)
8612 {
8613 if (startcol)
8614 coladvance(getvcol_nolist(&Insstart));
8615 if (old_topline != curwin->w_topline
8616#ifdef FEAT_DIFF
8617 || old_topfill != curwin->w_topfill
8618#endif
8619 )
8620 redraw_later(VALID);
8621 start_arrow(&tpos);
8622#ifdef FEAT_CINDENT
8623 can_cindent = TRUE;
8624#endif
8625 }
8626 else
8627 vim_beep();
8628}
8629
8630 static void
8631ins_pagedown()
8632{
8633 pos_T tpos;
8634
8635 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008636
8637#ifdef FEAT_WINDOWS
8638 if (mod_mask & MOD_MASK_CTRL)
8639 {
8640 /* <C-PageDown>: tab page forward */
8641 goto_tabpage(0);
8642 return;
8643 }
8644#endif
8645
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 tpos = curwin->w_cursor;
8647 if (onepage(FORWARD, 1L) == OK)
8648 {
8649 start_arrow(&tpos);
8650#ifdef FEAT_CINDENT
8651 can_cindent = TRUE;
8652#endif
8653 }
8654 else
8655 vim_beep();
8656}
8657
8658#ifdef FEAT_DND
8659 static void
8660ins_drop()
8661{
8662 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8663}
8664#endif
8665
8666/*
8667 * Handle TAB in Insert or Replace mode.
8668 * Return TRUE when the TAB needs to be inserted like a normal character.
8669 */
8670 static int
8671ins_tab()
8672{
8673 int ind;
8674 int i;
8675 int temp;
8676
8677 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8678 Insstart_blank_vcol = get_nolist_virtcol();
8679 if (echeck_abbr(TAB + ABBR_OFF))
8680 return FALSE;
8681
8682 ind = inindent(0);
8683#ifdef FEAT_CINDENT
8684 if (ind)
8685 can_cindent = FALSE;
8686#endif
8687
8688 /*
8689 * When nothing special, insert TAB like a normal character
8690 */
8691 if (!curbuf->b_p_et
8692 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8693 && curbuf->b_p_sts == 0)
8694 return TRUE;
8695
8696 if (stop_arrow() == FAIL)
8697 return TRUE;
8698
8699 did_ai = FALSE;
8700#ifdef FEAT_SMARTINDENT
8701 did_si = FALSE;
8702 can_si = FALSE;
8703 can_si_back = FALSE;
8704#endif
8705 AppendToRedobuff((char_u *)"\t");
8706
8707 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8708 temp = (int)curbuf->b_p_sw;
8709 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8710 temp = (int)curbuf->b_p_sts;
8711 else /* otherwise use 'tabstop' */
8712 temp = (int)curbuf->b_p_ts;
8713 temp -= get_nolist_virtcol() % temp;
8714
8715 /*
8716 * Insert the first space with ins_char(). It will delete one char in
8717 * replace mode. Insert the rest with ins_str(); it will not delete any
8718 * chars. For VREPLACE mode, we use ins_char() for all characters.
8719 */
8720 ins_char(' ');
8721 while (--temp > 0)
8722 {
8723#ifdef FEAT_VREPLACE
8724 if (State & VREPLACE_FLAG)
8725 ins_char(' ');
8726 else
8727#endif
8728 {
8729 ins_str((char_u *)" ");
8730 if (State & REPLACE_FLAG) /* no char replaced */
8731 replace_push(NUL);
8732 }
8733 }
8734
8735 /*
8736 * When 'expandtab' not set: Replace spaces by TABs where possible.
8737 */
8738 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8739 {
8740 char_u *ptr;
8741#ifdef FEAT_VREPLACE
8742 char_u *saved_line = NULL; /* init for GCC */
8743 pos_T pos;
8744#endif
8745 pos_T fpos;
8746 pos_T *cursor;
8747 colnr_T want_vcol, vcol;
8748 int change_col = -1;
8749 int save_list = curwin->w_p_list;
8750
8751 /*
8752 * Get the current line. For VREPLACE mode, don't make real changes
8753 * yet, just work on a copy of the line.
8754 */
8755#ifdef FEAT_VREPLACE
8756 if (State & VREPLACE_FLAG)
8757 {
8758 pos = curwin->w_cursor;
8759 cursor = &pos;
8760 saved_line = vim_strsave(ml_get_curline());
8761 if (saved_line == NULL)
8762 return FALSE;
8763 ptr = saved_line + pos.col;
8764 }
8765 else
8766#endif
8767 {
8768 ptr = ml_get_cursor();
8769 cursor = &curwin->w_cursor;
8770 }
8771
8772 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8773 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8774 curwin->w_p_list = FALSE;
8775
8776 /* Find first white before the cursor */
8777 fpos = curwin->w_cursor;
8778 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8779 {
8780 --fpos.col;
8781 --ptr;
8782 }
8783
8784 /* In Replace mode, don't change characters before the insert point. */
8785 if ((State & REPLACE_FLAG)
8786 && fpos.lnum == Insstart.lnum
8787 && fpos.col < Insstart.col)
8788 {
8789 ptr += Insstart.col - fpos.col;
8790 fpos.col = Insstart.col;
8791 }
8792
8793 /* compute virtual column numbers of first white and cursor */
8794 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8795 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8796
8797 /* Use as many TABs as possible. Beware of 'showbreak' and
8798 * 'linebreak' adding extra virtual columns. */
8799 while (vim_iswhite(*ptr))
8800 {
8801 i = lbr_chartabsize((char_u *)"\t", vcol);
8802 if (vcol + i > want_vcol)
8803 break;
8804 if (*ptr != TAB)
8805 {
8806 *ptr = TAB;
8807 if (change_col < 0)
8808 {
8809 change_col = fpos.col; /* Column of first change */
8810 /* May have to adjust Insstart */
8811 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8812 Insstart.col = fpos.col;
8813 }
8814 }
8815 ++fpos.col;
8816 ++ptr;
8817 vcol += i;
8818 }
8819
8820 if (change_col >= 0)
8821 {
8822 int repl_off = 0;
8823
8824 /* Skip over the spaces we need. */
8825 while (vcol < want_vcol && *ptr == ' ')
8826 {
8827 vcol += lbr_chartabsize(ptr, vcol);
8828 ++ptr;
8829 ++repl_off;
8830 }
8831 if (vcol > want_vcol)
8832 {
8833 /* Must have a char with 'showbreak' just before it. */
8834 --ptr;
8835 --repl_off;
8836 }
8837 fpos.col += repl_off;
8838
8839 /* Delete following spaces. */
8840 i = cursor->col - fpos.col;
8841 if (i > 0)
8842 {
8843 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8844 /* correct replace stack. */
8845 if ((State & REPLACE_FLAG)
8846#ifdef FEAT_VREPLACE
8847 && !(State & VREPLACE_FLAG)
8848#endif
8849 )
8850 for (temp = i; --temp >= 0; )
8851 replace_join(repl_off);
8852 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008853#ifdef FEAT_NETBEANS_INTG
8854 if (usingNetbeans)
8855 {
8856 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8857 (long)(i + 1));
8858 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8859 (char_u *)"\t", 1);
8860 }
8861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 cursor->col -= i;
8863
8864#ifdef FEAT_VREPLACE
8865 /*
8866 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8867 * backspacing over the changed spacing and then inserting the new
8868 * spacing.
8869 */
8870 if (State & VREPLACE_FLAG)
8871 {
8872 /* Backspace from real cursor to change_col */
8873 backspace_until_column(change_col);
8874
8875 /* Insert each char in saved_line from changed_col to
8876 * ptr-cursor */
8877 ins_bytes_len(saved_line + change_col,
8878 cursor->col - change_col);
8879 }
8880#endif
8881 }
8882
8883#ifdef FEAT_VREPLACE
8884 if (State & VREPLACE_FLAG)
8885 vim_free(saved_line);
8886#endif
8887 curwin->w_p_list = save_list;
8888 }
8889
8890 return FALSE;
8891}
8892
8893/*
8894 * Handle CR or NL in insert mode.
8895 * Return TRUE when out of memory or can't undo.
8896 */
8897 static int
8898ins_eol(c)
8899 int c;
8900{
8901 int i;
8902
8903 if (echeck_abbr(c + ABBR_OFF))
8904 return FALSE;
8905 if (stop_arrow() == FAIL)
8906 return TRUE;
8907 undisplay_dollar();
8908
8909 /*
8910 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8911 * character under the cursor. Only push a NUL on the replace stack,
8912 * nothing to put back when the NL is deleted.
8913 */
8914 if ((State & REPLACE_FLAG)
8915#ifdef FEAT_VREPLACE
8916 && !(State & VREPLACE_FLAG)
8917#endif
8918 )
8919 replace_push(NUL);
8920
8921 /*
8922 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8923 * replacing the next line, so we push all of the characters left on the
8924 * line onto the replace stack. This is not done here though, it is done
8925 * in open_line().
8926 */
8927
8928#ifdef FEAT_RIGHTLEFT
8929# ifdef FEAT_FKMAP
8930 if (p_altkeymap && p_fkmap)
8931 fkmap(NL);
8932# endif
8933 /* NL in reverse insert will always start in the end of
8934 * current line. */
8935 if (revins_on)
8936 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8937#endif
8938
8939 AppendToRedobuff(NL_STR);
8940 i = open_line(FORWARD,
8941#ifdef FEAT_COMMENTS
8942 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8943#endif
8944 0, old_indent);
8945 old_indent = 0;
8946#ifdef FEAT_CINDENT
8947 can_cindent = TRUE;
8948#endif
8949
8950 return (!i);
8951}
8952
8953#ifdef FEAT_DIGRAPHS
8954/*
8955 * Handle digraph in insert mode.
8956 * Returns character still to be inserted, or NUL when nothing remaining to be
8957 * done.
8958 */
8959 static int
8960ins_digraph()
8961{
8962 int c;
8963 int cc;
8964
8965 pc_status = PC_STATUS_UNSET;
8966 if (redrawing() && !char_avail())
8967 {
8968 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008969 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970
8971 edit_putchar('?', TRUE);
8972#ifdef FEAT_CMDL_INFO
8973 add_to_showcmd_c(Ctrl_K);
8974#endif
8975 }
8976
8977#ifdef USE_ON_FLY_SCROLL
8978 dont_scroll = TRUE; /* disallow scrolling here */
8979#endif
8980
8981 /* don't map the digraph chars. This also prevents the
8982 * mode message to be deleted when ESC is hit */
8983 ++no_mapping;
8984 ++allow_keys;
8985 c = safe_vgetc();
8986 --no_mapping;
8987 --allow_keys;
8988 if (IS_SPECIAL(c) || mod_mask) /* special key */
8989 {
8990#ifdef FEAT_CMDL_INFO
8991 clear_showcmd();
8992#endif
8993 insert_special(c, TRUE, FALSE);
8994 return NUL;
8995 }
8996 if (c != ESC)
8997 {
8998 if (redrawing() && !char_avail())
8999 {
9000 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009001 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002
9003 if (char2cells(c) == 1)
9004 {
9005 /* first remove the '?', otherwise it's restored when typing
9006 * an ESC next */
9007 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009008 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 edit_putchar(c, TRUE);
9010 }
9011#ifdef FEAT_CMDL_INFO
9012 add_to_showcmd_c(c);
9013#endif
9014 }
9015 ++no_mapping;
9016 ++allow_keys;
9017 cc = safe_vgetc();
9018 --no_mapping;
9019 --allow_keys;
9020 if (cc != ESC)
9021 {
9022 AppendToRedobuff((char_u *)CTRL_V_STR);
9023 c = getdigraph(c, cc, TRUE);
9024#ifdef FEAT_CMDL_INFO
9025 clear_showcmd();
9026#endif
9027 return c;
9028 }
9029 }
9030 edit_unputchar();
9031#ifdef FEAT_CMDL_INFO
9032 clear_showcmd();
9033#endif
9034 return NUL;
9035}
9036#endif
9037
9038/*
9039 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9040 * Returns the char to be inserted, or NUL if none found.
9041 */
9042 static int
9043ins_copychar(lnum)
9044 linenr_T lnum;
9045{
9046 int c;
9047 int temp;
9048 char_u *ptr, *prev_ptr;
9049
9050 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9051 {
9052 vim_beep();
9053 return NUL;
9054 }
9055
9056 /* try to advance to the cursor column */
9057 temp = 0;
9058 ptr = ml_get(lnum);
9059 prev_ptr = ptr;
9060 validate_virtcol();
9061 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9062 {
9063 prev_ptr = ptr;
9064 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9065 }
9066 if ((colnr_T)temp > curwin->w_virtcol)
9067 ptr = prev_ptr;
9068
9069#ifdef FEAT_MBYTE
9070 c = (*mb_ptr2char)(ptr);
9071#else
9072 c = *ptr;
9073#endif
9074 if (c == NUL)
9075 vim_beep();
9076 return c;
9077}
9078
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009079/*
9080 * CTRL-Y or CTRL-E typed in Insert mode.
9081 */
9082 static int
9083ins_ctrl_ey(tc)
9084 int tc;
9085{
9086 int c = tc;
9087
9088#ifdef FEAT_INS_EXPAND
9089 if (ctrl_x_mode == CTRL_X_SCROLL)
9090 {
9091 if (c == Ctrl_Y)
9092 scrolldown_clamp();
9093 else
9094 scrollup_clamp();
9095 redraw_later(VALID);
9096 }
9097 else
9098#endif
9099 {
9100 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9101 if (c != NUL)
9102 {
9103 long tw_save;
9104
9105 /* The character must be taken literally, insert like it
9106 * was typed after a CTRL-V, and pretend 'textwidth'
9107 * wasn't set. Digits, 'o' and 'x' are special after a
9108 * CTRL-V, don't use it for these. */
9109 if (c < 256 && !isalnum(c))
9110 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9111 tw_save = curbuf->b_p_tw;
9112 curbuf->b_p_tw = -1;
9113 insert_special(c, TRUE, FALSE);
9114 curbuf->b_p_tw = tw_save;
9115#ifdef FEAT_RIGHTLEFT
9116 revins_chars++;
9117 revins_legal++;
9118#endif
9119 c = Ctrl_V; /* pretend CTRL-V is last character */
9120 auto_format(FALSE, TRUE);
9121 }
9122 }
9123 return c;
9124}
9125
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126#ifdef FEAT_SMARTINDENT
9127/*
9128 * Try to do some very smart auto-indenting.
9129 * Used when inserting a "normal" character.
9130 */
9131 static void
9132ins_try_si(c)
9133 int c;
9134{
9135 pos_T *pos, old_pos;
9136 char_u *ptr;
9137 int i;
9138 int temp;
9139
9140 /*
9141 * do some very smart indenting when entering '{' or '}'
9142 */
9143 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9144 {
9145 /*
9146 * for '}' set indent equal to indent of line containing matching '{'
9147 */
9148 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9149 {
9150 old_pos = curwin->w_cursor;
9151 /*
9152 * If the matching '{' has a ')' immediately before it (ignoring
9153 * white-space), then line up with the start of the line
9154 * containing the matching '(' if there is one. This handles the
9155 * case where an "if (..\n..) {" statement continues over multiple
9156 * lines -- webb
9157 */
9158 ptr = ml_get(pos->lnum);
9159 i = pos->col;
9160 if (i > 0) /* skip blanks before '{' */
9161 while (--i > 0 && vim_iswhite(ptr[i]))
9162 ;
9163 curwin->w_cursor.lnum = pos->lnum;
9164 curwin->w_cursor.col = i;
9165 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9166 curwin->w_cursor = *pos;
9167 i = get_indent();
9168 curwin->w_cursor = old_pos;
9169#ifdef FEAT_VREPLACE
9170 if (State & VREPLACE_FLAG)
9171 change_indent(INDENT_SET, i, FALSE, NUL);
9172 else
9173#endif
9174 (void)set_indent(i, SIN_CHANGED);
9175 }
9176 else if (curwin->w_cursor.col > 0)
9177 {
9178 /*
9179 * when inserting '{' after "O" reduce indent, but not
9180 * more than indent of previous line
9181 */
9182 temp = TRUE;
9183 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9184 {
9185 old_pos = curwin->w_cursor;
9186 i = get_indent();
9187 while (curwin->w_cursor.lnum > 1)
9188 {
9189 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9190
9191 /* ignore empty lines and lines starting with '#'. */
9192 if (*ptr != '#' && *ptr != NUL)
9193 break;
9194 }
9195 if (get_indent() >= i)
9196 temp = FALSE;
9197 curwin->w_cursor = old_pos;
9198 }
9199 if (temp)
9200 shift_line(TRUE, FALSE, 1);
9201 }
9202 }
9203
9204 /*
9205 * set indent of '#' always to 0
9206 */
9207 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9208 {
9209 /* remember current indent for next line */
9210 old_indent = get_indent();
9211 (void)set_indent(0, SIN_CHANGED);
9212 }
9213
9214 /* Adjust ai_col, the char at this position can be deleted. */
9215 if (ai_col > curwin->w_cursor.col)
9216 ai_col = curwin->w_cursor.col;
9217}
9218#endif
9219
9220/*
9221 * Get the value that w_virtcol would have when 'list' is off.
9222 * Unless 'cpo' contains the 'L' flag.
9223 */
9224 static colnr_T
9225get_nolist_virtcol()
9226{
9227 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9228 return getvcol_nolist(&curwin->w_cursor);
9229 validate_virtcol();
9230 return curwin->w_virtcol;
9231}