blob: e5e0522f7964e195e0852b154dfe873b63294b80 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
39
40static char *ctrl_x_msgs[] =
41{
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000044 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 N_(" Whole line completion (^L^N^P)"),
46 N_(" File name completion (^F^N^P)"),
47 N_(" Tag completion (^]^N^P)"),
48 N_(" Path pattern completion (^N^P)"),
49 N_(" Definition completion (^D^N^P)"),
50 NULL,
51 N_(" Dictionary completion (^K^N^P)"),
52 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000053 N_(" Command-line completion (^V^N^P)"),
54 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000055 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000056 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000057 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +000058};
59
60static char_u e_hitend[] = N_("Hit end of paragraph");
61
62/*
63 * Structure used to store one match for insert completion.
64 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000065typedef struct compl_S compl_T;
66struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000067{
Bram Moolenaar572cb562005-08-05 21:35:02 +000068 compl_T *cp_next;
69 compl_T *cp_prev;
70 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000071 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000072 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000073 char_u *cp_fname; /* file containing the match, allocated when
74 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
76 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077};
78
Bram Moolenaar572cb562005-08-05 21:35:02 +000079#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#define FREE_FNAME (2)
81
82/*
83 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000084 * "compl_first_match" points to the start of the list.
85 * "compl_curr_match" points to the currently selected entry.
86 * "compl_shown_match" is different from compl_curr_match during
87 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000089static compl_T *compl_first_match = NULL;
90static compl_T *compl_curr_match = NULL;
91static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
Bram Moolenaar779b74b2006-04-10 14:55:34 +000093/* After using a cursor key <Enter> selects a match in the popup menu,
94 * otherwise it inserts a line break. */
95static int compl_enter_selects = FALSE;
96
Bram Moolenaara6557602006-02-04 22:43:20 +000097/* When "compl_leader" is not NULL only matches that start with this string
98 * are used. */
99static char_u *compl_leader = NULL;
100
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000101static int compl_get_longest = FALSE; /* put longest common string
102 in compl_leader */
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104static int compl_used_match; /* Selected one of the matches. When
105 FALSE the match was edited or using
106 the longest common string. */
107
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000108static int compl_was_interrupted = FALSE; /* didn't finish finding
109 completions. */
110
111static int compl_restarting = FALSE; /* don't insert match */
112
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000113/* When the first completion is done "compl_started" is set. When it's
114 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000115static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000116
Bram Moolenaar572cb562005-08-05 21:35:02 +0000117static int compl_matches = 0;
118static char_u *compl_pattern = NULL;
119static int compl_direction = FORWARD;
120static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000121static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000122static pos_T compl_startpos;
123static colnr_T compl_col = 0; /* column where the text starts
124 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000125static char_u *compl_orig_text = NULL; /* text as it was before
126 * completion started */
127static int compl_cont_mode = 0;
128static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000130static void ins_ctrl_x __ARGS((void));
131static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000132static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000133static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000134static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000135static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000137static void ins_compl_upd_pum __ARGS((void));
138static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000139static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000140static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000141static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000142static 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 +0000143static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144static void ins_compl_free __ARGS((void));
145static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000146static int ins_compl_bs __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000147static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000148static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000149static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000150static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000151static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000152static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000154#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
155static void ins_compl_add_list __ARGS((list_T *list));
156#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000157static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static void ins_compl_delete __ARGS((void));
159static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000160static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000161static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000162static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000163static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000164static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165static int ins_complete __ARGS((int c));
166static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
167#endif /* FEAT_INS_EXPAND */
168
169#define BACKSPACE_CHAR 1
170#define BACKSPACE_WORD 2
171#define BACKSPACE_WORD_NOT_SPACE 3
172#define BACKSPACE_LINE 4
173
Bram Moolenaar754b5602006-02-09 23:53:20 +0000174static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static void ins_ctrl_v __ARGS((void));
176static void undisplay_dollar __ARGS((void));
177static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000178static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179static void check_auto_format __ARGS((int));
180static void redo_literal __ARGS((int c));
181static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000182#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000183static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000184static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000185static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
188static int echeck_abbr __ARGS((int));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000189#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190static void replace_push_off __ARGS((int c));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192static int replace_pop __ARGS((void));
193static void replace_join __ARGS((int off));
194static void replace_pop_ins __ARGS((void));
195#ifdef FEAT_MBYTE
196static void mb_replace_pop_ins __ARGS((int cc));
197#endif
198static void replace_flush __ARGS((void));
199static void replace_do_bs __ARGS((void));
200#ifdef FEAT_CINDENT
201static int cindent_on __ARGS((void));
202#endif
203static void ins_reg __ARGS((void));
204static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000205static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000206static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifdef FEAT_RIGHTLEFT
208static void ins_ctrl_ __ARGS((void));
209#endif
210#ifdef FEAT_VISUAL
211static int ins_start_select __ARGS((int c));
212#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000213static void ins_insert __ARGS((int replaceState));
214static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215static void ins_shift __ARGS((int c, int lastc));
216static void ins_del __ARGS((void));
217static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
218#ifdef FEAT_MOUSE
219static void ins_mouse __ARGS((int c));
220static void ins_mousescroll __ARGS((int up));
221#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000222#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
223static void ins_tabline __ARGS((int c));
224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225static void ins_left __ARGS((void));
226static void ins_home __ARGS((int c));
227static void ins_end __ARGS((int c));
228static void ins_s_left __ARGS((void));
229static void ins_right __ARGS((void));
230static void ins_s_right __ARGS((void));
231static void ins_up __ARGS((int startcol));
232static void ins_pageup __ARGS((void));
233static void ins_down __ARGS((int startcol));
234static void ins_pagedown __ARGS((void));
235#ifdef FEAT_DND
236static void ins_drop __ARGS((void));
237#endif
238static int ins_tab __ARGS((void));
239static int ins_eol __ARGS((int c));
240#ifdef FEAT_DIGRAPHS
241static int ins_digraph __ARGS((void));
242#endif
243static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000244static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245#ifdef FEAT_SMARTINDENT
246static void ins_try_si __ARGS((int c));
247#endif
248static colnr_T get_nolist_virtcol __ARGS((void));
249
250static colnr_T Insstart_textlen; /* length of line when insert started */
251static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
252
253static char_u *last_insert = NULL; /* the text of the previous insert,
254 K_SPECIAL and CSI are escaped */
255static int last_insert_skip; /* nr of chars in front of previous insert */
256static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000257static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
259#ifdef FEAT_CINDENT
260static int can_cindent; /* may do cindenting on this line */
261#endif
262
263static int old_indent = 0; /* for ^^D command in insert mode */
264
265#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000266static int revins_on; /* reverse insert mode on */
267static int revins_chars; /* how much to skip after edit */
268static int revins_legal; /* was the last char 'legal'? */
269static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270#endif
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272static int ins_need_undo; /* call u_save() before inserting a
273 char. Set when edit() is called.
274 after that arrow_used is used. */
275
276static int did_add_space = FALSE; /* auto_format() added an extra space
277 under the cursor */
278
279/*
280 * edit(): Start inserting text.
281 *
282 * "cmdchar" can be:
283 * 'i' normal insert command
284 * 'a' normal append command
285 * 'R' replace command
286 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
287 * but still only one <CR> is inserted. The <Esc> is not used for redo.
288 * 'g' "gI" command.
289 * 'V' "gR" command for Virtual Replace mode.
290 * 'v' "gr" command for single character Virtual Replace mode.
291 *
292 * This function is not called recursively. For CTRL-O commands, it returns
293 * and lets the caller handle the Normal-mode command.
294 *
295 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
296 */
297 int
298edit(cmdchar, startln, count)
299 int cmdchar;
300 int startln; /* if set, insert at start of line */
301 long count;
302{
303 int c = 0;
304 char_u *ptr;
305 int lastc;
306 colnr_T mincol;
307 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 int i;
309 int did_backspace = TRUE; /* previous char was backspace */
310#ifdef FEAT_CINDENT
311 int line_is_white = FALSE; /* line is empty before insert */
312#endif
313 linenr_T old_topline = 0; /* topline before insertion */
314#ifdef FEAT_DIFF
315 int old_topfill = -1;
316#endif
317 int inserted_space = FALSE; /* just inserted a space */
318 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000319 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000321 /* Remember whether editing was restarted after CTRL-O. */
322 did_restart_edit = restart_edit;
323
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 /* sleep before redrawing, needed for "CTRL-O :" that results in an
325 * error message */
326 check_for_delay(TRUE);
327
328#ifdef HAVE_SANDBOX
329 /* Don't allow inserting in the sandbox. */
330 if (sandbox != 0)
331 {
332 EMSG(_(e_sandbox));
333 return FALSE;
334 }
335#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000336 /* Don't allow changes in the buffer while editing the cmdline. The
337 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000338 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000339 {
340 EMSG(_(e_secure));
341 return FALSE;
342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343
344#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000345 /* Don't allow recursive insert mode when busy with completion. */
346 if (compl_started || pum_visible())
347 {
348 EMSG(_(e_secure));
349 return FALSE;
350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 ins_compl_clear(); /* clear stuff for CTRL-X mode */
352#endif
353
Bram Moolenaar843ee412004-06-30 16:16:41 +0000354#ifdef FEAT_AUTOCMD
355 /*
356 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
357 */
358 if (cmdchar != 'r' && cmdchar != 'v')
359 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000360# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000361 if (cmdchar == 'R')
362 ptr = (char_u *)"r";
363 else if (cmdchar == 'V')
364 ptr = (char_u *)"v";
365 else
366 ptr = (char_u *)"i";
367 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000368# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000369 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
370 }
371#endif
372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#ifdef FEAT_MOUSE
374 /*
375 * When doing a paste with the middle mouse button, Insstart is set to
376 * where the paste started.
377 */
378 if (where_paste_started.lnum != 0)
379 Insstart = where_paste_started;
380 else
381#endif
382 {
383 Insstart = curwin->w_cursor;
384 if (startln)
385 Insstart.col = 0;
386 }
387 Insstart_textlen = linetabsize(ml_get_curline());
388 Insstart_blank_vcol = MAXCOL;
389 if (!did_ai)
390 ai_col = 0;
391
392 if (cmdchar != NUL && restart_edit == 0)
393 {
394 ResetRedobuff();
395 AppendNumberToRedobuff(count);
396#ifdef FEAT_VREPLACE
397 if (cmdchar == 'V' || cmdchar == 'v')
398 {
399 /* "gR" or "gr" command */
400 AppendCharToRedobuff('g');
401 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
402 }
403 else
404#endif
405 {
406 AppendCharToRedobuff(cmdchar);
407 if (cmdchar == 'g') /* "gI" command */
408 AppendCharToRedobuff('I');
409 else if (cmdchar == 'r') /* "r<CR>" command */
410 count = 1; /* insert only one <CR> */
411 }
412 }
413
414 if (cmdchar == 'R')
415 {
416#ifdef FEAT_FKMAP
417 if (p_fkmap && p_ri)
418 {
419 beep_flush();
420 EMSG(farsi_text_3); /* encoded in Farsi */
421 State = INSERT;
422 }
423 else
424#endif
425 State = REPLACE;
426 }
427#ifdef FEAT_VREPLACE
428 else if (cmdchar == 'V' || cmdchar == 'v')
429 {
430 State = VREPLACE;
431 replaceState = VREPLACE;
432 orig_line_count = curbuf->b_ml.ml_line_count;
433 vr_lines_changed = 1;
434 }
435#endif
436 else
437 State = INSERT;
438
439 stop_insert_mode = FALSE;
440
441 /*
442 * Need to recompute the cursor position, it might move when the cursor is
443 * on a TAB or special character.
444 */
445 curs_columns(TRUE);
446
447 /*
448 * Enable langmap or IME, indicated by 'iminsert'.
449 * Note that IME may enabled/disabled without us noticing here, thus the
450 * 'iminsert' value may not reflect what is actually used. It is updated
451 * when hitting <Esc>.
452 */
453 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
454 State |= LANGMAP;
455#ifdef USE_IM_CONTROL
456 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
457#endif
458
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459#ifdef FEAT_MOUSE
460 setmouse();
461#endif
462#ifdef FEAT_CMDL_INFO
463 clear_showcmd();
464#endif
465#ifdef FEAT_RIGHTLEFT
466 /* there is no reverse replace mode */
467 revins_on = (State == INSERT && p_ri);
468 if (revins_on)
469 undisplay_dollar();
470 revins_chars = 0;
471 revins_legal = 0;
472 revins_scol = -1;
473#endif
474
475 /*
476 * Handle restarting Insert mode.
477 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
478 * restart_edit non-zero, and something in the stuff buffer.
479 */
480 if (restart_edit != 0 && stuff_empty())
481 {
482#ifdef FEAT_MOUSE
483 /*
484 * After a paste we consider text typed to be part of the insert for
485 * the pasted text. You can backspace over the pasted text too.
486 */
487 if (where_paste_started.lnum)
488 arrow_used = FALSE;
489 else
490#endif
491 arrow_used = TRUE;
492 restart_edit = 0;
493
494 /*
495 * If the cursor was after the end-of-line before the CTRL-O and it is
496 * now at the end-of-line, put it after the end-of-line (this is not
497 * correct in very rare cases).
498 * Also do this if curswant is greater than the current virtual
499 * column. Eg after "^O$" or "^O80|".
500 */
501 validate_virtcol();
502 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000503 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 || curwin->w_curswant > curwin->w_virtcol)
505 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
506 {
507 if (ptr[1] == NUL)
508 ++curwin->w_cursor.col;
509#ifdef FEAT_MBYTE
510 else if (has_mbyte)
511 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000512 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 if (ptr[i] == NUL)
514 curwin->w_cursor.col += i;
515 }
516#endif
517 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000518 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 }
520 else
521 arrow_used = FALSE;
522
523 /* we are in insert mode now, don't need to start it anymore */
524 need_start_insertmode = FALSE;
525
526 /* Need to save the line for undo before inserting the first char. */
527 ins_need_undo = TRUE;
528
529#ifdef FEAT_MOUSE
530 where_paste_started.lnum = 0;
531#endif
532#ifdef FEAT_CINDENT
533 can_cindent = TRUE;
534#endif
535#ifdef FEAT_FOLDING
536 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
537 * restarting. */
538 if (!p_im && did_restart_edit == 0)
539 foldOpenCursor();
540#endif
541
542 /*
543 * If 'showmode' is set, show the current (insert/replace/..) mode.
544 * A warning message for changing a readonly file is given here, before
545 * actually changing anything. It's put after the mode, if any.
546 */
547 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000548 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 i = showmode();
550
551 if (!p_im && did_restart_edit == 0)
552 change_warning(i + 1);
553
554#ifdef CURSOR_SHAPE
555 ui_cursor_shape(); /* may show different cursor shape */
556#endif
557#ifdef FEAT_DIGRAPHS
558 do_digraph(-1); /* clear digraphs */
559#endif
560
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000561 /*
562 * Get the current length of the redo buffer, those characters have to be
563 * skipped if we want to get to the inserted characters.
564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 ptr = get_inserted();
566 if (ptr == NULL)
567 new_insert_skip = 0;
568 else
569 {
570 new_insert_skip = (int)STRLEN(ptr);
571 vim_free(ptr);
572 }
573
574 old_indent = 0;
575
576 /*
577 * Main loop in Insert mode: repeat until Insert mode is left.
578 */
579 for (;;)
580 {
581#ifdef FEAT_RIGHTLEFT
582 if (!revins_legal)
583 revins_scol = -1; /* reset on illegal motions */
584 else
585 revins_legal = 0;
586#endif
587 if (arrow_used) /* don't repeat insert when arrow key used */
588 count = 0;
589
590 if (stop_insert_mode)
591 {
592 /* ":stopinsert" used or 'insertmode' reset */
593 count = 0;
594 goto doESCkey;
595 }
596
597 /* set curwin->w_curswant for next K_DOWN or K_UP */
598 if (!arrow_used)
599 curwin->w_set_curswant = TRUE;
600
601 /* If there is no typeahead may check for timestamps (e.g., for when a
602 * menu invoked a shell command). */
603 if (stuff_empty())
604 {
605 did_check_timestamps = FALSE;
606 if (need_check_timestamps)
607 check_timestamps(FALSE);
608 }
609
610 /*
611 * When emsg() was called msg_scroll will have been set.
612 */
613 msg_scroll = FALSE;
614
615#ifdef FEAT_GUI
616 /* When 'mousefocus' is set a mouse movement may have taken us to
617 * another window. "need_mouse_correct" may then be set because of an
618 * autocommand. */
619 if (need_mouse_correct)
620 gui_mouse_correct();
621#endif
622
623#ifdef FEAT_FOLDING
624 /* Open fold at the cursor line, according to 'foldopen'. */
625 if (fdo_flags & FDO_INSERT)
626 foldOpenCursor();
627 /* Close folds where the cursor isn't, according to 'foldclose' */
628 if (!char_avail())
629 foldCheckClose();
630#endif
631
632 /*
633 * If we inserted a character at the last position of the last line in
634 * the window, scroll the window one line up. This avoids an extra
635 * redraw.
636 * This is detected when the cursor column is smaller after inserting
637 * something.
638 * Don't do this when the topline changed already, it has
639 * already been adjusted (by insertchar() calling open_line())).
640 */
641 if (curbuf->b_mod_set
642 && curwin->w_p_wrap
643 && !did_backspace
644 && curwin->w_topline == old_topline
645#ifdef FEAT_DIFF
646 && curwin->w_topfill == old_topfill
647#endif
648 )
649 {
650 mincol = curwin->w_wcol;
651 validate_cursor_col();
652
653 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
654 && curwin->w_wrow == W_WINROW(curwin)
655 + curwin->w_height - 1 - p_so
656 && (curwin->w_cursor.lnum != curwin->w_topline
657#ifdef FEAT_DIFF
658 || curwin->w_topfill > 0
659#endif
660 ))
661 {
662#ifdef FEAT_DIFF
663 if (curwin->w_topfill > 0)
664 --curwin->w_topfill;
665 else
666#endif
667#ifdef FEAT_FOLDING
668 if (hasFolding(curwin->w_topline, NULL, &old_topline))
669 set_topline(curwin, old_topline + 1);
670 else
671#endif
672 set_topline(curwin, curwin->w_topline + 1);
673 }
674 }
675
676 /* May need to adjust w_topline to show the cursor. */
677 update_topline();
678
679 did_backspace = FALSE;
680
681 validate_cursor(); /* may set must_redraw */
682
683 /*
684 * Redraw the display when no characters are waiting.
685 * Also shows mode, ruler and positions cursor.
686 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000687 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
689#ifdef FEAT_SCROLLBIND
690 if (curwin->w_p_scb)
691 do_check_scrollbind(TRUE);
692#endif
693
694 update_curswant();
695 old_topline = curwin->w_topline;
696#ifdef FEAT_DIFF
697 old_topfill = curwin->w_topfill;
698#endif
699
700#ifdef USE_ON_FLY_SCROLL
701 dont_scroll = FALSE; /* allow scrolling here */
702#endif
703
704 /*
705 * Get a character for Insert mode.
706 */
707 lastc = c; /* remember previous char for CTRL-D */
708 c = safe_vgetc();
709
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000710#ifdef FEAT_AUTOCMD
711 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
712 did_cursorhold = TRUE;
713#endif
714
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715#ifdef FEAT_RIGHTLEFT
716 if (p_hkmap && KeyTyped)
717 c = hkmap(c); /* Hebrew mode mapping */
718#endif
719#ifdef FEAT_FKMAP
720 if (p_fkmap && KeyTyped)
721 c = fkmap(c); /* Farsi mode mapping */
722#endif
723
724#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000725 /*
726 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000727 * and the cursor is still in the completed word. Only when there is
728 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000729 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000730 if (compl_started
731 && pum_wanted()
732 && curwin->w_cursor.col >= compl_col
733 && (compl_shown_match == NULL
734 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000735 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000736 /* BS: Delete one character from "compl_leader". */
737 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000738 && curwin->w_cursor.col > compl_col
739 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000740 continue;
741
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000742 /* When no match was selected or it was edited. */
743 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000744 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000745 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000746 * "compl_leader". Except when at the original match and
747 * there is nothing to add, CTRL-L works like CTRL-P then. */
748 if (c == Ctrl_L
749 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
750 || STRLEN(compl_shown_match->cp_str)
751 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000752 {
753 ins_compl_addfrommatch();
754 continue;
755 }
756
Bram Moolenaardf1bdc92006-02-23 21:32:16 +0000757 /* A printable, non-white character: Add to "compl_leader". */
758 if (vim_isprintc(c) && !vim_iswhite(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000759 {
760 ins_compl_addleader(c);
761 continue;
762 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000763
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000764 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000765 * compl_enter_selects is set the Enter key does the same. */
766 if (c == Ctrl_Y || (compl_enter_selects
767 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000768 {
769 ins_compl_delete();
770 ins_compl_insert();
771 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000772 }
773 }
774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
776 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000777 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000778 if (c != K_IGNORE && ins_compl_prep(c))
779 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#endif
781
Bram Moolenaar488c6512005-08-11 20:09:58 +0000782 /* CTRL-\ CTRL-N goes to Normal mode,
783 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
784 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 if (c == Ctrl_BSL)
786 {
787 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000788 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 ++no_mapping;
790 ++allow_keys;
791 c = safe_vgetc();
792 --no_mapping;
793 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000794 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000796 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 vungetc(c);
798 c = Ctrl_BSL;
799 }
800 else if (c == Ctrl_G && p_im)
801 continue;
802 else
803 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000804 if (c == Ctrl_O)
805 {
806 ins_ctrl_o();
807 ins_at_eol = FALSE; /* cursor keeps its column */
808 nomove = TRUE;
809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 count = 0;
811 goto doESCkey;
812 }
813 }
814
815#ifdef FEAT_DIGRAPHS
816 c = do_digraph(c);
817#endif
818
819#ifdef FEAT_INS_EXPAND
820 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
821 goto docomplete;
822#endif
823 if (c == Ctrl_V || c == Ctrl_Q)
824 {
825 ins_ctrl_v();
826 c = Ctrl_V; /* pretend CTRL-V is last typed character */
827 continue;
828 }
829
830#ifdef FEAT_CINDENT
831 if (cindent_on()
832# ifdef FEAT_INS_EXPAND
833 && ctrl_x_mode == 0
834# endif
835 )
836 {
837 /* A key name preceded by a bang means this key is not to be
838 * inserted. Skip ahead to the re-indenting below.
839 * A key name preceded by a star means that indenting has to be
840 * done before inserting the key. */
841 line_is_white = inindent(0);
842 if (in_cinkeys(c, '!', line_is_white))
843 goto force_cindent;
844 if (can_cindent && in_cinkeys(c, '*', line_is_white)
845 && stop_arrow() == OK)
846 do_c_expr_indent();
847 }
848#endif
849
850#ifdef FEAT_RIGHTLEFT
851 if (curwin->w_p_rl)
852 switch (c)
853 {
854 case K_LEFT: c = K_RIGHT; break;
855 case K_S_LEFT: c = K_S_RIGHT; break;
856 case K_C_LEFT: c = K_C_RIGHT; break;
857 case K_RIGHT: c = K_LEFT; break;
858 case K_S_RIGHT: c = K_S_LEFT; break;
859 case K_C_RIGHT: c = K_C_LEFT; break;
860 }
861#endif
862
863#ifdef FEAT_VISUAL
864 /*
865 * If 'keymodel' contains "startsel", may start selection. If it
866 * does, a CTRL-O and c will be stuffed, we need to get these
867 * characters.
868 */
869 if (ins_start_select(c))
870 continue;
871#endif
872
873 /*
874 * The big switch to handle a character in insert mode.
875 */
876 switch (c)
877 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000878 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (echeck_abbr(ESC + ABBR_OFF))
880 break;
881 /*FALLTHROUGH*/
882
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000883 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef FEAT_CMDWIN
885 if (c == Ctrl_C && cmdwin_type != 0)
886 {
887 /* Close the cmdline window. */
888 cmdwin_result = K_IGNORE;
889 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000890 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 goto doESCkey;
892 }
893#endif
894
895#ifdef UNIX
896do_intr:
897#endif
898 /* when 'insertmode' set, and not halfway a mapping, don't leave
899 * Insert mode */
900 if (goto_im())
901 {
902 if (got_int)
903 {
904 (void)vgetc(); /* flush all buffers */
905 got_int = FALSE;
906 }
907 else
908 vim_beep();
909 break;
910 }
911doESCkey:
912 /*
913 * This is the ONLY return from edit()!
914 */
915 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
916 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000917 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 o_lnum = curwin->w_cursor.lnum;
919
Bram Moolenaar488c6512005-08-11 20:09:58 +0000920 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000921 {
922#ifdef FEAT_AUTOCMD
923 if (cmdchar != 'r' && cmdchar != 'v')
924 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
925 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000926 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 continue;
931
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000932 case Ctrl_Z: /* suspend when 'insertmode' set */
933 if (!p_im)
934 goto normalchar; /* insert CTRL-Z as normal char */
935 stuffReadbuff((char_u *)":st\r");
936 c = Ctrl_O;
937 /*FALLTHROUGH*/
938
939 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000940#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000941 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000942 goto docomplete;
943#endif
944 if (echeck_abbr(Ctrl_O + ABBR_OFF))
945 break;
946 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000947
948#ifdef FEAT_VIRTUALEDIT
949 /* don't move the cursor left when 'virtualedit' has "onemore". */
950 if (ve_flags & VE_ONEMORE)
951 {
952 ins_at_eol = FALSE;
953 nomove = TRUE;
954 }
955#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000956 count = 0;
957 goto doESCkey;
958
Bram Moolenaar572cb562005-08-05 21:35:02 +0000959 case K_INS: /* toggle insert/replace mode */
960 case K_KINS:
961 ins_insert(replaceState);
962 break;
963
964 case K_SELECT: /* end of Select mode mapping - ignore */
965 break;
966
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000967#ifdef FEAT_SNIFF
968 case K_SNIFF: /* Sniff command received */
969 stuffcharReadbuff(K_SNIFF);
970 goto doESCkey;
971#endif
972
973 case K_HELP: /* Help key works like <ESC> <Help> */
974 case K_F1:
975 case K_XF1:
976 stuffcharReadbuff(K_HELP);
977 if (p_im)
978 need_start_insertmode = TRUE;
979 goto doESCkey;
980
981#ifdef FEAT_NETBEANS_INTG
982 case K_F21: /* NetBeans command */
983 ++no_mapping; /* don't map the next key hits */
984 i = safe_vgetc();
985 --no_mapping;
986 netbeans_keycommand(i);
987 break;
988#endif
989
990 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 case NUL:
992 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000993 /* For ^@ the trailing ESC will end the insert, unless there is an
994 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
996 && c != Ctrl_A && !p_im)
997 goto doESCkey; /* quit insert mode */
998 inserted_space = FALSE;
999 break;
1000
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001001 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 ins_reg();
1003 auto_format(FALSE, TRUE);
1004 inserted_space = FALSE;
1005 break;
1006
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001007 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 ins_ctrl_g();
1009 break;
1010
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001011 case Ctrl_HAT: /* switch input mode and/or langmap */
1012 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 break;
1014
1015#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001016 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 if (!p_ari)
1018 goto normalchar;
1019 ins_ctrl_();
1020 break;
1021#endif
1022
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001023 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1025 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1026 goto docomplete;
1027#endif
1028 /* FALLTHROUGH */
1029
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001030 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031# ifdef FEAT_INS_EXPAND
1032 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1033 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001034 if (has_compl_option(FALSE))
1035 goto docomplete;
1036 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 }
1038# endif
1039 ins_shift(c, lastc);
1040 auto_format(FALSE, TRUE);
1041 inserted_space = FALSE;
1042 break;
1043
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001044 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 case K_KDEL:
1046 ins_del();
1047 auto_format(FALSE, TRUE);
1048 break;
1049
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001050 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 case Ctrl_H:
1052 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1053 auto_format(FALSE, TRUE);
1054 break;
1055
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001056 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1058 auto_format(FALSE, TRUE);
1059 break;
1060
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001062# ifdef FEAT_COMPL_FUNC
1063 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001064 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001065 goto docomplete;
1066# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1068 auto_format(FALSE, TRUE);
1069 inserted_space = FALSE;
1070 break;
1071
1072#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001073 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 case K_LEFTMOUSE_NM:
1075 case K_LEFTDRAG:
1076 case K_LEFTRELEASE:
1077 case K_LEFTRELEASE_NM:
1078 case K_MIDDLEMOUSE:
1079 case K_MIDDLEDRAG:
1080 case K_MIDDLERELEASE:
1081 case K_RIGHTMOUSE:
1082 case K_RIGHTDRAG:
1083 case K_RIGHTRELEASE:
1084 case K_X1MOUSE:
1085 case K_X1DRAG:
1086 case K_X1RELEASE:
1087 case K_X2MOUSE:
1088 case K_X2DRAG:
1089 case K_X2RELEASE:
1090 ins_mouse(c);
1091 break;
1092
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001093 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 ins_mousescroll(FALSE);
1095 break;
1096
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001097 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 ins_mousescroll(TRUE);
1099 break;
1100#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001101#ifdef FEAT_GUI_TABLINE
1102 case K_TABLINE:
1103 case K_TABMENU:
1104 ins_tabline(c);
1105 break;
1106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 break;
1110
Bram Moolenaar754b5602006-02-09 23:53:20 +00001111#ifdef FEAT_AUTOCMD
1112 case K_CURSORHOLD: /* Didn't type something for a while. */
1113 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1114 did_cursorhold = TRUE;
1115 break;
1116#endif
1117
Bram Moolenaar4770d092006-01-12 23:22:24 +00001118#ifdef FEAT_GUI_W32
1119 /* On Win32 ignore <M-F4>, we get it when closing the window was
1120 * cancelled. */
1121 case K_F4:
1122 if (mod_mask != MOD_MASK_ALT)
1123 goto normalchar;
1124 break;
1125#endif
1126
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127#ifdef FEAT_GUI
1128 case K_VER_SCROLLBAR:
1129 ins_scroll();
1130 break;
1131
1132 case K_HOR_SCROLLBAR:
1133 ins_horscroll();
1134 break;
1135#endif
1136
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001137 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 case K_S_HOME:
1140 case K_C_HOME:
1141 ins_home(c);
1142 break;
1143
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001144 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 case K_S_END:
1147 case K_C_END:
1148 ins_end(c);
1149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001152 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1153 ins_s_left();
1154 else
1155 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 break;
1157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 case K_C_LEFT:
1160 ins_s_left();
1161 break;
1162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001164 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1165 ins_s_right();
1166 else
1167 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 break;
1169
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001170 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 case K_C_RIGHT:
1172 ins_s_right();
1173 break;
1174
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001175 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001176#ifdef FEAT_INS_EXPAND
1177 if (pum_visible())
1178 goto docomplete;
1179#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001180 if (mod_mask & MOD_MASK_SHIFT)
1181 ins_pageup();
1182 else
1183 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 break;
1185
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001186 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 case K_PAGEUP:
1188 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001189#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001190 if (pum_visible())
1191 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 ins_pageup();
1194 break;
1195
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001196 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001197#ifdef FEAT_INS_EXPAND
1198 if (pum_visible())
1199 goto docomplete;
1200#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001201 if (mod_mask & MOD_MASK_SHIFT)
1202 ins_pagedown();
1203 else
1204 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 break;
1206
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001207 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 case K_PAGEDOWN:
1209 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001210#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001211 if (pum_visible())
1212 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 ins_pagedown();
1215 break;
1216
1217#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001218 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 ins_drop();
1220 break;
1221#endif
1222
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001223 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 c = TAB;
1225 /* FALLTHROUGH */
1226
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001227 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1229 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1230 goto docomplete;
1231#endif
1232 inserted_space = FALSE;
1233 if (ins_tab())
1234 goto normalchar; /* insert TAB as a normal char */
1235 auto_format(FALSE, TRUE);
1236 break;
1237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001238 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 c = CAR;
1240 /* FALLTHROUGH */
1241 case CAR:
1242 case NL:
1243#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1244 /* In a quickfix window a <CR> jumps to the error under the
1245 * cursor. */
1246 if (bt_quickfix(curbuf) && c == CAR)
1247 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001248 if (curwin->w_llist_ref == NULL) /* quickfix window */
1249 do_cmdline_cmd((char_u *)".cc");
1250 else /* location list window */
1251 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 break;
1253 }
1254#endif
1255#ifdef FEAT_CMDWIN
1256 if (cmdwin_type != 0)
1257 {
1258 /* Execute the command in the cmdline window. */
1259 cmdwin_result = CAR;
1260 goto doESCkey;
1261 }
1262#endif
1263 if (ins_eol(c) && !p_im)
1264 goto doESCkey; /* out of memory */
1265 auto_format(FALSE, FALSE);
1266 inserted_space = FALSE;
1267 break;
1268
1269#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001270 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271# ifdef FEAT_INS_EXPAND
1272 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1273 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001274 if (has_compl_option(TRUE))
1275 goto docomplete;
1276 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 }
1278# endif
1279# ifdef FEAT_DIGRAPHS
1280 c = ins_digraph();
1281 if (c == NUL)
1282 break;
1283# endif
1284 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286
1287#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001288 case Ctrl_X: /* Enter CTRL-X mode */
1289 ins_ctrl_x();
1290 break;
1291
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001292 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 if (ctrl_x_mode != CTRL_X_TAGS)
1294 goto normalchar;
1295 goto docomplete;
1296
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001297 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 if (ctrl_x_mode != CTRL_X_FILES)
1299 goto normalchar;
1300 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001301
1302 case 's': /* Spelling completion after ^X */
1303 case Ctrl_S:
1304 if (ctrl_x_mode != CTRL_X_SPELL)
1305 goto normalchar;
1306 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307#endif
1308
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001309 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310#ifdef FEAT_INS_EXPAND
1311 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1312#endif
1313 {
1314 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1315 if (p_im)
1316 {
1317 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1318 break;
1319 goto doESCkey;
1320 }
1321 goto normalchar;
1322 }
1323#ifdef FEAT_INS_EXPAND
1324 /* FALLTHROUGH */
1325
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001326 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 case Ctrl_N:
1328 /* if 'complete' is empty then plain ^P is no longer special,
1329 * but it is under other ^X modes */
1330 if (*curbuf->b_p_cpt == NUL
1331 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001332 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 goto normalchar;
1334
1335docomplete:
1336 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001337 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 break;
1339#endif /* FEAT_INS_EXPAND */
1340
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001341 case Ctrl_Y: /* copy from previous line or scroll down */
1342 case Ctrl_E: /* copy from next line or scroll up */
1343 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 break;
1345
1346 default:
1347#ifdef UNIX
1348 if (c == intr_char) /* special interrupt char */
1349 goto do_intr;
1350#endif
1351
1352 /*
1353 * Insert a nomal character.
1354 */
1355normalchar:
1356#ifdef FEAT_SMARTINDENT
1357 /* Try to perform smart-indenting. */
1358 ins_try_si(c);
1359#endif
1360
1361 if (c == ' ')
1362 {
1363 inserted_space = TRUE;
1364#ifdef FEAT_CINDENT
1365 if (inindent(0))
1366 can_cindent = FALSE;
1367#endif
1368 if (Insstart_blank_vcol == MAXCOL
1369 && curwin->w_cursor.lnum == Insstart.lnum)
1370 Insstart_blank_vcol = get_nolist_virtcol();
1371 }
1372
1373 if (vim_iswordc(c) || !echeck_abbr(
1374#ifdef FEAT_MBYTE
1375 /* Add ABBR_OFF for characters above 0x100, this is
1376 * what check_abbr() expects. */
1377 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1378#endif
1379 c))
1380 {
1381 insert_special(c, FALSE, FALSE);
1382#ifdef FEAT_RIGHTLEFT
1383 revins_legal++;
1384 revins_chars++;
1385#endif
1386 }
1387
1388 auto_format(FALSE, TRUE);
1389
1390#ifdef FEAT_FOLDING
1391 /* When inserting a character the cursor line must never be in a
1392 * closed fold. */
1393 foldOpenCursor();
1394#endif
1395 break;
1396 } /* end of switch (c) */
1397
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001398#ifdef FEAT_AUTOCMD
1399 /* If typed something may trigger CursorHoldI again. */
1400 if (c != K_CURSORHOLD)
1401 did_cursorhold = FALSE;
1402#endif
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 /* If the cursor was moved we didn't just insert a space */
1405 if (arrow_used)
1406 inserted_space = FALSE;
1407
1408#ifdef FEAT_CINDENT
1409 if (can_cindent && cindent_on()
1410# ifdef FEAT_INS_EXPAND
1411 && ctrl_x_mode == 0
1412# endif
1413 )
1414 {
1415force_cindent:
1416 /*
1417 * Indent now if a key was typed that is in 'cinkeys'.
1418 */
1419 if (in_cinkeys(c, ' ', line_is_white))
1420 {
1421 if (stop_arrow() == OK)
1422 /* re-indent the current line */
1423 do_c_expr_indent();
1424 }
1425 }
1426#endif /* FEAT_CINDENT */
1427
1428 } /* for (;;) */
1429 /* NOTREACHED */
1430}
1431
1432/*
1433 * Redraw for Insert mode.
1434 * This is postponed until getting the next character to make '$' in the 'cpo'
1435 * option work correctly.
1436 * Only redraw when there are no characters available. This speeds up
1437 * inserting sequences of characters (e.g., for CTRL-R).
1438 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001439/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001441ins_redraw(ready)
1442 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443{
1444 if (!char_avail())
1445 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001446#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001447 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1448 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001449 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001450 && !equalpos(last_cursormoved, curwin->w_cursor)
1451# ifdef FEAT_INS_EXPAND
1452 && !pum_visible()
1453# endif
1454 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001455 {
1456 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1457 last_cursormoved = curwin->w_cursor;
1458 }
1459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 if (must_redraw)
1461 update_screen(0);
1462 else if (clear_cmdline || redraw_cmdline)
1463 showmode(); /* clear cmdline and show mode */
1464 showruler(FALSE);
1465 setcursor();
1466 emsg_on_display = FALSE; /* may remove error message now */
1467 }
1468}
1469
1470/*
1471 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1472 */
1473 static void
1474ins_ctrl_v()
1475{
1476 int c;
1477
1478 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001479 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480
1481 if (redrawing() && !char_avail())
1482 edit_putchar('^', TRUE);
1483 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1484
1485#ifdef FEAT_CMDL_INFO
1486 add_to_showcmd_c(Ctrl_V);
1487#endif
1488
1489 c = get_literal();
1490#ifdef FEAT_CMDL_INFO
1491 clear_showcmd();
1492#endif
1493 insert_special(c, FALSE, TRUE);
1494#ifdef FEAT_RIGHTLEFT
1495 revins_chars++;
1496 revins_legal++;
1497#endif
1498}
1499
1500/*
1501 * Put a character directly onto the screen. It's not stored in a buffer.
1502 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1503 */
1504static int pc_status;
1505#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1506#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1507#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1508#define PC_STATUS_SET 3 /* pc_bytes was filled */
1509#ifdef FEAT_MBYTE
1510static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1511#else
1512static char_u pc_bytes[2]; /* saved bytes */
1513#endif
1514static int pc_attr;
1515static int pc_row;
1516static int pc_col;
1517
1518 void
1519edit_putchar(c, highlight)
1520 int c;
1521 int highlight;
1522{
1523 int attr;
1524
1525 if (ScreenLines != NULL)
1526 {
1527 update_topline(); /* just in case w_topline isn't valid */
1528 validate_cursor();
1529 if (highlight)
1530 attr = hl_attr(HLF_8);
1531 else
1532 attr = 0;
1533 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1534 pc_col = W_WINCOL(curwin);
1535#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1536 pc_status = PC_STATUS_UNSET;
1537#endif
1538#ifdef FEAT_RIGHTLEFT
1539 if (curwin->w_p_rl)
1540 {
1541 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1542# ifdef FEAT_MBYTE
1543 if (has_mbyte)
1544 {
1545 int fix_col = mb_fix_col(pc_col, pc_row);
1546
1547 if (fix_col != pc_col)
1548 {
1549 screen_putchar(' ', pc_row, fix_col, attr);
1550 --curwin->w_wcol;
1551 pc_status = PC_STATUS_RIGHT;
1552 }
1553 }
1554# endif
1555 }
1556 else
1557#endif
1558 {
1559 pc_col += curwin->w_wcol;
1560#ifdef FEAT_MBYTE
1561 if (mb_lefthalve(pc_row, pc_col))
1562 pc_status = PC_STATUS_LEFT;
1563#endif
1564 }
1565
1566 /* save the character to be able to put it back */
1567#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1568 if (pc_status == PC_STATUS_UNSET)
1569#endif
1570 {
1571 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1572 pc_status = PC_STATUS_SET;
1573 }
1574 screen_putchar(c, pc_row, pc_col, attr);
1575 }
1576}
1577
1578/*
1579 * Undo the previous edit_putchar().
1580 */
1581 void
1582edit_unputchar()
1583{
1584 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1585 {
1586#if defined(FEAT_MBYTE)
1587 if (pc_status == PC_STATUS_RIGHT)
1588 ++curwin->w_wcol;
1589 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1590 redrawWinline(curwin->w_cursor.lnum, FALSE);
1591 else
1592#endif
1593 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1594 }
1595}
1596
1597/*
1598 * Called when p_dollar is set: display a '$' at the end of the changed text
1599 * Only works when cursor is in the line that changes.
1600 */
1601 void
1602display_dollar(col)
1603 colnr_T col;
1604{
1605 colnr_T save_col;
1606
1607 if (!redrawing())
1608 return;
1609
1610 cursor_off();
1611 save_col = curwin->w_cursor.col;
1612 curwin->w_cursor.col = col;
1613#ifdef FEAT_MBYTE
1614 if (has_mbyte)
1615 {
1616 char_u *p;
1617
1618 /* If on the last byte of a multi-byte move to the first byte. */
1619 p = ml_get_curline();
1620 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1621 }
1622#endif
1623 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1624 if (curwin->w_wcol < W_WIDTH(curwin))
1625 {
1626 edit_putchar('$', FALSE);
1627 dollar_vcol = curwin->w_virtcol;
1628 }
1629 curwin->w_cursor.col = save_col;
1630}
1631
1632/*
1633 * Call this function before moving the cursor from the normal insert position
1634 * in insert mode.
1635 */
1636 static void
1637undisplay_dollar()
1638{
1639 if (dollar_vcol)
1640 {
1641 dollar_vcol = 0;
1642 redrawWinline(curwin->w_cursor.lnum, FALSE);
1643 }
1644}
1645
1646/*
1647 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1648 * Keep the cursor on the same character.
1649 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1650 * type == INDENT_DEC decrease indent (for CTRL-D)
1651 * type == INDENT_SET set indent to "amount"
1652 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1653 */
1654 void
1655change_indent(type, amount, round, replaced)
1656 int type;
1657 int amount;
1658 int round;
1659 int replaced; /* replaced character, put on replace stack */
1660{
1661 int vcol;
1662 int last_vcol;
1663 int insstart_less; /* reduction for Insstart.col */
1664 int new_cursor_col;
1665 int i;
1666 char_u *ptr;
1667 int save_p_list;
1668 int start_col;
1669 colnr_T vc;
1670#ifdef FEAT_VREPLACE
1671 colnr_T orig_col = 0; /* init for GCC */
1672 char_u *new_line, *orig_line = NULL; /* init for GCC */
1673
1674 /* VREPLACE mode needs to know what the line was like before changing */
1675 if (State & VREPLACE_FLAG)
1676 {
1677 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1678 orig_col = curwin->w_cursor.col;
1679 }
1680#endif
1681
1682 /* for the following tricks we don't want list mode */
1683 save_p_list = curwin->w_p_list;
1684 curwin->w_p_list = FALSE;
1685 vc = getvcol_nolist(&curwin->w_cursor);
1686 vcol = vc;
1687
1688 /*
1689 * For Replace mode we need to fix the replace stack later, which is only
1690 * possible when the cursor is in the indent. Remember the number of
1691 * characters before the cursor if it's possible.
1692 */
1693 start_col = curwin->w_cursor.col;
1694
1695 /* determine offset from first non-blank */
1696 new_cursor_col = curwin->w_cursor.col;
1697 beginline(BL_WHITE);
1698 new_cursor_col -= curwin->w_cursor.col;
1699
1700 insstart_less = curwin->w_cursor.col;
1701
1702 /*
1703 * If the cursor is in the indent, compute how many screen columns the
1704 * cursor is to the left of the first non-blank.
1705 */
1706 if (new_cursor_col < 0)
1707 vcol = get_indent() - vcol;
1708
1709 if (new_cursor_col > 0) /* can't fix replace stack */
1710 start_col = -1;
1711
1712 /*
1713 * Set the new indent. The cursor will be put on the first non-blank.
1714 */
1715 if (type == INDENT_SET)
1716 (void)set_indent(amount, SIN_CHANGED);
1717 else
1718 {
1719#ifdef FEAT_VREPLACE
1720 int save_State = State;
1721
1722 /* Avoid being called recursively. */
1723 if (State & VREPLACE_FLAG)
1724 State = INSERT;
1725#endif
1726 shift_line(type == INDENT_DEC, round, 1);
1727#ifdef FEAT_VREPLACE
1728 State = save_State;
1729#endif
1730 }
1731 insstart_less -= curwin->w_cursor.col;
1732
1733 /*
1734 * Try to put cursor on same character.
1735 * If the cursor is at or after the first non-blank in the line,
1736 * compute the cursor column relative to the column of the first
1737 * non-blank character.
1738 * If we are not in insert mode, leave the cursor on the first non-blank.
1739 * If the cursor is before the first non-blank, position it relative
1740 * to the first non-blank, counted in screen columns.
1741 */
1742 if (new_cursor_col >= 0)
1743 {
1744 /*
1745 * When changing the indent while the cursor is touching it, reset
1746 * Insstart_col to 0.
1747 */
1748 if (new_cursor_col == 0)
1749 insstart_less = MAXCOL;
1750 new_cursor_col += curwin->w_cursor.col;
1751 }
1752 else if (!(State & INSERT))
1753 new_cursor_col = curwin->w_cursor.col;
1754 else
1755 {
1756 /*
1757 * Compute the screen column where the cursor should be.
1758 */
1759 vcol = get_indent() - vcol;
1760 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1761
1762 /*
1763 * Advance the cursor until we reach the right screen column.
1764 */
1765 vcol = last_vcol = 0;
1766 new_cursor_col = -1;
1767 ptr = ml_get_curline();
1768 while (vcol <= (int)curwin->w_virtcol)
1769 {
1770 last_vcol = vcol;
1771#ifdef FEAT_MBYTE
1772 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001773 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 else
1775#endif
1776 ++new_cursor_col;
1777 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1778 }
1779 vcol = last_vcol;
1780
1781 /*
1782 * May need to insert spaces to be able to position the cursor on
1783 * the right screen column.
1784 */
1785 if (vcol != (int)curwin->w_virtcol)
1786 {
1787 curwin->w_cursor.col = new_cursor_col;
1788 i = (int)curwin->w_virtcol - vcol;
1789 ptr = alloc(i + 1);
1790 if (ptr != NULL)
1791 {
1792 new_cursor_col += i;
1793 ptr[i] = NUL;
1794 while (--i >= 0)
1795 ptr[i] = ' ';
1796 ins_str(ptr);
1797 vim_free(ptr);
1798 }
1799 }
1800
1801 /*
1802 * When changing the indent while the cursor is in it, reset
1803 * Insstart_col to 0.
1804 */
1805 insstart_less = MAXCOL;
1806 }
1807
1808 curwin->w_p_list = save_p_list;
1809
1810 if (new_cursor_col <= 0)
1811 curwin->w_cursor.col = 0;
1812 else
1813 curwin->w_cursor.col = new_cursor_col;
1814 curwin->w_set_curswant = TRUE;
1815 changed_cline_bef_curs();
1816
1817 /*
1818 * May have to adjust the start of the insert.
1819 */
1820 if (State & INSERT)
1821 {
1822 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1823 {
1824 if ((int)Insstart.col <= insstart_less)
1825 Insstart.col = 0;
1826 else
1827 Insstart.col -= insstart_less;
1828 }
1829 if ((int)ai_col <= insstart_less)
1830 ai_col = 0;
1831 else
1832 ai_col -= insstart_less;
1833 }
1834
1835 /*
1836 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1837 * If the number of characters before the cursor decreased, need to pop a
1838 * few characters from the replace stack.
1839 * If the number of characters before the cursor increased, need to push a
1840 * few NULs onto the replace stack.
1841 */
1842 if (REPLACE_NORMAL(State) && start_col >= 0)
1843 {
1844 while (start_col > (int)curwin->w_cursor.col)
1845 {
1846 replace_join(0); /* remove a NUL from the replace stack */
1847 --start_col;
1848 }
1849 while (start_col < (int)curwin->w_cursor.col || replaced)
1850 {
1851 replace_push(NUL);
1852 if (replaced)
1853 {
1854 replace_push(replaced);
1855 replaced = NUL;
1856 }
1857 ++start_col;
1858 }
1859 }
1860
1861#ifdef FEAT_VREPLACE
1862 /*
1863 * For VREPLACE mode, we also have to fix the replace stack. In this case
1864 * it is always possible because we backspace over the whole line and then
1865 * put it back again the way we wanted it.
1866 */
1867 if (State & VREPLACE_FLAG)
1868 {
1869 /* If orig_line didn't allocate, just return. At least we did the job,
1870 * even if you can't backspace. */
1871 if (orig_line == NULL)
1872 return;
1873
1874 /* Save new line */
1875 new_line = vim_strsave(ml_get_curline());
1876 if (new_line == NULL)
1877 return;
1878
1879 /* We only put back the new line up to the cursor */
1880 new_line[curwin->w_cursor.col] = NUL;
1881
1882 /* Put back original line */
1883 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1884 curwin->w_cursor.col = orig_col;
1885
1886 /* Backspace from cursor to start of line */
1887 backspace_until_column(0);
1888
1889 /* Insert new stuff into line again */
1890 ins_bytes(new_line);
1891
1892 vim_free(new_line);
1893 }
1894#endif
1895}
1896
1897/*
1898 * Truncate the space at the end of a line. This is to be used only in an
1899 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1900 * modes.
1901 */
1902 void
1903truncate_spaces(line)
1904 char_u *line;
1905{
1906 int i;
1907
1908 /* find start of trailing white space */
1909 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1910 {
1911 if (State & REPLACE_FLAG)
1912 replace_join(0); /* remove a NUL from the replace stack */
1913 }
1914 line[i + 1] = NUL;
1915}
1916
1917#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1918 || defined(FEAT_COMMENTS) || defined(PROTO)
1919/*
1920 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1921 * modes correctly. May also be used when not in insert mode at all.
1922 */
1923 void
1924backspace_until_column(col)
1925 int col;
1926{
1927 while ((int)curwin->w_cursor.col > col)
1928 {
1929 curwin->w_cursor.col--;
1930 if (State & REPLACE_FLAG)
1931 replace_do_bs();
1932 else
1933 (void)del_char(FALSE);
1934 }
1935}
1936#endif
1937
1938#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1939/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001940 * CTRL-X pressed in Insert mode.
1941 */
1942 static void
1943ins_ctrl_x()
1944{
1945 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1946 * CTRL-V works like CTRL-N */
1947 if (ctrl_x_mode != CTRL_X_CMDLINE)
1948 {
1949 /* if the next ^X<> won't ADD nothing, then reset
1950 * compl_cont_status */
1951 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001952 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001953 else
1954 compl_cont_status = 0;
1955 /* We're not sure which CTRL-X mode it will be yet */
1956 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1957 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1958 edit_submode_pre = NULL;
1959 showmode();
1960 }
1961}
1962
1963/*
1964 * Return TRUE if the 'dict' or 'tsr' option can be used.
1965 */
1966 static int
1967has_compl_option(dict_opt)
1968 int dict_opt;
1969{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001970 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00001971# ifdef FEAT_SPELL
1972 && !curwin->w_p_spell
1973# endif
1974 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001975 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1976 {
1977 ctrl_x_mode = 0;
1978 edit_submode = NULL;
1979 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1980 : (char_u *)_("'thesaurus' option is empty"),
1981 hl_attr(HLF_E));
1982 if (emsg_silent == 0)
1983 {
1984 vim_beep();
1985 setcursor();
1986 out_flush();
1987 ui_delay(2000L, FALSE);
1988 }
1989 return FALSE;
1990 }
1991 return TRUE;
1992}
1993
1994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1996 * This depends on the current mode.
1997 */
1998 int
1999vim_is_ctrl_x_key(c)
2000 int c;
2001{
2002 /* Always allow ^R - let it's results then be checked */
2003 if (c == Ctrl_R)
2004 return TRUE;
2005
Bram Moolenaare3226be2005-12-18 22:10:00 +00002006 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002007 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002008 return TRUE;
2009
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 switch (ctrl_x_mode)
2011 {
2012 case 0: /* Not in any CTRL-X mode */
2013 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2014 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002015 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2017 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2018 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002019 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2020 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 case CTRL_X_SCROLL:
2022 return (c == Ctrl_Y || c == Ctrl_E);
2023 case CTRL_X_WHOLE_LINE:
2024 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2025 case CTRL_X_FILES:
2026 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2027 case CTRL_X_DICTIONARY:
2028 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2029 case CTRL_X_THESAURUS:
2030 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2031 case CTRL_X_TAGS:
2032 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2033#ifdef FEAT_FIND_ID
2034 case CTRL_X_PATH_PATTERNS:
2035 return (c == Ctrl_P || c == Ctrl_N);
2036 case CTRL_X_PATH_DEFINES:
2037 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2038#endif
2039 case CTRL_X_CMDLINE:
2040 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2041 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002042#ifdef FEAT_COMPL_FUNC
2043 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002044 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002045 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002046 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002047#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002048 case CTRL_X_SPELL:
2049 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 }
2051 EMSG(_(e_internal));
2052 return FALSE;
2053}
2054
2055/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002056 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002058 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 * the rest of the word to be in -- webb
2060 */
2061 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002062ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 char_u *str;
2064 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002065 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 char_u *fname;
2067 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002068 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002070 char_u *p;
2071 int i, c;
2072 int actual_len; /* Take multi-byte characters */
2073 int actual_compl_length; /* into account. */
2074 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 int has_lower = FALSE;
2076 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
Bram Moolenaara2993e12007-08-12 14:38:46 +00002078 if (p_ic && curbuf->b_p_inf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002080 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081
Bram Moolenaara2993e12007-08-12 14:38:46 +00002082 /* Find actual length of completion. */
2083#ifdef FEAT_MBYTE
2084 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002086 p = str;
2087 actual_len = 0;
2088 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002090 mb_ptr_adv(p);
2091 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
2093 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002094 else
2095#endif
2096 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097
Bram Moolenaara2993e12007-08-12 14:38:46 +00002098 /* Find actual length of original text. */
2099#ifdef FEAT_MBYTE
2100 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002102 p = compl_orig_text;
2103 actual_compl_length = 0;
2104 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002106 mb_ptr_adv(p);
2107 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 }
2109 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002110 else
2111#endif
2112 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
Bram Moolenaara2993e12007-08-12 14:38:46 +00002114 /* Allocate wide character array for the completion and fill it. */
2115 wca = (int *)alloc(actual_len * sizeof(int));
2116 if (wca != NULL)
2117 {
2118 p = str;
2119 for (i = 0; i < actual_len; ++i)
2120#ifdef FEAT_MBYTE
2121 if (has_mbyte)
2122 wca[i] = mb_ptr2char_adv(&p);
2123 else
2124#endif
2125 wca[i] = *(p++);
2126
2127 /* Rule 1: Were any chars converted to lower? */
2128 p = compl_orig_text;
2129 for (i = 0; i < actual_compl_length; ++i)
2130 {
2131#ifdef FEAT_MBYTE
2132 if (has_mbyte)
2133 c = mb_ptr2char_adv(&p);
2134 else
2135#endif
2136 c = *(p++);
2137 if (MB_ISLOWER(c))
2138 {
2139 has_lower = TRUE;
2140 if (MB_ISUPPER(wca[i]))
2141 {
2142 /* Rule 1 is satisfied. */
2143 for (i = actual_compl_length; i < actual_len; ++i)
2144 wca[i] = MB_TOLOWER(wca[i]);
2145 break;
2146 }
2147 }
2148 }
2149
2150 /*
2151 * Rule 2: No lower case, 2nd consecutive letter converted to
2152 * upper case.
2153 */
2154 if (!has_lower)
2155 {
2156 p = compl_orig_text;
2157 for (i = 0; i < actual_compl_length; ++i)
2158 {
2159#ifdef FEAT_MBYTE
2160 if (has_mbyte)
2161 c = mb_ptr2char_adv(&p);
2162 else
2163#endif
2164 c = *(p++);
2165 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2166 {
2167 /* Rule 2 is satisfied. */
2168 for (i = actual_compl_length; i < actual_len; ++i)
2169 wca[i] = MB_TOUPPER(wca[i]);
2170 break;
2171 }
2172 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2173 }
2174 }
2175
2176 /* Copy the original case of the part we typed. */
2177 p = compl_orig_text;
2178 for (i = 0; i < actual_compl_length; ++i)
2179 {
2180#ifdef FEAT_MBYTE
2181 if (has_mbyte)
2182 c = mb_ptr2char_adv(&p);
2183 else
2184#endif
2185 c = *(p++);
2186 if (MB_ISLOWER(c))
2187 wca[i] = MB_TOLOWER(wca[i]);
2188 else if (MB_ISUPPER(c))
2189 wca[i] = MB_TOUPPER(wca[i]);
2190 }
2191
2192 /*
2193 * Generate encoding specific output from wide character array.
2194 * Multi-byte characters can occupy up to five bytes more than
2195 * ASCII characters, and we also need one byte for NUL, so stay
2196 * six bytes away from the edge of IObuff.
2197 */
2198 p = IObuff;
2199 i = 0;
2200 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2201#ifdef FEAT_MBYTE
2202 if (has_mbyte)
2203 p += mb_char2bytes(wca[i++], p);
2204 else
2205#endif
2206 *(p++) = wca[i++];
2207 *p = NUL;
2208
2209 vim_free(wca);
2210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002212 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2213 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002215 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216}
2217
2218/*
2219 * Add a match to the list of matches.
2220 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002221 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002222 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002224 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002225ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 char_u *str;
2227 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002228 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002230 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002231 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002232 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002233 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002235 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002236 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 ui_breakcheck();
2239 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002240 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 if (len < 0)
2242 len = (int)STRLEN(str);
2243
2244 /*
2245 * If the same match is already present, don't add it.
2246 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002247 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002249 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 do
2251 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002252 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002253 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002254 && match->cp_str[len] == NUL)
2255 return NOTDONE;
2256 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002257 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
2259
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002260 /* Remove any popup menu before changing the list of matches. */
2261 ins_compl_del_pum();
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 /*
2264 * Allocate a new match structure.
2265 * Copy the values to the new match structure.
2266 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002267 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002269 return FAIL;
2270 match->cp_number = -1;
2271 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002272 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002273 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
2275 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002276 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002278 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002279
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002281 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2283 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002284 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002285 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002286 && compl_curr_match->cp_fname != NULL
2287 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002288 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002289 else if (fname != NULL)
2290 {
2291 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002292 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002295 match->cp_fname = NULL;
2296 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002297
2298 if (cptext != NULL)
2299 {
2300 int i;
2301
2302 for (i = 0; i < CPT_COUNT; ++i)
2303 if (cptext[i] != NULL && *cptext[i] != NUL)
2304 match->cp_text[i] = vim_strsave(cptext[i]);
2305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306
2307 /*
2308 * Link the new match structure in the list of matches.
2309 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002310 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002311 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 else if (dir == FORWARD)
2313 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002314 match->cp_next = compl_curr_match->cp_next;
2315 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
2317 else /* BACKWARD */
2318 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002319 match->cp_next = compl_curr_match;
2320 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002322 if (match->cp_next)
2323 match->cp_next->cp_prev = match;
2324 if (match->cp_prev)
2325 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002327 compl_first_match = match;
2328 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002330 /*
2331 * Find the longest common string if still doing that.
2332 */
2333 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2334 ins_compl_longest_match(match);
2335
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 return OK;
2337}
2338
2339/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002340 * Return TRUE if "str[len]" matches with match->cp_str, considering
2341 * match->cp_icase.
2342 */
2343 static int
2344ins_compl_equal(match, str, len)
2345 compl_T *match;
2346 char_u *str;
2347 int len;
2348{
2349 if (match->cp_icase)
2350 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2351 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2352}
2353
2354/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002355 * Reduce the longest common string for match "match".
2356 */
2357 static void
2358ins_compl_longest_match(match)
2359 compl_T *match;
2360{
2361 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002362 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002363 int had_match;
2364
2365 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002366 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002367 /* First match, use it as a whole. */
2368 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002369 if (compl_leader != NULL)
2370 {
2371 had_match = (curwin->w_cursor.col > compl_col);
2372 ins_compl_delete();
2373 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2374 ins_redraw(FALSE);
2375
2376 /* When the match isn't there (to avoid matching itself) remove it
2377 * again after redrawing. */
2378 if (!had_match)
2379 ins_compl_delete();
2380 compl_used_match = FALSE;
2381 }
2382 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002383 else
2384 {
2385 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002386 p = compl_leader;
2387 s = match->cp_str;
2388 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002389 {
2390#ifdef FEAT_MBYTE
2391 if (has_mbyte)
2392 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002393 c1 = mb_ptr2char(p);
2394 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002395 }
2396 else
2397#endif
2398 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002399 c1 = *p;
2400 c2 = *s;
2401 }
2402 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2403 : (c1 != c2))
2404 break;
2405#ifdef FEAT_MBYTE
2406 if (has_mbyte)
2407 {
2408 mb_ptr_adv(p);
2409 mb_ptr_adv(s);
2410 }
2411 else
2412#endif
2413 {
2414 ++p;
2415 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002416 }
2417 }
2418
2419 if (*p != NUL)
2420 {
2421 /* Leader was shortened, need to change the inserted text. */
2422 *p = NUL;
2423 had_match = (curwin->w_cursor.col > compl_col);
2424 ins_compl_delete();
2425 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2426 ins_redraw(FALSE);
2427
2428 /* When the match isn't there (to avoid matching itself) remove it
2429 * again after redrawing. */
2430 if (!had_match)
2431 ins_compl_delete();
2432 }
2433
2434 compl_used_match = FALSE;
2435 }
2436}
2437
2438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 * Add an array of matches to the list of matches.
2440 * Frees matches[].
2441 */
2442 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002443ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 int num_matches;
2445 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002446 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447{
2448 int i;
2449 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002450 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
Bram Moolenaar572cb562005-08-05 21:35:02 +00002452 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002453 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002454 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002456 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 FreeWild(num_matches, matches);
2458}
2459
2460/* Make the completion list cyclic.
2461 * Return the number of matches (excluding the original).
2462 */
2463 static int
2464ins_compl_make_cyclic()
2465{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002466 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 int count = 0;
2468
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002469 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 {
2471 /*
2472 * Find the end of the list.
2473 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002474 match = compl_first_match;
2475 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002476 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002478 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 ++count;
2480 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002481 match->cp_next = compl_first_match;
2482 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 }
2484 return count;
2485}
2486
Bram Moolenaara94bc432006-03-10 21:42:59 +00002487/*
2488 * Start completion for the complete() function.
2489 * "startcol" is where the matched text starts (1 is first column).
2490 * "list" is the list of matches.
2491 */
2492 void
2493set_completion(startcol, list)
2494 int startcol;
2495 list_T *list;
2496{
2497 /* If already doing completions stop it. */
2498 if (ctrl_x_mode != 0)
2499 ins_compl_prep(' ');
2500 ins_compl_clear();
2501
2502 if (stop_arrow() == FAIL)
2503 return;
2504
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002505 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002506 startcol = curwin->w_cursor.col;
2507 compl_col = startcol;
2508 compl_length = curwin->w_cursor.col - startcol;
2509 /* compl_pattern doesn't need to be set */
2510 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2511 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002512 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002513 return;
2514
2515 /* Handle like dictionary completion. */
2516 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2517
2518 ins_compl_add_list(list);
2519 compl_matches = ins_compl_make_cyclic();
2520 compl_started = TRUE;
2521 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002522 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002523
2524 compl_curr_match = compl_first_match;
2525 ins_complete(Ctrl_N);
2526 out_flush();
2527}
2528
2529
Bram Moolenaar9372a112005-12-06 19:59:18 +00002530/* "compl_match_array" points the currently displayed list of entries in the
2531 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002532static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002533static int compl_match_arraysize;
2534
2535/*
2536 * Update the screen and when there is any scrolling remove the popup menu.
2537 */
2538 static void
2539ins_compl_upd_pum()
2540{
2541 int h;
2542
2543 if (compl_match_array != NULL)
2544 {
2545 h = curwin->w_cline_height;
2546 update_screen(0);
2547 if (h != curwin->w_cline_height)
2548 ins_compl_del_pum();
2549 }
2550}
2551
2552/*
2553 * Remove any popup menu.
2554 */
2555 static void
2556ins_compl_del_pum()
2557{
2558 if (compl_match_array != NULL)
2559 {
2560 pum_undisplay();
2561 vim_free(compl_match_array);
2562 compl_match_array = NULL;
2563 }
2564}
2565
2566/*
2567 * Return TRUE if the popup menu should be displayed.
2568 */
2569 static int
2570pum_wanted()
2571{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002572 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002573 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002574 return FALSE;
2575
2576 /* The display looks bad on a B&W display. */
2577 if (t_colors < 8
2578#ifdef FEAT_GUI
2579 && !gui.in_use
2580#endif
2581 )
2582 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002583 return TRUE;
2584}
2585
2586/*
2587 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002588 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002589 */
2590 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002591pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002592{
2593 compl_T *compl;
2594 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002595
2596 /* Don't display the popup menu if there are no matches or there is only
2597 * one (ignoring the original text). */
2598 compl = compl_first_match;
2599 i = 0;
2600 do
2601 {
2602 if (compl == NULL
2603 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2604 break;
2605 compl = compl->cp_next;
2606 } while (compl != compl_first_match);
2607
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002608 if (strstr((char *)p_cot, "menuone") != NULL)
2609 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002610 return (i >= 2);
2611}
2612
2613/*
2614 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002615 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002616 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002617 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002618ins_compl_show_pum()
2619{
2620 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002621 compl_T *shown_compl = NULL;
2622 int did_find_shown_match = FALSE;
2623 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002624 int i;
2625 int cur = -1;
2626 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002627 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002628
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002629 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002630 return;
2631
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002632#if defined(FEAT_EVAL)
2633 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2634 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2635#endif
2636
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002637 /* Update the screen before drawing the popup menu over it. */
2638 update_screen(0);
2639
2640 if (compl_match_array == NULL)
2641 {
2642 /* Need to build the popup menu list. */
2643 compl_match_arraysize = 0;
2644 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002645 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002646 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002647 do
2648 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002649 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2650 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002651 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002652 ++compl_match_arraysize;
2653 compl = compl->cp_next;
2654 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002655 if (compl_match_arraysize == 0)
2656 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002657 compl_match_array = (pumitem_T *)alloc_clear(
2658 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002659 * compl_match_arraysize));
2660 if (compl_match_array != NULL)
2661 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002662 /* If the current match is the original text don't find the first
2663 * match after it, don't highlight anything. */
2664 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2665 shown_match_ok = TRUE;
2666
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002667 i = 0;
2668 compl = compl_first_match;
2669 do
2670 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002671 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2672 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002673 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002674 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002675 if (!shown_match_ok)
2676 {
2677 if (compl == compl_shown_match || did_find_shown_match)
2678 {
2679 /* This item is the shown match or this is the
2680 * first displayed item after the shown match. */
2681 compl_shown_match = compl;
2682 did_find_shown_match = TRUE;
2683 shown_match_ok = TRUE;
2684 }
2685 else
2686 /* Remember this displayed match for when the
2687 * shown match is just below it. */
2688 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002689 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002690 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002691
2692 if (compl->cp_text[CPT_ABBR] != NULL)
2693 compl_match_array[i].pum_text =
2694 compl->cp_text[CPT_ABBR];
2695 else
2696 compl_match_array[i].pum_text = compl->cp_str;
2697 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2698 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2699 if (compl->cp_text[CPT_MENU] != NULL)
2700 compl_match_array[i++].pum_extra =
2701 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002702 else
2703 compl_match_array[i++].pum_extra = compl->cp_fname;
2704 }
2705
2706 if (compl == compl_shown_match)
2707 {
2708 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002709
2710 /* When the original text is the shown match don't set
2711 * compl_shown_match. */
2712 if (compl->cp_flags & ORIGINAL_TEXT)
2713 shown_match_ok = TRUE;
2714
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002715 if (!shown_match_ok && shown_compl != NULL)
2716 {
2717 /* The shown match isn't displayed, set it to the
2718 * previously displayed match. */
2719 compl_shown_match = shown_compl;
2720 shown_match_ok = TRUE;
2721 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002722 }
2723 compl = compl->cp_next;
2724 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002725
2726 if (!shown_match_ok) /* no displayed match at all */
2727 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002728 }
2729 }
2730 else
2731 {
2732 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002733 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002734 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2735 || compl_match_array[i].pum_text
2736 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002737 {
2738 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002739 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002740 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002741 }
2742
2743 if (compl_match_array != NULL)
2744 {
2745 /* Compute the screen column of the start of the completed text.
2746 * Use the cursor to get all wrapping and other settings right. */
2747 col = curwin->w_cursor.col;
2748 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002749 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002750 curwin->w_cursor.col = col;
2751 }
2752}
2753
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754#define DICT_FIRST (1) /* use just first element in "dict" */
2755#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002756
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002758 * Add any identifiers that match the given pattern in the list of dictionary
2759 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 */
2761 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002762ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2763 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002765 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002766 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002768 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 char_u *ptr;
2770 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 char_u **files;
2773 int count;
2774 int i;
2775 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002776 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777
Bram Moolenaar0b238792006-03-02 22:49:12 +00002778 if (*dict == NUL)
2779 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002780#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002781 /* When 'dictionary' is empty and spell checking is enabled use
2782 * "spell". */
2783 if (!thesaurus && curwin->w_p_spell)
2784 dict = (char_u *)"spell";
2785 else
2786#endif
2787 return;
2788 }
2789
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002791 if (buf == NULL)
2792 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002793 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002794
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 /* If 'infercase' is set, don't use 'smartcase' here */
2796 save_p_scs = p_scs;
2797 if (curbuf->b_p_inf)
2798 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002799
2800 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2801 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002802 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002803 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2804 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002805 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
2806
2807 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00002808 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002809 i = (int)STRLEN(pat_esc) + 10;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002810 ptr = alloc(i);
2811 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002812 {
2813 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002814 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002815 }
2816 vim_snprintf((char *)ptr, i, "^\\s*\\zs\\V%s", pat_esc);
2817 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2818 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002819 vim_free(ptr);
2820 }
2821 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002822 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002823 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002824 if (regmatch.regprog == NULL)
2825 goto theend;
2826 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002827
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2829 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002830 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {
2832 /* copy one dictionary file name into buf */
2833 if (flags == DICT_EXACT)
2834 {
2835 count = 1;
2836 files = &dict;
2837 }
2838 else
2839 {
2840 /* Expand wildcards in the dictionary name, but do not allow
2841 * backticks (for security, the 'dict' option may have been set in
2842 * a modeline). */
2843 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002844# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002845 if (!thesaurus && STRCMP(buf, "spell") == 0)
2846 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002847 else
2848# endif
2849 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 || expand_wildcards(1, &buf, &count, &files,
2851 EW_FILE|EW_SILENT) != OK)
2852 count = 0;
2853 }
2854
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002855# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002856 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002858 /* Complete from active spelling. Skip "\<" in the pattern, we
2859 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002860 if (pat[0] == '\\' && pat[1] == '<')
2861 ptr = pat + 2;
2862 else
2863 ptr = pat;
2864 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002866 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002867# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00002868 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002869 {
2870 ins_compl_files(count, files, thesaurus, flags,
2871 &regmatch, buf, &dir);
2872 if (flags != DICT_EXACT)
2873 FreeWild(count, files);
2874 }
2875 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 break;
2877 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002878
2879theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 p_scs = save_p_scs;
2881 vim_free(regmatch.regprog);
2882 vim_free(buf);
2883}
2884
Bram Moolenaar0b238792006-03-02 22:49:12 +00002885 static void
2886ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2887 int count;
2888 char_u **files;
2889 int thesaurus;
2890 int flags;
2891 regmatch_T *regmatch;
2892 char_u *buf;
2893 int *dir;
2894{
2895 char_u *ptr;
2896 int i;
2897 FILE *fp;
2898 int add_r;
2899
2900 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2901 {
2902 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2903 if (flags != DICT_EXACT)
2904 {
2905 vim_snprintf((char *)IObuff, IOSIZE,
2906 _("Scanning dictionary: %s"), (char *)files[i]);
2907 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2908 }
2909
2910 if (fp != NULL)
2911 {
2912 /*
2913 * Read dictionary file line by line.
2914 * Check each line for a match.
2915 */
2916 while (!got_int && !compl_interrupted
2917 && !vim_fgets(buf, LSIZE, fp))
2918 {
2919 ptr = buf;
2920 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2921 {
2922 ptr = regmatch->startp[0];
2923 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2924 ptr = find_line_end(ptr);
2925 else
2926 ptr = find_word_end(ptr);
2927 add_r = ins_compl_add_infercase(regmatch->startp[0],
2928 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002929 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002930 if (thesaurus)
2931 {
2932 char_u *wstart;
2933
2934 /*
2935 * Add the other matches on the line
2936 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00002937 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00002938 while (!got_int)
2939 {
2940 /* Find start of the next word. Skip white
2941 * space and punctuation. */
2942 ptr = find_word_start(ptr);
2943 if (*ptr == NUL || *ptr == NL)
2944 break;
2945 wstart = ptr;
2946
Bram Moolenaara2993e12007-08-12 14:38:46 +00002947 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002948#ifdef FEAT_MBYTE
2949 if (has_mbyte)
2950 /* Japanese words may have characters in
2951 * different classes, only separate words
2952 * with single-byte non-word characters. */
2953 while (*ptr != NUL)
2954 {
2955 int l = (*mb_ptr2len)(ptr);
2956
2957 if (l < 2 && !vim_iswordc(*ptr))
2958 break;
2959 ptr += l;
2960 }
2961 else
2962#endif
2963 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002964
2965 /* Add the word. Skip the regexp match. */
2966 if (wstart != regmatch->startp[0])
2967 add_r = ins_compl_add_infercase(wstart,
2968 (int)(ptr - wstart),
2969 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002970 }
2971 }
2972 if (add_r == OK)
2973 /* if dir was BACKWARD then honor it just once */
2974 *dir = FORWARD;
2975 else if (add_r == FAIL)
2976 break;
2977 /* avoid expensive call to vim_regexec() when at end
2978 * of line */
2979 if (*ptr == '\n' || got_int)
2980 break;
2981 }
2982 line_breakcheck();
2983 ins_compl_check_keys(50);
2984 }
2985 fclose(fp);
2986 }
2987 }
2988}
2989
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990/*
2991 * Find the start of the next word.
2992 * Returns a pointer to the first char of the word. Also stops at a NUL.
2993 */
2994 char_u *
2995find_word_start(ptr)
2996 char_u *ptr;
2997{
2998#ifdef FEAT_MBYTE
2999 if (has_mbyte)
3000 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003001 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 else
3003#endif
3004 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3005 ++ptr;
3006 return ptr;
3007}
3008
3009/*
3010 * Find the end of the word. Assumes it starts inside a word.
3011 * Returns a pointer to just after the word.
3012 */
3013 char_u *
3014find_word_end(ptr)
3015 char_u *ptr;
3016{
3017#ifdef FEAT_MBYTE
3018 int start_class;
3019
3020 if (has_mbyte)
3021 {
3022 start_class = mb_get_class(ptr);
3023 if (start_class > 1)
3024 while (*ptr != NUL)
3025 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003026 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 if (mb_get_class(ptr) != start_class)
3028 break;
3029 }
3030 }
3031 else
3032#endif
3033 while (vim_iswordc(*ptr))
3034 ++ptr;
3035 return ptr;
3036}
3037
3038/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003039 * Find the end of the line, omitting CR and NL at the end.
3040 * Returns a pointer to just after the line.
3041 */
3042 static char_u *
3043find_line_end(ptr)
3044 char_u *ptr;
3045{
3046 char_u *s;
3047
3048 s = ptr + STRLEN(ptr);
3049 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3050 --s;
3051 return s;
3052}
3053
3054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 * Free the list of completions
3056 */
3057 static void
3058ins_compl_free()
3059{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003060 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003061 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003063 vim_free(compl_pattern);
3064 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003065 vim_free(compl_leader);
3066 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003068 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003070
3071 ins_compl_del_pum();
3072 pum_clear();
3073
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003074 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 do
3076 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003077 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003078 compl_curr_match = compl_curr_match->cp_next;
3079 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003081 if (match->cp_flags & FREE_FNAME)
3082 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003083 for (i = 0; i < CPT_COUNT; ++i)
3084 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003086 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3087 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088}
3089
3090 static void
3091ins_compl_clear()
3092{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003093 compl_cont_status = 0;
3094 compl_started = FALSE;
3095 compl_matches = 0;
3096 vim_free(compl_pattern);
3097 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003098 vim_free(compl_leader);
3099 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003101 vim_free(compl_orig_text);
3102 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003103 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104}
3105
3106/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003107 * Return TRUE when Insert completion is active.
3108 */
3109 int
3110ins_compl_active()
3111{
3112 return compl_started;
3113}
3114
3115/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003116 * Delete one character before the cursor and show the subset of the matches
3117 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003118 * Returns the character to be used, NUL if the work is done and another char
3119 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003120 */
3121 static int
3122ins_compl_bs()
3123{
3124 char_u *line;
3125 char_u *p;
3126
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003127 line = ml_get_curline();
3128 p = line + curwin->w_cursor.col;
3129 mb_ptr_back(line, p);
3130
3131 /* Stop completion when the whole word was deleted. */
3132 if ((int)(p - line) - (int)compl_col <= 0)
3133 return K_BS;
3134
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003135 /* Deleted more than what was used to find matches or didn't finish
3136 * finding all matches: need to look for matches all over again. */
3137 if (curwin->w_cursor.col <= compl_col + compl_length
3138 || compl_was_interrupted)
3139 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003140
Bram Moolenaara6557602006-02-04 22:43:20 +00003141 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003142 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003143 if (compl_leader != NULL)
3144 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003145 ins_compl_new_leader();
3146 return NUL;
3147 }
3148 return K_BS;
3149}
Bram Moolenaara6557602006-02-04 22:43:20 +00003150
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003151/*
3152 * Called after changing "compl_leader".
3153 * Show the popup menu with a different set of matches.
3154 * May also search for matches again if the previous search was interrupted.
3155 */
3156 static void
3157ins_compl_new_leader()
3158{
3159 ins_compl_del_pum();
3160 ins_compl_delete();
3161 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3162 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003163
3164 if (compl_started)
3165 ins_compl_set_original_text(compl_leader);
3166 else
3167 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003168#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003169 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003170#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003171 /*
3172 * Matches were cleared, need to search for them now. First display
3173 * the changed text before the cursor. Set "compl_restarting" to
3174 * avoid that the first match is inserted.
3175 */
3176 update_screen(0);
3177#ifdef FEAT_GUI
3178 if (gui.in_use)
3179 {
3180 /* Show the cursor after the match, not after the redrawn text. */
3181 setcursor();
3182 out_flush();
3183 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003184 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003185#endif
3186 compl_restarting = TRUE;
3187 if (ins_complete(Ctrl_N) == FAIL)
3188 compl_cont_status = 0;
3189 compl_restarting = FALSE;
3190 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003191
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003192#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003193 if (!compl_used_match)
3194 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003195 /* Go to the original text, since none of the matches is inserted. */
3196 if (compl_first_match->cp_prev != NULL
3197 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3198 compl_shown_match = compl_first_match->cp_prev;
3199 else
3200 compl_shown_match = compl_first_match;
3201 compl_curr_match = compl_shown_match;
3202 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003203 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003204#endif
3205 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003206
3207 /* Show the popup menu with a different set of matches. */
3208 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003209
3210 /* Don't let Enter select the original text when there is no popup menu. */
3211 if (compl_match_array == NULL)
3212 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003213}
3214
3215/*
3216 * Append one character to the match leader. May reduce the number of
3217 * matches.
3218 */
3219 static void
3220ins_compl_addleader(c)
3221 int c;
3222{
3223#ifdef FEAT_MBYTE
3224 int cc;
3225
3226 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3227 {
3228 char_u buf[MB_MAXBYTES + 1];
3229
3230 (*mb_char2bytes)(c, buf);
3231 buf[cc] = NUL;
3232 ins_char_bytes(buf, cc);
3233 }
3234 else
3235#endif
3236 ins_char(c);
3237
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003238 /* If we didn't complete finding matches we must search again. */
3239 if (compl_was_interrupted)
3240 ins_compl_restart();
3241
Bram Moolenaara6557602006-02-04 22:43:20 +00003242 vim_free(compl_leader);
3243 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3244 curwin->w_cursor.col - compl_col);
3245 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003246 ins_compl_new_leader();
3247}
3248
3249/*
3250 * Setup for finding completions again without leaving CTRL-X mode. Used when
3251 * BS or a key was typed while still searching for matches.
3252 */
3253 static void
3254ins_compl_restart()
3255{
3256 ins_compl_free();
3257 compl_started = FALSE;
3258 compl_matches = 0;
3259 compl_cont_status = 0;
3260 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003261}
3262
3263/*
3264 * Set the first match, the original text.
3265 */
3266 static void
3267ins_compl_set_original_text(str)
3268 char_u *str;
3269{
3270 char_u *p;
3271
3272 /* Replace the original text entry. */
3273 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3274 {
3275 p = vim_strsave(str);
3276 if (p != NULL)
3277 {
3278 vim_free(compl_first_match->cp_str);
3279 compl_first_match->cp_str = p;
3280 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003281 }
3282}
3283
3284/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003285 * Append one character to the match leader. May reduce the number of
3286 * matches.
3287 */
3288 static void
3289ins_compl_addfrommatch()
3290{
3291 char_u *p;
3292 int len = curwin->w_cursor.col - compl_col;
3293 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003294 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003295
3296 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003297 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003298 {
3299 /* When still at the original match use the first entry that matches
3300 * the leader. */
3301 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3302 {
3303 p = NULL;
3304 for (cp = compl_shown_match->cp_next; cp != NULL
3305 && cp != compl_first_match; cp = cp->cp_next)
3306 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003307 if (compl_leader == NULL
3308 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003309 (int)STRLEN(compl_leader)))
3310 {
3311 p = cp->cp_str;
3312 break;
3313 }
3314 }
3315 if (p == NULL || (int)STRLEN(p) <= len)
3316 return;
3317 }
3318 else
3319 return;
3320 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003321 p += len;
3322#ifdef FEAT_MBYTE
3323 c = mb_ptr2char(p);
3324#else
3325 c = *p;
3326#endif
3327 ins_compl_addleader(c);
3328}
3329
3330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003332 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003333 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003335 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336ins_compl_prep(c)
3337 int c;
3338{
3339 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003341 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
3343 /* Forget any previous 'special' messages if this is actually
3344 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3345 */
3346 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3347 edit_submode_extra = NULL;
3348
3349 /* Ignore end of Select mode mapping */
3350 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003351 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003353 /* Set "compl_get_longest" when finding the first matches. */
3354 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3355 || (ctrl_x_mode == 0 && !compl_started))
3356 {
3357 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3358 compl_used_match = TRUE;
3359 }
3360
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3362 {
3363 /*
3364 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3365 * it will be yet. Now we decide.
3366 */
3367 switch (c)
3368 {
3369 case Ctrl_E:
3370 case Ctrl_Y:
3371 ctrl_x_mode = CTRL_X_SCROLL;
3372 if (!(State & REPLACE_FLAG))
3373 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3374 else
3375 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3376 edit_submode_pre = NULL;
3377 showmode();
3378 break;
3379 case Ctrl_L:
3380 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3381 break;
3382 case Ctrl_F:
3383 ctrl_x_mode = CTRL_X_FILES;
3384 break;
3385 case Ctrl_K:
3386 ctrl_x_mode = CTRL_X_DICTIONARY;
3387 break;
3388 case Ctrl_R:
3389 /* Simply allow ^R to happen without affecting ^X mode */
3390 break;
3391 case Ctrl_T:
3392 ctrl_x_mode = CTRL_X_THESAURUS;
3393 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003394#ifdef FEAT_COMPL_FUNC
3395 case Ctrl_U:
3396 ctrl_x_mode = CTRL_X_FUNCTION;
3397 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003398 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003399 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003400 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003401#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003402 case 's':
3403 case Ctrl_S:
3404 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003405#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003406 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003407 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003408 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003409#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003410 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 case Ctrl_RSB:
3412 ctrl_x_mode = CTRL_X_TAGS;
3413 break;
3414#ifdef FEAT_FIND_ID
3415 case Ctrl_I:
3416 case K_S_TAB:
3417 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3418 break;
3419 case Ctrl_D:
3420 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3421 break;
3422#endif
3423 case Ctrl_V:
3424 case Ctrl_Q:
3425 ctrl_x_mode = CTRL_X_CMDLINE;
3426 break;
3427 case Ctrl_P:
3428 case Ctrl_N:
3429 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3430 * just started ^X mode, or there were enough ^X's to cancel
3431 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3432 * do normal expansion when interrupting a different mode (say
3433 * ^X^F^X^P or ^P^X^X^P, see below)
3434 * nothing changes if interrupting mode 0, (eg, the flag
3435 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003436 if (!(compl_cont_status & CONT_INTRPT))
3437 compl_cont_status |= CONT_LOCAL;
3438 else if (compl_cont_mode != 0)
3439 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 /* FALLTHROUGH */
3441 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003442 /* If we have typed at least 2 ^X's... for modes != 0, we set
3443 * compl_cont_status = 0 (eg, as if we had just started ^X
3444 * mode).
3445 * For mode 0, we set "compl_cont_mode" to an impossible
3446 * value, in both cases ^X^X can be used to restart the same
3447 * mode (avoiding ADDING mode).
3448 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3449 * 'complete' and local ^P expansions respectively.
3450 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3451 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 if (c == Ctrl_X)
3453 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003454 if (compl_cont_mode != 0)
3455 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003457 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
3459 ctrl_x_mode = 0;
3460 edit_submode = NULL;
3461 showmode();
3462 break;
3463 }
3464 }
3465 else if (ctrl_x_mode != 0)
3466 {
3467 /* We're already in CTRL-X mode, do we stay in it? */
3468 if (!vim_is_ctrl_x_key(c))
3469 {
3470 if (ctrl_x_mode == CTRL_X_SCROLL)
3471 ctrl_x_mode = 0;
3472 else
3473 ctrl_x_mode = CTRL_X_FINISHED;
3474 edit_submode = NULL;
3475 }
3476 showmode();
3477 }
3478
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003479 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 {
3481 /* Show error message from attempted keyword completion (probably
3482 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003483 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003485 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3486 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 || ctrl_x_mode == CTRL_X_FINISHED)
3488 {
3489 /* Get here when we have finished typing a sequence of ^N and
3490 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003491 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003492 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003494 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003495 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003498 * If any of the original typed text has been changed, eg when
3499 * ignorecase is set, we must add back-spaces to the redo
3500 * buffer. We add as few as necessary to delete just the part
3501 * of the original text that has changed.
3502 * When using the longest match, edited the match or used
3503 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003505 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3506 ptr = compl_curr_match->cp_str;
3507 else if (compl_leader != NULL)
3508 ptr = compl_leader;
3509 else
3510 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003511 if (compl_orig_text != NULL)
3512 {
3513 p = compl_orig_text;
3514 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3515 ++temp)
3516 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003517#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003518 if (temp > 0)
3519 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003520#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003521 for (p += temp; *p != NUL; mb_ptr_adv(p))
3522 AppendCharToRedobuff(K_BS);
3523 }
3524 if (ptr != NULL)
3525 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
3527
3528#ifdef FEAT_CINDENT
3529 want_cindent = (can_cindent && cindent_on());
3530#endif
3531 /*
3532 * When completing whole lines: fix indent for 'cindent'.
3533 * Otherwise, break line if it's too long.
3534 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003535 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
3537#ifdef FEAT_CINDENT
3538 /* re-indent the current line */
3539 if (want_cindent)
3540 {
3541 do_c_expr_indent();
3542 want_cindent = FALSE; /* don't do it again */
3543 }
3544#endif
3545 }
3546 else
3547 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003548 int prev_col = curwin->w_cursor.col;
3549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003551 if (prev_col > 0)
3552 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 if (stop_arrow() == OK)
3554 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003555 if (prev_col > 0
3556 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3557 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
3559
3560 auto_format(FALSE, TRUE);
3561
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003562 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003563 * the selection without inserting anything. When
3564 * compl_enter_selects is set the Enter key does the same. */
3565 if ((c == Ctrl_Y || (compl_enter_selects
3566 && (c == CAR || c == K_KENTER || c == NL)))
3567 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003568 retval = TRUE;
3569
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003570 /* CTRL-E means completion is Ended, go back to the typed text. */
3571 if (c == Ctrl_E)
3572 {
3573 ins_compl_delete();
3574 if (compl_leader != NULL)
3575 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3576 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003577 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003578 + curwin->w_cursor.col - compl_col);
3579 retval = TRUE;
3580 }
3581
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003583 compl_started = FALSE;
3584 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 msg_clr_cmdline(); /* necessary for "noshowmode" */
3586 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003587 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 if (edit_submode != NULL)
3589 {
3590 edit_submode = NULL;
3591 showmode();
3592 }
3593
3594#ifdef FEAT_CINDENT
3595 /*
3596 * Indent now if a key was typed that is in 'cinkeys'.
3597 */
3598 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3599 do_c_expr_indent();
3600#endif
3601 }
3602 }
3603
3604 /* reset continue_* if we left expansion-mode, if we stay they'll be
3605 * (re)set properly in ins_complete() */
3606 if (!vim_is_ctrl_x_key(c))
3607 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003608 compl_cont_status = 0;
3609 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003611
3612 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613}
3614
3615/*
3616 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3617 * (depending on flag) starting from buf and looking for a non-scanned
3618 * buffer (other than curbuf). curbuf is special, if it is called with
3619 * buf=curbuf then it has to be the first call for a given flag/expansion.
3620 *
3621 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3622 */
3623 static buf_T *
3624ins_compl_next_buf(buf, flag)
3625 buf_T *buf;
3626 int flag;
3627{
3628#ifdef FEAT_WINDOWS
3629 static win_T *wp;
3630#endif
3631
3632 if (flag == 'w') /* just windows */
3633 {
3634#ifdef FEAT_WINDOWS
3635 if (buf == curbuf) /* first call for this flag/expansion */
3636 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003637 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 && wp->w_buffer->b_scanned)
3639 ;
3640 buf = wp->w_buffer;
3641#else
3642 buf = curbuf;
3643#endif
3644 }
3645 else
3646 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3647 * (unlisted buffers)
3648 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003649 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 && ((flag == 'U'
3651 ? buf->b_p_bl
3652 : (!buf->b_p_bl
3653 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003654 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 ;
3656 return buf;
3657}
3658
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003659#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003660static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003661
3662/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003663 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003664 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003665 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003666 static void
3667expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003668 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003669 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003670{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003671 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003672 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003673 char_u *funcname;
3674 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003675
Bram Moolenaare344bea2005-09-01 20:46:49 +00003676 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3677 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003678 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003679
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003680 /* Call 'completefunc' to obtain the list of matches. */
3681 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003682 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003683
Bram Moolenaare344bea2005-09-01 20:46:49 +00003684 pos = curwin->w_cursor;
3685 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3686 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003687 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003688 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003689
Bram Moolenaara94bc432006-03-10 21:42:59 +00003690 ins_compl_add_list(matchlist);
3691 list_unref(matchlist);
3692}
3693#endif /* FEAT_COMPL_FUNC */
3694
Bram Moolenaar39f05632006-03-19 22:15:26 +00003695#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003696/*
3697 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003698 */
3699 static void
3700ins_compl_add_list(list)
3701 list_T *list;
3702{
3703 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003704 int dir = compl_direction;
3705
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003706 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003707 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003708 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003709 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3710 /* if dir was BACKWARD then honor it just once */
3711 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003712 else if (did_emsg)
3713 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003714 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003715}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003716
3717/*
3718 * Add a match to the list of matches from a typeval_T.
3719 * If the given string is already in the list of completions, then return
3720 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3721 * maybe because alloc() returns NULL, then FAIL is returned.
3722 */
3723 int
3724ins_compl_add_tv(tv, dir)
3725 typval_T *tv;
3726 int dir;
3727{
3728 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003729 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003730 int adup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003731 char_u *(cptext[CPT_COUNT]);
3732
3733 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3734 {
3735 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3736 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3737 (char_u *)"abbr", FALSE);
3738 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3739 (char_u *)"menu", FALSE);
3740 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3741 (char_u *)"kind", FALSE);
3742 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3743 (char_u *)"info", FALSE);
3744 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3745 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003746 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003747 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003748 }
3749 else
3750 {
3751 word = get_tv_string_chk(tv);
3752 vim_memset(cptext, 0, sizeof(cptext));
3753 }
3754 if (word == NULL || *word == NUL)
3755 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003756 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003757}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003758#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003759
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003760/*
3761 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003762 * The search starts at position "ini" in curbuf and in the direction
3763 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003764 * When "compl_started" is FALSE start at that position, otherwise continue
3765 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003766 * This may return before finding all the matches.
3767 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 */
3769 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003770ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772{
3773 static pos_T first_match_pos;
3774 static pos_T last_match_pos;
3775 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003776 static int found_all = FALSE; /* Found all matches of a
3777 certain type. */
3778 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779
Bram Moolenaar572cb562005-08-05 21:35:02 +00003780 pos_T *pos;
3781 char_u **matches;
3782 int save_p_scs;
3783 int save_p_ws;
3784 int save_p_ic;
3785 int i;
3786 int num_matches;
3787 int len;
3788 int found_new_match;
3789 int type = ctrl_x_mode;
3790 char_u *ptr;
3791 char_u *dict = NULL;
3792 int dict_f = 0;
3793 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003795 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
3797 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3798 ins_buf->b_scanned = 0;
3799 found_all = FALSE;
3800 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003801 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003802 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 last_match_pos = first_match_pos = *ini;
3804 }
3805
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003806 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003807 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3809 for (;;)
3810 {
3811 found_new_match = FAIL;
3812
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003813 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 * or if found_all says this entry is done. For ^X^L only use the
3815 * entries from 'complete' that look in loaded buffers. */
3816 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003817 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 {
3819 found_all = FALSE;
3820 while (*e_cpt == ',' || *e_cpt == ' ')
3821 e_cpt++;
3822 if (*e_cpt == '.' && !curbuf->b_scanned)
3823 {
3824 ins_buf = curbuf;
3825 first_match_pos = *ini;
3826 /* So that ^N can match word immediately after cursor */
3827 if (ctrl_x_mode == 0)
3828 dec(&first_match_pos);
3829 last_match_pos = first_match_pos;
3830 type = 0;
3831 }
3832 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3833 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3834 {
3835 /* Scan a buffer, but not the current one. */
3836 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3837 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003838 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 first_match_pos.col = last_match_pos.col = 0;
3840 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3841 last_match_pos.lnum = 0;
3842 type = 0;
3843 }
3844 else /* unloaded buffer, scan like dictionary */
3845 {
3846 found_all = TRUE;
3847 if (ins_buf->b_fname == NULL)
3848 continue;
3849 type = CTRL_X_DICTIONARY;
3850 dict = ins_buf->b_fname;
3851 dict_f = DICT_EXACT;
3852 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003853 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 ins_buf->b_fname == NULL
3855 ? buf_spname(ins_buf)
3856 : ins_buf->b_sfname == NULL
3857 ? (char *)ins_buf->b_fname
3858 : (char *)ins_buf->b_sfname);
3859 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3860 }
3861 else if (*e_cpt == NUL)
3862 break;
3863 else
3864 {
3865 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3866 type = -1;
3867 else if (*e_cpt == 'k' || *e_cpt == 's')
3868 {
3869 if (*e_cpt == 'k')
3870 type = CTRL_X_DICTIONARY;
3871 else
3872 type = CTRL_X_THESAURUS;
3873 if (*++e_cpt != ',' && *e_cpt != NUL)
3874 {
3875 dict = e_cpt;
3876 dict_f = DICT_FIRST;
3877 }
3878 }
3879#ifdef FEAT_FIND_ID
3880 else if (*e_cpt == 'i')
3881 type = CTRL_X_PATH_PATTERNS;
3882 else if (*e_cpt == 'd')
3883 type = CTRL_X_PATH_DEFINES;
3884#endif
3885 else if (*e_cpt == ']' || *e_cpt == 't')
3886 {
3887 type = CTRL_X_TAGS;
3888 sprintf((char*)IObuff, _("Scanning tags."));
3889 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3890 }
3891 else
3892 type = -1;
3893
3894 /* in any case e_cpt is advanced to the next entry */
3895 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3896
3897 found_all = TRUE;
3898 if (type == -1)
3899 continue;
3900 }
3901 }
3902
3903 switch (type)
3904 {
3905 case -1:
3906 break;
3907#ifdef FEAT_FIND_ID
3908 case CTRL_X_PATH_PATTERNS:
3909 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003910 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003911 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003913 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3915 (linenr_T)1, (linenr_T)MAXLNUM);
3916 break;
3917#endif
3918
3919 case CTRL_X_DICTIONARY:
3920 case CTRL_X_THESAURUS:
3921 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003922 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 : (type == CTRL_X_THESAURUS
3924 ? (*curbuf->b_p_tsr == NUL
3925 ? p_tsr
3926 : curbuf->b_p_tsr)
3927 : (*curbuf->b_p_dict == NUL
3928 ? p_dict
3929 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003930 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003931 dict != NULL ? dict_f
3932 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 dict = NULL;
3934 break;
3935
3936 case CTRL_X_TAGS:
3937 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3938 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003939 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
3941 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003942 * of matches is found when compl_pattern is empty */
3943 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3945 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3946 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3947 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003948 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 p_ic = save_p_ic;
3951 break;
3952
3953 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003954 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3956 {
3957
3958 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003959 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003960 ins_compl_add_matches(num_matches, matches,
3961#ifdef CASE_INSENSITIVE_FILENAME
3962 TRUE
3963#else
3964 FALSE
3965#endif
3966 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 }
3968 break;
3969
3970 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003971 if (expand_cmdline(&compl_xp, compl_pattern,
3972 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003974 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 break;
3976
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003977#ifdef FEAT_COMPL_FUNC
3978 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003979 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003980 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003981 break;
3982#endif
3983
Bram Moolenaar488c6512005-08-11 20:09:58 +00003984 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003985#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003986 num_matches = expand_spelling(first_match_pos.lnum,
3987 first_match_pos.col, compl_pattern, &matches);
3988 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003989 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003990#endif
3991 break;
3992
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 default: /* normal ^P/^N and ^X^L */
3994 /*
3995 * If 'infercase' is set, don't use 'smartcase' here
3996 */
3997 save_p_scs = p_scs;
3998 if (ins_buf->b_p_inf)
3999 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004000
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 /* buffers other than curbuf are scanned from the beginning or the
4002 * end but never from the middle, thus setting nowrapscan in this
4003 * buffers is a good idea, on the other hand, we always set
4004 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4005 save_p_ws = p_ws;
4006 if (ins_buf != curbuf)
4007 p_ws = FALSE;
4008 else if (*e_cpt == '.')
4009 p_ws = TRUE;
4010 for (;;)
4011 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004012 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004014 ++msg_silent; /* Don't want messages for wrapscan. */
4015
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004016 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4017 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004019 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004021 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004023 found_new_match = searchit(NULL, ins_buf, pos,
4024 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004025 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004026 RE_LAST, (linenr_T)0);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004027 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004028 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004030 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004031 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 first_match_pos = *pos;
4033 last_match_pos = *pos;
4034 }
4035 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004036 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 found_new_match = FAIL;
4038 if (found_new_match == FAIL)
4039 {
4040 if (ins_buf == curbuf)
4041 found_all = TRUE;
4042 break;
4043 }
4044
4045 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004046 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 && ini->lnum == pos->lnum
4048 && ini->col == pos->col)
4049 continue;
4050 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4051 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4052 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004053 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
4055 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4056 continue;
4057 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4058 if (!p_paste)
4059 ptr = skipwhite(ptr);
4060 }
4061 len = (int)STRLEN(ptr);
4062 }
4063 else
4064 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004065 char_u *tmp_ptr = ptr;
4066
4067 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004069 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 /* Skip if already inside a word. */
4071 if (vim_iswordp(tmp_ptr))
4072 continue;
4073 /* Find start of next word. */
4074 tmp_ptr = find_word_start(tmp_ptr);
4075 }
4076 /* Find end of this word. */
4077 tmp_ptr = find_word_end(tmp_ptr);
4078 len = (int)(tmp_ptr - ptr);
4079
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004080 if ((compl_cont_status & CONT_ADDING)
4081 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 {
4083 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4084 {
4085 /* Try next line, if any. the new word will be
4086 * "join" as if the normal command "J" was used.
4087 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004088 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 * works -- Acevedo */
4090 STRNCPY(IObuff, ptr, len);
4091 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4092 tmp_ptr = ptr = skipwhite(ptr);
4093 /* Find start of next word. */
4094 tmp_ptr = find_word_start(tmp_ptr);
4095 /* Find end of next word. */
4096 tmp_ptr = find_word_end(tmp_ptr);
4097 if (tmp_ptr > ptr)
4098 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004099 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004101 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 IObuff[len++] = ' ';
4103 /* IObuf =~ "\k.* ", thus len >= 2 */
4104 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004105 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 || (vim_strchr(p_cpo, CPO_JOINSP)
4107 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004108 && (IObuff[len - 2] == '?'
4109 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 IObuff[len++] = ' ';
4111 }
4112 /* copy as much as posible of the new word */
4113 if (tmp_ptr - ptr >= IOSIZE - len)
4114 tmp_ptr = ptr + IOSIZE - len - 1;
4115 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4116 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004117 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
4119 IObuff[len] = NUL;
4120 ptr = IObuff;
4121 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004122 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 continue;
4124 }
4125 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004126 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004127 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004128 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
4130 found_new_match = OK;
4131 break;
4132 }
4133 }
4134 p_scs = save_p_scs;
4135 p_ws = save_p_ws;
4136 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004137
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004138 /* check if compl_curr_match has changed, (e.g. other type of
4139 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004140 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 found_new_match = OK;
4142
4143 /* break the loop for specialized modes (use 'complete' just for the
4144 * generic ctrl_x_mode == 0) or when we've found a new match */
4145 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004146 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004147 {
4148 if (got_int)
4149 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004150 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004151 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004152 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004153
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004154 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4155 || compl_interrupted)
4156 break;
4157 compl_started = TRUE;
4158 }
4159 else
4160 {
4161 /* Mark a buffer scanned when it has been scanned completely */
4162 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4163 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004165 compl_started = FALSE;
4166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004168 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
4170 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4171 && *e_cpt == NUL) /* Got to end of 'complete' */
4172 found_new_match = FAIL;
4173
4174 i = -1; /* total of matches, unknown */
4175 if (found_new_match == FAIL
4176 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4177 i = ins_compl_make_cyclic();
4178
4179 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004180 * just been made cyclic then we have to move compl_curr_match to the next
4181 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004182 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4183 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004184 if (compl_curr_match == NULL)
4185 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 return i;
4187}
4188
4189/* Delete the old text being completed. */
4190 static void
4191ins_compl_delete()
4192{
4193 int i;
4194
4195 /*
4196 * In insert mode: Delete the typed part.
4197 * In replace mode: Put the old characters back, if any.
4198 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004199 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 backspace_until_column(i);
4201 changed_cline_bef_curs();
4202}
4203
4204/* Insert the new text being completed. */
4205 static void
4206ins_compl_insert()
4207{
Bram Moolenaar572cb562005-08-05 21:35:02 +00004208 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004209 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4210 compl_used_match = FALSE;
4211 else
4212 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213}
4214
4215/*
4216 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004217 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4218 * get more completions. If it is FALSE, then we just do nothing when there
4219 * are no more completions in a given direction. The latter case is used when
4220 * we are still in the middle of finding completions, to allow browsing
4221 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 * Return the total number of matches, or -1 if still unknown -- webb.
4223 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004224 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4225 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 *
4227 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004228 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4229 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 */
4231 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004232ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004234 int count; /* repeat completion this many times; should
4235 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004236 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
4238 int num_matches = -1;
4239 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004240 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004241 compl_T *found_compl = NULL;
4242 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004243 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004245 if (compl_leader != NULL
4246 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004248 /* Set "compl_shown_match" to the actually shown match, it may differ
4249 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004250 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004251 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004252 && compl_shown_match->cp_next != NULL
4253 && compl_shown_match->cp_next != compl_first_match)
4254 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004255
4256 /* If we didn't find it searching forward, and compl_shows_dir is
4257 * backward, find the last match. */
4258 if (compl_shows_dir == BACKWARD
4259 && !ins_compl_equal(compl_shown_match,
4260 compl_leader, (int)STRLEN(compl_leader))
4261 && (compl_shown_match->cp_next == NULL
4262 || compl_shown_match->cp_next == compl_first_match))
4263 {
4264 while (!ins_compl_equal(compl_shown_match,
4265 compl_leader, (int)STRLEN(compl_leader))
4266 && compl_shown_match->cp_prev != NULL
4267 && compl_shown_match->cp_prev != compl_first_match)
4268 compl_shown_match = compl_shown_match->cp_prev;
4269 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004270 }
4271
4272 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004273 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 /* Delete old text to be replaced */
4275 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004276
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004277 /* When finding the longest common text we stick at the original text,
4278 * don't let CTRL-N or CTRL-P move to the first match. */
4279 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4280
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004281 /* When restarting the search don't insert the first match either. */
4282 if (compl_restarting)
4283 {
4284 advance = FALSE;
4285 compl_restarting = FALSE;
4286 }
4287
Bram Moolenaare3226be2005-12-18 22:10:00 +00004288 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4289 * around. */
4290 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004292 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004294 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004295 found_end = (compl_first_match != NULL
4296 && (compl_shown_match->cp_next == compl_first_match
4297 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004298 }
4299 else if (compl_shows_dir == BACKWARD
4300 && compl_shown_match->cp_prev != NULL)
4301 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004302 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004303 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004304 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004307 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004308 if (!allow_get_expansion)
4309 {
4310 if (advance)
4311 {
4312 if (compl_shows_dir == BACKWARD)
4313 compl_pending -= todo + 1;
4314 else
4315 compl_pending += todo + 1;
4316 }
4317 return -1;
4318 }
4319
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004320 if (advance)
4321 {
4322 if (compl_shows_dir == BACKWARD)
4323 --compl_pending;
4324 else
4325 ++compl_pending;
4326 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004327
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004328 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004329 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004330
4331 /* handle any pending completions */
4332 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004333 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004334 {
4335 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4336 {
4337 compl_shown_match = compl_shown_match->cp_next;
4338 --compl_pending;
4339 }
4340 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4341 {
4342 compl_shown_match = compl_shown_match->cp_prev;
4343 ++compl_pending;
4344 }
4345 else
4346 break;
4347 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004348 found_end = FALSE;
4349 }
4350 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4351 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004352 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004353 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004354 ++todo;
4355 else
4356 /* Remember a matching item. */
4357 found_compl = compl_shown_match;
4358
4359 /* Stop at the end of the list when we found a usable match. */
4360 if (found_end)
4361 {
4362 if (found_compl != NULL)
4363 {
4364 compl_shown_match = found_compl;
4365 break;
4366 }
4367 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004371 /* Insert the text of the new completion, or the compl_leader. */
4372 if (insert_match)
4373 {
4374 if (!compl_get_longest || compl_used_match)
4375 ins_compl_insert();
4376 else
4377 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4378 }
4379 else
4380 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381
4382 if (!allow_get_expansion)
4383 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004384 /* may undisplay the popup menu first */
4385 ins_compl_upd_pum();
4386
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004387 /* redraw to show the user what was inserted */
4388 update_screen(0);
4389
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004390 /* display the updated popup menu */
4391 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004392#ifdef FEAT_GUI
4393 if (gui.in_use)
4394 {
4395 /* Show the cursor after the match, not after the redrawn text. */
4396 setcursor();
4397 out_flush();
4398 gui_update_cursor(FALSE, FALSE);
4399 }
4400#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004401
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 /* Delete old text to be replaced, since we're still searching and
4403 * don't want to match ourselves! */
4404 ins_compl_delete();
4405 }
4406
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004407 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004408 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004409 compl_enter_selects = !insert_match && compl_match_array != NULL;
4410
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 /*
4412 * Show the file name for the match (if any)
4413 * Truncate the file name to avoid a wait for return.
4414 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004415 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 {
4417 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004418 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 if (i <= 0)
4420 i = 0;
4421 else
4422 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004423 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 msg(IObuff);
4425 redraw_cmdline = FALSE; /* don't overwrite! */
4426 }
4427
4428 return num_matches;
4429}
4430
4431/*
4432 * Call this while finding completions, to check whether the user has hit a key
4433 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004434 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004436 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 */
4438 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004439ins_compl_check_keys(frequency)
4440 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441{
4442 static int count = 0;
4443
4444 int c;
4445
4446 /* Don't check when reading keys from a script. That would break the test
4447 * scripts */
4448 if (using_script())
4449 return;
4450
4451 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004452 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 return;
4454 count = 0;
4455
Bram Moolenaara260a972006-06-23 19:36:29 +00004456 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4457 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 if (c != NUL)
4460 {
4461 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4462 {
4463 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004464 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004465 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4466 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004468 else
4469 {
4470 /* Need to get the character to have KeyTyped set. We'll put it
4471 * back with vungetc() below. */
4472 c = safe_vgetc();
4473
4474 /* Don't interrupt completion when the character wasn't typed,
4475 * e.g., when doing @q to replay keys. */
4476 if (c != Ctrl_R && KeyTyped)
4477 compl_interrupted = TRUE;
4478
4479 vungetc(c);
4480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004482 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004483 {
4484 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4485
4486 compl_pending = 0;
4487 (void)ins_compl_next(FALSE, todo, TRUE);
4488 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004489}
4490
4491/*
4492 * Decide the direction of Insert mode complete from the key typed.
4493 * Returns BACKWARD or FORWARD.
4494 */
4495 static int
4496ins_compl_key2dir(c)
4497 int c;
4498{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004499 if (c == Ctrl_P || c == Ctrl_L
4500 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4501 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004502 return BACKWARD;
4503 return FORWARD;
4504}
4505
4506/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004507 * Return TRUE for keys that are used for completion only when the popup menu
4508 * is visible.
4509 */
4510 static int
4511ins_compl_pum_key(c)
4512 int c;
4513{
4514 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004515 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4516 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004517}
4518
4519/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004520 * Decide the number of completions to move forward.
4521 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4522 */
4523 static int
4524ins_compl_key2count(c)
4525 int c;
4526{
4527 int h;
4528
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004529 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004530 {
4531 h = pum_get_height();
4532 if (h > 3)
4533 h -= 2; /* keep some context */
4534 return h;
4535 }
4536 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537}
4538
4539/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004540 * Return TRUE if completion with "c" should insert the match, FALSE if only
4541 * to change the currently selected completion.
4542 */
4543 static int
4544ins_compl_use_match(c)
4545 int c;
4546{
4547 switch (c)
4548 {
4549 case K_UP:
4550 case K_DOWN:
4551 case K_PAGEDOWN:
4552 case K_KPAGEDOWN:
4553 case K_S_DOWN:
4554 case K_PAGEUP:
4555 case K_KPAGEUP:
4556 case K_S_UP:
4557 return FALSE;
4558 }
4559 return TRUE;
4560}
4561
4562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 * Do Insert mode completion.
4564 * Called when character "c" was typed, which has a meaning for completion.
4565 * Returns OK if completion was done, FAIL if something failed (out of mem).
4566 */
4567 static int
4568ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004569 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004571 char_u *line;
4572 int startcol = 0; /* column where searched text starts */
4573 colnr_T curs_col; /* cursor column */
4574 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
Bram Moolenaare3226be2005-12-18 22:10:00 +00004576 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004577 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
4579 /* First time we hit ^N or ^P (in a row, I mean) */
4580
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 did_ai = FALSE;
4582#ifdef FEAT_SMARTINDENT
4583 did_si = FALSE;
4584 can_si = FALSE;
4585 can_si_back = FALSE;
4586#endif
4587 if (stop_arrow() == FAIL)
4588 return FAIL;
4589
4590 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004591 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004592 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
4594 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004595 * "compl_startpos" to the cursor as a pattern to add a new word
4596 * instead of expand the one before the cursor, in word-wise if
4597 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 * is not in the same line as the cursor then fix it (the line has
4599 * been split because it was longer than 'tw'). if SOL is set then
4600 * skip the previous pattern, a word at the beginning of the line has
4601 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004602 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4603 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 {
4605 /*
4606 * it is a continued search
4607 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004608 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4610 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4611 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004612 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004614 /* line (probably) wrapped, set compl_startpos to the
4615 * first non_blank in the line, if it is not a wordchar
4616 * include it to get a better pattern, but then we don't
4617 * want the "\\<" prefix, check it bellow */
4618 compl_col = (colnr_T)(skipwhite(line) - line);
4619 compl_startpos.col = compl_col;
4620 compl_startpos.lnum = curwin->w_cursor.lnum;
4621 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 }
4623 else
4624 {
4625 /* S_IPOS was set when we inserted a word that was at the
4626 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004627 * mode but first we need to redefine compl_startpos */
4628 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004630 compl_cont_status |= CONT_SOL;
4631 compl_startpos.col = (colnr_T)(skipwhite(
4632 line + compl_length
4633 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004635 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004637 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004638 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 * have enough space? just being paranoic */
4640#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004641 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004643 compl_cont_status &= ~CONT_SOL;
4644 compl_length = (IOSIZE - MIN_SPACE);
4645 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004647 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4648 if (compl_length < 1)
4649 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 }
4651 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004652 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004654 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004657 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004659 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004661 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004663 compl_cont_status = 0;
4664 compl_cont_status |= CONT_N_ADDS;
4665 compl_startpos = curwin->w_cursor;
4666 startcol = (int)curs_col;
4667 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
4669
4670 /* Work out completion pattern and original text -- webb */
4671 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4672 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004673 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4675 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004676 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004678 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004680 compl_col += ++startcol;
4681 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
4683 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004684 compl_pattern = str_foldcase(line + compl_col,
4685 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004687 compl_pattern = vim_strnsave(line + compl_col,
4688 compl_length);
4689 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 return FAIL;
4691 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004692 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 {
4694 char_u *prefix = (char_u *)"\\<";
4695
4696 /* we need 3 extra chars, 1 for the NUL and
4697 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004698 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4699 compl_length) + 3);
4700 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004702 if (!vim_iswordp(line + compl_col)
4703 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 && (
4705#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004706 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004708 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709#endif
4710 )))
4711 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004712 STRCPY((char *)compl_pattern, prefix);
4713 (void)quote_meta(compl_pattern + STRLEN(prefix),
4714 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004716 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004718 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004720 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721#endif
4722 )
4723 {
4724 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004725 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4726 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004728 compl_col += curs_col;
4729 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
4731 else
4732 {
4733#ifdef FEAT_MBYTE
4734 /* Search the point of change class of multibyte character
4735 * or not a word single byte character backward. */
4736 if (has_mbyte)
4737 {
4738 int base_class;
4739 int head_off;
4740
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004741 startcol -= (*mb_head_off)(line, line + startcol);
4742 base_class = mb_get_class(line + startcol);
4743 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004745 head_off = (*mb_head_off)(line, line + startcol);
4746 if (base_class != mb_get_class(line + startcol
4747 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004749 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 }
4751 }
4752 else
4753#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004754 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004756 compl_col += ++startcol;
4757 compl_length = (int)curs_col - startcol;
4758 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 {
4760 /* Only match word with at least two chars -- webb
4761 * there's no need to call quote_meta,
4762 * alloc(7) is enough -- Acevedo
4763 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004764 compl_pattern = alloc(7);
4765 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004767 STRCPY((char *)compl_pattern, "\\<");
4768 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4769 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771 else
4772 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004773 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4774 compl_length) + 3);
4775 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004777 STRCPY((char *)compl_pattern, "\\<");
4778 (void)quote_meta(compl_pattern + 2, line + compl_col,
4779 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
4781 }
4782 }
4783 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4784 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004785 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004786 compl_length = (int)curs_col - (int)compl_col;
4787 if (compl_length < 0) /* cursor in indent: empty pattern */
4788 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004790 compl_pattern = str_foldcase(line + compl_col, compl_length,
4791 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004793 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4794 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 return FAIL;
4796 }
4797 else if (ctrl_x_mode == CTRL_X_FILES)
4798 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004799 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004801 compl_col += ++startcol;
4802 compl_length = (int)curs_col - startcol;
4803 compl_pattern = addstar(line + compl_col, compl_length,
4804 EXPAND_FILES);
4805 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 return FAIL;
4807 }
4808 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4809 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004810 compl_pattern = vim_strnsave(line, curs_col);
4811 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004813 set_cmd_context(&compl_xp, compl_pattern,
4814 (int)STRLEN(compl_pattern), curs_col);
4815 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4816 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004817 /* No completion possible, use an empty pattern to get a
4818 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004819 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004820 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004821 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4822 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004824 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004825 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004826#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004827 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004828 * Call user defined function 'completefunc' with "a:findstart"
4829 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004830 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004831 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004832 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004833 char_u *funcname;
4834 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004835
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004836 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004837 * string */
4838 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4839 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4840 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004841 {
4842 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4843 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004844 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004845 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004846
4847 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004848 args[1] = NULL;
4849 pos = curwin->w_cursor;
4850 col = call_func_retnr(funcname, 2, args, FALSE);
4851 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004852
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004853 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004854 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004855 compl_col = col;
4856 if ((colnr_T)compl_col > curs_col)
4857 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004858
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004859 /* Setup variables for completion. Need to obtain "line" again,
4860 * it may have become invalid. */
4861 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004862 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004863 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4864 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004865#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004866 return FAIL;
4867 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004868 else if (ctrl_x_mode == CTRL_X_SPELL)
4869 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004870#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004871 if (spell_bad_len > 0)
4872 compl_col = curs_col - spell_bad_len;
4873 else
4874 compl_col = spell_word_start(startcol);
4875 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004876 {
4877 compl_length = 0;
4878 compl_col = curs_col;
4879 }
4880 else
4881 {
4882 spell_expand_check_cap(compl_col);
4883 compl_length = (int)curs_col - compl_col;
4884 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004885 /* Need to obtain "line" again, it may have become invalid. */
4886 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004887 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4888 if (compl_pattern == NULL)
4889#endif
4890 return FAIL;
4891 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004892 else
4893 {
4894 EMSG2(_(e_intern2), "ins_complete()");
4895 return FAIL;
4896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004898 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 {
4900 edit_submode_pre = (char_u *)_(" Adding");
4901 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4902 {
4903 /* Insert a new line, keep indentation but ignore 'comments' */
4904#ifdef FEAT_COMMENTS
4905 char_u *old = curbuf->b_p_com;
4906
4907 curbuf->b_p_com = (char_u *)"";
4908#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004909 compl_startpos.lnum = curwin->w_cursor.lnum;
4910 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 ins_eol('\r');
4912#ifdef FEAT_COMMENTS
4913 curbuf->b_p_com = old;
4914#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004915 compl_length = 0;
4916 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 }
4919 else
4920 {
4921 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004922 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 }
4924
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004925 if (compl_cont_status & CONT_LOCAL)
4926 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 else
4928 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4929
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004930 /* Always add completion for the original text. */
4931 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004932 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4933 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004934 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004936 vim_free(compl_pattern);
4937 compl_pattern = NULL;
4938 vim_free(compl_orig_text);
4939 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 return FAIL;
4941 }
4942
4943 /* showmode might reset the internal line pointers, so it must
4944 * be called before line = ml_get(), or when this address is no
4945 * longer needed. -- Acevedo.
4946 */
4947 edit_submode_extra = (char_u *)_("-- Searching...");
4948 edit_submode_highl = HLF_COUNT;
4949 showmode();
4950 edit_submode_extra = NULL;
4951 out_flush();
4952 }
4953
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004954 compl_shown_match = compl_curr_match;
4955 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956
4957 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004958 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004960 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004962 /* may undisplay the popup menu */
4963 ins_compl_upd_pum();
4964
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004965 if (n > 1) /* all matches have been found */
4966 compl_matches = n;
4967 compl_curr_match = compl_shown_match;
4968 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
Bram Moolenaard68071d2006-05-02 22:08:30 +00004970 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4971 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 if (got_int && !global_busy)
4973 {
4974 (void)vgetc();
4975 got_int = FALSE;
4976 }
4977
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004978 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004979 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004981 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4982 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4984 edit_submode_highl = HLF_E;
4985 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4986 * because we couldn't expand anything at first place, but if we used
4987 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4988 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004989 if ( compl_length > 1
4990 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 || (ctrl_x_mode != 0
4992 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4993 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004994 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
4996
Bram Moolenaar572cb562005-08-05 21:35:02 +00004997 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004998 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005000 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
5002 if (edit_submode_extra == NULL)
5003 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005004 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
5006 edit_submode_extra = (char_u *)_("Back at original");
5007 edit_submode_highl = HLF_W;
5008 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005009 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
5011 edit_submode_extra = (char_u *)_("Word from other line");
5012 edit_submode_highl = HLF_COUNT;
5013 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005014 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
5016 edit_submode_extra = (char_u *)_("The only match");
5017 edit_submode_highl = HLF_COUNT;
5018 }
5019 else
5020 {
5021 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005022 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005024 int number = 0;
5025 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005027 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
5029 /* search backwards for the first valid (!= -1) number.
5030 * This should normally succeed already at the first loop
5031 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005032 for (match = compl_curr_match->cp_prev; match != NULL
5033 && match != compl_first_match;
5034 match = match->cp_prev)
5035 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005037 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 break;
5039 }
5040 if (match != NULL)
5041 /* go up and assign all numbers which are not assigned
5042 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005043 for (match = match->cp_next;
5044 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005045 match = match->cp_next)
5046 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
5048 else /* BACKWARD */
5049 {
5050 /* search forwards (upwards) for the first valid (!= -1)
5051 * number. This should normally succeed already at the
5052 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005053 for (match = compl_curr_match->cp_next; match != NULL
5054 && match != compl_first_match;
5055 match = match->cp_next)
5056 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005058 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 break;
5060 }
5061 if (match != NULL)
5062 /* go down and assign all numbers which are not
5063 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005064 for (match = match->cp_prev; match
5065 && match->cp_number == -1;
5066 match = match->cp_prev)
5067 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069 }
5070
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005071 /* The match should always have a sequence number now, this is
5072 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005073 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005075 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5076 * Translations may need more than twice that. */
5077 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005079 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005080 vim_snprintf((char *)match_ref, sizeof(match_ref),
5081 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005082 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005084 vim_snprintf((char *)match_ref, sizeof(match_ref),
5085 _("match %d"),
5086 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 edit_submode_extra = match_ref;
5088 edit_submode_highl = HLF_R;
5089 if (dollar_vcol)
5090 curs_columns(FALSE);
5091 }
5092 }
5093 }
5094
5095 /* Show a message about what (completion) mode we're in. */
5096 showmode();
5097 if (edit_submode_extra != NULL)
5098 {
5099 if (!p_smd)
5100 msg_attr(edit_submode_extra,
5101 edit_submode_highl < HLF_COUNT
5102 ? hl_attr(edit_submode_highl) : 0);
5103 }
5104 else
5105 msg_clr_cmdline(); /* necessary for "noshowmode" */
5106
Bram Moolenaard68071d2006-05-02 22:08:30 +00005107 /* Show the popup menu, unless we got interrupted. */
5108 if (!compl_interrupted)
5109 {
5110 /* RedrawingDisabled may be set when invoked through complete(). */
5111 n = RedrawingDisabled;
5112 RedrawingDisabled = 0;
5113 ins_compl_show_pum();
5114 setcursor();
5115 RedrawingDisabled = n;
5116 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005117 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005118 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005119
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 return OK;
5121}
5122
5123/*
5124 * Looks in the first "len" chars. of "src" for search-metachars.
5125 * If dest is not NULL the chars. are copied there quoting (with
5126 * a backslash) the metachars, and dest would be NUL terminated.
5127 * Returns the length (needed) of dest
5128 */
5129 static int
5130quote_meta(dest, src, len)
5131 char_u *dest;
5132 char_u *src;
5133 int len;
5134{
5135 int m;
5136
5137 for (m = len; --len >= 0; src++)
5138 {
5139 switch (*src)
5140 {
5141 case '.':
5142 case '*':
5143 case '[':
5144 if (ctrl_x_mode == CTRL_X_DICTIONARY
5145 || ctrl_x_mode == CTRL_X_THESAURUS)
5146 break;
5147 case '~':
5148 if (!p_magic) /* quote these only if magic is set */
5149 break;
5150 case '\\':
5151 if (ctrl_x_mode == CTRL_X_DICTIONARY
5152 || ctrl_x_mode == CTRL_X_THESAURUS)
5153 break;
5154 case '^': /* currently it's not needed. */
5155 case '$':
5156 m++;
5157 if (dest != NULL)
5158 *dest++ = '\\';
5159 break;
5160 }
5161 if (dest != NULL)
5162 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005163# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 /* Copy remaining bytes of a multibyte character. */
5165 if (has_mbyte)
5166 {
5167 int i, mb_len;
5168
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005169 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 if (mb_len > 0 && len >= mb_len)
5171 for (i = 0; i < mb_len; ++i)
5172 {
5173 --len;
5174 ++src;
5175 if (dest != NULL)
5176 *dest++ = *src;
5177 }
5178 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005179# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 }
5181 if (dest != NULL)
5182 *dest = NUL;
5183
5184 return m;
5185}
5186#endif /* FEAT_INS_EXPAND */
5187
5188/*
5189 * Next character is interpreted literally.
5190 * A one, two or three digit decimal number is interpreted as its byte value.
5191 * If one or two digits are entered, the next character is given to vungetc().
5192 * For Unicode a character > 255 may be returned.
5193 */
5194 int
5195get_literal()
5196{
5197 int cc;
5198 int nc;
5199 int i;
5200 int hex = FALSE;
5201 int octal = FALSE;
5202#ifdef FEAT_MBYTE
5203 int unicode = 0;
5204#endif
5205
5206 if (got_int)
5207 return Ctrl_C;
5208
5209#ifdef FEAT_GUI
5210 /*
5211 * In GUI there is no point inserting the internal code for a special key.
5212 * It is more useful to insert the string "<KEY>" instead. This would
5213 * probably be useful in a text window too, but it would not be
5214 * vi-compatible (maybe there should be an option for it?) -- webb
5215 */
5216 if (gui.in_use)
5217 ++allow_keys;
5218#endif
5219#ifdef USE_ON_FLY_SCROLL
5220 dont_scroll = TRUE; /* disallow scrolling here */
5221#endif
5222 ++no_mapping; /* don't map the next key hits */
5223 cc = 0;
5224 i = 0;
5225 for (;;)
5226 {
5227 do
5228 nc = safe_vgetc();
5229 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
5230 || nc == K_HOR_SCROLLBAR);
5231#ifdef FEAT_CMDL_INFO
5232 if (!(State & CMDLINE)
5233# ifdef FEAT_MBYTE
5234 && MB_BYTE2LEN_CHECK(nc) == 1
5235# endif
5236 )
5237 add_to_showcmd(nc);
5238#endif
5239 if (nc == 'x' || nc == 'X')
5240 hex = TRUE;
5241 else if (nc == 'o' || nc == 'O')
5242 octal = TRUE;
5243#ifdef FEAT_MBYTE
5244 else if (nc == 'u' || nc == 'U')
5245 unicode = nc;
5246#endif
5247 else
5248 {
5249 if (hex
5250#ifdef FEAT_MBYTE
5251 || unicode != 0
5252#endif
5253 )
5254 {
5255 if (!vim_isxdigit(nc))
5256 break;
5257 cc = cc * 16 + hex2nr(nc);
5258 }
5259 else if (octal)
5260 {
5261 if (nc < '0' || nc > '7')
5262 break;
5263 cc = cc * 8 + nc - '0';
5264 }
5265 else
5266 {
5267 if (!VIM_ISDIGIT(nc))
5268 break;
5269 cc = cc * 10 + nc - '0';
5270 }
5271
5272 ++i;
5273 }
5274
5275 if (cc > 255
5276#ifdef FEAT_MBYTE
5277 && unicode == 0
5278#endif
5279 )
5280 cc = 255; /* limit range to 0-255 */
5281 nc = 0;
5282
5283 if (hex) /* hex: up to two chars */
5284 {
5285 if (i >= 2)
5286 break;
5287 }
5288#ifdef FEAT_MBYTE
5289 else if (unicode) /* Unicode: up to four or eight chars */
5290 {
5291 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5292 break;
5293 }
5294#endif
5295 else if (i >= 3) /* decimal or octal: up to three chars */
5296 break;
5297 }
5298 if (i == 0) /* no number entered */
5299 {
5300 if (nc == K_ZERO) /* NUL is stored as NL */
5301 {
5302 cc = '\n';
5303 nc = 0;
5304 }
5305 else
5306 {
5307 cc = nc;
5308 nc = 0;
5309 }
5310 }
5311
5312 if (cc == 0) /* NUL is stored as NL */
5313 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005314#ifdef FEAT_MBYTE
5315 if (enc_dbcs && (cc & 0xff) == 0)
5316 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5317 second byte will cause trouble! */
5318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
5320 --no_mapping;
5321#ifdef FEAT_GUI
5322 if (gui.in_use)
5323 --allow_keys;
5324#endif
5325 if (nc)
5326 vungetc(nc);
5327 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5328 return cc;
5329}
5330
5331/*
5332 * Insert character, taking care of special keys and mod_mask
5333 */
5334 static void
5335insert_special(c, allow_modmask, ctrlv)
5336 int c;
5337 int allow_modmask;
5338 int ctrlv; /* c was typed after CTRL-V */
5339{
5340 char_u *p;
5341 int len;
5342
5343 /*
5344 * Special function key, translate into "<Key>". Up to the last '>' is
5345 * inserted with ins_str(), so as not to replace characters in replace
5346 * mode.
5347 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5348 * unless 'allow_modmask' is TRUE.
5349 */
5350#ifdef MACOS
5351 /* Command-key never produces a normal key */
5352 if (mod_mask & MOD_MASK_CMD)
5353 allow_modmask = TRUE;
5354#endif
5355 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5356 {
5357 p = get_special_key_name(c, mod_mask);
5358 len = (int)STRLEN(p);
5359 c = p[len - 1];
5360 if (len > 2)
5361 {
5362 if (stop_arrow() == FAIL)
5363 return;
5364 p[len - 1] = NUL;
5365 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005366 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 ctrlv = FALSE;
5368 }
5369 }
5370 if (stop_arrow() == OK)
5371 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5372}
5373
5374/*
5375 * Special characters in this context are those that need processing other
5376 * than the simple insertion that can be performed here. This includes ESC
5377 * which terminates the insert, and CR/NL which need special processing to
5378 * open up a new line. This routine tries to optimize insertions performed by
5379 * the "redo", "undo" or "put" commands, so it needs to know when it should
5380 * stop and defer processing to the "normal" mechanism.
5381 * '0' and '^' are special, because they can be followed by CTRL-D.
5382 */
5383#ifdef EBCDIC
5384# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5385#else
5386# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5387#endif
5388
5389#ifdef FEAT_MBYTE
5390# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5391#else
5392# define WHITECHAR(cc) vim_iswhite(cc)
5393#endif
5394
5395 void
5396insertchar(c, flags, second_indent)
5397 int c; /* character to insert or NUL */
5398 int flags; /* INSCHAR_FORMAT, etc. */
5399 int second_indent; /* indent for second line if >= 0 */
5400{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 int textwidth;
5402#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406
5407 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5408 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
5410 /*
5411 * Try to break the line in two or more pieces when:
5412 * - Always do this if we have been called to do formatting only.
5413 * - Always do this when 'formatoptions' has the 'a' flag and the line
5414 * ends in white space.
5415 * - Otherwise:
5416 * - Don't do this if inserting a blank
5417 * - Don't do this if an existing character is being replaced, unless
5418 * we're in VREPLACE mode.
5419 * - Do this if the cursor is not on the line where insert started
5420 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5421 * before the insert.
5422 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5423 * before 'textwidth'
5424 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005425 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 && ((flags & INSCHAR_FORMAT)
5427 || (!vim_iswhite(c)
5428 && !((State & REPLACE_FLAG)
5429#ifdef FEAT_VREPLACE
5430 && !(State & VREPLACE_FLAG)
5431#endif
5432 && *ml_get_cursor() != NUL)
5433 && (curwin->w_cursor.lnum != Insstart.lnum
5434 || ((!has_format_option(FO_INS_LONG)
5435 || Insstart_textlen <= (colnr_T)textwidth)
5436 && (!fo_ins_blank
5437 || Insstart_blank_vcol <= (colnr_T)textwidth
5438 ))))))
5439 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005440 /* Format with 'formatexpr' when it's set. Use internal formatting
5441 * when 'formatexpr' isn't set or it returns non-zero. */
5442#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005443 int do_internal = TRUE;
5444
5445 if (*curbuf->b_p_fex != NUL)
5446 {
5447 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5448 /* It may be required to save for undo again, e.g. when setline()
5449 * was called. */
5450 ins_need_undo = TRUE;
5451 }
5452 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005454 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005456
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 if (c == NUL) /* only formatting was wanted */
5458 return;
5459
5460#ifdef FEAT_COMMENTS
5461 /* Check whether this character should end a comment. */
5462 if (did_ai && (int)c == end_comment_pending)
5463 {
5464 char_u *line;
5465 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5466 int middle_len, end_len;
5467 int i;
5468
5469 /*
5470 * Need to remove existing (middle) comment leader and insert end
5471 * comment leader. First, check what comment leader we can find.
5472 */
5473 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5474 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5475 {
5476 /* Skip middle-comment string */
5477 while (*p && p[-1] != ':') /* find end of middle flags */
5478 ++p;
5479 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5480 /* Don't count trailing white space for middle_len */
5481 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5482 --middle_len;
5483
5484 /* Find the end-comment string */
5485 while (*p && p[-1] != ':') /* find end of end flags */
5486 ++p;
5487 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5488
5489 /* Skip white space before the cursor */
5490 i = curwin->w_cursor.col;
5491 while (--i >= 0 && vim_iswhite(line[i]))
5492 ;
5493 i++;
5494
5495 /* Skip to before the middle leader */
5496 i -= middle_len;
5497
5498 /* Check some expected things before we go on */
5499 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5500 {
5501 /* Backspace over all the stuff we want to replace */
5502 backspace_until_column(i);
5503
5504 /*
5505 * Insert the end-comment string, except for the last
5506 * character, which will get inserted as normal later.
5507 */
5508 ins_bytes_len(lead_end, end_len - 1);
5509 }
5510 }
5511 }
5512 end_comment_pending = NUL;
5513#endif
5514
5515 did_ai = FALSE;
5516#ifdef FEAT_SMARTINDENT
5517 did_si = FALSE;
5518 can_si = FALSE;
5519 can_si_back = FALSE;
5520#endif
5521
5522 /*
5523 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5524 * This speeds up normal text input considerably.
5525 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5526 * need to re-indent at a ':', or any other character (but not what
5527 * 'paste' is set)..
5528 */
5529#ifdef USE_ON_FLY_SCROLL
5530 dont_scroll = FALSE; /* allow scrolling here */
5531#endif
5532
5533 if ( !ISSPECIAL(c)
5534#ifdef FEAT_MBYTE
5535 && (!has_mbyte || (*mb_char2len)(c) == 1)
5536#endif
5537 && vpeekc() != NUL
5538 && !(State & REPLACE_FLAG)
5539#ifdef FEAT_CINDENT
5540 && !cindent_on()
5541#endif
5542#ifdef FEAT_RIGHTLEFT
5543 && !p_ri
5544#endif
5545 )
5546 {
5547#define INPUT_BUFLEN 100
5548 char_u buf[INPUT_BUFLEN + 1];
5549 int i;
5550 colnr_T virtcol = 0;
5551
5552 buf[0] = c;
5553 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005554 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 virtcol = get_nolist_virtcol();
5556 /*
5557 * Stop the string when:
5558 * - no more chars available
5559 * - finding a special character (command key)
5560 * - buffer is full
5561 * - running into the 'textwidth' boundary
5562 * - need to check for abbreviation: A non-word char after a word-char
5563 */
5564 while ( (c = vpeekc()) != NUL
5565 && !ISSPECIAL(c)
5566#ifdef FEAT_MBYTE
5567 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5568#endif
5569 && i < INPUT_BUFLEN
5570 && (textwidth == 0
5571 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5572 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5573 {
5574#ifdef FEAT_RIGHTLEFT
5575 c = vgetc();
5576 if (p_hkmap && KeyTyped)
5577 c = hkmap(c); /* Hebrew mode mapping */
5578# ifdef FEAT_FKMAP
5579 if (p_fkmap && KeyTyped)
5580 c = fkmap(c); /* Farsi mode mapping */
5581# endif
5582 buf[i++] = c;
5583#else
5584 buf[i++] = vgetc();
5585#endif
5586 }
5587
5588#ifdef FEAT_DIGRAPHS
5589 do_digraph(-1); /* clear digraphs */
5590 do_digraph(buf[i-1]); /* may be the start of a digraph */
5591#endif
5592 buf[i] = NUL;
5593 ins_str(buf);
5594 if (flags & INSCHAR_CTRLV)
5595 {
5596 redo_literal(*buf);
5597 i = 1;
5598 }
5599 else
5600 i = 0;
5601 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005602 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 }
5604 else
5605 {
5606#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005607 int cc;
5608
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5610 {
5611 char_u buf[MB_MAXBYTES + 1];
5612
5613 (*mb_char2bytes)(c, buf);
5614 buf[cc] = NUL;
5615 ins_char_bytes(buf, cc);
5616 AppendCharToRedobuff(c);
5617 }
5618 else
5619#endif
5620 {
5621 ins_char(c);
5622 if (flags & INSCHAR_CTRLV)
5623 redo_literal(c);
5624 else
5625 AppendCharToRedobuff(c);
5626 }
5627 }
5628}
5629
5630/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005631 * Format text at the current insert position.
5632 */
5633 static void
5634internal_format(textwidth, second_indent, flags, format_only)
5635 int textwidth;
5636 int second_indent;
5637 int flags;
5638 int format_only;
5639{
5640 int cc;
5641 int save_char = NUL;
5642 int haveto_redraw = FALSE;
5643 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5644#ifdef FEAT_MBYTE
5645 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5646#endif
5647 int fo_white_par = has_format_option(FO_WHITE_PAR);
5648 int first_line = TRUE;
5649#ifdef FEAT_COMMENTS
5650 colnr_T leader_len;
5651 int no_leader = FALSE;
5652 int do_comments = (flags & INSCHAR_DO_COM);
5653#endif
5654
5655 /*
5656 * When 'ai' is off we don't want a space under the cursor to be
5657 * deleted. Replace it with an 'x' temporarily.
5658 */
5659 if (!curbuf->b_p_ai)
5660 {
5661 cc = gchar_cursor();
5662 if (vim_iswhite(cc))
5663 {
5664 save_char = cc;
5665 pchar_cursor('x');
5666 }
5667 }
5668
5669 /*
5670 * Repeat breaking lines, until the current line is not too long.
5671 */
5672 while (!got_int)
5673 {
5674 int startcol; /* Cursor column at entry */
5675 int wantcol; /* column at textwidth border */
5676 int foundcol; /* column for start of spaces */
5677 int end_foundcol = 0; /* column for start of word */
5678 colnr_T len;
5679 colnr_T virtcol;
5680#ifdef FEAT_VREPLACE
5681 int orig_col = 0;
5682 char_u *saved_text = NULL;
5683#endif
5684 colnr_T col;
5685
5686 virtcol = get_nolist_virtcol();
5687 if (virtcol < (colnr_T)textwidth)
5688 break;
5689
5690#ifdef FEAT_COMMENTS
5691 if (no_leader)
5692 do_comments = FALSE;
5693 else if (!(flags & INSCHAR_FORMAT)
5694 && has_format_option(FO_WRAP_COMS))
5695 do_comments = TRUE;
5696
5697 /* Don't break until after the comment leader */
5698 if (do_comments)
5699 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5700 else
5701 leader_len = 0;
5702
5703 /* If the line doesn't start with a comment leader, then don't
5704 * start one in a following broken line. Avoids that a %word
5705 * moved to the start of the next line causes all following lines
5706 * to start with %. */
5707 if (leader_len == 0)
5708 no_leader = TRUE;
5709#endif
5710 if (!(flags & INSCHAR_FORMAT)
5711#ifdef FEAT_COMMENTS
5712 && leader_len == 0
5713#endif
5714 && !has_format_option(FO_WRAP))
5715
5716 {
5717 textwidth = 0;
5718 break;
5719 }
5720 if ((startcol = curwin->w_cursor.col) == 0)
5721 break;
5722
5723 /* find column of textwidth border */
5724 coladvance((colnr_T)textwidth);
5725 wantcol = curwin->w_cursor.col;
5726
5727 curwin->w_cursor.col = startcol - 1;
5728#ifdef FEAT_MBYTE
5729 /* Correct cursor for multi-byte character. */
5730 if (has_mbyte)
5731 mb_adjust_cursor();
5732#endif
5733 foundcol = 0;
5734
5735 /*
5736 * Find position to break at.
5737 * Stop at first entered white when 'formatoptions' has 'v'
5738 */
5739 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5740 || curwin->w_cursor.lnum != Insstart.lnum
5741 || curwin->w_cursor.col >= Insstart.col)
5742 {
5743 cc = gchar_cursor();
5744 if (WHITECHAR(cc))
5745 {
5746 /* remember position of blank just before text */
5747 end_foundcol = curwin->w_cursor.col;
5748
5749 /* find start of sequence of blanks */
5750 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5751 {
5752 dec_cursor();
5753 cc = gchar_cursor();
5754 }
5755 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5756 break; /* only spaces in front of text */
5757#ifdef FEAT_COMMENTS
5758 /* Don't break until after the comment leader */
5759 if (curwin->w_cursor.col < leader_len)
5760 break;
5761#endif
5762 if (has_format_option(FO_ONE_LETTER))
5763 {
5764 /* do not break after one-letter words */
5765 if (curwin->w_cursor.col == 0)
5766 break; /* one-letter word at begin */
5767
5768 col = curwin->w_cursor.col;
5769 dec_cursor();
5770 cc = gchar_cursor();
5771
5772 if (WHITECHAR(cc))
5773 continue; /* one-letter, continue */
5774 curwin->w_cursor.col = col;
5775 }
5776#ifdef FEAT_MBYTE
5777 if (has_mbyte)
5778 foundcol = curwin->w_cursor.col
5779 + (*mb_ptr2len)(ml_get_cursor());
5780 else
5781#endif
5782 foundcol = curwin->w_cursor.col + 1;
5783 if (curwin->w_cursor.col < (colnr_T)wantcol)
5784 break;
5785 }
5786#ifdef FEAT_MBYTE
5787 else if (cc >= 0x100 && fo_multibyte
5788 && curwin->w_cursor.col <= (colnr_T)wantcol)
5789 {
5790 /* Break after or before a multi-byte character. */
5791 foundcol = curwin->w_cursor.col;
5792 if (curwin->w_cursor.col < (colnr_T)wantcol)
5793 foundcol += (*mb_char2len)(cc);
5794 end_foundcol = foundcol;
5795 break;
5796 }
5797#endif
5798 if (curwin->w_cursor.col == 0)
5799 break;
5800 dec_cursor();
5801 }
5802
5803 if (foundcol == 0) /* no spaces, cannot break line */
5804 {
5805 curwin->w_cursor.col = startcol;
5806 break;
5807 }
5808
5809 /* Going to break the line, remove any "$" now. */
5810 undisplay_dollar();
5811
5812 /*
5813 * Offset between cursor position and line break is used by replace
5814 * stack functions. VREPLACE does not use this, and backspaces
5815 * over the text instead.
5816 */
5817#ifdef FEAT_VREPLACE
5818 if (State & VREPLACE_FLAG)
5819 orig_col = startcol; /* Will start backspacing from here */
5820 else
5821#endif
5822 replace_offset = startcol - end_foundcol - 1;
5823
5824 /*
5825 * adjust startcol for spaces that will be deleted and
5826 * characters that will remain on top line
5827 */
5828 curwin->w_cursor.col = foundcol;
5829 while (cc = gchar_cursor(), WHITECHAR(cc))
5830 inc_cursor();
5831 startcol -= curwin->w_cursor.col;
5832 if (startcol < 0)
5833 startcol = 0;
5834
5835#ifdef FEAT_VREPLACE
5836 if (State & VREPLACE_FLAG)
5837 {
5838 /*
5839 * In VREPLACE mode, we will backspace over the text to be
5840 * wrapped, so save a copy now to put on the next line.
5841 */
5842 saved_text = vim_strsave(ml_get_cursor());
5843 curwin->w_cursor.col = orig_col;
5844 if (saved_text == NULL)
5845 break; /* Can't do it, out of memory */
5846 saved_text[startcol] = NUL;
5847
5848 /* Backspace over characters that will move to the next line */
5849 if (!fo_white_par)
5850 backspace_until_column(foundcol);
5851 }
5852 else
5853#endif
5854 {
5855 /* put cursor after pos. to break line */
5856 if (!fo_white_par)
5857 curwin->w_cursor.col = foundcol;
5858 }
5859
5860 /*
5861 * Split the line just before the margin.
5862 * Only insert/delete lines, but don't really redraw the window.
5863 */
5864 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5865 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5866#ifdef FEAT_COMMENTS
5867 + (do_comments ? OPENLINE_DO_COM : 0)
5868#endif
5869 , old_indent);
5870 old_indent = 0;
5871
5872 replace_offset = 0;
5873 if (first_line)
5874 {
5875 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5876 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5877 if (second_indent >= 0)
5878 {
5879#ifdef FEAT_VREPLACE
5880 if (State & VREPLACE_FLAG)
5881 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5882 else
5883#endif
5884 (void)set_indent(second_indent, SIN_CHANGED);
5885 }
5886 first_line = FALSE;
5887 }
5888
5889#ifdef FEAT_VREPLACE
5890 if (State & VREPLACE_FLAG)
5891 {
5892 /*
5893 * In VREPLACE mode we have backspaced over the text to be
5894 * moved, now we re-insert it into the new line.
5895 */
5896 ins_bytes(saved_text);
5897 vim_free(saved_text);
5898 }
5899 else
5900#endif
5901 {
5902 /*
5903 * Check if cursor is not past the NUL off the line, cindent
5904 * may have added or removed indent.
5905 */
5906 curwin->w_cursor.col += startcol;
5907 len = (colnr_T)STRLEN(ml_get_curline());
5908 if (curwin->w_cursor.col > len)
5909 curwin->w_cursor.col = len;
5910 }
5911
5912 haveto_redraw = TRUE;
5913#ifdef FEAT_CINDENT
5914 can_cindent = TRUE;
5915#endif
5916 /* moved the cursor, don't autoindent or cindent now */
5917 did_ai = FALSE;
5918#ifdef FEAT_SMARTINDENT
5919 did_si = FALSE;
5920 can_si = FALSE;
5921 can_si_back = FALSE;
5922#endif
5923 line_breakcheck();
5924 }
5925
5926 if (save_char != NUL) /* put back space after cursor */
5927 pchar_cursor(save_char);
5928
5929 if (!format_only && haveto_redraw)
5930 {
5931 update_topline();
5932 redraw_curbuf_later(VALID);
5933 }
5934}
5935
5936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 * Called after inserting or deleting text: When 'formatoptions' includes the
5938 * 'a' flag format from the current line until the end of the paragraph.
5939 * Keep the cursor at the same position relative to the text.
5940 * The caller must have saved the cursor line for undo, following ones will be
5941 * saved here.
5942 */
5943 void
5944auto_format(trailblank, prev_line)
5945 int trailblank; /* when TRUE also format with trailing blank */
5946 int prev_line; /* may start in previous line */
5947{
5948 pos_T pos;
5949 colnr_T len;
5950 char_u *old;
5951 char_u *new, *pnew;
5952 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005953 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954
5955 if (!has_format_option(FO_AUTO))
5956 return;
5957
5958 pos = curwin->w_cursor;
5959 old = ml_get_curline();
5960
5961 /* may remove added space */
5962 check_auto_format(FALSE);
5963
5964 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5965 * user might insert normal text next. Also skip formatting when "1" is
5966 * in 'formatoptions' and there is a single character before the cursor.
5967 * Otherwise the line would be broken and when typing another non-white
5968 * next they are not joined back together. */
5969 wasatend = (pos.col == STRLEN(old));
5970 if (*old != NUL && !trailblank && wasatend)
5971 {
5972 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005973 cc = gchar_cursor();
5974 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5975 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005977 cc = gchar_cursor();
5978 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 {
5980 curwin->w_cursor = pos;
5981 return;
5982 }
5983 curwin->w_cursor = pos;
5984 }
5985
5986#ifdef FEAT_COMMENTS
5987 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5988 * comments. */
5989 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5990 && get_leader_len(old, NULL, FALSE) == 0)
5991 return;
5992#endif
5993
5994 /*
5995 * May start formatting in a previous line, so that after "x" a word is
5996 * moved to the previous line if it fits there now. Only when this is not
5997 * the start of a paragraph.
5998 */
5999 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6000 {
6001 --curwin->w_cursor.lnum;
6002 if (u_save_cursor() == FAIL)
6003 return;
6004 }
6005
6006 /*
6007 * Do the formatting and restore the cursor position. "saved_cursor" will
6008 * be adjusted for the text formatting.
6009 */
6010 saved_cursor = pos;
6011 format_lines((linenr_T)-1);
6012 curwin->w_cursor = saved_cursor;
6013 saved_cursor.lnum = 0;
6014
6015 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6016 {
6017 /* "cannot happen" */
6018 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6019 coladvance((colnr_T)MAXCOL);
6020 }
6021 else
6022 check_cursor_col();
6023
6024 /* Insert mode: If the cursor is now after the end of the line while it
6025 * previously wasn't, the line was broken. Because of the rule above we
6026 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6027 * formatted. */
6028 if (!wasatend && has_format_option(FO_WHITE_PAR))
6029 {
6030 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006031 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 if (curwin->w_cursor.col == len)
6033 {
6034 pnew = vim_strnsave(new, len + 2);
6035 pnew[len] = ' ';
6036 pnew[len + 1] = NUL;
6037 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6038 /* remove the space later */
6039 did_add_space = TRUE;
6040 }
6041 else
6042 /* may remove added space */
6043 check_auto_format(FALSE);
6044 }
6045
6046 check_cursor();
6047}
6048
6049/*
6050 * When an extra space was added to continue a paragraph for auto-formatting,
6051 * delete it now. The space must be under the cursor, just after the insert
6052 * position.
6053 */
6054 static void
6055check_auto_format(end_insert)
6056 int end_insert; /* TRUE when ending Insert mode */
6057{
6058 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006059 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060
6061 if (did_add_space)
6062 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006063 cc = gchar_cursor();
6064 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 /* Somehow the space was removed already. */
6066 did_add_space = FALSE;
6067 else
6068 {
6069 if (!end_insert)
6070 {
6071 inc_cursor();
6072 c = gchar_cursor();
6073 dec_cursor();
6074 }
6075 if (c != NUL)
6076 {
6077 /* The space is no longer at the end of the line, delete it. */
6078 del_char(FALSE);
6079 did_add_space = FALSE;
6080 }
6081 }
6082 }
6083}
6084
6085/*
6086 * Find out textwidth to be used for formatting:
6087 * if 'textwidth' option is set, use it
6088 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6089 * if invalid value, use 0.
6090 * Set default to window width (maximum 79) for "gq" operator.
6091 */
6092 int
6093comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006094 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095{
6096 int textwidth;
6097
6098 textwidth = curbuf->b_p_tw;
6099 if (textwidth == 0 && curbuf->b_p_wm)
6100 {
6101 /* The width is the window width minus 'wrapmargin' minus all the
6102 * things that add to the margin. */
6103 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6104#ifdef FEAT_CMDWIN
6105 if (cmdwin_type != 0)
6106 textwidth -= 1;
6107#endif
6108#ifdef FEAT_FOLDING
6109 textwidth -= curwin->w_p_fdc;
6110#endif
6111#ifdef FEAT_SIGNS
6112 if (curwin->w_buffer->b_signlist != NULL
6113# ifdef FEAT_NETBEANS_INTG
6114 || usingNetbeans
6115# endif
6116 )
6117 textwidth -= 1;
6118#endif
6119 if (curwin->w_p_nu)
6120 textwidth -= 8;
6121 }
6122 if (textwidth < 0)
6123 textwidth = 0;
6124 if (ff && textwidth == 0)
6125 {
6126 textwidth = W_WIDTH(curwin) - 1;
6127 if (textwidth > 79)
6128 textwidth = 79;
6129 }
6130 return textwidth;
6131}
6132
6133/*
6134 * Put a character in the redo buffer, for when just after a CTRL-V.
6135 */
6136 static void
6137redo_literal(c)
6138 int c;
6139{
6140 char_u buf[10];
6141
6142 /* Only digits need special treatment. Translate them into a string of
6143 * three digits. */
6144 if (VIM_ISDIGIT(c))
6145 {
6146 sprintf((char *)buf, "%03d", c);
6147 AppendToRedobuff(buf);
6148 }
6149 else
6150 AppendCharToRedobuff(c);
6151}
6152
6153/*
6154 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006155 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 */
6157 static void
6158start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006159 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160{
6161 if (!arrow_used) /* something has been inserted */
6162 {
6163 AppendToRedobuff(ESC_STR);
6164 stop_insert(end_insert_pos, FALSE);
6165 arrow_used = TRUE; /* this means we stopped the current insert */
6166 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006167#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006168 check_spell_redraw();
6169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170}
6171
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006172#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006173/*
6174 * If we skipped highlighting word at cursor, do it now.
6175 * It may be skipped again, thus reset spell_redraw_lnum first.
6176 */
6177 static void
6178check_spell_redraw()
6179{
6180 if (spell_redraw_lnum != 0)
6181 {
6182 linenr_T lnum = spell_redraw_lnum;
6183
6184 spell_redraw_lnum = 0;
6185 redrawWinline(lnum, FALSE);
6186 }
6187}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006188
6189/*
6190 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6191 * spelled word, if there is one.
6192 */
6193 static void
6194spell_back_to_badword()
6195{
6196 pos_T tpos = curwin->w_cursor;
6197
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006198 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006199 if (curwin->w_cursor.col != tpos.col)
6200 start_arrow(&tpos);
6201}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006202#endif
6203
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204/*
6205 * stop_arrow() is called before a change is made in insert mode.
6206 * If an arrow key has been used, start a new insertion.
6207 * Returns FAIL if undo is impossible, shouldn't insert then.
6208 */
6209 int
6210stop_arrow()
6211{
6212 if (arrow_used)
6213 {
6214 if (u_save_cursor() == OK)
6215 {
6216 arrow_used = FALSE;
6217 ins_need_undo = FALSE;
6218 }
6219 Insstart = curwin->w_cursor; /* new insertion starts here */
6220 Insstart_textlen = linetabsize(ml_get_curline());
6221 ai_col = 0;
6222#ifdef FEAT_VREPLACE
6223 if (State & VREPLACE_FLAG)
6224 {
6225 orig_line_count = curbuf->b_ml.ml_line_count;
6226 vr_lines_changed = 1;
6227 }
6228#endif
6229 ResetRedobuff();
6230 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006231 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 }
6233 else if (ins_need_undo)
6234 {
6235 if (u_save_cursor() == OK)
6236 ins_need_undo = FALSE;
6237 }
6238
6239#ifdef FEAT_FOLDING
6240 /* Always open fold at the cursor line when inserting something. */
6241 foldOpenCursor();
6242#endif
6243
6244 return (arrow_used || ins_need_undo ? FAIL : OK);
6245}
6246
6247/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006248 * Do a few things to stop inserting.
6249 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6250 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 */
6252 static void
6253stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006254 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006255 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006257 int cc;
6258 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259
6260 stop_redo_ins();
6261 replace_flush(); /* abandon replace stack */
6262
6263 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006264 * Save the inserted text for later redo with ^@ and CTRL-A.
6265 * Don't do it when "restart_edit" was set and nothing was inserted,
6266 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006268 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006269 if (did_restart_edit == 0 || (ptr != NULL
6270 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006271 {
6272 vim_free(last_insert);
6273 last_insert = ptr;
6274 last_insert_skip = new_insert_skip;
6275 }
6276 else
6277 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006279 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 {
6281 /* Auto-format now. It may seem strange to do this when stopping an
6282 * insertion (or moving the cursor), but it's required when appending
6283 * a line and having it end in a space. But only do it when something
6284 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006285 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006287 pos_T tpos = curwin->w_cursor;
6288
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 /* When the cursor is at the end of the line after a space the
6290 * formatting will move it to the following word. Avoid that by
6291 * moving the cursor onto the space. */
6292 cc = 'x';
6293 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6294 {
6295 dec_cursor();
6296 cc = gchar_cursor();
6297 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006298 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 }
6300
6301 auto_format(TRUE, FALSE);
6302
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006303 if (vim_iswhite(cc))
6304 {
6305 if (gchar_cursor() != NUL)
6306 inc_cursor();
6307#ifdef FEAT_VIRTUALEDIT
6308 /* If the cursor is still at the same character, also keep
6309 * the "coladd". */
6310 if (gchar_cursor() == NUL
6311 && curwin->w_cursor.lnum == tpos.lnum
6312 && curwin->w_cursor.col == tpos.col)
6313 curwin->w_cursor.coladd = tpos.coladd;
6314#endif
6315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 }
6317
6318 /* If a space was inserted for auto-formatting, remove it now. */
6319 check_auto_format(TRUE);
6320
6321 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006322 * of the line, and put the cursor back.
6323 * Do this when ESC was used or moving the cursor up/down. */
6324 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006325 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006327 pos_T tpos = curwin->w_cursor;
6328
6329 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006330 for (;;)
6331 {
6332 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6333 --curwin->w_cursor.col;
6334 cc = gchar_cursor();
6335 if (!vim_iswhite(cc))
6336 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006338 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006339 if (curwin->w_cursor.lnum != tpos.lnum)
6340 curwin->w_cursor = tpos;
6341 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6343
6344#ifdef FEAT_VISUAL
6345 /* <C-S-Right> may have started Visual mode, adjust the position for
6346 * deleted characters. */
6347 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6348 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006349 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 if (VIsual.col > (colnr_T)cc)
6351 {
6352 VIsual.col = cc;
6353# ifdef FEAT_VIRTUALEDIT
6354 VIsual.coladd = 0;
6355# endif
6356 }
6357 }
6358#endif
6359 }
6360 }
6361 did_ai = FALSE;
6362#ifdef FEAT_SMARTINDENT
6363 did_si = FALSE;
6364 can_si = FALSE;
6365 can_si_back = FALSE;
6366#endif
6367
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006368 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6369 * now in a different buffer. */
6370 if (end_insert_pos != NULL)
6371 {
6372 curbuf->b_op_start = Insstart;
6373 curbuf->b_op_end = *end_insert_pos;
6374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375}
6376
6377/*
6378 * Set the last inserted text to a single character.
6379 * Used for the replace command.
6380 */
6381 void
6382set_last_insert(c)
6383 int c;
6384{
6385 char_u *s;
6386
6387 vim_free(last_insert);
6388#ifdef FEAT_MBYTE
6389 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6390#else
6391 last_insert = alloc(6);
6392#endif
6393 if (last_insert != NULL)
6394 {
6395 s = last_insert;
6396 /* Use the CTRL-V only when entering a special char */
6397 if (c < ' ' || c == DEL)
6398 *s++ = Ctrl_V;
6399 s = add_char2buf(c, s);
6400 *s++ = ESC;
6401 *s++ = NUL;
6402 last_insert_skip = 0;
6403 }
6404}
6405
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006406#if defined(EXITFREE) || defined(PROTO)
6407 void
6408free_last_insert()
6409{
6410 vim_free(last_insert);
6411 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006412 vim_free(compl_orig_text);
6413 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006414}
6415#endif
6416
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417/*
6418 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6419 * and CSI. Handle multi-byte characters.
6420 * Returns a pointer to after the added bytes.
6421 */
6422 char_u *
6423add_char2buf(c, s)
6424 int c;
6425 char_u *s;
6426{
6427#ifdef FEAT_MBYTE
6428 char_u temp[MB_MAXBYTES];
6429 int i;
6430 int len;
6431
6432 len = (*mb_char2bytes)(c, temp);
6433 for (i = 0; i < len; ++i)
6434 {
6435 c = temp[i];
6436#endif
6437 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6438 if (c == K_SPECIAL)
6439 {
6440 *s++ = K_SPECIAL;
6441 *s++ = KS_SPECIAL;
6442 *s++ = KE_FILLER;
6443 }
6444#ifdef FEAT_GUI
6445 else if (c == CSI)
6446 {
6447 *s++ = CSI;
6448 *s++ = KS_EXTRA;
6449 *s++ = (int)KE_CSI;
6450 }
6451#endif
6452 else
6453 *s++ = c;
6454#ifdef FEAT_MBYTE
6455 }
6456#endif
6457 return s;
6458}
6459
6460/*
6461 * move cursor to start of line
6462 * if flags & BL_WHITE move to first non-white
6463 * if flags & BL_SOL move to first non-white if startofline is set,
6464 * otherwise keep "curswant" column
6465 * if flags & BL_FIX don't leave the cursor on a NUL.
6466 */
6467 void
6468beginline(flags)
6469 int flags;
6470{
6471 if ((flags & BL_SOL) && !p_sol)
6472 coladvance(curwin->w_curswant);
6473 else
6474 {
6475 curwin->w_cursor.col = 0;
6476#ifdef FEAT_VIRTUALEDIT
6477 curwin->w_cursor.coladd = 0;
6478#endif
6479
6480 if (flags & (BL_WHITE | BL_SOL))
6481 {
6482 char_u *ptr;
6483
6484 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6485 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6486 ++curwin->w_cursor.col;
6487 }
6488 curwin->w_set_curswant = TRUE;
6489 }
6490}
6491
6492/*
6493 * oneright oneleft cursor_down cursor_up
6494 *
6495 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006496 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 * Return OK when successful, FAIL when we hit a line of file boundary.
6498 */
6499
6500 int
6501oneright()
6502{
6503 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
6506#ifdef FEAT_VIRTUALEDIT
6507 if (virtual_active())
6508 {
6509 pos_T prevpos = curwin->w_cursor;
6510
6511 /* Adjust for multi-wide char (excluding TAB) */
6512 ptr = ml_get_cursor();
6513 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006514# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006516# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 ))
6520 ? ptr2cells(ptr) : 1));
6521 curwin->w_set_curswant = TRUE;
6522 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6523 return (prevpos.col != curwin->w_cursor.col
6524 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6525 }
6526#endif
6527
6528 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006529 if (*ptr == NUL)
6530 return FAIL; /* already at the very end */
6531
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006533 if (has_mbyte)
6534 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 else
6536#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006537 l = 1;
6538
6539 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6540 * contains "onemore". */
6541 if (ptr[l] == NUL
6542#ifdef FEAT_VIRTUALEDIT
6543 && (ve_flags & VE_ONEMORE) == 0
6544#endif
6545 )
6546 return FAIL;
6547 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
6549 curwin->w_set_curswant = TRUE;
6550 return OK;
6551}
6552
6553 int
6554oneleft()
6555{
6556#ifdef FEAT_VIRTUALEDIT
6557 if (virtual_active())
6558 {
6559 int width;
6560 int v = getviscol();
6561
6562 if (v == 0)
6563 return FAIL;
6564
6565# ifdef FEAT_LINEBREAK
6566 /* We might get stuck on 'showbreak', skip over it. */
6567 width = 1;
6568 for (;;)
6569 {
6570 coladvance(v - width);
6571 /* getviscol() is slow, skip it when 'showbreak' is empty and
6572 * there are no multi-byte characters */
6573 if ((*p_sbr == NUL
6574# ifdef FEAT_MBYTE
6575 && !has_mbyte
6576# endif
6577 ) || getviscol() < v)
6578 break;
6579 ++width;
6580 }
6581# else
6582 coladvance(v - 1);
6583# endif
6584
6585 if (curwin->w_cursor.coladd == 1)
6586 {
6587 char_u *ptr;
6588
6589 /* Adjust for multi-wide char (not a TAB) */
6590 ptr = ml_get_cursor();
6591 if (*ptr != TAB && vim_isprintc(
6592# ifdef FEAT_MBYTE
6593 (*mb_ptr2char)(ptr)
6594# else
6595 *ptr
6596# endif
6597 ) && ptr2cells(ptr) > 1)
6598 curwin->w_cursor.coladd = 0;
6599 }
6600
6601 curwin->w_set_curswant = TRUE;
6602 return OK;
6603 }
6604#endif
6605
6606 if (curwin->w_cursor.col == 0)
6607 return FAIL;
6608
6609 curwin->w_set_curswant = TRUE;
6610 --curwin->w_cursor.col;
6611
6612#ifdef FEAT_MBYTE
6613 /* if the character on the left of the current cursor is a multi-byte
6614 * character, move to its first byte */
6615 if (has_mbyte)
6616 mb_adjust_cursor();
6617#endif
6618 return OK;
6619}
6620
6621 int
6622cursor_up(n, upd_topline)
6623 long n;
6624 int upd_topline; /* When TRUE: update topline */
6625{
6626 linenr_T lnum;
6627
6628 if (n > 0)
6629 {
6630 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006631 /* This fails if the cursor is already in the first line or the count
6632 * is larger than the line number and '-' is in 'cpoptions' */
6633 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 return FAIL;
6635 if (n >= lnum)
6636 lnum = 1;
6637 else
6638#ifdef FEAT_FOLDING
6639 if (hasAnyFolding(curwin))
6640 {
6641 /*
6642 * Count each sequence of folded lines as one logical line.
6643 */
6644 /* go to the the start of the current fold */
6645 (void)hasFolding(lnum, &lnum, NULL);
6646
6647 while (n--)
6648 {
6649 /* move up one line */
6650 --lnum;
6651 if (lnum <= 1)
6652 break;
6653 /* If we entered a fold, move to the beginning, unless in
6654 * Insert mode or when 'foldopen' contains "all": it will open
6655 * in a moment. */
6656 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6657 (void)hasFolding(lnum, &lnum, NULL);
6658 }
6659 if (lnum < 1)
6660 lnum = 1;
6661 }
6662 else
6663#endif
6664 lnum -= n;
6665 curwin->w_cursor.lnum = lnum;
6666 }
6667
6668 /* try to advance to the column we want to be at */
6669 coladvance(curwin->w_curswant);
6670
6671 if (upd_topline)
6672 update_topline(); /* make sure curwin->w_topline is valid */
6673
6674 return OK;
6675}
6676
6677/*
6678 * Cursor down a number of logical lines.
6679 */
6680 int
6681cursor_down(n, upd_topline)
6682 long n;
6683 int upd_topline; /* When TRUE: update topline */
6684{
6685 linenr_T lnum;
6686
6687 if (n > 0)
6688 {
6689 lnum = curwin->w_cursor.lnum;
6690#ifdef FEAT_FOLDING
6691 /* Move to last line of fold, will fail if it's the end-of-file. */
6692 (void)hasFolding(lnum, NULL, &lnum);
6693#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006694 /* This fails if the cursor is already in the last line or would move
6695 * beyound the last line and '-' is in 'cpoptions' */
6696 if (lnum >= curbuf->b_ml.ml_line_count
6697 || (lnum + n > curbuf->b_ml.ml_line_count
6698 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 return FAIL;
6700 if (lnum + n >= curbuf->b_ml.ml_line_count)
6701 lnum = curbuf->b_ml.ml_line_count;
6702 else
6703#ifdef FEAT_FOLDING
6704 if (hasAnyFolding(curwin))
6705 {
6706 linenr_T last;
6707
6708 /* count each sequence of folded lines as one logical line */
6709 while (n--)
6710 {
6711 if (hasFolding(lnum, NULL, &last))
6712 lnum = last + 1;
6713 else
6714 ++lnum;
6715 if (lnum >= curbuf->b_ml.ml_line_count)
6716 break;
6717 }
6718 if (lnum > curbuf->b_ml.ml_line_count)
6719 lnum = curbuf->b_ml.ml_line_count;
6720 }
6721 else
6722#endif
6723 lnum += n;
6724 curwin->w_cursor.lnum = lnum;
6725 }
6726
6727 /* try to advance to the column we want to be at */
6728 coladvance(curwin->w_curswant);
6729
6730 if (upd_topline)
6731 update_topline(); /* make sure curwin->w_topline is valid */
6732
6733 return OK;
6734}
6735
6736/*
6737 * Stuff the last inserted text in the read buffer.
6738 * Last_insert actually is a copy of the redo buffer, so we
6739 * first have to remove the command.
6740 */
6741 int
6742stuff_inserted(c, count, no_esc)
6743 int c; /* Command character to be inserted */
6744 long count; /* Repeat this many times */
6745 int no_esc; /* Don't add an ESC at the end */
6746{
6747 char_u *esc_ptr;
6748 char_u *ptr;
6749 char_u *last_ptr;
6750 char_u last = NUL;
6751
6752 ptr = get_last_insert();
6753 if (ptr == NULL)
6754 {
6755 EMSG(_(e_noinstext));
6756 return FAIL;
6757 }
6758
6759 /* may want to stuff the command character, to start Insert mode */
6760 if (c != NUL)
6761 stuffcharReadbuff(c);
6762 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6763 *esc_ptr = NUL; /* remove the ESC */
6764
6765 /* when the last char is either "0" or "^" it will be quoted if no ESC
6766 * comes after it OR if it will inserted more than once and "ptr"
6767 * starts with ^D. -- Acevedo
6768 */
6769 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6770 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6771 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6772 {
6773 last = *last_ptr;
6774 *last_ptr = NUL;
6775 }
6776
6777 do
6778 {
6779 stuffReadbuff(ptr);
6780 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6781 if (last)
6782 stuffReadbuff((char_u *)(last == '0'
6783 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6784 : IF_EB("\026^", CTRL_V_STR "^")));
6785 }
6786 while (--count > 0);
6787
6788 if (last)
6789 *last_ptr = last;
6790
6791 if (esc_ptr != NULL)
6792 *esc_ptr = ESC; /* put the ESC back */
6793
6794 /* may want to stuff a trailing ESC, to get out of Insert mode */
6795 if (!no_esc)
6796 stuffcharReadbuff(ESC);
6797
6798 return OK;
6799}
6800
6801 char_u *
6802get_last_insert()
6803{
6804 if (last_insert == NULL)
6805 return NULL;
6806 return last_insert + last_insert_skip;
6807}
6808
6809/*
6810 * Get last inserted string, and remove trailing <Esc>.
6811 * Returns pointer to allocated memory (must be freed) or NULL.
6812 */
6813 char_u *
6814get_last_insert_save()
6815{
6816 char_u *s;
6817 int len;
6818
6819 if (last_insert == NULL)
6820 return NULL;
6821 s = vim_strsave(last_insert + last_insert_skip);
6822 if (s != NULL)
6823 {
6824 len = (int)STRLEN(s);
6825 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6826 s[len - 1] = NUL;
6827 }
6828 return s;
6829}
6830
6831/*
6832 * Check the word in front of the cursor for an abbreviation.
6833 * Called when the non-id character "c" has been entered.
6834 * When an abbreviation is recognized it is removed from the text and
6835 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6836 */
6837 static int
6838echeck_abbr(c)
6839 int c;
6840{
6841 /* Don't check for abbreviation in paste mode, when disabled and just
6842 * after moving around with cursor keys. */
6843 if (p_paste || no_abbr || arrow_used)
6844 return FALSE;
6845
6846 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6847 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6848}
6849
6850/*
6851 * replace-stack functions
6852 *
6853 * When replacing characters, the replaced characters are remembered for each
6854 * new character. This is used to re-insert the old text when backspacing.
6855 *
6856 * There is a NUL headed list of characters for each character that is
6857 * currently in the file after the insertion point. When BS is used, one NUL
6858 * headed list is put back for the deleted character.
6859 *
6860 * For a newline, there are two NUL headed lists. One contains the characters
6861 * that the NL replaced. The extra one stores the characters after the cursor
6862 * that were deleted (always white space).
6863 *
6864 * Replace_offset is normally 0, in which case replace_push will add a new
6865 * character at the end of the stack. If replace_offset is not 0, that many
6866 * characters will be left on the stack above the newly inserted character.
6867 */
6868
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006869static char_u *replace_stack = NULL;
6870static long replace_stack_nr = 0; /* next entry in replace stack */
6871static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872
6873 void
6874replace_push(c)
6875 int c; /* character that is replaced (NUL is none) */
6876{
6877 char_u *p;
6878
6879 if (replace_stack_nr < replace_offset) /* nothing to do */
6880 return;
6881 if (replace_stack_len <= replace_stack_nr)
6882 {
6883 replace_stack_len += 50;
6884 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6885 if (p == NULL) /* out of memory */
6886 {
6887 replace_stack_len -= 50;
6888 return;
6889 }
6890 if (replace_stack != NULL)
6891 {
6892 mch_memmove(p, replace_stack,
6893 (size_t)(replace_stack_nr * sizeof(char_u)));
6894 vim_free(replace_stack);
6895 }
6896 replace_stack = p;
6897 }
6898 p = replace_stack + replace_stack_nr - replace_offset;
6899 if (replace_offset)
6900 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6901 *p = c;
6902 ++replace_stack_nr;
6903}
6904
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006905#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906/*
6907 * call replace_push(c) with replace_offset set to the first NUL.
6908 */
6909 static void
6910replace_push_off(c)
6911 int c;
6912{
6913 char_u *p;
6914
6915 p = replace_stack + replace_stack_nr;
6916 for (replace_offset = 1; replace_offset < replace_stack_nr;
6917 ++replace_offset)
6918 if (*--p == NUL)
6919 break;
6920 replace_push(c);
6921 replace_offset = 0;
6922}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924
6925/*
6926 * Pop one item from the replace stack.
6927 * return -1 if stack empty
6928 * return replaced character or NUL otherwise
6929 */
6930 static int
6931replace_pop()
6932{
6933 if (replace_stack_nr == 0)
6934 return -1;
6935 return (int)replace_stack[--replace_stack_nr];
6936}
6937
6938/*
6939 * Join the top two items on the replace stack. This removes to "off"'th NUL
6940 * encountered.
6941 */
6942 static void
6943replace_join(off)
6944 int off; /* offset for which NUL to remove */
6945{
6946 int i;
6947
6948 for (i = replace_stack_nr; --i >= 0; )
6949 if (replace_stack[i] == NUL && off-- <= 0)
6950 {
6951 --replace_stack_nr;
6952 mch_memmove(replace_stack + i, replace_stack + i + 1,
6953 (size_t)(replace_stack_nr - i));
6954 return;
6955 }
6956}
6957
6958/*
6959 * Pop bytes from the replace stack until a NUL is found, and insert them
6960 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6961 */
6962 static void
6963replace_pop_ins()
6964{
6965 int cc;
6966 int oldState = State;
6967
6968 State = NORMAL; /* don't want REPLACE here */
6969 while ((cc = replace_pop()) > 0)
6970 {
6971#ifdef FEAT_MBYTE
6972 mb_replace_pop_ins(cc);
6973#else
6974 ins_char(cc);
6975#endif
6976 dec_cursor();
6977 }
6978 State = oldState;
6979}
6980
6981#ifdef FEAT_MBYTE
6982/*
6983 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6984 * indicates a multi-byte char, pop the other bytes too.
6985 */
6986 static void
6987mb_replace_pop_ins(cc)
6988 int cc;
6989{
6990 int n;
6991 char_u buf[MB_MAXBYTES];
6992 int i;
6993 int c;
6994
6995 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6996 {
6997 buf[0] = cc;
6998 for (i = 1; i < n; ++i)
6999 buf[i] = replace_pop();
7000 ins_bytes_len(buf, n);
7001 }
7002 else
7003 ins_char(cc);
7004
7005 if (enc_utf8)
7006 /* Handle composing chars. */
7007 for (;;)
7008 {
7009 c = replace_pop();
7010 if (c == -1) /* stack empty */
7011 break;
7012 if ((n = MB_BYTE2LEN(c)) == 1)
7013 {
7014 /* Not a multi-byte char, put it back. */
7015 replace_push(c);
7016 break;
7017 }
7018 else
7019 {
7020 buf[0] = c;
7021 for (i = 1; i < n; ++i)
7022 buf[i] = replace_pop();
7023 if (utf_iscomposing(utf_ptr2char(buf)))
7024 ins_bytes_len(buf, n);
7025 else
7026 {
7027 /* Not a composing char, put it back. */
7028 for (i = n - 1; i >= 0; --i)
7029 replace_push(buf[i]);
7030 break;
7031 }
7032 }
7033 }
7034}
7035#endif
7036
7037/*
7038 * make the replace stack empty
7039 * (called when exiting replace mode)
7040 */
7041 static void
7042replace_flush()
7043{
7044 vim_free(replace_stack);
7045 replace_stack = NULL;
7046 replace_stack_len = 0;
7047 replace_stack_nr = 0;
7048}
7049
7050/*
7051 * Handle doing a BS for one character.
7052 * cc < 0: replace stack empty, just move cursor
7053 * cc == 0: character was inserted, delete it
7054 * cc > 0: character was replaced, put cc (first byte of original char) back
7055 * and check for more characters to be put back
7056 */
7057 static void
7058replace_do_bs()
7059{
7060 int cc;
7061#ifdef FEAT_VREPLACE
7062 int orig_len = 0;
7063 int ins_len;
7064 int orig_vcols = 0;
7065 colnr_T start_vcol;
7066 char_u *p;
7067 int i;
7068 int vcol;
7069#endif
7070
7071 cc = replace_pop();
7072 if (cc > 0)
7073 {
7074#ifdef FEAT_VREPLACE
7075 if (State & VREPLACE_FLAG)
7076 {
7077 /* Get the number of screen cells used by the character we are
7078 * going to delete. */
7079 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7080 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7081 }
7082#endif
7083#ifdef FEAT_MBYTE
7084 if (has_mbyte)
7085 {
7086 del_char(FALSE);
7087# ifdef FEAT_VREPLACE
7088 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007089 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090# endif
7091 replace_push(cc);
7092 }
7093 else
7094#endif
7095 {
7096 pchar_cursor(cc);
7097#ifdef FEAT_VREPLACE
7098 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007099 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100#endif
7101 }
7102 replace_pop_ins();
7103
7104#ifdef FEAT_VREPLACE
7105 if (State & VREPLACE_FLAG)
7106 {
7107 /* Get the number of screen cells used by the inserted characters */
7108 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007109 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 vcol = start_vcol;
7111 for (i = 0; i < ins_len; ++i)
7112 {
7113 vcol += chartabsize(p + i, vcol);
7114#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007115 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116#endif
7117 }
7118 vcol -= start_vcol;
7119
7120 /* Delete spaces that were inserted after the cursor to keep the
7121 * text aligned. */
7122 curwin->w_cursor.col += ins_len;
7123 while (vcol > orig_vcols && gchar_cursor() == ' ')
7124 {
7125 del_char(FALSE);
7126 ++orig_vcols;
7127 }
7128 curwin->w_cursor.col -= ins_len;
7129 }
7130#endif
7131
7132 /* mark the buffer as changed and prepare for displaying */
7133 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7134 }
7135 else if (cc == 0)
7136 (void)del_char(FALSE);
7137}
7138
7139#ifdef FEAT_CINDENT
7140/*
7141 * Return TRUE if C-indenting is on.
7142 */
7143 static int
7144cindent_on()
7145{
7146 return (!p_paste && (curbuf->b_p_cin
7147# ifdef FEAT_EVAL
7148 || *curbuf->b_p_inde != NUL
7149# endif
7150 ));
7151}
7152#endif
7153
7154#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7155/*
7156 * Re-indent the current line, based on the current contents of it and the
7157 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7158 * confused what all the part that handles Control-T is doing that I'm not.
7159 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7160 */
7161
7162 void
7163fixthisline(get_the_indent)
7164 int (*get_the_indent) __ARGS((void));
7165{
7166 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
7167 if (linewhite(curwin->w_cursor.lnum))
7168 did_ai = TRUE; /* delete the indent if the line stays empty */
7169}
7170
7171 void
7172fix_indent()
7173{
7174 if (p_paste)
7175 return;
7176# ifdef FEAT_LISP
7177 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7178 fixthisline(get_lisp_indent);
7179# endif
7180# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7181 else
7182# endif
7183# ifdef FEAT_CINDENT
7184 if (cindent_on())
7185 do_c_expr_indent();
7186# endif
7187}
7188
7189#endif
7190
7191#ifdef FEAT_CINDENT
7192/*
7193 * return TRUE if 'cinkeys' contains the key "keytyped",
7194 * when == '*': Only if key is preceded with '*' (indent before insert)
7195 * when == '!': Only if key is prededed with '!' (don't insert)
7196 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7197 *
7198 * "keytyped" can have a few special values:
7199 * KEY_OPEN_FORW
7200 * KEY_OPEN_BACK
7201 * KEY_COMPLETE just finished completion.
7202 *
7203 * If line_is_empty is TRUE accept keys with '0' before them.
7204 */
7205 int
7206in_cinkeys(keytyped, when, line_is_empty)
7207 int keytyped;
7208 int when;
7209 int line_is_empty;
7210{
7211 char_u *look;
7212 int try_match;
7213 int try_match_word;
7214 char_u *p;
7215 char_u *line;
7216 int icase;
7217 int i;
7218
7219#ifdef FEAT_EVAL
7220 if (*curbuf->b_p_inde != NUL)
7221 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7222 else
7223#endif
7224 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7225 while (*look)
7226 {
7227 /*
7228 * Find out if we want to try a match with this key, depending on
7229 * 'when' and a '*' or '!' before the key.
7230 */
7231 switch (when)
7232 {
7233 case '*': try_match = (*look == '*'); break;
7234 case '!': try_match = (*look == '!'); break;
7235 default: try_match = (*look != '*'); break;
7236 }
7237 if (*look == '*' || *look == '!')
7238 ++look;
7239
7240 /*
7241 * If there is a '0', only accept a match if the line is empty.
7242 * But may still match when typing last char of a word.
7243 */
7244 if (*look == '0')
7245 {
7246 try_match_word = try_match;
7247 if (!line_is_empty)
7248 try_match = FALSE;
7249 ++look;
7250 }
7251 else
7252 try_match_word = FALSE;
7253
7254 /*
7255 * does it look like a control character?
7256 */
7257 if (*look == '^'
7258#ifdef EBCDIC
7259 && (Ctrl_chr(look[1]) != 0)
7260#else
7261 && look[1] >= '?' && look[1] <= '_'
7262#endif
7263 )
7264 {
7265 if (try_match && keytyped == Ctrl_chr(look[1]))
7266 return TRUE;
7267 look += 2;
7268 }
7269 /*
7270 * 'o' means "o" command, open forward.
7271 * 'O' means "O" command, open backward.
7272 */
7273 else if (*look == 'o')
7274 {
7275 if (try_match && keytyped == KEY_OPEN_FORW)
7276 return TRUE;
7277 ++look;
7278 }
7279 else if (*look == 'O')
7280 {
7281 if (try_match && keytyped == KEY_OPEN_BACK)
7282 return TRUE;
7283 ++look;
7284 }
7285
7286 /*
7287 * 'e' means to check for "else" at start of line and just before the
7288 * cursor.
7289 */
7290 else if (*look == 'e')
7291 {
7292 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7293 {
7294 p = ml_get_curline();
7295 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7296 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7297 return TRUE;
7298 }
7299 ++look;
7300 }
7301
7302 /*
7303 * ':' only causes an indent if it is at the end of a label or case
7304 * statement, or when it was before typing the ':' (to fix
7305 * class::method for C++).
7306 */
7307 else if (*look == ':')
7308 {
7309 if (try_match && keytyped == ':')
7310 {
7311 p = ml_get_curline();
7312 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7313 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007314 /* Need to get the line again after cin_islabel(). */
7315 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 if (curwin->w_cursor.col > 2
7317 && p[curwin->w_cursor.col - 1] == ':'
7318 && p[curwin->w_cursor.col - 2] == ':')
7319 {
7320 p[curwin->w_cursor.col - 1] = ' ';
7321 i = (cin_iscase(p) || cin_isscopedecl(p)
7322 || cin_islabel(30));
7323 p = ml_get_curline();
7324 p[curwin->w_cursor.col - 1] = ':';
7325 if (i)
7326 return TRUE;
7327 }
7328 }
7329 ++look;
7330 }
7331
7332
7333 /*
7334 * Is it a key in <>, maybe?
7335 */
7336 else if (*look == '<')
7337 {
7338 if (try_match)
7339 {
7340 /*
7341 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7342 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7343 * >, *, : and ! keys if they really really want to.
7344 */
7345 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7346 && keytyped == look[1])
7347 return TRUE;
7348
7349 if (keytyped == get_special_key_code(look + 1))
7350 return TRUE;
7351 }
7352 while (*look && *look != '>')
7353 look++;
7354 while (*look == '>')
7355 look++;
7356 }
7357
7358 /*
7359 * Is it a word: "=word"?
7360 */
7361 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7362 {
7363 ++look;
7364 if (*look == '~')
7365 {
7366 icase = TRUE;
7367 ++look;
7368 }
7369 else
7370 icase = FALSE;
7371 p = vim_strchr(look, ',');
7372 if (p == NULL)
7373 p = look + STRLEN(look);
7374 if ((try_match || try_match_word)
7375 && curwin->w_cursor.col >= (colnr_T)(p - look))
7376 {
7377 int match = FALSE;
7378
7379#ifdef FEAT_INS_EXPAND
7380 if (keytyped == KEY_COMPLETE)
7381 {
7382 char_u *s;
7383
7384 /* Just completed a word, check if it starts with "look".
7385 * search back for the start of a word. */
7386 line = ml_get_curline();
7387# ifdef FEAT_MBYTE
7388 if (has_mbyte)
7389 {
7390 char_u *n;
7391
7392 for (s = line + curwin->w_cursor.col; s > line; s = n)
7393 {
7394 n = mb_prevptr(line, s);
7395 if (!vim_iswordp(n))
7396 break;
7397 }
7398 }
7399 else
7400# endif
7401 for (s = line + curwin->w_cursor.col; s > line; --s)
7402 if (!vim_iswordc(s[-1]))
7403 break;
7404 if (s + (p - look) <= line + curwin->w_cursor.col
7405 && (icase
7406 ? MB_STRNICMP(s, look, p - look)
7407 : STRNCMP(s, look, p - look)) == 0)
7408 match = TRUE;
7409 }
7410 else
7411#endif
7412 /* TODO: multi-byte */
7413 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7414 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7415 {
7416 line = ml_get_cursor();
7417 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7418 || !vim_iswordc(line[-(p - look) - 1]))
7419 && (icase
7420 ? MB_STRNICMP(line - (p - look), look, p - look)
7421 : STRNCMP(line - (p - look), look, p - look))
7422 == 0)
7423 match = TRUE;
7424 }
7425 if (match && try_match_word && !try_match)
7426 {
7427 /* "0=word": Check if there are only blanks before the
7428 * word. */
7429 line = ml_get_curline();
7430 if ((int)(skipwhite(line) - line) !=
7431 (int)(curwin->w_cursor.col - (p - look)))
7432 match = FALSE;
7433 }
7434 if (match)
7435 return TRUE;
7436 }
7437 look = p;
7438 }
7439
7440 /*
7441 * ok, it's a boring generic character.
7442 */
7443 else
7444 {
7445 if (try_match && *look == keytyped)
7446 return TRUE;
7447 ++look;
7448 }
7449
7450 /*
7451 * Skip over ", ".
7452 */
7453 look = skip_to_option_part(look);
7454 }
7455 return FALSE;
7456}
7457#endif /* FEAT_CINDENT */
7458
7459#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7460/*
7461 * Map Hebrew keyboard when in hkmap mode.
7462 */
7463 int
7464hkmap(c)
7465 int c;
7466{
7467 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7468 {
7469 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7470 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7471 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7472 static char_u map[26] =
7473 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7474 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7475 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7476 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7477 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7478 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7479 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7480 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7481 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7482
7483 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7484 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7485 /* '-1'='sofit' */
7486 else if (c == 'x')
7487 return 'X';
7488 else if (c == 'q')
7489 return '\''; /* {geresh}={'} */
7490 else if (c == 246)
7491 return ' '; /* \"o --> ' ' for a german keyboard */
7492 else if (c == 228)
7493 return ' '; /* \"a --> ' ' -- / -- */
7494 else if (c == 252)
7495 return ' '; /* \"u --> ' ' -- / -- */
7496#ifdef EBCDIC
7497 else if (islower(c))
7498#else
7499 /* NOTE: islower() does not do the right thing for us on Linux so we
7500 * do this the same was as 5.7 and previous, so it works correctly on
7501 * all systems. Specifically, the e.g. Delete and Arrow keys are
7502 * munged and won't work if e.g. searching for Hebrew text.
7503 */
7504 else if (c >= 'a' && c <= 'z')
7505#endif
7506 return (int)(map[CharOrdLow(c)] + p_aleph);
7507 else
7508 return c;
7509 }
7510 else
7511 {
7512 switch (c)
7513 {
7514 case '`': return ';';
7515 case '/': return '.';
7516 case '\'': return ',';
7517 case 'q': return '/';
7518 case 'w': return '\'';
7519
7520 /* Hebrew letters - set offset from 'a' */
7521 case ',': c = '{'; break;
7522 case '.': c = 'v'; break;
7523 case ';': c = 't'; break;
7524 default: {
7525 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7526
7527#ifdef EBCDIC
7528 /* see note about islower() above */
7529 if (!islower(c))
7530#else
7531 if (c < 'a' || c > 'z')
7532#endif
7533 return c;
7534 c = str[CharOrdLow(c)];
7535 break;
7536 }
7537 }
7538
7539 return (int)(CharOrdLow(c) + p_aleph);
7540 }
7541}
7542#endif
7543
7544 static void
7545ins_reg()
7546{
7547 int need_redraw = FALSE;
7548 int regname;
7549 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007550#ifdef FEAT_VISUAL
7551 int vis_active = VIsual_active;
7552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553
7554 /*
7555 * If we are going to wait for a character, show a '"'.
7556 */
7557 pc_status = PC_STATUS_UNSET;
7558 if (redrawing() && !char_avail())
7559 {
7560 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007561 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562
7563 edit_putchar('"', TRUE);
7564#ifdef FEAT_CMDL_INFO
7565 add_to_showcmd_c(Ctrl_R);
7566#endif
7567 }
7568
7569#ifdef USE_ON_FLY_SCROLL
7570 dont_scroll = TRUE; /* disallow scrolling here */
7571#endif
7572
7573 /*
7574 * Don't map the register name. This also prevents the mode message to be
7575 * deleted when ESC is hit.
7576 */
7577 ++no_mapping;
7578 regname = safe_vgetc();
7579#ifdef FEAT_LANGMAP
7580 LANGMAP_ADJUST(regname, TRUE);
7581#endif
7582 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7583 {
7584 /* Get a third key for literal register insertion */
7585 literally = regname;
7586#ifdef FEAT_CMDL_INFO
7587 add_to_showcmd_c(literally);
7588#endif
7589 regname = safe_vgetc();
7590#ifdef FEAT_LANGMAP
7591 LANGMAP_ADJUST(regname, TRUE);
7592#endif
7593 }
7594 --no_mapping;
7595
7596#ifdef FEAT_EVAL
7597 /*
7598 * Don't call u_sync() while getting the expression,
7599 * evaluating it or giving an error message for it!
7600 */
7601 ++no_u_sync;
7602 if (regname == '=')
7603 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007604# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007606# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007608# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 /* Restore the Input Method. */
7610 if (im_on)
7611 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007614 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7615 {
7616 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 else
7620 {
7621#endif
7622 if (literally == Ctrl_O || literally == Ctrl_P)
7623 {
7624 /* Append the command to the redo buffer. */
7625 AppendCharToRedobuff(Ctrl_R);
7626 AppendCharToRedobuff(literally);
7627 AppendCharToRedobuff(regname);
7628
7629 do_put(regname, BACKWARD, 1L,
7630 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7631 }
7632 else if (insert_reg(regname, literally) == FAIL)
7633 {
7634 vim_beep();
7635 need_redraw = TRUE; /* remove the '"' */
7636 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007637 else if (stop_insert_mode)
7638 /* When the '=' register was used and a function was invoked that
7639 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7640 * insert anything, need to remove the '"' */
7641 need_redraw = TRUE;
7642
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643#ifdef FEAT_EVAL
7644 }
7645 --no_u_sync;
7646#endif
7647#ifdef FEAT_CMDL_INFO
7648 clear_showcmd();
7649#endif
7650
7651 /* If the inserted register is empty, we need to remove the '"' */
7652 if (need_redraw || stuff_empty())
7653 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007654
7655#ifdef FEAT_VISUAL
7656 /* Disallow starting Visual mode here, would get a weird mode. */
7657 if (!vis_active && VIsual_active)
7658 end_visual_mode();
7659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660}
7661
7662/*
7663 * CTRL-G commands in Insert mode.
7664 */
7665 static void
7666ins_ctrl_g()
7667{
7668 int c;
7669
7670#ifdef FEAT_INS_EXPAND
7671 /* Right after CTRL-X the cursor will be after the ruler. */
7672 setcursor();
7673#endif
7674
7675 /*
7676 * Don't map the second key. This also prevents the mode message to be
7677 * deleted when ESC is hit.
7678 */
7679 ++no_mapping;
7680 c = safe_vgetc();
7681 --no_mapping;
7682 switch (c)
7683 {
7684 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7685 case K_UP:
7686 case Ctrl_K:
7687 case 'k': ins_up(TRUE);
7688 break;
7689
7690 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7691 case K_DOWN:
7692 case Ctrl_J:
7693 case 'j': ins_down(TRUE);
7694 break;
7695
7696 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007697 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007699
7700 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00007701 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007702 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 break;
7704
7705 /* Unknown CTRL-G command, reserved for future expansion. */
7706 default: vim_beep();
7707 }
7708}
7709
7710/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007711 * CTRL-^ in Insert mode.
7712 */
7713 static void
7714ins_ctrl_hat()
7715{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007716 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007717 {
7718 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7719 if (State & LANGMAP)
7720 {
7721 curbuf->b_p_iminsert = B_IMODE_NONE;
7722 State &= ~LANGMAP;
7723 }
7724 else
7725 {
7726 curbuf->b_p_iminsert = B_IMODE_LMAP;
7727 State |= LANGMAP;
7728#ifdef USE_IM_CONTROL
7729 im_set_active(FALSE);
7730#endif
7731 }
7732 }
7733#ifdef USE_IM_CONTROL
7734 else
7735 {
7736 /* There are no ":lmap" mappings, toggle IM */
7737 if (im_get_status())
7738 {
7739 curbuf->b_p_iminsert = B_IMODE_NONE;
7740 im_set_active(FALSE);
7741 }
7742 else
7743 {
7744 curbuf->b_p_iminsert = B_IMODE_IM;
7745 State &= ~LANGMAP;
7746 im_set_active(TRUE);
7747 }
7748 }
7749#endif
7750 set_iminsert_global();
7751 showmode();
7752#ifdef FEAT_GUI
7753 /* may show different cursor shape or color */
7754 if (gui.in_use)
7755 gui_update_cursor(TRUE, FALSE);
7756#endif
7757#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7758 /* Show/unshow value of 'keymap' in status lines. */
7759 status_redraw_curbuf();
7760#endif
7761}
7762
7763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 * Handle ESC in insert mode.
7765 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7766 * insert.
7767 */
7768 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007769ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 long *count;
7771 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007772 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773{
7774 int temp;
7775 static int disabled_redraw = FALSE;
7776
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007777#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007778 check_spell_redraw();
7779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780#if defined(FEAT_HANGULIN)
7781# if defined(ESC_CHG_TO_ENG_MODE)
7782 hangul_input_state_set(0);
7783# endif
7784 if (composing_hangul)
7785 {
7786 push_raw_key(composing_hangul_buffer, 2);
7787 composing_hangul = 0;
7788 }
7789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790
7791 temp = curwin->w_cursor.col;
7792 if (disabled_redraw)
7793 {
7794 --RedrawingDisabled;
7795 disabled_redraw = FALSE;
7796 }
7797 if (!arrow_used)
7798 {
7799 /*
7800 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007801 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7802 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 */
7804 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007805 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806
7807 /*
7808 * Repeating insert may take a long time. Check for
7809 * interrupt now and then.
7810 */
7811 if (*count > 0)
7812 {
7813 line_breakcheck();
7814 if (got_int)
7815 *count = 0;
7816 }
7817
7818 if (--*count > 0) /* repeat what was typed */
7819 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007820 /* Vi repeats the insert without replacing characters. */
7821 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7822 State &= ~REPLACE_FLAG;
7823
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 (void)start_redo_ins();
7825 if (cmdchar == 'r' || cmdchar == 'v')
7826 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7827 ++RedrawingDisabled;
7828 disabled_redraw = TRUE;
7829 return FALSE; /* repeat the insert */
7830 }
7831 stop_insert(&curwin->w_cursor, TRUE);
7832 undisplay_dollar();
7833 }
7834
7835 /* When an autoindent was removed, curswant stays after the
7836 * indent */
7837 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7838 curwin->w_set_curswant = TRUE;
7839
7840 /* Remember the last Insert position in the '^ mark. */
7841 if (!cmdmod.keepjumps)
7842 curbuf->b_last_insert = curwin->w_cursor;
7843
7844 /*
7845 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007846 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007848 if (!nomove
7849 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850#ifdef FEAT_VIRTUALEDIT
7851 || curwin->w_cursor.coladd > 0
7852#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007853 )
7854 && (restart_edit == NUL
7855 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007857 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007859 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860#ifdef FEAT_RIGHTLEFT
7861 && !revins_on
7862#endif
7863 )
7864 {
7865#ifdef FEAT_VIRTUALEDIT
7866 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7867 {
7868 oneleft();
7869 if (restart_edit != NUL)
7870 ++curwin->w_cursor.coladd;
7871 }
7872 else
7873#endif
7874 {
7875 --curwin->w_cursor.col;
7876#ifdef FEAT_MBYTE
7877 /* Correct cursor for multi-byte character. */
7878 if (has_mbyte)
7879 mb_adjust_cursor();
7880#endif
7881 }
7882 }
7883
7884#ifdef USE_IM_CONTROL
7885 /* Disable IM to allow typing English directly for Normal mode commands.
7886 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7887 * well). */
7888 if (!(State & LANGMAP))
7889 im_save_status(&curbuf->b_p_iminsert);
7890 im_set_active(FALSE);
7891#endif
7892
7893 State = NORMAL;
7894 /* need to position cursor again (e.g. when on a TAB ) */
7895 changed_cline_bef_curs();
7896
7897#ifdef FEAT_MOUSE
7898 setmouse();
7899#endif
7900#ifdef CURSOR_SHAPE
7901 ui_cursor_shape(); /* may show different cursor shape */
7902#endif
7903
7904 /*
7905 * When recording or for CTRL-O, need to display the new mode.
7906 * Otherwise remove the mode message.
7907 */
7908 if (Recording || restart_edit != NUL)
7909 showmode();
7910 else if (p_smd)
7911 MSG("");
7912
7913 return TRUE; /* exit Insert mode */
7914}
7915
7916#ifdef FEAT_RIGHTLEFT
7917/*
7918 * Toggle language: hkmap and revins_on.
7919 * Move to end of reverse inserted text.
7920 */
7921 static void
7922ins_ctrl_()
7923{
7924 if (revins_on && revins_chars && revins_scol >= 0)
7925 {
7926 while (gchar_cursor() != NUL && revins_chars--)
7927 ++curwin->w_cursor.col;
7928 }
7929 p_ri = !p_ri;
7930 revins_on = (State == INSERT && p_ri);
7931 if (revins_on)
7932 {
7933 revins_scol = curwin->w_cursor.col;
7934 revins_legal++;
7935 revins_chars = 0;
7936 undisplay_dollar();
7937 }
7938 else
7939 revins_scol = -1;
7940#ifdef FEAT_FKMAP
7941 if (p_altkeymap)
7942 {
7943 /*
7944 * to be consistent also for redo command, using '.'
7945 * set arrow_used to true and stop it - causing to redo
7946 * characters entered in one mode (normal/reverse insert).
7947 */
7948 arrow_used = TRUE;
7949 (void)stop_arrow();
7950 p_fkmap = curwin->w_p_rl ^ p_ri;
7951 if (p_fkmap && p_ri)
7952 State = INSERT;
7953 }
7954 else
7955#endif
7956 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7957 showmode();
7958}
7959#endif
7960
7961#ifdef FEAT_VISUAL
7962/*
7963 * If 'keymodel' contains "startsel", may start selection.
7964 * Returns TRUE when a CTRL-O and other keys stuffed.
7965 */
7966 static int
7967ins_start_select(c)
7968 int c;
7969{
7970 if (km_startsel)
7971 switch (c)
7972 {
7973 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 case K_PAGEUP:
7976 case K_KPAGEUP:
7977 case K_PAGEDOWN:
7978 case K_KPAGEDOWN:
7979# ifdef MACOS
7980 case K_LEFT:
7981 case K_RIGHT:
7982 case K_UP:
7983 case K_DOWN:
7984 case K_END:
7985 case K_HOME:
7986# endif
7987 if (!(mod_mask & MOD_MASK_SHIFT))
7988 break;
7989 /* FALLTHROUGH */
7990 case K_S_LEFT:
7991 case K_S_RIGHT:
7992 case K_S_UP:
7993 case K_S_DOWN:
7994 case K_S_END:
7995 case K_S_HOME:
7996 /* Start selection right away, the cursor can move with
7997 * CTRL-O when beyond the end of the line. */
7998 start_selection();
7999
8000 /* Execute the key in (insert) Select mode. */
8001 stuffcharReadbuff(Ctrl_O);
8002 if (mod_mask)
8003 {
8004 char_u buf[4];
8005
8006 buf[0] = K_SPECIAL;
8007 buf[1] = KS_MODIFIER;
8008 buf[2] = mod_mask;
8009 buf[3] = NUL;
8010 stuffReadbuff(buf);
8011 }
8012 stuffcharReadbuff(c);
8013 return TRUE;
8014 }
8015 return FALSE;
8016}
8017#endif
8018
8019/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008020 * <Insert> key in Insert mode: toggle insert/remplace mode.
8021 */
8022 static void
8023ins_insert(replaceState)
8024 int replaceState;
8025{
8026#ifdef FEAT_FKMAP
8027 if (p_fkmap && p_ri)
8028 {
8029 beep_flush();
8030 EMSG(farsi_text_3); /* encoded in Farsi */
8031 return;
8032 }
8033#endif
8034
8035#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008036# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008037 set_vim_var_string(VV_INSERTMODE,
8038 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008039# ifdef FEAT_VREPLACE
8040 replaceState == VREPLACE ? "v" :
8041# endif
8042 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008043# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008044 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8045#endif
8046 if (State & REPLACE_FLAG)
8047 State = INSERT | (State & LANGMAP);
8048 else
8049 State = replaceState | (State & LANGMAP);
8050 AppendCharToRedobuff(K_INS);
8051 showmode();
8052#ifdef CURSOR_SHAPE
8053 ui_cursor_shape(); /* may show different cursor shape */
8054#endif
8055}
8056
8057/*
8058 * Pressed CTRL-O in Insert mode.
8059 */
8060 static void
8061ins_ctrl_o()
8062{
8063#ifdef FEAT_VREPLACE
8064 if (State & VREPLACE_FLAG)
8065 restart_edit = 'V';
8066 else
8067#endif
8068 if (State & REPLACE_FLAG)
8069 restart_edit = 'R';
8070 else
8071 restart_edit = 'I';
8072#ifdef FEAT_VIRTUALEDIT
8073 if (virtual_active())
8074 ins_at_eol = FALSE; /* cursor always keeps its column */
8075 else
8076#endif
8077 ins_at_eol = (gchar_cursor() == NUL);
8078}
8079
8080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 * If the cursor is on an indent, ^T/^D insert/delete one
8082 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
8083 * Always round the indent to 'shiftwith', this is compatible
8084 * with vi. But vi only supports ^T and ^D after an
8085 * autoindent, we support it everywhere.
8086 */
8087 static void
8088ins_shift(c, lastc)
8089 int c;
8090 int lastc;
8091{
8092 if (stop_arrow() == FAIL)
8093 return;
8094 AppendCharToRedobuff(c);
8095
8096 /*
8097 * 0^D and ^^D: remove all indent.
8098 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008099 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8100 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {
8102 --curwin->w_cursor.col;
8103 (void)del_char(FALSE); /* delete the '^' or '0' */
8104 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8105 if (State & REPLACE_FLAG)
8106 replace_pop_ins();
8107 if (lastc == '^')
8108 old_indent = get_indent(); /* remember curr. indent */
8109 change_indent(INDENT_SET, 0, TRUE, 0);
8110 }
8111 else
8112 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
8113
8114 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8115 did_ai = FALSE;
8116#ifdef FEAT_SMARTINDENT
8117 did_si = FALSE;
8118 can_si = FALSE;
8119 can_si_back = FALSE;
8120#endif
8121#ifdef FEAT_CINDENT
8122 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8123#endif
8124}
8125
8126 static void
8127ins_del()
8128{
8129 int temp;
8130
8131 if (stop_arrow() == FAIL)
8132 return;
8133 if (gchar_cursor() == NUL) /* delete newline */
8134 {
8135 temp = curwin->w_cursor.col;
8136 if (!can_bs(BS_EOL) /* only if "eol" included */
8137 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8138 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8139 || do_join(FALSE) == FAIL)
8140 vim_beep();
8141 else
8142 curwin->w_cursor.col = temp;
8143 }
8144 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8145 vim_beep();
8146 did_ai = FALSE;
8147#ifdef FEAT_SMARTINDENT
8148 did_si = FALSE;
8149 can_si = FALSE;
8150 can_si_back = FALSE;
8151#endif
8152 AppendCharToRedobuff(K_DEL);
8153}
8154
8155/*
8156 * Handle Backspace, delete-word and delete-line in Insert mode.
8157 * Return TRUE when backspace was actually used.
8158 */
8159 static int
8160ins_bs(c, mode, inserted_space_p)
8161 int c;
8162 int mode;
8163 int *inserted_space_p;
8164{
8165 linenr_T lnum;
8166 int cc;
8167 int temp = 0; /* init for GCC */
8168 colnr_T mincol;
8169 int did_backspace = FALSE;
8170 int in_indent;
8171 int oldState;
8172#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008173 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174#endif
8175
8176 /*
8177 * can't delete anything in an empty file
8178 * can't backup past first character in buffer
8179 * can't backup past starting point unless 'backspace' > 1
8180 * can backup to a previous line if 'backspace' == 0
8181 */
8182 if ( bufempty()
8183 || (
8184#ifdef FEAT_RIGHTLEFT
8185 !revins_on &&
8186#endif
8187 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8188 || (!can_bs(BS_START)
8189 && (arrow_used
8190 || (curwin->w_cursor.lnum == Insstart.lnum
8191 && curwin->w_cursor.col <= Insstart.col)))
8192 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8193 && curwin->w_cursor.col <= ai_col)
8194 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8195 {
8196 vim_beep();
8197 return FALSE;
8198 }
8199
8200 if (stop_arrow() == FAIL)
8201 return FALSE;
8202 in_indent = inindent(0);
8203#ifdef FEAT_CINDENT
8204 if (in_indent)
8205 can_cindent = FALSE;
8206#endif
8207#ifdef FEAT_COMMENTS
8208 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8209#endif
8210#ifdef FEAT_RIGHTLEFT
8211 if (revins_on) /* put cursor after last inserted char */
8212 inc_cursor();
8213#endif
8214
8215#ifdef FEAT_VIRTUALEDIT
8216 /* Virtualedit:
8217 * BACKSPACE_CHAR eats a virtual space
8218 * BACKSPACE_WORD eats all coladd
8219 * BACKSPACE_LINE eats all coladd and keeps going
8220 */
8221 if (curwin->w_cursor.coladd > 0)
8222 {
8223 if (mode == BACKSPACE_CHAR)
8224 {
8225 --curwin->w_cursor.coladd;
8226 return TRUE;
8227 }
8228 if (mode == BACKSPACE_WORD)
8229 {
8230 curwin->w_cursor.coladd = 0;
8231 return TRUE;
8232 }
8233 curwin->w_cursor.coladd = 0;
8234 }
8235#endif
8236
8237 /*
8238 * delete newline!
8239 */
8240 if (curwin->w_cursor.col == 0)
8241 {
8242 lnum = Insstart.lnum;
8243 if (curwin->w_cursor.lnum == Insstart.lnum
8244#ifdef FEAT_RIGHTLEFT
8245 || revins_on
8246#endif
8247 )
8248 {
8249 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8250 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8251 return FALSE;
8252 --Insstart.lnum;
8253 Insstart.col = MAXCOL;
8254 }
8255 /*
8256 * In replace mode:
8257 * cc < 0: NL was inserted, delete it
8258 * cc >= 0: NL was replaced, put original characters back
8259 */
8260 cc = -1;
8261 if (State & REPLACE_FLAG)
8262 cc = replace_pop(); /* returns -1 if NL was inserted */
8263 /*
8264 * In replace mode, in the line we started replacing, we only move the
8265 * cursor.
8266 */
8267 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8268 {
8269 dec_cursor();
8270 }
8271 else
8272 {
8273#ifdef FEAT_VREPLACE
8274 if (!(State & VREPLACE_FLAG)
8275 || curwin->w_cursor.lnum > orig_line_count)
8276#endif
8277 {
8278 temp = gchar_cursor(); /* remember current char */
8279 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008280
8281 /* When "aw" is in 'formatoptions' we must delete the space at
8282 * the end of the line, otherwise the line will be broken
8283 * again when auto-formatting. */
8284 if (has_format_option(FO_AUTO)
8285 && has_format_option(FO_WHITE_PAR))
8286 {
8287 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8288 TRUE);
8289 int len;
8290
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008291 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008292 if (len > 0 && ptr[len - 1] == ' ')
8293 ptr[len - 1] = NUL;
8294 }
8295
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 (void)do_join(FALSE);
8297 if (temp == NUL && gchar_cursor() != NUL)
8298 inc_cursor();
8299 }
8300#ifdef FEAT_VREPLACE
8301 else
8302 dec_cursor();
8303#endif
8304
8305 /*
8306 * In REPLACE mode we have to put back the text that was replaced
8307 * by the NL. On the replace stack is first a NUL-terminated
8308 * sequence of characters that were deleted and then the
8309 * characters that NL replaced.
8310 */
8311 if (State & REPLACE_FLAG)
8312 {
8313 /*
8314 * Do the next ins_char() in NORMAL state, to
8315 * prevent ins_char() from replacing characters and
8316 * avoiding showmatch().
8317 */
8318 oldState = State;
8319 State = NORMAL;
8320 /*
8321 * restore characters (blanks) deleted after cursor
8322 */
8323 while (cc > 0)
8324 {
8325 temp = curwin->w_cursor.col;
8326#ifdef FEAT_MBYTE
8327 mb_replace_pop_ins(cc);
8328#else
8329 ins_char(cc);
8330#endif
8331 curwin->w_cursor.col = temp;
8332 cc = replace_pop();
8333 }
8334 /* restore the characters that NL replaced */
8335 replace_pop_ins();
8336 State = oldState;
8337 }
8338 }
8339 did_ai = FALSE;
8340 }
8341 else
8342 {
8343 /*
8344 * Delete character(s) before the cursor.
8345 */
8346#ifdef FEAT_RIGHTLEFT
8347 if (revins_on) /* put cursor on last inserted char */
8348 dec_cursor();
8349#endif
8350 mincol = 0;
8351 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008352 if (mode == BACKSPACE_LINE
8353 && (curbuf->b_p_ai
8354#ifdef FEAT_CINDENT
8355 || cindent_on()
8356#endif
8357 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358#ifdef FEAT_RIGHTLEFT
8359 && !revins_on
8360#endif
8361 )
8362 {
8363 temp = curwin->w_cursor.col;
8364 beginline(BL_WHITE);
8365 if (curwin->w_cursor.col < (colnr_T)temp)
8366 mincol = curwin->w_cursor.col;
8367 curwin->w_cursor.col = temp;
8368 }
8369
8370 /*
8371 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8372 */
8373 if ( mode == BACKSPACE_CHAR
8374 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008375 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 && (*(ml_get_cursor() - 1) == TAB
8377 || (*(ml_get_cursor() - 1) == ' '
8378 && (!*inserted_space_p
8379 || arrow_used))))))
8380 {
8381 int ts;
8382 colnr_T vcol;
8383 colnr_T want_vcol;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008384#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 int extra = 0;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387
8388 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008389 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 ts = curbuf->b_p_sw;
8391 else
8392 ts = curbuf->b_p_sts;
8393 /* Compute the virtual column where we want to be. Since
8394 * 'showbreak' may get in the way, need to get the last column of
8395 * the previous character. */
8396 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8397 dec_cursor();
8398 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8399 inc_cursor();
8400 want_vcol = (want_vcol / ts) * ts;
8401
8402 /* delete characters until we are at or before want_vcol */
8403 while (vcol > want_vcol
8404 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
8405 {
8406 dec_cursor();
8407 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8408 if (State & REPLACE_FLAG)
8409 {
8410 /* Don't delete characters before the insert point when in
8411 * Replace mode */
8412 if (curwin->w_cursor.lnum != Insstart.lnum
8413 || curwin->w_cursor.col >= Insstart.col)
8414 {
8415#if 0 /* what was this for? It causes problems when sw != ts. */
8416 if (State == REPLACE && (int)vcol < want_vcol)
8417 {
8418 (void)del_char(FALSE);
8419 extra = 2; /* don't pop too much */
8420 }
8421 else
8422#endif
8423 replace_do_bs();
8424 }
8425 }
8426 else
8427 (void)del_char(FALSE);
8428 }
8429
8430 /* insert extra spaces until we are at want_vcol */
8431 while (vcol < want_vcol)
8432 {
8433 /* Remember the first char we inserted */
8434 if (curwin->w_cursor.lnum == Insstart.lnum
8435 && curwin->w_cursor.col < Insstart.col)
8436 Insstart.col = curwin->w_cursor.col;
8437
8438#ifdef FEAT_VREPLACE
8439 if (State & VREPLACE_FLAG)
8440 ins_char(' ');
8441 else
8442#endif
8443 {
8444 ins_str((char_u *)" ");
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008445 if ((State & REPLACE_FLAG) /* && extra <= 1 */)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008447#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 if (extra)
8449 replace_push_off(NUL);
8450 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 replace_push(NUL);
8453 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008454#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 if (extra == 2)
8456 extra = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 }
8459 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8460 }
8461 }
8462
8463 /*
8464 * Delete upto starting point, start of line or previous word.
8465 */
8466 else do
8467 {
8468#ifdef FEAT_RIGHTLEFT
8469 if (!revins_on) /* put cursor on char to be deleted */
8470#endif
8471 dec_cursor();
8472
8473 /* start of word? */
8474 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8475 {
8476 mode = BACKSPACE_WORD_NOT_SPACE;
8477 temp = vim_iswordc(gchar_cursor());
8478 }
8479 /* end of word? */
8480 else if (mode == BACKSPACE_WORD_NOT_SPACE
8481 && (vim_isspace(cc = gchar_cursor())
8482 || vim_iswordc(cc) != temp))
8483 {
8484#ifdef FEAT_RIGHTLEFT
8485 if (!revins_on)
8486#endif
8487 inc_cursor();
8488#ifdef FEAT_RIGHTLEFT
8489 else if (State & REPLACE_FLAG)
8490 dec_cursor();
8491#endif
8492 break;
8493 }
8494 if (State & REPLACE_FLAG)
8495 replace_do_bs();
8496 else
8497 {
8498#ifdef FEAT_MBYTE
8499 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008500 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501#endif
8502 (void)del_char(FALSE);
8503#ifdef FEAT_MBYTE
8504 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008505 * If there are combining characters and 'delcombine' is set
8506 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 * character.
8508 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008509 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 inc_cursor();
8511#endif
8512#ifdef FEAT_RIGHTLEFT
8513 if (revins_chars)
8514 {
8515 revins_chars--;
8516 revins_legal++;
8517 }
8518 if (revins_on && gchar_cursor() == NUL)
8519 break;
8520#endif
8521 }
8522 /* Just a single backspace?: */
8523 if (mode == BACKSPACE_CHAR)
8524 break;
8525 } while (
8526#ifdef FEAT_RIGHTLEFT
8527 revins_on ||
8528#endif
8529 (curwin->w_cursor.col > mincol
8530 && (curwin->w_cursor.lnum != Insstart.lnum
8531 || curwin->w_cursor.col != Insstart.col)));
8532 did_backspace = TRUE;
8533 }
8534#ifdef FEAT_SMARTINDENT
8535 did_si = FALSE;
8536 can_si = FALSE;
8537 can_si_back = FALSE;
8538#endif
8539 if (curwin->w_cursor.col <= 1)
8540 did_ai = FALSE;
8541 /*
8542 * It's a little strange to put backspaces into the redo
8543 * buffer, but it makes auto-indent a lot easier to deal
8544 * with.
8545 */
8546 AppendCharToRedobuff(c);
8547
8548 /* If deleted before the insertion point, adjust it */
8549 if (curwin->w_cursor.lnum == Insstart.lnum
8550 && curwin->w_cursor.col < Insstart.col)
8551 Insstart.col = curwin->w_cursor.col;
8552
8553 /* vi behaviour: the cursor moves backward but the character that
8554 * was there remains visible
8555 * Vim behaviour: the cursor moves backward and the character that
8556 * was there is erased from the screen.
8557 * We can emulate the vi behaviour by pretending there is a dollar
8558 * displayed even when there isn't.
8559 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8560 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8561 dollar_vcol = curwin->w_virtcol;
8562
8563 return did_backspace;
8564}
8565
8566#ifdef FEAT_MOUSE
8567 static void
8568ins_mouse(c)
8569 int c;
8570{
8571 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008572 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573
8574# ifdef FEAT_GUI
8575 /* When GUI is active, also move/paste when 'mouse' is empty */
8576 if (!gui.in_use)
8577# endif
8578 if (!mouse_has(MOUSE_INSERT))
8579 return;
8580
8581 undisplay_dollar();
8582 tpos = curwin->w_cursor;
8583 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8584 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008585#ifdef FEAT_WINDOWS
8586 win_T *new_curwin = curwin;
8587
8588 if (curwin != old_curwin && win_valid(old_curwin))
8589 {
8590 /* Mouse took us to another window. We need to go back to the
8591 * previous one to stop insert there properly. */
8592 curwin = old_curwin;
8593 curbuf = curwin->w_buffer;
8594 }
8595#endif
8596 start_arrow(curwin == old_curwin ? &tpos : NULL);
8597#ifdef FEAT_WINDOWS
8598 if (curwin != new_curwin && win_valid(new_curwin))
8599 {
8600 curwin = new_curwin;
8601 curbuf = curwin->w_buffer;
8602 }
8603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604# ifdef FEAT_CINDENT
8605 can_cindent = TRUE;
8606# endif
8607 }
8608
8609#ifdef FEAT_WINDOWS
8610 /* redraw status lines (in case another window became active) */
8611 redraw_statuslines();
8612#endif
8613}
8614
8615 static void
8616ins_mousescroll(up)
8617 int up;
8618{
8619 pos_T tpos;
8620# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8621 win_T *old_curwin;
8622# endif
8623
8624 tpos = curwin->w_cursor;
8625
8626# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8627 old_curwin = curwin;
8628
8629 /* Currently the mouse coordinates are only known in the GUI. */
8630 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8631 {
8632 int row, col;
8633
8634 row = mouse_row;
8635 col = mouse_col;
8636
8637 /* find the window at the pointer coordinates */
8638 curwin = mouse_find_win(&row, &col);
8639 curbuf = curwin->w_buffer;
8640 }
8641 if (curwin == old_curwin)
8642# endif
8643 undisplay_dollar();
8644
8645 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8646 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8647 else
8648 scroll_redraw(up, 3L);
8649
8650# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8651 curwin->w_redr_status = TRUE;
8652
8653 curwin = old_curwin;
8654 curbuf = curwin->w_buffer;
8655# endif
8656
8657 if (!equalpos(curwin->w_cursor, tpos))
8658 {
8659 start_arrow(&tpos);
8660# ifdef FEAT_CINDENT
8661 can_cindent = TRUE;
8662# endif
8663 }
8664}
8665#endif
8666
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008667#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008668 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008669ins_tabline(c)
8670 int c;
8671{
8672 /* We will be leaving the current window, unless closing another tab. */
8673 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8674 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8675 {
8676 undisplay_dollar();
8677 start_arrow(&curwin->w_cursor);
8678# ifdef FEAT_CINDENT
8679 can_cindent = TRUE;
8680# endif
8681 }
8682
8683 if (c == K_TABLINE)
8684 goto_tabpage(current_tab);
8685 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008686 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008687 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008688 redraw_statuslines(); /* will redraw the tabline when needed */
8689 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008690}
8691#endif
8692
8693#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 void
8695ins_scroll()
8696{
8697 pos_T tpos;
8698
8699 undisplay_dollar();
8700 tpos = curwin->w_cursor;
8701 if (gui_do_scroll())
8702 {
8703 start_arrow(&tpos);
8704# ifdef FEAT_CINDENT
8705 can_cindent = TRUE;
8706# endif
8707 }
8708}
8709
8710 void
8711ins_horscroll()
8712{
8713 pos_T tpos;
8714
8715 undisplay_dollar();
8716 tpos = curwin->w_cursor;
8717 if (gui_do_horiz_scroll())
8718 {
8719 start_arrow(&tpos);
8720# ifdef FEAT_CINDENT
8721 can_cindent = TRUE;
8722# endif
8723 }
8724}
8725#endif
8726
8727 static void
8728ins_left()
8729{
8730 pos_T tpos;
8731
8732#ifdef FEAT_FOLDING
8733 if ((fdo_flags & FDO_HOR) && KeyTyped)
8734 foldOpenCursor();
8735#endif
8736 undisplay_dollar();
8737 tpos = curwin->w_cursor;
8738 if (oneleft() == OK)
8739 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00008740#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8741 /* Only call start_arrow() when not busy with preediting, it will
8742 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8743 if (!im_is_preediting())
8744#endif
8745 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746#ifdef FEAT_RIGHTLEFT
8747 /* If exit reversed string, position is fixed */
8748 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8749 revins_legal++;
8750 revins_chars++;
8751#endif
8752 }
8753
8754 /*
8755 * if 'whichwrap' set for cursor in insert mode may go to
8756 * previous line
8757 */
8758 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8759 {
8760 start_arrow(&tpos);
8761 --(curwin->w_cursor.lnum);
8762 coladvance((colnr_T)MAXCOL);
8763 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8764 }
8765 else
8766 vim_beep();
8767}
8768
8769 static void
8770ins_home(c)
8771 int c;
8772{
8773 pos_T tpos;
8774
8775#ifdef FEAT_FOLDING
8776 if ((fdo_flags & FDO_HOR) && KeyTyped)
8777 foldOpenCursor();
8778#endif
8779 undisplay_dollar();
8780 tpos = curwin->w_cursor;
8781 if (c == K_C_HOME)
8782 curwin->w_cursor.lnum = 1;
8783 curwin->w_cursor.col = 0;
8784#ifdef FEAT_VIRTUALEDIT
8785 curwin->w_cursor.coladd = 0;
8786#endif
8787 curwin->w_curswant = 0;
8788 start_arrow(&tpos);
8789}
8790
8791 static void
8792ins_end(c)
8793 int c;
8794{
8795 pos_T tpos;
8796
8797#ifdef FEAT_FOLDING
8798 if ((fdo_flags & FDO_HOR) && KeyTyped)
8799 foldOpenCursor();
8800#endif
8801 undisplay_dollar();
8802 tpos = curwin->w_cursor;
8803 if (c == K_C_END)
8804 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8805 coladvance((colnr_T)MAXCOL);
8806 curwin->w_curswant = MAXCOL;
8807
8808 start_arrow(&tpos);
8809}
8810
8811 static void
8812ins_s_left()
8813{
8814#ifdef FEAT_FOLDING
8815 if ((fdo_flags & FDO_HOR) && KeyTyped)
8816 foldOpenCursor();
8817#endif
8818 undisplay_dollar();
8819 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8820 {
8821 start_arrow(&curwin->w_cursor);
8822 (void)bck_word(1L, FALSE, FALSE);
8823 curwin->w_set_curswant = TRUE;
8824 }
8825 else
8826 vim_beep();
8827}
8828
8829 static void
8830ins_right()
8831{
8832#ifdef FEAT_FOLDING
8833 if ((fdo_flags & FDO_HOR) && KeyTyped)
8834 foldOpenCursor();
8835#endif
8836 undisplay_dollar();
8837 if (gchar_cursor() != NUL || virtual_active()
8838 )
8839 {
8840 start_arrow(&curwin->w_cursor);
8841 curwin->w_set_curswant = TRUE;
8842#ifdef FEAT_VIRTUALEDIT
8843 if (virtual_active())
8844 oneright();
8845 else
8846#endif
8847 {
8848#ifdef FEAT_MBYTE
8849 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008850 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 else
8852#endif
8853 ++curwin->w_cursor.col;
8854 }
8855
8856#ifdef FEAT_RIGHTLEFT
8857 revins_legal++;
8858 if (revins_chars)
8859 revins_chars--;
8860#endif
8861 }
8862 /* if 'whichwrap' set for cursor in insert mode, may move the
8863 * cursor to the next line */
8864 else if (vim_strchr(p_ww, ']') != NULL
8865 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8866 {
8867 start_arrow(&curwin->w_cursor);
8868 curwin->w_set_curswant = TRUE;
8869 ++curwin->w_cursor.lnum;
8870 curwin->w_cursor.col = 0;
8871 }
8872 else
8873 vim_beep();
8874}
8875
8876 static void
8877ins_s_right()
8878{
8879#ifdef FEAT_FOLDING
8880 if ((fdo_flags & FDO_HOR) && KeyTyped)
8881 foldOpenCursor();
8882#endif
8883 undisplay_dollar();
8884 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8885 || gchar_cursor() != NUL)
8886 {
8887 start_arrow(&curwin->w_cursor);
8888 (void)fwd_word(1L, FALSE, 0);
8889 curwin->w_set_curswant = TRUE;
8890 }
8891 else
8892 vim_beep();
8893}
8894
8895 static void
8896ins_up(startcol)
8897 int startcol; /* when TRUE move to Insstart.col */
8898{
8899 pos_T tpos;
8900 linenr_T old_topline = curwin->w_topline;
8901#ifdef FEAT_DIFF
8902 int old_topfill = curwin->w_topfill;
8903#endif
8904
8905 undisplay_dollar();
8906 tpos = curwin->w_cursor;
8907 if (cursor_up(1L, TRUE) == OK)
8908 {
8909 if (startcol)
8910 coladvance(getvcol_nolist(&Insstart));
8911 if (old_topline != curwin->w_topline
8912#ifdef FEAT_DIFF
8913 || old_topfill != curwin->w_topfill
8914#endif
8915 )
8916 redraw_later(VALID);
8917 start_arrow(&tpos);
8918#ifdef FEAT_CINDENT
8919 can_cindent = TRUE;
8920#endif
8921 }
8922 else
8923 vim_beep();
8924}
8925
8926 static void
8927ins_pageup()
8928{
8929 pos_T tpos;
8930
8931 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008932
8933#ifdef FEAT_WINDOWS
8934 if (mod_mask & MOD_MASK_CTRL)
8935 {
8936 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00008937 if (first_tabpage->tp_next != NULL)
8938 {
8939 start_arrow(&curwin->w_cursor);
8940 goto_tabpage(-1);
8941 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008942 return;
8943 }
8944#endif
8945
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 tpos = curwin->w_cursor;
8947 if (onepage(BACKWARD, 1L) == OK)
8948 {
8949 start_arrow(&tpos);
8950#ifdef FEAT_CINDENT
8951 can_cindent = TRUE;
8952#endif
8953 }
8954 else
8955 vim_beep();
8956}
8957
8958 static void
8959ins_down(startcol)
8960 int startcol; /* when TRUE move to Insstart.col */
8961{
8962 pos_T tpos;
8963 linenr_T old_topline = curwin->w_topline;
8964#ifdef FEAT_DIFF
8965 int old_topfill = curwin->w_topfill;
8966#endif
8967
8968 undisplay_dollar();
8969 tpos = curwin->w_cursor;
8970 if (cursor_down(1L, TRUE) == OK)
8971 {
8972 if (startcol)
8973 coladvance(getvcol_nolist(&Insstart));
8974 if (old_topline != curwin->w_topline
8975#ifdef FEAT_DIFF
8976 || old_topfill != curwin->w_topfill
8977#endif
8978 )
8979 redraw_later(VALID);
8980 start_arrow(&tpos);
8981#ifdef FEAT_CINDENT
8982 can_cindent = TRUE;
8983#endif
8984 }
8985 else
8986 vim_beep();
8987}
8988
8989 static void
8990ins_pagedown()
8991{
8992 pos_T tpos;
8993
8994 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008995
8996#ifdef FEAT_WINDOWS
8997 if (mod_mask & MOD_MASK_CTRL)
8998 {
8999 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009000 if (first_tabpage->tp_next != NULL)
9001 {
9002 start_arrow(&curwin->w_cursor);
9003 goto_tabpage(0);
9004 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009005 return;
9006 }
9007#endif
9008
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 tpos = curwin->w_cursor;
9010 if (onepage(FORWARD, 1L) == OK)
9011 {
9012 start_arrow(&tpos);
9013#ifdef FEAT_CINDENT
9014 can_cindent = TRUE;
9015#endif
9016 }
9017 else
9018 vim_beep();
9019}
9020
9021#ifdef FEAT_DND
9022 static void
9023ins_drop()
9024{
9025 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9026}
9027#endif
9028
9029/*
9030 * Handle TAB in Insert or Replace mode.
9031 * Return TRUE when the TAB needs to be inserted like a normal character.
9032 */
9033 static int
9034ins_tab()
9035{
9036 int ind;
9037 int i;
9038 int temp;
9039
9040 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9041 Insstart_blank_vcol = get_nolist_virtcol();
9042 if (echeck_abbr(TAB + ABBR_OFF))
9043 return FALSE;
9044
9045 ind = inindent(0);
9046#ifdef FEAT_CINDENT
9047 if (ind)
9048 can_cindent = FALSE;
9049#endif
9050
9051 /*
9052 * When nothing special, insert TAB like a normal character
9053 */
9054 if (!curbuf->b_p_et
9055 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9056 && curbuf->b_p_sts == 0)
9057 return TRUE;
9058
9059 if (stop_arrow() == FAIL)
9060 return TRUE;
9061
9062 did_ai = FALSE;
9063#ifdef FEAT_SMARTINDENT
9064 did_si = FALSE;
9065 can_si = FALSE;
9066 can_si_back = FALSE;
9067#endif
9068 AppendToRedobuff((char_u *)"\t");
9069
9070 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9071 temp = (int)curbuf->b_p_sw;
9072 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9073 temp = (int)curbuf->b_p_sts;
9074 else /* otherwise use 'tabstop' */
9075 temp = (int)curbuf->b_p_ts;
9076 temp -= get_nolist_virtcol() % temp;
9077
9078 /*
9079 * Insert the first space with ins_char(). It will delete one char in
9080 * replace mode. Insert the rest with ins_str(); it will not delete any
9081 * chars. For VREPLACE mode, we use ins_char() for all characters.
9082 */
9083 ins_char(' ');
9084 while (--temp > 0)
9085 {
9086#ifdef FEAT_VREPLACE
9087 if (State & VREPLACE_FLAG)
9088 ins_char(' ');
9089 else
9090#endif
9091 {
9092 ins_str((char_u *)" ");
9093 if (State & REPLACE_FLAG) /* no char replaced */
9094 replace_push(NUL);
9095 }
9096 }
9097
9098 /*
9099 * When 'expandtab' not set: Replace spaces by TABs where possible.
9100 */
9101 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9102 {
9103 char_u *ptr;
9104#ifdef FEAT_VREPLACE
9105 char_u *saved_line = NULL; /* init for GCC */
9106 pos_T pos;
9107#endif
9108 pos_T fpos;
9109 pos_T *cursor;
9110 colnr_T want_vcol, vcol;
9111 int change_col = -1;
9112 int save_list = curwin->w_p_list;
9113
9114 /*
9115 * Get the current line. For VREPLACE mode, don't make real changes
9116 * yet, just work on a copy of the line.
9117 */
9118#ifdef FEAT_VREPLACE
9119 if (State & VREPLACE_FLAG)
9120 {
9121 pos = curwin->w_cursor;
9122 cursor = &pos;
9123 saved_line = vim_strsave(ml_get_curline());
9124 if (saved_line == NULL)
9125 return FALSE;
9126 ptr = saved_line + pos.col;
9127 }
9128 else
9129#endif
9130 {
9131 ptr = ml_get_cursor();
9132 cursor = &curwin->w_cursor;
9133 }
9134
9135 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9136 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9137 curwin->w_p_list = FALSE;
9138
9139 /* Find first white before the cursor */
9140 fpos = curwin->w_cursor;
9141 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9142 {
9143 --fpos.col;
9144 --ptr;
9145 }
9146
9147 /* In Replace mode, don't change characters before the insert point. */
9148 if ((State & REPLACE_FLAG)
9149 && fpos.lnum == Insstart.lnum
9150 && fpos.col < Insstart.col)
9151 {
9152 ptr += Insstart.col - fpos.col;
9153 fpos.col = Insstart.col;
9154 }
9155
9156 /* compute virtual column numbers of first white and cursor */
9157 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9158 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9159
9160 /* Use as many TABs as possible. Beware of 'showbreak' and
9161 * 'linebreak' adding extra virtual columns. */
9162 while (vim_iswhite(*ptr))
9163 {
9164 i = lbr_chartabsize((char_u *)"\t", vcol);
9165 if (vcol + i > want_vcol)
9166 break;
9167 if (*ptr != TAB)
9168 {
9169 *ptr = TAB;
9170 if (change_col < 0)
9171 {
9172 change_col = fpos.col; /* Column of first change */
9173 /* May have to adjust Insstart */
9174 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9175 Insstart.col = fpos.col;
9176 }
9177 }
9178 ++fpos.col;
9179 ++ptr;
9180 vcol += i;
9181 }
9182
9183 if (change_col >= 0)
9184 {
9185 int repl_off = 0;
9186
9187 /* Skip over the spaces we need. */
9188 while (vcol < want_vcol && *ptr == ' ')
9189 {
9190 vcol += lbr_chartabsize(ptr, vcol);
9191 ++ptr;
9192 ++repl_off;
9193 }
9194 if (vcol > want_vcol)
9195 {
9196 /* Must have a char with 'showbreak' just before it. */
9197 --ptr;
9198 --repl_off;
9199 }
9200 fpos.col += repl_off;
9201
9202 /* Delete following spaces. */
9203 i = cursor->col - fpos.col;
9204 if (i > 0)
9205 {
9206 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
9207 /* correct replace stack. */
9208 if ((State & REPLACE_FLAG)
9209#ifdef FEAT_VREPLACE
9210 && !(State & VREPLACE_FLAG)
9211#endif
9212 )
9213 for (temp = i; --temp >= 0; )
9214 replace_join(repl_off);
9215 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009216#ifdef FEAT_NETBEANS_INTG
9217 if (usingNetbeans)
9218 {
9219 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9220 (long)(i + 1));
9221 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9222 (char_u *)"\t", 1);
9223 }
9224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 cursor->col -= i;
9226
9227#ifdef FEAT_VREPLACE
9228 /*
9229 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9230 * backspacing over the changed spacing and then inserting the new
9231 * spacing.
9232 */
9233 if (State & VREPLACE_FLAG)
9234 {
9235 /* Backspace from real cursor to change_col */
9236 backspace_until_column(change_col);
9237
9238 /* Insert each char in saved_line from changed_col to
9239 * ptr-cursor */
9240 ins_bytes_len(saved_line + change_col,
9241 cursor->col - change_col);
9242 }
9243#endif
9244 }
9245
9246#ifdef FEAT_VREPLACE
9247 if (State & VREPLACE_FLAG)
9248 vim_free(saved_line);
9249#endif
9250 curwin->w_p_list = save_list;
9251 }
9252
9253 return FALSE;
9254}
9255
9256/*
9257 * Handle CR or NL in insert mode.
9258 * Return TRUE when out of memory or can't undo.
9259 */
9260 static int
9261ins_eol(c)
9262 int c;
9263{
9264 int i;
9265
9266 if (echeck_abbr(c + ABBR_OFF))
9267 return FALSE;
9268 if (stop_arrow() == FAIL)
9269 return TRUE;
9270 undisplay_dollar();
9271
9272 /*
9273 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9274 * character under the cursor. Only push a NUL on the replace stack,
9275 * nothing to put back when the NL is deleted.
9276 */
9277 if ((State & REPLACE_FLAG)
9278#ifdef FEAT_VREPLACE
9279 && !(State & VREPLACE_FLAG)
9280#endif
9281 )
9282 replace_push(NUL);
9283
9284 /*
9285 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9286 * replacing the next line, so we push all of the characters left on the
9287 * line onto the replace stack. This is not done here though, it is done
9288 * in open_line().
9289 */
9290
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009291#ifdef FEAT_VIRTUALEDIT
9292 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9293 * CTRL-O). */
9294 if (virtual_active() && curwin->w_cursor.coladd > 0)
9295 coladvance(getviscol());
9296#endif
9297
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298#ifdef FEAT_RIGHTLEFT
9299# ifdef FEAT_FKMAP
9300 if (p_altkeymap && p_fkmap)
9301 fkmap(NL);
9302# endif
9303 /* NL in reverse insert will always start in the end of
9304 * current line. */
9305 if (revins_on)
9306 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9307#endif
9308
9309 AppendToRedobuff(NL_STR);
9310 i = open_line(FORWARD,
9311#ifdef FEAT_COMMENTS
9312 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9313#endif
9314 0, old_indent);
9315 old_indent = 0;
9316#ifdef FEAT_CINDENT
9317 can_cindent = TRUE;
9318#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009319#ifdef FEAT_FOLDING
9320 /* When inserting a line the cursor line must never be in a closed fold. */
9321 foldOpenCursor();
9322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323
9324 return (!i);
9325}
9326
9327#ifdef FEAT_DIGRAPHS
9328/*
9329 * Handle digraph in insert mode.
9330 * Returns character still to be inserted, or NUL when nothing remaining to be
9331 * done.
9332 */
9333 static int
9334ins_digraph()
9335{
9336 int c;
9337 int cc;
9338
9339 pc_status = PC_STATUS_UNSET;
9340 if (redrawing() && !char_avail())
9341 {
9342 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009343 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344
9345 edit_putchar('?', TRUE);
9346#ifdef FEAT_CMDL_INFO
9347 add_to_showcmd_c(Ctrl_K);
9348#endif
9349 }
9350
9351#ifdef USE_ON_FLY_SCROLL
9352 dont_scroll = TRUE; /* disallow scrolling here */
9353#endif
9354
9355 /* don't map the digraph chars. This also prevents the
9356 * mode message to be deleted when ESC is hit */
9357 ++no_mapping;
9358 ++allow_keys;
9359 c = safe_vgetc();
9360 --no_mapping;
9361 --allow_keys;
9362 if (IS_SPECIAL(c) || mod_mask) /* special key */
9363 {
9364#ifdef FEAT_CMDL_INFO
9365 clear_showcmd();
9366#endif
9367 insert_special(c, TRUE, FALSE);
9368 return NUL;
9369 }
9370 if (c != ESC)
9371 {
9372 if (redrawing() && !char_avail())
9373 {
9374 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009375 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376
9377 if (char2cells(c) == 1)
9378 {
9379 /* first remove the '?', otherwise it's restored when typing
9380 * an ESC next */
9381 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009382 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 edit_putchar(c, TRUE);
9384 }
9385#ifdef FEAT_CMDL_INFO
9386 add_to_showcmd_c(c);
9387#endif
9388 }
9389 ++no_mapping;
9390 ++allow_keys;
9391 cc = safe_vgetc();
9392 --no_mapping;
9393 --allow_keys;
9394 if (cc != ESC)
9395 {
9396 AppendToRedobuff((char_u *)CTRL_V_STR);
9397 c = getdigraph(c, cc, TRUE);
9398#ifdef FEAT_CMDL_INFO
9399 clear_showcmd();
9400#endif
9401 return c;
9402 }
9403 }
9404 edit_unputchar();
9405#ifdef FEAT_CMDL_INFO
9406 clear_showcmd();
9407#endif
9408 return NUL;
9409}
9410#endif
9411
9412/*
9413 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9414 * Returns the char to be inserted, or NUL if none found.
9415 */
9416 static int
9417ins_copychar(lnum)
9418 linenr_T lnum;
9419{
9420 int c;
9421 int temp;
9422 char_u *ptr, *prev_ptr;
9423
9424 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9425 {
9426 vim_beep();
9427 return NUL;
9428 }
9429
9430 /* try to advance to the cursor column */
9431 temp = 0;
9432 ptr = ml_get(lnum);
9433 prev_ptr = ptr;
9434 validate_virtcol();
9435 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9436 {
9437 prev_ptr = ptr;
9438 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9439 }
9440 if ((colnr_T)temp > curwin->w_virtcol)
9441 ptr = prev_ptr;
9442
9443#ifdef FEAT_MBYTE
9444 c = (*mb_ptr2char)(ptr);
9445#else
9446 c = *ptr;
9447#endif
9448 if (c == NUL)
9449 vim_beep();
9450 return c;
9451}
9452
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009453/*
9454 * CTRL-Y or CTRL-E typed in Insert mode.
9455 */
9456 static int
9457ins_ctrl_ey(tc)
9458 int tc;
9459{
9460 int c = tc;
9461
9462#ifdef FEAT_INS_EXPAND
9463 if (ctrl_x_mode == CTRL_X_SCROLL)
9464 {
9465 if (c == Ctrl_Y)
9466 scrolldown_clamp();
9467 else
9468 scrollup_clamp();
9469 redraw_later(VALID);
9470 }
9471 else
9472#endif
9473 {
9474 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9475 if (c != NUL)
9476 {
9477 long tw_save;
9478
9479 /* The character must be taken literally, insert like it
9480 * was typed after a CTRL-V, and pretend 'textwidth'
9481 * wasn't set. Digits, 'o' and 'x' are special after a
9482 * CTRL-V, don't use it for these. */
9483 if (c < 256 && !isalnum(c))
9484 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9485 tw_save = curbuf->b_p_tw;
9486 curbuf->b_p_tw = -1;
9487 insert_special(c, TRUE, FALSE);
9488 curbuf->b_p_tw = tw_save;
9489#ifdef FEAT_RIGHTLEFT
9490 revins_chars++;
9491 revins_legal++;
9492#endif
9493 c = Ctrl_V; /* pretend CTRL-V is last character */
9494 auto_format(FALSE, TRUE);
9495 }
9496 }
9497 return c;
9498}
9499
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500#ifdef FEAT_SMARTINDENT
9501/*
9502 * Try to do some very smart auto-indenting.
9503 * Used when inserting a "normal" character.
9504 */
9505 static void
9506ins_try_si(c)
9507 int c;
9508{
9509 pos_T *pos, old_pos;
9510 char_u *ptr;
9511 int i;
9512 int temp;
9513
9514 /*
9515 * do some very smart indenting when entering '{' or '}'
9516 */
9517 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9518 {
9519 /*
9520 * for '}' set indent equal to indent of line containing matching '{'
9521 */
9522 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9523 {
9524 old_pos = curwin->w_cursor;
9525 /*
9526 * If the matching '{' has a ')' immediately before it (ignoring
9527 * white-space), then line up with the start of the line
9528 * containing the matching '(' if there is one. This handles the
9529 * case where an "if (..\n..) {" statement continues over multiple
9530 * lines -- webb
9531 */
9532 ptr = ml_get(pos->lnum);
9533 i = pos->col;
9534 if (i > 0) /* skip blanks before '{' */
9535 while (--i > 0 && vim_iswhite(ptr[i]))
9536 ;
9537 curwin->w_cursor.lnum = pos->lnum;
9538 curwin->w_cursor.col = i;
9539 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9540 curwin->w_cursor = *pos;
9541 i = get_indent();
9542 curwin->w_cursor = old_pos;
9543#ifdef FEAT_VREPLACE
9544 if (State & VREPLACE_FLAG)
9545 change_indent(INDENT_SET, i, FALSE, NUL);
9546 else
9547#endif
9548 (void)set_indent(i, SIN_CHANGED);
9549 }
9550 else if (curwin->w_cursor.col > 0)
9551 {
9552 /*
9553 * when inserting '{' after "O" reduce indent, but not
9554 * more than indent of previous line
9555 */
9556 temp = TRUE;
9557 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9558 {
9559 old_pos = curwin->w_cursor;
9560 i = get_indent();
9561 while (curwin->w_cursor.lnum > 1)
9562 {
9563 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9564
9565 /* ignore empty lines and lines starting with '#'. */
9566 if (*ptr != '#' && *ptr != NUL)
9567 break;
9568 }
9569 if (get_indent() >= i)
9570 temp = FALSE;
9571 curwin->w_cursor = old_pos;
9572 }
9573 if (temp)
9574 shift_line(TRUE, FALSE, 1);
9575 }
9576 }
9577
9578 /*
9579 * set indent of '#' always to 0
9580 */
9581 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9582 {
9583 /* remember current indent for next line */
9584 old_indent = get_indent();
9585 (void)set_indent(0, SIN_CHANGED);
9586 }
9587
9588 /* Adjust ai_col, the char at this position can be deleted. */
9589 if (ai_col > curwin->w_cursor.col)
9590 ai_col = curwin->w_cursor.col;
9591}
9592#endif
9593
9594/*
9595 * Get the value that w_virtcol would have when 'list' is off.
9596 * Unless 'cpo' contains the 'L' flag.
9597 */
9598 static colnr_T
9599get_nolist_virtcol()
9600{
9601 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9602 return getvcol_nolist(&curwin->w_cursor);
9603 validate_virtcol();
9604 return curwin->w_virtcol;
9605}