blob: a8044151b0dda10ea04c726c0ce105d8d4eae763 [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 Moolenaar4a85b412006-04-23 22:40:29 +0000132static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int dup));
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
710#ifdef FEAT_RIGHTLEFT
711 if (p_hkmap && KeyTyped)
712 c = hkmap(c); /* Hebrew mode mapping */
713#endif
714#ifdef FEAT_FKMAP
715 if (p_fkmap && KeyTyped)
716 c = fkmap(c); /* Farsi mode mapping */
717#endif
718
719#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000720 /*
721 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000722 * and the cursor is still in the completed word. Only when there is
723 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000724 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000725 if (compl_started
726 && pum_wanted()
727 && curwin->w_cursor.col >= compl_col
728 && (compl_shown_match == NULL
729 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000730 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000731 /* BS: Delete one character from "compl_leader". */
732 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000733 && curwin->w_cursor.col > compl_col
734 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000735 continue;
736
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000737 /* When no match was selected or it was edited. */
738 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000739 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000740 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000741 * "compl_leader". Except when at the original match and
742 * there is nothing to add, CTRL-L works like CTRL-P then. */
743 if (c == Ctrl_L
744 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
745 || STRLEN(compl_shown_match->cp_str)
746 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000747 {
748 ins_compl_addfrommatch();
749 continue;
750 }
751
Bram Moolenaardf1bdc92006-02-23 21:32:16 +0000752 /* A printable, non-white character: Add to "compl_leader". */
753 if (vim_isprintc(c) && !vim_iswhite(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000754 {
755 ins_compl_addleader(c);
756 continue;
757 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000758
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000759 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000760 * compl_enter_selects is set the Enter key does the same. */
761 if (c == Ctrl_Y || (compl_enter_selects
762 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000763 {
764 ins_compl_delete();
765 ins_compl_insert();
766 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000767 }
768 }
769
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
771 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000772 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000773 if (c != K_IGNORE && ins_compl_prep(c))
774 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#endif
776
Bram Moolenaar488c6512005-08-11 20:09:58 +0000777 /* CTRL-\ CTRL-N goes to Normal mode,
778 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
779 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (c == Ctrl_BSL)
781 {
782 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000783 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 ++no_mapping;
785 ++allow_keys;
786 c = safe_vgetc();
787 --no_mapping;
788 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000789 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000791 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 vungetc(c);
793 c = Ctrl_BSL;
794 }
795 else if (c == Ctrl_G && p_im)
796 continue;
797 else
798 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000799 if (c == Ctrl_O)
800 {
801 ins_ctrl_o();
802 ins_at_eol = FALSE; /* cursor keeps its column */
803 nomove = TRUE;
804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 count = 0;
806 goto doESCkey;
807 }
808 }
809
810#ifdef FEAT_DIGRAPHS
811 c = do_digraph(c);
812#endif
813
814#ifdef FEAT_INS_EXPAND
815 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
816 goto docomplete;
817#endif
818 if (c == Ctrl_V || c == Ctrl_Q)
819 {
820 ins_ctrl_v();
821 c = Ctrl_V; /* pretend CTRL-V is last typed character */
822 continue;
823 }
824
825#ifdef FEAT_CINDENT
826 if (cindent_on()
827# ifdef FEAT_INS_EXPAND
828 && ctrl_x_mode == 0
829# endif
830 )
831 {
832 /* A key name preceded by a bang means this key is not to be
833 * inserted. Skip ahead to the re-indenting below.
834 * A key name preceded by a star means that indenting has to be
835 * done before inserting the key. */
836 line_is_white = inindent(0);
837 if (in_cinkeys(c, '!', line_is_white))
838 goto force_cindent;
839 if (can_cindent && in_cinkeys(c, '*', line_is_white)
840 && stop_arrow() == OK)
841 do_c_expr_indent();
842 }
843#endif
844
845#ifdef FEAT_RIGHTLEFT
846 if (curwin->w_p_rl)
847 switch (c)
848 {
849 case K_LEFT: c = K_RIGHT; break;
850 case K_S_LEFT: c = K_S_RIGHT; break;
851 case K_C_LEFT: c = K_C_RIGHT; break;
852 case K_RIGHT: c = K_LEFT; break;
853 case K_S_RIGHT: c = K_S_LEFT; break;
854 case K_C_RIGHT: c = K_C_LEFT; break;
855 }
856#endif
857
858#ifdef FEAT_VISUAL
859 /*
860 * If 'keymodel' contains "startsel", may start selection. If it
861 * does, a CTRL-O and c will be stuffed, we need to get these
862 * characters.
863 */
864 if (ins_start_select(c))
865 continue;
866#endif
867
868 /*
869 * The big switch to handle a character in insert mode.
870 */
871 switch (c)
872 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000873 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (echeck_abbr(ESC + ABBR_OFF))
875 break;
876 /*FALLTHROUGH*/
877
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000878 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#ifdef FEAT_CMDWIN
880 if (c == Ctrl_C && cmdwin_type != 0)
881 {
882 /* Close the cmdline window. */
883 cmdwin_result = K_IGNORE;
884 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000885 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 goto doESCkey;
887 }
888#endif
889
890#ifdef UNIX
891do_intr:
892#endif
893 /* when 'insertmode' set, and not halfway a mapping, don't leave
894 * Insert mode */
895 if (goto_im())
896 {
897 if (got_int)
898 {
899 (void)vgetc(); /* flush all buffers */
900 got_int = FALSE;
901 }
902 else
903 vim_beep();
904 break;
905 }
906doESCkey:
907 /*
908 * This is the ONLY return from edit()!
909 */
910 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
911 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000912 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 o_lnum = curwin->w_cursor.lnum;
914
Bram Moolenaar488c6512005-08-11 20:09:58 +0000915 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000916 {
917#ifdef FEAT_AUTOCMD
918 if (cmdchar != 'r' && cmdchar != 'v')
919 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
920 FALSE, curbuf);
921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 continue;
925
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000926 case Ctrl_Z: /* suspend when 'insertmode' set */
927 if (!p_im)
928 goto normalchar; /* insert CTRL-Z as normal char */
929 stuffReadbuff((char_u *)":st\r");
930 c = Ctrl_O;
931 /*FALLTHROUGH*/
932
933 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000934#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000935 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000936 goto docomplete;
937#endif
938 if (echeck_abbr(Ctrl_O + ABBR_OFF))
939 break;
940 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000941
942#ifdef FEAT_VIRTUALEDIT
943 /* don't move the cursor left when 'virtualedit' has "onemore". */
944 if (ve_flags & VE_ONEMORE)
945 {
946 ins_at_eol = FALSE;
947 nomove = TRUE;
948 }
949#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000950 count = 0;
951 goto doESCkey;
952
Bram Moolenaar572cb562005-08-05 21:35:02 +0000953 case K_INS: /* toggle insert/replace mode */
954 case K_KINS:
955 ins_insert(replaceState);
956 break;
957
958 case K_SELECT: /* end of Select mode mapping - ignore */
959 break;
960
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000961#ifdef FEAT_SNIFF
962 case K_SNIFF: /* Sniff command received */
963 stuffcharReadbuff(K_SNIFF);
964 goto doESCkey;
965#endif
966
967 case K_HELP: /* Help key works like <ESC> <Help> */
968 case K_F1:
969 case K_XF1:
970 stuffcharReadbuff(K_HELP);
971 if (p_im)
972 need_start_insertmode = TRUE;
973 goto doESCkey;
974
975#ifdef FEAT_NETBEANS_INTG
976 case K_F21: /* NetBeans command */
977 ++no_mapping; /* don't map the next key hits */
978 i = safe_vgetc();
979 --no_mapping;
980 netbeans_keycommand(i);
981 break;
982#endif
983
984 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 case NUL:
986 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000987 /* For ^@ the trailing ESC will end the insert, unless there is an
988 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
990 && c != Ctrl_A && !p_im)
991 goto doESCkey; /* quit insert mode */
992 inserted_space = FALSE;
993 break;
994
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000995 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 ins_reg();
997 auto_format(FALSE, TRUE);
998 inserted_space = FALSE;
999 break;
1000
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001001 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 ins_ctrl_g();
1003 break;
1004
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001005 case Ctrl_HAT: /* switch input mode and/or langmap */
1006 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 break;
1008
1009#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001010 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 if (!p_ari)
1012 goto normalchar;
1013 ins_ctrl_();
1014 break;
1015#endif
1016
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001017 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1019 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1020 goto docomplete;
1021#endif
1022 /* FALLTHROUGH */
1023
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001024 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025# ifdef FEAT_INS_EXPAND
1026 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1027 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001028 if (has_compl_option(FALSE))
1029 goto docomplete;
1030 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
1032# endif
1033 ins_shift(c, lastc);
1034 auto_format(FALSE, TRUE);
1035 inserted_space = FALSE;
1036 break;
1037
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001038 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 case K_KDEL:
1040 ins_del();
1041 auto_format(FALSE, TRUE);
1042 break;
1043
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001044 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 case Ctrl_H:
1046 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1047 auto_format(FALSE, TRUE);
1048 break;
1049
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001050 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1052 auto_format(FALSE, TRUE);
1053 break;
1054
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001056# ifdef FEAT_COMPL_FUNC
1057 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001058 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001059 goto docomplete;
1060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1062 auto_format(FALSE, TRUE);
1063 inserted_space = FALSE;
1064 break;
1065
1066#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001067 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 case K_LEFTMOUSE_NM:
1069 case K_LEFTDRAG:
1070 case K_LEFTRELEASE:
1071 case K_LEFTRELEASE_NM:
1072 case K_MIDDLEMOUSE:
1073 case K_MIDDLEDRAG:
1074 case K_MIDDLERELEASE:
1075 case K_RIGHTMOUSE:
1076 case K_RIGHTDRAG:
1077 case K_RIGHTRELEASE:
1078 case K_X1MOUSE:
1079 case K_X1DRAG:
1080 case K_X1RELEASE:
1081 case K_X2MOUSE:
1082 case K_X2DRAG:
1083 case K_X2RELEASE:
1084 ins_mouse(c);
1085 break;
1086
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001087 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 ins_mousescroll(FALSE);
1089 break;
1090
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001091 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 ins_mousescroll(TRUE);
1093 break;
1094#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001095#ifdef FEAT_GUI_TABLINE
1096 case K_TABLINE:
1097 case K_TABMENU:
1098 ins_tabline(c);
1099 break;
1100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 break;
1104
Bram Moolenaar754b5602006-02-09 23:53:20 +00001105#ifdef FEAT_AUTOCMD
1106 case K_CURSORHOLD: /* Didn't type something for a while. */
1107 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1108 did_cursorhold = TRUE;
1109 break;
1110#endif
1111
Bram Moolenaar4770d092006-01-12 23:22:24 +00001112#ifdef FEAT_GUI_W32
1113 /* On Win32 ignore <M-F4>, we get it when closing the window was
1114 * cancelled. */
1115 case K_F4:
1116 if (mod_mask != MOD_MASK_ALT)
1117 goto normalchar;
1118 break;
1119#endif
1120
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121#ifdef FEAT_GUI
1122 case K_VER_SCROLLBAR:
1123 ins_scroll();
1124 break;
1125
1126 case K_HOR_SCROLLBAR:
1127 ins_horscroll();
1128 break;
1129#endif
1130
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001131 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 case K_S_HOME:
1134 case K_C_HOME:
1135 ins_home(c);
1136 break;
1137
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001138 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 case K_S_END:
1141 case K_C_END:
1142 ins_end(c);
1143 break;
1144
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001145 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001146 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1147 ins_s_left();
1148 else
1149 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 break;
1151
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001152 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 case K_C_LEFT:
1154 ins_s_left();
1155 break;
1156
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001157 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001158 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1159 ins_s_right();
1160 else
1161 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 break;
1163
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001164 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 case K_C_RIGHT:
1166 ins_s_right();
1167 break;
1168
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001170#ifdef FEAT_INS_EXPAND
1171 if (pum_visible())
1172 goto docomplete;
1173#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001174 if (mod_mask & MOD_MASK_SHIFT)
1175 ins_pageup();
1176 else
1177 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 break;
1179
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001180 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 case K_PAGEUP:
1182 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001183#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001184 if (pum_visible())
1185 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 ins_pageup();
1188 break;
1189
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001190 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001191#ifdef FEAT_INS_EXPAND
1192 if (pum_visible())
1193 goto docomplete;
1194#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001195 if (mod_mask & MOD_MASK_SHIFT)
1196 ins_pagedown();
1197 else
1198 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 break;
1200
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001201 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 case K_PAGEDOWN:
1203 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001204#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001205 if (pum_visible())
1206 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 ins_pagedown();
1209 break;
1210
1211#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001212 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 ins_drop();
1214 break;
1215#endif
1216
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001217 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 c = TAB;
1219 /* FALLTHROUGH */
1220
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001221 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1223 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1224 goto docomplete;
1225#endif
1226 inserted_space = FALSE;
1227 if (ins_tab())
1228 goto normalchar; /* insert TAB as a normal char */
1229 auto_format(FALSE, TRUE);
1230 break;
1231
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001232 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 c = CAR;
1234 /* FALLTHROUGH */
1235 case CAR:
1236 case NL:
1237#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1238 /* In a quickfix window a <CR> jumps to the error under the
1239 * cursor. */
1240 if (bt_quickfix(curbuf) && c == CAR)
1241 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001242 if (curwin->w_llist_ref == NULL) /* quickfix window */
1243 do_cmdline_cmd((char_u *)".cc");
1244 else /* location list window */
1245 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 break;
1247 }
1248#endif
1249#ifdef FEAT_CMDWIN
1250 if (cmdwin_type != 0)
1251 {
1252 /* Execute the command in the cmdline window. */
1253 cmdwin_result = CAR;
1254 goto doESCkey;
1255 }
1256#endif
1257 if (ins_eol(c) && !p_im)
1258 goto doESCkey; /* out of memory */
1259 auto_format(FALSE, FALSE);
1260 inserted_space = FALSE;
1261 break;
1262
1263#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001264 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265# ifdef FEAT_INS_EXPAND
1266 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1267 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001268 if (has_compl_option(TRUE))
1269 goto docomplete;
1270 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 }
1272# endif
1273# ifdef FEAT_DIGRAPHS
1274 c = ins_digraph();
1275 if (c == NUL)
1276 break;
1277# endif
1278 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
1281#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001282 case Ctrl_X: /* Enter CTRL-X mode */
1283 ins_ctrl_x();
1284 break;
1285
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001286 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 if (ctrl_x_mode != CTRL_X_TAGS)
1288 goto normalchar;
1289 goto docomplete;
1290
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001291 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 if (ctrl_x_mode != CTRL_X_FILES)
1293 goto normalchar;
1294 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001295
1296 case 's': /* Spelling completion after ^X */
1297 case Ctrl_S:
1298 if (ctrl_x_mode != CTRL_X_SPELL)
1299 goto normalchar;
1300 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301#endif
1302
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001303 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304#ifdef FEAT_INS_EXPAND
1305 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1306#endif
1307 {
1308 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1309 if (p_im)
1310 {
1311 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1312 break;
1313 goto doESCkey;
1314 }
1315 goto normalchar;
1316 }
1317#ifdef FEAT_INS_EXPAND
1318 /* FALLTHROUGH */
1319
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001320 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 case Ctrl_N:
1322 /* if 'complete' is empty then plain ^P is no longer special,
1323 * but it is under other ^X modes */
1324 if (*curbuf->b_p_cpt == NUL
1325 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001326 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 goto normalchar;
1328
1329docomplete:
1330 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001331 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 break;
1333#endif /* FEAT_INS_EXPAND */
1334
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001335 case Ctrl_Y: /* copy from previous line or scroll down */
1336 case Ctrl_E: /* copy from next line or scroll up */
1337 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 break;
1339
1340 default:
1341#ifdef UNIX
1342 if (c == intr_char) /* special interrupt char */
1343 goto do_intr;
1344#endif
1345
1346 /*
1347 * Insert a nomal character.
1348 */
1349normalchar:
1350#ifdef FEAT_SMARTINDENT
1351 /* Try to perform smart-indenting. */
1352 ins_try_si(c);
1353#endif
1354
1355 if (c == ' ')
1356 {
1357 inserted_space = TRUE;
1358#ifdef FEAT_CINDENT
1359 if (inindent(0))
1360 can_cindent = FALSE;
1361#endif
1362 if (Insstart_blank_vcol == MAXCOL
1363 && curwin->w_cursor.lnum == Insstart.lnum)
1364 Insstart_blank_vcol = get_nolist_virtcol();
1365 }
1366
1367 if (vim_iswordc(c) || !echeck_abbr(
1368#ifdef FEAT_MBYTE
1369 /* Add ABBR_OFF for characters above 0x100, this is
1370 * what check_abbr() expects. */
1371 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1372#endif
1373 c))
1374 {
1375 insert_special(c, FALSE, FALSE);
1376#ifdef FEAT_RIGHTLEFT
1377 revins_legal++;
1378 revins_chars++;
1379#endif
1380 }
1381
1382 auto_format(FALSE, TRUE);
1383
1384#ifdef FEAT_FOLDING
1385 /* When inserting a character the cursor line must never be in a
1386 * closed fold. */
1387 foldOpenCursor();
1388#endif
1389 break;
1390 } /* end of switch (c) */
1391
1392 /* If the cursor was moved we didn't just insert a space */
1393 if (arrow_used)
1394 inserted_space = FALSE;
1395
1396#ifdef FEAT_CINDENT
1397 if (can_cindent && cindent_on()
1398# ifdef FEAT_INS_EXPAND
1399 && ctrl_x_mode == 0
1400# endif
1401 )
1402 {
1403force_cindent:
1404 /*
1405 * Indent now if a key was typed that is in 'cinkeys'.
1406 */
1407 if (in_cinkeys(c, ' ', line_is_white))
1408 {
1409 if (stop_arrow() == OK)
1410 /* re-indent the current line */
1411 do_c_expr_indent();
1412 }
1413 }
1414#endif /* FEAT_CINDENT */
1415
1416 } /* for (;;) */
1417 /* NOTREACHED */
1418}
1419
1420/*
1421 * Redraw for Insert mode.
1422 * This is postponed until getting the next character to make '$' in the 'cpo'
1423 * option work correctly.
1424 * Only redraw when there are no characters available. This speeds up
1425 * inserting sequences of characters (e.g., for CTRL-R).
1426 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001427/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001429ins_redraw(ready)
1430 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
1432 if (!char_avail())
1433 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001434#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001435 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1436 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001437 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001438 && !equalpos(last_cursormoved, curwin->w_cursor)
1439# ifdef FEAT_INS_EXPAND
1440 && !pum_visible()
1441# endif
1442 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001443 {
1444 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1445 last_cursormoved = curwin->w_cursor;
1446 }
1447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 if (must_redraw)
1449 update_screen(0);
1450 else if (clear_cmdline || redraw_cmdline)
1451 showmode(); /* clear cmdline and show mode */
1452 showruler(FALSE);
1453 setcursor();
1454 emsg_on_display = FALSE; /* may remove error message now */
1455 }
1456}
1457
1458/*
1459 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1460 */
1461 static void
1462ins_ctrl_v()
1463{
1464 int c;
1465
1466 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001467 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
1469 if (redrawing() && !char_avail())
1470 edit_putchar('^', TRUE);
1471 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1472
1473#ifdef FEAT_CMDL_INFO
1474 add_to_showcmd_c(Ctrl_V);
1475#endif
1476
1477 c = get_literal();
1478#ifdef FEAT_CMDL_INFO
1479 clear_showcmd();
1480#endif
1481 insert_special(c, FALSE, TRUE);
1482#ifdef FEAT_RIGHTLEFT
1483 revins_chars++;
1484 revins_legal++;
1485#endif
1486}
1487
1488/*
1489 * Put a character directly onto the screen. It's not stored in a buffer.
1490 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1491 */
1492static int pc_status;
1493#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1494#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1495#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1496#define PC_STATUS_SET 3 /* pc_bytes was filled */
1497#ifdef FEAT_MBYTE
1498static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1499#else
1500static char_u pc_bytes[2]; /* saved bytes */
1501#endif
1502static int pc_attr;
1503static int pc_row;
1504static int pc_col;
1505
1506 void
1507edit_putchar(c, highlight)
1508 int c;
1509 int highlight;
1510{
1511 int attr;
1512
1513 if (ScreenLines != NULL)
1514 {
1515 update_topline(); /* just in case w_topline isn't valid */
1516 validate_cursor();
1517 if (highlight)
1518 attr = hl_attr(HLF_8);
1519 else
1520 attr = 0;
1521 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1522 pc_col = W_WINCOL(curwin);
1523#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1524 pc_status = PC_STATUS_UNSET;
1525#endif
1526#ifdef FEAT_RIGHTLEFT
1527 if (curwin->w_p_rl)
1528 {
1529 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1530# ifdef FEAT_MBYTE
1531 if (has_mbyte)
1532 {
1533 int fix_col = mb_fix_col(pc_col, pc_row);
1534
1535 if (fix_col != pc_col)
1536 {
1537 screen_putchar(' ', pc_row, fix_col, attr);
1538 --curwin->w_wcol;
1539 pc_status = PC_STATUS_RIGHT;
1540 }
1541 }
1542# endif
1543 }
1544 else
1545#endif
1546 {
1547 pc_col += curwin->w_wcol;
1548#ifdef FEAT_MBYTE
1549 if (mb_lefthalve(pc_row, pc_col))
1550 pc_status = PC_STATUS_LEFT;
1551#endif
1552 }
1553
1554 /* save the character to be able to put it back */
1555#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1556 if (pc_status == PC_STATUS_UNSET)
1557#endif
1558 {
1559 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1560 pc_status = PC_STATUS_SET;
1561 }
1562 screen_putchar(c, pc_row, pc_col, attr);
1563 }
1564}
1565
1566/*
1567 * Undo the previous edit_putchar().
1568 */
1569 void
1570edit_unputchar()
1571{
1572 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1573 {
1574#if defined(FEAT_MBYTE)
1575 if (pc_status == PC_STATUS_RIGHT)
1576 ++curwin->w_wcol;
1577 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1578 redrawWinline(curwin->w_cursor.lnum, FALSE);
1579 else
1580#endif
1581 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1582 }
1583}
1584
1585/*
1586 * Called when p_dollar is set: display a '$' at the end of the changed text
1587 * Only works when cursor is in the line that changes.
1588 */
1589 void
1590display_dollar(col)
1591 colnr_T col;
1592{
1593 colnr_T save_col;
1594
1595 if (!redrawing())
1596 return;
1597
1598 cursor_off();
1599 save_col = curwin->w_cursor.col;
1600 curwin->w_cursor.col = col;
1601#ifdef FEAT_MBYTE
1602 if (has_mbyte)
1603 {
1604 char_u *p;
1605
1606 /* If on the last byte of a multi-byte move to the first byte. */
1607 p = ml_get_curline();
1608 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1609 }
1610#endif
1611 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1612 if (curwin->w_wcol < W_WIDTH(curwin))
1613 {
1614 edit_putchar('$', FALSE);
1615 dollar_vcol = curwin->w_virtcol;
1616 }
1617 curwin->w_cursor.col = save_col;
1618}
1619
1620/*
1621 * Call this function before moving the cursor from the normal insert position
1622 * in insert mode.
1623 */
1624 static void
1625undisplay_dollar()
1626{
1627 if (dollar_vcol)
1628 {
1629 dollar_vcol = 0;
1630 redrawWinline(curwin->w_cursor.lnum, FALSE);
1631 }
1632}
1633
1634/*
1635 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1636 * Keep the cursor on the same character.
1637 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1638 * type == INDENT_DEC decrease indent (for CTRL-D)
1639 * type == INDENT_SET set indent to "amount"
1640 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1641 */
1642 void
1643change_indent(type, amount, round, replaced)
1644 int type;
1645 int amount;
1646 int round;
1647 int replaced; /* replaced character, put on replace stack */
1648{
1649 int vcol;
1650 int last_vcol;
1651 int insstart_less; /* reduction for Insstart.col */
1652 int new_cursor_col;
1653 int i;
1654 char_u *ptr;
1655 int save_p_list;
1656 int start_col;
1657 colnr_T vc;
1658#ifdef FEAT_VREPLACE
1659 colnr_T orig_col = 0; /* init for GCC */
1660 char_u *new_line, *orig_line = NULL; /* init for GCC */
1661
1662 /* VREPLACE mode needs to know what the line was like before changing */
1663 if (State & VREPLACE_FLAG)
1664 {
1665 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1666 orig_col = curwin->w_cursor.col;
1667 }
1668#endif
1669
1670 /* for the following tricks we don't want list mode */
1671 save_p_list = curwin->w_p_list;
1672 curwin->w_p_list = FALSE;
1673 vc = getvcol_nolist(&curwin->w_cursor);
1674 vcol = vc;
1675
1676 /*
1677 * For Replace mode we need to fix the replace stack later, which is only
1678 * possible when the cursor is in the indent. Remember the number of
1679 * characters before the cursor if it's possible.
1680 */
1681 start_col = curwin->w_cursor.col;
1682
1683 /* determine offset from first non-blank */
1684 new_cursor_col = curwin->w_cursor.col;
1685 beginline(BL_WHITE);
1686 new_cursor_col -= curwin->w_cursor.col;
1687
1688 insstart_less = curwin->w_cursor.col;
1689
1690 /*
1691 * If the cursor is in the indent, compute how many screen columns the
1692 * cursor is to the left of the first non-blank.
1693 */
1694 if (new_cursor_col < 0)
1695 vcol = get_indent() - vcol;
1696
1697 if (new_cursor_col > 0) /* can't fix replace stack */
1698 start_col = -1;
1699
1700 /*
1701 * Set the new indent. The cursor will be put on the first non-blank.
1702 */
1703 if (type == INDENT_SET)
1704 (void)set_indent(amount, SIN_CHANGED);
1705 else
1706 {
1707#ifdef FEAT_VREPLACE
1708 int save_State = State;
1709
1710 /* Avoid being called recursively. */
1711 if (State & VREPLACE_FLAG)
1712 State = INSERT;
1713#endif
1714 shift_line(type == INDENT_DEC, round, 1);
1715#ifdef FEAT_VREPLACE
1716 State = save_State;
1717#endif
1718 }
1719 insstart_less -= curwin->w_cursor.col;
1720
1721 /*
1722 * Try to put cursor on same character.
1723 * If the cursor is at or after the first non-blank in the line,
1724 * compute the cursor column relative to the column of the first
1725 * non-blank character.
1726 * If we are not in insert mode, leave the cursor on the first non-blank.
1727 * If the cursor is before the first non-blank, position it relative
1728 * to the first non-blank, counted in screen columns.
1729 */
1730 if (new_cursor_col >= 0)
1731 {
1732 /*
1733 * When changing the indent while the cursor is touching it, reset
1734 * Insstart_col to 0.
1735 */
1736 if (new_cursor_col == 0)
1737 insstart_less = MAXCOL;
1738 new_cursor_col += curwin->w_cursor.col;
1739 }
1740 else if (!(State & INSERT))
1741 new_cursor_col = curwin->w_cursor.col;
1742 else
1743 {
1744 /*
1745 * Compute the screen column where the cursor should be.
1746 */
1747 vcol = get_indent() - vcol;
1748 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1749
1750 /*
1751 * Advance the cursor until we reach the right screen column.
1752 */
1753 vcol = last_vcol = 0;
1754 new_cursor_col = -1;
1755 ptr = ml_get_curline();
1756 while (vcol <= (int)curwin->w_virtcol)
1757 {
1758 last_vcol = vcol;
1759#ifdef FEAT_MBYTE
1760 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001761 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 else
1763#endif
1764 ++new_cursor_col;
1765 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1766 }
1767 vcol = last_vcol;
1768
1769 /*
1770 * May need to insert spaces to be able to position the cursor on
1771 * the right screen column.
1772 */
1773 if (vcol != (int)curwin->w_virtcol)
1774 {
1775 curwin->w_cursor.col = new_cursor_col;
1776 i = (int)curwin->w_virtcol - vcol;
1777 ptr = alloc(i + 1);
1778 if (ptr != NULL)
1779 {
1780 new_cursor_col += i;
1781 ptr[i] = NUL;
1782 while (--i >= 0)
1783 ptr[i] = ' ';
1784 ins_str(ptr);
1785 vim_free(ptr);
1786 }
1787 }
1788
1789 /*
1790 * When changing the indent while the cursor is in it, reset
1791 * Insstart_col to 0.
1792 */
1793 insstart_less = MAXCOL;
1794 }
1795
1796 curwin->w_p_list = save_p_list;
1797
1798 if (new_cursor_col <= 0)
1799 curwin->w_cursor.col = 0;
1800 else
1801 curwin->w_cursor.col = new_cursor_col;
1802 curwin->w_set_curswant = TRUE;
1803 changed_cline_bef_curs();
1804
1805 /*
1806 * May have to adjust the start of the insert.
1807 */
1808 if (State & INSERT)
1809 {
1810 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1811 {
1812 if ((int)Insstart.col <= insstart_less)
1813 Insstart.col = 0;
1814 else
1815 Insstart.col -= insstart_less;
1816 }
1817 if ((int)ai_col <= insstart_less)
1818 ai_col = 0;
1819 else
1820 ai_col -= insstart_less;
1821 }
1822
1823 /*
1824 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1825 * If the number of characters before the cursor decreased, need to pop a
1826 * few characters from the replace stack.
1827 * If the number of characters before the cursor increased, need to push a
1828 * few NULs onto the replace stack.
1829 */
1830 if (REPLACE_NORMAL(State) && start_col >= 0)
1831 {
1832 while (start_col > (int)curwin->w_cursor.col)
1833 {
1834 replace_join(0); /* remove a NUL from the replace stack */
1835 --start_col;
1836 }
1837 while (start_col < (int)curwin->w_cursor.col || replaced)
1838 {
1839 replace_push(NUL);
1840 if (replaced)
1841 {
1842 replace_push(replaced);
1843 replaced = NUL;
1844 }
1845 ++start_col;
1846 }
1847 }
1848
1849#ifdef FEAT_VREPLACE
1850 /*
1851 * For VREPLACE mode, we also have to fix the replace stack. In this case
1852 * it is always possible because we backspace over the whole line and then
1853 * put it back again the way we wanted it.
1854 */
1855 if (State & VREPLACE_FLAG)
1856 {
1857 /* If orig_line didn't allocate, just return. At least we did the job,
1858 * even if you can't backspace. */
1859 if (orig_line == NULL)
1860 return;
1861
1862 /* Save new line */
1863 new_line = vim_strsave(ml_get_curline());
1864 if (new_line == NULL)
1865 return;
1866
1867 /* We only put back the new line up to the cursor */
1868 new_line[curwin->w_cursor.col] = NUL;
1869
1870 /* Put back original line */
1871 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1872 curwin->w_cursor.col = orig_col;
1873
1874 /* Backspace from cursor to start of line */
1875 backspace_until_column(0);
1876
1877 /* Insert new stuff into line again */
1878 ins_bytes(new_line);
1879
1880 vim_free(new_line);
1881 }
1882#endif
1883}
1884
1885/*
1886 * Truncate the space at the end of a line. This is to be used only in an
1887 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1888 * modes.
1889 */
1890 void
1891truncate_spaces(line)
1892 char_u *line;
1893{
1894 int i;
1895
1896 /* find start of trailing white space */
1897 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1898 {
1899 if (State & REPLACE_FLAG)
1900 replace_join(0); /* remove a NUL from the replace stack */
1901 }
1902 line[i + 1] = NUL;
1903}
1904
1905#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1906 || defined(FEAT_COMMENTS) || defined(PROTO)
1907/*
1908 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1909 * modes correctly. May also be used when not in insert mode at all.
1910 */
1911 void
1912backspace_until_column(col)
1913 int col;
1914{
1915 while ((int)curwin->w_cursor.col > col)
1916 {
1917 curwin->w_cursor.col--;
1918 if (State & REPLACE_FLAG)
1919 replace_do_bs();
1920 else
1921 (void)del_char(FALSE);
1922 }
1923}
1924#endif
1925
1926#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1927/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001928 * CTRL-X pressed in Insert mode.
1929 */
1930 static void
1931ins_ctrl_x()
1932{
1933 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1934 * CTRL-V works like CTRL-N */
1935 if (ctrl_x_mode != CTRL_X_CMDLINE)
1936 {
1937 /* if the next ^X<> won't ADD nothing, then reset
1938 * compl_cont_status */
1939 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001940 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001941 else
1942 compl_cont_status = 0;
1943 /* We're not sure which CTRL-X mode it will be yet */
1944 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1945 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1946 edit_submode_pre = NULL;
1947 showmode();
1948 }
1949}
1950
1951/*
1952 * Return TRUE if the 'dict' or 'tsr' option can be used.
1953 */
1954 static int
1955has_compl_option(dict_opt)
1956 int dict_opt;
1957{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001958 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00001959# ifdef FEAT_SPELL
1960 && !curwin->w_p_spell
1961# endif
1962 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001963 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1964 {
1965 ctrl_x_mode = 0;
1966 edit_submode = NULL;
1967 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1968 : (char_u *)_("'thesaurus' option is empty"),
1969 hl_attr(HLF_E));
1970 if (emsg_silent == 0)
1971 {
1972 vim_beep();
1973 setcursor();
1974 out_flush();
1975 ui_delay(2000L, FALSE);
1976 }
1977 return FALSE;
1978 }
1979 return TRUE;
1980}
1981
1982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1984 * This depends on the current mode.
1985 */
1986 int
1987vim_is_ctrl_x_key(c)
1988 int c;
1989{
1990 /* Always allow ^R - let it's results then be checked */
1991 if (c == Ctrl_R)
1992 return TRUE;
1993
Bram Moolenaare3226be2005-12-18 22:10:00 +00001994 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001995 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001996 return TRUE;
1997
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 switch (ctrl_x_mode)
1999 {
2000 case 0: /* Not in any CTRL-X mode */
2001 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2002 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002003 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2005 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2006 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002007 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2008 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 case CTRL_X_SCROLL:
2010 return (c == Ctrl_Y || c == Ctrl_E);
2011 case CTRL_X_WHOLE_LINE:
2012 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2013 case CTRL_X_FILES:
2014 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2015 case CTRL_X_DICTIONARY:
2016 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2017 case CTRL_X_THESAURUS:
2018 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2019 case CTRL_X_TAGS:
2020 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2021#ifdef FEAT_FIND_ID
2022 case CTRL_X_PATH_PATTERNS:
2023 return (c == Ctrl_P || c == Ctrl_N);
2024 case CTRL_X_PATH_DEFINES:
2025 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2026#endif
2027 case CTRL_X_CMDLINE:
2028 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2029 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002030#ifdef FEAT_COMPL_FUNC
2031 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002032 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002033 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002034 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002035#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002036 case CTRL_X_SPELL:
2037 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039 EMSG(_(e_internal));
2040 return FALSE;
2041}
2042
2043/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002044 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 * case of the originally typed text is used, and the case of the completed
2046 * text is infered, ie this tries to work out what case you probably wanted
2047 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002048 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 */
2050 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002051ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 char_u *str;
2053 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002054 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 char_u *fname;
2056 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002057 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058{
2059 int has_lower = FALSE;
2060 int was_letter = FALSE;
2061 int idx;
2062
2063 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2064 {
2065 /* Infer case of completed part -- webb */
2066 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002067 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068
2069 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002070 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002072 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
2074 has_lower = TRUE;
2075 if (isupper(IObuff[idx]))
2076 {
2077 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002078 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2080 break;
2081 }
2082 }
2083 }
2084
2085 /*
2086 * Rule 2: No lower case, 2nd consecutive letter converted to
2087 * upper case.
2088 */
2089 if (!has_lower)
2090 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002091 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002093 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 && islower(IObuff[idx]))
2095 {
2096 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002097 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2099 break;
2100 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002101 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 }
2103 }
2104
2105 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002106 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002108 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2109 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002111 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112}
2113
2114/*
2115 * Add a match to the list of matches.
2116 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002117 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002118 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002120 static int
2121ins_compl_add(str, len, icase, fname, cptext, cdir, flags, dup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 char_u *str;
2123 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002124 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002126 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002127 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002128 int flags;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002129 int dup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002131 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002132 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
2134 ui_breakcheck();
2135 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002136 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 if (len < 0)
2138 len = (int)STRLEN(str);
2139
2140 /*
2141 * If the same match is already present, don't add it.
2142 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002143 if (compl_first_match != NULL && !dup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002145 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 do
2147 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002148 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002149 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002150 && match->cp_str[len] == NUL)
2151 return NOTDONE;
2152 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002153 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 }
2155
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002156 /* Remove any popup menu before changing the list of matches. */
2157 ins_compl_del_pum();
2158
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 /*
2160 * Allocate a new match structure.
2161 * Copy the values to the new match structure.
2162 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002163 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002165 return FAIL;
2166 match->cp_number = -1;
2167 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002168 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002169 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
2171 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002172 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002174 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002175
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002177 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2179 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002180 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002181 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002182 && compl_curr_match->cp_fname != NULL
2183 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002184 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002185 else if (fname != NULL)
2186 {
2187 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002188 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002191 match->cp_fname = NULL;
2192 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002193
2194 if (cptext != NULL)
2195 {
2196 int i;
2197
2198 for (i = 0; i < CPT_COUNT; ++i)
2199 if (cptext[i] != NULL && *cptext[i] != NUL)
2200 match->cp_text[i] = vim_strsave(cptext[i]);
2201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
2203 /*
2204 * Link the new match structure in the list of matches.
2205 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002206 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002207 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 else if (dir == FORWARD)
2209 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002210 match->cp_next = compl_curr_match->cp_next;
2211 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 }
2213 else /* BACKWARD */
2214 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002215 match->cp_next = compl_curr_match;
2216 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002218 if (match->cp_next)
2219 match->cp_next->cp_prev = match;
2220 if (match->cp_prev)
2221 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002223 compl_first_match = match;
2224 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002226 /*
2227 * Find the longest common string if still doing that.
2228 */
2229 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2230 ins_compl_longest_match(match);
2231
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 return OK;
2233}
2234
2235/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002236 * Return TRUE if "str[len]" matches with match->cp_str, considering
2237 * match->cp_icase.
2238 */
2239 static int
2240ins_compl_equal(match, str, len)
2241 compl_T *match;
2242 char_u *str;
2243 int len;
2244{
2245 if (match->cp_icase)
2246 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2247 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2248}
2249
2250/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002251 * Reduce the longest common string for match "match".
2252 */
2253 static void
2254ins_compl_longest_match(match)
2255 compl_T *match;
2256{
2257 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002258 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002259 int had_match;
2260
2261 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002262 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002263 /* First match, use it as a whole. */
2264 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002265 if (compl_leader != NULL)
2266 {
2267 had_match = (curwin->w_cursor.col > compl_col);
2268 ins_compl_delete();
2269 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2270 ins_redraw(FALSE);
2271
2272 /* When the match isn't there (to avoid matching itself) remove it
2273 * again after redrawing. */
2274 if (!had_match)
2275 ins_compl_delete();
2276 compl_used_match = FALSE;
2277 }
2278 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002279 else
2280 {
2281 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002282 p = compl_leader;
2283 s = match->cp_str;
2284 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002285 {
2286#ifdef FEAT_MBYTE
2287 if (has_mbyte)
2288 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002289 c1 = mb_ptr2char(p);
2290 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002291 }
2292 else
2293#endif
2294 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002295 c1 = *p;
2296 c2 = *s;
2297 }
2298 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2299 : (c1 != c2))
2300 break;
2301#ifdef FEAT_MBYTE
2302 if (has_mbyte)
2303 {
2304 mb_ptr_adv(p);
2305 mb_ptr_adv(s);
2306 }
2307 else
2308#endif
2309 {
2310 ++p;
2311 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002312 }
2313 }
2314
2315 if (*p != NUL)
2316 {
2317 /* Leader was shortened, need to change the inserted text. */
2318 *p = NUL;
2319 had_match = (curwin->w_cursor.col > compl_col);
2320 ins_compl_delete();
2321 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2322 ins_redraw(FALSE);
2323
2324 /* When the match isn't there (to avoid matching itself) remove it
2325 * again after redrawing. */
2326 if (!had_match)
2327 ins_compl_delete();
2328 }
2329
2330 compl_used_match = FALSE;
2331 }
2332}
2333
2334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 * Add an array of matches to the list of matches.
2336 * Frees matches[].
2337 */
2338 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002339ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 int num_matches;
2341 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002342 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
2344 int i;
2345 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002346 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
Bram Moolenaar572cb562005-08-05 21:35:02 +00002348 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002349 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002350 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002352 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 FreeWild(num_matches, matches);
2354}
2355
2356/* Make the completion list cyclic.
2357 * Return the number of matches (excluding the original).
2358 */
2359 static int
2360ins_compl_make_cyclic()
2361{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002362 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 int count = 0;
2364
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002365 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
2367 /*
2368 * Find the end of the list.
2369 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002370 match = compl_first_match;
2371 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002372 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002374 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 ++count;
2376 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002377 match->cp_next = compl_first_match;
2378 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 }
2380 return count;
2381}
2382
Bram Moolenaara94bc432006-03-10 21:42:59 +00002383/*
2384 * Start completion for the complete() function.
2385 * "startcol" is where the matched text starts (1 is first column).
2386 * "list" is the list of matches.
2387 */
2388 void
2389set_completion(startcol, list)
2390 int startcol;
2391 list_T *list;
2392{
2393 /* If already doing completions stop it. */
2394 if (ctrl_x_mode != 0)
2395 ins_compl_prep(' ');
2396 ins_compl_clear();
2397
2398 if (stop_arrow() == FAIL)
2399 return;
2400
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002401 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002402 startcol = curwin->w_cursor.col;
2403 compl_col = startcol;
2404 compl_length = curwin->w_cursor.col - startcol;
2405 /* compl_pattern doesn't need to be set */
2406 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2407 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002408 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002409 return;
2410
2411 /* Handle like dictionary completion. */
2412 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2413
2414 ins_compl_add_list(list);
2415 compl_matches = ins_compl_make_cyclic();
2416 compl_started = TRUE;
2417 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002418 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002419
2420 compl_curr_match = compl_first_match;
2421 ins_complete(Ctrl_N);
2422 out_flush();
2423}
2424
2425
Bram Moolenaar9372a112005-12-06 19:59:18 +00002426/* "compl_match_array" points the currently displayed list of entries in the
2427 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002428static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002429static int compl_match_arraysize;
2430
2431/*
2432 * Update the screen and when there is any scrolling remove the popup menu.
2433 */
2434 static void
2435ins_compl_upd_pum()
2436{
2437 int h;
2438
2439 if (compl_match_array != NULL)
2440 {
2441 h = curwin->w_cline_height;
2442 update_screen(0);
2443 if (h != curwin->w_cline_height)
2444 ins_compl_del_pum();
2445 }
2446}
2447
2448/*
2449 * Remove any popup menu.
2450 */
2451 static void
2452ins_compl_del_pum()
2453{
2454 if (compl_match_array != NULL)
2455 {
2456 pum_undisplay();
2457 vim_free(compl_match_array);
2458 compl_match_array = NULL;
2459 }
2460}
2461
2462/*
2463 * Return TRUE if the popup menu should be displayed.
2464 */
2465 static int
2466pum_wanted()
2467{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002468 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002469 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002470 return FALSE;
2471
2472 /* The display looks bad on a B&W display. */
2473 if (t_colors < 8
2474#ifdef FEAT_GUI
2475 && !gui.in_use
2476#endif
2477 )
2478 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002479 return TRUE;
2480}
2481
2482/*
2483 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002484 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002485 */
2486 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002487pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002488{
2489 compl_T *compl;
2490 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002491
2492 /* Don't display the popup menu if there are no matches or there is only
2493 * one (ignoring the original text). */
2494 compl = compl_first_match;
2495 i = 0;
2496 do
2497 {
2498 if (compl == NULL
2499 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2500 break;
2501 compl = compl->cp_next;
2502 } while (compl != compl_first_match);
2503
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002504 if (strstr((char *)p_cot, "menuone") != NULL)
2505 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002506 return (i >= 2);
2507}
2508
2509/*
2510 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002511 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002512 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002513 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002514ins_compl_show_pum()
2515{
2516 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002517 compl_T *shown_compl = NULL;
2518 int did_find_shown_match = FALSE;
2519 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002520 int i;
2521 int cur = -1;
2522 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002523 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002524
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002525 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002526 return;
2527
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002528#if defined(FEAT_EVAL)
2529 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2530 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2531#endif
2532
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002533 /* Update the screen before drawing the popup menu over it. */
2534 update_screen(0);
2535
2536 if (compl_match_array == NULL)
2537 {
2538 /* Need to build the popup menu list. */
2539 compl_match_arraysize = 0;
2540 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002541 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002542 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002543 do
2544 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002545 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2546 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002547 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002548 ++compl_match_arraysize;
2549 compl = compl->cp_next;
2550 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002551 if (compl_match_arraysize == 0)
2552 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002553 compl_match_array = (pumitem_T *)alloc_clear(
2554 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002555 * compl_match_arraysize));
2556 if (compl_match_array != NULL)
2557 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002558 /* If the current match is the original text don't find the first
2559 * match after it, don't highlight anything. */
2560 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2561 shown_match_ok = TRUE;
2562
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002563 i = 0;
2564 compl = compl_first_match;
2565 do
2566 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002567 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2568 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002569 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002570 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002571 if (!shown_match_ok)
2572 {
2573 if (compl == compl_shown_match || did_find_shown_match)
2574 {
2575 /* This item is the shown match or this is the
2576 * first displayed item after the shown match. */
2577 compl_shown_match = compl;
2578 did_find_shown_match = TRUE;
2579 shown_match_ok = TRUE;
2580 }
2581 else
2582 /* Remember this displayed match for when the
2583 * shown match is just below it. */
2584 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002585 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002586 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002587
2588 if (compl->cp_text[CPT_ABBR] != NULL)
2589 compl_match_array[i].pum_text =
2590 compl->cp_text[CPT_ABBR];
2591 else
2592 compl_match_array[i].pum_text = compl->cp_str;
2593 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2594 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2595 if (compl->cp_text[CPT_MENU] != NULL)
2596 compl_match_array[i++].pum_extra =
2597 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002598 else
2599 compl_match_array[i++].pum_extra = compl->cp_fname;
2600 }
2601
2602 if (compl == compl_shown_match)
2603 {
2604 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002605
2606 /* When the original text is the shown match don't set
2607 * compl_shown_match. */
2608 if (compl->cp_flags & ORIGINAL_TEXT)
2609 shown_match_ok = TRUE;
2610
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002611 if (!shown_match_ok && shown_compl != NULL)
2612 {
2613 /* The shown match isn't displayed, set it to the
2614 * previously displayed match. */
2615 compl_shown_match = shown_compl;
2616 shown_match_ok = TRUE;
2617 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002618 }
2619 compl = compl->cp_next;
2620 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002621
2622 if (!shown_match_ok) /* no displayed match at all */
2623 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002624 }
2625 }
2626 else
2627 {
2628 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002629 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002630 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2631 || compl_match_array[i].pum_text
2632 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002633 {
2634 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002635 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002636 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002637 }
2638
2639 if (compl_match_array != NULL)
2640 {
2641 /* Compute the screen column of the start of the completed text.
2642 * Use the cursor to get all wrapping and other settings right. */
2643 col = curwin->w_cursor.col;
2644 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002645 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002646 curwin->w_cursor.col = col;
2647 }
2648}
2649
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650#define DICT_FIRST (1) /* use just first element in "dict" */
2651#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002652
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002654 * Add any identifiers that match the given pattern in the list of dictionary
2655 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 */
2657 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002658ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2659 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002661 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002662 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002664 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 char_u *ptr;
2666 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 char_u **files;
2669 int count;
2670 int i;
2671 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002672 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
Bram Moolenaar0b238792006-03-02 22:49:12 +00002674 if (*dict == NUL)
2675 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002676#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002677 /* When 'dictionary' is empty and spell checking is enabled use
2678 * "spell". */
2679 if (!thesaurus && curwin->w_p_spell)
2680 dict = (char_u *)"spell";
2681 else
2682#endif
2683 return;
2684 }
2685
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002687 if (buf == NULL)
2688 return;
2689
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 /* If 'infercase' is set, don't use 'smartcase' here */
2691 save_p_scs = p_scs;
2692 if (curbuf->b_p_inf)
2693 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002694
2695 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2696 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002697 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002698 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2699 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002700 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
2701
2702 if (pat_esc == NULL)
2703 return ;
2704 i = (int)STRLEN(pat_esc) + 10;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002705 ptr = alloc(i);
2706 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002707 {
2708 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002709 return;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002710 }
2711 vim_snprintf((char *)ptr, i, "^\\s*\\zs\\V%s", pat_esc);
2712 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2713 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002714 vim_free(ptr);
2715 }
2716 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002717 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002718 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002719 if (regmatch.regprog == NULL)
2720 goto theend;
2721 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002722
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2724 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002725 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
2727 /* copy one dictionary file name into buf */
2728 if (flags == DICT_EXACT)
2729 {
2730 count = 1;
2731 files = &dict;
2732 }
2733 else
2734 {
2735 /* Expand wildcards in the dictionary name, but do not allow
2736 * backticks (for security, the 'dict' option may have been set in
2737 * a modeline). */
2738 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002739# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002740 if (!thesaurus && STRCMP(buf, "spell") == 0)
2741 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002742 else
2743# endif
2744 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 || expand_wildcards(1, &buf, &count, &files,
2746 EW_FILE|EW_SILENT) != OK)
2747 count = 0;
2748 }
2749
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002750# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002751 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002753 /* Complete from active spelling. Skip "\<" in the pattern, we
2754 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002755 if (pat[0] == '\\' && pat[1] == '<')
2756 ptr = pat + 2;
2757 else
2758 ptr = pat;
2759 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002761 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002762# endif
Bram Moolenaar0b238792006-03-02 22:49:12 +00002763 {
2764 ins_compl_files(count, files, thesaurus, flags,
2765 &regmatch, buf, &dir);
2766 if (flags != DICT_EXACT)
2767 FreeWild(count, files);
2768 }
2769 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 break;
2771 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002772
2773theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 p_scs = save_p_scs;
2775 vim_free(regmatch.regprog);
2776 vim_free(buf);
2777}
2778
Bram Moolenaar0b238792006-03-02 22:49:12 +00002779 static void
2780ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2781 int count;
2782 char_u **files;
2783 int thesaurus;
2784 int flags;
2785 regmatch_T *regmatch;
2786 char_u *buf;
2787 int *dir;
2788{
2789 char_u *ptr;
2790 int i;
2791 FILE *fp;
2792 int add_r;
2793
2794 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2795 {
2796 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2797 if (flags != DICT_EXACT)
2798 {
2799 vim_snprintf((char *)IObuff, IOSIZE,
2800 _("Scanning dictionary: %s"), (char *)files[i]);
2801 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2802 }
2803
2804 if (fp != NULL)
2805 {
2806 /*
2807 * Read dictionary file line by line.
2808 * Check each line for a match.
2809 */
2810 while (!got_int && !compl_interrupted
2811 && !vim_fgets(buf, LSIZE, fp))
2812 {
2813 ptr = buf;
2814 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2815 {
2816 ptr = regmatch->startp[0];
2817 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2818 ptr = find_line_end(ptr);
2819 else
2820 ptr = find_word_end(ptr);
2821 add_r = ins_compl_add_infercase(regmatch->startp[0],
2822 (int)(ptr - regmatch->startp[0]),
Bram Moolenaar91170f82006-05-05 21:15:17 +00002823 FALSE, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002824 if (thesaurus)
2825 {
2826 char_u *wstart;
2827
2828 /*
2829 * Add the other matches on the line
2830 */
2831 while (!got_int)
2832 {
2833 /* Find start of the next word. Skip white
2834 * space and punctuation. */
2835 ptr = find_word_start(ptr);
2836 if (*ptr == NUL || *ptr == NL)
2837 break;
2838 wstart = ptr;
2839
2840 /* Find end of the word and add it. */
2841#ifdef FEAT_MBYTE
2842 if (has_mbyte)
2843 /* Japanese words may have characters in
2844 * different classes, only separate words
2845 * with single-byte non-word characters. */
2846 while (*ptr != NUL)
2847 {
2848 int l = (*mb_ptr2len)(ptr);
2849
2850 if (l < 2 && !vim_iswordc(*ptr))
2851 break;
2852 ptr += l;
2853 }
2854 else
2855#endif
2856 ptr = find_word_end(ptr);
2857 add_r = ins_compl_add_infercase(wstart,
2858 (int)(ptr - wstart),
Bram Moolenaar91170f82006-05-05 21:15:17 +00002859 FALSE, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002860 }
2861 }
2862 if (add_r == OK)
2863 /* if dir was BACKWARD then honor it just once */
2864 *dir = FORWARD;
2865 else if (add_r == FAIL)
2866 break;
2867 /* avoid expensive call to vim_regexec() when at end
2868 * of line */
2869 if (*ptr == '\n' || got_int)
2870 break;
2871 }
2872 line_breakcheck();
2873 ins_compl_check_keys(50);
2874 }
2875 fclose(fp);
2876 }
2877 }
2878}
2879
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880/*
2881 * Find the start of the next word.
2882 * Returns a pointer to the first char of the word. Also stops at a NUL.
2883 */
2884 char_u *
2885find_word_start(ptr)
2886 char_u *ptr;
2887{
2888#ifdef FEAT_MBYTE
2889 if (has_mbyte)
2890 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002891 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 else
2893#endif
2894 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2895 ++ptr;
2896 return ptr;
2897}
2898
2899/*
2900 * Find the end of the word. Assumes it starts inside a word.
2901 * Returns a pointer to just after the word.
2902 */
2903 char_u *
2904find_word_end(ptr)
2905 char_u *ptr;
2906{
2907#ifdef FEAT_MBYTE
2908 int start_class;
2909
2910 if (has_mbyte)
2911 {
2912 start_class = mb_get_class(ptr);
2913 if (start_class > 1)
2914 while (*ptr != NUL)
2915 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002916 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 if (mb_get_class(ptr) != start_class)
2918 break;
2919 }
2920 }
2921 else
2922#endif
2923 while (vim_iswordc(*ptr))
2924 ++ptr;
2925 return ptr;
2926}
2927
2928/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002929 * Find the end of the line, omitting CR and NL at the end.
2930 * Returns a pointer to just after the line.
2931 */
2932 static char_u *
2933find_line_end(ptr)
2934 char_u *ptr;
2935{
2936 char_u *s;
2937
2938 s = ptr + STRLEN(ptr);
2939 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2940 --s;
2941 return s;
2942}
2943
2944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 * Free the list of completions
2946 */
2947 static void
2948ins_compl_free()
2949{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002950 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002951 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002953 vim_free(compl_pattern);
2954 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002955 vim_free(compl_leader);
2956 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002958 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002960
2961 ins_compl_del_pum();
2962 pum_clear();
2963
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002964 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 do
2966 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002967 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002968 compl_curr_match = compl_curr_match->cp_next;
2969 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002971 if (match->cp_flags & FREE_FNAME)
2972 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00002973 for (i = 0; i < CPT_COUNT; ++i)
2974 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002976 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2977 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978}
2979
2980 static void
2981ins_compl_clear()
2982{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002983 compl_cont_status = 0;
2984 compl_started = FALSE;
2985 compl_matches = 0;
2986 vim_free(compl_pattern);
2987 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002988 vim_free(compl_leader);
2989 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002991 vim_free(compl_orig_text);
2992 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002993 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994}
2995
2996/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002997 * Return TRUE when Insert completion is active.
2998 */
2999 int
3000ins_compl_active()
3001{
3002 return compl_started;
3003}
3004
3005/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003006 * Delete one character before the cursor and show the subset of the matches
3007 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003008 * Returns the character to be used, NUL if the work is done and another char
3009 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003010 */
3011 static int
3012ins_compl_bs()
3013{
3014 char_u *line;
3015 char_u *p;
3016
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003017 line = ml_get_curline();
3018 p = line + curwin->w_cursor.col;
3019 mb_ptr_back(line, p);
3020
3021 /* Stop completion when the whole word was deleted. */
3022 if ((int)(p - line) - (int)compl_col <= 0)
3023 return K_BS;
3024
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003025 /* Deleted more than what was used to find matches or didn't finish
3026 * finding all matches: need to look for matches all over again. */
3027 if (curwin->w_cursor.col <= compl_col + compl_length
3028 || compl_was_interrupted)
3029 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003030
Bram Moolenaara6557602006-02-04 22:43:20 +00003031 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003032 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003033 if (compl_leader != NULL)
3034 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003035 ins_compl_new_leader();
3036 return NUL;
3037 }
3038 return K_BS;
3039}
Bram Moolenaara6557602006-02-04 22:43:20 +00003040
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003041/*
3042 * Called after changing "compl_leader".
3043 * Show the popup menu with a different set of matches.
3044 * May also search for matches again if the previous search was interrupted.
3045 */
3046 static void
3047ins_compl_new_leader()
3048{
3049 ins_compl_del_pum();
3050 ins_compl_delete();
3051 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3052 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003053
3054 if (compl_started)
3055 ins_compl_set_original_text(compl_leader);
3056 else
3057 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003058#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003059 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003060#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003061 /*
3062 * Matches were cleared, need to search for them now. First display
3063 * the changed text before the cursor. Set "compl_restarting" to
3064 * avoid that the first match is inserted.
3065 */
3066 update_screen(0);
3067#ifdef FEAT_GUI
3068 if (gui.in_use)
3069 {
3070 /* Show the cursor after the match, not after the redrawn text. */
3071 setcursor();
3072 out_flush();
3073 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003074 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003075#endif
3076 compl_restarting = TRUE;
3077 if (ins_complete(Ctrl_N) == FAIL)
3078 compl_cont_status = 0;
3079 compl_restarting = FALSE;
3080 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003081
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003082#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003083 if (!compl_used_match)
3084 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003085 /* Go to the original text, since none of the matches is inserted. */
3086 if (compl_first_match->cp_prev != NULL
3087 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3088 compl_shown_match = compl_first_match->cp_prev;
3089 else
3090 compl_shown_match = compl_first_match;
3091 compl_curr_match = compl_shown_match;
3092 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003093 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003094#endif
3095 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003096
3097 /* Show the popup menu with a different set of matches. */
3098 ins_compl_show_pum();
Bram Moolenaara6557602006-02-04 22:43:20 +00003099}
3100
3101/*
3102 * Append one character to the match leader. May reduce the number of
3103 * matches.
3104 */
3105 static void
3106ins_compl_addleader(c)
3107 int c;
3108{
3109#ifdef FEAT_MBYTE
3110 int cc;
3111
3112 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3113 {
3114 char_u buf[MB_MAXBYTES + 1];
3115
3116 (*mb_char2bytes)(c, buf);
3117 buf[cc] = NUL;
3118 ins_char_bytes(buf, cc);
3119 }
3120 else
3121#endif
3122 ins_char(c);
3123
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003124 /* If we didn't complete finding matches we must search again. */
3125 if (compl_was_interrupted)
3126 ins_compl_restart();
3127
Bram Moolenaara6557602006-02-04 22:43:20 +00003128 vim_free(compl_leader);
3129 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3130 curwin->w_cursor.col - compl_col);
3131 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003132 ins_compl_new_leader();
3133}
3134
3135/*
3136 * Setup for finding completions again without leaving CTRL-X mode. Used when
3137 * BS or a key was typed while still searching for matches.
3138 */
3139 static void
3140ins_compl_restart()
3141{
3142 ins_compl_free();
3143 compl_started = FALSE;
3144 compl_matches = 0;
3145 compl_cont_status = 0;
3146 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003147}
3148
3149/*
3150 * Set the first match, the original text.
3151 */
3152 static void
3153ins_compl_set_original_text(str)
3154 char_u *str;
3155{
3156 char_u *p;
3157
3158 /* Replace the original text entry. */
3159 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3160 {
3161 p = vim_strsave(str);
3162 if (p != NULL)
3163 {
3164 vim_free(compl_first_match->cp_str);
3165 compl_first_match->cp_str = p;
3166 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003167 }
3168}
3169
3170/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003171 * Append one character to the match leader. May reduce the number of
3172 * matches.
3173 */
3174 static void
3175ins_compl_addfrommatch()
3176{
3177 char_u *p;
3178 int len = curwin->w_cursor.col - compl_col;
3179 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003180 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003181
3182 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003183 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003184 {
3185 /* When still at the original match use the first entry that matches
3186 * the leader. */
3187 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3188 {
3189 p = NULL;
3190 for (cp = compl_shown_match->cp_next; cp != NULL
3191 && cp != compl_first_match; cp = cp->cp_next)
3192 {
3193 if (ins_compl_equal(cp, compl_leader,
3194 (int)STRLEN(compl_leader)))
3195 {
3196 p = cp->cp_str;
3197 break;
3198 }
3199 }
3200 if (p == NULL || (int)STRLEN(p) <= len)
3201 return;
3202 }
3203 else
3204 return;
3205 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003206 p += len;
3207#ifdef FEAT_MBYTE
3208 c = mb_ptr2char(p);
3209#else
3210 c = *p;
3211#endif
3212 ins_compl_addleader(c);
3213}
3214
3215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003217 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003218 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003220 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221ins_compl_prep(c)
3222 int c;
3223{
3224 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 int temp;
3226 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003227 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228
3229 /* Forget any previous 'special' messages if this is actually
3230 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3231 */
3232 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3233 edit_submode_extra = NULL;
3234
3235 /* Ignore end of Select mode mapping */
3236 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003237 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003239 /* Set "compl_get_longest" when finding the first matches. */
3240 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3241 || (ctrl_x_mode == 0 && !compl_started))
3242 {
3243 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3244 compl_used_match = TRUE;
3245 }
3246
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3248 {
3249 /*
3250 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3251 * it will be yet. Now we decide.
3252 */
3253 switch (c)
3254 {
3255 case Ctrl_E:
3256 case Ctrl_Y:
3257 ctrl_x_mode = CTRL_X_SCROLL;
3258 if (!(State & REPLACE_FLAG))
3259 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3260 else
3261 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3262 edit_submode_pre = NULL;
3263 showmode();
3264 break;
3265 case Ctrl_L:
3266 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3267 break;
3268 case Ctrl_F:
3269 ctrl_x_mode = CTRL_X_FILES;
3270 break;
3271 case Ctrl_K:
3272 ctrl_x_mode = CTRL_X_DICTIONARY;
3273 break;
3274 case Ctrl_R:
3275 /* Simply allow ^R to happen without affecting ^X mode */
3276 break;
3277 case Ctrl_T:
3278 ctrl_x_mode = CTRL_X_THESAURUS;
3279 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003280#ifdef FEAT_COMPL_FUNC
3281 case Ctrl_U:
3282 ctrl_x_mode = CTRL_X_FUNCTION;
3283 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003284 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003285 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003286 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003287#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003288 case 's':
3289 case Ctrl_S:
3290 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003291#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003292 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003293 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003294 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003295#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003296 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 case Ctrl_RSB:
3298 ctrl_x_mode = CTRL_X_TAGS;
3299 break;
3300#ifdef FEAT_FIND_ID
3301 case Ctrl_I:
3302 case K_S_TAB:
3303 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3304 break;
3305 case Ctrl_D:
3306 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3307 break;
3308#endif
3309 case Ctrl_V:
3310 case Ctrl_Q:
3311 ctrl_x_mode = CTRL_X_CMDLINE;
3312 break;
3313 case Ctrl_P:
3314 case Ctrl_N:
3315 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3316 * just started ^X mode, or there were enough ^X's to cancel
3317 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3318 * do normal expansion when interrupting a different mode (say
3319 * ^X^F^X^P or ^P^X^X^P, see below)
3320 * nothing changes if interrupting mode 0, (eg, the flag
3321 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003322 if (!(compl_cont_status & CONT_INTRPT))
3323 compl_cont_status |= CONT_LOCAL;
3324 else if (compl_cont_mode != 0)
3325 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 /* FALLTHROUGH */
3327 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003328 /* If we have typed at least 2 ^X's... for modes != 0, we set
3329 * compl_cont_status = 0 (eg, as if we had just started ^X
3330 * mode).
3331 * For mode 0, we set "compl_cont_mode" to an impossible
3332 * value, in both cases ^X^X can be used to restart the same
3333 * mode (avoiding ADDING mode).
3334 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3335 * 'complete' and local ^P expansions respectively.
3336 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3337 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 if (c == Ctrl_X)
3339 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003340 if (compl_cont_mode != 0)
3341 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003343 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 }
3345 ctrl_x_mode = 0;
3346 edit_submode = NULL;
3347 showmode();
3348 break;
3349 }
3350 }
3351 else if (ctrl_x_mode != 0)
3352 {
3353 /* We're already in CTRL-X mode, do we stay in it? */
3354 if (!vim_is_ctrl_x_key(c))
3355 {
3356 if (ctrl_x_mode == CTRL_X_SCROLL)
3357 ctrl_x_mode = 0;
3358 else
3359 ctrl_x_mode = CTRL_X_FINISHED;
3360 edit_submode = NULL;
3361 }
3362 showmode();
3363 }
3364
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003365 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
3367 /* Show error message from attempted keyword completion (probably
3368 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003369 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003371 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3372 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 || ctrl_x_mode == CTRL_X_FINISHED)
3374 {
3375 /* Get here when we have finished typing a sequence of ^N and
3376 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003377 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003378 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003380 char_u *p;
3381
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003383 * If any of the original typed text has been changed, eg when
3384 * ignorecase is set, we must add back-spaces to the redo
3385 * buffer. We add as few as necessary to delete just the part
3386 * of the original text that has changed.
3387 * When using the longest match, edited the match or used
3388 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003390 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3391 ptr = compl_curr_match->cp_str;
3392 else if (compl_leader != NULL)
3393 ptr = compl_leader;
3394 else
3395 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003396 if (compl_orig_text != NULL)
3397 {
3398 p = compl_orig_text;
3399 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3400 ++temp)
3401 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003402#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003403 if (temp > 0)
3404 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003405#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003406 for (p += temp; *p != NUL; mb_ptr_adv(p))
3407 AppendCharToRedobuff(K_BS);
3408 }
3409 if (ptr != NULL)
3410 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 }
3412
3413#ifdef FEAT_CINDENT
3414 want_cindent = (can_cindent && cindent_on());
3415#endif
3416 /*
3417 * When completing whole lines: fix indent for 'cindent'.
3418 * Otherwise, break line if it's too long.
3419 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003420 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 {
3422#ifdef FEAT_CINDENT
3423 /* re-indent the current line */
3424 if (want_cindent)
3425 {
3426 do_c_expr_indent();
3427 want_cindent = FALSE; /* don't do it again */
3428 }
3429#endif
3430 }
3431 else
3432 {
3433 /* put the cursor on the last char, for 'tw' formatting */
3434 curwin->w_cursor.col--;
3435 if (stop_arrow() == OK)
3436 insertchar(NUL, 0, -1);
3437 curwin->w_cursor.col++;
3438 }
3439
3440 auto_format(FALSE, TRUE);
3441
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003442 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003443 * the selection without inserting anything. When
3444 * compl_enter_selects is set the Enter key does the same. */
3445 if ((c == Ctrl_Y || (compl_enter_selects
3446 && (c == CAR || c == K_KENTER || c == NL)))
3447 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003448 retval = TRUE;
3449
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003450 /* CTRL-E means completion is Ended, go back to the typed text. */
3451 if (c == Ctrl_E)
3452 {
3453 ins_compl_delete();
3454 if (compl_leader != NULL)
3455 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3456 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003457 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003458 + curwin->w_cursor.col - compl_col);
3459 retval = TRUE;
3460 }
3461
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003463 compl_started = FALSE;
3464 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 msg_clr_cmdline(); /* necessary for "noshowmode" */
3466 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003467 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 if (edit_submode != NULL)
3469 {
3470 edit_submode = NULL;
3471 showmode();
3472 }
3473
3474#ifdef FEAT_CINDENT
3475 /*
3476 * Indent now if a key was typed that is in 'cinkeys'.
3477 */
3478 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3479 do_c_expr_indent();
3480#endif
3481 }
3482 }
3483
3484 /* reset continue_* if we left expansion-mode, if we stay they'll be
3485 * (re)set properly in ins_complete() */
3486 if (!vim_is_ctrl_x_key(c))
3487 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003488 compl_cont_status = 0;
3489 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003491
3492 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493}
3494
3495/*
3496 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3497 * (depending on flag) starting from buf and looking for a non-scanned
3498 * buffer (other than curbuf). curbuf is special, if it is called with
3499 * buf=curbuf then it has to be the first call for a given flag/expansion.
3500 *
3501 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3502 */
3503 static buf_T *
3504ins_compl_next_buf(buf, flag)
3505 buf_T *buf;
3506 int flag;
3507{
3508#ifdef FEAT_WINDOWS
3509 static win_T *wp;
3510#endif
3511
3512 if (flag == 'w') /* just windows */
3513 {
3514#ifdef FEAT_WINDOWS
3515 if (buf == curbuf) /* first call for this flag/expansion */
3516 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003517 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 && wp->w_buffer->b_scanned)
3519 ;
3520 buf = wp->w_buffer;
3521#else
3522 buf = curbuf;
3523#endif
3524 }
3525 else
3526 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3527 * (unlisted buffers)
3528 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003529 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 && ((flag == 'U'
3531 ? buf->b_p_bl
3532 : (!buf->b_p_bl
3533 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003534 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 ;
3536 return buf;
3537}
3538
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003539#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003540static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003541
3542/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003543 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003544 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003545 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003546 static void
3547expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003548 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003549 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003550{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003551 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003552 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003553 char_u *funcname;
3554 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003555
Bram Moolenaare344bea2005-09-01 20:46:49 +00003556 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3557 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003558 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003559
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003560 /* Call 'completefunc' to obtain the list of matches. */
3561 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003562 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003563
Bram Moolenaare344bea2005-09-01 20:46:49 +00003564 pos = curwin->w_cursor;
3565 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3566 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003567 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003568 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003569
Bram Moolenaara94bc432006-03-10 21:42:59 +00003570 ins_compl_add_list(matchlist);
3571 list_unref(matchlist);
3572}
3573#endif /* FEAT_COMPL_FUNC */
3574
Bram Moolenaar39f05632006-03-19 22:15:26 +00003575#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003576/*
3577 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003578 */
3579 static void
3580ins_compl_add_list(list)
3581 list_T *list;
3582{
3583 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003584 int dir = compl_direction;
3585
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003586 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003587 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003588 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003589 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3590 /* if dir was BACKWARD then honor it just once */
3591 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003592 else if (did_emsg)
3593 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003594 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003595}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003596
3597/*
3598 * Add a match to the list of matches from a typeval_T.
3599 * If the given string is already in the list of completions, then return
3600 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3601 * maybe because alloc() returns NULL, then FAIL is returned.
3602 */
3603 int
3604ins_compl_add_tv(tv, dir)
3605 typval_T *tv;
3606 int dir;
3607{
3608 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003609 int icase = FALSE;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003610 int dup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003611 char_u *(cptext[CPT_COUNT]);
3612
3613 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3614 {
3615 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3616 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3617 (char_u *)"abbr", FALSE);
3618 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3619 (char_u *)"menu", FALSE);
3620 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3621 (char_u *)"kind", FALSE);
3622 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3623 (char_u *)"info", FALSE);
3624 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3625 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003626 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
3627 dup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003628 }
3629 else
3630 {
3631 word = get_tv_string_chk(tv);
3632 vim_memset(cptext, 0, sizeof(cptext));
3633 }
3634 if (word == NULL || *word == NUL)
3635 return FAIL;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003636 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, dup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003637}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003638#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003639
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003640/*
3641 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003642 * The search starts at position "ini" in curbuf and in the direction
3643 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003644 * When "compl_started" is FALSE start at that position, otherwise continue
3645 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003646 * This may return before finding all the matches.
3647 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 */
3649 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003650ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652{
3653 static pos_T first_match_pos;
3654 static pos_T last_match_pos;
3655 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003656 static int found_all = FALSE; /* Found all matches of a
3657 certain type. */
3658 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
Bram Moolenaar572cb562005-08-05 21:35:02 +00003660 pos_T *pos;
3661 char_u **matches;
3662 int save_p_scs;
3663 int save_p_ws;
3664 int save_p_ic;
3665 int i;
3666 int num_matches;
3667 int len;
3668 int found_new_match;
3669 int type = ctrl_x_mode;
3670 char_u *ptr;
3671 char_u *dict = NULL;
3672 int dict_f = 0;
3673 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003675 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 {
3677 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3678 ins_buf->b_scanned = 0;
3679 found_all = FALSE;
3680 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003681 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003682 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 last_match_pos = first_match_pos = *ini;
3684 }
3685
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003686 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003687 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3689 for (;;)
3690 {
3691 found_new_match = FAIL;
3692
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003693 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 * or if found_all says this entry is done. For ^X^L only use the
3695 * entries from 'complete' that look in loaded buffers. */
3696 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003697 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
3699 found_all = FALSE;
3700 while (*e_cpt == ',' || *e_cpt == ' ')
3701 e_cpt++;
3702 if (*e_cpt == '.' && !curbuf->b_scanned)
3703 {
3704 ins_buf = curbuf;
3705 first_match_pos = *ini;
3706 /* So that ^N can match word immediately after cursor */
3707 if (ctrl_x_mode == 0)
3708 dec(&first_match_pos);
3709 last_match_pos = first_match_pos;
3710 type = 0;
3711 }
3712 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3713 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3714 {
3715 /* Scan a buffer, but not the current one. */
3716 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3717 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003718 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 first_match_pos.col = last_match_pos.col = 0;
3720 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3721 last_match_pos.lnum = 0;
3722 type = 0;
3723 }
3724 else /* unloaded buffer, scan like dictionary */
3725 {
3726 found_all = TRUE;
3727 if (ins_buf->b_fname == NULL)
3728 continue;
3729 type = CTRL_X_DICTIONARY;
3730 dict = ins_buf->b_fname;
3731 dict_f = DICT_EXACT;
3732 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003733 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 ins_buf->b_fname == NULL
3735 ? buf_spname(ins_buf)
3736 : ins_buf->b_sfname == NULL
3737 ? (char *)ins_buf->b_fname
3738 : (char *)ins_buf->b_sfname);
3739 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3740 }
3741 else if (*e_cpt == NUL)
3742 break;
3743 else
3744 {
3745 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3746 type = -1;
3747 else if (*e_cpt == 'k' || *e_cpt == 's')
3748 {
3749 if (*e_cpt == 'k')
3750 type = CTRL_X_DICTIONARY;
3751 else
3752 type = CTRL_X_THESAURUS;
3753 if (*++e_cpt != ',' && *e_cpt != NUL)
3754 {
3755 dict = e_cpt;
3756 dict_f = DICT_FIRST;
3757 }
3758 }
3759#ifdef FEAT_FIND_ID
3760 else if (*e_cpt == 'i')
3761 type = CTRL_X_PATH_PATTERNS;
3762 else if (*e_cpt == 'd')
3763 type = CTRL_X_PATH_DEFINES;
3764#endif
3765 else if (*e_cpt == ']' || *e_cpt == 't')
3766 {
3767 type = CTRL_X_TAGS;
3768 sprintf((char*)IObuff, _("Scanning tags."));
3769 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3770 }
3771 else
3772 type = -1;
3773
3774 /* in any case e_cpt is advanced to the next entry */
3775 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3776
3777 found_all = TRUE;
3778 if (type == -1)
3779 continue;
3780 }
3781 }
3782
3783 switch (type)
3784 {
3785 case -1:
3786 break;
3787#ifdef FEAT_FIND_ID
3788 case CTRL_X_PATH_PATTERNS:
3789 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003790 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003791 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003793 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3795 (linenr_T)1, (linenr_T)MAXLNUM);
3796 break;
3797#endif
3798
3799 case CTRL_X_DICTIONARY:
3800 case CTRL_X_THESAURUS:
3801 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003802 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 : (type == CTRL_X_THESAURUS
3804 ? (*curbuf->b_p_tsr == NUL
3805 ? p_tsr
3806 : curbuf->b_p_tsr)
3807 : (*curbuf->b_p_dict == NUL
3808 ? p_dict
3809 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003810 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003811 dict != NULL ? dict_f
3812 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 dict = NULL;
3814 break;
3815
3816 case CTRL_X_TAGS:
3817 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3818 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003819 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820
3821 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003822 * of matches is found when compl_pattern is empty */
3823 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3825 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3826 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3827 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00003828 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830 p_ic = save_p_ic;
3831 break;
3832
3833 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003834 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3836 {
3837
3838 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003839 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003840 ins_compl_add_matches(num_matches, matches,
3841#ifdef CASE_INSENSITIVE_FILENAME
3842 TRUE
3843#else
3844 FALSE
3845#endif
3846 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
3848 break;
3849
3850 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003851 if (expand_cmdline(&compl_xp, compl_pattern,
3852 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003854 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 break;
3856
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003857#ifdef FEAT_COMPL_FUNC
3858 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003859 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003860 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003861 break;
3862#endif
3863
Bram Moolenaar488c6512005-08-11 20:09:58 +00003864 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003865#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003866 num_matches = expand_spelling(first_match_pos.lnum,
3867 first_match_pos.col, compl_pattern, &matches);
3868 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003869 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003870#endif
3871 break;
3872
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 default: /* normal ^P/^N and ^X^L */
3874 /*
3875 * If 'infercase' is set, don't use 'smartcase' here
3876 */
3877 save_p_scs = p_scs;
3878 if (ins_buf->b_p_inf)
3879 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003880
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 /* buffers other than curbuf are scanned from the beginning or the
3882 * end but never from the middle, thus setting nowrapscan in this
3883 * buffers is a good idea, on the other hand, we always set
3884 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3885 save_p_ws = p_ws;
3886 if (ins_buf != curbuf)
3887 p_ws = FALSE;
3888 else if (*e_cpt == '.')
3889 p_ws = TRUE;
3890 for (;;)
3891 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003892 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003894 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3895 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003897 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003899 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003901 found_new_match = searchit(NULL, ins_buf, pos,
3902 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003903 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003904 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003905 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003907 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003908 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 first_match_pos = *pos;
3910 last_match_pos = *pos;
3911 }
3912 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003913 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 found_new_match = FAIL;
3915 if (found_new_match == FAIL)
3916 {
3917 if (ins_buf == curbuf)
3918 found_all = TRUE;
3919 break;
3920 }
3921
3922 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003923 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 && ini->lnum == pos->lnum
3925 && ini->col == pos->col)
3926 continue;
3927 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3928 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3929 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003930 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
3932 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3933 continue;
3934 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3935 if (!p_paste)
3936 ptr = skipwhite(ptr);
3937 }
3938 len = (int)STRLEN(ptr);
3939 }
3940 else
3941 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003942 char_u *tmp_ptr = ptr;
3943
3944 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003946 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 /* Skip if already inside a word. */
3948 if (vim_iswordp(tmp_ptr))
3949 continue;
3950 /* Find start of next word. */
3951 tmp_ptr = find_word_start(tmp_ptr);
3952 }
3953 /* Find end of this word. */
3954 tmp_ptr = find_word_end(tmp_ptr);
3955 len = (int)(tmp_ptr - ptr);
3956
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003957 if ((compl_cont_status & CONT_ADDING)
3958 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 {
3960 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3961 {
3962 /* Try next line, if any. the new word will be
3963 * "join" as if the normal command "J" was used.
3964 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003965 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 * works -- Acevedo */
3967 STRNCPY(IObuff, ptr, len);
3968 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3969 tmp_ptr = ptr = skipwhite(ptr);
3970 /* Find start of next word. */
3971 tmp_ptr = find_word_start(tmp_ptr);
3972 /* Find end of next word. */
3973 tmp_ptr = find_word_end(tmp_ptr);
3974 if (tmp_ptr > ptr)
3975 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003976 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003978 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 IObuff[len++] = ' ';
3980 /* IObuf =~ "\k.* ", thus len >= 2 */
3981 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003982 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 || (vim_strchr(p_cpo, CPO_JOINSP)
3984 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003985 && (IObuff[len - 2] == '?'
3986 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 IObuff[len++] = ' ';
3988 }
3989 /* copy as much as posible of the new word */
3990 if (tmp_ptr - ptr >= IOSIZE - len)
3991 tmp_ptr = ptr + IOSIZE - len - 1;
3992 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3993 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003994 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 }
3996 IObuff[len] = NUL;
3997 ptr = IObuff;
3998 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003999 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 continue;
4001 }
4002 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004003 if (ins_compl_add_infercase(ptr, len, FALSE,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004004 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004005 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 {
4007 found_new_match = OK;
4008 break;
4009 }
4010 }
4011 p_scs = save_p_scs;
4012 p_ws = save_p_ws;
4013 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004014
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004015 /* check if compl_curr_match has changed, (e.g. other type of
4016 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004017 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 found_new_match = OK;
4019
4020 /* break the loop for specialized modes (use 'complete' just for the
4021 * generic ctrl_x_mode == 0) or when we've found a new match */
4022 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004023 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004024 {
4025 if (got_int)
4026 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004027 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004028 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004029 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004030
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004031 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4032 || compl_interrupted)
4033 break;
4034 compl_started = TRUE;
4035 }
4036 else
4037 {
4038 /* Mark a buffer scanned when it has been scanned completely */
4039 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4040 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004042 compl_started = FALSE;
4043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004045 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
4047 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4048 && *e_cpt == NUL) /* Got to end of 'complete' */
4049 found_new_match = FAIL;
4050
4051 i = -1; /* total of matches, unknown */
4052 if (found_new_match == FAIL
4053 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4054 i = ins_compl_make_cyclic();
4055
4056 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004057 * just been made cyclic then we have to move compl_curr_match to the next
4058 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004059 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4060 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004061 if (compl_curr_match == NULL)
4062 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 return i;
4064}
4065
4066/* Delete the old text being completed. */
4067 static void
4068ins_compl_delete()
4069{
4070 int i;
4071
4072 /*
4073 * In insert mode: Delete the typed part.
4074 * In replace mode: Put the old characters back, if any.
4075 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004076 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 backspace_until_column(i);
4078 changed_cline_bef_curs();
4079}
4080
4081/* Insert the new text being completed. */
4082 static void
4083ins_compl_insert()
4084{
Bram Moolenaar572cb562005-08-05 21:35:02 +00004085 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004086 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4087 compl_used_match = FALSE;
4088 else
4089 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090}
4091
4092/*
4093 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004094 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4095 * get more completions. If it is FALSE, then we just do nothing when there
4096 * are no more completions in a given direction. The latter case is used when
4097 * we are still in the middle of finding completions, to allow browsing
4098 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 * Return the total number of matches, or -1 if still unknown -- webb.
4100 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004101 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4102 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 *
4104 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004105 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4106 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 */
4108 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004109ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004111 int count; /* repeat completion this many times; should
4112 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004113 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114{
4115 int num_matches = -1;
4116 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004117 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004118 compl_T *found_compl = NULL;
4119 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004120 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004122 if (compl_leader != NULL
4123 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004125 /* Set "compl_shown_match" to the actually shown match, it may differ
4126 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004127 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004128 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004129 && compl_shown_match->cp_next != NULL
4130 && compl_shown_match->cp_next != compl_first_match)
4131 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004132
4133 /* If we didn't find it searching forward, and compl_shows_dir is
4134 * backward, find the last match. */
4135 if (compl_shows_dir == BACKWARD
4136 && !ins_compl_equal(compl_shown_match,
4137 compl_leader, (int)STRLEN(compl_leader))
4138 && (compl_shown_match->cp_next == NULL
4139 || compl_shown_match->cp_next == compl_first_match))
4140 {
4141 while (!ins_compl_equal(compl_shown_match,
4142 compl_leader, (int)STRLEN(compl_leader))
4143 && compl_shown_match->cp_prev != NULL
4144 && compl_shown_match->cp_prev != compl_first_match)
4145 compl_shown_match = compl_shown_match->cp_prev;
4146 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004147 }
4148
4149 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004150 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 /* Delete old text to be replaced */
4152 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004153
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004154 /* When finding the longest common text we stick at the original text,
4155 * don't let CTRL-N or CTRL-P move to the first match. */
4156 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4157
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004158 /* When restarting the search don't insert the first match either. */
4159 if (compl_restarting)
4160 {
4161 advance = FALSE;
4162 compl_restarting = FALSE;
4163 }
4164
Bram Moolenaare3226be2005-12-18 22:10:00 +00004165 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4166 * around. */
4167 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004169 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004171 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004172 found_end = (compl_first_match != NULL
4173 && (compl_shown_match->cp_next == compl_first_match
4174 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004175 }
4176 else if (compl_shows_dir == BACKWARD
4177 && compl_shown_match->cp_prev != NULL)
4178 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004179 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004180 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004181 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 }
4183 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004184 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004185 if (!allow_get_expansion)
4186 {
4187 if (advance)
4188 {
4189 if (compl_shows_dir == BACKWARD)
4190 compl_pending -= todo + 1;
4191 else
4192 compl_pending += todo + 1;
4193 }
4194 return -1;
4195 }
4196
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004197 if (advance)
4198 {
4199 if (compl_shows_dir == BACKWARD)
4200 --compl_pending;
4201 else
4202 ++compl_pending;
4203 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004204
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004205 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004206 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004207
4208 /* handle any pending completions */
4209 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004210 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004211 {
4212 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4213 {
4214 compl_shown_match = compl_shown_match->cp_next;
4215 --compl_pending;
4216 }
4217 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4218 {
4219 compl_shown_match = compl_shown_match->cp_prev;
4220 ++compl_pending;
4221 }
4222 else
4223 break;
4224 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004225 found_end = FALSE;
4226 }
4227 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4228 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004229 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004230 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004231 ++todo;
4232 else
4233 /* Remember a matching item. */
4234 found_compl = compl_shown_match;
4235
4236 /* Stop at the end of the list when we found a usable match. */
4237 if (found_end)
4238 {
4239 if (found_compl != NULL)
4240 {
4241 compl_shown_match = found_compl;
4242 break;
4243 }
4244 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004248 /* Insert the text of the new completion, or the compl_leader. */
4249 if (insert_match)
4250 {
4251 if (!compl_get_longest || compl_used_match)
4252 ins_compl_insert();
4253 else
4254 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4255 }
4256 else
4257 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
4259 if (!allow_get_expansion)
4260 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004261 /* may undisplay the popup menu first */
4262 ins_compl_upd_pum();
4263
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004264 /* redraw to show the user what was inserted */
4265 update_screen(0);
4266
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004267 /* display the updated popup menu */
4268 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004269#ifdef FEAT_GUI
4270 if (gui.in_use)
4271 {
4272 /* Show the cursor after the match, not after the redrawn text. */
4273 setcursor();
4274 out_flush();
4275 gui_update_cursor(FALSE, FALSE);
4276 }
4277#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004278
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 /* Delete old text to be replaced, since we're still searching and
4280 * don't want to match ourselves! */
4281 ins_compl_delete();
4282 }
4283
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004284 /* Enter will select a match when the match wasn't inserted and the popup
4285 * menu is visislbe. */
4286 compl_enter_selects = !insert_match && compl_match_array != NULL;
4287
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 /*
4289 * Show the file name for the match (if any)
4290 * Truncate the file name to avoid a wait for return.
4291 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004292 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 {
4294 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004295 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 if (i <= 0)
4297 i = 0;
4298 else
4299 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004300 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 msg(IObuff);
4302 redraw_cmdline = FALSE; /* don't overwrite! */
4303 }
4304
4305 return num_matches;
4306}
4307
4308/*
4309 * Call this while finding completions, to check whether the user has hit a key
4310 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004311 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004313 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 */
4315 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004316ins_compl_check_keys(frequency)
4317 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318{
4319 static int count = 0;
4320
4321 int c;
4322
4323 /* Don't check when reading keys from a script. That would break the test
4324 * scripts */
4325 if (using_script())
4326 return;
4327
4328 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004329 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 return;
4331 count = 0;
4332
Bram Moolenaara260a972006-06-23 19:36:29 +00004333 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4334 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 if (c != NUL)
4337 {
4338 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4339 {
4340 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004341 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004342 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4343 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004345 else
4346 {
4347 /* Need to get the character to have KeyTyped set. We'll put it
4348 * back with vungetc() below. */
4349 c = safe_vgetc();
4350
4351 /* Don't interrupt completion when the character wasn't typed,
4352 * e.g., when doing @q to replay keys. */
4353 if (c != Ctrl_R && KeyTyped)
4354 compl_interrupted = TRUE;
4355
4356 vungetc(c);
4357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004359 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004360 {
4361 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4362
4363 compl_pending = 0;
4364 (void)ins_compl_next(FALSE, todo, TRUE);
4365 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004366}
4367
4368/*
4369 * Decide the direction of Insert mode complete from the key typed.
4370 * Returns BACKWARD or FORWARD.
4371 */
4372 static int
4373ins_compl_key2dir(c)
4374 int c;
4375{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004376 if (c == Ctrl_P || c == Ctrl_L
4377 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4378 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004379 return BACKWARD;
4380 return FORWARD;
4381}
4382
4383/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004384 * Return TRUE for keys that are used for completion only when the popup menu
4385 * is visible.
4386 */
4387 static int
4388ins_compl_pum_key(c)
4389 int c;
4390{
4391 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004392 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4393 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004394}
4395
4396/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004397 * Decide the number of completions to move forward.
4398 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4399 */
4400 static int
4401ins_compl_key2count(c)
4402 int c;
4403{
4404 int h;
4405
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004406 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004407 {
4408 h = pum_get_height();
4409 if (h > 3)
4410 h -= 2; /* keep some context */
4411 return h;
4412 }
4413 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414}
4415
4416/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004417 * Return TRUE if completion with "c" should insert the match, FALSE if only
4418 * to change the currently selected completion.
4419 */
4420 static int
4421ins_compl_use_match(c)
4422 int c;
4423{
4424 switch (c)
4425 {
4426 case K_UP:
4427 case K_DOWN:
4428 case K_PAGEDOWN:
4429 case K_KPAGEDOWN:
4430 case K_S_DOWN:
4431 case K_PAGEUP:
4432 case K_KPAGEUP:
4433 case K_S_UP:
4434 return FALSE;
4435 }
4436 return TRUE;
4437}
4438
4439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 * Do Insert mode completion.
4441 * Called when character "c" was typed, which has a meaning for completion.
4442 * Returns OK if completion was done, FAIL if something failed (out of mem).
4443 */
4444 static int
4445ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004446 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004448 char_u *line;
4449 int startcol = 0; /* column where searched text starts */
4450 colnr_T curs_col; /* cursor column */
4451 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
Bram Moolenaare3226be2005-12-18 22:10:00 +00004453 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004454 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 {
4456 /* First time we hit ^N or ^P (in a row, I mean) */
4457
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 did_ai = FALSE;
4459#ifdef FEAT_SMARTINDENT
4460 did_si = FALSE;
4461 can_si = FALSE;
4462 can_si_back = FALSE;
4463#endif
4464 if (stop_arrow() == FAIL)
4465 return FAIL;
4466
4467 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004468 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004469 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004472 * "compl_startpos" to the cursor as a pattern to add a new word
4473 * instead of expand the one before the cursor, in word-wise if
4474 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 * is not in the same line as the cursor then fix it (the line has
4476 * been split because it was longer than 'tw'). if SOL is set then
4477 * skip the previous pattern, a word at the beginning of the line has
4478 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004479 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4480 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
4482 /*
4483 * it is a continued search
4484 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004485 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4487 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4488 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004489 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004491 /* line (probably) wrapped, set compl_startpos to the
4492 * first non_blank in the line, if it is not a wordchar
4493 * include it to get a better pattern, but then we don't
4494 * want the "\\<" prefix, check it bellow */
4495 compl_col = (colnr_T)(skipwhite(line) - line);
4496 compl_startpos.col = compl_col;
4497 compl_startpos.lnum = curwin->w_cursor.lnum;
4498 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500 else
4501 {
4502 /* S_IPOS was set when we inserted a word that was at the
4503 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004504 * mode but first we need to redefine compl_startpos */
4505 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004507 compl_cont_status |= CONT_SOL;
4508 compl_startpos.col = (colnr_T)(skipwhite(
4509 line + compl_length
4510 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004512 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004514 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004515 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 * have enough space? just being paranoic */
4517#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004518 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004520 compl_cont_status &= ~CONT_SOL;
4521 compl_length = (IOSIZE - MIN_SPACE);
4522 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004524 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4525 if (compl_length < 1)
4526 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004529 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004531 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 }
4533 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004534 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004536 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004538 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004540 compl_cont_status = 0;
4541 compl_cont_status |= CONT_N_ADDS;
4542 compl_startpos = curwin->w_cursor;
4543 startcol = (int)curs_col;
4544 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 }
4546
4547 /* Work out completion pattern and original text -- webb */
4548 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4549 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004550 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4552 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004553 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004555 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 compl_col += ++startcol;
4558 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 }
4560 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004561 compl_pattern = str_foldcase(line + compl_col,
4562 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004564 compl_pattern = vim_strnsave(line + compl_col,
4565 compl_length);
4566 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 return FAIL;
4568 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004569 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 {
4571 char_u *prefix = (char_u *)"\\<";
4572
4573 /* we need 3 extra chars, 1 for the NUL and
4574 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004575 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4576 compl_length) + 3);
4577 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004579 if (!vim_iswordp(line + compl_col)
4580 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 && (
4582#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004583 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004585 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586#endif
4587 )))
4588 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004589 STRCPY((char *)compl_pattern, prefix);
4590 (void)quote_meta(compl_pattern + STRLEN(prefix),
4591 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004593 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004595 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004597 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598#endif
4599 )
4600 {
4601 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004602 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4603 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004605 compl_col += curs_col;
4606 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608 else
4609 {
4610#ifdef FEAT_MBYTE
4611 /* Search the point of change class of multibyte character
4612 * or not a word single byte character backward. */
4613 if (has_mbyte)
4614 {
4615 int base_class;
4616 int head_off;
4617
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004618 startcol -= (*mb_head_off)(line, line + startcol);
4619 base_class = mb_get_class(line + startcol);
4620 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004622 head_off = (*mb_head_off)(line, line + startcol);
4623 if (base_class != mb_get_class(line + startcol
4624 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004626 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 }
4628 }
4629 else
4630#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004631 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004633 compl_col += ++startcol;
4634 compl_length = (int)curs_col - startcol;
4635 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 {
4637 /* Only match word with at least two chars -- webb
4638 * there's no need to call quote_meta,
4639 * alloc(7) is enough -- Acevedo
4640 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004641 compl_pattern = alloc(7);
4642 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004644 STRCPY((char *)compl_pattern, "\\<");
4645 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4646 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 }
4648 else
4649 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004650 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4651 compl_length) + 3);
4652 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004654 STRCPY((char *)compl_pattern, "\\<");
4655 (void)quote_meta(compl_pattern + 2, line + compl_col,
4656 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 }
4658 }
4659 }
4660 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4661 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004662 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004663 compl_length = (int)curs_col - (int)compl_col;
4664 if (compl_length < 0) /* cursor in indent: empty pattern */
4665 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004667 compl_pattern = str_foldcase(line + compl_col, compl_length,
4668 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004670 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4671 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 return FAIL;
4673 }
4674 else if (ctrl_x_mode == CTRL_X_FILES)
4675 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004676 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004678 compl_col += ++startcol;
4679 compl_length = (int)curs_col - startcol;
4680 compl_pattern = addstar(line + compl_col, compl_length,
4681 EXPAND_FILES);
4682 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 return FAIL;
4684 }
4685 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4686 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004687 compl_pattern = vim_strnsave(line, curs_col);
4688 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004690 set_cmd_context(&compl_xp, compl_pattern,
4691 (int)STRLEN(compl_pattern), curs_col);
4692 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4693 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004694 /* No completion possible, use an empty pattern to get a
4695 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004696 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004697 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004698 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4699 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004701 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004702 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004703#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004704 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004705 * Call user defined function 'completefunc' with "a:findstart"
4706 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004707 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004708 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004709 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004710 char_u *funcname;
4711 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004712
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004713 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004714 * string */
4715 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4716 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4717 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004718 {
4719 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4720 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004721 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004722 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004723
4724 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004725 args[1] = NULL;
4726 pos = curwin->w_cursor;
4727 col = call_func_retnr(funcname, 2, args, FALSE);
4728 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004729
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004730 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004731 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004732 compl_col = col;
4733 if ((colnr_T)compl_col > curs_col)
4734 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004735
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004736 /* Setup variables for completion. Need to obtain "line" again,
4737 * it may have become invalid. */
4738 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004739 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004740 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4741 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004742#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004743 return FAIL;
4744 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004745 else if (ctrl_x_mode == CTRL_X_SPELL)
4746 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004747#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004748 if (spell_bad_len > 0)
4749 compl_col = curs_col - spell_bad_len;
4750 else
4751 compl_col = spell_word_start(startcol);
4752 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004753 {
4754 compl_length = 0;
4755 compl_col = curs_col;
4756 }
4757 else
4758 {
4759 spell_expand_check_cap(compl_col);
4760 compl_length = (int)curs_col - compl_col;
4761 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004762 /* Need to obtain "line" again, it may have become invalid. */
4763 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004764 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4765 if (compl_pattern == NULL)
4766#endif
4767 return FAIL;
4768 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004769 else
4770 {
4771 EMSG2(_(e_intern2), "ins_complete()");
4772 return FAIL;
4773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004775 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 {
4777 edit_submode_pre = (char_u *)_(" Adding");
4778 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4779 {
4780 /* Insert a new line, keep indentation but ignore 'comments' */
4781#ifdef FEAT_COMMENTS
4782 char_u *old = curbuf->b_p_com;
4783
4784 curbuf->b_p_com = (char_u *)"";
4785#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004786 compl_startpos.lnum = curwin->w_cursor.lnum;
4787 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 ins_eol('\r');
4789#ifdef FEAT_COMMENTS
4790 curbuf->b_p_com = old;
4791#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004792 compl_length = 0;
4793 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
4795 }
4796 else
4797 {
4798 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004799 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 }
4801
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004802 if (compl_cont_status & CONT_LOCAL)
4803 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 else
4805 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4806
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004807 /* Always add completion for the original text. */
4808 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004809 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4810 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004811 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004813 vim_free(compl_pattern);
4814 compl_pattern = NULL;
4815 vim_free(compl_orig_text);
4816 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 return FAIL;
4818 }
4819
4820 /* showmode might reset the internal line pointers, so it must
4821 * be called before line = ml_get(), or when this address is no
4822 * longer needed. -- Acevedo.
4823 */
4824 edit_submode_extra = (char_u *)_("-- Searching...");
4825 edit_submode_highl = HLF_COUNT;
4826 showmode();
4827 edit_submode_extra = NULL;
4828 out_flush();
4829 }
4830
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004831 compl_shown_match = compl_curr_match;
4832 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833
4834 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004835 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004837 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004839 /* may undisplay the popup menu */
4840 ins_compl_upd_pum();
4841
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004842 if (n > 1) /* all matches have been found */
4843 compl_matches = n;
4844 compl_curr_match = compl_shown_match;
4845 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846
Bram Moolenaard68071d2006-05-02 22:08:30 +00004847 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4848 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 if (got_int && !global_busy)
4850 {
4851 (void)vgetc();
4852 got_int = FALSE;
4853 }
4854
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004855 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004856 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004858 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4859 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4861 edit_submode_highl = HLF_E;
4862 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4863 * because we couldn't expand anything at first place, but if we used
4864 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4865 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004866 if ( compl_length > 1
4867 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 || (ctrl_x_mode != 0
4869 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4870 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004871 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
4873
Bram Moolenaar572cb562005-08-05 21:35:02 +00004874 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004875 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004877 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878
4879 if (edit_submode_extra == NULL)
4880 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004881 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
4883 edit_submode_extra = (char_u *)_("Back at original");
4884 edit_submode_highl = HLF_W;
4885 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004886 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 {
4888 edit_submode_extra = (char_u *)_("Word from other line");
4889 edit_submode_highl = HLF_COUNT;
4890 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004891 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 {
4893 edit_submode_extra = (char_u *)_("The only match");
4894 edit_submode_highl = HLF_COUNT;
4895 }
4896 else
4897 {
4898 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004899 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004901 int number = 0;
4902 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004904 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
4906 /* search backwards for the first valid (!= -1) number.
4907 * This should normally succeed already at the first loop
4908 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004909 for (match = compl_curr_match->cp_prev; match != NULL
4910 && match != compl_first_match;
4911 match = match->cp_prev)
4912 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004914 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 break;
4916 }
4917 if (match != NULL)
4918 /* go up and assign all numbers which are not assigned
4919 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004920 for (match = match->cp_next;
4921 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004922 match = match->cp_next)
4923 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
4925 else /* BACKWARD */
4926 {
4927 /* search forwards (upwards) for the first valid (!= -1)
4928 * number. This should normally succeed already at the
4929 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004930 for (match = compl_curr_match->cp_next; match != NULL
4931 && match != compl_first_match;
4932 match = match->cp_next)
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 down and assign all numbers which are not
4940 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004941 for (match = match->cp_prev; match
4942 && match->cp_number == -1;
4943 match = match->cp_prev)
4944 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946 }
4947
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004948 /* The match should always have a sequence number now, this is
4949 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004950 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 {
4952 /* Space for 10 text chars. + 2x10-digit no.s */
4953 static char_u match_ref[31];
4954
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004955 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004957 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004959 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004960 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004961 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 edit_submode_extra = match_ref;
4963 edit_submode_highl = HLF_R;
4964 if (dollar_vcol)
4965 curs_columns(FALSE);
4966 }
4967 }
4968 }
4969
4970 /* Show a message about what (completion) mode we're in. */
4971 showmode();
4972 if (edit_submode_extra != NULL)
4973 {
4974 if (!p_smd)
4975 msg_attr(edit_submode_extra,
4976 edit_submode_highl < HLF_COUNT
4977 ? hl_attr(edit_submode_highl) : 0);
4978 }
4979 else
4980 msg_clr_cmdline(); /* necessary for "noshowmode" */
4981
Bram Moolenaard68071d2006-05-02 22:08:30 +00004982 /* Show the popup menu, unless we got interrupted. */
4983 if (!compl_interrupted)
4984 {
4985 /* RedrawingDisabled may be set when invoked through complete(). */
4986 n = RedrawingDisabled;
4987 RedrawingDisabled = 0;
4988 ins_compl_show_pum();
4989 setcursor();
4990 RedrawingDisabled = n;
4991 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004992 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004993 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004994
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 return OK;
4996}
4997
4998/*
4999 * Looks in the first "len" chars. of "src" for search-metachars.
5000 * If dest is not NULL the chars. are copied there quoting (with
5001 * a backslash) the metachars, and dest would be NUL terminated.
5002 * Returns the length (needed) of dest
5003 */
5004 static int
5005quote_meta(dest, src, len)
5006 char_u *dest;
5007 char_u *src;
5008 int len;
5009{
5010 int m;
5011
5012 for (m = len; --len >= 0; src++)
5013 {
5014 switch (*src)
5015 {
5016 case '.':
5017 case '*':
5018 case '[':
5019 if (ctrl_x_mode == CTRL_X_DICTIONARY
5020 || ctrl_x_mode == CTRL_X_THESAURUS)
5021 break;
5022 case '~':
5023 if (!p_magic) /* quote these only if magic is set */
5024 break;
5025 case '\\':
5026 if (ctrl_x_mode == CTRL_X_DICTIONARY
5027 || ctrl_x_mode == CTRL_X_THESAURUS)
5028 break;
5029 case '^': /* currently it's not needed. */
5030 case '$':
5031 m++;
5032 if (dest != NULL)
5033 *dest++ = '\\';
5034 break;
5035 }
5036 if (dest != NULL)
5037 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005038# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 /* Copy remaining bytes of a multibyte character. */
5040 if (has_mbyte)
5041 {
5042 int i, mb_len;
5043
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005044 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 if (mb_len > 0 && len >= mb_len)
5046 for (i = 0; i < mb_len; ++i)
5047 {
5048 --len;
5049 ++src;
5050 if (dest != NULL)
5051 *dest++ = *src;
5052 }
5053 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005054# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
5056 if (dest != NULL)
5057 *dest = NUL;
5058
5059 return m;
5060}
5061#endif /* FEAT_INS_EXPAND */
5062
5063/*
5064 * Next character is interpreted literally.
5065 * A one, two or three digit decimal number is interpreted as its byte value.
5066 * If one or two digits are entered, the next character is given to vungetc().
5067 * For Unicode a character > 255 may be returned.
5068 */
5069 int
5070get_literal()
5071{
5072 int cc;
5073 int nc;
5074 int i;
5075 int hex = FALSE;
5076 int octal = FALSE;
5077#ifdef FEAT_MBYTE
5078 int unicode = 0;
5079#endif
5080
5081 if (got_int)
5082 return Ctrl_C;
5083
5084#ifdef FEAT_GUI
5085 /*
5086 * In GUI there is no point inserting the internal code for a special key.
5087 * It is more useful to insert the string "<KEY>" instead. This would
5088 * probably be useful in a text window too, but it would not be
5089 * vi-compatible (maybe there should be an option for it?) -- webb
5090 */
5091 if (gui.in_use)
5092 ++allow_keys;
5093#endif
5094#ifdef USE_ON_FLY_SCROLL
5095 dont_scroll = TRUE; /* disallow scrolling here */
5096#endif
5097 ++no_mapping; /* don't map the next key hits */
5098 cc = 0;
5099 i = 0;
5100 for (;;)
5101 {
5102 do
5103 nc = safe_vgetc();
5104 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
5105 || nc == K_HOR_SCROLLBAR);
5106#ifdef FEAT_CMDL_INFO
5107 if (!(State & CMDLINE)
5108# ifdef FEAT_MBYTE
5109 && MB_BYTE2LEN_CHECK(nc) == 1
5110# endif
5111 )
5112 add_to_showcmd(nc);
5113#endif
5114 if (nc == 'x' || nc == 'X')
5115 hex = TRUE;
5116 else if (nc == 'o' || nc == 'O')
5117 octal = TRUE;
5118#ifdef FEAT_MBYTE
5119 else if (nc == 'u' || nc == 'U')
5120 unicode = nc;
5121#endif
5122 else
5123 {
5124 if (hex
5125#ifdef FEAT_MBYTE
5126 || unicode != 0
5127#endif
5128 )
5129 {
5130 if (!vim_isxdigit(nc))
5131 break;
5132 cc = cc * 16 + hex2nr(nc);
5133 }
5134 else if (octal)
5135 {
5136 if (nc < '0' || nc > '7')
5137 break;
5138 cc = cc * 8 + nc - '0';
5139 }
5140 else
5141 {
5142 if (!VIM_ISDIGIT(nc))
5143 break;
5144 cc = cc * 10 + nc - '0';
5145 }
5146
5147 ++i;
5148 }
5149
5150 if (cc > 255
5151#ifdef FEAT_MBYTE
5152 && unicode == 0
5153#endif
5154 )
5155 cc = 255; /* limit range to 0-255 */
5156 nc = 0;
5157
5158 if (hex) /* hex: up to two chars */
5159 {
5160 if (i >= 2)
5161 break;
5162 }
5163#ifdef FEAT_MBYTE
5164 else if (unicode) /* Unicode: up to four or eight chars */
5165 {
5166 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5167 break;
5168 }
5169#endif
5170 else if (i >= 3) /* decimal or octal: up to three chars */
5171 break;
5172 }
5173 if (i == 0) /* no number entered */
5174 {
5175 if (nc == K_ZERO) /* NUL is stored as NL */
5176 {
5177 cc = '\n';
5178 nc = 0;
5179 }
5180 else
5181 {
5182 cc = nc;
5183 nc = 0;
5184 }
5185 }
5186
5187 if (cc == 0) /* NUL is stored as NL */
5188 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005189#ifdef FEAT_MBYTE
5190 if (enc_dbcs && (cc & 0xff) == 0)
5191 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5192 second byte will cause trouble! */
5193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194
5195 --no_mapping;
5196#ifdef FEAT_GUI
5197 if (gui.in_use)
5198 --allow_keys;
5199#endif
5200 if (nc)
5201 vungetc(nc);
5202 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5203 return cc;
5204}
5205
5206/*
5207 * Insert character, taking care of special keys and mod_mask
5208 */
5209 static void
5210insert_special(c, allow_modmask, ctrlv)
5211 int c;
5212 int allow_modmask;
5213 int ctrlv; /* c was typed after CTRL-V */
5214{
5215 char_u *p;
5216 int len;
5217
5218 /*
5219 * Special function key, translate into "<Key>". Up to the last '>' is
5220 * inserted with ins_str(), so as not to replace characters in replace
5221 * mode.
5222 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5223 * unless 'allow_modmask' is TRUE.
5224 */
5225#ifdef MACOS
5226 /* Command-key never produces a normal key */
5227 if (mod_mask & MOD_MASK_CMD)
5228 allow_modmask = TRUE;
5229#endif
5230 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5231 {
5232 p = get_special_key_name(c, mod_mask);
5233 len = (int)STRLEN(p);
5234 c = p[len - 1];
5235 if (len > 2)
5236 {
5237 if (stop_arrow() == FAIL)
5238 return;
5239 p[len - 1] = NUL;
5240 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005241 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 ctrlv = FALSE;
5243 }
5244 }
5245 if (stop_arrow() == OK)
5246 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5247}
5248
5249/*
5250 * Special characters in this context are those that need processing other
5251 * than the simple insertion that can be performed here. This includes ESC
5252 * which terminates the insert, and CR/NL which need special processing to
5253 * open up a new line. This routine tries to optimize insertions performed by
5254 * the "redo", "undo" or "put" commands, so it needs to know when it should
5255 * stop and defer processing to the "normal" mechanism.
5256 * '0' and '^' are special, because they can be followed by CTRL-D.
5257 */
5258#ifdef EBCDIC
5259# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5260#else
5261# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5262#endif
5263
5264#ifdef FEAT_MBYTE
5265# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5266#else
5267# define WHITECHAR(cc) vim_iswhite(cc)
5268#endif
5269
5270 void
5271insertchar(c, flags, second_indent)
5272 int c; /* character to insert or NUL */
5273 int flags; /* INSCHAR_FORMAT, etc. */
5274 int second_indent; /* indent for second line if >= 0 */
5275{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 int textwidth;
5277#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281
5282 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5283 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284
5285 /*
5286 * Try to break the line in two or more pieces when:
5287 * - Always do this if we have been called to do formatting only.
5288 * - Always do this when 'formatoptions' has the 'a' flag and the line
5289 * ends in white space.
5290 * - Otherwise:
5291 * - Don't do this if inserting a blank
5292 * - Don't do this if an existing character is being replaced, unless
5293 * we're in VREPLACE mode.
5294 * - Do this if the cursor is not on the line where insert started
5295 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5296 * before the insert.
5297 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5298 * before 'textwidth'
5299 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005300 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 && ((flags & INSCHAR_FORMAT)
5302 || (!vim_iswhite(c)
5303 && !((State & REPLACE_FLAG)
5304#ifdef FEAT_VREPLACE
5305 && !(State & VREPLACE_FLAG)
5306#endif
5307 && *ml_get_cursor() != NUL)
5308 && (curwin->w_cursor.lnum != Insstart.lnum
5309 || ((!has_format_option(FO_INS_LONG)
5310 || Insstart_textlen <= (colnr_T)textwidth)
5311 && (!fo_ins_blank
5312 || Insstart_blank_vcol <= (colnr_T)textwidth
5313 ))))))
5314 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005315 /* Format with 'formatexpr' when it's set. Use internal formatting
5316 * when 'formatexpr' isn't set or it returns non-zero. */
5317#if defined(FEAT_EVAL)
5318 if (*curbuf->b_p_fex == NUL
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005319 || fex_format(curwin->w_cursor.lnum, 1L, c) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005321 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005323
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 if (c == NUL) /* only formatting was wanted */
5325 return;
5326
5327#ifdef FEAT_COMMENTS
5328 /* Check whether this character should end a comment. */
5329 if (did_ai && (int)c == end_comment_pending)
5330 {
5331 char_u *line;
5332 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5333 int middle_len, end_len;
5334 int i;
5335
5336 /*
5337 * Need to remove existing (middle) comment leader and insert end
5338 * comment leader. First, check what comment leader we can find.
5339 */
5340 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5341 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5342 {
5343 /* Skip middle-comment string */
5344 while (*p && p[-1] != ':') /* find end of middle flags */
5345 ++p;
5346 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5347 /* Don't count trailing white space for middle_len */
5348 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5349 --middle_len;
5350
5351 /* Find the end-comment string */
5352 while (*p && p[-1] != ':') /* find end of end flags */
5353 ++p;
5354 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5355
5356 /* Skip white space before the cursor */
5357 i = curwin->w_cursor.col;
5358 while (--i >= 0 && vim_iswhite(line[i]))
5359 ;
5360 i++;
5361
5362 /* Skip to before the middle leader */
5363 i -= middle_len;
5364
5365 /* Check some expected things before we go on */
5366 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5367 {
5368 /* Backspace over all the stuff we want to replace */
5369 backspace_until_column(i);
5370
5371 /*
5372 * Insert the end-comment string, except for the last
5373 * character, which will get inserted as normal later.
5374 */
5375 ins_bytes_len(lead_end, end_len - 1);
5376 }
5377 }
5378 }
5379 end_comment_pending = NUL;
5380#endif
5381
5382 did_ai = FALSE;
5383#ifdef FEAT_SMARTINDENT
5384 did_si = FALSE;
5385 can_si = FALSE;
5386 can_si_back = FALSE;
5387#endif
5388
5389 /*
5390 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5391 * This speeds up normal text input considerably.
5392 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5393 * need to re-indent at a ':', or any other character (but not what
5394 * 'paste' is set)..
5395 */
5396#ifdef USE_ON_FLY_SCROLL
5397 dont_scroll = FALSE; /* allow scrolling here */
5398#endif
5399
5400 if ( !ISSPECIAL(c)
5401#ifdef FEAT_MBYTE
5402 && (!has_mbyte || (*mb_char2len)(c) == 1)
5403#endif
5404 && vpeekc() != NUL
5405 && !(State & REPLACE_FLAG)
5406#ifdef FEAT_CINDENT
5407 && !cindent_on()
5408#endif
5409#ifdef FEAT_RIGHTLEFT
5410 && !p_ri
5411#endif
5412 )
5413 {
5414#define INPUT_BUFLEN 100
5415 char_u buf[INPUT_BUFLEN + 1];
5416 int i;
5417 colnr_T virtcol = 0;
5418
5419 buf[0] = c;
5420 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005421 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 virtcol = get_nolist_virtcol();
5423 /*
5424 * Stop the string when:
5425 * - no more chars available
5426 * - finding a special character (command key)
5427 * - buffer is full
5428 * - running into the 'textwidth' boundary
5429 * - need to check for abbreviation: A non-word char after a word-char
5430 */
5431 while ( (c = vpeekc()) != NUL
5432 && !ISSPECIAL(c)
5433#ifdef FEAT_MBYTE
5434 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5435#endif
5436 && i < INPUT_BUFLEN
5437 && (textwidth == 0
5438 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5439 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5440 {
5441#ifdef FEAT_RIGHTLEFT
5442 c = vgetc();
5443 if (p_hkmap && KeyTyped)
5444 c = hkmap(c); /* Hebrew mode mapping */
5445# ifdef FEAT_FKMAP
5446 if (p_fkmap && KeyTyped)
5447 c = fkmap(c); /* Farsi mode mapping */
5448# endif
5449 buf[i++] = c;
5450#else
5451 buf[i++] = vgetc();
5452#endif
5453 }
5454
5455#ifdef FEAT_DIGRAPHS
5456 do_digraph(-1); /* clear digraphs */
5457 do_digraph(buf[i-1]); /* may be the start of a digraph */
5458#endif
5459 buf[i] = NUL;
5460 ins_str(buf);
5461 if (flags & INSCHAR_CTRLV)
5462 {
5463 redo_literal(*buf);
5464 i = 1;
5465 }
5466 else
5467 i = 0;
5468 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005469 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 }
5471 else
5472 {
5473#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005474 int cc;
5475
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5477 {
5478 char_u buf[MB_MAXBYTES + 1];
5479
5480 (*mb_char2bytes)(c, buf);
5481 buf[cc] = NUL;
5482 ins_char_bytes(buf, cc);
5483 AppendCharToRedobuff(c);
5484 }
5485 else
5486#endif
5487 {
5488 ins_char(c);
5489 if (flags & INSCHAR_CTRLV)
5490 redo_literal(c);
5491 else
5492 AppendCharToRedobuff(c);
5493 }
5494 }
5495}
5496
5497/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005498 * Format text at the current insert position.
5499 */
5500 static void
5501internal_format(textwidth, second_indent, flags, format_only)
5502 int textwidth;
5503 int second_indent;
5504 int flags;
5505 int format_only;
5506{
5507 int cc;
5508 int save_char = NUL;
5509 int haveto_redraw = FALSE;
5510 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5511#ifdef FEAT_MBYTE
5512 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5513#endif
5514 int fo_white_par = has_format_option(FO_WHITE_PAR);
5515 int first_line = TRUE;
5516#ifdef FEAT_COMMENTS
5517 colnr_T leader_len;
5518 int no_leader = FALSE;
5519 int do_comments = (flags & INSCHAR_DO_COM);
5520#endif
5521
5522 /*
5523 * When 'ai' is off we don't want a space under the cursor to be
5524 * deleted. Replace it with an 'x' temporarily.
5525 */
5526 if (!curbuf->b_p_ai)
5527 {
5528 cc = gchar_cursor();
5529 if (vim_iswhite(cc))
5530 {
5531 save_char = cc;
5532 pchar_cursor('x');
5533 }
5534 }
5535
5536 /*
5537 * Repeat breaking lines, until the current line is not too long.
5538 */
5539 while (!got_int)
5540 {
5541 int startcol; /* Cursor column at entry */
5542 int wantcol; /* column at textwidth border */
5543 int foundcol; /* column for start of spaces */
5544 int end_foundcol = 0; /* column for start of word */
5545 colnr_T len;
5546 colnr_T virtcol;
5547#ifdef FEAT_VREPLACE
5548 int orig_col = 0;
5549 char_u *saved_text = NULL;
5550#endif
5551 colnr_T col;
5552
5553 virtcol = get_nolist_virtcol();
5554 if (virtcol < (colnr_T)textwidth)
5555 break;
5556
5557#ifdef FEAT_COMMENTS
5558 if (no_leader)
5559 do_comments = FALSE;
5560 else if (!(flags & INSCHAR_FORMAT)
5561 && has_format_option(FO_WRAP_COMS))
5562 do_comments = TRUE;
5563
5564 /* Don't break until after the comment leader */
5565 if (do_comments)
5566 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5567 else
5568 leader_len = 0;
5569
5570 /* If the line doesn't start with a comment leader, then don't
5571 * start one in a following broken line. Avoids that a %word
5572 * moved to the start of the next line causes all following lines
5573 * to start with %. */
5574 if (leader_len == 0)
5575 no_leader = TRUE;
5576#endif
5577 if (!(flags & INSCHAR_FORMAT)
5578#ifdef FEAT_COMMENTS
5579 && leader_len == 0
5580#endif
5581 && !has_format_option(FO_WRAP))
5582
5583 {
5584 textwidth = 0;
5585 break;
5586 }
5587 if ((startcol = curwin->w_cursor.col) == 0)
5588 break;
5589
5590 /* find column of textwidth border */
5591 coladvance((colnr_T)textwidth);
5592 wantcol = curwin->w_cursor.col;
5593
5594 curwin->w_cursor.col = startcol - 1;
5595#ifdef FEAT_MBYTE
5596 /* Correct cursor for multi-byte character. */
5597 if (has_mbyte)
5598 mb_adjust_cursor();
5599#endif
5600 foundcol = 0;
5601
5602 /*
5603 * Find position to break at.
5604 * Stop at first entered white when 'formatoptions' has 'v'
5605 */
5606 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5607 || curwin->w_cursor.lnum != Insstart.lnum
5608 || curwin->w_cursor.col >= Insstart.col)
5609 {
5610 cc = gchar_cursor();
5611 if (WHITECHAR(cc))
5612 {
5613 /* remember position of blank just before text */
5614 end_foundcol = curwin->w_cursor.col;
5615
5616 /* find start of sequence of blanks */
5617 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5618 {
5619 dec_cursor();
5620 cc = gchar_cursor();
5621 }
5622 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5623 break; /* only spaces in front of text */
5624#ifdef FEAT_COMMENTS
5625 /* Don't break until after the comment leader */
5626 if (curwin->w_cursor.col < leader_len)
5627 break;
5628#endif
5629 if (has_format_option(FO_ONE_LETTER))
5630 {
5631 /* do not break after one-letter words */
5632 if (curwin->w_cursor.col == 0)
5633 break; /* one-letter word at begin */
5634
5635 col = curwin->w_cursor.col;
5636 dec_cursor();
5637 cc = gchar_cursor();
5638
5639 if (WHITECHAR(cc))
5640 continue; /* one-letter, continue */
5641 curwin->w_cursor.col = col;
5642 }
5643#ifdef FEAT_MBYTE
5644 if (has_mbyte)
5645 foundcol = curwin->w_cursor.col
5646 + (*mb_ptr2len)(ml_get_cursor());
5647 else
5648#endif
5649 foundcol = curwin->w_cursor.col + 1;
5650 if (curwin->w_cursor.col < (colnr_T)wantcol)
5651 break;
5652 }
5653#ifdef FEAT_MBYTE
5654 else if (cc >= 0x100 && fo_multibyte
5655 && curwin->w_cursor.col <= (colnr_T)wantcol)
5656 {
5657 /* Break after or before a multi-byte character. */
5658 foundcol = curwin->w_cursor.col;
5659 if (curwin->w_cursor.col < (colnr_T)wantcol)
5660 foundcol += (*mb_char2len)(cc);
5661 end_foundcol = foundcol;
5662 break;
5663 }
5664#endif
5665 if (curwin->w_cursor.col == 0)
5666 break;
5667 dec_cursor();
5668 }
5669
5670 if (foundcol == 0) /* no spaces, cannot break line */
5671 {
5672 curwin->w_cursor.col = startcol;
5673 break;
5674 }
5675
5676 /* Going to break the line, remove any "$" now. */
5677 undisplay_dollar();
5678
5679 /*
5680 * Offset between cursor position and line break is used by replace
5681 * stack functions. VREPLACE does not use this, and backspaces
5682 * over the text instead.
5683 */
5684#ifdef FEAT_VREPLACE
5685 if (State & VREPLACE_FLAG)
5686 orig_col = startcol; /* Will start backspacing from here */
5687 else
5688#endif
5689 replace_offset = startcol - end_foundcol - 1;
5690
5691 /*
5692 * adjust startcol for spaces that will be deleted and
5693 * characters that will remain on top line
5694 */
5695 curwin->w_cursor.col = foundcol;
5696 while (cc = gchar_cursor(), WHITECHAR(cc))
5697 inc_cursor();
5698 startcol -= curwin->w_cursor.col;
5699 if (startcol < 0)
5700 startcol = 0;
5701
5702#ifdef FEAT_VREPLACE
5703 if (State & VREPLACE_FLAG)
5704 {
5705 /*
5706 * In VREPLACE mode, we will backspace over the text to be
5707 * wrapped, so save a copy now to put on the next line.
5708 */
5709 saved_text = vim_strsave(ml_get_cursor());
5710 curwin->w_cursor.col = orig_col;
5711 if (saved_text == NULL)
5712 break; /* Can't do it, out of memory */
5713 saved_text[startcol] = NUL;
5714
5715 /* Backspace over characters that will move to the next line */
5716 if (!fo_white_par)
5717 backspace_until_column(foundcol);
5718 }
5719 else
5720#endif
5721 {
5722 /* put cursor after pos. to break line */
5723 if (!fo_white_par)
5724 curwin->w_cursor.col = foundcol;
5725 }
5726
5727 /*
5728 * Split the line just before the margin.
5729 * Only insert/delete lines, but don't really redraw the window.
5730 */
5731 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5732 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5733#ifdef FEAT_COMMENTS
5734 + (do_comments ? OPENLINE_DO_COM : 0)
5735#endif
5736 , old_indent);
5737 old_indent = 0;
5738
5739 replace_offset = 0;
5740 if (first_line)
5741 {
5742 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5743 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5744 if (second_indent >= 0)
5745 {
5746#ifdef FEAT_VREPLACE
5747 if (State & VREPLACE_FLAG)
5748 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5749 else
5750#endif
5751 (void)set_indent(second_indent, SIN_CHANGED);
5752 }
5753 first_line = FALSE;
5754 }
5755
5756#ifdef FEAT_VREPLACE
5757 if (State & VREPLACE_FLAG)
5758 {
5759 /*
5760 * In VREPLACE mode we have backspaced over the text to be
5761 * moved, now we re-insert it into the new line.
5762 */
5763 ins_bytes(saved_text);
5764 vim_free(saved_text);
5765 }
5766 else
5767#endif
5768 {
5769 /*
5770 * Check if cursor is not past the NUL off the line, cindent
5771 * may have added or removed indent.
5772 */
5773 curwin->w_cursor.col += startcol;
5774 len = (colnr_T)STRLEN(ml_get_curline());
5775 if (curwin->w_cursor.col > len)
5776 curwin->w_cursor.col = len;
5777 }
5778
5779 haveto_redraw = TRUE;
5780#ifdef FEAT_CINDENT
5781 can_cindent = TRUE;
5782#endif
5783 /* moved the cursor, don't autoindent or cindent now */
5784 did_ai = FALSE;
5785#ifdef FEAT_SMARTINDENT
5786 did_si = FALSE;
5787 can_si = FALSE;
5788 can_si_back = FALSE;
5789#endif
5790 line_breakcheck();
5791 }
5792
5793 if (save_char != NUL) /* put back space after cursor */
5794 pchar_cursor(save_char);
5795
5796 if (!format_only && haveto_redraw)
5797 {
5798 update_topline();
5799 redraw_curbuf_later(VALID);
5800 }
5801}
5802
5803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 * Called after inserting or deleting text: When 'formatoptions' includes the
5805 * 'a' flag format from the current line until the end of the paragraph.
5806 * Keep the cursor at the same position relative to the text.
5807 * The caller must have saved the cursor line for undo, following ones will be
5808 * saved here.
5809 */
5810 void
5811auto_format(trailblank, prev_line)
5812 int trailblank; /* when TRUE also format with trailing blank */
5813 int prev_line; /* may start in previous line */
5814{
5815 pos_T pos;
5816 colnr_T len;
5817 char_u *old;
5818 char_u *new, *pnew;
5819 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005820 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821
5822 if (!has_format_option(FO_AUTO))
5823 return;
5824
5825 pos = curwin->w_cursor;
5826 old = ml_get_curline();
5827
5828 /* may remove added space */
5829 check_auto_format(FALSE);
5830
5831 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5832 * user might insert normal text next. Also skip formatting when "1" is
5833 * in 'formatoptions' and there is a single character before the cursor.
5834 * Otherwise the line would be broken and when typing another non-white
5835 * next they are not joined back together. */
5836 wasatend = (pos.col == STRLEN(old));
5837 if (*old != NUL && !trailblank && wasatend)
5838 {
5839 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005840 cc = gchar_cursor();
5841 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5842 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005844 cc = gchar_cursor();
5845 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {
5847 curwin->w_cursor = pos;
5848 return;
5849 }
5850 curwin->w_cursor = pos;
5851 }
5852
5853#ifdef FEAT_COMMENTS
5854 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5855 * comments. */
5856 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5857 && get_leader_len(old, NULL, FALSE) == 0)
5858 return;
5859#endif
5860
5861 /*
5862 * May start formatting in a previous line, so that after "x" a word is
5863 * moved to the previous line if it fits there now. Only when this is not
5864 * the start of a paragraph.
5865 */
5866 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5867 {
5868 --curwin->w_cursor.lnum;
5869 if (u_save_cursor() == FAIL)
5870 return;
5871 }
5872
5873 /*
5874 * Do the formatting and restore the cursor position. "saved_cursor" will
5875 * be adjusted for the text formatting.
5876 */
5877 saved_cursor = pos;
5878 format_lines((linenr_T)-1);
5879 curwin->w_cursor = saved_cursor;
5880 saved_cursor.lnum = 0;
5881
5882 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5883 {
5884 /* "cannot happen" */
5885 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5886 coladvance((colnr_T)MAXCOL);
5887 }
5888 else
5889 check_cursor_col();
5890
5891 /* Insert mode: If the cursor is now after the end of the line while it
5892 * previously wasn't, the line was broken. Because of the rule above we
5893 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5894 * formatted. */
5895 if (!wasatend && has_format_option(FO_WHITE_PAR))
5896 {
5897 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005898 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 if (curwin->w_cursor.col == len)
5900 {
5901 pnew = vim_strnsave(new, len + 2);
5902 pnew[len] = ' ';
5903 pnew[len + 1] = NUL;
5904 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5905 /* remove the space later */
5906 did_add_space = TRUE;
5907 }
5908 else
5909 /* may remove added space */
5910 check_auto_format(FALSE);
5911 }
5912
5913 check_cursor();
5914}
5915
5916/*
5917 * When an extra space was added to continue a paragraph for auto-formatting,
5918 * delete it now. The space must be under the cursor, just after the insert
5919 * position.
5920 */
5921 static void
5922check_auto_format(end_insert)
5923 int end_insert; /* TRUE when ending Insert mode */
5924{
5925 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005926 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927
5928 if (did_add_space)
5929 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005930 cc = gchar_cursor();
5931 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 /* Somehow the space was removed already. */
5933 did_add_space = FALSE;
5934 else
5935 {
5936 if (!end_insert)
5937 {
5938 inc_cursor();
5939 c = gchar_cursor();
5940 dec_cursor();
5941 }
5942 if (c != NUL)
5943 {
5944 /* The space is no longer at the end of the line, delete it. */
5945 del_char(FALSE);
5946 did_add_space = FALSE;
5947 }
5948 }
5949 }
5950}
5951
5952/*
5953 * Find out textwidth to be used for formatting:
5954 * if 'textwidth' option is set, use it
5955 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5956 * if invalid value, use 0.
5957 * Set default to window width (maximum 79) for "gq" operator.
5958 */
5959 int
5960comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005961 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962{
5963 int textwidth;
5964
5965 textwidth = curbuf->b_p_tw;
5966 if (textwidth == 0 && curbuf->b_p_wm)
5967 {
5968 /* The width is the window width minus 'wrapmargin' minus all the
5969 * things that add to the margin. */
5970 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5971#ifdef FEAT_CMDWIN
5972 if (cmdwin_type != 0)
5973 textwidth -= 1;
5974#endif
5975#ifdef FEAT_FOLDING
5976 textwidth -= curwin->w_p_fdc;
5977#endif
5978#ifdef FEAT_SIGNS
5979 if (curwin->w_buffer->b_signlist != NULL
5980# ifdef FEAT_NETBEANS_INTG
5981 || usingNetbeans
5982# endif
5983 )
5984 textwidth -= 1;
5985#endif
5986 if (curwin->w_p_nu)
5987 textwidth -= 8;
5988 }
5989 if (textwidth < 0)
5990 textwidth = 0;
5991 if (ff && textwidth == 0)
5992 {
5993 textwidth = W_WIDTH(curwin) - 1;
5994 if (textwidth > 79)
5995 textwidth = 79;
5996 }
5997 return textwidth;
5998}
5999
6000/*
6001 * Put a character in the redo buffer, for when just after a CTRL-V.
6002 */
6003 static void
6004redo_literal(c)
6005 int c;
6006{
6007 char_u buf[10];
6008
6009 /* Only digits need special treatment. Translate them into a string of
6010 * three digits. */
6011 if (VIM_ISDIGIT(c))
6012 {
6013 sprintf((char *)buf, "%03d", c);
6014 AppendToRedobuff(buf);
6015 }
6016 else
6017 AppendCharToRedobuff(c);
6018}
6019
6020/*
6021 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006022 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 */
6024 static void
6025start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006026 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027{
6028 if (!arrow_used) /* something has been inserted */
6029 {
6030 AppendToRedobuff(ESC_STR);
6031 stop_insert(end_insert_pos, FALSE);
6032 arrow_used = TRUE; /* this means we stopped the current insert */
6033 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006034#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006035 check_spell_redraw();
6036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037}
6038
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006039#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006040/*
6041 * If we skipped highlighting word at cursor, do it now.
6042 * It may be skipped again, thus reset spell_redraw_lnum first.
6043 */
6044 static void
6045check_spell_redraw()
6046{
6047 if (spell_redraw_lnum != 0)
6048 {
6049 linenr_T lnum = spell_redraw_lnum;
6050
6051 spell_redraw_lnum = 0;
6052 redrawWinline(lnum, FALSE);
6053 }
6054}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006055
6056/*
6057 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6058 * spelled word, if there is one.
6059 */
6060 static void
6061spell_back_to_badword()
6062{
6063 pos_T tpos = curwin->w_cursor;
6064
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006065 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006066 if (curwin->w_cursor.col != tpos.col)
6067 start_arrow(&tpos);
6068}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006069#endif
6070
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071/*
6072 * stop_arrow() is called before a change is made in insert mode.
6073 * If an arrow key has been used, start a new insertion.
6074 * Returns FAIL if undo is impossible, shouldn't insert then.
6075 */
6076 int
6077stop_arrow()
6078{
6079 if (arrow_used)
6080 {
6081 if (u_save_cursor() == OK)
6082 {
6083 arrow_used = FALSE;
6084 ins_need_undo = FALSE;
6085 }
6086 Insstart = curwin->w_cursor; /* new insertion starts here */
6087 Insstart_textlen = linetabsize(ml_get_curline());
6088 ai_col = 0;
6089#ifdef FEAT_VREPLACE
6090 if (State & VREPLACE_FLAG)
6091 {
6092 orig_line_count = curbuf->b_ml.ml_line_count;
6093 vr_lines_changed = 1;
6094 }
6095#endif
6096 ResetRedobuff();
6097 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006098 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 }
6100 else if (ins_need_undo)
6101 {
6102 if (u_save_cursor() == OK)
6103 ins_need_undo = FALSE;
6104 }
6105
6106#ifdef FEAT_FOLDING
6107 /* Always open fold at the cursor line when inserting something. */
6108 foldOpenCursor();
6109#endif
6110
6111 return (arrow_used || ins_need_undo ? FAIL : OK);
6112}
6113
6114/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006115 * Do a few things to stop inserting.
6116 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6117 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 */
6119 static void
6120stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006121 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006122 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006124 int cc;
6125 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126
6127 stop_redo_ins();
6128 replace_flush(); /* abandon replace stack */
6129
6130 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006131 * Save the inserted text for later redo with ^@ and CTRL-A.
6132 * Don't do it when "restart_edit" was set and nothing was inserted,
6133 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006135 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006136 if (did_restart_edit == 0 || (ptr != NULL
6137 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006138 {
6139 vim_free(last_insert);
6140 last_insert = ptr;
6141 last_insert_skip = new_insert_skip;
6142 }
6143 else
6144 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006146 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 {
6148 /* Auto-format now. It may seem strange to do this when stopping an
6149 * insertion (or moving the cursor), but it's required when appending
6150 * a line and having it end in a space. But only do it when something
6151 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006152 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006154 pos_T tpos = curwin->w_cursor;
6155
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 /* When the cursor is at the end of the line after a space the
6157 * formatting will move it to the following word. Avoid that by
6158 * moving the cursor onto the space. */
6159 cc = 'x';
6160 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6161 {
6162 dec_cursor();
6163 cc = gchar_cursor();
6164 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006165 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 }
6167
6168 auto_format(TRUE, FALSE);
6169
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006170 if (vim_iswhite(cc))
6171 {
6172 if (gchar_cursor() != NUL)
6173 inc_cursor();
6174#ifdef FEAT_VIRTUALEDIT
6175 /* If the cursor is still at the same character, also keep
6176 * the "coladd". */
6177 if (gchar_cursor() == NUL
6178 && curwin->w_cursor.lnum == tpos.lnum
6179 && curwin->w_cursor.col == tpos.col)
6180 curwin->w_cursor.coladd = tpos.coladd;
6181#endif
6182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 }
6184
6185 /* If a space was inserted for auto-formatting, remove it now. */
6186 check_auto_format(TRUE);
6187
6188 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006189 * of the line, and put the cursor back.
6190 * Do this when ESC was used or moving the cursor up/down. */
6191 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006192 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006194 pos_T tpos = curwin->w_cursor;
6195
6196 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006197 for (;;)
6198 {
6199 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6200 --curwin->w_cursor.col;
6201 cc = gchar_cursor();
6202 if (!vim_iswhite(cc))
6203 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006205 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006206 if (curwin->w_cursor.lnum != tpos.lnum)
6207 curwin->w_cursor = tpos;
6208 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6210
6211#ifdef FEAT_VISUAL
6212 /* <C-S-Right> may have started Visual mode, adjust the position for
6213 * deleted characters. */
6214 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6215 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006216 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 if (VIsual.col > (colnr_T)cc)
6218 {
6219 VIsual.col = cc;
6220# ifdef FEAT_VIRTUALEDIT
6221 VIsual.coladd = 0;
6222# endif
6223 }
6224 }
6225#endif
6226 }
6227 }
6228 did_ai = FALSE;
6229#ifdef FEAT_SMARTINDENT
6230 did_si = FALSE;
6231 can_si = FALSE;
6232 can_si_back = FALSE;
6233#endif
6234
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006235 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6236 * now in a different buffer. */
6237 if (end_insert_pos != NULL)
6238 {
6239 curbuf->b_op_start = Insstart;
6240 curbuf->b_op_end = *end_insert_pos;
6241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242}
6243
6244/*
6245 * Set the last inserted text to a single character.
6246 * Used for the replace command.
6247 */
6248 void
6249set_last_insert(c)
6250 int c;
6251{
6252 char_u *s;
6253
6254 vim_free(last_insert);
6255#ifdef FEAT_MBYTE
6256 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6257#else
6258 last_insert = alloc(6);
6259#endif
6260 if (last_insert != NULL)
6261 {
6262 s = last_insert;
6263 /* Use the CTRL-V only when entering a special char */
6264 if (c < ' ' || c == DEL)
6265 *s++ = Ctrl_V;
6266 s = add_char2buf(c, s);
6267 *s++ = ESC;
6268 *s++ = NUL;
6269 last_insert_skip = 0;
6270 }
6271}
6272
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006273#if defined(EXITFREE) || defined(PROTO)
6274 void
6275free_last_insert()
6276{
6277 vim_free(last_insert);
6278 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006279 vim_free(compl_orig_text);
6280 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006281}
6282#endif
6283
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284/*
6285 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6286 * and CSI. Handle multi-byte characters.
6287 * Returns a pointer to after the added bytes.
6288 */
6289 char_u *
6290add_char2buf(c, s)
6291 int c;
6292 char_u *s;
6293{
6294#ifdef FEAT_MBYTE
6295 char_u temp[MB_MAXBYTES];
6296 int i;
6297 int len;
6298
6299 len = (*mb_char2bytes)(c, temp);
6300 for (i = 0; i < len; ++i)
6301 {
6302 c = temp[i];
6303#endif
6304 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6305 if (c == K_SPECIAL)
6306 {
6307 *s++ = K_SPECIAL;
6308 *s++ = KS_SPECIAL;
6309 *s++ = KE_FILLER;
6310 }
6311#ifdef FEAT_GUI
6312 else if (c == CSI)
6313 {
6314 *s++ = CSI;
6315 *s++ = KS_EXTRA;
6316 *s++ = (int)KE_CSI;
6317 }
6318#endif
6319 else
6320 *s++ = c;
6321#ifdef FEAT_MBYTE
6322 }
6323#endif
6324 return s;
6325}
6326
6327/*
6328 * move cursor to start of line
6329 * if flags & BL_WHITE move to first non-white
6330 * if flags & BL_SOL move to first non-white if startofline is set,
6331 * otherwise keep "curswant" column
6332 * if flags & BL_FIX don't leave the cursor on a NUL.
6333 */
6334 void
6335beginline(flags)
6336 int flags;
6337{
6338 if ((flags & BL_SOL) && !p_sol)
6339 coladvance(curwin->w_curswant);
6340 else
6341 {
6342 curwin->w_cursor.col = 0;
6343#ifdef FEAT_VIRTUALEDIT
6344 curwin->w_cursor.coladd = 0;
6345#endif
6346
6347 if (flags & (BL_WHITE | BL_SOL))
6348 {
6349 char_u *ptr;
6350
6351 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6352 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6353 ++curwin->w_cursor.col;
6354 }
6355 curwin->w_set_curswant = TRUE;
6356 }
6357}
6358
6359/*
6360 * oneright oneleft cursor_down cursor_up
6361 *
6362 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006363 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 * Return OK when successful, FAIL when we hit a line of file boundary.
6365 */
6366
6367 int
6368oneright()
6369{
6370 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372
6373#ifdef FEAT_VIRTUALEDIT
6374 if (virtual_active())
6375 {
6376 pos_T prevpos = curwin->w_cursor;
6377
6378 /* Adjust for multi-wide char (excluding TAB) */
6379 ptr = ml_get_cursor();
6380 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006381# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006383# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 ))
6387 ? ptr2cells(ptr) : 1));
6388 curwin->w_set_curswant = TRUE;
6389 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6390 return (prevpos.col != curwin->w_cursor.col
6391 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6392 }
6393#endif
6394
6395 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006396 if (*ptr == NUL)
6397 return FAIL; /* already at the very end */
6398
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006400 if (has_mbyte)
6401 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 else
6403#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006404 l = 1;
6405
6406 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6407 * contains "onemore". */
6408 if (ptr[l] == NUL
6409#ifdef FEAT_VIRTUALEDIT
6410 && (ve_flags & VE_ONEMORE) == 0
6411#endif
6412 )
6413 return FAIL;
6414 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415
6416 curwin->w_set_curswant = TRUE;
6417 return OK;
6418}
6419
6420 int
6421oneleft()
6422{
6423#ifdef FEAT_VIRTUALEDIT
6424 if (virtual_active())
6425 {
6426 int width;
6427 int v = getviscol();
6428
6429 if (v == 0)
6430 return FAIL;
6431
6432# ifdef FEAT_LINEBREAK
6433 /* We might get stuck on 'showbreak', skip over it. */
6434 width = 1;
6435 for (;;)
6436 {
6437 coladvance(v - width);
6438 /* getviscol() is slow, skip it when 'showbreak' is empty and
6439 * there are no multi-byte characters */
6440 if ((*p_sbr == NUL
6441# ifdef FEAT_MBYTE
6442 && !has_mbyte
6443# endif
6444 ) || getviscol() < v)
6445 break;
6446 ++width;
6447 }
6448# else
6449 coladvance(v - 1);
6450# endif
6451
6452 if (curwin->w_cursor.coladd == 1)
6453 {
6454 char_u *ptr;
6455
6456 /* Adjust for multi-wide char (not a TAB) */
6457 ptr = ml_get_cursor();
6458 if (*ptr != TAB && vim_isprintc(
6459# ifdef FEAT_MBYTE
6460 (*mb_ptr2char)(ptr)
6461# else
6462 *ptr
6463# endif
6464 ) && ptr2cells(ptr) > 1)
6465 curwin->w_cursor.coladd = 0;
6466 }
6467
6468 curwin->w_set_curswant = TRUE;
6469 return OK;
6470 }
6471#endif
6472
6473 if (curwin->w_cursor.col == 0)
6474 return FAIL;
6475
6476 curwin->w_set_curswant = TRUE;
6477 --curwin->w_cursor.col;
6478
6479#ifdef FEAT_MBYTE
6480 /* if the character on the left of the current cursor is a multi-byte
6481 * character, move to its first byte */
6482 if (has_mbyte)
6483 mb_adjust_cursor();
6484#endif
6485 return OK;
6486}
6487
6488 int
6489cursor_up(n, upd_topline)
6490 long n;
6491 int upd_topline; /* When TRUE: update topline */
6492{
6493 linenr_T lnum;
6494
6495 if (n > 0)
6496 {
6497 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006498 /* This fails if the cursor is already in the first line or the count
6499 * is larger than the line number and '-' is in 'cpoptions' */
6500 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 return FAIL;
6502 if (n >= lnum)
6503 lnum = 1;
6504 else
6505#ifdef FEAT_FOLDING
6506 if (hasAnyFolding(curwin))
6507 {
6508 /*
6509 * Count each sequence of folded lines as one logical line.
6510 */
6511 /* go to the the start of the current fold */
6512 (void)hasFolding(lnum, &lnum, NULL);
6513
6514 while (n--)
6515 {
6516 /* move up one line */
6517 --lnum;
6518 if (lnum <= 1)
6519 break;
6520 /* If we entered a fold, move to the beginning, unless in
6521 * Insert mode or when 'foldopen' contains "all": it will open
6522 * in a moment. */
6523 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6524 (void)hasFolding(lnum, &lnum, NULL);
6525 }
6526 if (lnum < 1)
6527 lnum = 1;
6528 }
6529 else
6530#endif
6531 lnum -= n;
6532 curwin->w_cursor.lnum = lnum;
6533 }
6534
6535 /* try to advance to the column we want to be at */
6536 coladvance(curwin->w_curswant);
6537
6538 if (upd_topline)
6539 update_topline(); /* make sure curwin->w_topline is valid */
6540
6541 return OK;
6542}
6543
6544/*
6545 * Cursor down a number of logical lines.
6546 */
6547 int
6548cursor_down(n, upd_topline)
6549 long n;
6550 int upd_topline; /* When TRUE: update topline */
6551{
6552 linenr_T lnum;
6553
6554 if (n > 0)
6555 {
6556 lnum = curwin->w_cursor.lnum;
6557#ifdef FEAT_FOLDING
6558 /* Move to last line of fold, will fail if it's the end-of-file. */
6559 (void)hasFolding(lnum, NULL, &lnum);
6560#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006561 /* This fails if the cursor is already in the last line or would move
6562 * beyound the last line and '-' is in 'cpoptions' */
6563 if (lnum >= curbuf->b_ml.ml_line_count
6564 || (lnum + n > curbuf->b_ml.ml_line_count
6565 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 return FAIL;
6567 if (lnum + n >= curbuf->b_ml.ml_line_count)
6568 lnum = curbuf->b_ml.ml_line_count;
6569 else
6570#ifdef FEAT_FOLDING
6571 if (hasAnyFolding(curwin))
6572 {
6573 linenr_T last;
6574
6575 /* count each sequence of folded lines as one logical line */
6576 while (n--)
6577 {
6578 if (hasFolding(lnum, NULL, &last))
6579 lnum = last + 1;
6580 else
6581 ++lnum;
6582 if (lnum >= curbuf->b_ml.ml_line_count)
6583 break;
6584 }
6585 if (lnum > curbuf->b_ml.ml_line_count)
6586 lnum = curbuf->b_ml.ml_line_count;
6587 }
6588 else
6589#endif
6590 lnum += n;
6591 curwin->w_cursor.lnum = lnum;
6592 }
6593
6594 /* try to advance to the column we want to be at */
6595 coladvance(curwin->w_curswant);
6596
6597 if (upd_topline)
6598 update_topline(); /* make sure curwin->w_topline is valid */
6599
6600 return OK;
6601}
6602
6603/*
6604 * Stuff the last inserted text in the read buffer.
6605 * Last_insert actually is a copy of the redo buffer, so we
6606 * first have to remove the command.
6607 */
6608 int
6609stuff_inserted(c, count, no_esc)
6610 int c; /* Command character to be inserted */
6611 long count; /* Repeat this many times */
6612 int no_esc; /* Don't add an ESC at the end */
6613{
6614 char_u *esc_ptr;
6615 char_u *ptr;
6616 char_u *last_ptr;
6617 char_u last = NUL;
6618
6619 ptr = get_last_insert();
6620 if (ptr == NULL)
6621 {
6622 EMSG(_(e_noinstext));
6623 return FAIL;
6624 }
6625
6626 /* may want to stuff the command character, to start Insert mode */
6627 if (c != NUL)
6628 stuffcharReadbuff(c);
6629 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6630 *esc_ptr = NUL; /* remove the ESC */
6631
6632 /* when the last char is either "0" or "^" it will be quoted if no ESC
6633 * comes after it OR if it will inserted more than once and "ptr"
6634 * starts with ^D. -- Acevedo
6635 */
6636 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6637 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6638 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6639 {
6640 last = *last_ptr;
6641 *last_ptr = NUL;
6642 }
6643
6644 do
6645 {
6646 stuffReadbuff(ptr);
6647 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6648 if (last)
6649 stuffReadbuff((char_u *)(last == '0'
6650 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6651 : IF_EB("\026^", CTRL_V_STR "^")));
6652 }
6653 while (--count > 0);
6654
6655 if (last)
6656 *last_ptr = last;
6657
6658 if (esc_ptr != NULL)
6659 *esc_ptr = ESC; /* put the ESC back */
6660
6661 /* may want to stuff a trailing ESC, to get out of Insert mode */
6662 if (!no_esc)
6663 stuffcharReadbuff(ESC);
6664
6665 return OK;
6666}
6667
6668 char_u *
6669get_last_insert()
6670{
6671 if (last_insert == NULL)
6672 return NULL;
6673 return last_insert + last_insert_skip;
6674}
6675
6676/*
6677 * Get last inserted string, and remove trailing <Esc>.
6678 * Returns pointer to allocated memory (must be freed) or NULL.
6679 */
6680 char_u *
6681get_last_insert_save()
6682{
6683 char_u *s;
6684 int len;
6685
6686 if (last_insert == NULL)
6687 return NULL;
6688 s = vim_strsave(last_insert + last_insert_skip);
6689 if (s != NULL)
6690 {
6691 len = (int)STRLEN(s);
6692 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6693 s[len - 1] = NUL;
6694 }
6695 return s;
6696}
6697
6698/*
6699 * Check the word in front of the cursor for an abbreviation.
6700 * Called when the non-id character "c" has been entered.
6701 * When an abbreviation is recognized it is removed from the text and
6702 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6703 */
6704 static int
6705echeck_abbr(c)
6706 int c;
6707{
6708 /* Don't check for abbreviation in paste mode, when disabled and just
6709 * after moving around with cursor keys. */
6710 if (p_paste || no_abbr || arrow_used)
6711 return FALSE;
6712
6713 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6714 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6715}
6716
6717/*
6718 * replace-stack functions
6719 *
6720 * When replacing characters, the replaced characters are remembered for each
6721 * new character. This is used to re-insert the old text when backspacing.
6722 *
6723 * There is a NUL headed list of characters for each character that is
6724 * currently in the file after the insertion point. When BS is used, one NUL
6725 * headed list is put back for the deleted character.
6726 *
6727 * For a newline, there are two NUL headed lists. One contains the characters
6728 * that the NL replaced. The extra one stores the characters after the cursor
6729 * that were deleted (always white space).
6730 *
6731 * Replace_offset is normally 0, in which case replace_push will add a new
6732 * character at the end of the stack. If replace_offset is not 0, that many
6733 * characters will be left on the stack above the newly inserted character.
6734 */
6735
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006736static char_u *replace_stack = NULL;
6737static long replace_stack_nr = 0; /* next entry in replace stack */
6738static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739
6740 void
6741replace_push(c)
6742 int c; /* character that is replaced (NUL is none) */
6743{
6744 char_u *p;
6745
6746 if (replace_stack_nr < replace_offset) /* nothing to do */
6747 return;
6748 if (replace_stack_len <= replace_stack_nr)
6749 {
6750 replace_stack_len += 50;
6751 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6752 if (p == NULL) /* out of memory */
6753 {
6754 replace_stack_len -= 50;
6755 return;
6756 }
6757 if (replace_stack != NULL)
6758 {
6759 mch_memmove(p, replace_stack,
6760 (size_t)(replace_stack_nr * sizeof(char_u)));
6761 vim_free(replace_stack);
6762 }
6763 replace_stack = p;
6764 }
6765 p = replace_stack + replace_stack_nr - replace_offset;
6766 if (replace_offset)
6767 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6768 *p = c;
6769 ++replace_stack_nr;
6770}
6771
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006772#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773/*
6774 * call replace_push(c) with replace_offset set to the first NUL.
6775 */
6776 static void
6777replace_push_off(c)
6778 int c;
6779{
6780 char_u *p;
6781
6782 p = replace_stack + replace_stack_nr;
6783 for (replace_offset = 1; replace_offset < replace_stack_nr;
6784 ++replace_offset)
6785 if (*--p == NUL)
6786 break;
6787 replace_push(c);
6788 replace_offset = 0;
6789}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791
6792/*
6793 * Pop one item from the replace stack.
6794 * return -1 if stack empty
6795 * return replaced character or NUL otherwise
6796 */
6797 static int
6798replace_pop()
6799{
6800 if (replace_stack_nr == 0)
6801 return -1;
6802 return (int)replace_stack[--replace_stack_nr];
6803}
6804
6805/*
6806 * Join the top two items on the replace stack. This removes to "off"'th NUL
6807 * encountered.
6808 */
6809 static void
6810replace_join(off)
6811 int off; /* offset for which NUL to remove */
6812{
6813 int i;
6814
6815 for (i = replace_stack_nr; --i >= 0; )
6816 if (replace_stack[i] == NUL && off-- <= 0)
6817 {
6818 --replace_stack_nr;
6819 mch_memmove(replace_stack + i, replace_stack + i + 1,
6820 (size_t)(replace_stack_nr - i));
6821 return;
6822 }
6823}
6824
6825/*
6826 * Pop bytes from the replace stack until a NUL is found, and insert them
6827 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6828 */
6829 static void
6830replace_pop_ins()
6831{
6832 int cc;
6833 int oldState = State;
6834
6835 State = NORMAL; /* don't want REPLACE here */
6836 while ((cc = replace_pop()) > 0)
6837 {
6838#ifdef FEAT_MBYTE
6839 mb_replace_pop_ins(cc);
6840#else
6841 ins_char(cc);
6842#endif
6843 dec_cursor();
6844 }
6845 State = oldState;
6846}
6847
6848#ifdef FEAT_MBYTE
6849/*
6850 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6851 * indicates a multi-byte char, pop the other bytes too.
6852 */
6853 static void
6854mb_replace_pop_ins(cc)
6855 int cc;
6856{
6857 int n;
6858 char_u buf[MB_MAXBYTES];
6859 int i;
6860 int c;
6861
6862 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6863 {
6864 buf[0] = cc;
6865 for (i = 1; i < n; ++i)
6866 buf[i] = replace_pop();
6867 ins_bytes_len(buf, n);
6868 }
6869 else
6870 ins_char(cc);
6871
6872 if (enc_utf8)
6873 /* Handle composing chars. */
6874 for (;;)
6875 {
6876 c = replace_pop();
6877 if (c == -1) /* stack empty */
6878 break;
6879 if ((n = MB_BYTE2LEN(c)) == 1)
6880 {
6881 /* Not a multi-byte char, put it back. */
6882 replace_push(c);
6883 break;
6884 }
6885 else
6886 {
6887 buf[0] = c;
6888 for (i = 1; i < n; ++i)
6889 buf[i] = replace_pop();
6890 if (utf_iscomposing(utf_ptr2char(buf)))
6891 ins_bytes_len(buf, n);
6892 else
6893 {
6894 /* Not a composing char, put it back. */
6895 for (i = n - 1; i >= 0; --i)
6896 replace_push(buf[i]);
6897 break;
6898 }
6899 }
6900 }
6901}
6902#endif
6903
6904/*
6905 * make the replace stack empty
6906 * (called when exiting replace mode)
6907 */
6908 static void
6909replace_flush()
6910{
6911 vim_free(replace_stack);
6912 replace_stack = NULL;
6913 replace_stack_len = 0;
6914 replace_stack_nr = 0;
6915}
6916
6917/*
6918 * Handle doing a BS for one character.
6919 * cc < 0: replace stack empty, just move cursor
6920 * cc == 0: character was inserted, delete it
6921 * cc > 0: character was replaced, put cc (first byte of original char) back
6922 * and check for more characters to be put back
6923 */
6924 static void
6925replace_do_bs()
6926{
6927 int cc;
6928#ifdef FEAT_VREPLACE
6929 int orig_len = 0;
6930 int ins_len;
6931 int orig_vcols = 0;
6932 colnr_T start_vcol;
6933 char_u *p;
6934 int i;
6935 int vcol;
6936#endif
6937
6938 cc = replace_pop();
6939 if (cc > 0)
6940 {
6941#ifdef FEAT_VREPLACE
6942 if (State & VREPLACE_FLAG)
6943 {
6944 /* Get the number of screen cells used by the character we are
6945 * going to delete. */
6946 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6947 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6948 }
6949#endif
6950#ifdef FEAT_MBYTE
6951 if (has_mbyte)
6952 {
6953 del_char(FALSE);
6954# ifdef FEAT_VREPLACE
6955 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006956 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957# endif
6958 replace_push(cc);
6959 }
6960 else
6961#endif
6962 {
6963 pchar_cursor(cc);
6964#ifdef FEAT_VREPLACE
6965 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006966 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967#endif
6968 }
6969 replace_pop_ins();
6970
6971#ifdef FEAT_VREPLACE
6972 if (State & VREPLACE_FLAG)
6973 {
6974 /* Get the number of screen cells used by the inserted characters */
6975 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006976 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 vcol = start_vcol;
6978 for (i = 0; i < ins_len; ++i)
6979 {
6980 vcol += chartabsize(p + i, vcol);
6981#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006982 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983#endif
6984 }
6985 vcol -= start_vcol;
6986
6987 /* Delete spaces that were inserted after the cursor to keep the
6988 * text aligned. */
6989 curwin->w_cursor.col += ins_len;
6990 while (vcol > orig_vcols && gchar_cursor() == ' ')
6991 {
6992 del_char(FALSE);
6993 ++orig_vcols;
6994 }
6995 curwin->w_cursor.col -= ins_len;
6996 }
6997#endif
6998
6999 /* mark the buffer as changed and prepare for displaying */
7000 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7001 }
7002 else if (cc == 0)
7003 (void)del_char(FALSE);
7004}
7005
7006#ifdef FEAT_CINDENT
7007/*
7008 * Return TRUE if C-indenting is on.
7009 */
7010 static int
7011cindent_on()
7012{
7013 return (!p_paste && (curbuf->b_p_cin
7014# ifdef FEAT_EVAL
7015 || *curbuf->b_p_inde != NUL
7016# endif
7017 ));
7018}
7019#endif
7020
7021#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7022/*
7023 * Re-indent the current line, based on the current contents of it and the
7024 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7025 * confused what all the part that handles Control-T is doing that I'm not.
7026 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7027 */
7028
7029 void
7030fixthisline(get_the_indent)
7031 int (*get_the_indent) __ARGS((void));
7032{
7033 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
7034 if (linewhite(curwin->w_cursor.lnum))
7035 did_ai = TRUE; /* delete the indent if the line stays empty */
7036}
7037
7038 void
7039fix_indent()
7040{
7041 if (p_paste)
7042 return;
7043# ifdef FEAT_LISP
7044 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7045 fixthisline(get_lisp_indent);
7046# endif
7047# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7048 else
7049# endif
7050# ifdef FEAT_CINDENT
7051 if (cindent_on())
7052 do_c_expr_indent();
7053# endif
7054}
7055
7056#endif
7057
7058#ifdef FEAT_CINDENT
7059/*
7060 * return TRUE if 'cinkeys' contains the key "keytyped",
7061 * when == '*': Only if key is preceded with '*' (indent before insert)
7062 * when == '!': Only if key is prededed with '!' (don't insert)
7063 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7064 *
7065 * "keytyped" can have a few special values:
7066 * KEY_OPEN_FORW
7067 * KEY_OPEN_BACK
7068 * KEY_COMPLETE just finished completion.
7069 *
7070 * If line_is_empty is TRUE accept keys with '0' before them.
7071 */
7072 int
7073in_cinkeys(keytyped, when, line_is_empty)
7074 int keytyped;
7075 int when;
7076 int line_is_empty;
7077{
7078 char_u *look;
7079 int try_match;
7080 int try_match_word;
7081 char_u *p;
7082 char_u *line;
7083 int icase;
7084 int i;
7085
7086#ifdef FEAT_EVAL
7087 if (*curbuf->b_p_inde != NUL)
7088 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7089 else
7090#endif
7091 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7092 while (*look)
7093 {
7094 /*
7095 * Find out if we want to try a match with this key, depending on
7096 * 'when' and a '*' or '!' before the key.
7097 */
7098 switch (when)
7099 {
7100 case '*': try_match = (*look == '*'); break;
7101 case '!': try_match = (*look == '!'); break;
7102 default: try_match = (*look != '*'); break;
7103 }
7104 if (*look == '*' || *look == '!')
7105 ++look;
7106
7107 /*
7108 * If there is a '0', only accept a match if the line is empty.
7109 * But may still match when typing last char of a word.
7110 */
7111 if (*look == '0')
7112 {
7113 try_match_word = try_match;
7114 if (!line_is_empty)
7115 try_match = FALSE;
7116 ++look;
7117 }
7118 else
7119 try_match_word = FALSE;
7120
7121 /*
7122 * does it look like a control character?
7123 */
7124 if (*look == '^'
7125#ifdef EBCDIC
7126 && (Ctrl_chr(look[1]) != 0)
7127#else
7128 && look[1] >= '?' && look[1] <= '_'
7129#endif
7130 )
7131 {
7132 if (try_match && keytyped == Ctrl_chr(look[1]))
7133 return TRUE;
7134 look += 2;
7135 }
7136 /*
7137 * 'o' means "o" command, open forward.
7138 * 'O' means "O" command, open backward.
7139 */
7140 else if (*look == 'o')
7141 {
7142 if (try_match && keytyped == KEY_OPEN_FORW)
7143 return TRUE;
7144 ++look;
7145 }
7146 else if (*look == 'O')
7147 {
7148 if (try_match && keytyped == KEY_OPEN_BACK)
7149 return TRUE;
7150 ++look;
7151 }
7152
7153 /*
7154 * 'e' means to check for "else" at start of line and just before the
7155 * cursor.
7156 */
7157 else if (*look == 'e')
7158 {
7159 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7160 {
7161 p = ml_get_curline();
7162 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7163 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7164 return TRUE;
7165 }
7166 ++look;
7167 }
7168
7169 /*
7170 * ':' only causes an indent if it is at the end of a label or case
7171 * statement, or when it was before typing the ':' (to fix
7172 * class::method for C++).
7173 */
7174 else if (*look == ':')
7175 {
7176 if (try_match && keytyped == ':')
7177 {
7178 p = ml_get_curline();
7179 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7180 return TRUE;
7181 if (curwin->w_cursor.col > 2
7182 && p[curwin->w_cursor.col - 1] == ':'
7183 && p[curwin->w_cursor.col - 2] == ':')
7184 {
7185 p[curwin->w_cursor.col - 1] = ' ';
7186 i = (cin_iscase(p) || cin_isscopedecl(p)
7187 || cin_islabel(30));
7188 p = ml_get_curline();
7189 p[curwin->w_cursor.col - 1] = ':';
7190 if (i)
7191 return TRUE;
7192 }
7193 }
7194 ++look;
7195 }
7196
7197
7198 /*
7199 * Is it a key in <>, maybe?
7200 */
7201 else if (*look == '<')
7202 {
7203 if (try_match)
7204 {
7205 /*
7206 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7207 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7208 * >, *, : and ! keys if they really really want to.
7209 */
7210 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7211 && keytyped == look[1])
7212 return TRUE;
7213
7214 if (keytyped == get_special_key_code(look + 1))
7215 return TRUE;
7216 }
7217 while (*look && *look != '>')
7218 look++;
7219 while (*look == '>')
7220 look++;
7221 }
7222
7223 /*
7224 * Is it a word: "=word"?
7225 */
7226 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7227 {
7228 ++look;
7229 if (*look == '~')
7230 {
7231 icase = TRUE;
7232 ++look;
7233 }
7234 else
7235 icase = FALSE;
7236 p = vim_strchr(look, ',');
7237 if (p == NULL)
7238 p = look + STRLEN(look);
7239 if ((try_match || try_match_word)
7240 && curwin->w_cursor.col >= (colnr_T)(p - look))
7241 {
7242 int match = FALSE;
7243
7244#ifdef FEAT_INS_EXPAND
7245 if (keytyped == KEY_COMPLETE)
7246 {
7247 char_u *s;
7248
7249 /* Just completed a word, check if it starts with "look".
7250 * search back for the start of a word. */
7251 line = ml_get_curline();
7252# ifdef FEAT_MBYTE
7253 if (has_mbyte)
7254 {
7255 char_u *n;
7256
7257 for (s = line + curwin->w_cursor.col; s > line; s = n)
7258 {
7259 n = mb_prevptr(line, s);
7260 if (!vim_iswordp(n))
7261 break;
7262 }
7263 }
7264 else
7265# endif
7266 for (s = line + curwin->w_cursor.col; s > line; --s)
7267 if (!vim_iswordc(s[-1]))
7268 break;
7269 if (s + (p - look) <= line + curwin->w_cursor.col
7270 && (icase
7271 ? MB_STRNICMP(s, look, p - look)
7272 : STRNCMP(s, look, p - look)) == 0)
7273 match = TRUE;
7274 }
7275 else
7276#endif
7277 /* TODO: multi-byte */
7278 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7279 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7280 {
7281 line = ml_get_cursor();
7282 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7283 || !vim_iswordc(line[-(p - look) - 1]))
7284 && (icase
7285 ? MB_STRNICMP(line - (p - look), look, p - look)
7286 : STRNCMP(line - (p - look), look, p - look))
7287 == 0)
7288 match = TRUE;
7289 }
7290 if (match && try_match_word && !try_match)
7291 {
7292 /* "0=word": Check if there are only blanks before the
7293 * word. */
7294 line = ml_get_curline();
7295 if ((int)(skipwhite(line) - line) !=
7296 (int)(curwin->w_cursor.col - (p - look)))
7297 match = FALSE;
7298 }
7299 if (match)
7300 return TRUE;
7301 }
7302 look = p;
7303 }
7304
7305 /*
7306 * ok, it's a boring generic character.
7307 */
7308 else
7309 {
7310 if (try_match && *look == keytyped)
7311 return TRUE;
7312 ++look;
7313 }
7314
7315 /*
7316 * Skip over ", ".
7317 */
7318 look = skip_to_option_part(look);
7319 }
7320 return FALSE;
7321}
7322#endif /* FEAT_CINDENT */
7323
7324#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7325/*
7326 * Map Hebrew keyboard when in hkmap mode.
7327 */
7328 int
7329hkmap(c)
7330 int c;
7331{
7332 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7333 {
7334 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7335 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7336 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7337 static char_u map[26] =
7338 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7339 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7340 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7341 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7342 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7343 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7344 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7345 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7346 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7347
7348 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7349 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7350 /* '-1'='sofit' */
7351 else if (c == 'x')
7352 return 'X';
7353 else if (c == 'q')
7354 return '\''; /* {geresh}={'} */
7355 else if (c == 246)
7356 return ' '; /* \"o --> ' ' for a german keyboard */
7357 else if (c == 228)
7358 return ' '; /* \"a --> ' ' -- / -- */
7359 else if (c == 252)
7360 return ' '; /* \"u --> ' ' -- / -- */
7361#ifdef EBCDIC
7362 else if (islower(c))
7363#else
7364 /* NOTE: islower() does not do the right thing for us on Linux so we
7365 * do this the same was as 5.7 and previous, so it works correctly on
7366 * all systems. Specifically, the e.g. Delete and Arrow keys are
7367 * munged and won't work if e.g. searching for Hebrew text.
7368 */
7369 else if (c >= 'a' && c <= 'z')
7370#endif
7371 return (int)(map[CharOrdLow(c)] + p_aleph);
7372 else
7373 return c;
7374 }
7375 else
7376 {
7377 switch (c)
7378 {
7379 case '`': return ';';
7380 case '/': return '.';
7381 case '\'': return ',';
7382 case 'q': return '/';
7383 case 'w': return '\'';
7384
7385 /* Hebrew letters - set offset from 'a' */
7386 case ',': c = '{'; break;
7387 case '.': c = 'v'; break;
7388 case ';': c = 't'; break;
7389 default: {
7390 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7391
7392#ifdef EBCDIC
7393 /* see note about islower() above */
7394 if (!islower(c))
7395#else
7396 if (c < 'a' || c > 'z')
7397#endif
7398 return c;
7399 c = str[CharOrdLow(c)];
7400 break;
7401 }
7402 }
7403
7404 return (int)(CharOrdLow(c) + p_aleph);
7405 }
7406}
7407#endif
7408
7409 static void
7410ins_reg()
7411{
7412 int need_redraw = FALSE;
7413 int regname;
7414 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007415#ifdef FEAT_VISUAL
7416 int vis_active = VIsual_active;
7417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418
7419 /*
7420 * If we are going to wait for a character, show a '"'.
7421 */
7422 pc_status = PC_STATUS_UNSET;
7423 if (redrawing() && !char_avail())
7424 {
7425 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007426 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427
7428 edit_putchar('"', TRUE);
7429#ifdef FEAT_CMDL_INFO
7430 add_to_showcmd_c(Ctrl_R);
7431#endif
7432 }
7433
7434#ifdef USE_ON_FLY_SCROLL
7435 dont_scroll = TRUE; /* disallow scrolling here */
7436#endif
7437
7438 /*
7439 * Don't map the register name. This also prevents the mode message to be
7440 * deleted when ESC is hit.
7441 */
7442 ++no_mapping;
7443 regname = safe_vgetc();
7444#ifdef FEAT_LANGMAP
7445 LANGMAP_ADJUST(regname, TRUE);
7446#endif
7447 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7448 {
7449 /* Get a third key for literal register insertion */
7450 literally = regname;
7451#ifdef FEAT_CMDL_INFO
7452 add_to_showcmd_c(literally);
7453#endif
7454 regname = safe_vgetc();
7455#ifdef FEAT_LANGMAP
7456 LANGMAP_ADJUST(regname, TRUE);
7457#endif
7458 }
7459 --no_mapping;
7460
7461#ifdef FEAT_EVAL
7462 /*
7463 * Don't call u_sync() while getting the expression,
7464 * evaluating it or giving an error message for it!
7465 */
7466 ++no_u_sync;
7467 if (regname == '=')
7468 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007469# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007473# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 /* Restore the Input Method. */
7475 if (im_on)
7476 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007479 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7480 {
7481 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 else
7485 {
7486#endif
7487 if (literally == Ctrl_O || literally == Ctrl_P)
7488 {
7489 /* Append the command to the redo buffer. */
7490 AppendCharToRedobuff(Ctrl_R);
7491 AppendCharToRedobuff(literally);
7492 AppendCharToRedobuff(regname);
7493
7494 do_put(regname, BACKWARD, 1L,
7495 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7496 }
7497 else if (insert_reg(regname, literally) == FAIL)
7498 {
7499 vim_beep();
7500 need_redraw = TRUE; /* remove the '"' */
7501 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007502 else if (stop_insert_mode)
7503 /* When the '=' register was used and a function was invoked that
7504 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7505 * insert anything, need to remove the '"' */
7506 need_redraw = TRUE;
7507
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508#ifdef FEAT_EVAL
7509 }
7510 --no_u_sync;
7511#endif
7512#ifdef FEAT_CMDL_INFO
7513 clear_showcmd();
7514#endif
7515
7516 /* If the inserted register is empty, we need to remove the '"' */
7517 if (need_redraw || stuff_empty())
7518 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007519
7520#ifdef FEAT_VISUAL
7521 /* Disallow starting Visual mode here, would get a weird mode. */
7522 if (!vis_active && VIsual_active)
7523 end_visual_mode();
7524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525}
7526
7527/*
7528 * CTRL-G commands in Insert mode.
7529 */
7530 static void
7531ins_ctrl_g()
7532{
7533 int c;
7534
7535#ifdef FEAT_INS_EXPAND
7536 /* Right after CTRL-X the cursor will be after the ruler. */
7537 setcursor();
7538#endif
7539
7540 /*
7541 * Don't map the second key. This also prevents the mode message to be
7542 * deleted when ESC is hit.
7543 */
7544 ++no_mapping;
7545 c = safe_vgetc();
7546 --no_mapping;
7547 switch (c)
7548 {
7549 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7550 case K_UP:
7551 case Ctrl_K:
7552 case 'k': ins_up(TRUE);
7553 break;
7554
7555 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7556 case K_DOWN:
7557 case Ctrl_J:
7558 case 'j': ins_down(TRUE);
7559 break;
7560
7561 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007562 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007564
7565 /* Need to reset Insstart, esp. because a BS that joins
7566 * aline to the previous one must save for undo. */
7567 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 break;
7569
7570 /* Unknown CTRL-G command, reserved for future expansion. */
7571 default: vim_beep();
7572 }
7573}
7574
7575/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007576 * CTRL-^ in Insert mode.
7577 */
7578 static void
7579ins_ctrl_hat()
7580{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007581 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007582 {
7583 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7584 if (State & LANGMAP)
7585 {
7586 curbuf->b_p_iminsert = B_IMODE_NONE;
7587 State &= ~LANGMAP;
7588 }
7589 else
7590 {
7591 curbuf->b_p_iminsert = B_IMODE_LMAP;
7592 State |= LANGMAP;
7593#ifdef USE_IM_CONTROL
7594 im_set_active(FALSE);
7595#endif
7596 }
7597 }
7598#ifdef USE_IM_CONTROL
7599 else
7600 {
7601 /* There are no ":lmap" mappings, toggle IM */
7602 if (im_get_status())
7603 {
7604 curbuf->b_p_iminsert = B_IMODE_NONE;
7605 im_set_active(FALSE);
7606 }
7607 else
7608 {
7609 curbuf->b_p_iminsert = B_IMODE_IM;
7610 State &= ~LANGMAP;
7611 im_set_active(TRUE);
7612 }
7613 }
7614#endif
7615 set_iminsert_global();
7616 showmode();
7617#ifdef FEAT_GUI
7618 /* may show different cursor shape or color */
7619 if (gui.in_use)
7620 gui_update_cursor(TRUE, FALSE);
7621#endif
7622#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7623 /* Show/unshow value of 'keymap' in status lines. */
7624 status_redraw_curbuf();
7625#endif
7626}
7627
7628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 * Handle ESC in insert mode.
7630 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7631 * insert.
7632 */
7633 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007634ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 long *count;
7636 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007637 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638{
7639 int temp;
7640 static int disabled_redraw = FALSE;
7641
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007642#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007643 check_spell_redraw();
7644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645#if defined(FEAT_HANGULIN)
7646# if defined(ESC_CHG_TO_ENG_MODE)
7647 hangul_input_state_set(0);
7648# endif
7649 if (composing_hangul)
7650 {
7651 push_raw_key(composing_hangul_buffer, 2);
7652 composing_hangul = 0;
7653 }
7654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655
7656 temp = curwin->w_cursor.col;
7657 if (disabled_redraw)
7658 {
7659 --RedrawingDisabled;
7660 disabled_redraw = FALSE;
7661 }
7662 if (!arrow_used)
7663 {
7664 /*
7665 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007666 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7667 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 */
7669 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007670 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671
7672 /*
7673 * Repeating insert may take a long time. Check for
7674 * interrupt now and then.
7675 */
7676 if (*count > 0)
7677 {
7678 line_breakcheck();
7679 if (got_int)
7680 *count = 0;
7681 }
7682
7683 if (--*count > 0) /* repeat what was typed */
7684 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007685 /* Vi repeats the insert without replacing characters. */
7686 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7687 State &= ~REPLACE_FLAG;
7688
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 (void)start_redo_ins();
7690 if (cmdchar == 'r' || cmdchar == 'v')
7691 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7692 ++RedrawingDisabled;
7693 disabled_redraw = TRUE;
7694 return FALSE; /* repeat the insert */
7695 }
7696 stop_insert(&curwin->w_cursor, TRUE);
7697 undisplay_dollar();
7698 }
7699
7700 /* When an autoindent was removed, curswant stays after the
7701 * indent */
7702 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7703 curwin->w_set_curswant = TRUE;
7704
7705 /* Remember the last Insert position in the '^ mark. */
7706 if (!cmdmod.keepjumps)
7707 curbuf->b_last_insert = curwin->w_cursor;
7708
7709 /*
7710 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007711 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007713 if (!nomove
7714 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715#ifdef FEAT_VIRTUALEDIT
7716 || curwin->w_cursor.coladd > 0
7717#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007718 )
7719 && (restart_edit == NUL
7720 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007722 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007724 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725#ifdef FEAT_RIGHTLEFT
7726 && !revins_on
7727#endif
7728 )
7729 {
7730#ifdef FEAT_VIRTUALEDIT
7731 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7732 {
7733 oneleft();
7734 if (restart_edit != NUL)
7735 ++curwin->w_cursor.coladd;
7736 }
7737 else
7738#endif
7739 {
7740 --curwin->w_cursor.col;
7741#ifdef FEAT_MBYTE
7742 /* Correct cursor for multi-byte character. */
7743 if (has_mbyte)
7744 mb_adjust_cursor();
7745#endif
7746 }
7747 }
7748
7749#ifdef USE_IM_CONTROL
7750 /* Disable IM to allow typing English directly for Normal mode commands.
7751 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7752 * well). */
7753 if (!(State & LANGMAP))
7754 im_save_status(&curbuf->b_p_iminsert);
7755 im_set_active(FALSE);
7756#endif
7757
7758 State = NORMAL;
7759 /* need to position cursor again (e.g. when on a TAB ) */
7760 changed_cline_bef_curs();
7761
7762#ifdef FEAT_MOUSE
7763 setmouse();
7764#endif
7765#ifdef CURSOR_SHAPE
7766 ui_cursor_shape(); /* may show different cursor shape */
7767#endif
7768
7769 /*
7770 * When recording or for CTRL-O, need to display the new mode.
7771 * Otherwise remove the mode message.
7772 */
7773 if (Recording || restart_edit != NUL)
7774 showmode();
7775 else if (p_smd)
7776 MSG("");
7777
7778 return TRUE; /* exit Insert mode */
7779}
7780
7781#ifdef FEAT_RIGHTLEFT
7782/*
7783 * Toggle language: hkmap and revins_on.
7784 * Move to end of reverse inserted text.
7785 */
7786 static void
7787ins_ctrl_()
7788{
7789 if (revins_on && revins_chars && revins_scol >= 0)
7790 {
7791 while (gchar_cursor() != NUL && revins_chars--)
7792 ++curwin->w_cursor.col;
7793 }
7794 p_ri = !p_ri;
7795 revins_on = (State == INSERT && p_ri);
7796 if (revins_on)
7797 {
7798 revins_scol = curwin->w_cursor.col;
7799 revins_legal++;
7800 revins_chars = 0;
7801 undisplay_dollar();
7802 }
7803 else
7804 revins_scol = -1;
7805#ifdef FEAT_FKMAP
7806 if (p_altkeymap)
7807 {
7808 /*
7809 * to be consistent also for redo command, using '.'
7810 * set arrow_used to true and stop it - causing to redo
7811 * characters entered in one mode (normal/reverse insert).
7812 */
7813 arrow_used = TRUE;
7814 (void)stop_arrow();
7815 p_fkmap = curwin->w_p_rl ^ p_ri;
7816 if (p_fkmap && p_ri)
7817 State = INSERT;
7818 }
7819 else
7820#endif
7821 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7822 showmode();
7823}
7824#endif
7825
7826#ifdef FEAT_VISUAL
7827/*
7828 * If 'keymodel' contains "startsel", may start selection.
7829 * Returns TRUE when a CTRL-O and other keys stuffed.
7830 */
7831 static int
7832ins_start_select(c)
7833 int c;
7834{
7835 if (km_startsel)
7836 switch (c)
7837 {
7838 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 case K_PAGEUP:
7841 case K_KPAGEUP:
7842 case K_PAGEDOWN:
7843 case K_KPAGEDOWN:
7844# ifdef MACOS
7845 case K_LEFT:
7846 case K_RIGHT:
7847 case K_UP:
7848 case K_DOWN:
7849 case K_END:
7850 case K_HOME:
7851# endif
7852 if (!(mod_mask & MOD_MASK_SHIFT))
7853 break;
7854 /* FALLTHROUGH */
7855 case K_S_LEFT:
7856 case K_S_RIGHT:
7857 case K_S_UP:
7858 case K_S_DOWN:
7859 case K_S_END:
7860 case K_S_HOME:
7861 /* Start selection right away, the cursor can move with
7862 * CTRL-O when beyond the end of the line. */
7863 start_selection();
7864
7865 /* Execute the key in (insert) Select mode. */
7866 stuffcharReadbuff(Ctrl_O);
7867 if (mod_mask)
7868 {
7869 char_u buf[4];
7870
7871 buf[0] = K_SPECIAL;
7872 buf[1] = KS_MODIFIER;
7873 buf[2] = mod_mask;
7874 buf[3] = NUL;
7875 stuffReadbuff(buf);
7876 }
7877 stuffcharReadbuff(c);
7878 return TRUE;
7879 }
7880 return FALSE;
7881}
7882#endif
7883
7884/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007885 * <Insert> key in Insert mode: toggle insert/remplace mode.
7886 */
7887 static void
7888ins_insert(replaceState)
7889 int replaceState;
7890{
7891#ifdef FEAT_FKMAP
7892 if (p_fkmap && p_ri)
7893 {
7894 beep_flush();
7895 EMSG(farsi_text_3); /* encoded in Farsi */
7896 return;
7897 }
7898#endif
7899
7900#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007901# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007902 set_vim_var_string(VV_INSERTMODE,
7903 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007904# ifdef FEAT_VREPLACE
7905 replaceState == VREPLACE ? "v" :
7906# endif
7907 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007908# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007909 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7910#endif
7911 if (State & REPLACE_FLAG)
7912 State = INSERT | (State & LANGMAP);
7913 else
7914 State = replaceState | (State & LANGMAP);
7915 AppendCharToRedobuff(K_INS);
7916 showmode();
7917#ifdef CURSOR_SHAPE
7918 ui_cursor_shape(); /* may show different cursor shape */
7919#endif
7920}
7921
7922/*
7923 * Pressed CTRL-O in Insert mode.
7924 */
7925 static void
7926ins_ctrl_o()
7927{
7928#ifdef FEAT_VREPLACE
7929 if (State & VREPLACE_FLAG)
7930 restart_edit = 'V';
7931 else
7932#endif
7933 if (State & REPLACE_FLAG)
7934 restart_edit = 'R';
7935 else
7936 restart_edit = 'I';
7937#ifdef FEAT_VIRTUALEDIT
7938 if (virtual_active())
7939 ins_at_eol = FALSE; /* cursor always keeps its column */
7940 else
7941#endif
7942 ins_at_eol = (gchar_cursor() == NUL);
7943}
7944
7945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 * If the cursor is on an indent, ^T/^D insert/delete one
7947 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7948 * Always round the indent to 'shiftwith', this is compatible
7949 * with vi. But vi only supports ^T and ^D after an
7950 * autoindent, we support it everywhere.
7951 */
7952 static void
7953ins_shift(c, lastc)
7954 int c;
7955 int lastc;
7956{
7957 if (stop_arrow() == FAIL)
7958 return;
7959 AppendCharToRedobuff(c);
7960
7961 /*
7962 * 0^D and ^^D: remove all indent.
7963 */
7964 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7965 {
7966 --curwin->w_cursor.col;
7967 (void)del_char(FALSE); /* delete the '^' or '0' */
7968 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7969 if (State & REPLACE_FLAG)
7970 replace_pop_ins();
7971 if (lastc == '^')
7972 old_indent = get_indent(); /* remember curr. indent */
7973 change_indent(INDENT_SET, 0, TRUE, 0);
7974 }
7975 else
7976 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7977
7978 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7979 did_ai = FALSE;
7980#ifdef FEAT_SMARTINDENT
7981 did_si = FALSE;
7982 can_si = FALSE;
7983 can_si_back = FALSE;
7984#endif
7985#ifdef FEAT_CINDENT
7986 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7987#endif
7988}
7989
7990 static void
7991ins_del()
7992{
7993 int temp;
7994
7995 if (stop_arrow() == FAIL)
7996 return;
7997 if (gchar_cursor() == NUL) /* delete newline */
7998 {
7999 temp = curwin->w_cursor.col;
8000 if (!can_bs(BS_EOL) /* only if "eol" included */
8001 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8002 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8003 || do_join(FALSE) == FAIL)
8004 vim_beep();
8005 else
8006 curwin->w_cursor.col = temp;
8007 }
8008 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8009 vim_beep();
8010 did_ai = FALSE;
8011#ifdef FEAT_SMARTINDENT
8012 did_si = FALSE;
8013 can_si = FALSE;
8014 can_si_back = FALSE;
8015#endif
8016 AppendCharToRedobuff(K_DEL);
8017}
8018
8019/*
8020 * Handle Backspace, delete-word and delete-line in Insert mode.
8021 * Return TRUE when backspace was actually used.
8022 */
8023 static int
8024ins_bs(c, mode, inserted_space_p)
8025 int c;
8026 int mode;
8027 int *inserted_space_p;
8028{
8029 linenr_T lnum;
8030 int cc;
8031 int temp = 0; /* init for GCC */
8032 colnr_T mincol;
8033 int did_backspace = FALSE;
8034 int in_indent;
8035 int oldState;
8036#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008037 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038#endif
8039
8040 /*
8041 * can't delete anything in an empty file
8042 * can't backup past first character in buffer
8043 * can't backup past starting point unless 'backspace' > 1
8044 * can backup to a previous line if 'backspace' == 0
8045 */
8046 if ( bufempty()
8047 || (
8048#ifdef FEAT_RIGHTLEFT
8049 !revins_on &&
8050#endif
8051 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8052 || (!can_bs(BS_START)
8053 && (arrow_used
8054 || (curwin->w_cursor.lnum == Insstart.lnum
8055 && curwin->w_cursor.col <= Insstart.col)))
8056 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8057 && curwin->w_cursor.col <= ai_col)
8058 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8059 {
8060 vim_beep();
8061 return FALSE;
8062 }
8063
8064 if (stop_arrow() == FAIL)
8065 return FALSE;
8066 in_indent = inindent(0);
8067#ifdef FEAT_CINDENT
8068 if (in_indent)
8069 can_cindent = FALSE;
8070#endif
8071#ifdef FEAT_COMMENTS
8072 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8073#endif
8074#ifdef FEAT_RIGHTLEFT
8075 if (revins_on) /* put cursor after last inserted char */
8076 inc_cursor();
8077#endif
8078
8079#ifdef FEAT_VIRTUALEDIT
8080 /* Virtualedit:
8081 * BACKSPACE_CHAR eats a virtual space
8082 * BACKSPACE_WORD eats all coladd
8083 * BACKSPACE_LINE eats all coladd and keeps going
8084 */
8085 if (curwin->w_cursor.coladd > 0)
8086 {
8087 if (mode == BACKSPACE_CHAR)
8088 {
8089 --curwin->w_cursor.coladd;
8090 return TRUE;
8091 }
8092 if (mode == BACKSPACE_WORD)
8093 {
8094 curwin->w_cursor.coladd = 0;
8095 return TRUE;
8096 }
8097 curwin->w_cursor.coladd = 0;
8098 }
8099#endif
8100
8101 /*
8102 * delete newline!
8103 */
8104 if (curwin->w_cursor.col == 0)
8105 {
8106 lnum = Insstart.lnum;
8107 if (curwin->w_cursor.lnum == Insstart.lnum
8108#ifdef FEAT_RIGHTLEFT
8109 || revins_on
8110#endif
8111 )
8112 {
8113 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8114 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8115 return FALSE;
8116 --Insstart.lnum;
8117 Insstart.col = MAXCOL;
8118 }
8119 /*
8120 * In replace mode:
8121 * cc < 0: NL was inserted, delete it
8122 * cc >= 0: NL was replaced, put original characters back
8123 */
8124 cc = -1;
8125 if (State & REPLACE_FLAG)
8126 cc = replace_pop(); /* returns -1 if NL was inserted */
8127 /*
8128 * In replace mode, in the line we started replacing, we only move the
8129 * cursor.
8130 */
8131 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8132 {
8133 dec_cursor();
8134 }
8135 else
8136 {
8137#ifdef FEAT_VREPLACE
8138 if (!(State & VREPLACE_FLAG)
8139 || curwin->w_cursor.lnum > orig_line_count)
8140#endif
8141 {
8142 temp = gchar_cursor(); /* remember current char */
8143 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008144
8145 /* When "aw" is in 'formatoptions' we must delete the space at
8146 * the end of the line, otherwise the line will be broken
8147 * again when auto-formatting. */
8148 if (has_format_option(FO_AUTO)
8149 && has_format_option(FO_WHITE_PAR))
8150 {
8151 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8152 TRUE);
8153 int len;
8154
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008155 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008156 if (len > 0 && ptr[len - 1] == ' ')
8157 ptr[len - 1] = NUL;
8158 }
8159
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 (void)do_join(FALSE);
8161 if (temp == NUL && gchar_cursor() != NUL)
8162 inc_cursor();
8163 }
8164#ifdef FEAT_VREPLACE
8165 else
8166 dec_cursor();
8167#endif
8168
8169 /*
8170 * In REPLACE mode we have to put back the text that was replaced
8171 * by the NL. On the replace stack is first a NUL-terminated
8172 * sequence of characters that were deleted and then the
8173 * characters that NL replaced.
8174 */
8175 if (State & REPLACE_FLAG)
8176 {
8177 /*
8178 * Do the next ins_char() in NORMAL state, to
8179 * prevent ins_char() from replacing characters and
8180 * avoiding showmatch().
8181 */
8182 oldState = State;
8183 State = NORMAL;
8184 /*
8185 * restore characters (blanks) deleted after cursor
8186 */
8187 while (cc > 0)
8188 {
8189 temp = curwin->w_cursor.col;
8190#ifdef FEAT_MBYTE
8191 mb_replace_pop_ins(cc);
8192#else
8193 ins_char(cc);
8194#endif
8195 curwin->w_cursor.col = temp;
8196 cc = replace_pop();
8197 }
8198 /* restore the characters that NL replaced */
8199 replace_pop_ins();
8200 State = oldState;
8201 }
8202 }
8203 did_ai = FALSE;
8204 }
8205 else
8206 {
8207 /*
8208 * Delete character(s) before the cursor.
8209 */
8210#ifdef FEAT_RIGHTLEFT
8211 if (revins_on) /* put cursor on last inserted char */
8212 dec_cursor();
8213#endif
8214 mincol = 0;
8215 /* keep indent */
8216 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
8217#ifdef FEAT_RIGHTLEFT
8218 && !revins_on
8219#endif
8220 )
8221 {
8222 temp = curwin->w_cursor.col;
8223 beginline(BL_WHITE);
8224 if (curwin->w_cursor.col < (colnr_T)temp)
8225 mincol = curwin->w_cursor.col;
8226 curwin->w_cursor.col = temp;
8227 }
8228
8229 /*
8230 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8231 */
8232 if ( mode == BACKSPACE_CHAR
8233 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008234 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 && (*(ml_get_cursor() - 1) == TAB
8236 || (*(ml_get_cursor() - 1) == ' '
8237 && (!*inserted_space_p
8238 || arrow_used))))))
8239 {
8240 int ts;
8241 colnr_T vcol;
8242 colnr_T want_vcol;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008243#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 int extra = 0;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246
8247 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008248 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 ts = curbuf->b_p_sw;
8250 else
8251 ts = curbuf->b_p_sts;
8252 /* Compute the virtual column where we want to be. Since
8253 * 'showbreak' may get in the way, need to get the last column of
8254 * the previous character. */
8255 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8256 dec_cursor();
8257 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8258 inc_cursor();
8259 want_vcol = (want_vcol / ts) * ts;
8260
8261 /* delete characters until we are at or before want_vcol */
8262 while (vcol > want_vcol
8263 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
8264 {
8265 dec_cursor();
8266 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8267 if (State & REPLACE_FLAG)
8268 {
8269 /* Don't delete characters before the insert point when in
8270 * Replace mode */
8271 if (curwin->w_cursor.lnum != Insstart.lnum
8272 || curwin->w_cursor.col >= Insstart.col)
8273 {
8274#if 0 /* what was this for? It causes problems when sw != ts. */
8275 if (State == REPLACE && (int)vcol < want_vcol)
8276 {
8277 (void)del_char(FALSE);
8278 extra = 2; /* don't pop too much */
8279 }
8280 else
8281#endif
8282 replace_do_bs();
8283 }
8284 }
8285 else
8286 (void)del_char(FALSE);
8287 }
8288
8289 /* insert extra spaces until we are at want_vcol */
8290 while (vcol < want_vcol)
8291 {
8292 /* Remember the first char we inserted */
8293 if (curwin->w_cursor.lnum == Insstart.lnum
8294 && curwin->w_cursor.col < Insstart.col)
8295 Insstart.col = curwin->w_cursor.col;
8296
8297#ifdef FEAT_VREPLACE
8298 if (State & VREPLACE_FLAG)
8299 ins_char(' ');
8300 else
8301#endif
8302 {
8303 ins_str((char_u *)" ");
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008304 if ((State & REPLACE_FLAG) /* && extra <= 1 */)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008306#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 if (extra)
8308 replace_push_off(NUL);
8309 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 replace_push(NUL);
8312 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008313#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 if (extra == 2)
8315 extra = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 }
8318 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8319 }
8320 }
8321
8322 /*
8323 * Delete upto starting point, start of line or previous word.
8324 */
8325 else do
8326 {
8327#ifdef FEAT_RIGHTLEFT
8328 if (!revins_on) /* put cursor on char to be deleted */
8329#endif
8330 dec_cursor();
8331
8332 /* start of word? */
8333 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8334 {
8335 mode = BACKSPACE_WORD_NOT_SPACE;
8336 temp = vim_iswordc(gchar_cursor());
8337 }
8338 /* end of word? */
8339 else if (mode == BACKSPACE_WORD_NOT_SPACE
8340 && (vim_isspace(cc = gchar_cursor())
8341 || vim_iswordc(cc) != temp))
8342 {
8343#ifdef FEAT_RIGHTLEFT
8344 if (!revins_on)
8345#endif
8346 inc_cursor();
8347#ifdef FEAT_RIGHTLEFT
8348 else if (State & REPLACE_FLAG)
8349 dec_cursor();
8350#endif
8351 break;
8352 }
8353 if (State & REPLACE_FLAG)
8354 replace_do_bs();
8355 else
8356 {
8357#ifdef FEAT_MBYTE
8358 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008359 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360#endif
8361 (void)del_char(FALSE);
8362#ifdef FEAT_MBYTE
8363 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008364 * If there are combining characters and 'delcombine' is set
8365 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 * character.
8367 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008368 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 inc_cursor();
8370#endif
8371#ifdef FEAT_RIGHTLEFT
8372 if (revins_chars)
8373 {
8374 revins_chars--;
8375 revins_legal++;
8376 }
8377 if (revins_on && gchar_cursor() == NUL)
8378 break;
8379#endif
8380 }
8381 /* Just a single backspace?: */
8382 if (mode == BACKSPACE_CHAR)
8383 break;
8384 } while (
8385#ifdef FEAT_RIGHTLEFT
8386 revins_on ||
8387#endif
8388 (curwin->w_cursor.col > mincol
8389 && (curwin->w_cursor.lnum != Insstart.lnum
8390 || curwin->w_cursor.col != Insstart.col)));
8391 did_backspace = TRUE;
8392 }
8393#ifdef FEAT_SMARTINDENT
8394 did_si = FALSE;
8395 can_si = FALSE;
8396 can_si_back = FALSE;
8397#endif
8398 if (curwin->w_cursor.col <= 1)
8399 did_ai = FALSE;
8400 /*
8401 * It's a little strange to put backspaces into the redo
8402 * buffer, but it makes auto-indent a lot easier to deal
8403 * with.
8404 */
8405 AppendCharToRedobuff(c);
8406
8407 /* If deleted before the insertion point, adjust it */
8408 if (curwin->w_cursor.lnum == Insstart.lnum
8409 && curwin->w_cursor.col < Insstart.col)
8410 Insstart.col = curwin->w_cursor.col;
8411
8412 /* vi behaviour: the cursor moves backward but the character that
8413 * was there remains visible
8414 * Vim behaviour: the cursor moves backward and the character that
8415 * was there is erased from the screen.
8416 * We can emulate the vi behaviour by pretending there is a dollar
8417 * displayed even when there isn't.
8418 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8419 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8420 dollar_vcol = curwin->w_virtcol;
8421
8422 return did_backspace;
8423}
8424
8425#ifdef FEAT_MOUSE
8426 static void
8427ins_mouse(c)
8428 int c;
8429{
8430 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008431 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432
8433# ifdef FEAT_GUI
8434 /* When GUI is active, also move/paste when 'mouse' is empty */
8435 if (!gui.in_use)
8436# endif
8437 if (!mouse_has(MOUSE_INSERT))
8438 return;
8439
8440 undisplay_dollar();
8441 tpos = curwin->w_cursor;
8442 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8443 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008444#ifdef FEAT_WINDOWS
8445 win_T *new_curwin = curwin;
8446
8447 if (curwin != old_curwin && win_valid(old_curwin))
8448 {
8449 /* Mouse took us to another window. We need to go back to the
8450 * previous one to stop insert there properly. */
8451 curwin = old_curwin;
8452 curbuf = curwin->w_buffer;
8453 }
8454#endif
8455 start_arrow(curwin == old_curwin ? &tpos : NULL);
8456#ifdef FEAT_WINDOWS
8457 if (curwin != new_curwin && win_valid(new_curwin))
8458 {
8459 curwin = new_curwin;
8460 curbuf = curwin->w_buffer;
8461 }
8462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463# ifdef FEAT_CINDENT
8464 can_cindent = TRUE;
8465# endif
8466 }
8467
8468#ifdef FEAT_WINDOWS
8469 /* redraw status lines (in case another window became active) */
8470 redraw_statuslines();
8471#endif
8472}
8473
8474 static void
8475ins_mousescroll(up)
8476 int up;
8477{
8478 pos_T tpos;
8479# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8480 win_T *old_curwin;
8481# endif
8482
8483 tpos = curwin->w_cursor;
8484
8485# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8486 old_curwin = curwin;
8487
8488 /* Currently the mouse coordinates are only known in the GUI. */
8489 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8490 {
8491 int row, col;
8492
8493 row = mouse_row;
8494 col = mouse_col;
8495
8496 /* find the window at the pointer coordinates */
8497 curwin = mouse_find_win(&row, &col);
8498 curbuf = curwin->w_buffer;
8499 }
8500 if (curwin == old_curwin)
8501# endif
8502 undisplay_dollar();
8503
8504 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8505 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8506 else
8507 scroll_redraw(up, 3L);
8508
8509# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8510 curwin->w_redr_status = TRUE;
8511
8512 curwin = old_curwin;
8513 curbuf = curwin->w_buffer;
8514# endif
8515
8516 if (!equalpos(curwin->w_cursor, tpos))
8517 {
8518 start_arrow(&tpos);
8519# ifdef FEAT_CINDENT
8520 can_cindent = TRUE;
8521# endif
8522 }
8523}
8524#endif
8525
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008526#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008527 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008528ins_tabline(c)
8529 int c;
8530{
8531 /* We will be leaving the current window, unless closing another tab. */
8532 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8533 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8534 {
8535 undisplay_dollar();
8536 start_arrow(&curwin->w_cursor);
8537# ifdef FEAT_CINDENT
8538 can_cindent = TRUE;
8539# endif
8540 }
8541
8542 if (c == K_TABLINE)
8543 goto_tabpage(current_tab);
8544 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008545 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008546 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008547 redraw_statuslines(); /* will redraw the tabline when needed */
8548 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008549}
8550#endif
8551
8552#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 void
8554ins_scroll()
8555{
8556 pos_T tpos;
8557
8558 undisplay_dollar();
8559 tpos = curwin->w_cursor;
8560 if (gui_do_scroll())
8561 {
8562 start_arrow(&tpos);
8563# ifdef FEAT_CINDENT
8564 can_cindent = TRUE;
8565# endif
8566 }
8567}
8568
8569 void
8570ins_horscroll()
8571{
8572 pos_T tpos;
8573
8574 undisplay_dollar();
8575 tpos = curwin->w_cursor;
8576 if (gui_do_horiz_scroll())
8577 {
8578 start_arrow(&tpos);
8579# ifdef FEAT_CINDENT
8580 can_cindent = TRUE;
8581# endif
8582 }
8583}
8584#endif
8585
8586 static void
8587ins_left()
8588{
8589 pos_T tpos;
8590
8591#ifdef FEAT_FOLDING
8592 if ((fdo_flags & FDO_HOR) && KeyTyped)
8593 foldOpenCursor();
8594#endif
8595 undisplay_dollar();
8596 tpos = curwin->w_cursor;
8597 if (oneleft() == OK)
8598 {
8599 start_arrow(&tpos);
8600#ifdef FEAT_RIGHTLEFT
8601 /* If exit reversed string, position is fixed */
8602 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8603 revins_legal++;
8604 revins_chars++;
8605#endif
8606 }
8607
8608 /*
8609 * if 'whichwrap' set for cursor in insert mode may go to
8610 * previous line
8611 */
8612 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8613 {
8614 start_arrow(&tpos);
8615 --(curwin->w_cursor.lnum);
8616 coladvance((colnr_T)MAXCOL);
8617 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8618 }
8619 else
8620 vim_beep();
8621}
8622
8623 static void
8624ins_home(c)
8625 int c;
8626{
8627 pos_T tpos;
8628
8629#ifdef FEAT_FOLDING
8630 if ((fdo_flags & FDO_HOR) && KeyTyped)
8631 foldOpenCursor();
8632#endif
8633 undisplay_dollar();
8634 tpos = curwin->w_cursor;
8635 if (c == K_C_HOME)
8636 curwin->w_cursor.lnum = 1;
8637 curwin->w_cursor.col = 0;
8638#ifdef FEAT_VIRTUALEDIT
8639 curwin->w_cursor.coladd = 0;
8640#endif
8641 curwin->w_curswant = 0;
8642 start_arrow(&tpos);
8643}
8644
8645 static void
8646ins_end(c)
8647 int c;
8648{
8649 pos_T tpos;
8650
8651#ifdef FEAT_FOLDING
8652 if ((fdo_flags & FDO_HOR) && KeyTyped)
8653 foldOpenCursor();
8654#endif
8655 undisplay_dollar();
8656 tpos = curwin->w_cursor;
8657 if (c == K_C_END)
8658 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8659 coladvance((colnr_T)MAXCOL);
8660 curwin->w_curswant = MAXCOL;
8661
8662 start_arrow(&tpos);
8663}
8664
8665 static void
8666ins_s_left()
8667{
8668#ifdef FEAT_FOLDING
8669 if ((fdo_flags & FDO_HOR) && KeyTyped)
8670 foldOpenCursor();
8671#endif
8672 undisplay_dollar();
8673 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8674 {
8675 start_arrow(&curwin->w_cursor);
8676 (void)bck_word(1L, FALSE, FALSE);
8677 curwin->w_set_curswant = TRUE;
8678 }
8679 else
8680 vim_beep();
8681}
8682
8683 static void
8684ins_right()
8685{
8686#ifdef FEAT_FOLDING
8687 if ((fdo_flags & FDO_HOR) && KeyTyped)
8688 foldOpenCursor();
8689#endif
8690 undisplay_dollar();
8691 if (gchar_cursor() != NUL || virtual_active()
8692 )
8693 {
8694 start_arrow(&curwin->w_cursor);
8695 curwin->w_set_curswant = TRUE;
8696#ifdef FEAT_VIRTUALEDIT
8697 if (virtual_active())
8698 oneright();
8699 else
8700#endif
8701 {
8702#ifdef FEAT_MBYTE
8703 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008704 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 else
8706#endif
8707 ++curwin->w_cursor.col;
8708 }
8709
8710#ifdef FEAT_RIGHTLEFT
8711 revins_legal++;
8712 if (revins_chars)
8713 revins_chars--;
8714#endif
8715 }
8716 /* if 'whichwrap' set for cursor in insert mode, may move the
8717 * cursor to the next line */
8718 else if (vim_strchr(p_ww, ']') != NULL
8719 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8720 {
8721 start_arrow(&curwin->w_cursor);
8722 curwin->w_set_curswant = TRUE;
8723 ++curwin->w_cursor.lnum;
8724 curwin->w_cursor.col = 0;
8725 }
8726 else
8727 vim_beep();
8728}
8729
8730 static void
8731ins_s_right()
8732{
8733#ifdef FEAT_FOLDING
8734 if ((fdo_flags & FDO_HOR) && KeyTyped)
8735 foldOpenCursor();
8736#endif
8737 undisplay_dollar();
8738 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8739 || gchar_cursor() != NUL)
8740 {
8741 start_arrow(&curwin->w_cursor);
8742 (void)fwd_word(1L, FALSE, 0);
8743 curwin->w_set_curswant = TRUE;
8744 }
8745 else
8746 vim_beep();
8747}
8748
8749 static void
8750ins_up(startcol)
8751 int startcol; /* when TRUE move to Insstart.col */
8752{
8753 pos_T tpos;
8754 linenr_T old_topline = curwin->w_topline;
8755#ifdef FEAT_DIFF
8756 int old_topfill = curwin->w_topfill;
8757#endif
8758
8759 undisplay_dollar();
8760 tpos = curwin->w_cursor;
8761 if (cursor_up(1L, TRUE) == OK)
8762 {
8763 if (startcol)
8764 coladvance(getvcol_nolist(&Insstart));
8765 if (old_topline != curwin->w_topline
8766#ifdef FEAT_DIFF
8767 || old_topfill != curwin->w_topfill
8768#endif
8769 )
8770 redraw_later(VALID);
8771 start_arrow(&tpos);
8772#ifdef FEAT_CINDENT
8773 can_cindent = TRUE;
8774#endif
8775 }
8776 else
8777 vim_beep();
8778}
8779
8780 static void
8781ins_pageup()
8782{
8783 pos_T tpos;
8784
8785 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008786
8787#ifdef FEAT_WINDOWS
8788 if (mod_mask & MOD_MASK_CTRL)
8789 {
8790 /* <C-PageUp>: tab page back */
8791 goto_tabpage(-1);
8792 return;
8793 }
8794#endif
8795
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 tpos = curwin->w_cursor;
8797 if (onepage(BACKWARD, 1L) == OK)
8798 {
8799 start_arrow(&tpos);
8800#ifdef FEAT_CINDENT
8801 can_cindent = TRUE;
8802#endif
8803 }
8804 else
8805 vim_beep();
8806}
8807
8808 static void
8809ins_down(startcol)
8810 int startcol; /* when TRUE move to Insstart.col */
8811{
8812 pos_T tpos;
8813 linenr_T old_topline = curwin->w_topline;
8814#ifdef FEAT_DIFF
8815 int old_topfill = curwin->w_topfill;
8816#endif
8817
8818 undisplay_dollar();
8819 tpos = curwin->w_cursor;
8820 if (cursor_down(1L, TRUE) == OK)
8821 {
8822 if (startcol)
8823 coladvance(getvcol_nolist(&Insstart));
8824 if (old_topline != curwin->w_topline
8825#ifdef FEAT_DIFF
8826 || old_topfill != curwin->w_topfill
8827#endif
8828 )
8829 redraw_later(VALID);
8830 start_arrow(&tpos);
8831#ifdef FEAT_CINDENT
8832 can_cindent = TRUE;
8833#endif
8834 }
8835 else
8836 vim_beep();
8837}
8838
8839 static void
8840ins_pagedown()
8841{
8842 pos_T tpos;
8843
8844 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008845
8846#ifdef FEAT_WINDOWS
8847 if (mod_mask & MOD_MASK_CTRL)
8848 {
8849 /* <C-PageDown>: tab page forward */
8850 goto_tabpage(0);
8851 return;
8852 }
8853#endif
8854
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 tpos = curwin->w_cursor;
8856 if (onepage(FORWARD, 1L) == OK)
8857 {
8858 start_arrow(&tpos);
8859#ifdef FEAT_CINDENT
8860 can_cindent = TRUE;
8861#endif
8862 }
8863 else
8864 vim_beep();
8865}
8866
8867#ifdef FEAT_DND
8868 static void
8869ins_drop()
8870{
8871 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8872}
8873#endif
8874
8875/*
8876 * Handle TAB in Insert or Replace mode.
8877 * Return TRUE when the TAB needs to be inserted like a normal character.
8878 */
8879 static int
8880ins_tab()
8881{
8882 int ind;
8883 int i;
8884 int temp;
8885
8886 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8887 Insstart_blank_vcol = get_nolist_virtcol();
8888 if (echeck_abbr(TAB + ABBR_OFF))
8889 return FALSE;
8890
8891 ind = inindent(0);
8892#ifdef FEAT_CINDENT
8893 if (ind)
8894 can_cindent = FALSE;
8895#endif
8896
8897 /*
8898 * When nothing special, insert TAB like a normal character
8899 */
8900 if (!curbuf->b_p_et
8901 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8902 && curbuf->b_p_sts == 0)
8903 return TRUE;
8904
8905 if (stop_arrow() == FAIL)
8906 return TRUE;
8907
8908 did_ai = FALSE;
8909#ifdef FEAT_SMARTINDENT
8910 did_si = FALSE;
8911 can_si = FALSE;
8912 can_si_back = FALSE;
8913#endif
8914 AppendToRedobuff((char_u *)"\t");
8915
8916 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8917 temp = (int)curbuf->b_p_sw;
8918 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8919 temp = (int)curbuf->b_p_sts;
8920 else /* otherwise use 'tabstop' */
8921 temp = (int)curbuf->b_p_ts;
8922 temp -= get_nolist_virtcol() % temp;
8923
8924 /*
8925 * Insert the first space with ins_char(). It will delete one char in
8926 * replace mode. Insert the rest with ins_str(); it will not delete any
8927 * chars. For VREPLACE mode, we use ins_char() for all characters.
8928 */
8929 ins_char(' ');
8930 while (--temp > 0)
8931 {
8932#ifdef FEAT_VREPLACE
8933 if (State & VREPLACE_FLAG)
8934 ins_char(' ');
8935 else
8936#endif
8937 {
8938 ins_str((char_u *)" ");
8939 if (State & REPLACE_FLAG) /* no char replaced */
8940 replace_push(NUL);
8941 }
8942 }
8943
8944 /*
8945 * When 'expandtab' not set: Replace spaces by TABs where possible.
8946 */
8947 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8948 {
8949 char_u *ptr;
8950#ifdef FEAT_VREPLACE
8951 char_u *saved_line = NULL; /* init for GCC */
8952 pos_T pos;
8953#endif
8954 pos_T fpos;
8955 pos_T *cursor;
8956 colnr_T want_vcol, vcol;
8957 int change_col = -1;
8958 int save_list = curwin->w_p_list;
8959
8960 /*
8961 * Get the current line. For VREPLACE mode, don't make real changes
8962 * yet, just work on a copy of the line.
8963 */
8964#ifdef FEAT_VREPLACE
8965 if (State & VREPLACE_FLAG)
8966 {
8967 pos = curwin->w_cursor;
8968 cursor = &pos;
8969 saved_line = vim_strsave(ml_get_curline());
8970 if (saved_line == NULL)
8971 return FALSE;
8972 ptr = saved_line + pos.col;
8973 }
8974 else
8975#endif
8976 {
8977 ptr = ml_get_cursor();
8978 cursor = &curwin->w_cursor;
8979 }
8980
8981 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8982 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8983 curwin->w_p_list = FALSE;
8984
8985 /* Find first white before the cursor */
8986 fpos = curwin->w_cursor;
8987 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8988 {
8989 --fpos.col;
8990 --ptr;
8991 }
8992
8993 /* In Replace mode, don't change characters before the insert point. */
8994 if ((State & REPLACE_FLAG)
8995 && fpos.lnum == Insstart.lnum
8996 && fpos.col < Insstart.col)
8997 {
8998 ptr += Insstart.col - fpos.col;
8999 fpos.col = Insstart.col;
9000 }
9001
9002 /* compute virtual column numbers of first white and cursor */
9003 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9004 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9005
9006 /* Use as many TABs as possible. Beware of 'showbreak' and
9007 * 'linebreak' adding extra virtual columns. */
9008 while (vim_iswhite(*ptr))
9009 {
9010 i = lbr_chartabsize((char_u *)"\t", vcol);
9011 if (vcol + i > want_vcol)
9012 break;
9013 if (*ptr != TAB)
9014 {
9015 *ptr = TAB;
9016 if (change_col < 0)
9017 {
9018 change_col = fpos.col; /* Column of first change */
9019 /* May have to adjust Insstart */
9020 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9021 Insstart.col = fpos.col;
9022 }
9023 }
9024 ++fpos.col;
9025 ++ptr;
9026 vcol += i;
9027 }
9028
9029 if (change_col >= 0)
9030 {
9031 int repl_off = 0;
9032
9033 /* Skip over the spaces we need. */
9034 while (vcol < want_vcol && *ptr == ' ')
9035 {
9036 vcol += lbr_chartabsize(ptr, vcol);
9037 ++ptr;
9038 ++repl_off;
9039 }
9040 if (vcol > want_vcol)
9041 {
9042 /* Must have a char with 'showbreak' just before it. */
9043 --ptr;
9044 --repl_off;
9045 }
9046 fpos.col += repl_off;
9047
9048 /* Delete following spaces. */
9049 i = cursor->col - fpos.col;
9050 if (i > 0)
9051 {
9052 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
9053 /* correct replace stack. */
9054 if ((State & REPLACE_FLAG)
9055#ifdef FEAT_VREPLACE
9056 && !(State & VREPLACE_FLAG)
9057#endif
9058 )
9059 for (temp = i; --temp >= 0; )
9060 replace_join(repl_off);
9061 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009062#ifdef FEAT_NETBEANS_INTG
9063 if (usingNetbeans)
9064 {
9065 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9066 (long)(i + 1));
9067 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9068 (char_u *)"\t", 1);
9069 }
9070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 cursor->col -= i;
9072
9073#ifdef FEAT_VREPLACE
9074 /*
9075 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9076 * backspacing over the changed spacing and then inserting the new
9077 * spacing.
9078 */
9079 if (State & VREPLACE_FLAG)
9080 {
9081 /* Backspace from real cursor to change_col */
9082 backspace_until_column(change_col);
9083
9084 /* Insert each char in saved_line from changed_col to
9085 * ptr-cursor */
9086 ins_bytes_len(saved_line + change_col,
9087 cursor->col - change_col);
9088 }
9089#endif
9090 }
9091
9092#ifdef FEAT_VREPLACE
9093 if (State & VREPLACE_FLAG)
9094 vim_free(saved_line);
9095#endif
9096 curwin->w_p_list = save_list;
9097 }
9098
9099 return FALSE;
9100}
9101
9102/*
9103 * Handle CR or NL in insert mode.
9104 * Return TRUE when out of memory or can't undo.
9105 */
9106 static int
9107ins_eol(c)
9108 int c;
9109{
9110 int i;
9111
9112 if (echeck_abbr(c + ABBR_OFF))
9113 return FALSE;
9114 if (stop_arrow() == FAIL)
9115 return TRUE;
9116 undisplay_dollar();
9117
9118 /*
9119 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9120 * character under the cursor. Only push a NUL on the replace stack,
9121 * nothing to put back when the NL is deleted.
9122 */
9123 if ((State & REPLACE_FLAG)
9124#ifdef FEAT_VREPLACE
9125 && !(State & VREPLACE_FLAG)
9126#endif
9127 )
9128 replace_push(NUL);
9129
9130 /*
9131 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9132 * replacing the next line, so we push all of the characters left on the
9133 * line onto the replace stack. This is not done here though, it is done
9134 * in open_line().
9135 */
9136
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009137#ifdef FEAT_VIRTUALEDIT
9138 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9139 * CTRL-O). */
9140 if (virtual_active() && curwin->w_cursor.coladd > 0)
9141 coladvance(getviscol());
9142#endif
9143
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144#ifdef FEAT_RIGHTLEFT
9145# ifdef FEAT_FKMAP
9146 if (p_altkeymap && p_fkmap)
9147 fkmap(NL);
9148# endif
9149 /* NL in reverse insert will always start in the end of
9150 * current line. */
9151 if (revins_on)
9152 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9153#endif
9154
9155 AppendToRedobuff(NL_STR);
9156 i = open_line(FORWARD,
9157#ifdef FEAT_COMMENTS
9158 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9159#endif
9160 0, old_indent);
9161 old_indent = 0;
9162#ifdef FEAT_CINDENT
9163 can_cindent = TRUE;
9164#endif
9165
9166 return (!i);
9167}
9168
9169#ifdef FEAT_DIGRAPHS
9170/*
9171 * Handle digraph in insert mode.
9172 * Returns character still to be inserted, or NUL when nothing remaining to be
9173 * done.
9174 */
9175 static int
9176ins_digraph()
9177{
9178 int c;
9179 int cc;
9180
9181 pc_status = PC_STATUS_UNSET;
9182 if (redrawing() && !char_avail())
9183 {
9184 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009185 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186
9187 edit_putchar('?', TRUE);
9188#ifdef FEAT_CMDL_INFO
9189 add_to_showcmd_c(Ctrl_K);
9190#endif
9191 }
9192
9193#ifdef USE_ON_FLY_SCROLL
9194 dont_scroll = TRUE; /* disallow scrolling here */
9195#endif
9196
9197 /* don't map the digraph chars. This also prevents the
9198 * mode message to be deleted when ESC is hit */
9199 ++no_mapping;
9200 ++allow_keys;
9201 c = safe_vgetc();
9202 --no_mapping;
9203 --allow_keys;
9204 if (IS_SPECIAL(c) || mod_mask) /* special key */
9205 {
9206#ifdef FEAT_CMDL_INFO
9207 clear_showcmd();
9208#endif
9209 insert_special(c, TRUE, FALSE);
9210 return NUL;
9211 }
9212 if (c != ESC)
9213 {
9214 if (redrawing() && !char_avail())
9215 {
9216 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009217 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218
9219 if (char2cells(c) == 1)
9220 {
9221 /* first remove the '?', otherwise it's restored when typing
9222 * an ESC next */
9223 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009224 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 edit_putchar(c, TRUE);
9226 }
9227#ifdef FEAT_CMDL_INFO
9228 add_to_showcmd_c(c);
9229#endif
9230 }
9231 ++no_mapping;
9232 ++allow_keys;
9233 cc = safe_vgetc();
9234 --no_mapping;
9235 --allow_keys;
9236 if (cc != ESC)
9237 {
9238 AppendToRedobuff((char_u *)CTRL_V_STR);
9239 c = getdigraph(c, cc, TRUE);
9240#ifdef FEAT_CMDL_INFO
9241 clear_showcmd();
9242#endif
9243 return c;
9244 }
9245 }
9246 edit_unputchar();
9247#ifdef FEAT_CMDL_INFO
9248 clear_showcmd();
9249#endif
9250 return NUL;
9251}
9252#endif
9253
9254/*
9255 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9256 * Returns the char to be inserted, or NUL if none found.
9257 */
9258 static int
9259ins_copychar(lnum)
9260 linenr_T lnum;
9261{
9262 int c;
9263 int temp;
9264 char_u *ptr, *prev_ptr;
9265
9266 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9267 {
9268 vim_beep();
9269 return NUL;
9270 }
9271
9272 /* try to advance to the cursor column */
9273 temp = 0;
9274 ptr = ml_get(lnum);
9275 prev_ptr = ptr;
9276 validate_virtcol();
9277 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9278 {
9279 prev_ptr = ptr;
9280 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9281 }
9282 if ((colnr_T)temp > curwin->w_virtcol)
9283 ptr = prev_ptr;
9284
9285#ifdef FEAT_MBYTE
9286 c = (*mb_ptr2char)(ptr);
9287#else
9288 c = *ptr;
9289#endif
9290 if (c == NUL)
9291 vim_beep();
9292 return c;
9293}
9294
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009295/*
9296 * CTRL-Y or CTRL-E typed in Insert mode.
9297 */
9298 static int
9299ins_ctrl_ey(tc)
9300 int tc;
9301{
9302 int c = tc;
9303
9304#ifdef FEAT_INS_EXPAND
9305 if (ctrl_x_mode == CTRL_X_SCROLL)
9306 {
9307 if (c == Ctrl_Y)
9308 scrolldown_clamp();
9309 else
9310 scrollup_clamp();
9311 redraw_later(VALID);
9312 }
9313 else
9314#endif
9315 {
9316 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9317 if (c != NUL)
9318 {
9319 long tw_save;
9320
9321 /* The character must be taken literally, insert like it
9322 * was typed after a CTRL-V, and pretend 'textwidth'
9323 * wasn't set. Digits, 'o' and 'x' are special after a
9324 * CTRL-V, don't use it for these. */
9325 if (c < 256 && !isalnum(c))
9326 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9327 tw_save = curbuf->b_p_tw;
9328 curbuf->b_p_tw = -1;
9329 insert_special(c, TRUE, FALSE);
9330 curbuf->b_p_tw = tw_save;
9331#ifdef FEAT_RIGHTLEFT
9332 revins_chars++;
9333 revins_legal++;
9334#endif
9335 c = Ctrl_V; /* pretend CTRL-V is last character */
9336 auto_format(FALSE, TRUE);
9337 }
9338 }
9339 return c;
9340}
9341
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342#ifdef FEAT_SMARTINDENT
9343/*
9344 * Try to do some very smart auto-indenting.
9345 * Used when inserting a "normal" character.
9346 */
9347 static void
9348ins_try_si(c)
9349 int c;
9350{
9351 pos_T *pos, old_pos;
9352 char_u *ptr;
9353 int i;
9354 int temp;
9355
9356 /*
9357 * do some very smart indenting when entering '{' or '}'
9358 */
9359 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9360 {
9361 /*
9362 * for '}' set indent equal to indent of line containing matching '{'
9363 */
9364 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9365 {
9366 old_pos = curwin->w_cursor;
9367 /*
9368 * If the matching '{' has a ')' immediately before it (ignoring
9369 * white-space), then line up with the start of the line
9370 * containing the matching '(' if there is one. This handles the
9371 * case where an "if (..\n..) {" statement continues over multiple
9372 * lines -- webb
9373 */
9374 ptr = ml_get(pos->lnum);
9375 i = pos->col;
9376 if (i > 0) /* skip blanks before '{' */
9377 while (--i > 0 && vim_iswhite(ptr[i]))
9378 ;
9379 curwin->w_cursor.lnum = pos->lnum;
9380 curwin->w_cursor.col = i;
9381 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9382 curwin->w_cursor = *pos;
9383 i = get_indent();
9384 curwin->w_cursor = old_pos;
9385#ifdef FEAT_VREPLACE
9386 if (State & VREPLACE_FLAG)
9387 change_indent(INDENT_SET, i, FALSE, NUL);
9388 else
9389#endif
9390 (void)set_indent(i, SIN_CHANGED);
9391 }
9392 else if (curwin->w_cursor.col > 0)
9393 {
9394 /*
9395 * when inserting '{' after "O" reduce indent, but not
9396 * more than indent of previous line
9397 */
9398 temp = TRUE;
9399 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9400 {
9401 old_pos = curwin->w_cursor;
9402 i = get_indent();
9403 while (curwin->w_cursor.lnum > 1)
9404 {
9405 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9406
9407 /* ignore empty lines and lines starting with '#'. */
9408 if (*ptr != '#' && *ptr != NUL)
9409 break;
9410 }
9411 if (get_indent() >= i)
9412 temp = FALSE;
9413 curwin->w_cursor = old_pos;
9414 }
9415 if (temp)
9416 shift_line(TRUE, FALSE, 1);
9417 }
9418 }
9419
9420 /*
9421 * set indent of '#' always to 0
9422 */
9423 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9424 {
9425 /* remember current indent for next line */
9426 old_indent = get_indent();
9427 (void)set_indent(0, SIN_CHANGED);
9428 }
9429
9430 /* Adjust ai_col, the char at this position can be deleted. */
9431 if (ai_col > curwin->w_cursor.col)
9432 ai_col = curwin->w_cursor.col;
9433}
9434#endif
9435
9436/*
9437 * Get the value that w_virtcol would have when 'list' is off.
9438 * Unless 'cpo' contains the 'L' flag.
9439 */
9440 static colnr_T
9441get_nolist_virtcol()
9442{
9443 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9444 return getvcol_nolist(&curwin->w_cursor);
9445 validate_virtcol();
9446 return curwin->w_virtcol;
9447}