blob: 46b496ba4592c7ec7b72815cbeeac730dc58e021 [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
2058 * text is infered, ie this tries to work out what case you probably wanted
2059 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002060 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 */
2062 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002063ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 char_u *str;
2065 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002066 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 char_u *fname;
2068 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002069 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070{
2071 int has_lower = FALSE;
2072 int was_letter = FALSE;
2073 int idx;
2074
2075 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2076 {
2077 /* Infer case of completed part -- webb */
2078 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002079 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
2081 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002082 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002084 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
2086 has_lower = TRUE;
2087 if (isupper(IObuff[idx]))
2088 {
2089 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002090 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2092 break;
2093 }
2094 }
2095 }
2096
2097 /*
2098 * Rule 2: No lower case, 2nd consecutive letter converted to
2099 * upper case.
2100 */
2101 if (!has_lower)
2102 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002103 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002105 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 && islower(IObuff[idx]))
2107 {
2108 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002109 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2111 break;
2112 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002113 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 }
2115 }
2116
2117 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002118 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002120 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2121 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002123 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124}
2125
2126/*
2127 * Add a match to the list of matches.
2128 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002129 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002130 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002132 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002133ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 char_u *str;
2135 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002136 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002138 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002139 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002140 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002141 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002143 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002144 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145
2146 ui_breakcheck();
2147 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002148 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 if (len < 0)
2150 len = (int)STRLEN(str);
2151
2152 /*
2153 * If the same match is already present, don't add it.
2154 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002155 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002157 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 do
2159 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002160 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002161 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002162 && match->cp_str[len] == NUL)
2163 return NOTDONE;
2164 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002165 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
2167
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002168 /* Remove any popup menu before changing the list of matches. */
2169 ins_compl_del_pum();
2170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 /*
2172 * Allocate a new match structure.
2173 * Copy the values to the new match structure.
2174 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002175 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002177 return FAIL;
2178 match->cp_number = -1;
2179 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002180 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002181 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 {
2183 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002184 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002186 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002187
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002189 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2191 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002192 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002193 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002194 && compl_curr_match->cp_fname != NULL
2195 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002196 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002197 else if (fname != NULL)
2198 {
2199 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002200 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002203 match->cp_fname = NULL;
2204 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002205
2206 if (cptext != NULL)
2207 {
2208 int i;
2209
2210 for (i = 0; i < CPT_COUNT; ++i)
2211 if (cptext[i] != NULL && *cptext[i] != NUL)
2212 match->cp_text[i] = vim_strsave(cptext[i]);
2213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214
2215 /*
2216 * Link the new match structure in the list of matches.
2217 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002218 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002219 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 else if (dir == FORWARD)
2221 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002222 match->cp_next = compl_curr_match->cp_next;
2223 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
2225 else /* BACKWARD */
2226 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002227 match->cp_next = compl_curr_match;
2228 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002230 if (match->cp_next)
2231 match->cp_next->cp_prev = match;
2232 if (match->cp_prev)
2233 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002235 compl_first_match = match;
2236 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002238 /*
2239 * Find the longest common string if still doing that.
2240 */
2241 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2242 ins_compl_longest_match(match);
2243
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 return OK;
2245}
2246
2247/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002248 * Return TRUE if "str[len]" matches with match->cp_str, considering
2249 * match->cp_icase.
2250 */
2251 static int
2252ins_compl_equal(match, str, len)
2253 compl_T *match;
2254 char_u *str;
2255 int len;
2256{
2257 if (match->cp_icase)
2258 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2259 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2260}
2261
2262/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002263 * Reduce the longest common string for match "match".
2264 */
2265 static void
2266ins_compl_longest_match(match)
2267 compl_T *match;
2268{
2269 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002270 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002271 int had_match;
2272
2273 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002274 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002275 /* First match, use it as a whole. */
2276 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002277 if (compl_leader != NULL)
2278 {
2279 had_match = (curwin->w_cursor.col > compl_col);
2280 ins_compl_delete();
2281 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2282 ins_redraw(FALSE);
2283
2284 /* When the match isn't there (to avoid matching itself) remove it
2285 * again after redrawing. */
2286 if (!had_match)
2287 ins_compl_delete();
2288 compl_used_match = FALSE;
2289 }
2290 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002291 else
2292 {
2293 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002294 p = compl_leader;
2295 s = match->cp_str;
2296 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002297 {
2298#ifdef FEAT_MBYTE
2299 if (has_mbyte)
2300 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002301 c1 = mb_ptr2char(p);
2302 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002303 }
2304 else
2305#endif
2306 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002307 c1 = *p;
2308 c2 = *s;
2309 }
2310 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2311 : (c1 != c2))
2312 break;
2313#ifdef FEAT_MBYTE
2314 if (has_mbyte)
2315 {
2316 mb_ptr_adv(p);
2317 mb_ptr_adv(s);
2318 }
2319 else
2320#endif
2321 {
2322 ++p;
2323 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002324 }
2325 }
2326
2327 if (*p != NUL)
2328 {
2329 /* Leader was shortened, need to change the inserted text. */
2330 *p = NUL;
2331 had_match = (curwin->w_cursor.col > compl_col);
2332 ins_compl_delete();
2333 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2334 ins_redraw(FALSE);
2335
2336 /* When the match isn't there (to avoid matching itself) remove it
2337 * again after redrawing. */
2338 if (!had_match)
2339 ins_compl_delete();
2340 }
2341
2342 compl_used_match = FALSE;
2343 }
2344}
2345
2346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 * Add an array of matches to the list of matches.
2348 * Frees matches[].
2349 */
2350 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002351ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 int num_matches;
2353 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002354 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355{
2356 int i;
2357 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002358 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
Bram Moolenaar572cb562005-08-05 21:35:02 +00002360 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002361 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002362 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002364 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 FreeWild(num_matches, matches);
2366}
2367
2368/* Make the completion list cyclic.
2369 * Return the number of matches (excluding the original).
2370 */
2371 static int
2372ins_compl_make_cyclic()
2373{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002374 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 int count = 0;
2376
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002377 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 {
2379 /*
2380 * Find the end of the list.
2381 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002382 match = compl_first_match;
2383 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002384 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002386 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 ++count;
2388 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002389 match->cp_next = compl_first_match;
2390 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392 return count;
2393}
2394
Bram Moolenaara94bc432006-03-10 21:42:59 +00002395/*
2396 * Start completion for the complete() function.
2397 * "startcol" is where the matched text starts (1 is first column).
2398 * "list" is the list of matches.
2399 */
2400 void
2401set_completion(startcol, list)
2402 int startcol;
2403 list_T *list;
2404{
2405 /* If already doing completions stop it. */
2406 if (ctrl_x_mode != 0)
2407 ins_compl_prep(' ');
2408 ins_compl_clear();
2409
2410 if (stop_arrow() == FAIL)
2411 return;
2412
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002413 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002414 startcol = curwin->w_cursor.col;
2415 compl_col = startcol;
2416 compl_length = curwin->w_cursor.col - startcol;
2417 /* compl_pattern doesn't need to be set */
2418 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2419 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002420 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002421 return;
2422
2423 /* Handle like dictionary completion. */
2424 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2425
2426 ins_compl_add_list(list);
2427 compl_matches = ins_compl_make_cyclic();
2428 compl_started = TRUE;
2429 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002430 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002431
2432 compl_curr_match = compl_first_match;
2433 ins_complete(Ctrl_N);
2434 out_flush();
2435}
2436
2437
Bram Moolenaar9372a112005-12-06 19:59:18 +00002438/* "compl_match_array" points the currently displayed list of entries in the
2439 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002440static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002441static int compl_match_arraysize;
2442
2443/*
2444 * Update the screen and when there is any scrolling remove the popup menu.
2445 */
2446 static void
2447ins_compl_upd_pum()
2448{
2449 int h;
2450
2451 if (compl_match_array != NULL)
2452 {
2453 h = curwin->w_cline_height;
2454 update_screen(0);
2455 if (h != curwin->w_cline_height)
2456 ins_compl_del_pum();
2457 }
2458}
2459
2460/*
2461 * Remove any popup menu.
2462 */
2463 static void
2464ins_compl_del_pum()
2465{
2466 if (compl_match_array != NULL)
2467 {
2468 pum_undisplay();
2469 vim_free(compl_match_array);
2470 compl_match_array = NULL;
2471 }
2472}
2473
2474/*
2475 * Return TRUE if the popup menu should be displayed.
2476 */
2477 static int
2478pum_wanted()
2479{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002480 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002481 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002482 return FALSE;
2483
2484 /* The display looks bad on a B&W display. */
2485 if (t_colors < 8
2486#ifdef FEAT_GUI
2487 && !gui.in_use
2488#endif
2489 )
2490 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002491 return TRUE;
2492}
2493
2494/*
2495 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002496 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002497 */
2498 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002499pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002500{
2501 compl_T *compl;
2502 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002503
2504 /* Don't display the popup menu if there are no matches or there is only
2505 * one (ignoring the original text). */
2506 compl = compl_first_match;
2507 i = 0;
2508 do
2509 {
2510 if (compl == NULL
2511 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2512 break;
2513 compl = compl->cp_next;
2514 } while (compl != compl_first_match);
2515
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002516 if (strstr((char *)p_cot, "menuone") != NULL)
2517 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002518 return (i >= 2);
2519}
2520
2521/*
2522 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002523 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002524 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002525 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002526ins_compl_show_pum()
2527{
2528 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002529 compl_T *shown_compl = NULL;
2530 int did_find_shown_match = FALSE;
2531 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002532 int i;
2533 int cur = -1;
2534 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002535 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002536
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002537 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002538 return;
2539
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002540#if defined(FEAT_EVAL)
2541 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2542 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2543#endif
2544
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002545 /* Update the screen before drawing the popup menu over it. */
2546 update_screen(0);
2547
2548 if (compl_match_array == NULL)
2549 {
2550 /* Need to build the popup menu list. */
2551 compl_match_arraysize = 0;
2552 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002553 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002554 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002555 do
2556 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002557 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2558 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002559 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002560 ++compl_match_arraysize;
2561 compl = compl->cp_next;
2562 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002563 if (compl_match_arraysize == 0)
2564 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002565 compl_match_array = (pumitem_T *)alloc_clear(
2566 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002567 * compl_match_arraysize));
2568 if (compl_match_array != NULL)
2569 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002570 /* If the current match is the original text don't find the first
2571 * match after it, don't highlight anything. */
2572 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2573 shown_match_ok = TRUE;
2574
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002575 i = 0;
2576 compl = compl_first_match;
2577 do
2578 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002579 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2580 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002581 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002582 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002583 if (!shown_match_ok)
2584 {
2585 if (compl == compl_shown_match || did_find_shown_match)
2586 {
2587 /* This item is the shown match or this is the
2588 * first displayed item after the shown match. */
2589 compl_shown_match = compl;
2590 did_find_shown_match = TRUE;
2591 shown_match_ok = TRUE;
2592 }
2593 else
2594 /* Remember this displayed match for when the
2595 * shown match is just below it. */
2596 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002597 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002598 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002599
2600 if (compl->cp_text[CPT_ABBR] != NULL)
2601 compl_match_array[i].pum_text =
2602 compl->cp_text[CPT_ABBR];
2603 else
2604 compl_match_array[i].pum_text = compl->cp_str;
2605 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2606 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2607 if (compl->cp_text[CPT_MENU] != NULL)
2608 compl_match_array[i++].pum_extra =
2609 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002610 else
2611 compl_match_array[i++].pum_extra = compl->cp_fname;
2612 }
2613
2614 if (compl == compl_shown_match)
2615 {
2616 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002617
2618 /* When the original text is the shown match don't set
2619 * compl_shown_match. */
2620 if (compl->cp_flags & ORIGINAL_TEXT)
2621 shown_match_ok = TRUE;
2622
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002623 if (!shown_match_ok && shown_compl != NULL)
2624 {
2625 /* The shown match isn't displayed, set it to the
2626 * previously displayed match. */
2627 compl_shown_match = shown_compl;
2628 shown_match_ok = TRUE;
2629 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002630 }
2631 compl = compl->cp_next;
2632 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002633
2634 if (!shown_match_ok) /* no displayed match at all */
2635 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002636 }
2637 }
2638 else
2639 {
2640 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002641 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002642 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2643 || compl_match_array[i].pum_text
2644 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002645 {
2646 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002647 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002648 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002649 }
2650
2651 if (compl_match_array != NULL)
2652 {
2653 /* Compute the screen column of the start of the completed text.
2654 * Use the cursor to get all wrapping and other settings right. */
2655 col = curwin->w_cursor.col;
2656 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002657 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002658 curwin->w_cursor.col = col;
2659 }
2660}
2661
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662#define DICT_FIRST (1) /* use just first element in "dict" */
2663#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002666 * Add any identifiers that match the given pattern in the list of dictionary
2667 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 */
2669 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002670ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2671 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002673 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002674 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002676 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 char_u *ptr;
2678 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 char_u **files;
2681 int count;
2682 int i;
2683 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002684 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
Bram Moolenaar0b238792006-03-02 22:49:12 +00002686 if (*dict == NUL)
2687 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002688#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002689 /* When 'dictionary' is empty and spell checking is enabled use
2690 * "spell". */
2691 if (!thesaurus && curwin->w_p_spell)
2692 dict = (char_u *)"spell";
2693 else
2694#endif
2695 return;
2696 }
2697
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002699 if (buf == NULL)
2700 return;
2701
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 /* If 'infercase' is set, don't use 'smartcase' here */
2703 save_p_scs = p_scs;
2704 if (curbuf->b_p_inf)
2705 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002706
2707 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2708 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002709 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002710 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2711 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002712 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
2713
2714 if (pat_esc == NULL)
2715 return ;
2716 i = (int)STRLEN(pat_esc) + 10;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002717 ptr = alloc(i);
2718 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002719 {
2720 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002721 return;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002722 }
2723 vim_snprintf((char *)ptr, i, "^\\s*\\zs\\V%s", pat_esc);
2724 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2725 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002726 vim_free(ptr);
2727 }
2728 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002729 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002730 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002731 if (regmatch.regprog == NULL)
2732 goto theend;
2733 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002734
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2736 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002737 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 {
2739 /* copy one dictionary file name into buf */
2740 if (flags == DICT_EXACT)
2741 {
2742 count = 1;
2743 files = &dict;
2744 }
2745 else
2746 {
2747 /* Expand wildcards in the dictionary name, but do not allow
2748 * backticks (for security, the 'dict' option may have been set in
2749 * a modeline). */
2750 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002751# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002752 if (!thesaurus && STRCMP(buf, "spell") == 0)
2753 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002754 else
2755# endif
2756 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 || expand_wildcards(1, &buf, &count, &files,
2758 EW_FILE|EW_SILENT) != OK)
2759 count = 0;
2760 }
2761
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002762# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002763 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002765 /* Complete from active spelling. Skip "\<" in the pattern, we
2766 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002767 if (pat[0] == '\\' && pat[1] == '<')
2768 ptr = pat + 2;
2769 else
2770 ptr = pat;
2771 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002773 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002774# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00002775 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002776 {
2777 ins_compl_files(count, files, thesaurus, flags,
2778 &regmatch, buf, &dir);
2779 if (flags != DICT_EXACT)
2780 FreeWild(count, files);
2781 }
2782 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 break;
2784 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002785
2786theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 p_scs = save_p_scs;
2788 vim_free(regmatch.regprog);
2789 vim_free(buf);
2790}
2791
Bram Moolenaar0b238792006-03-02 22:49:12 +00002792 static void
2793ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2794 int count;
2795 char_u **files;
2796 int thesaurus;
2797 int flags;
2798 regmatch_T *regmatch;
2799 char_u *buf;
2800 int *dir;
2801{
2802 char_u *ptr;
2803 int i;
2804 FILE *fp;
2805 int add_r;
2806
2807 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2808 {
2809 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2810 if (flags != DICT_EXACT)
2811 {
2812 vim_snprintf((char *)IObuff, IOSIZE,
2813 _("Scanning dictionary: %s"), (char *)files[i]);
2814 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2815 }
2816
2817 if (fp != NULL)
2818 {
2819 /*
2820 * Read dictionary file line by line.
2821 * Check each line for a match.
2822 */
2823 while (!got_int && !compl_interrupted
2824 && !vim_fgets(buf, LSIZE, fp))
2825 {
2826 ptr = buf;
2827 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2828 {
2829 ptr = regmatch->startp[0];
2830 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2831 ptr = find_line_end(ptr);
2832 else
2833 ptr = find_word_end(ptr);
2834 add_r = ins_compl_add_infercase(regmatch->startp[0],
2835 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002836 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002837 if (thesaurus)
2838 {
2839 char_u *wstart;
2840
2841 /*
2842 * Add the other matches on the line
2843 */
2844 while (!got_int)
2845 {
2846 /* Find start of the next word. Skip white
2847 * space and punctuation. */
2848 ptr = find_word_start(ptr);
2849 if (*ptr == NUL || *ptr == NL)
2850 break;
2851 wstart = ptr;
2852
2853 /* Find end of the word and add it. */
2854#ifdef FEAT_MBYTE
2855 if (has_mbyte)
2856 /* Japanese words may have characters in
2857 * different classes, only separate words
2858 * with single-byte non-word characters. */
2859 while (*ptr != NUL)
2860 {
2861 int l = (*mb_ptr2len)(ptr);
2862
2863 if (l < 2 && !vim_iswordc(*ptr))
2864 break;
2865 ptr += l;
2866 }
2867 else
2868#endif
2869 ptr = find_word_end(ptr);
2870 add_r = ins_compl_add_infercase(wstart,
2871 (int)(ptr - wstart),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002872 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002873 }
2874 }
2875 if (add_r == OK)
2876 /* if dir was BACKWARD then honor it just once */
2877 *dir = FORWARD;
2878 else if (add_r == FAIL)
2879 break;
2880 /* avoid expensive call to vim_regexec() when at end
2881 * of line */
2882 if (*ptr == '\n' || got_int)
2883 break;
2884 }
2885 line_breakcheck();
2886 ins_compl_check_keys(50);
2887 }
2888 fclose(fp);
2889 }
2890 }
2891}
2892
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893/*
2894 * Find the start of the next word.
2895 * Returns a pointer to the first char of the word. Also stops at a NUL.
2896 */
2897 char_u *
2898find_word_start(ptr)
2899 char_u *ptr;
2900{
2901#ifdef FEAT_MBYTE
2902 if (has_mbyte)
2903 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002904 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 else
2906#endif
2907 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2908 ++ptr;
2909 return ptr;
2910}
2911
2912/*
2913 * Find the end of the word. Assumes it starts inside a word.
2914 * Returns a pointer to just after the word.
2915 */
2916 char_u *
2917find_word_end(ptr)
2918 char_u *ptr;
2919{
2920#ifdef FEAT_MBYTE
2921 int start_class;
2922
2923 if (has_mbyte)
2924 {
2925 start_class = mb_get_class(ptr);
2926 if (start_class > 1)
2927 while (*ptr != NUL)
2928 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002929 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 if (mb_get_class(ptr) != start_class)
2931 break;
2932 }
2933 }
2934 else
2935#endif
2936 while (vim_iswordc(*ptr))
2937 ++ptr;
2938 return ptr;
2939}
2940
2941/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002942 * Find the end of the line, omitting CR and NL at the end.
2943 * Returns a pointer to just after the line.
2944 */
2945 static char_u *
2946find_line_end(ptr)
2947 char_u *ptr;
2948{
2949 char_u *s;
2950
2951 s = ptr + STRLEN(ptr);
2952 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2953 --s;
2954 return s;
2955}
2956
2957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 * Free the list of completions
2959 */
2960 static void
2961ins_compl_free()
2962{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002963 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002964 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002966 vim_free(compl_pattern);
2967 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002968 vim_free(compl_leader);
2969 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002971 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002973
2974 ins_compl_del_pum();
2975 pum_clear();
2976
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002977 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 do
2979 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002980 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002981 compl_curr_match = compl_curr_match->cp_next;
2982 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002984 if (match->cp_flags & FREE_FNAME)
2985 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00002986 for (i = 0; i < CPT_COUNT; ++i)
2987 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002989 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2990 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991}
2992
2993 static void
2994ins_compl_clear()
2995{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002996 compl_cont_status = 0;
2997 compl_started = FALSE;
2998 compl_matches = 0;
2999 vim_free(compl_pattern);
3000 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003001 vim_free(compl_leader);
3002 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003004 vim_free(compl_orig_text);
3005 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003006 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007}
3008
3009/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003010 * Return TRUE when Insert completion is active.
3011 */
3012 int
3013ins_compl_active()
3014{
3015 return compl_started;
3016}
3017
3018/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003019 * Delete one character before the cursor and show the subset of the matches
3020 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003021 * Returns the character to be used, NUL if the work is done and another char
3022 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003023 */
3024 static int
3025ins_compl_bs()
3026{
3027 char_u *line;
3028 char_u *p;
3029
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003030 line = ml_get_curline();
3031 p = line + curwin->w_cursor.col;
3032 mb_ptr_back(line, p);
3033
3034 /* Stop completion when the whole word was deleted. */
3035 if ((int)(p - line) - (int)compl_col <= 0)
3036 return K_BS;
3037
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003038 /* Deleted more than what was used to find matches or didn't finish
3039 * finding all matches: need to look for matches all over again. */
3040 if (curwin->w_cursor.col <= compl_col + compl_length
3041 || compl_was_interrupted)
3042 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003043
Bram Moolenaara6557602006-02-04 22:43:20 +00003044 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003045 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003046 if (compl_leader != NULL)
3047 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003048 ins_compl_new_leader();
3049 return NUL;
3050 }
3051 return K_BS;
3052}
Bram Moolenaara6557602006-02-04 22:43:20 +00003053
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003054/*
3055 * Called after changing "compl_leader".
3056 * Show the popup menu with a different set of matches.
3057 * May also search for matches again if the previous search was interrupted.
3058 */
3059 static void
3060ins_compl_new_leader()
3061{
3062 ins_compl_del_pum();
3063 ins_compl_delete();
3064 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3065 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003066
3067 if (compl_started)
3068 ins_compl_set_original_text(compl_leader);
3069 else
3070 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003071#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003072 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003073#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003074 /*
3075 * Matches were cleared, need to search for them now. First display
3076 * the changed text before the cursor. Set "compl_restarting" to
3077 * avoid that the first match is inserted.
3078 */
3079 update_screen(0);
3080#ifdef FEAT_GUI
3081 if (gui.in_use)
3082 {
3083 /* Show the cursor after the match, not after the redrawn text. */
3084 setcursor();
3085 out_flush();
3086 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003087 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003088#endif
3089 compl_restarting = TRUE;
3090 if (ins_complete(Ctrl_N) == FAIL)
3091 compl_cont_status = 0;
3092 compl_restarting = FALSE;
3093 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003094
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003095#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003096 if (!compl_used_match)
3097 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003098 /* Go to the original text, since none of the matches is inserted. */
3099 if (compl_first_match->cp_prev != NULL
3100 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3101 compl_shown_match = compl_first_match->cp_prev;
3102 else
3103 compl_shown_match = compl_first_match;
3104 compl_curr_match = compl_shown_match;
3105 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003106 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003107#endif
3108 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003109
3110 /* Show the popup menu with a different set of matches. */
3111 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003112
3113 /* Don't let Enter select the original text when there is no popup menu. */
3114 if (compl_match_array == NULL)
3115 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003116}
3117
3118/*
3119 * Append one character to the match leader. May reduce the number of
3120 * matches.
3121 */
3122 static void
3123ins_compl_addleader(c)
3124 int c;
3125{
3126#ifdef FEAT_MBYTE
3127 int cc;
3128
3129 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3130 {
3131 char_u buf[MB_MAXBYTES + 1];
3132
3133 (*mb_char2bytes)(c, buf);
3134 buf[cc] = NUL;
3135 ins_char_bytes(buf, cc);
3136 }
3137 else
3138#endif
3139 ins_char(c);
3140
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003141 /* If we didn't complete finding matches we must search again. */
3142 if (compl_was_interrupted)
3143 ins_compl_restart();
3144
Bram Moolenaara6557602006-02-04 22:43:20 +00003145 vim_free(compl_leader);
3146 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3147 curwin->w_cursor.col - compl_col);
3148 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003149 ins_compl_new_leader();
3150}
3151
3152/*
3153 * Setup for finding completions again without leaving CTRL-X mode. Used when
3154 * BS or a key was typed while still searching for matches.
3155 */
3156 static void
3157ins_compl_restart()
3158{
3159 ins_compl_free();
3160 compl_started = FALSE;
3161 compl_matches = 0;
3162 compl_cont_status = 0;
3163 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003164}
3165
3166/*
3167 * Set the first match, the original text.
3168 */
3169 static void
3170ins_compl_set_original_text(str)
3171 char_u *str;
3172{
3173 char_u *p;
3174
3175 /* Replace the original text entry. */
3176 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3177 {
3178 p = vim_strsave(str);
3179 if (p != NULL)
3180 {
3181 vim_free(compl_first_match->cp_str);
3182 compl_first_match->cp_str = p;
3183 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003184 }
3185}
3186
3187/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003188 * Append one character to the match leader. May reduce the number of
3189 * matches.
3190 */
3191 static void
3192ins_compl_addfrommatch()
3193{
3194 char_u *p;
3195 int len = curwin->w_cursor.col - compl_col;
3196 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003197 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003198
3199 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003200 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003201 {
3202 /* When still at the original match use the first entry that matches
3203 * the leader. */
3204 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3205 {
3206 p = NULL;
3207 for (cp = compl_shown_match->cp_next; cp != NULL
3208 && cp != compl_first_match; cp = cp->cp_next)
3209 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003210 if (compl_leader == NULL
3211 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003212 (int)STRLEN(compl_leader)))
3213 {
3214 p = cp->cp_str;
3215 break;
3216 }
3217 }
3218 if (p == NULL || (int)STRLEN(p) <= len)
3219 return;
3220 }
3221 else
3222 return;
3223 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003224 p += len;
3225#ifdef FEAT_MBYTE
3226 c = mb_ptr2char(p);
3227#else
3228 c = *p;
3229#endif
3230 ins_compl_addleader(c);
3231}
3232
3233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003235 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003236 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003238 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239ins_compl_prep(c)
3240 int c;
3241{
3242 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003244 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
3246 /* Forget any previous 'special' messages if this is actually
3247 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3248 */
3249 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3250 edit_submode_extra = NULL;
3251
3252 /* Ignore end of Select mode mapping */
3253 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003254 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003256 /* Set "compl_get_longest" when finding the first matches. */
3257 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3258 || (ctrl_x_mode == 0 && !compl_started))
3259 {
3260 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3261 compl_used_match = TRUE;
3262 }
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3265 {
3266 /*
3267 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3268 * it will be yet. Now we decide.
3269 */
3270 switch (c)
3271 {
3272 case Ctrl_E:
3273 case Ctrl_Y:
3274 ctrl_x_mode = CTRL_X_SCROLL;
3275 if (!(State & REPLACE_FLAG))
3276 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3277 else
3278 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3279 edit_submode_pre = NULL;
3280 showmode();
3281 break;
3282 case Ctrl_L:
3283 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3284 break;
3285 case Ctrl_F:
3286 ctrl_x_mode = CTRL_X_FILES;
3287 break;
3288 case Ctrl_K:
3289 ctrl_x_mode = CTRL_X_DICTIONARY;
3290 break;
3291 case Ctrl_R:
3292 /* Simply allow ^R to happen without affecting ^X mode */
3293 break;
3294 case Ctrl_T:
3295 ctrl_x_mode = CTRL_X_THESAURUS;
3296 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003297#ifdef FEAT_COMPL_FUNC
3298 case Ctrl_U:
3299 ctrl_x_mode = CTRL_X_FUNCTION;
3300 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003301 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003302 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003303 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003304#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003305 case 's':
3306 case Ctrl_S:
3307 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003308#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003309 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003310 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003311 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003312#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003313 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 case Ctrl_RSB:
3315 ctrl_x_mode = CTRL_X_TAGS;
3316 break;
3317#ifdef FEAT_FIND_ID
3318 case Ctrl_I:
3319 case K_S_TAB:
3320 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3321 break;
3322 case Ctrl_D:
3323 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3324 break;
3325#endif
3326 case Ctrl_V:
3327 case Ctrl_Q:
3328 ctrl_x_mode = CTRL_X_CMDLINE;
3329 break;
3330 case Ctrl_P:
3331 case Ctrl_N:
3332 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3333 * just started ^X mode, or there were enough ^X's to cancel
3334 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3335 * do normal expansion when interrupting a different mode (say
3336 * ^X^F^X^P or ^P^X^X^P, see below)
3337 * nothing changes if interrupting mode 0, (eg, the flag
3338 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003339 if (!(compl_cont_status & CONT_INTRPT))
3340 compl_cont_status |= CONT_LOCAL;
3341 else if (compl_cont_mode != 0)
3342 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 /* FALLTHROUGH */
3344 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003345 /* If we have typed at least 2 ^X's... for modes != 0, we set
3346 * compl_cont_status = 0 (eg, as if we had just started ^X
3347 * mode).
3348 * For mode 0, we set "compl_cont_mode" to an impossible
3349 * value, in both cases ^X^X can be used to restart the same
3350 * mode (avoiding ADDING mode).
3351 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3352 * 'complete' and local ^P expansions respectively.
3353 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3354 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (c == Ctrl_X)
3356 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003357 if (compl_cont_mode != 0)
3358 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003360 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 }
3362 ctrl_x_mode = 0;
3363 edit_submode = NULL;
3364 showmode();
3365 break;
3366 }
3367 }
3368 else if (ctrl_x_mode != 0)
3369 {
3370 /* We're already in CTRL-X mode, do we stay in it? */
3371 if (!vim_is_ctrl_x_key(c))
3372 {
3373 if (ctrl_x_mode == CTRL_X_SCROLL)
3374 ctrl_x_mode = 0;
3375 else
3376 ctrl_x_mode = CTRL_X_FINISHED;
3377 edit_submode = NULL;
3378 }
3379 showmode();
3380 }
3381
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003382 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
3384 /* Show error message from attempted keyword completion (probably
3385 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003386 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003388 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3389 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 || ctrl_x_mode == CTRL_X_FINISHED)
3391 {
3392 /* Get here when we have finished typing a sequence of ^N and
3393 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003394 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003395 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003397 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003398 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003399
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003401 * If any of the original typed text has been changed, eg when
3402 * ignorecase is set, we must add back-spaces to the redo
3403 * buffer. We add as few as necessary to delete just the part
3404 * of the original text that has changed.
3405 * When using the longest match, edited the match or used
3406 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003408 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3409 ptr = compl_curr_match->cp_str;
3410 else if (compl_leader != NULL)
3411 ptr = compl_leader;
3412 else
3413 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003414 if (compl_orig_text != NULL)
3415 {
3416 p = compl_orig_text;
3417 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3418 ++temp)
3419 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003420#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003421 if (temp > 0)
3422 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003423#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003424 for (p += temp; *p != NUL; mb_ptr_adv(p))
3425 AppendCharToRedobuff(K_BS);
3426 }
3427 if (ptr != NULL)
3428 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 }
3430
3431#ifdef FEAT_CINDENT
3432 want_cindent = (can_cindent && cindent_on());
3433#endif
3434 /*
3435 * When completing whole lines: fix indent for 'cindent'.
3436 * Otherwise, break line if it's too long.
3437 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003438 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
3440#ifdef FEAT_CINDENT
3441 /* re-indent the current line */
3442 if (want_cindent)
3443 {
3444 do_c_expr_indent();
3445 want_cindent = FALSE; /* don't do it again */
3446 }
3447#endif
3448 }
3449 else
3450 {
3451 /* put the cursor on the last char, for 'tw' formatting */
3452 curwin->w_cursor.col--;
3453 if (stop_arrow() == OK)
3454 insertchar(NUL, 0, -1);
3455 curwin->w_cursor.col++;
3456 }
3457
3458 auto_format(FALSE, TRUE);
3459
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003460 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003461 * the selection without inserting anything. When
3462 * compl_enter_selects is set the Enter key does the same. */
3463 if ((c == Ctrl_Y || (compl_enter_selects
3464 && (c == CAR || c == K_KENTER || c == NL)))
3465 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003466 retval = TRUE;
3467
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003468 /* CTRL-E means completion is Ended, go back to the typed text. */
3469 if (c == Ctrl_E)
3470 {
3471 ins_compl_delete();
3472 if (compl_leader != NULL)
3473 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3474 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003475 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003476 + curwin->w_cursor.col - compl_col);
3477 retval = TRUE;
3478 }
3479
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003481 compl_started = FALSE;
3482 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 msg_clr_cmdline(); /* necessary for "noshowmode" */
3484 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003485 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 if (edit_submode != NULL)
3487 {
3488 edit_submode = NULL;
3489 showmode();
3490 }
3491
3492#ifdef FEAT_CINDENT
3493 /*
3494 * Indent now if a key was typed that is in 'cinkeys'.
3495 */
3496 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3497 do_c_expr_indent();
3498#endif
3499 }
3500 }
3501
3502 /* reset continue_* if we left expansion-mode, if we stay they'll be
3503 * (re)set properly in ins_complete() */
3504 if (!vim_is_ctrl_x_key(c))
3505 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003506 compl_cont_status = 0;
3507 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003509
3510 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511}
3512
3513/*
3514 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3515 * (depending on flag) starting from buf and looking for a non-scanned
3516 * buffer (other than curbuf). curbuf is special, if it is called with
3517 * buf=curbuf then it has to be the first call for a given flag/expansion.
3518 *
3519 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3520 */
3521 static buf_T *
3522ins_compl_next_buf(buf, flag)
3523 buf_T *buf;
3524 int flag;
3525{
3526#ifdef FEAT_WINDOWS
3527 static win_T *wp;
3528#endif
3529
3530 if (flag == 'w') /* just windows */
3531 {
3532#ifdef FEAT_WINDOWS
3533 if (buf == curbuf) /* first call for this flag/expansion */
3534 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003535 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 && wp->w_buffer->b_scanned)
3537 ;
3538 buf = wp->w_buffer;
3539#else
3540 buf = curbuf;
3541#endif
3542 }
3543 else
3544 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3545 * (unlisted buffers)
3546 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003547 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 && ((flag == 'U'
3549 ? buf->b_p_bl
3550 : (!buf->b_p_bl
3551 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003552 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 ;
3554 return buf;
3555}
3556
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003557#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003558static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003559
3560/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003561 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003562 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003563 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003564 static void
3565expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003566 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003567 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003568{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003569 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003570 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003571 char_u *funcname;
3572 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003573
Bram Moolenaare344bea2005-09-01 20:46:49 +00003574 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3575 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003576 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003577
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003578 /* Call 'completefunc' to obtain the list of matches. */
3579 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003580 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003581
Bram Moolenaare344bea2005-09-01 20:46:49 +00003582 pos = curwin->w_cursor;
3583 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3584 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003585 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003586 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003587
Bram Moolenaara94bc432006-03-10 21:42:59 +00003588 ins_compl_add_list(matchlist);
3589 list_unref(matchlist);
3590}
3591#endif /* FEAT_COMPL_FUNC */
3592
Bram Moolenaar39f05632006-03-19 22:15:26 +00003593#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003594/*
3595 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003596 */
3597 static void
3598ins_compl_add_list(list)
3599 list_T *list;
3600{
3601 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003602 int dir = compl_direction;
3603
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003604 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003605 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003606 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003607 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3608 /* if dir was BACKWARD then honor it just once */
3609 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003610 else if (did_emsg)
3611 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003612 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003613}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003614
3615/*
3616 * Add a match to the list of matches from a typeval_T.
3617 * If the given string is already in the list of completions, then return
3618 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3619 * maybe because alloc() returns NULL, then FAIL is returned.
3620 */
3621 int
3622ins_compl_add_tv(tv, dir)
3623 typval_T *tv;
3624 int dir;
3625{
3626 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003627 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003628 int adup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003629 char_u *(cptext[CPT_COUNT]);
3630
3631 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3632 {
3633 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3634 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3635 (char_u *)"abbr", FALSE);
3636 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3637 (char_u *)"menu", FALSE);
3638 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3639 (char_u *)"kind", FALSE);
3640 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3641 (char_u *)"info", FALSE);
3642 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3643 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003644 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003645 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003646 }
3647 else
3648 {
3649 word = get_tv_string_chk(tv);
3650 vim_memset(cptext, 0, sizeof(cptext));
3651 }
3652 if (word == NULL || *word == NUL)
3653 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003654 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003655}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003656#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003657
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003658/*
3659 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003660 * The search starts at position "ini" in curbuf and in the direction
3661 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003662 * When "compl_started" is FALSE start at that position, otherwise continue
3663 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003664 * This may return before finding all the matches.
3665 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 */
3667 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003668ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670{
3671 static pos_T first_match_pos;
3672 static pos_T last_match_pos;
3673 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003674 static int found_all = FALSE; /* Found all matches of a
3675 certain type. */
3676 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677
Bram Moolenaar572cb562005-08-05 21:35:02 +00003678 pos_T *pos;
3679 char_u **matches;
3680 int save_p_scs;
3681 int save_p_ws;
3682 int save_p_ic;
3683 int i;
3684 int num_matches;
3685 int len;
3686 int found_new_match;
3687 int type = ctrl_x_mode;
3688 char_u *ptr;
3689 char_u *dict = NULL;
3690 int dict_f = 0;
3691 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003693 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 {
3695 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3696 ins_buf->b_scanned = 0;
3697 found_all = FALSE;
3698 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003699 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003700 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 last_match_pos = first_match_pos = *ini;
3702 }
3703
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003704 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003705 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3707 for (;;)
3708 {
3709 found_new_match = FAIL;
3710
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003711 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 * or if found_all says this entry is done. For ^X^L only use the
3713 * entries from 'complete' that look in loaded buffers. */
3714 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003715 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 {
3717 found_all = FALSE;
3718 while (*e_cpt == ',' || *e_cpt == ' ')
3719 e_cpt++;
3720 if (*e_cpt == '.' && !curbuf->b_scanned)
3721 {
3722 ins_buf = curbuf;
3723 first_match_pos = *ini;
3724 /* So that ^N can match word immediately after cursor */
3725 if (ctrl_x_mode == 0)
3726 dec(&first_match_pos);
3727 last_match_pos = first_match_pos;
3728 type = 0;
3729 }
3730 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3731 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3732 {
3733 /* Scan a buffer, but not the current one. */
3734 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3735 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003736 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 first_match_pos.col = last_match_pos.col = 0;
3738 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3739 last_match_pos.lnum = 0;
3740 type = 0;
3741 }
3742 else /* unloaded buffer, scan like dictionary */
3743 {
3744 found_all = TRUE;
3745 if (ins_buf->b_fname == NULL)
3746 continue;
3747 type = CTRL_X_DICTIONARY;
3748 dict = ins_buf->b_fname;
3749 dict_f = DICT_EXACT;
3750 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003751 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 ins_buf->b_fname == NULL
3753 ? buf_spname(ins_buf)
3754 : ins_buf->b_sfname == NULL
3755 ? (char *)ins_buf->b_fname
3756 : (char *)ins_buf->b_sfname);
3757 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3758 }
3759 else if (*e_cpt == NUL)
3760 break;
3761 else
3762 {
3763 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3764 type = -1;
3765 else if (*e_cpt == 'k' || *e_cpt == 's')
3766 {
3767 if (*e_cpt == 'k')
3768 type = CTRL_X_DICTIONARY;
3769 else
3770 type = CTRL_X_THESAURUS;
3771 if (*++e_cpt != ',' && *e_cpt != NUL)
3772 {
3773 dict = e_cpt;
3774 dict_f = DICT_FIRST;
3775 }
3776 }
3777#ifdef FEAT_FIND_ID
3778 else if (*e_cpt == 'i')
3779 type = CTRL_X_PATH_PATTERNS;
3780 else if (*e_cpt == 'd')
3781 type = CTRL_X_PATH_DEFINES;
3782#endif
3783 else if (*e_cpt == ']' || *e_cpt == 't')
3784 {
3785 type = CTRL_X_TAGS;
3786 sprintf((char*)IObuff, _("Scanning tags."));
3787 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3788 }
3789 else
3790 type = -1;
3791
3792 /* in any case e_cpt is advanced to the next entry */
3793 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3794
3795 found_all = TRUE;
3796 if (type == -1)
3797 continue;
3798 }
3799 }
3800
3801 switch (type)
3802 {
3803 case -1:
3804 break;
3805#ifdef FEAT_FIND_ID
3806 case CTRL_X_PATH_PATTERNS:
3807 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003808 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003809 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003811 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3813 (linenr_T)1, (linenr_T)MAXLNUM);
3814 break;
3815#endif
3816
3817 case CTRL_X_DICTIONARY:
3818 case CTRL_X_THESAURUS:
3819 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003820 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 : (type == CTRL_X_THESAURUS
3822 ? (*curbuf->b_p_tsr == NUL
3823 ? p_tsr
3824 : curbuf->b_p_tsr)
3825 : (*curbuf->b_p_dict == NUL
3826 ? p_dict
3827 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003828 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003829 dict != NULL ? dict_f
3830 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 dict = NULL;
3832 break;
3833
3834 case CTRL_X_TAGS:
3835 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3836 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003837 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838
3839 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003840 * of matches is found when compl_pattern is empty */
3841 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3843 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3844 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3845 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003846 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
3848 p_ic = save_p_ic;
3849 break;
3850
3851 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003852 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3854 {
3855
3856 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003857 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003858 ins_compl_add_matches(num_matches, matches,
3859#ifdef CASE_INSENSITIVE_FILENAME
3860 TRUE
3861#else
3862 FALSE
3863#endif
3864 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
3866 break;
3867
3868 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003869 if (expand_cmdline(&compl_xp, compl_pattern,
3870 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003872 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 break;
3874
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003875#ifdef FEAT_COMPL_FUNC
3876 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003877 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003878 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003879 break;
3880#endif
3881
Bram Moolenaar488c6512005-08-11 20:09:58 +00003882 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003883#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003884 num_matches = expand_spelling(first_match_pos.lnum,
3885 first_match_pos.col, compl_pattern, &matches);
3886 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003887 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003888#endif
3889 break;
3890
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 default: /* normal ^P/^N and ^X^L */
3892 /*
3893 * If 'infercase' is set, don't use 'smartcase' here
3894 */
3895 save_p_scs = p_scs;
3896 if (ins_buf->b_p_inf)
3897 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003898
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 /* buffers other than curbuf are scanned from the beginning or the
3900 * end but never from the middle, thus setting nowrapscan in this
3901 * buffers is a good idea, on the other hand, we always set
3902 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3903 save_p_ws = p_ws;
3904 if (ins_buf != curbuf)
3905 p_ws = FALSE;
3906 else if (*e_cpt == '.')
3907 p_ws = TRUE;
3908 for (;;)
3909 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003910 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
Bram Moolenaardf40adf2006-10-14 12:32:39 +00003912 ++msg_silent; /* Don't want messages for wrapscan. */
3913
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003914 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3915 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003917 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003919 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003921 found_new_match = searchit(NULL, ins_buf, pos,
3922 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003923 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003924 RE_LAST, (linenr_T)0);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00003925 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003926 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003928 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003929 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 first_match_pos = *pos;
3931 last_match_pos = *pos;
3932 }
3933 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003934 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 found_new_match = FAIL;
3936 if (found_new_match == FAIL)
3937 {
3938 if (ins_buf == curbuf)
3939 found_all = TRUE;
3940 break;
3941 }
3942
3943 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003944 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 && ini->lnum == pos->lnum
3946 && ini->col == pos->col)
3947 continue;
3948 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3949 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3950 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003951 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
3953 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3954 continue;
3955 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3956 if (!p_paste)
3957 ptr = skipwhite(ptr);
3958 }
3959 len = (int)STRLEN(ptr);
3960 }
3961 else
3962 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003963 char_u *tmp_ptr = ptr;
3964
3965 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003967 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 /* Skip if already inside a word. */
3969 if (vim_iswordp(tmp_ptr))
3970 continue;
3971 /* Find start of next word. */
3972 tmp_ptr = find_word_start(tmp_ptr);
3973 }
3974 /* Find end of this word. */
3975 tmp_ptr = find_word_end(tmp_ptr);
3976 len = (int)(tmp_ptr - ptr);
3977
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003978 if ((compl_cont_status & CONT_ADDING)
3979 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 {
3981 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3982 {
3983 /* Try next line, if any. the new word will be
3984 * "join" as if the normal command "J" was used.
3985 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003986 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 * works -- Acevedo */
3988 STRNCPY(IObuff, ptr, len);
3989 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3990 tmp_ptr = ptr = skipwhite(ptr);
3991 /* Find start of next word. */
3992 tmp_ptr = find_word_start(tmp_ptr);
3993 /* Find end of next word. */
3994 tmp_ptr = find_word_end(tmp_ptr);
3995 if (tmp_ptr > ptr)
3996 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003997 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003999 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 IObuff[len++] = ' ';
4001 /* IObuf =~ "\k.* ", thus len >= 2 */
4002 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004003 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 || (vim_strchr(p_cpo, CPO_JOINSP)
4005 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004006 && (IObuff[len - 2] == '?'
4007 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 IObuff[len++] = ' ';
4009 }
4010 /* copy as much as posible of the new word */
4011 if (tmp_ptr - ptr >= IOSIZE - len)
4012 tmp_ptr = ptr + IOSIZE - len - 1;
4013 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4014 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004015 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 IObuff[len] = NUL;
4018 ptr = IObuff;
4019 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004020 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 continue;
4022 }
4023 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004024 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004025 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004026 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
4028 found_new_match = OK;
4029 break;
4030 }
4031 }
4032 p_scs = save_p_scs;
4033 p_ws = save_p_ws;
4034 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004035
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004036 /* check if compl_curr_match has changed, (e.g. other type of
4037 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004038 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 found_new_match = OK;
4040
4041 /* break the loop for specialized modes (use 'complete' just for the
4042 * generic ctrl_x_mode == 0) or when we've found a new match */
4043 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004044 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004045 {
4046 if (got_int)
4047 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004048 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004049 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004050 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004051
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004052 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4053 || compl_interrupted)
4054 break;
4055 compl_started = TRUE;
4056 }
4057 else
4058 {
4059 /* Mark a buffer scanned when it has been scanned completely */
4060 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4061 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004063 compl_started = FALSE;
4064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004066 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4069 && *e_cpt == NUL) /* Got to end of 'complete' */
4070 found_new_match = FAIL;
4071
4072 i = -1; /* total of matches, unknown */
4073 if (found_new_match == FAIL
4074 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4075 i = ins_compl_make_cyclic();
4076
4077 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004078 * just been made cyclic then we have to move compl_curr_match to the next
4079 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004080 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4081 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004082 if (compl_curr_match == NULL)
4083 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 return i;
4085}
4086
4087/* Delete the old text being completed. */
4088 static void
4089ins_compl_delete()
4090{
4091 int i;
4092
4093 /*
4094 * In insert mode: Delete the typed part.
4095 * In replace mode: Put the old characters back, if any.
4096 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004097 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 backspace_until_column(i);
4099 changed_cline_bef_curs();
4100}
4101
4102/* Insert the new text being completed. */
4103 static void
4104ins_compl_insert()
4105{
Bram Moolenaar572cb562005-08-05 21:35:02 +00004106 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004107 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4108 compl_used_match = FALSE;
4109 else
4110 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111}
4112
4113/*
4114 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004115 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4116 * get more completions. If it is FALSE, then we just do nothing when there
4117 * are no more completions in a given direction. The latter case is used when
4118 * we are still in the middle of finding completions, to allow browsing
4119 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 * Return the total number of matches, or -1 if still unknown -- webb.
4121 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004122 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4123 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 *
4125 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004126 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4127 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 */
4129 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004130ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004132 int count; /* repeat completion this many times; should
4133 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004134 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135{
4136 int num_matches = -1;
4137 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004138 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004139 compl_T *found_compl = NULL;
4140 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004141 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004143 if (compl_leader != NULL
4144 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004146 /* Set "compl_shown_match" to the actually shown match, it may differ
4147 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004148 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004149 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004150 && compl_shown_match->cp_next != NULL
4151 && compl_shown_match->cp_next != compl_first_match)
4152 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004153
4154 /* If we didn't find it searching forward, and compl_shows_dir is
4155 * backward, find the last match. */
4156 if (compl_shows_dir == BACKWARD
4157 && !ins_compl_equal(compl_shown_match,
4158 compl_leader, (int)STRLEN(compl_leader))
4159 && (compl_shown_match->cp_next == NULL
4160 || compl_shown_match->cp_next == compl_first_match))
4161 {
4162 while (!ins_compl_equal(compl_shown_match,
4163 compl_leader, (int)STRLEN(compl_leader))
4164 && compl_shown_match->cp_prev != NULL
4165 && compl_shown_match->cp_prev != compl_first_match)
4166 compl_shown_match = compl_shown_match->cp_prev;
4167 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004168 }
4169
4170 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004171 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 /* Delete old text to be replaced */
4173 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004174
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004175 /* When finding the longest common text we stick at the original text,
4176 * don't let CTRL-N or CTRL-P move to the first match. */
4177 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4178
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004179 /* When restarting the search don't insert the first match either. */
4180 if (compl_restarting)
4181 {
4182 advance = FALSE;
4183 compl_restarting = FALSE;
4184 }
4185
Bram Moolenaare3226be2005-12-18 22:10:00 +00004186 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4187 * around. */
4188 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004190 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004192 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004193 found_end = (compl_first_match != NULL
4194 && (compl_shown_match->cp_next == compl_first_match
4195 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004196 }
4197 else if (compl_shows_dir == BACKWARD
4198 && compl_shown_match->cp_prev != NULL)
4199 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004200 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004201 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004202 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004205 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004206 if (!allow_get_expansion)
4207 {
4208 if (advance)
4209 {
4210 if (compl_shows_dir == BACKWARD)
4211 compl_pending -= todo + 1;
4212 else
4213 compl_pending += todo + 1;
4214 }
4215 return -1;
4216 }
4217
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004218 if (advance)
4219 {
4220 if (compl_shows_dir == BACKWARD)
4221 --compl_pending;
4222 else
4223 ++compl_pending;
4224 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004225
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004226 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004227 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004228
4229 /* handle any pending completions */
4230 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004231 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004232 {
4233 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4234 {
4235 compl_shown_match = compl_shown_match->cp_next;
4236 --compl_pending;
4237 }
4238 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4239 {
4240 compl_shown_match = compl_shown_match->cp_prev;
4241 ++compl_pending;
4242 }
4243 else
4244 break;
4245 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004246 found_end = FALSE;
4247 }
4248 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4249 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004250 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004251 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004252 ++todo;
4253 else
4254 /* Remember a matching item. */
4255 found_compl = compl_shown_match;
4256
4257 /* Stop at the end of the list when we found a usable match. */
4258 if (found_end)
4259 {
4260 if (found_compl != NULL)
4261 {
4262 compl_shown_match = found_compl;
4263 break;
4264 }
4265 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004269 /* Insert the text of the new completion, or the compl_leader. */
4270 if (insert_match)
4271 {
4272 if (!compl_get_longest || compl_used_match)
4273 ins_compl_insert();
4274 else
4275 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4276 }
4277 else
4278 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
4280 if (!allow_get_expansion)
4281 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004282 /* may undisplay the popup menu first */
4283 ins_compl_upd_pum();
4284
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004285 /* redraw to show the user what was inserted */
4286 update_screen(0);
4287
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004288 /* display the updated popup menu */
4289 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004290#ifdef FEAT_GUI
4291 if (gui.in_use)
4292 {
4293 /* Show the cursor after the match, not after the redrawn text. */
4294 setcursor();
4295 out_flush();
4296 gui_update_cursor(FALSE, FALSE);
4297 }
4298#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004299
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 /* Delete old text to be replaced, since we're still searching and
4301 * don't want to match ourselves! */
4302 ins_compl_delete();
4303 }
4304
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004305 /* Enter will select a match when the match wasn't inserted and the popup
4306 * menu is visislbe. */
4307 compl_enter_selects = !insert_match && compl_match_array != NULL;
4308
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 /*
4310 * Show the file name for the match (if any)
4311 * Truncate the file name to avoid a wait for return.
4312 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004313 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 {
4315 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004316 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 if (i <= 0)
4318 i = 0;
4319 else
4320 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004321 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 msg(IObuff);
4323 redraw_cmdline = FALSE; /* don't overwrite! */
4324 }
4325
4326 return num_matches;
4327}
4328
4329/*
4330 * Call this while finding completions, to check whether the user has hit a key
4331 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004332 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004334 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 */
4336 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004337ins_compl_check_keys(frequency)
4338 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339{
4340 static int count = 0;
4341
4342 int c;
4343
4344 /* Don't check when reading keys from a script. That would break the test
4345 * scripts */
4346 if (using_script())
4347 return;
4348
4349 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004350 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 return;
4352 count = 0;
4353
Bram Moolenaara260a972006-06-23 19:36:29 +00004354 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4355 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 if (c != NUL)
4358 {
4359 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4360 {
4361 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004362 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004363 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4364 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004366 else
4367 {
4368 /* Need to get the character to have KeyTyped set. We'll put it
4369 * back with vungetc() below. */
4370 c = safe_vgetc();
4371
4372 /* Don't interrupt completion when the character wasn't typed,
4373 * e.g., when doing @q to replay keys. */
4374 if (c != Ctrl_R && KeyTyped)
4375 compl_interrupted = TRUE;
4376
4377 vungetc(c);
4378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004380 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004381 {
4382 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4383
4384 compl_pending = 0;
4385 (void)ins_compl_next(FALSE, todo, TRUE);
4386 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004387}
4388
4389/*
4390 * Decide the direction of Insert mode complete from the key typed.
4391 * Returns BACKWARD or FORWARD.
4392 */
4393 static int
4394ins_compl_key2dir(c)
4395 int c;
4396{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004397 if (c == Ctrl_P || c == Ctrl_L
4398 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4399 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004400 return BACKWARD;
4401 return FORWARD;
4402}
4403
4404/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004405 * Return TRUE for keys that are used for completion only when the popup menu
4406 * is visible.
4407 */
4408 static int
4409ins_compl_pum_key(c)
4410 int c;
4411{
4412 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004413 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4414 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004415}
4416
4417/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004418 * Decide the number of completions to move forward.
4419 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4420 */
4421 static int
4422ins_compl_key2count(c)
4423 int c;
4424{
4425 int h;
4426
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004427 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004428 {
4429 h = pum_get_height();
4430 if (h > 3)
4431 h -= 2; /* keep some context */
4432 return h;
4433 }
4434 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435}
4436
4437/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004438 * Return TRUE if completion with "c" should insert the match, FALSE if only
4439 * to change the currently selected completion.
4440 */
4441 static int
4442ins_compl_use_match(c)
4443 int c;
4444{
4445 switch (c)
4446 {
4447 case K_UP:
4448 case K_DOWN:
4449 case K_PAGEDOWN:
4450 case K_KPAGEDOWN:
4451 case K_S_DOWN:
4452 case K_PAGEUP:
4453 case K_KPAGEUP:
4454 case K_S_UP:
4455 return FALSE;
4456 }
4457 return TRUE;
4458}
4459
4460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 * Do Insert mode completion.
4462 * Called when character "c" was typed, which has a meaning for completion.
4463 * Returns OK if completion was done, FAIL if something failed (out of mem).
4464 */
4465 static int
4466ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004467 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004469 char_u *line;
4470 int startcol = 0; /* column where searched text starts */
4471 colnr_T curs_col; /* cursor column */
4472 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
Bram Moolenaare3226be2005-12-18 22:10:00 +00004474 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004475 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
4477 /* First time we hit ^N or ^P (in a row, I mean) */
4478
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 did_ai = FALSE;
4480#ifdef FEAT_SMARTINDENT
4481 did_si = FALSE;
4482 can_si = FALSE;
4483 can_si_back = FALSE;
4484#endif
4485 if (stop_arrow() == FAIL)
4486 return FAIL;
4487
4488 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004489 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004490 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
4492 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004493 * "compl_startpos" to the cursor as a pattern to add a new word
4494 * instead of expand the one before the cursor, in word-wise if
4495 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 * is not in the same line as the cursor then fix it (the line has
4497 * been split because it was longer than 'tw'). if SOL is set then
4498 * skip the previous pattern, a word at the beginning of the line has
4499 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004500 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4501 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 {
4503 /*
4504 * it is a continued search
4505 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004506 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4508 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4509 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004510 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004512 /* line (probably) wrapped, set compl_startpos to the
4513 * first non_blank in the line, if it is not a wordchar
4514 * include it to get a better pattern, but then we don't
4515 * want the "\\<" prefix, check it bellow */
4516 compl_col = (colnr_T)(skipwhite(line) - line);
4517 compl_startpos.col = compl_col;
4518 compl_startpos.lnum = curwin->w_cursor.lnum;
4519 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
4521 else
4522 {
4523 /* S_IPOS was set when we inserted a word that was at the
4524 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004525 * mode but first we need to redefine compl_startpos */
4526 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004528 compl_cont_status |= CONT_SOL;
4529 compl_startpos.col = (colnr_T)(skipwhite(
4530 line + compl_length
4531 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004533 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004535 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004536 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 * have enough space? just being paranoic */
4538#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004539 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004541 compl_cont_status &= ~CONT_SOL;
4542 compl_length = (IOSIZE - MIN_SPACE);
4543 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004545 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4546 if (compl_length < 1)
4547 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004550 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004552 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
4554 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004555 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004559 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004561 compl_cont_status = 0;
4562 compl_cont_status |= CONT_N_ADDS;
4563 compl_startpos = curwin->w_cursor;
4564 startcol = (int)curs_col;
4565 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
4567
4568 /* Work out completion pattern and original text -- webb */
4569 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4570 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004571 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4573 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004574 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004576 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004578 compl_col += ++startcol;
4579 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
4581 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004582 compl_pattern = str_foldcase(line + compl_col,
4583 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004585 compl_pattern = vim_strnsave(line + compl_col,
4586 compl_length);
4587 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 return FAIL;
4589 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004590 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 {
4592 char_u *prefix = (char_u *)"\\<";
4593
4594 /* we need 3 extra chars, 1 for the NUL and
4595 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004596 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4597 compl_length) + 3);
4598 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004600 if (!vim_iswordp(line + compl_col)
4601 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 && (
4603#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004604 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004606 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607#endif
4608 )))
4609 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610 STRCPY((char *)compl_pattern, prefix);
4611 (void)quote_meta(compl_pattern + STRLEN(prefix),
4612 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004614 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004616 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004618 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619#endif
4620 )
4621 {
4622 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004623 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4624 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004626 compl_col += curs_col;
4627 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
4629 else
4630 {
4631#ifdef FEAT_MBYTE
4632 /* Search the point of change class of multibyte character
4633 * or not a word single byte character backward. */
4634 if (has_mbyte)
4635 {
4636 int base_class;
4637 int head_off;
4638
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004639 startcol -= (*mb_head_off)(line, line + startcol);
4640 base_class = mb_get_class(line + startcol);
4641 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004643 head_off = (*mb_head_off)(line, line + startcol);
4644 if (base_class != mb_get_class(line + startcol
4645 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004647 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 }
4649 }
4650 else
4651#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004652 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004654 compl_col += ++startcol;
4655 compl_length = (int)curs_col - startcol;
4656 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 {
4658 /* Only match word with at least two chars -- webb
4659 * there's no need to call quote_meta,
4660 * alloc(7) is enough -- Acevedo
4661 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004662 compl_pattern = alloc(7);
4663 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004665 STRCPY((char *)compl_pattern, "\\<");
4666 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4667 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
4669 else
4670 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004671 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4672 compl_length) + 3);
4673 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004675 STRCPY((char *)compl_pattern, "\\<");
4676 (void)quote_meta(compl_pattern + 2, line + compl_col,
4677 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
4679 }
4680 }
4681 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4682 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004683 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004684 compl_length = (int)curs_col - (int)compl_col;
4685 if (compl_length < 0) /* cursor in indent: empty pattern */
4686 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004688 compl_pattern = str_foldcase(line + compl_col, compl_length,
4689 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004691 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4692 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 return FAIL;
4694 }
4695 else if (ctrl_x_mode == CTRL_X_FILES)
4696 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004697 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004699 compl_col += ++startcol;
4700 compl_length = (int)curs_col - startcol;
4701 compl_pattern = addstar(line + compl_col, compl_length,
4702 EXPAND_FILES);
4703 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 return FAIL;
4705 }
4706 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4707 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004708 compl_pattern = vim_strnsave(line, curs_col);
4709 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004711 set_cmd_context(&compl_xp, compl_pattern,
4712 (int)STRLEN(compl_pattern), curs_col);
4713 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4714 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004715 /* No completion possible, use an empty pattern to get a
4716 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004717 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004718 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004719 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4720 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004722 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004723 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004724#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004725 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004726 * Call user defined function 'completefunc' with "a:findstart"
4727 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004728 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004729 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004730 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004731 char_u *funcname;
4732 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004733
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004734 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004735 * string */
4736 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4737 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4738 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004739 {
4740 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4741 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004742 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004743 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004744
4745 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004746 args[1] = NULL;
4747 pos = curwin->w_cursor;
4748 col = call_func_retnr(funcname, 2, args, FALSE);
4749 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004750
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004751 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004752 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004753 compl_col = col;
4754 if ((colnr_T)compl_col > curs_col)
4755 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004756
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004757 /* Setup variables for completion. Need to obtain "line" again,
4758 * it may have become invalid. */
4759 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004760 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004761 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4762 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004763#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004764 return FAIL;
4765 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004766 else if (ctrl_x_mode == CTRL_X_SPELL)
4767 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004768#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004769 if (spell_bad_len > 0)
4770 compl_col = curs_col - spell_bad_len;
4771 else
4772 compl_col = spell_word_start(startcol);
4773 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004774 {
4775 compl_length = 0;
4776 compl_col = curs_col;
4777 }
4778 else
4779 {
4780 spell_expand_check_cap(compl_col);
4781 compl_length = (int)curs_col - compl_col;
4782 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004783 /* Need to obtain "line" again, it may have become invalid. */
4784 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004785 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4786 if (compl_pattern == NULL)
4787#endif
4788 return FAIL;
4789 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004790 else
4791 {
4792 EMSG2(_(e_intern2), "ins_complete()");
4793 return FAIL;
4794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004796 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 {
4798 edit_submode_pre = (char_u *)_(" Adding");
4799 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4800 {
4801 /* Insert a new line, keep indentation but ignore 'comments' */
4802#ifdef FEAT_COMMENTS
4803 char_u *old = curbuf->b_p_com;
4804
4805 curbuf->b_p_com = (char_u *)"";
4806#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004807 compl_startpos.lnum = curwin->w_cursor.lnum;
4808 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 ins_eol('\r');
4810#ifdef FEAT_COMMENTS
4811 curbuf->b_p_com = old;
4812#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004813 compl_length = 0;
4814 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
4816 }
4817 else
4818 {
4819 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004820 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
4822
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004823 if (compl_cont_status & CONT_LOCAL)
4824 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 else
4826 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4827
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004828 /* Always add completion for the original text. */
4829 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004830 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4831 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004832 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004834 vim_free(compl_pattern);
4835 compl_pattern = NULL;
4836 vim_free(compl_orig_text);
4837 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 return FAIL;
4839 }
4840
4841 /* showmode might reset the internal line pointers, so it must
4842 * be called before line = ml_get(), or when this address is no
4843 * longer needed. -- Acevedo.
4844 */
4845 edit_submode_extra = (char_u *)_("-- Searching...");
4846 edit_submode_highl = HLF_COUNT;
4847 showmode();
4848 edit_submode_extra = NULL;
4849 out_flush();
4850 }
4851
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004852 compl_shown_match = compl_curr_match;
4853 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
4855 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004856 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004858 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004860 /* may undisplay the popup menu */
4861 ins_compl_upd_pum();
4862
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004863 if (n > 1) /* all matches have been found */
4864 compl_matches = n;
4865 compl_curr_match = compl_shown_match;
4866 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
Bram Moolenaard68071d2006-05-02 22:08:30 +00004868 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4869 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 if (got_int && !global_busy)
4871 {
4872 (void)vgetc();
4873 got_int = FALSE;
4874 }
4875
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004876 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004877 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004879 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4880 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4882 edit_submode_highl = HLF_E;
4883 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4884 * because we couldn't expand anything at first place, but if we used
4885 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4886 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004887 if ( compl_length > 1
4888 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 || (ctrl_x_mode != 0
4890 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4891 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004892 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
4894
Bram Moolenaar572cb562005-08-05 21:35:02 +00004895 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004896 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004898 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899
4900 if (edit_submode_extra == NULL)
4901 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004902 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
4904 edit_submode_extra = (char_u *)_("Back at original");
4905 edit_submode_highl = HLF_W;
4906 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004907 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
4909 edit_submode_extra = (char_u *)_("Word from other line");
4910 edit_submode_highl = HLF_COUNT;
4911 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004912 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 {
4914 edit_submode_extra = (char_u *)_("The only match");
4915 edit_submode_highl = HLF_COUNT;
4916 }
4917 else
4918 {
4919 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004920 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004922 int number = 0;
4923 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004925 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 {
4927 /* search backwards for the first valid (!= -1) number.
4928 * This should normally succeed already at the first loop
4929 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004930 for (match = compl_curr_match->cp_prev; match != NULL
4931 && match != compl_first_match;
4932 match = match->cp_prev)
4933 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004935 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 break;
4937 }
4938 if (match != NULL)
4939 /* go up and assign all numbers which are not assigned
4940 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004941 for (match = match->cp_next;
4942 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004943 match = match->cp_next)
4944 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946 else /* BACKWARD */
4947 {
4948 /* search forwards (upwards) for the first valid (!= -1)
4949 * number. This should normally succeed already at the
4950 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004951 for (match = compl_curr_match->cp_next; match != NULL
4952 && match != compl_first_match;
4953 match = match->cp_next)
4954 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004956 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 break;
4958 }
4959 if (match != NULL)
4960 /* go down and assign all numbers which are not
4961 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004962 for (match = match->cp_prev; match
4963 && match->cp_number == -1;
4964 match = match->cp_prev)
4965 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 }
4967 }
4968
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004969 /* The match should always have a sequence number now, this is
4970 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004971 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00004973 /* Space for 10 text chars. + 2x10-digit no.s = 31.
4974 * Translations may need more than twice that. */
4975 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004977 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00004978 vim_snprintf((char *)match_ref, sizeof(match_ref),
4979 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004980 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00004982 vim_snprintf((char *)match_ref, sizeof(match_ref),
4983 _("match %d"),
4984 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 edit_submode_extra = match_ref;
4986 edit_submode_highl = HLF_R;
4987 if (dollar_vcol)
4988 curs_columns(FALSE);
4989 }
4990 }
4991 }
4992
4993 /* Show a message about what (completion) mode we're in. */
4994 showmode();
4995 if (edit_submode_extra != NULL)
4996 {
4997 if (!p_smd)
4998 msg_attr(edit_submode_extra,
4999 edit_submode_highl < HLF_COUNT
5000 ? hl_attr(edit_submode_highl) : 0);
5001 }
5002 else
5003 msg_clr_cmdline(); /* necessary for "noshowmode" */
5004
Bram Moolenaard68071d2006-05-02 22:08:30 +00005005 /* Show the popup menu, unless we got interrupted. */
5006 if (!compl_interrupted)
5007 {
5008 /* RedrawingDisabled may be set when invoked through complete(). */
5009 n = RedrawingDisabled;
5010 RedrawingDisabled = 0;
5011 ins_compl_show_pum();
5012 setcursor();
5013 RedrawingDisabled = n;
5014 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005015 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005016 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005017
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 return OK;
5019}
5020
5021/*
5022 * Looks in the first "len" chars. of "src" for search-metachars.
5023 * If dest is not NULL the chars. are copied there quoting (with
5024 * a backslash) the metachars, and dest would be NUL terminated.
5025 * Returns the length (needed) of dest
5026 */
5027 static int
5028quote_meta(dest, src, len)
5029 char_u *dest;
5030 char_u *src;
5031 int len;
5032{
5033 int m;
5034
5035 for (m = len; --len >= 0; src++)
5036 {
5037 switch (*src)
5038 {
5039 case '.':
5040 case '*':
5041 case '[':
5042 if (ctrl_x_mode == CTRL_X_DICTIONARY
5043 || ctrl_x_mode == CTRL_X_THESAURUS)
5044 break;
5045 case '~':
5046 if (!p_magic) /* quote these only if magic is set */
5047 break;
5048 case '\\':
5049 if (ctrl_x_mode == CTRL_X_DICTIONARY
5050 || ctrl_x_mode == CTRL_X_THESAURUS)
5051 break;
5052 case '^': /* currently it's not needed. */
5053 case '$':
5054 m++;
5055 if (dest != NULL)
5056 *dest++ = '\\';
5057 break;
5058 }
5059 if (dest != NULL)
5060 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005061# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 /* Copy remaining bytes of a multibyte character. */
5063 if (has_mbyte)
5064 {
5065 int i, mb_len;
5066
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005067 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 if (mb_len > 0 && len >= mb_len)
5069 for (i = 0; i < mb_len; ++i)
5070 {
5071 --len;
5072 ++src;
5073 if (dest != NULL)
5074 *dest++ = *src;
5075 }
5076 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079 if (dest != NULL)
5080 *dest = NUL;
5081
5082 return m;
5083}
5084#endif /* FEAT_INS_EXPAND */
5085
5086/*
5087 * Next character is interpreted literally.
5088 * A one, two or three digit decimal number is interpreted as its byte value.
5089 * If one or two digits are entered, the next character is given to vungetc().
5090 * For Unicode a character > 255 may be returned.
5091 */
5092 int
5093get_literal()
5094{
5095 int cc;
5096 int nc;
5097 int i;
5098 int hex = FALSE;
5099 int octal = FALSE;
5100#ifdef FEAT_MBYTE
5101 int unicode = 0;
5102#endif
5103
5104 if (got_int)
5105 return Ctrl_C;
5106
5107#ifdef FEAT_GUI
5108 /*
5109 * In GUI there is no point inserting the internal code for a special key.
5110 * It is more useful to insert the string "<KEY>" instead. This would
5111 * probably be useful in a text window too, but it would not be
5112 * vi-compatible (maybe there should be an option for it?) -- webb
5113 */
5114 if (gui.in_use)
5115 ++allow_keys;
5116#endif
5117#ifdef USE_ON_FLY_SCROLL
5118 dont_scroll = TRUE; /* disallow scrolling here */
5119#endif
5120 ++no_mapping; /* don't map the next key hits */
5121 cc = 0;
5122 i = 0;
5123 for (;;)
5124 {
5125 do
5126 nc = safe_vgetc();
5127 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
5128 || nc == K_HOR_SCROLLBAR);
5129#ifdef FEAT_CMDL_INFO
5130 if (!(State & CMDLINE)
5131# ifdef FEAT_MBYTE
5132 && MB_BYTE2LEN_CHECK(nc) == 1
5133# endif
5134 )
5135 add_to_showcmd(nc);
5136#endif
5137 if (nc == 'x' || nc == 'X')
5138 hex = TRUE;
5139 else if (nc == 'o' || nc == 'O')
5140 octal = TRUE;
5141#ifdef FEAT_MBYTE
5142 else if (nc == 'u' || nc == 'U')
5143 unicode = nc;
5144#endif
5145 else
5146 {
5147 if (hex
5148#ifdef FEAT_MBYTE
5149 || unicode != 0
5150#endif
5151 )
5152 {
5153 if (!vim_isxdigit(nc))
5154 break;
5155 cc = cc * 16 + hex2nr(nc);
5156 }
5157 else if (octal)
5158 {
5159 if (nc < '0' || nc > '7')
5160 break;
5161 cc = cc * 8 + nc - '0';
5162 }
5163 else
5164 {
5165 if (!VIM_ISDIGIT(nc))
5166 break;
5167 cc = cc * 10 + nc - '0';
5168 }
5169
5170 ++i;
5171 }
5172
5173 if (cc > 255
5174#ifdef FEAT_MBYTE
5175 && unicode == 0
5176#endif
5177 )
5178 cc = 255; /* limit range to 0-255 */
5179 nc = 0;
5180
5181 if (hex) /* hex: up to two chars */
5182 {
5183 if (i >= 2)
5184 break;
5185 }
5186#ifdef FEAT_MBYTE
5187 else if (unicode) /* Unicode: up to four or eight chars */
5188 {
5189 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5190 break;
5191 }
5192#endif
5193 else if (i >= 3) /* decimal or octal: up to three chars */
5194 break;
5195 }
5196 if (i == 0) /* no number entered */
5197 {
5198 if (nc == K_ZERO) /* NUL is stored as NL */
5199 {
5200 cc = '\n';
5201 nc = 0;
5202 }
5203 else
5204 {
5205 cc = nc;
5206 nc = 0;
5207 }
5208 }
5209
5210 if (cc == 0) /* NUL is stored as NL */
5211 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005212#ifdef FEAT_MBYTE
5213 if (enc_dbcs && (cc & 0xff) == 0)
5214 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5215 second byte will cause trouble! */
5216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
5218 --no_mapping;
5219#ifdef FEAT_GUI
5220 if (gui.in_use)
5221 --allow_keys;
5222#endif
5223 if (nc)
5224 vungetc(nc);
5225 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5226 return cc;
5227}
5228
5229/*
5230 * Insert character, taking care of special keys and mod_mask
5231 */
5232 static void
5233insert_special(c, allow_modmask, ctrlv)
5234 int c;
5235 int allow_modmask;
5236 int ctrlv; /* c was typed after CTRL-V */
5237{
5238 char_u *p;
5239 int len;
5240
5241 /*
5242 * Special function key, translate into "<Key>". Up to the last '>' is
5243 * inserted with ins_str(), so as not to replace characters in replace
5244 * mode.
5245 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5246 * unless 'allow_modmask' is TRUE.
5247 */
5248#ifdef MACOS
5249 /* Command-key never produces a normal key */
5250 if (mod_mask & MOD_MASK_CMD)
5251 allow_modmask = TRUE;
5252#endif
5253 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5254 {
5255 p = get_special_key_name(c, mod_mask);
5256 len = (int)STRLEN(p);
5257 c = p[len - 1];
5258 if (len > 2)
5259 {
5260 if (stop_arrow() == FAIL)
5261 return;
5262 p[len - 1] = NUL;
5263 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005264 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 ctrlv = FALSE;
5266 }
5267 }
5268 if (stop_arrow() == OK)
5269 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5270}
5271
5272/*
5273 * Special characters in this context are those that need processing other
5274 * than the simple insertion that can be performed here. This includes ESC
5275 * which terminates the insert, and CR/NL which need special processing to
5276 * open up a new line. This routine tries to optimize insertions performed by
5277 * the "redo", "undo" or "put" commands, so it needs to know when it should
5278 * stop and defer processing to the "normal" mechanism.
5279 * '0' and '^' are special, because they can be followed by CTRL-D.
5280 */
5281#ifdef EBCDIC
5282# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5283#else
5284# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5285#endif
5286
5287#ifdef FEAT_MBYTE
5288# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5289#else
5290# define WHITECHAR(cc) vim_iswhite(cc)
5291#endif
5292
5293 void
5294insertchar(c, flags, second_indent)
5295 int c; /* character to insert or NUL */
5296 int flags; /* INSCHAR_FORMAT, etc. */
5297 int second_indent; /* indent for second line if >= 0 */
5298{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 int textwidth;
5300#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
5305 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5306 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307
5308 /*
5309 * Try to break the line in two or more pieces when:
5310 * - Always do this if we have been called to do formatting only.
5311 * - Always do this when 'formatoptions' has the 'a' flag and the line
5312 * ends in white space.
5313 * - Otherwise:
5314 * - Don't do this if inserting a blank
5315 * - Don't do this if an existing character is being replaced, unless
5316 * we're in VREPLACE mode.
5317 * - Do this if the cursor is not on the line where insert started
5318 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5319 * before the insert.
5320 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5321 * before 'textwidth'
5322 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005323 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 && ((flags & INSCHAR_FORMAT)
5325 || (!vim_iswhite(c)
5326 && !((State & REPLACE_FLAG)
5327#ifdef FEAT_VREPLACE
5328 && !(State & VREPLACE_FLAG)
5329#endif
5330 && *ml_get_cursor() != NUL)
5331 && (curwin->w_cursor.lnum != Insstart.lnum
5332 || ((!has_format_option(FO_INS_LONG)
5333 || Insstart_textlen <= (colnr_T)textwidth)
5334 && (!fo_ins_blank
5335 || Insstart_blank_vcol <= (colnr_T)textwidth
5336 ))))))
5337 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005338 /* Format with 'formatexpr' when it's set. Use internal formatting
5339 * when 'formatexpr' isn't set or it returns non-zero. */
5340#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005341 int do_internal = TRUE;
5342
5343 if (*curbuf->b_p_fex != NUL)
5344 {
5345 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5346 /* It may be required to save for undo again, e.g. when setline()
5347 * was called. */
5348 ins_need_undo = TRUE;
5349 }
5350 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005352 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005354
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 if (c == NUL) /* only formatting was wanted */
5356 return;
5357
5358#ifdef FEAT_COMMENTS
5359 /* Check whether this character should end a comment. */
5360 if (did_ai && (int)c == end_comment_pending)
5361 {
5362 char_u *line;
5363 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5364 int middle_len, end_len;
5365 int i;
5366
5367 /*
5368 * Need to remove existing (middle) comment leader and insert end
5369 * comment leader. First, check what comment leader we can find.
5370 */
5371 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5372 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5373 {
5374 /* Skip middle-comment string */
5375 while (*p && p[-1] != ':') /* find end of middle flags */
5376 ++p;
5377 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5378 /* Don't count trailing white space for middle_len */
5379 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5380 --middle_len;
5381
5382 /* Find the end-comment string */
5383 while (*p && p[-1] != ':') /* find end of end flags */
5384 ++p;
5385 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5386
5387 /* Skip white space before the cursor */
5388 i = curwin->w_cursor.col;
5389 while (--i >= 0 && vim_iswhite(line[i]))
5390 ;
5391 i++;
5392
5393 /* Skip to before the middle leader */
5394 i -= middle_len;
5395
5396 /* Check some expected things before we go on */
5397 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5398 {
5399 /* Backspace over all the stuff we want to replace */
5400 backspace_until_column(i);
5401
5402 /*
5403 * Insert the end-comment string, except for the last
5404 * character, which will get inserted as normal later.
5405 */
5406 ins_bytes_len(lead_end, end_len - 1);
5407 }
5408 }
5409 }
5410 end_comment_pending = NUL;
5411#endif
5412
5413 did_ai = FALSE;
5414#ifdef FEAT_SMARTINDENT
5415 did_si = FALSE;
5416 can_si = FALSE;
5417 can_si_back = FALSE;
5418#endif
5419
5420 /*
5421 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5422 * This speeds up normal text input considerably.
5423 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5424 * need to re-indent at a ':', or any other character (but not what
5425 * 'paste' is set)..
5426 */
5427#ifdef USE_ON_FLY_SCROLL
5428 dont_scroll = FALSE; /* allow scrolling here */
5429#endif
5430
5431 if ( !ISSPECIAL(c)
5432#ifdef FEAT_MBYTE
5433 && (!has_mbyte || (*mb_char2len)(c) == 1)
5434#endif
5435 && vpeekc() != NUL
5436 && !(State & REPLACE_FLAG)
5437#ifdef FEAT_CINDENT
5438 && !cindent_on()
5439#endif
5440#ifdef FEAT_RIGHTLEFT
5441 && !p_ri
5442#endif
5443 )
5444 {
5445#define INPUT_BUFLEN 100
5446 char_u buf[INPUT_BUFLEN + 1];
5447 int i;
5448 colnr_T virtcol = 0;
5449
5450 buf[0] = c;
5451 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005452 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 virtcol = get_nolist_virtcol();
5454 /*
5455 * Stop the string when:
5456 * - no more chars available
5457 * - finding a special character (command key)
5458 * - buffer is full
5459 * - running into the 'textwidth' boundary
5460 * - need to check for abbreviation: A non-word char after a word-char
5461 */
5462 while ( (c = vpeekc()) != NUL
5463 && !ISSPECIAL(c)
5464#ifdef FEAT_MBYTE
5465 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5466#endif
5467 && i < INPUT_BUFLEN
5468 && (textwidth == 0
5469 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5470 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5471 {
5472#ifdef FEAT_RIGHTLEFT
5473 c = vgetc();
5474 if (p_hkmap && KeyTyped)
5475 c = hkmap(c); /* Hebrew mode mapping */
5476# ifdef FEAT_FKMAP
5477 if (p_fkmap && KeyTyped)
5478 c = fkmap(c); /* Farsi mode mapping */
5479# endif
5480 buf[i++] = c;
5481#else
5482 buf[i++] = vgetc();
5483#endif
5484 }
5485
5486#ifdef FEAT_DIGRAPHS
5487 do_digraph(-1); /* clear digraphs */
5488 do_digraph(buf[i-1]); /* may be the start of a digraph */
5489#endif
5490 buf[i] = NUL;
5491 ins_str(buf);
5492 if (flags & INSCHAR_CTRLV)
5493 {
5494 redo_literal(*buf);
5495 i = 1;
5496 }
5497 else
5498 i = 0;
5499 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005500 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
5502 else
5503 {
5504#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005505 int cc;
5506
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5508 {
5509 char_u buf[MB_MAXBYTES + 1];
5510
5511 (*mb_char2bytes)(c, buf);
5512 buf[cc] = NUL;
5513 ins_char_bytes(buf, cc);
5514 AppendCharToRedobuff(c);
5515 }
5516 else
5517#endif
5518 {
5519 ins_char(c);
5520 if (flags & INSCHAR_CTRLV)
5521 redo_literal(c);
5522 else
5523 AppendCharToRedobuff(c);
5524 }
5525 }
5526}
5527
5528/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005529 * Format text at the current insert position.
5530 */
5531 static void
5532internal_format(textwidth, second_indent, flags, format_only)
5533 int textwidth;
5534 int second_indent;
5535 int flags;
5536 int format_only;
5537{
5538 int cc;
5539 int save_char = NUL;
5540 int haveto_redraw = FALSE;
5541 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5542#ifdef FEAT_MBYTE
5543 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5544#endif
5545 int fo_white_par = has_format_option(FO_WHITE_PAR);
5546 int first_line = TRUE;
5547#ifdef FEAT_COMMENTS
5548 colnr_T leader_len;
5549 int no_leader = FALSE;
5550 int do_comments = (flags & INSCHAR_DO_COM);
5551#endif
5552
5553 /*
5554 * When 'ai' is off we don't want a space under the cursor to be
5555 * deleted. Replace it with an 'x' temporarily.
5556 */
5557 if (!curbuf->b_p_ai)
5558 {
5559 cc = gchar_cursor();
5560 if (vim_iswhite(cc))
5561 {
5562 save_char = cc;
5563 pchar_cursor('x');
5564 }
5565 }
5566
5567 /*
5568 * Repeat breaking lines, until the current line is not too long.
5569 */
5570 while (!got_int)
5571 {
5572 int startcol; /* Cursor column at entry */
5573 int wantcol; /* column at textwidth border */
5574 int foundcol; /* column for start of spaces */
5575 int end_foundcol = 0; /* column for start of word */
5576 colnr_T len;
5577 colnr_T virtcol;
5578#ifdef FEAT_VREPLACE
5579 int orig_col = 0;
5580 char_u *saved_text = NULL;
5581#endif
5582 colnr_T col;
5583
5584 virtcol = get_nolist_virtcol();
5585 if (virtcol < (colnr_T)textwidth)
5586 break;
5587
5588#ifdef FEAT_COMMENTS
5589 if (no_leader)
5590 do_comments = FALSE;
5591 else if (!(flags & INSCHAR_FORMAT)
5592 && has_format_option(FO_WRAP_COMS))
5593 do_comments = TRUE;
5594
5595 /* Don't break until after the comment leader */
5596 if (do_comments)
5597 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5598 else
5599 leader_len = 0;
5600
5601 /* If the line doesn't start with a comment leader, then don't
5602 * start one in a following broken line. Avoids that a %word
5603 * moved to the start of the next line causes all following lines
5604 * to start with %. */
5605 if (leader_len == 0)
5606 no_leader = TRUE;
5607#endif
5608 if (!(flags & INSCHAR_FORMAT)
5609#ifdef FEAT_COMMENTS
5610 && leader_len == 0
5611#endif
5612 && !has_format_option(FO_WRAP))
5613
5614 {
5615 textwidth = 0;
5616 break;
5617 }
5618 if ((startcol = curwin->w_cursor.col) == 0)
5619 break;
5620
5621 /* find column of textwidth border */
5622 coladvance((colnr_T)textwidth);
5623 wantcol = curwin->w_cursor.col;
5624
5625 curwin->w_cursor.col = startcol - 1;
5626#ifdef FEAT_MBYTE
5627 /* Correct cursor for multi-byte character. */
5628 if (has_mbyte)
5629 mb_adjust_cursor();
5630#endif
5631 foundcol = 0;
5632
5633 /*
5634 * Find position to break at.
5635 * Stop at first entered white when 'formatoptions' has 'v'
5636 */
5637 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5638 || curwin->w_cursor.lnum != Insstart.lnum
5639 || curwin->w_cursor.col >= Insstart.col)
5640 {
5641 cc = gchar_cursor();
5642 if (WHITECHAR(cc))
5643 {
5644 /* remember position of blank just before text */
5645 end_foundcol = curwin->w_cursor.col;
5646
5647 /* find start of sequence of blanks */
5648 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5649 {
5650 dec_cursor();
5651 cc = gchar_cursor();
5652 }
5653 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5654 break; /* only spaces in front of text */
5655#ifdef FEAT_COMMENTS
5656 /* Don't break until after the comment leader */
5657 if (curwin->w_cursor.col < leader_len)
5658 break;
5659#endif
5660 if (has_format_option(FO_ONE_LETTER))
5661 {
5662 /* do not break after one-letter words */
5663 if (curwin->w_cursor.col == 0)
5664 break; /* one-letter word at begin */
5665
5666 col = curwin->w_cursor.col;
5667 dec_cursor();
5668 cc = gchar_cursor();
5669
5670 if (WHITECHAR(cc))
5671 continue; /* one-letter, continue */
5672 curwin->w_cursor.col = col;
5673 }
5674#ifdef FEAT_MBYTE
5675 if (has_mbyte)
5676 foundcol = curwin->w_cursor.col
5677 + (*mb_ptr2len)(ml_get_cursor());
5678 else
5679#endif
5680 foundcol = curwin->w_cursor.col + 1;
5681 if (curwin->w_cursor.col < (colnr_T)wantcol)
5682 break;
5683 }
5684#ifdef FEAT_MBYTE
5685 else if (cc >= 0x100 && fo_multibyte
5686 && curwin->w_cursor.col <= (colnr_T)wantcol)
5687 {
5688 /* Break after or before a multi-byte character. */
5689 foundcol = curwin->w_cursor.col;
5690 if (curwin->w_cursor.col < (colnr_T)wantcol)
5691 foundcol += (*mb_char2len)(cc);
5692 end_foundcol = foundcol;
5693 break;
5694 }
5695#endif
5696 if (curwin->w_cursor.col == 0)
5697 break;
5698 dec_cursor();
5699 }
5700
5701 if (foundcol == 0) /* no spaces, cannot break line */
5702 {
5703 curwin->w_cursor.col = startcol;
5704 break;
5705 }
5706
5707 /* Going to break the line, remove any "$" now. */
5708 undisplay_dollar();
5709
5710 /*
5711 * Offset between cursor position and line break is used by replace
5712 * stack functions. VREPLACE does not use this, and backspaces
5713 * over the text instead.
5714 */
5715#ifdef FEAT_VREPLACE
5716 if (State & VREPLACE_FLAG)
5717 orig_col = startcol; /* Will start backspacing from here */
5718 else
5719#endif
5720 replace_offset = startcol - end_foundcol - 1;
5721
5722 /*
5723 * adjust startcol for spaces that will be deleted and
5724 * characters that will remain on top line
5725 */
5726 curwin->w_cursor.col = foundcol;
5727 while (cc = gchar_cursor(), WHITECHAR(cc))
5728 inc_cursor();
5729 startcol -= curwin->w_cursor.col;
5730 if (startcol < 0)
5731 startcol = 0;
5732
5733#ifdef FEAT_VREPLACE
5734 if (State & VREPLACE_FLAG)
5735 {
5736 /*
5737 * In VREPLACE mode, we will backspace over the text to be
5738 * wrapped, so save a copy now to put on the next line.
5739 */
5740 saved_text = vim_strsave(ml_get_cursor());
5741 curwin->w_cursor.col = orig_col;
5742 if (saved_text == NULL)
5743 break; /* Can't do it, out of memory */
5744 saved_text[startcol] = NUL;
5745
5746 /* Backspace over characters that will move to the next line */
5747 if (!fo_white_par)
5748 backspace_until_column(foundcol);
5749 }
5750 else
5751#endif
5752 {
5753 /* put cursor after pos. to break line */
5754 if (!fo_white_par)
5755 curwin->w_cursor.col = foundcol;
5756 }
5757
5758 /*
5759 * Split the line just before the margin.
5760 * Only insert/delete lines, but don't really redraw the window.
5761 */
5762 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5763 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5764#ifdef FEAT_COMMENTS
5765 + (do_comments ? OPENLINE_DO_COM : 0)
5766#endif
5767 , old_indent);
5768 old_indent = 0;
5769
5770 replace_offset = 0;
5771 if (first_line)
5772 {
5773 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5774 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5775 if (second_indent >= 0)
5776 {
5777#ifdef FEAT_VREPLACE
5778 if (State & VREPLACE_FLAG)
5779 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5780 else
5781#endif
5782 (void)set_indent(second_indent, SIN_CHANGED);
5783 }
5784 first_line = FALSE;
5785 }
5786
5787#ifdef FEAT_VREPLACE
5788 if (State & VREPLACE_FLAG)
5789 {
5790 /*
5791 * In VREPLACE mode we have backspaced over the text to be
5792 * moved, now we re-insert it into the new line.
5793 */
5794 ins_bytes(saved_text);
5795 vim_free(saved_text);
5796 }
5797 else
5798#endif
5799 {
5800 /*
5801 * Check if cursor is not past the NUL off the line, cindent
5802 * may have added or removed indent.
5803 */
5804 curwin->w_cursor.col += startcol;
5805 len = (colnr_T)STRLEN(ml_get_curline());
5806 if (curwin->w_cursor.col > len)
5807 curwin->w_cursor.col = len;
5808 }
5809
5810 haveto_redraw = TRUE;
5811#ifdef FEAT_CINDENT
5812 can_cindent = TRUE;
5813#endif
5814 /* moved the cursor, don't autoindent or cindent now */
5815 did_ai = FALSE;
5816#ifdef FEAT_SMARTINDENT
5817 did_si = FALSE;
5818 can_si = FALSE;
5819 can_si_back = FALSE;
5820#endif
5821 line_breakcheck();
5822 }
5823
5824 if (save_char != NUL) /* put back space after cursor */
5825 pchar_cursor(save_char);
5826
5827 if (!format_only && haveto_redraw)
5828 {
5829 update_topline();
5830 redraw_curbuf_later(VALID);
5831 }
5832}
5833
5834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 * Called after inserting or deleting text: When 'formatoptions' includes the
5836 * 'a' flag format from the current line until the end of the paragraph.
5837 * Keep the cursor at the same position relative to the text.
5838 * The caller must have saved the cursor line for undo, following ones will be
5839 * saved here.
5840 */
5841 void
5842auto_format(trailblank, prev_line)
5843 int trailblank; /* when TRUE also format with trailing blank */
5844 int prev_line; /* may start in previous line */
5845{
5846 pos_T pos;
5847 colnr_T len;
5848 char_u *old;
5849 char_u *new, *pnew;
5850 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005851 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
5853 if (!has_format_option(FO_AUTO))
5854 return;
5855
5856 pos = curwin->w_cursor;
5857 old = ml_get_curline();
5858
5859 /* may remove added space */
5860 check_auto_format(FALSE);
5861
5862 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5863 * user might insert normal text next. Also skip formatting when "1" is
5864 * in 'formatoptions' and there is a single character before the cursor.
5865 * Otherwise the line would be broken and when typing another non-white
5866 * next they are not joined back together. */
5867 wasatend = (pos.col == STRLEN(old));
5868 if (*old != NUL && !trailblank && wasatend)
5869 {
5870 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005871 cc = gchar_cursor();
5872 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5873 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005875 cc = gchar_cursor();
5876 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 {
5878 curwin->w_cursor = pos;
5879 return;
5880 }
5881 curwin->w_cursor = pos;
5882 }
5883
5884#ifdef FEAT_COMMENTS
5885 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5886 * comments. */
5887 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5888 && get_leader_len(old, NULL, FALSE) == 0)
5889 return;
5890#endif
5891
5892 /*
5893 * May start formatting in a previous line, so that after "x" a word is
5894 * moved to the previous line if it fits there now. Only when this is not
5895 * the start of a paragraph.
5896 */
5897 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5898 {
5899 --curwin->w_cursor.lnum;
5900 if (u_save_cursor() == FAIL)
5901 return;
5902 }
5903
5904 /*
5905 * Do the formatting and restore the cursor position. "saved_cursor" will
5906 * be adjusted for the text formatting.
5907 */
5908 saved_cursor = pos;
5909 format_lines((linenr_T)-1);
5910 curwin->w_cursor = saved_cursor;
5911 saved_cursor.lnum = 0;
5912
5913 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5914 {
5915 /* "cannot happen" */
5916 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5917 coladvance((colnr_T)MAXCOL);
5918 }
5919 else
5920 check_cursor_col();
5921
5922 /* Insert mode: If the cursor is now after the end of the line while it
5923 * previously wasn't, the line was broken. Because of the rule above we
5924 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5925 * formatted. */
5926 if (!wasatend && has_format_option(FO_WHITE_PAR))
5927 {
5928 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005929 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 if (curwin->w_cursor.col == len)
5931 {
5932 pnew = vim_strnsave(new, len + 2);
5933 pnew[len] = ' ';
5934 pnew[len + 1] = NUL;
5935 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5936 /* remove the space later */
5937 did_add_space = TRUE;
5938 }
5939 else
5940 /* may remove added space */
5941 check_auto_format(FALSE);
5942 }
5943
5944 check_cursor();
5945}
5946
5947/*
5948 * When an extra space was added to continue a paragraph for auto-formatting,
5949 * delete it now. The space must be under the cursor, just after the insert
5950 * position.
5951 */
5952 static void
5953check_auto_format(end_insert)
5954 int end_insert; /* TRUE when ending Insert mode */
5955{
5956 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005957 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958
5959 if (did_add_space)
5960 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005961 cc = gchar_cursor();
5962 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 /* Somehow the space was removed already. */
5964 did_add_space = FALSE;
5965 else
5966 {
5967 if (!end_insert)
5968 {
5969 inc_cursor();
5970 c = gchar_cursor();
5971 dec_cursor();
5972 }
5973 if (c != NUL)
5974 {
5975 /* The space is no longer at the end of the line, delete it. */
5976 del_char(FALSE);
5977 did_add_space = FALSE;
5978 }
5979 }
5980 }
5981}
5982
5983/*
5984 * Find out textwidth to be used for formatting:
5985 * if 'textwidth' option is set, use it
5986 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5987 * if invalid value, use 0.
5988 * Set default to window width (maximum 79) for "gq" operator.
5989 */
5990 int
5991comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005992 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
5994 int textwidth;
5995
5996 textwidth = curbuf->b_p_tw;
5997 if (textwidth == 0 && curbuf->b_p_wm)
5998 {
5999 /* The width is the window width minus 'wrapmargin' minus all the
6000 * things that add to the margin. */
6001 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6002#ifdef FEAT_CMDWIN
6003 if (cmdwin_type != 0)
6004 textwidth -= 1;
6005#endif
6006#ifdef FEAT_FOLDING
6007 textwidth -= curwin->w_p_fdc;
6008#endif
6009#ifdef FEAT_SIGNS
6010 if (curwin->w_buffer->b_signlist != NULL
6011# ifdef FEAT_NETBEANS_INTG
6012 || usingNetbeans
6013# endif
6014 )
6015 textwidth -= 1;
6016#endif
6017 if (curwin->w_p_nu)
6018 textwidth -= 8;
6019 }
6020 if (textwidth < 0)
6021 textwidth = 0;
6022 if (ff && textwidth == 0)
6023 {
6024 textwidth = W_WIDTH(curwin) - 1;
6025 if (textwidth > 79)
6026 textwidth = 79;
6027 }
6028 return textwidth;
6029}
6030
6031/*
6032 * Put a character in the redo buffer, for when just after a CTRL-V.
6033 */
6034 static void
6035redo_literal(c)
6036 int c;
6037{
6038 char_u buf[10];
6039
6040 /* Only digits need special treatment. Translate them into a string of
6041 * three digits. */
6042 if (VIM_ISDIGIT(c))
6043 {
6044 sprintf((char *)buf, "%03d", c);
6045 AppendToRedobuff(buf);
6046 }
6047 else
6048 AppendCharToRedobuff(c);
6049}
6050
6051/*
6052 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006053 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 */
6055 static void
6056start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006057 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
6059 if (!arrow_used) /* something has been inserted */
6060 {
6061 AppendToRedobuff(ESC_STR);
6062 stop_insert(end_insert_pos, FALSE);
6063 arrow_used = TRUE; /* this means we stopped the current insert */
6064 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006065#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006066 check_spell_redraw();
6067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068}
6069
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006070#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006071/*
6072 * If we skipped highlighting word at cursor, do it now.
6073 * It may be skipped again, thus reset spell_redraw_lnum first.
6074 */
6075 static void
6076check_spell_redraw()
6077{
6078 if (spell_redraw_lnum != 0)
6079 {
6080 linenr_T lnum = spell_redraw_lnum;
6081
6082 spell_redraw_lnum = 0;
6083 redrawWinline(lnum, FALSE);
6084 }
6085}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006086
6087/*
6088 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6089 * spelled word, if there is one.
6090 */
6091 static void
6092spell_back_to_badword()
6093{
6094 pos_T tpos = curwin->w_cursor;
6095
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006096 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006097 if (curwin->w_cursor.col != tpos.col)
6098 start_arrow(&tpos);
6099}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006100#endif
6101
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102/*
6103 * stop_arrow() is called before a change is made in insert mode.
6104 * If an arrow key has been used, start a new insertion.
6105 * Returns FAIL if undo is impossible, shouldn't insert then.
6106 */
6107 int
6108stop_arrow()
6109{
6110 if (arrow_used)
6111 {
6112 if (u_save_cursor() == OK)
6113 {
6114 arrow_used = FALSE;
6115 ins_need_undo = FALSE;
6116 }
6117 Insstart = curwin->w_cursor; /* new insertion starts here */
6118 Insstart_textlen = linetabsize(ml_get_curline());
6119 ai_col = 0;
6120#ifdef FEAT_VREPLACE
6121 if (State & VREPLACE_FLAG)
6122 {
6123 orig_line_count = curbuf->b_ml.ml_line_count;
6124 vr_lines_changed = 1;
6125 }
6126#endif
6127 ResetRedobuff();
6128 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006129 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 }
6131 else if (ins_need_undo)
6132 {
6133 if (u_save_cursor() == OK)
6134 ins_need_undo = FALSE;
6135 }
6136
6137#ifdef FEAT_FOLDING
6138 /* Always open fold at the cursor line when inserting something. */
6139 foldOpenCursor();
6140#endif
6141
6142 return (arrow_used || ins_need_undo ? FAIL : OK);
6143}
6144
6145/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006146 * Do a few things to stop inserting.
6147 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6148 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 */
6150 static void
6151stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006152 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006153 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006155 int cc;
6156 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157
6158 stop_redo_ins();
6159 replace_flush(); /* abandon replace stack */
6160
6161 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006162 * Save the inserted text for later redo with ^@ and CTRL-A.
6163 * Don't do it when "restart_edit" was set and nothing was inserted,
6164 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006166 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006167 if (did_restart_edit == 0 || (ptr != NULL
6168 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006169 {
6170 vim_free(last_insert);
6171 last_insert = ptr;
6172 last_insert_skip = new_insert_skip;
6173 }
6174 else
6175 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006177 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 {
6179 /* Auto-format now. It may seem strange to do this when stopping an
6180 * insertion (or moving the cursor), but it's required when appending
6181 * a line and having it end in a space. But only do it when something
6182 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006183 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006185 pos_T tpos = curwin->w_cursor;
6186
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 /* When the cursor is at the end of the line after a space the
6188 * formatting will move it to the following word. Avoid that by
6189 * moving the cursor onto the space. */
6190 cc = 'x';
6191 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6192 {
6193 dec_cursor();
6194 cc = gchar_cursor();
6195 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006196 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 }
6198
6199 auto_format(TRUE, FALSE);
6200
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006201 if (vim_iswhite(cc))
6202 {
6203 if (gchar_cursor() != NUL)
6204 inc_cursor();
6205#ifdef FEAT_VIRTUALEDIT
6206 /* If the cursor is still at the same character, also keep
6207 * the "coladd". */
6208 if (gchar_cursor() == NUL
6209 && curwin->w_cursor.lnum == tpos.lnum
6210 && curwin->w_cursor.col == tpos.col)
6211 curwin->w_cursor.coladd = tpos.coladd;
6212#endif
6213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 }
6215
6216 /* If a space was inserted for auto-formatting, remove it now. */
6217 check_auto_format(TRUE);
6218
6219 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006220 * of the line, and put the cursor back.
6221 * Do this when ESC was used or moving the cursor up/down. */
6222 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006223 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006225 pos_T tpos = curwin->w_cursor;
6226
6227 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006228 for (;;)
6229 {
6230 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6231 --curwin->w_cursor.col;
6232 cc = gchar_cursor();
6233 if (!vim_iswhite(cc))
6234 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006236 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006237 if (curwin->w_cursor.lnum != tpos.lnum)
6238 curwin->w_cursor = tpos;
6239 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6241
6242#ifdef FEAT_VISUAL
6243 /* <C-S-Right> may have started Visual mode, adjust the position for
6244 * deleted characters. */
6245 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6246 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006247 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 if (VIsual.col > (colnr_T)cc)
6249 {
6250 VIsual.col = cc;
6251# ifdef FEAT_VIRTUALEDIT
6252 VIsual.coladd = 0;
6253# endif
6254 }
6255 }
6256#endif
6257 }
6258 }
6259 did_ai = FALSE;
6260#ifdef FEAT_SMARTINDENT
6261 did_si = FALSE;
6262 can_si = FALSE;
6263 can_si_back = FALSE;
6264#endif
6265
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006266 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6267 * now in a different buffer. */
6268 if (end_insert_pos != NULL)
6269 {
6270 curbuf->b_op_start = Insstart;
6271 curbuf->b_op_end = *end_insert_pos;
6272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273}
6274
6275/*
6276 * Set the last inserted text to a single character.
6277 * Used for the replace command.
6278 */
6279 void
6280set_last_insert(c)
6281 int c;
6282{
6283 char_u *s;
6284
6285 vim_free(last_insert);
6286#ifdef FEAT_MBYTE
6287 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6288#else
6289 last_insert = alloc(6);
6290#endif
6291 if (last_insert != NULL)
6292 {
6293 s = last_insert;
6294 /* Use the CTRL-V only when entering a special char */
6295 if (c < ' ' || c == DEL)
6296 *s++ = Ctrl_V;
6297 s = add_char2buf(c, s);
6298 *s++ = ESC;
6299 *s++ = NUL;
6300 last_insert_skip = 0;
6301 }
6302}
6303
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006304#if defined(EXITFREE) || defined(PROTO)
6305 void
6306free_last_insert()
6307{
6308 vim_free(last_insert);
6309 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006310 vim_free(compl_orig_text);
6311 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006312}
6313#endif
6314
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315/*
6316 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6317 * and CSI. Handle multi-byte characters.
6318 * Returns a pointer to after the added bytes.
6319 */
6320 char_u *
6321add_char2buf(c, s)
6322 int c;
6323 char_u *s;
6324{
6325#ifdef FEAT_MBYTE
6326 char_u temp[MB_MAXBYTES];
6327 int i;
6328 int len;
6329
6330 len = (*mb_char2bytes)(c, temp);
6331 for (i = 0; i < len; ++i)
6332 {
6333 c = temp[i];
6334#endif
6335 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6336 if (c == K_SPECIAL)
6337 {
6338 *s++ = K_SPECIAL;
6339 *s++ = KS_SPECIAL;
6340 *s++ = KE_FILLER;
6341 }
6342#ifdef FEAT_GUI
6343 else if (c == CSI)
6344 {
6345 *s++ = CSI;
6346 *s++ = KS_EXTRA;
6347 *s++ = (int)KE_CSI;
6348 }
6349#endif
6350 else
6351 *s++ = c;
6352#ifdef FEAT_MBYTE
6353 }
6354#endif
6355 return s;
6356}
6357
6358/*
6359 * move cursor to start of line
6360 * if flags & BL_WHITE move to first non-white
6361 * if flags & BL_SOL move to first non-white if startofline is set,
6362 * otherwise keep "curswant" column
6363 * if flags & BL_FIX don't leave the cursor on a NUL.
6364 */
6365 void
6366beginline(flags)
6367 int flags;
6368{
6369 if ((flags & BL_SOL) && !p_sol)
6370 coladvance(curwin->w_curswant);
6371 else
6372 {
6373 curwin->w_cursor.col = 0;
6374#ifdef FEAT_VIRTUALEDIT
6375 curwin->w_cursor.coladd = 0;
6376#endif
6377
6378 if (flags & (BL_WHITE | BL_SOL))
6379 {
6380 char_u *ptr;
6381
6382 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6383 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6384 ++curwin->w_cursor.col;
6385 }
6386 curwin->w_set_curswant = TRUE;
6387 }
6388}
6389
6390/*
6391 * oneright oneleft cursor_down cursor_up
6392 *
6393 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006394 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 * Return OK when successful, FAIL when we hit a line of file boundary.
6396 */
6397
6398 int
6399oneright()
6400{
6401 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403
6404#ifdef FEAT_VIRTUALEDIT
6405 if (virtual_active())
6406 {
6407 pos_T prevpos = curwin->w_cursor;
6408
6409 /* Adjust for multi-wide char (excluding TAB) */
6410 ptr = ml_get_cursor();
6411 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006412# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006414# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006416# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 ))
6418 ? ptr2cells(ptr) : 1));
6419 curwin->w_set_curswant = TRUE;
6420 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6421 return (prevpos.col != curwin->w_cursor.col
6422 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6423 }
6424#endif
6425
6426 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006427 if (*ptr == NUL)
6428 return FAIL; /* already at the very end */
6429
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006431 if (has_mbyte)
6432 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 else
6434#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006435 l = 1;
6436
6437 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6438 * contains "onemore". */
6439 if (ptr[l] == NUL
6440#ifdef FEAT_VIRTUALEDIT
6441 && (ve_flags & VE_ONEMORE) == 0
6442#endif
6443 )
6444 return FAIL;
6445 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446
6447 curwin->w_set_curswant = TRUE;
6448 return OK;
6449}
6450
6451 int
6452oneleft()
6453{
6454#ifdef FEAT_VIRTUALEDIT
6455 if (virtual_active())
6456 {
6457 int width;
6458 int v = getviscol();
6459
6460 if (v == 0)
6461 return FAIL;
6462
6463# ifdef FEAT_LINEBREAK
6464 /* We might get stuck on 'showbreak', skip over it. */
6465 width = 1;
6466 for (;;)
6467 {
6468 coladvance(v - width);
6469 /* getviscol() is slow, skip it when 'showbreak' is empty and
6470 * there are no multi-byte characters */
6471 if ((*p_sbr == NUL
6472# ifdef FEAT_MBYTE
6473 && !has_mbyte
6474# endif
6475 ) || getviscol() < v)
6476 break;
6477 ++width;
6478 }
6479# else
6480 coladvance(v - 1);
6481# endif
6482
6483 if (curwin->w_cursor.coladd == 1)
6484 {
6485 char_u *ptr;
6486
6487 /* Adjust for multi-wide char (not a TAB) */
6488 ptr = ml_get_cursor();
6489 if (*ptr != TAB && vim_isprintc(
6490# ifdef FEAT_MBYTE
6491 (*mb_ptr2char)(ptr)
6492# else
6493 *ptr
6494# endif
6495 ) && ptr2cells(ptr) > 1)
6496 curwin->w_cursor.coladd = 0;
6497 }
6498
6499 curwin->w_set_curswant = TRUE;
6500 return OK;
6501 }
6502#endif
6503
6504 if (curwin->w_cursor.col == 0)
6505 return FAIL;
6506
6507 curwin->w_set_curswant = TRUE;
6508 --curwin->w_cursor.col;
6509
6510#ifdef FEAT_MBYTE
6511 /* if the character on the left of the current cursor is a multi-byte
6512 * character, move to its first byte */
6513 if (has_mbyte)
6514 mb_adjust_cursor();
6515#endif
6516 return OK;
6517}
6518
6519 int
6520cursor_up(n, upd_topline)
6521 long n;
6522 int upd_topline; /* When TRUE: update topline */
6523{
6524 linenr_T lnum;
6525
6526 if (n > 0)
6527 {
6528 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006529 /* This fails if the cursor is already in the first line or the count
6530 * is larger than the line number and '-' is in 'cpoptions' */
6531 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 return FAIL;
6533 if (n >= lnum)
6534 lnum = 1;
6535 else
6536#ifdef FEAT_FOLDING
6537 if (hasAnyFolding(curwin))
6538 {
6539 /*
6540 * Count each sequence of folded lines as one logical line.
6541 */
6542 /* go to the the start of the current fold */
6543 (void)hasFolding(lnum, &lnum, NULL);
6544
6545 while (n--)
6546 {
6547 /* move up one line */
6548 --lnum;
6549 if (lnum <= 1)
6550 break;
6551 /* If we entered a fold, move to the beginning, unless in
6552 * Insert mode or when 'foldopen' contains "all": it will open
6553 * in a moment. */
6554 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6555 (void)hasFolding(lnum, &lnum, NULL);
6556 }
6557 if (lnum < 1)
6558 lnum = 1;
6559 }
6560 else
6561#endif
6562 lnum -= n;
6563 curwin->w_cursor.lnum = lnum;
6564 }
6565
6566 /* try to advance to the column we want to be at */
6567 coladvance(curwin->w_curswant);
6568
6569 if (upd_topline)
6570 update_topline(); /* make sure curwin->w_topline is valid */
6571
6572 return OK;
6573}
6574
6575/*
6576 * Cursor down a number of logical lines.
6577 */
6578 int
6579cursor_down(n, upd_topline)
6580 long n;
6581 int upd_topline; /* When TRUE: update topline */
6582{
6583 linenr_T lnum;
6584
6585 if (n > 0)
6586 {
6587 lnum = curwin->w_cursor.lnum;
6588#ifdef FEAT_FOLDING
6589 /* Move to last line of fold, will fail if it's the end-of-file. */
6590 (void)hasFolding(lnum, NULL, &lnum);
6591#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006592 /* This fails if the cursor is already in the last line or would move
6593 * beyound the last line and '-' is in 'cpoptions' */
6594 if (lnum >= curbuf->b_ml.ml_line_count
6595 || (lnum + n > curbuf->b_ml.ml_line_count
6596 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 return FAIL;
6598 if (lnum + n >= curbuf->b_ml.ml_line_count)
6599 lnum = curbuf->b_ml.ml_line_count;
6600 else
6601#ifdef FEAT_FOLDING
6602 if (hasAnyFolding(curwin))
6603 {
6604 linenr_T last;
6605
6606 /* count each sequence of folded lines as one logical line */
6607 while (n--)
6608 {
6609 if (hasFolding(lnum, NULL, &last))
6610 lnum = last + 1;
6611 else
6612 ++lnum;
6613 if (lnum >= curbuf->b_ml.ml_line_count)
6614 break;
6615 }
6616 if (lnum > curbuf->b_ml.ml_line_count)
6617 lnum = curbuf->b_ml.ml_line_count;
6618 }
6619 else
6620#endif
6621 lnum += n;
6622 curwin->w_cursor.lnum = lnum;
6623 }
6624
6625 /* try to advance to the column we want to be at */
6626 coladvance(curwin->w_curswant);
6627
6628 if (upd_topline)
6629 update_topline(); /* make sure curwin->w_topline is valid */
6630
6631 return OK;
6632}
6633
6634/*
6635 * Stuff the last inserted text in the read buffer.
6636 * Last_insert actually is a copy of the redo buffer, so we
6637 * first have to remove the command.
6638 */
6639 int
6640stuff_inserted(c, count, no_esc)
6641 int c; /* Command character to be inserted */
6642 long count; /* Repeat this many times */
6643 int no_esc; /* Don't add an ESC at the end */
6644{
6645 char_u *esc_ptr;
6646 char_u *ptr;
6647 char_u *last_ptr;
6648 char_u last = NUL;
6649
6650 ptr = get_last_insert();
6651 if (ptr == NULL)
6652 {
6653 EMSG(_(e_noinstext));
6654 return FAIL;
6655 }
6656
6657 /* may want to stuff the command character, to start Insert mode */
6658 if (c != NUL)
6659 stuffcharReadbuff(c);
6660 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6661 *esc_ptr = NUL; /* remove the ESC */
6662
6663 /* when the last char is either "0" or "^" it will be quoted if no ESC
6664 * comes after it OR if it will inserted more than once and "ptr"
6665 * starts with ^D. -- Acevedo
6666 */
6667 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6668 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6669 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6670 {
6671 last = *last_ptr;
6672 *last_ptr = NUL;
6673 }
6674
6675 do
6676 {
6677 stuffReadbuff(ptr);
6678 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6679 if (last)
6680 stuffReadbuff((char_u *)(last == '0'
6681 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6682 : IF_EB("\026^", CTRL_V_STR "^")));
6683 }
6684 while (--count > 0);
6685
6686 if (last)
6687 *last_ptr = last;
6688
6689 if (esc_ptr != NULL)
6690 *esc_ptr = ESC; /* put the ESC back */
6691
6692 /* may want to stuff a trailing ESC, to get out of Insert mode */
6693 if (!no_esc)
6694 stuffcharReadbuff(ESC);
6695
6696 return OK;
6697}
6698
6699 char_u *
6700get_last_insert()
6701{
6702 if (last_insert == NULL)
6703 return NULL;
6704 return last_insert + last_insert_skip;
6705}
6706
6707/*
6708 * Get last inserted string, and remove trailing <Esc>.
6709 * Returns pointer to allocated memory (must be freed) or NULL.
6710 */
6711 char_u *
6712get_last_insert_save()
6713{
6714 char_u *s;
6715 int len;
6716
6717 if (last_insert == NULL)
6718 return NULL;
6719 s = vim_strsave(last_insert + last_insert_skip);
6720 if (s != NULL)
6721 {
6722 len = (int)STRLEN(s);
6723 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6724 s[len - 1] = NUL;
6725 }
6726 return s;
6727}
6728
6729/*
6730 * Check the word in front of the cursor for an abbreviation.
6731 * Called when the non-id character "c" has been entered.
6732 * When an abbreviation is recognized it is removed from the text and
6733 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6734 */
6735 static int
6736echeck_abbr(c)
6737 int c;
6738{
6739 /* Don't check for abbreviation in paste mode, when disabled and just
6740 * after moving around with cursor keys. */
6741 if (p_paste || no_abbr || arrow_used)
6742 return FALSE;
6743
6744 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6745 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6746}
6747
6748/*
6749 * replace-stack functions
6750 *
6751 * When replacing characters, the replaced characters are remembered for each
6752 * new character. This is used to re-insert the old text when backspacing.
6753 *
6754 * There is a NUL headed list of characters for each character that is
6755 * currently in the file after the insertion point. When BS is used, one NUL
6756 * headed list is put back for the deleted character.
6757 *
6758 * For a newline, there are two NUL headed lists. One contains the characters
6759 * that the NL replaced. The extra one stores the characters after the cursor
6760 * that were deleted (always white space).
6761 *
6762 * Replace_offset is normally 0, in which case replace_push will add a new
6763 * character at the end of the stack. If replace_offset is not 0, that many
6764 * characters will be left on the stack above the newly inserted character.
6765 */
6766
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006767static char_u *replace_stack = NULL;
6768static long replace_stack_nr = 0; /* next entry in replace stack */
6769static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770
6771 void
6772replace_push(c)
6773 int c; /* character that is replaced (NUL is none) */
6774{
6775 char_u *p;
6776
6777 if (replace_stack_nr < replace_offset) /* nothing to do */
6778 return;
6779 if (replace_stack_len <= replace_stack_nr)
6780 {
6781 replace_stack_len += 50;
6782 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6783 if (p == NULL) /* out of memory */
6784 {
6785 replace_stack_len -= 50;
6786 return;
6787 }
6788 if (replace_stack != NULL)
6789 {
6790 mch_memmove(p, replace_stack,
6791 (size_t)(replace_stack_nr * sizeof(char_u)));
6792 vim_free(replace_stack);
6793 }
6794 replace_stack = p;
6795 }
6796 p = replace_stack + replace_stack_nr - replace_offset;
6797 if (replace_offset)
6798 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6799 *p = c;
6800 ++replace_stack_nr;
6801}
6802
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006803#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804/*
6805 * call replace_push(c) with replace_offset set to the first NUL.
6806 */
6807 static void
6808replace_push_off(c)
6809 int c;
6810{
6811 char_u *p;
6812
6813 p = replace_stack + replace_stack_nr;
6814 for (replace_offset = 1; replace_offset < replace_stack_nr;
6815 ++replace_offset)
6816 if (*--p == NUL)
6817 break;
6818 replace_push(c);
6819 replace_offset = 0;
6820}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822
6823/*
6824 * Pop one item from the replace stack.
6825 * return -1 if stack empty
6826 * return replaced character or NUL otherwise
6827 */
6828 static int
6829replace_pop()
6830{
6831 if (replace_stack_nr == 0)
6832 return -1;
6833 return (int)replace_stack[--replace_stack_nr];
6834}
6835
6836/*
6837 * Join the top two items on the replace stack. This removes to "off"'th NUL
6838 * encountered.
6839 */
6840 static void
6841replace_join(off)
6842 int off; /* offset for which NUL to remove */
6843{
6844 int i;
6845
6846 for (i = replace_stack_nr; --i >= 0; )
6847 if (replace_stack[i] == NUL && off-- <= 0)
6848 {
6849 --replace_stack_nr;
6850 mch_memmove(replace_stack + i, replace_stack + i + 1,
6851 (size_t)(replace_stack_nr - i));
6852 return;
6853 }
6854}
6855
6856/*
6857 * Pop bytes from the replace stack until a NUL is found, and insert them
6858 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6859 */
6860 static void
6861replace_pop_ins()
6862{
6863 int cc;
6864 int oldState = State;
6865
6866 State = NORMAL; /* don't want REPLACE here */
6867 while ((cc = replace_pop()) > 0)
6868 {
6869#ifdef FEAT_MBYTE
6870 mb_replace_pop_ins(cc);
6871#else
6872 ins_char(cc);
6873#endif
6874 dec_cursor();
6875 }
6876 State = oldState;
6877}
6878
6879#ifdef FEAT_MBYTE
6880/*
6881 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6882 * indicates a multi-byte char, pop the other bytes too.
6883 */
6884 static void
6885mb_replace_pop_ins(cc)
6886 int cc;
6887{
6888 int n;
6889 char_u buf[MB_MAXBYTES];
6890 int i;
6891 int c;
6892
6893 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6894 {
6895 buf[0] = cc;
6896 for (i = 1; i < n; ++i)
6897 buf[i] = replace_pop();
6898 ins_bytes_len(buf, n);
6899 }
6900 else
6901 ins_char(cc);
6902
6903 if (enc_utf8)
6904 /* Handle composing chars. */
6905 for (;;)
6906 {
6907 c = replace_pop();
6908 if (c == -1) /* stack empty */
6909 break;
6910 if ((n = MB_BYTE2LEN(c)) == 1)
6911 {
6912 /* Not a multi-byte char, put it back. */
6913 replace_push(c);
6914 break;
6915 }
6916 else
6917 {
6918 buf[0] = c;
6919 for (i = 1; i < n; ++i)
6920 buf[i] = replace_pop();
6921 if (utf_iscomposing(utf_ptr2char(buf)))
6922 ins_bytes_len(buf, n);
6923 else
6924 {
6925 /* Not a composing char, put it back. */
6926 for (i = n - 1; i >= 0; --i)
6927 replace_push(buf[i]);
6928 break;
6929 }
6930 }
6931 }
6932}
6933#endif
6934
6935/*
6936 * make the replace stack empty
6937 * (called when exiting replace mode)
6938 */
6939 static void
6940replace_flush()
6941{
6942 vim_free(replace_stack);
6943 replace_stack = NULL;
6944 replace_stack_len = 0;
6945 replace_stack_nr = 0;
6946}
6947
6948/*
6949 * Handle doing a BS for one character.
6950 * cc < 0: replace stack empty, just move cursor
6951 * cc == 0: character was inserted, delete it
6952 * cc > 0: character was replaced, put cc (first byte of original char) back
6953 * and check for more characters to be put back
6954 */
6955 static void
6956replace_do_bs()
6957{
6958 int cc;
6959#ifdef FEAT_VREPLACE
6960 int orig_len = 0;
6961 int ins_len;
6962 int orig_vcols = 0;
6963 colnr_T start_vcol;
6964 char_u *p;
6965 int i;
6966 int vcol;
6967#endif
6968
6969 cc = replace_pop();
6970 if (cc > 0)
6971 {
6972#ifdef FEAT_VREPLACE
6973 if (State & VREPLACE_FLAG)
6974 {
6975 /* Get the number of screen cells used by the character we are
6976 * going to delete. */
6977 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6978 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6979 }
6980#endif
6981#ifdef FEAT_MBYTE
6982 if (has_mbyte)
6983 {
6984 del_char(FALSE);
6985# ifdef FEAT_VREPLACE
6986 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006987 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988# endif
6989 replace_push(cc);
6990 }
6991 else
6992#endif
6993 {
6994 pchar_cursor(cc);
6995#ifdef FEAT_VREPLACE
6996 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006997 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998#endif
6999 }
7000 replace_pop_ins();
7001
7002#ifdef FEAT_VREPLACE
7003 if (State & VREPLACE_FLAG)
7004 {
7005 /* Get the number of screen cells used by the inserted characters */
7006 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007007 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 vcol = start_vcol;
7009 for (i = 0; i < ins_len; ++i)
7010 {
7011 vcol += chartabsize(p + i, vcol);
7012#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007013 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014#endif
7015 }
7016 vcol -= start_vcol;
7017
7018 /* Delete spaces that were inserted after the cursor to keep the
7019 * text aligned. */
7020 curwin->w_cursor.col += ins_len;
7021 while (vcol > orig_vcols && gchar_cursor() == ' ')
7022 {
7023 del_char(FALSE);
7024 ++orig_vcols;
7025 }
7026 curwin->w_cursor.col -= ins_len;
7027 }
7028#endif
7029
7030 /* mark the buffer as changed and prepare for displaying */
7031 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7032 }
7033 else if (cc == 0)
7034 (void)del_char(FALSE);
7035}
7036
7037#ifdef FEAT_CINDENT
7038/*
7039 * Return TRUE if C-indenting is on.
7040 */
7041 static int
7042cindent_on()
7043{
7044 return (!p_paste && (curbuf->b_p_cin
7045# ifdef FEAT_EVAL
7046 || *curbuf->b_p_inde != NUL
7047# endif
7048 ));
7049}
7050#endif
7051
7052#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7053/*
7054 * Re-indent the current line, based on the current contents of it and the
7055 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7056 * confused what all the part that handles Control-T is doing that I'm not.
7057 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7058 */
7059
7060 void
7061fixthisline(get_the_indent)
7062 int (*get_the_indent) __ARGS((void));
7063{
7064 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
7065 if (linewhite(curwin->w_cursor.lnum))
7066 did_ai = TRUE; /* delete the indent if the line stays empty */
7067}
7068
7069 void
7070fix_indent()
7071{
7072 if (p_paste)
7073 return;
7074# ifdef FEAT_LISP
7075 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7076 fixthisline(get_lisp_indent);
7077# endif
7078# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7079 else
7080# endif
7081# ifdef FEAT_CINDENT
7082 if (cindent_on())
7083 do_c_expr_indent();
7084# endif
7085}
7086
7087#endif
7088
7089#ifdef FEAT_CINDENT
7090/*
7091 * return TRUE if 'cinkeys' contains the key "keytyped",
7092 * when == '*': Only if key is preceded with '*' (indent before insert)
7093 * when == '!': Only if key is prededed with '!' (don't insert)
7094 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7095 *
7096 * "keytyped" can have a few special values:
7097 * KEY_OPEN_FORW
7098 * KEY_OPEN_BACK
7099 * KEY_COMPLETE just finished completion.
7100 *
7101 * If line_is_empty is TRUE accept keys with '0' before them.
7102 */
7103 int
7104in_cinkeys(keytyped, when, line_is_empty)
7105 int keytyped;
7106 int when;
7107 int line_is_empty;
7108{
7109 char_u *look;
7110 int try_match;
7111 int try_match_word;
7112 char_u *p;
7113 char_u *line;
7114 int icase;
7115 int i;
7116
7117#ifdef FEAT_EVAL
7118 if (*curbuf->b_p_inde != NUL)
7119 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7120 else
7121#endif
7122 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7123 while (*look)
7124 {
7125 /*
7126 * Find out if we want to try a match with this key, depending on
7127 * 'when' and a '*' or '!' before the key.
7128 */
7129 switch (when)
7130 {
7131 case '*': try_match = (*look == '*'); break;
7132 case '!': try_match = (*look == '!'); break;
7133 default: try_match = (*look != '*'); break;
7134 }
7135 if (*look == '*' || *look == '!')
7136 ++look;
7137
7138 /*
7139 * If there is a '0', only accept a match if the line is empty.
7140 * But may still match when typing last char of a word.
7141 */
7142 if (*look == '0')
7143 {
7144 try_match_word = try_match;
7145 if (!line_is_empty)
7146 try_match = FALSE;
7147 ++look;
7148 }
7149 else
7150 try_match_word = FALSE;
7151
7152 /*
7153 * does it look like a control character?
7154 */
7155 if (*look == '^'
7156#ifdef EBCDIC
7157 && (Ctrl_chr(look[1]) != 0)
7158#else
7159 && look[1] >= '?' && look[1] <= '_'
7160#endif
7161 )
7162 {
7163 if (try_match && keytyped == Ctrl_chr(look[1]))
7164 return TRUE;
7165 look += 2;
7166 }
7167 /*
7168 * 'o' means "o" command, open forward.
7169 * 'O' means "O" command, open backward.
7170 */
7171 else if (*look == 'o')
7172 {
7173 if (try_match && keytyped == KEY_OPEN_FORW)
7174 return TRUE;
7175 ++look;
7176 }
7177 else if (*look == 'O')
7178 {
7179 if (try_match && keytyped == KEY_OPEN_BACK)
7180 return TRUE;
7181 ++look;
7182 }
7183
7184 /*
7185 * 'e' means to check for "else" at start of line and just before the
7186 * cursor.
7187 */
7188 else if (*look == 'e')
7189 {
7190 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7191 {
7192 p = ml_get_curline();
7193 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7194 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7195 return TRUE;
7196 }
7197 ++look;
7198 }
7199
7200 /*
7201 * ':' only causes an indent if it is at the end of a label or case
7202 * statement, or when it was before typing the ':' (to fix
7203 * class::method for C++).
7204 */
7205 else if (*look == ':')
7206 {
7207 if (try_match && keytyped == ':')
7208 {
7209 p = ml_get_curline();
7210 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7211 return TRUE;
7212 if (curwin->w_cursor.col > 2
7213 && p[curwin->w_cursor.col - 1] == ':'
7214 && p[curwin->w_cursor.col - 2] == ':')
7215 {
7216 p[curwin->w_cursor.col - 1] = ' ';
7217 i = (cin_iscase(p) || cin_isscopedecl(p)
7218 || cin_islabel(30));
7219 p = ml_get_curline();
7220 p[curwin->w_cursor.col - 1] = ':';
7221 if (i)
7222 return TRUE;
7223 }
7224 }
7225 ++look;
7226 }
7227
7228
7229 /*
7230 * Is it a key in <>, maybe?
7231 */
7232 else if (*look == '<')
7233 {
7234 if (try_match)
7235 {
7236 /*
7237 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7238 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7239 * >, *, : and ! keys if they really really want to.
7240 */
7241 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7242 && keytyped == look[1])
7243 return TRUE;
7244
7245 if (keytyped == get_special_key_code(look + 1))
7246 return TRUE;
7247 }
7248 while (*look && *look != '>')
7249 look++;
7250 while (*look == '>')
7251 look++;
7252 }
7253
7254 /*
7255 * Is it a word: "=word"?
7256 */
7257 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7258 {
7259 ++look;
7260 if (*look == '~')
7261 {
7262 icase = TRUE;
7263 ++look;
7264 }
7265 else
7266 icase = FALSE;
7267 p = vim_strchr(look, ',');
7268 if (p == NULL)
7269 p = look + STRLEN(look);
7270 if ((try_match || try_match_word)
7271 && curwin->w_cursor.col >= (colnr_T)(p - look))
7272 {
7273 int match = FALSE;
7274
7275#ifdef FEAT_INS_EXPAND
7276 if (keytyped == KEY_COMPLETE)
7277 {
7278 char_u *s;
7279
7280 /* Just completed a word, check if it starts with "look".
7281 * search back for the start of a word. */
7282 line = ml_get_curline();
7283# ifdef FEAT_MBYTE
7284 if (has_mbyte)
7285 {
7286 char_u *n;
7287
7288 for (s = line + curwin->w_cursor.col; s > line; s = n)
7289 {
7290 n = mb_prevptr(line, s);
7291 if (!vim_iswordp(n))
7292 break;
7293 }
7294 }
7295 else
7296# endif
7297 for (s = line + curwin->w_cursor.col; s > line; --s)
7298 if (!vim_iswordc(s[-1]))
7299 break;
7300 if (s + (p - look) <= line + curwin->w_cursor.col
7301 && (icase
7302 ? MB_STRNICMP(s, look, p - look)
7303 : STRNCMP(s, look, p - look)) == 0)
7304 match = TRUE;
7305 }
7306 else
7307#endif
7308 /* TODO: multi-byte */
7309 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7310 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7311 {
7312 line = ml_get_cursor();
7313 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7314 || !vim_iswordc(line[-(p - look) - 1]))
7315 && (icase
7316 ? MB_STRNICMP(line - (p - look), look, p - look)
7317 : STRNCMP(line - (p - look), look, p - look))
7318 == 0)
7319 match = TRUE;
7320 }
7321 if (match && try_match_word && !try_match)
7322 {
7323 /* "0=word": Check if there are only blanks before the
7324 * word. */
7325 line = ml_get_curline();
7326 if ((int)(skipwhite(line) - line) !=
7327 (int)(curwin->w_cursor.col - (p - look)))
7328 match = FALSE;
7329 }
7330 if (match)
7331 return TRUE;
7332 }
7333 look = p;
7334 }
7335
7336 /*
7337 * ok, it's a boring generic character.
7338 */
7339 else
7340 {
7341 if (try_match && *look == keytyped)
7342 return TRUE;
7343 ++look;
7344 }
7345
7346 /*
7347 * Skip over ", ".
7348 */
7349 look = skip_to_option_part(look);
7350 }
7351 return FALSE;
7352}
7353#endif /* FEAT_CINDENT */
7354
7355#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7356/*
7357 * Map Hebrew keyboard when in hkmap mode.
7358 */
7359 int
7360hkmap(c)
7361 int c;
7362{
7363 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7364 {
7365 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7366 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7367 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7368 static char_u map[26] =
7369 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7370 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7371 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7372 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7373 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7374 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7375 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7376 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7377 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7378
7379 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7380 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7381 /* '-1'='sofit' */
7382 else if (c == 'x')
7383 return 'X';
7384 else if (c == 'q')
7385 return '\''; /* {geresh}={'} */
7386 else if (c == 246)
7387 return ' '; /* \"o --> ' ' for a german keyboard */
7388 else if (c == 228)
7389 return ' '; /* \"a --> ' ' -- / -- */
7390 else if (c == 252)
7391 return ' '; /* \"u --> ' ' -- / -- */
7392#ifdef EBCDIC
7393 else if (islower(c))
7394#else
7395 /* NOTE: islower() does not do the right thing for us on Linux so we
7396 * do this the same was as 5.7 and previous, so it works correctly on
7397 * all systems. Specifically, the e.g. Delete and Arrow keys are
7398 * munged and won't work if e.g. searching for Hebrew text.
7399 */
7400 else if (c >= 'a' && c <= 'z')
7401#endif
7402 return (int)(map[CharOrdLow(c)] + p_aleph);
7403 else
7404 return c;
7405 }
7406 else
7407 {
7408 switch (c)
7409 {
7410 case '`': return ';';
7411 case '/': return '.';
7412 case '\'': return ',';
7413 case 'q': return '/';
7414 case 'w': return '\'';
7415
7416 /* Hebrew letters - set offset from 'a' */
7417 case ',': c = '{'; break;
7418 case '.': c = 'v'; break;
7419 case ';': c = 't'; break;
7420 default: {
7421 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7422
7423#ifdef EBCDIC
7424 /* see note about islower() above */
7425 if (!islower(c))
7426#else
7427 if (c < 'a' || c > 'z')
7428#endif
7429 return c;
7430 c = str[CharOrdLow(c)];
7431 break;
7432 }
7433 }
7434
7435 return (int)(CharOrdLow(c) + p_aleph);
7436 }
7437}
7438#endif
7439
7440 static void
7441ins_reg()
7442{
7443 int need_redraw = FALSE;
7444 int regname;
7445 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007446#ifdef FEAT_VISUAL
7447 int vis_active = VIsual_active;
7448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449
7450 /*
7451 * If we are going to wait for a character, show a '"'.
7452 */
7453 pc_status = PC_STATUS_UNSET;
7454 if (redrawing() && !char_avail())
7455 {
7456 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007457 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458
7459 edit_putchar('"', TRUE);
7460#ifdef FEAT_CMDL_INFO
7461 add_to_showcmd_c(Ctrl_R);
7462#endif
7463 }
7464
7465#ifdef USE_ON_FLY_SCROLL
7466 dont_scroll = TRUE; /* disallow scrolling here */
7467#endif
7468
7469 /*
7470 * Don't map the register name. This also prevents the mode message to be
7471 * deleted when ESC is hit.
7472 */
7473 ++no_mapping;
7474 regname = safe_vgetc();
7475#ifdef FEAT_LANGMAP
7476 LANGMAP_ADJUST(regname, TRUE);
7477#endif
7478 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7479 {
7480 /* Get a third key for literal register insertion */
7481 literally = regname;
7482#ifdef FEAT_CMDL_INFO
7483 add_to_showcmd_c(literally);
7484#endif
7485 regname = safe_vgetc();
7486#ifdef FEAT_LANGMAP
7487 LANGMAP_ADJUST(regname, TRUE);
7488#endif
7489 }
7490 --no_mapping;
7491
7492#ifdef FEAT_EVAL
7493 /*
7494 * Don't call u_sync() while getting the expression,
7495 * evaluating it or giving an error message for it!
7496 */
7497 ++no_u_sync;
7498 if (regname == '=')
7499 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007500# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007502# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007504# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 /* Restore the Input Method. */
7506 if (im_on)
7507 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007510 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7511 {
7512 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 else
7516 {
7517#endif
7518 if (literally == Ctrl_O || literally == Ctrl_P)
7519 {
7520 /* Append the command to the redo buffer. */
7521 AppendCharToRedobuff(Ctrl_R);
7522 AppendCharToRedobuff(literally);
7523 AppendCharToRedobuff(regname);
7524
7525 do_put(regname, BACKWARD, 1L,
7526 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7527 }
7528 else if (insert_reg(regname, literally) == FAIL)
7529 {
7530 vim_beep();
7531 need_redraw = TRUE; /* remove the '"' */
7532 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007533 else if (stop_insert_mode)
7534 /* When the '=' register was used and a function was invoked that
7535 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7536 * insert anything, need to remove the '"' */
7537 need_redraw = TRUE;
7538
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539#ifdef FEAT_EVAL
7540 }
7541 --no_u_sync;
7542#endif
7543#ifdef FEAT_CMDL_INFO
7544 clear_showcmd();
7545#endif
7546
7547 /* If the inserted register is empty, we need to remove the '"' */
7548 if (need_redraw || stuff_empty())
7549 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007550
7551#ifdef FEAT_VISUAL
7552 /* Disallow starting Visual mode here, would get a weird mode. */
7553 if (!vis_active && VIsual_active)
7554 end_visual_mode();
7555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556}
7557
7558/*
7559 * CTRL-G commands in Insert mode.
7560 */
7561 static void
7562ins_ctrl_g()
7563{
7564 int c;
7565
7566#ifdef FEAT_INS_EXPAND
7567 /* Right after CTRL-X the cursor will be after the ruler. */
7568 setcursor();
7569#endif
7570
7571 /*
7572 * Don't map the second key. This also prevents the mode message to be
7573 * deleted when ESC is hit.
7574 */
7575 ++no_mapping;
7576 c = safe_vgetc();
7577 --no_mapping;
7578 switch (c)
7579 {
7580 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7581 case K_UP:
7582 case Ctrl_K:
7583 case 'k': ins_up(TRUE);
7584 break;
7585
7586 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7587 case K_DOWN:
7588 case Ctrl_J:
7589 case 'j': ins_down(TRUE);
7590 break;
7591
7592 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007593 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007595
7596 /* Need to reset Insstart, esp. because a BS that joins
7597 * aline to the previous one must save for undo. */
7598 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 break;
7600
7601 /* Unknown CTRL-G command, reserved for future expansion. */
7602 default: vim_beep();
7603 }
7604}
7605
7606/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007607 * CTRL-^ in Insert mode.
7608 */
7609 static void
7610ins_ctrl_hat()
7611{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007612 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007613 {
7614 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7615 if (State & LANGMAP)
7616 {
7617 curbuf->b_p_iminsert = B_IMODE_NONE;
7618 State &= ~LANGMAP;
7619 }
7620 else
7621 {
7622 curbuf->b_p_iminsert = B_IMODE_LMAP;
7623 State |= LANGMAP;
7624#ifdef USE_IM_CONTROL
7625 im_set_active(FALSE);
7626#endif
7627 }
7628 }
7629#ifdef USE_IM_CONTROL
7630 else
7631 {
7632 /* There are no ":lmap" mappings, toggle IM */
7633 if (im_get_status())
7634 {
7635 curbuf->b_p_iminsert = B_IMODE_NONE;
7636 im_set_active(FALSE);
7637 }
7638 else
7639 {
7640 curbuf->b_p_iminsert = B_IMODE_IM;
7641 State &= ~LANGMAP;
7642 im_set_active(TRUE);
7643 }
7644 }
7645#endif
7646 set_iminsert_global();
7647 showmode();
7648#ifdef FEAT_GUI
7649 /* may show different cursor shape or color */
7650 if (gui.in_use)
7651 gui_update_cursor(TRUE, FALSE);
7652#endif
7653#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7654 /* Show/unshow value of 'keymap' in status lines. */
7655 status_redraw_curbuf();
7656#endif
7657}
7658
7659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 * Handle ESC in insert mode.
7661 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7662 * insert.
7663 */
7664 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007665ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 long *count;
7667 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007668 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669{
7670 int temp;
7671 static int disabled_redraw = FALSE;
7672
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007673#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007674 check_spell_redraw();
7675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676#if defined(FEAT_HANGULIN)
7677# if defined(ESC_CHG_TO_ENG_MODE)
7678 hangul_input_state_set(0);
7679# endif
7680 if (composing_hangul)
7681 {
7682 push_raw_key(composing_hangul_buffer, 2);
7683 composing_hangul = 0;
7684 }
7685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686
7687 temp = curwin->w_cursor.col;
7688 if (disabled_redraw)
7689 {
7690 --RedrawingDisabled;
7691 disabled_redraw = FALSE;
7692 }
7693 if (!arrow_used)
7694 {
7695 /*
7696 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007697 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7698 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 */
7700 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007701 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702
7703 /*
7704 * Repeating insert may take a long time. Check for
7705 * interrupt now and then.
7706 */
7707 if (*count > 0)
7708 {
7709 line_breakcheck();
7710 if (got_int)
7711 *count = 0;
7712 }
7713
7714 if (--*count > 0) /* repeat what was typed */
7715 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007716 /* Vi repeats the insert without replacing characters. */
7717 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7718 State &= ~REPLACE_FLAG;
7719
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 (void)start_redo_ins();
7721 if (cmdchar == 'r' || cmdchar == 'v')
7722 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7723 ++RedrawingDisabled;
7724 disabled_redraw = TRUE;
7725 return FALSE; /* repeat the insert */
7726 }
7727 stop_insert(&curwin->w_cursor, TRUE);
7728 undisplay_dollar();
7729 }
7730
7731 /* When an autoindent was removed, curswant stays after the
7732 * indent */
7733 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7734 curwin->w_set_curswant = TRUE;
7735
7736 /* Remember the last Insert position in the '^ mark. */
7737 if (!cmdmod.keepjumps)
7738 curbuf->b_last_insert = curwin->w_cursor;
7739
7740 /*
7741 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007742 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007744 if (!nomove
7745 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746#ifdef FEAT_VIRTUALEDIT
7747 || curwin->w_cursor.coladd > 0
7748#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007749 )
7750 && (restart_edit == NUL
7751 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007753 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007755 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756#ifdef FEAT_RIGHTLEFT
7757 && !revins_on
7758#endif
7759 )
7760 {
7761#ifdef FEAT_VIRTUALEDIT
7762 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7763 {
7764 oneleft();
7765 if (restart_edit != NUL)
7766 ++curwin->w_cursor.coladd;
7767 }
7768 else
7769#endif
7770 {
7771 --curwin->w_cursor.col;
7772#ifdef FEAT_MBYTE
7773 /* Correct cursor for multi-byte character. */
7774 if (has_mbyte)
7775 mb_adjust_cursor();
7776#endif
7777 }
7778 }
7779
7780#ifdef USE_IM_CONTROL
7781 /* Disable IM to allow typing English directly for Normal mode commands.
7782 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7783 * well). */
7784 if (!(State & LANGMAP))
7785 im_save_status(&curbuf->b_p_iminsert);
7786 im_set_active(FALSE);
7787#endif
7788
7789 State = NORMAL;
7790 /* need to position cursor again (e.g. when on a TAB ) */
7791 changed_cline_bef_curs();
7792
7793#ifdef FEAT_MOUSE
7794 setmouse();
7795#endif
7796#ifdef CURSOR_SHAPE
7797 ui_cursor_shape(); /* may show different cursor shape */
7798#endif
7799
7800 /*
7801 * When recording or for CTRL-O, need to display the new mode.
7802 * Otherwise remove the mode message.
7803 */
7804 if (Recording || restart_edit != NUL)
7805 showmode();
7806 else if (p_smd)
7807 MSG("");
7808
7809 return TRUE; /* exit Insert mode */
7810}
7811
7812#ifdef FEAT_RIGHTLEFT
7813/*
7814 * Toggle language: hkmap and revins_on.
7815 * Move to end of reverse inserted text.
7816 */
7817 static void
7818ins_ctrl_()
7819{
7820 if (revins_on && revins_chars && revins_scol >= 0)
7821 {
7822 while (gchar_cursor() != NUL && revins_chars--)
7823 ++curwin->w_cursor.col;
7824 }
7825 p_ri = !p_ri;
7826 revins_on = (State == INSERT && p_ri);
7827 if (revins_on)
7828 {
7829 revins_scol = curwin->w_cursor.col;
7830 revins_legal++;
7831 revins_chars = 0;
7832 undisplay_dollar();
7833 }
7834 else
7835 revins_scol = -1;
7836#ifdef FEAT_FKMAP
7837 if (p_altkeymap)
7838 {
7839 /*
7840 * to be consistent also for redo command, using '.'
7841 * set arrow_used to true and stop it - causing to redo
7842 * characters entered in one mode (normal/reverse insert).
7843 */
7844 arrow_used = TRUE;
7845 (void)stop_arrow();
7846 p_fkmap = curwin->w_p_rl ^ p_ri;
7847 if (p_fkmap && p_ri)
7848 State = INSERT;
7849 }
7850 else
7851#endif
7852 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7853 showmode();
7854}
7855#endif
7856
7857#ifdef FEAT_VISUAL
7858/*
7859 * If 'keymodel' contains "startsel", may start selection.
7860 * Returns TRUE when a CTRL-O and other keys stuffed.
7861 */
7862 static int
7863ins_start_select(c)
7864 int c;
7865{
7866 if (km_startsel)
7867 switch (c)
7868 {
7869 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 case K_PAGEUP:
7872 case K_KPAGEUP:
7873 case K_PAGEDOWN:
7874 case K_KPAGEDOWN:
7875# ifdef MACOS
7876 case K_LEFT:
7877 case K_RIGHT:
7878 case K_UP:
7879 case K_DOWN:
7880 case K_END:
7881 case K_HOME:
7882# endif
7883 if (!(mod_mask & MOD_MASK_SHIFT))
7884 break;
7885 /* FALLTHROUGH */
7886 case K_S_LEFT:
7887 case K_S_RIGHT:
7888 case K_S_UP:
7889 case K_S_DOWN:
7890 case K_S_END:
7891 case K_S_HOME:
7892 /* Start selection right away, the cursor can move with
7893 * CTRL-O when beyond the end of the line. */
7894 start_selection();
7895
7896 /* Execute the key in (insert) Select mode. */
7897 stuffcharReadbuff(Ctrl_O);
7898 if (mod_mask)
7899 {
7900 char_u buf[4];
7901
7902 buf[0] = K_SPECIAL;
7903 buf[1] = KS_MODIFIER;
7904 buf[2] = mod_mask;
7905 buf[3] = NUL;
7906 stuffReadbuff(buf);
7907 }
7908 stuffcharReadbuff(c);
7909 return TRUE;
7910 }
7911 return FALSE;
7912}
7913#endif
7914
7915/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007916 * <Insert> key in Insert mode: toggle insert/remplace mode.
7917 */
7918 static void
7919ins_insert(replaceState)
7920 int replaceState;
7921{
7922#ifdef FEAT_FKMAP
7923 if (p_fkmap && p_ri)
7924 {
7925 beep_flush();
7926 EMSG(farsi_text_3); /* encoded in Farsi */
7927 return;
7928 }
7929#endif
7930
7931#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007932# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007933 set_vim_var_string(VV_INSERTMODE,
7934 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007935# ifdef FEAT_VREPLACE
7936 replaceState == VREPLACE ? "v" :
7937# endif
7938 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007939# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007940 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7941#endif
7942 if (State & REPLACE_FLAG)
7943 State = INSERT | (State & LANGMAP);
7944 else
7945 State = replaceState | (State & LANGMAP);
7946 AppendCharToRedobuff(K_INS);
7947 showmode();
7948#ifdef CURSOR_SHAPE
7949 ui_cursor_shape(); /* may show different cursor shape */
7950#endif
7951}
7952
7953/*
7954 * Pressed CTRL-O in Insert mode.
7955 */
7956 static void
7957ins_ctrl_o()
7958{
7959#ifdef FEAT_VREPLACE
7960 if (State & VREPLACE_FLAG)
7961 restart_edit = 'V';
7962 else
7963#endif
7964 if (State & REPLACE_FLAG)
7965 restart_edit = 'R';
7966 else
7967 restart_edit = 'I';
7968#ifdef FEAT_VIRTUALEDIT
7969 if (virtual_active())
7970 ins_at_eol = FALSE; /* cursor always keeps its column */
7971 else
7972#endif
7973 ins_at_eol = (gchar_cursor() == NUL);
7974}
7975
7976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 * If the cursor is on an indent, ^T/^D insert/delete one
7978 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7979 * Always round the indent to 'shiftwith', this is compatible
7980 * with vi. But vi only supports ^T and ^D after an
7981 * autoindent, we support it everywhere.
7982 */
7983 static void
7984ins_shift(c, lastc)
7985 int c;
7986 int lastc;
7987{
7988 if (stop_arrow() == FAIL)
7989 return;
7990 AppendCharToRedobuff(c);
7991
7992 /*
7993 * 0^D and ^^D: remove all indent.
7994 */
7995 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7996 {
7997 --curwin->w_cursor.col;
7998 (void)del_char(FALSE); /* delete the '^' or '0' */
7999 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8000 if (State & REPLACE_FLAG)
8001 replace_pop_ins();
8002 if (lastc == '^')
8003 old_indent = get_indent(); /* remember curr. indent */
8004 change_indent(INDENT_SET, 0, TRUE, 0);
8005 }
8006 else
8007 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
8008
8009 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8010 did_ai = FALSE;
8011#ifdef FEAT_SMARTINDENT
8012 did_si = FALSE;
8013 can_si = FALSE;
8014 can_si_back = FALSE;
8015#endif
8016#ifdef FEAT_CINDENT
8017 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8018#endif
8019}
8020
8021 static void
8022ins_del()
8023{
8024 int temp;
8025
8026 if (stop_arrow() == FAIL)
8027 return;
8028 if (gchar_cursor() == NUL) /* delete newline */
8029 {
8030 temp = curwin->w_cursor.col;
8031 if (!can_bs(BS_EOL) /* only if "eol" included */
8032 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8033 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8034 || do_join(FALSE) == FAIL)
8035 vim_beep();
8036 else
8037 curwin->w_cursor.col = temp;
8038 }
8039 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8040 vim_beep();
8041 did_ai = FALSE;
8042#ifdef FEAT_SMARTINDENT
8043 did_si = FALSE;
8044 can_si = FALSE;
8045 can_si_back = FALSE;
8046#endif
8047 AppendCharToRedobuff(K_DEL);
8048}
8049
8050/*
8051 * Handle Backspace, delete-word and delete-line in Insert mode.
8052 * Return TRUE when backspace was actually used.
8053 */
8054 static int
8055ins_bs(c, mode, inserted_space_p)
8056 int c;
8057 int mode;
8058 int *inserted_space_p;
8059{
8060 linenr_T lnum;
8061 int cc;
8062 int temp = 0; /* init for GCC */
8063 colnr_T mincol;
8064 int did_backspace = FALSE;
8065 int in_indent;
8066 int oldState;
8067#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008068 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069#endif
8070
8071 /*
8072 * can't delete anything in an empty file
8073 * can't backup past first character in buffer
8074 * can't backup past starting point unless 'backspace' > 1
8075 * can backup to a previous line if 'backspace' == 0
8076 */
8077 if ( bufempty()
8078 || (
8079#ifdef FEAT_RIGHTLEFT
8080 !revins_on &&
8081#endif
8082 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8083 || (!can_bs(BS_START)
8084 && (arrow_used
8085 || (curwin->w_cursor.lnum == Insstart.lnum
8086 && curwin->w_cursor.col <= Insstart.col)))
8087 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8088 && curwin->w_cursor.col <= ai_col)
8089 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8090 {
8091 vim_beep();
8092 return FALSE;
8093 }
8094
8095 if (stop_arrow() == FAIL)
8096 return FALSE;
8097 in_indent = inindent(0);
8098#ifdef FEAT_CINDENT
8099 if (in_indent)
8100 can_cindent = FALSE;
8101#endif
8102#ifdef FEAT_COMMENTS
8103 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8104#endif
8105#ifdef FEAT_RIGHTLEFT
8106 if (revins_on) /* put cursor after last inserted char */
8107 inc_cursor();
8108#endif
8109
8110#ifdef FEAT_VIRTUALEDIT
8111 /* Virtualedit:
8112 * BACKSPACE_CHAR eats a virtual space
8113 * BACKSPACE_WORD eats all coladd
8114 * BACKSPACE_LINE eats all coladd and keeps going
8115 */
8116 if (curwin->w_cursor.coladd > 0)
8117 {
8118 if (mode == BACKSPACE_CHAR)
8119 {
8120 --curwin->w_cursor.coladd;
8121 return TRUE;
8122 }
8123 if (mode == BACKSPACE_WORD)
8124 {
8125 curwin->w_cursor.coladd = 0;
8126 return TRUE;
8127 }
8128 curwin->w_cursor.coladd = 0;
8129 }
8130#endif
8131
8132 /*
8133 * delete newline!
8134 */
8135 if (curwin->w_cursor.col == 0)
8136 {
8137 lnum = Insstart.lnum;
8138 if (curwin->w_cursor.lnum == Insstart.lnum
8139#ifdef FEAT_RIGHTLEFT
8140 || revins_on
8141#endif
8142 )
8143 {
8144 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8145 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8146 return FALSE;
8147 --Insstart.lnum;
8148 Insstart.col = MAXCOL;
8149 }
8150 /*
8151 * In replace mode:
8152 * cc < 0: NL was inserted, delete it
8153 * cc >= 0: NL was replaced, put original characters back
8154 */
8155 cc = -1;
8156 if (State & REPLACE_FLAG)
8157 cc = replace_pop(); /* returns -1 if NL was inserted */
8158 /*
8159 * In replace mode, in the line we started replacing, we only move the
8160 * cursor.
8161 */
8162 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8163 {
8164 dec_cursor();
8165 }
8166 else
8167 {
8168#ifdef FEAT_VREPLACE
8169 if (!(State & VREPLACE_FLAG)
8170 || curwin->w_cursor.lnum > orig_line_count)
8171#endif
8172 {
8173 temp = gchar_cursor(); /* remember current char */
8174 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008175
8176 /* When "aw" is in 'formatoptions' we must delete the space at
8177 * the end of the line, otherwise the line will be broken
8178 * again when auto-formatting. */
8179 if (has_format_option(FO_AUTO)
8180 && has_format_option(FO_WHITE_PAR))
8181 {
8182 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8183 TRUE);
8184 int len;
8185
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008186 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008187 if (len > 0 && ptr[len - 1] == ' ')
8188 ptr[len - 1] = NUL;
8189 }
8190
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 (void)do_join(FALSE);
8192 if (temp == NUL && gchar_cursor() != NUL)
8193 inc_cursor();
8194 }
8195#ifdef FEAT_VREPLACE
8196 else
8197 dec_cursor();
8198#endif
8199
8200 /*
8201 * In REPLACE mode we have to put back the text that was replaced
8202 * by the NL. On the replace stack is first a NUL-terminated
8203 * sequence of characters that were deleted and then the
8204 * characters that NL replaced.
8205 */
8206 if (State & REPLACE_FLAG)
8207 {
8208 /*
8209 * Do the next ins_char() in NORMAL state, to
8210 * prevent ins_char() from replacing characters and
8211 * avoiding showmatch().
8212 */
8213 oldState = State;
8214 State = NORMAL;
8215 /*
8216 * restore characters (blanks) deleted after cursor
8217 */
8218 while (cc > 0)
8219 {
8220 temp = curwin->w_cursor.col;
8221#ifdef FEAT_MBYTE
8222 mb_replace_pop_ins(cc);
8223#else
8224 ins_char(cc);
8225#endif
8226 curwin->w_cursor.col = temp;
8227 cc = replace_pop();
8228 }
8229 /* restore the characters that NL replaced */
8230 replace_pop_ins();
8231 State = oldState;
8232 }
8233 }
8234 did_ai = FALSE;
8235 }
8236 else
8237 {
8238 /*
8239 * Delete character(s) before the cursor.
8240 */
8241#ifdef FEAT_RIGHTLEFT
8242 if (revins_on) /* put cursor on last inserted char */
8243 dec_cursor();
8244#endif
8245 mincol = 0;
8246 /* keep indent */
8247 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
8248#ifdef FEAT_RIGHTLEFT
8249 && !revins_on
8250#endif
8251 )
8252 {
8253 temp = curwin->w_cursor.col;
8254 beginline(BL_WHITE);
8255 if (curwin->w_cursor.col < (colnr_T)temp)
8256 mincol = curwin->w_cursor.col;
8257 curwin->w_cursor.col = temp;
8258 }
8259
8260 /*
8261 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8262 */
8263 if ( mode == BACKSPACE_CHAR
8264 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008265 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 && (*(ml_get_cursor() - 1) == TAB
8267 || (*(ml_get_cursor() - 1) == ' '
8268 && (!*inserted_space_p
8269 || arrow_used))))))
8270 {
8271 int ts;
8272 colnr_T vcol;
8273 colnr_T want_vcol;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008274#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 int extra = 0;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277
8278 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008279 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 ts = curbuf->b_p_sw;
8281 else
8282 ts = curbuf->b_p_sts;
8283 /* Compute the virtual column where we want to be. Since
8284 * 'showbreak' may get in the way, need to get the last column of
8285 * the previous character. */
8286 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8287 dec_cursor();
8288 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8289 inc_cursor();
8290 want_vcol = (want_vcol / ts) * ts;
8291
8292 /* delete characters until we are at or before want_vcol */
8293 while (vcol > want_vcol
8294 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
8295 {
8296 dec_cursor();
8297 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8298 if (State & REPLACE_FLAG)
8299 {
8300 /* Don't delete characters before the insert point when in
8301 * Replace mode */
8302 if (curwin->w_cursor.lnum != Insstart.lnum
8303 || curwin->w_cursor.col >= Insstart.col)
8304 {
8305#if 0 /* what was this for? It causes problems when sw != ts. */
8306 if (State == REPLACE && (int)vcol < want_vcol)
8307 {
8308 (void)del_char(FALSE);
8309 extra = 2; /* don't pop too much */
8310 }
8311 else
8312#endif
8313 replace_do_bs();
8314 }
8315 }
8316 else
8317 (void)del_char(FALSE);
8318 }
8319
8320 /* insert extra spaces until we are at want_vcol */
8321 while (vcol < want_vcol)
8322 {
8323 /* Remember the first char we inserted */
8324 if (curwin->w_cursor.lnum == Insstart.lnum
8325 && curwin->w_cursor.col < Insstart.col)
8326 Insstart.col = curwin->w_cursor.col;
8327
8328#ifdef FEAT_VREPLACE
8329 if (State & VREPLACE_FLAG)
8330 ins_char(' ');
8331 else
8332#endif
8333 {
8334 ins_str((char_u *)" ");
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008335 if ((State & REPLACE_FLAG) /* && extra <= 1 */)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008337#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 if (extra)
8339 replace_push_off(NUL);
8340 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 replace_push(NUL);
8343 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008344#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 if (extra == 2)
8346 extra = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 }
8349 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8350 }
8351 }
8352
8353 /*
8354 * Delete upto starting point, start of line or previous word.
8355 */
8356 else do
8357 {
8358#ifdef FEAT_RIGHTLEFT
8359 if (!revins_on) /* put cursor on char to be deleted */
8360#endif
8361 dec_cursor();
8362
8363 /* start of word? */
8364 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8365 {
8366 mode = BACKSPACE_WORD_NOT_SPACE;
8367 temp = vim_iswordc(gchar_cursor());
8368 }
8369 /* end of word? */
8370 else if (mode == BACKSPACE_WORD_NOT_SPACE
8371 && (vim_isspace(cc = gchar_cursor())
8372 || vim_iswordc(cc) != temp))
8373 {
8374#ifdef FEAT_RIGHTLEFT
8375 if (!revins_on)
8376#endif
8377 inc_cursor();
8378#ifdef FEAT_RIGHTLEFT
8379 else if (State & REPLACE_FLAG)
8380 dec_cursor();
8381#endif
8382 break;
8383 }
8384 if (State & REPLACE_FLAG)
8385 replace_do_bs();
8386 else
8387 {
8388#ifdef FEAT_MBYTE
8389 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008390 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391#endif
8392 (void)del_char(FALSE);
8393#ifdef FEAT_MBYTE
8394 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008395 * If there are combining characters and 'delcombine' is set
8396 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 * character.
8398 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008399 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 inc_cursor();
8401#endif
8402#ifdef FEAT_RIGHTLEFT
8403 if (revins_chars)
8404 {
8405 revins_chars--;
8406 revins_legal++;
8407 }
8408 if (revins_on && gchar_cursor() == NUL)
8409 break;
8410#endif
8411 }
8412 /* Just a single backspace?: */
8413 if (mode == BACKSPACE_CHAR)
8414 break;
8415 } while (
8416#ifdef FEAT_RIGHTLEFT
8417 revins_on ||
8418#endif
8419 (curwin->w_cursor.col > mincol
8420 && (curwin->w_cursor.lnum != Insstart.lnum
8421 || curwin->w_cursor.col != Insstart.col)));
8422 did_backspace = TRUE;
8423 }
8424#ifdef FEAT_SMARTINDENT
8425 did_si = FALSE;
8426 can_si = FALSE;
8427 can_si_back = FALSE;
8428#endif
8429 if (curwin->w_cursor.col <= 1)
8430 did_ai = FALSE;
8431 /*
8432 * It's a little strange to put backspaces into the redo
8433 * buffer, but it makes auto-indent a lot easier to deal
8434 * with.
8435 */
8436 AppendCharToRedobuff(c);
8437
8438 /* If deleted before the insertion point, adjust it */
8439 if (curwin->w_cursor.lnum == Insstart.lnum
8440 && curwin->w_cursor.col < Insstart.col)
8441 Insstart.col = curwin->w_cursor.col;
8442
8443 /* vi behaviour: the cursor moves backward but the character that
8444 * was there remains visible
8445 * Vim behaviour: the cursor moves backward and the character that
8446 * was there is erased from the screen.
8447 * We can emulate the vi behaviour by pretending there is a dollar
8448 * displayed even when there isn't.
8449 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8450 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8451 dollar_vcol = curwin->w_virtcol;
8452
8453 return did_backspace;
8454}
8455
8456#ifdef FEAT_MOUSE
8457 static void
8458ins_mouse(c)
8459 int c;
8460{
8461 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008462 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463
8464# ifdef FEAT_GUI
8465 /* When GUI is active, also move/paste when 'mouse' is empty */
8466 if (!gui.in_use)
8467# endif
8468 if (!mouse_has(MOUSE_INSERT))
8469 return;
8470
8471 undisplay_dollar();
8472 tpos = curwin->w_cursor;
8473 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8474 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008475#ifdef FEAT_WINDOWS
8476 win_T *new_curwin = curwin;
8477
8478 if (curwin != old_curwin && win_valid(old_curwin))
8479 {
8480 /* Mouse took us to another window. We need to go back to the
8481 * previous one to stop insert there properly. */
8482 curwin = old_curwin;
8483 curbuf = curwin->w_buffer;
8484 }
8485#endif
8486 start_arrow(curwin == old_curwin ? &tpos : NULL);
8487#ifdef FEAT_WINDOWS
8488 if (curwin != new_curwin && win_valid(new_curwin))
8489 {
8490 curwin = new_curwin;
8491 curbuf = curwin->w_buffer;
8492 }
8493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494# ifdef FEAT_CINDENT
8495 can_cindent = TRUE;
8496# endif
8497 }
8498
8499#ifdef FEAT_WINDOWS
8500 /* redraw status lines (in case another window became active) */
8501 redraw_statuslines();
8502#endif
8503}
8504
8505 static void
8506ins_mousescroll(up)
8507 int up;
8508{
8509 pos_T tpos;
8510# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8511 win_T *old_curwin;
8512# endif
8513
8514 tpos = curwin->w_cursor;
8515
8516# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8517 old_curwin = curwin;
8518
8519 /* Currently the mouse coordinates are only known in the GUI. */
8520 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8521 {
8522 int row, col;
8523
8524 row = mouse_row;
8525 col = mouse_col;
8526
8527 /* find the window at the pointer coordinates */
8528 curwin = mouse_find_win(&row, &col);
8529 curbuf = curwin->w_buffer;
8530 }
8531 if (curwin == old_curwin)
8532# endif
8533 undisplay_dollar();
8534
8535 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8536 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8537 else
8538 scroll_redraw(up, 3L);
8539
8540# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8541 curwin->w_redr_status = TRUE;
8542
8543 curwin = old_curwin;
8544 curbuf = curwin->w_buffer;
8545# endif
8546
8547 if (!equalpos(curwin->w_cursor, tpos))
8548 {
8549 start_arrow(&tpos);
8550# ifdef FEAT_CINDENT
8551 can_cindent = TRUE;
8552# endif
8553 }
8554}
8555#endif
8556
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008557#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008558 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008559ins_tabline(c)
8560 int c;
8561{
8562 /* We will be leaving the current window, unless closing another tab. */
8563 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8564 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8565 {
8566 undisplay_dollar();
8567 start_arrow(&curwin->w_cursor);
8568# ifdef FEAT_CINDENT
8569 can_cindent = TRUE;
8570# endif
8571 }
8572
8573 if (c == K_TABLINE)
8574 goto_tabpage(current_tab);
8575 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008576 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008577 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008578 redraw_statuslines(); /* will redraw the tabline when needed */
8579 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008580}
8581#endif
8582
8583#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 void
8585ins_scroll()
8586{
8587 pos_T tpos;
8588
8589 undisplay_dollar();
8590 tpos = curwin->w_cursor;
8591 if (gui_do_scroll())
8592 {
8593 start_arrow(&tpos);
8594# ifdef FEAT_CINDENT
8595 can_cindent = TRUE;
8596# endif
8597 }
8598}
8599
8600 void
8601ins_horscroll()
8602{
8603 pos_T tpos;
8604
8605 undisplay_dollar();
8606 tpos = curwin->w_cursor;
8607 if (gui_do_horiz_scroll())
8608 {
8609 start_arrow(&tpos);
8610# ifdef FEAT_CINDENT
8611 can_cindent = TRUE;
8612# endif
8613 }
8614}
8615#endif
8616
8617 static void
8618ins_left()
8619{
8620 pos_T tpos;
8621
8622#ifdef FEAT_FOLDING
8623 if ((fdo_flags & FDO_HOR) && KeyTyped)
8624 foldOpenCursor();
8625#endif
8626 undisplay_dollar();
8627 tpos = curwin->w_cursor;
8628 if (oneleft() == OK)
8629 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00008630#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8631 /* Only call start_arrow() when not busy with preediting, it will
8632 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8633 if (!im_is_preediting())
8634#endif
8635 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636#ifdef FEAT_RIGHTLEFT
8637 /* If exit reversed string, position is fixed */
8638 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8639 revins_legal++;
8640 revins_chars++;
8641#endif
8642 }
8643
8644 /*
8645 * if 'whichwrap' set for cursor in insert mode may go to
8646 * previous line
8647 */
8648 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8649 {
8650 start_arrow(&tpos);
8651 --(curwin->w_cursor.lnum);
8652 coladvance((colnr_T)MAXCOL);
8653 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8654 }
8655 else
8656 vim_beep();
8657}
8658
8659 static void
8660ins_home(c)
8661 int c;
8662{
8663 pos_T tpos;
8664
8665#ifdef FEAT_FOLDING
8666 if ((fdo_flags & FDO_HOR) && KeyTyped)
8667 foldOpenCursor();
8668#endif
8669 undisplay_dollar();
8670 tpos = curwin->w_cursor;
8671 if (c == K_C_HOME)
8672 curwin->w_cursor.lnum = 1;
8673 curwin->w_cursor.col = 0;
8674#ifdef FEAT_VIRTUALEDIT
8675 curwin->w_cursor.coladd = 0;
8676#endif
8677 curwin->w_curswant = 0;
8678 start_arrow(&tpos);
8679}
8680
8681 static void
8682ins_end(c)
8683 int c;
8684{
8685 pos_T tpos;
8686
8687#ifdef FEAT_FOLDING
8688 if ((fdo_flags & FDO_HOR) && KeyTyped)
8689 foldOpenCursor();
8690#endif
8691 undisplay_dollar();
8692 tpos = curwin->w_cursor;
8693 if (c == K_C_END)
8694 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8695 coladvance((colnr_T)MAXCOL);
8696 curwin->w_curswant = MAXCOL;
8697
8698 start_arrow(&tpos);
8699}
8700
8701 static void
8702ins_s_left()
8703{
8704#ifdef FEAT_FOLDING
8705 if ((fdo_flags & FDO_HOR) && KeyTyped)
8706 foldOpenCursor();
8707#endif
8708 undisplay_dollar();
8709 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8710 {
8711 start_arrow(&curwin->w_cursor);
8712 (void)bck_word(1L, FALSE, FALSE);
8713 curwin->w_set_curswant = TRUE;
8714 }
8715 else
8716 vim_beep();
8717}
8718
8719 static void
8720ins_right()
8721{
8722#ifdef FEAT_FOLDING
8723 if ((fdo_flags & FDO_HOR) && KeyTyped)
8724 foldOpenCursor();
8725#endif
8726 undisplay_dollar();
8727 if (gchar_cursor() != NUL || virtual_active()
8728 )
8729 {
8730 start_arrow(&curwin->w_cursor);
8731 curwin->w_set_curswant = TRUE;
8732#ifdef FEAT_VIRTUALEDIT
8733 if (virtual_active())
8734 oneright();
8735 else
8736#endif
8737 {
8738#ifdef FEAT_MBYTE
8739 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008740 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 else
8742#endif
8743 ++curwin->w_cursor.col;
8744 }
8745
8746#ifdef FEAT_RIGHTLEFT
8747 revins_legal++;
8748 if (revins_chars)
8749 revins_chars--;
8750#endif
8751 }
8752 /* if 'whichwrap' set for cursor in insert mode, may move the
8753 * cursor to the next line */
8754 else if (vim_strchr(p_ww, ']') != NULL
8755 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8756 {
8757 start_arrow(&curwin->w_cursor);
8758 curwin->w_set_curswant = TRUE;
8759 ++curwin->w_cursor.lnum;
8760 curwin->w_cursor.col = 0;
8761 }
8762 else
8763 vim_beep();
8764}
8765
8766 static void
8767ins_s_right()
8768{
8769#ifdef FEAT_FOLDING
8770 if ((fdo_flags & FDO_HOR) && KeyTyped)
8771 foldOpenCursor();
8772#endif
8773 undisplay_dollar();
8774 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8775 || gchar_cursor() != NUL)
8776 {
8777 start_arrow(&curwin->w_cursor);
8778 (void)fwd_word(1L, FALSE, 0);
8779 curwin->w_set_curswant = TRUE;
8780 }
8781 else
8782 vim_beep();
8783}
8784
8785 static void
8786ins_up(startcol)
8787 int startcol; /* when TRUE move to Insstart.col */
8788{
8789 pos_T tpos;
8790 linenr_T old_topline = curwin->w_topline;
8791#ifdef FEAT_DIFF
8792 int old_topfill = curwin->w_topfill;
8793#endif
8794
8795 undisplay_dollar();
8796 tpos = curwin->w_cursor;
8797 if (cursor_up(1L, TRUE) == OK)
8798 {
8799 if (startcol)
8800 coladvance(getvcol_nolist(&Insstart));
8801 if (old_topline != curwin->w_topline
8802#ifdef FEAT_DIFF
8803 || old_topfill != curwin->w_topfill
8804#endif
8805 )
8806 redraw_later(VALID);
8807 start_arrow(&tpos);
8808#ifdef FEAT_CINDENT
8809 can_cindent = TRUE;
8810#endif
8811 }
8812 else
8813 vim_beep();
8814}
8815
8816 static void
8817ins_pageup()
8818{
8819 pos_T tpos;
8820
8821 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008822
8823#ifdef FEAT_WINDOWS
8824 if (mod_mask & MOD_MASK_CTRL)
8825 {
8826 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00008827 if (first_tabpage->tp_next != NULL)
8828 {
8829 start_arrow(&curwin->w_cursor);
8830 goto_tabpage(-1);
8831 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008832 return;
8833 }
8834#endif
8835
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 tpos = curwin->w_cursor;
8837 if (onepage(BACKWARD, 1L) == OK)
8838 {
8839 start_arrow(&tpos);
8840#ifdef FEAT_CINDENT
8841 can_cindent = TRUE;
8842#endif
8843 }
8844 else
8845 vim_beep();
8846}
8847
8848 static void
8849ins_down(startcol)
8850 int startcol; /* when TRUE move to Insstart.col */
8851{
8852 pos_T tpos;
8853 linenr_T old_topline = curwin->w_topline;
8854#ifdef FEAT_DIFF
8855 int old_topfill = curwin->w_topfill;
8856#endif
8857
8858 undisplay_dollar();
8859 tpos = curwin->w_cursor;
8860 if (cursor_down(1L, TRUE) == OK)
8861 {
8862 if (startcol)
8863 coladvance(getvcol_nolist(&Insstart));
8864 if (old_topline != curwin->w_topline
8865#ifdef FEAT_DIFF
8866 || old_topfill != curwin->w_topfill
8867#endif
8868 )
8869 redraw_later(VALID);
8870 start_arrow(&tpos);
8871#ifdef FEAT_CINDENT
8872 can_cindent = TRUE;
8873#endif
8874 }
8875 else
8876 vim_beep();
8877}
8878
8879 static void
8880ins_pagedown()
8881{
8882 pos_T tpos;
8883
8884 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008885
8886#ifdef FEAT_WINDOWS
8887 if (mod_mask & MOD_MASK_CTRL)
8888 {
8889 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00008890 if (first_tabpage->tp_next != NULL)
8891 {
8892 start_arrow(&curwin->w_cursor);
8893 goto_tabpage(0);
8894 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008895 return;
8896 }
8897#endif
8898
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 tpos = curwin->w_cursor;
8900 if (onepage(FORWARD, 1L) == OK)
8901 {
8902 start_arrow(&tpos);
8903#ifdef FEAT_CINDENT
8904 can_cindent = TRUE;
8905#endif
8906 }
8907 else
8908 vim_beep();
8909}
8910
8911#ifdef FEAT_DND
8912 static void
8913ins_drop()
8914{
8915 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8916}
8917#endif
8918
8919/*
8920 * Handle TAB in Insert or Replace mode.
8921 * Return TRUE when the TAB needs to be inserted like a normal character.
8922 */
8923 static int
8924ins_tab()
8925{
8926 int ind;
8927 int i;
8928 int temp;
8929
8930 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8931 Insstart_blank_vcol = get_nolist_virtcol();
8932 if (echeck_abbr(TAB + ABBR_OFF))
8933 return FALSE;
8934
8935 ind = inindent(0);
8936#ifdef FEAT_CINDENT
8937 if (ind)
8938 can_cindent = FALSE;
8939#endif
8940
8941 /*
8942 * When nothing special, insert TAB like a normal character
8943 */
8944 if (!curbuf->b_p_et
8945 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8946 && curbuf->b_p_sts == 0)
8947 return TRUE;
8948
8949 if (stop_arrow() == FAIL)
8950 return TRUE;
8951
8952 did_ai = FALSE;
8953#ifdef FEAT_SMARTINDENT
8954 did_si = FALSE;
8955 can_si = FALSE;
8956 can_si_back = FALSE;
8957#endif
8958 AppendToRedobuff((char_u *)"\t");
8959
8960 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8961 temp = (int)curbuf->b_p_sw;
8962 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8963 temp = (int)curbuf->b_p_sts;
8964 else /* otherwise use 'tabstop' */
8965 temp = (int)curbuf->b_p_ts;
8966 temp -= get_nolist_virtcol() % temp;
8967
8968 /*
8969 * Insert the first space with ins_char(). It will delete one char in
8970 * replace mode. Insert the rest with ins_str(); it will not delete any
8971 * chars. For VREPLACE mode, we use ins_char() for all characters.
8972 */
8973 ins_char(' ');
8974 while (--temp > 0)
8975 {
8976#ifdef FEAT_VREPLACE
8977 if (State & VREPLACE_FLAG)
8978 ins_char(' ');
8979 else
8980#endif
8981 {
8982 ins_str((char_u *)" ");
8983 if (State & REPLACE_FLAG) /* no char replaced */
8984 replace_push(NUL);
8985 }
8986 }
8987
8988 /*
8989 * When 'expandtab' not set: Replace spaces by TABs where possible.
8990 */
8991 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8992 {
8993 char_u *ptr;
8994#ifdef FEAT_VREPLACE
8995 char_u *saved_line = NULL; /* init for GCC */
8996 pos_T pos;
8997#endif
8998 pos_T fpos;
8999 pos_T *cursor;
9000 colnr_T want_vcol, vcol;
9001 int change_col = -1;
9002 int save_list = curwin->w_p_list;
9003
9004 /*
9005 * Get the current line. For VREPLACE mode, don't make real changes
9006 * yet, just work on a copy of the line.
9007 */
9008#ifdef FEAT_VREPLACE
9009 if (State & VREPLACE_FLAG)
9010 {
9011 pos = curwin->w_cursor;
9012 cursor = &pos;
9013 saved_line = vim_strsave(ml_get_curline());
9014 if (saved_line == NULL)
9015 return FALSE;
9016 ptr = saved_line + pos.col;
9017 }
9018 else
9019#endif
9020 {
9021 ptr = ml_get_cursor();
9022 cursor = &curwin->w_cursor;
9023 }
9024
9025 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9026 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9027 curwin->w_p_list = FALSE;
9028
9029 /* Find first white before the cursor */
9030 fpos = curwin->w_cursor;
9031 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9032 {
9033 --fpos.col;
9034 --ptr;
9035 }
9036
9037 /* In Replace mode, don't change characters before the insert point. */
9038 if ((State & REPLACE_FLAG)
9039 && fpos.lnum == Insstart.lnum
9040 && fpos.col < Insstart.col)
9041 {
9042 ptr += Insstart.col - fpos.col;
9043 fpos.col = Insstart.col;
9044 }
9045
9046 /* compute virtual column numbers of first white and cursor */
9047 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9048 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9049
9050 /* Use as many TABs as possible. Beware of 'showbreak' and
9051 * 'linebreak' adding extra virtual columns. */
9052 while (vim_iswhite(*ptr))
9053 {
9054 i = lbr_chartabsize((char_u *)"\t", vcol);
9055 if (vcol + i > want_vcol)
9056 break;
9057 if (*ptr != TAB)
9058 {
9059 *ptr = TAB;
9060 if (change_col < 0)
9061 {
9062 change_col = fpos.col; /* Column of first change */
9063 /* May have to adjust Insstart */
9064 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9065 Insstart.col = fpos.col;
9066 }
9067 }
9068 ++fpos.col;
9069 ++ptr;
9070 vcol += i;
9071 }
9072
9073 if (change_col >= 0)
9074 {
9075 int repl_off = 0;
9076
9077 /* Skip over the spaces we need. */
9078 while (vcol < want_vcol && *ptr == ' ')
9079 {
9080 vcol += lbr_chartabsize(ptr, vcol);
9081 ++ptr;
9082 ++repl_off;
9083 }
9084 if (vcol > want_vcol)
9085 {
9086 /* Must have a char with 'showbreak' just before it. */
9087 --ptr;
9088 --repl_off;
9089 }
9090 fpos.col += repl_off;
9091
9092 /* Delete following spaces. */
9093 i = cursor->col - fpos.col;
9094 if (i > 0)
9095 {
9096 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
9097 /* correct replace stack. */
9098 if ((State & REPLACE_FLAG)
9099#ifdef FEAT_VREPLACE
9100 && !(State & VREPLACE_FLAG)
9101#endif
9102 )
9103 for (temp = i; --temp >= 0; )
9104 replace_join(repl_off);
9105 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009106#ifdef FEAT_NETBEANS_INTG
9107 if (usingNetbeans)
9108 {
9109 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9110 (long)(i + 1));
9111 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9112 (char_u *)"\t", 1);
9113 }
9114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 cursor->col -= i;
9116
9117#ifdef FEAT_VREPLACE
9118 /*
9119 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9120 * backspacing over the changed spacing and then inserting the new
9121 * spacing.
9122 */
9123 if (State & VREPLACE_FLAG)
9124 {
9125 /* Backspace from real cursor to change_col */
9126 backspace_until_column(change_col);
9127
9128 /* Insert each char in saved_line from changed_col to
9129 * ptr-cursor */
9130 ins_bytes_len(saved_line + change_col,
9131 cursor->col - change_col);
9132 }
9133#endif
9134 }
9135
9136#ifdef FEAT_VREPLACE
9137 if (State & VREPLACE_FLAG)
9138 vim_free(saved_line);
9139#endif
9140 curwin->w_p_list = save_list;
9141 }
9142
9143 return FALSE;
9144}
9145
9146/*
9147 * Handle CR or NL in insert mode.
9148 * Return TRUE when out of memory or can't undo.
9149 */
9150 static int
9151ins_eol(c)
9152 int c;
9153{
9154 int i;
9155
9156 if (echeck_abbr(c + ABBR_OFF))
9157 return FALSE;
9158 if (stop_arrow() == FAIL)
9159 return TRUE;
9160 undisplay_dollar();
9161
9162 /*
9163 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9164 * character under the cursor. Only push a NUL on the replace stack,
9165 * nothing to put back when the NL is deleted.
9166 */
9167 if ((State & REPLACE_FLAG)
9168#ifdef FEAT_VREPLACE
9169 && !(State & VREPLACE_FLAG)
9170#endif
9171 )
9172 replace_push(NUL);
9173
9174 /*
9175 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9176 * replacing the next line, so we push all of the characters left on the
9177 * line onto the replace stack. This is not done here though, it is done
9178 * in open_line().
9179 */
9180
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009181#ifdef FEAT_VIRTUALEDIT
9182 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9183 * CTRL-O). */
9184 if (virtual_active() && curwin->w_cursor.coladd > 0)
9185 coladvance(getviscol());
9186#endif
9187
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188#ifdef FEAT_RIGHTLEFT
9189# ifdef FEAT_FKMAP
9190 if (p_altkeymap && p_fkmap)
9191 fkmap(NL);
9192# endif
9193 /* NL in reverse insert will always start in the end of
9194 * current line. */
9195 if (revins_on)
9196 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9197#endif
9198
9199 AppendToRedobuff(NL_STR);
9200 i = open_line(FORWARD,
9201#ifdef FEAT_COMMENTS
9202 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9203#endif
9204 0, old_indent);
9205 old_indent = 0;
9206#ifdef FEAT_CINDENT
9207 can_cindent = TRUE;
9208#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009209#ifdef FEAT_FOLDING
9210 /* When inserting a line the cursor line must never be in a closed fold. */
9211 foldOpenCursor();
9212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213
9214 return (!i);
9215}
9216
9217#ifdef FEAT_DIGRAPHS
9218/*
9219 * Handle digraph in insert mode.
9220 * Returns character still to be inserted, or NUL when nothing remaining to be
9221 * done.
9222 */
9223 static int
9224ins_digraph()
9225{
9226 int c;
9227 int cc;
9228
9229 pc_status = PC_STATUS_UNSET;
9230 if (redrawing() && !char_avail())
9231 {
9232 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009233 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234
9235 edit_putchar('?', TRUE);
9236#ifdef FEAT_CMDL_INFO
9237 add_to_showcmd_c(Ctrl_K);
9238#endif
9239 }
9240
9241#ifdef USE_ON_FLY_SCROLL
9242 dont_scroll = TRUE; /* disallow scrolling here */
9243#endif
9244
9245 /* don't map the digraph chars. This also prevents the
9246 * mode message to be deleted when ESC is hit */
9247 ++no_mapping;
9248 ++allow_keys;
9249 c = safe_vgetc();
9250 --no_mapping;
9251 --allow_keys;
9252 if (IS_SPECIAL(c) || mod_mask) /* special key */
9253 {
9254#ifdef FEAT_CMDL_INFO
9255 clear_showcmd();
9256#endif
9257 insert_special(c, TRUE, FALSE);
9258 return NUL;
9259 }
9260 if (c != ESC)
9261 {
9262 if (redrawing() && !char_avail())
9263 {
9264 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009265 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266
9267 if (char2cells(c) == 1)
9268 {
9269 /* first remove the '?', otherwise it's restored when typing
9270 * an ESC next */
9271 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009272 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 edit_putchar(c, TRUE);
9274 }
9275#ifdef FEAT_CMDL_INFO
9276 add_to_showcmd_c(c);
9277#endif
9278 }
9279 ++no_mapping;
9280 ++allow_keys;
9281 cc = safe_vgetc();
9282 --no_mapping;
9283 --allow_keys;
9284 if (cc != ESC)
9285 {
9286 AppendToRedobuff((char_u *)CTRL_V_STR);
9287 c = getdigraph(c, cc, TRUE);
9288#ifdef FEAT_CMDL_INFO
9289 clear_showcmd();
9290#endif
9291 return c;
9292 }
9293 }
9294 edit_unputchar();
9295#ifdef FEAT_CMDL_INFO
9296 clear_showcmd();
9297#endif
9298 return NUL;
9299}
9300#endif
9301
9302/*
9303 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9304 * Returns the char to be inserted, or NUL if none found.
9305 */
9306 static int
9307ins_copychar(lnum)
9308 linenr_T lnum;
9309{
9310 int c;
9311 int temp;
9312 char_u *ptr, *prev_ptr;
9313
9314 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9315 {
9316 vim_beep();
9317 return NUL;
9318 }
9319
9320 /* try to advance to the cursor column */
9321 temp = 0;
9322 ptr = ml_get(lnum);
9323 prev_ptr = ptr;
9324 validate_virtcol();
9325 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9326 {
9327 prev_ptr = ptr;
9328 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9329 }
9330 if ((colnr_T)temp > curwin->w_virtcol)
9331 ptr = prev_ptr;
9332
9333#ifdef FEAT_MBYTE
9334 c = (*mb_ptr2char)(ptr);
9335#else
9336 c = *ptr;
9337#endif
9338 if (c == NUL)
9339 vim_beep();
9340 return c;
9341}
9342
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009343/*
9344 * CTRL-Y or CTRL-E typed in Insert mode.
9345 */
9346 static int
9347ins_ctrl_ey(tc)
9348 int tc;
9349{
9350 int c = tc;
9351
9352#ifdef FEAT_INS_EXPAND
9353 if (ctrl_x_mode == CTRL_X_SCROLL)
9354 {
9355 if (c == Ctrl_Y)
9356 scrolldown_clamp();
9357 else
9358 scrollup_clamp();
9359 redraw_later(VALID);
9360 }
9361 else
9362#endif
9363 {
9364 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9365 if (c != NUL)
9366 {
9367 long tw_save;
9368
9369 /* The character must be taken literally, insert like it
9370 * was typed after a CTRL-V, and pretend 'textwidth'
9371 * wasn't set. Digits, 'o' and 'x' are special after a
9372 * CTRL-V, don't use it for these. */
9373 if (c < 256 && !isalnum(c))
9374 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9375 tw_save = curbuf->b_p_tw;
9376 curbuf->b_p_tw = -1;
9377 insert_special(c, TRUE, FALSE);
9378 curbuf->b_p_tw = tw_save;
9379#ifdef FEAT_RIGHTLEFT
9380 revins_chars++;
9381 revins_legal++;
9382#endif
9383 c = Ctrl_V; /* pretend CTRL-V is last character */
9384 auto_format(FALSE, TRUE);
9385 }
9386 }
9387 return c;
9388}
9389
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390#ifdef FEAT_SMARTINDENT
9391/*
9392 * Try to do some very smart auto-indenting.
9393 * Used when inserting a "normal" character.
9394 */
9395 static void
9396ins_try_si(c)
9397 int c;
9398{
9399 pos_T *pos, old_pos;
9400 char_u *ptr;
9401 int i;
9402 int temp;
9403
9404 /*
9405 * do some very smart indenting when entering '{' or '}'
9406 */
9407 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9408 {
9409 /*
9410 * for '}' set indent equal to indent of line containing matching '{'
9411 */
9412 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9413 {
9414 old_pos = curwin->w_cursor;
9415 /*
9416 * If the matching '{' has a ')' immediately before it (ignoring
9417 * white-space), then line up with the start of the line
9418 * containing the matching '(' if there is one. This handles the
9419 * case where an "if (..\n..) {" statement continues over multiple
9420 * lines -- webb
9421 */
9422 ptr = ml_get(pos->lnum);
9423 i = pos->col;
9424 if (i > 0) /* skip blanks before '{' */
9425 while (--i > 0 && vim_iswhite(ptr[i]))
9426 ;
9427 curwin->w_cursor.lnum = pos->lnum;
9428 curwin->w_cursor.col = i;
9429 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9430 curwin->w_cursor = *pos;
9431 i = get_indent();
9432 curwin->w_cursor = old_pos;
9433#ifdef FEAT_VREPLACE
9434 if (State & VREPLACE_FLAG)
9435 change_indent(INDENT_SET, i, FALSE, NUL);
9436 else
9437#endif
9438 (void)set_indent(i, SIN_CHANGED);
9439 }
9440 else if (curwin->w_cursor.col > 0)
9441 {
9442 /*
9443 * when inserting '{' after "O" reduce indent, but not
9444 * more than indent of previous line
9445 */
9446 temp = TRUE;
9447 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9448 {
9449 old_pos = curwin->w_cursor;
9450 i = get_indent();
9451 while (curwin->w_cursor.lnum > 1)
9452 {
9453 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9454
9455 /* ignore empty lines and lines starting with '#'. */
9456 if (*ptr != '#' && *ptr != NUL)
9457 break;
9458 }
9459 if (get_indent() >= i)
9460 temp = FALSE;
9461 curwin->w_cursor = old_pos;
9462 }
9463 if (temp)
9464 shift_line(TRUE, FALSE, 1);
9465 }
9466 }
9467
9468 /*
9469 * set indent of '#' always to 0
9470 */
9471 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9472 {
9473 /* remember current indent for next line */
9474 old_indent = get_indent();
9475 (void)set_indent(0, SIN_CHANGED);
9476 }
9477
9478 /* Adjust ai_col, the char at this position can be deleted. */
9479 if (ai_col > curwin->w_cursor.col)
9480 ai_col = curwin->w_cursor.col;
9481}
9482#endif
9483
9484/*
9485 * Get the value that w_virtcol would have when 'list' is off.
9486 * Unless 'cpo' contains the 'L' flag.
9487 */
9488 static colnr_T
9489get_nolist_virtcol()
9490{
9491 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9492 return getvcol_nolist(&curwin->w_cursor);
9493 validate_virtcol();
9494 return curwin->w_virtcol;
9495}