blob: 4ee21d5f36177c93d461b22d72b43161d30de403 [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 Moolenaarbe46a1e2006-06-22 15:13:21 +00004694 {
4695 compl_col = curs_col;
4696 compl_length = 0;
4697 vim_free(compl_pattern);
4698 compl_pattern = NULL;
4699 }
4700 else
4701 {
4702 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4703 compl_col = startcol;
4704 compl_length = curs_col - startcol;
4705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004707 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004708 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004709#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004710 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004711 * Call user defined function 'completefunc' with "a:findstart"
4712 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004713 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004714 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004715 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004716 char_u *funcname;
4717 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004718
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004719 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004720 * string */
4721 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4722 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4723 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004724 {
4725 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4726 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004727 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004728 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004729
4730 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004731 args[1] = NULL;
4732 pos = curwin->w_cursor;
4733 col = call_func_retnr(funcname, 2, args, FALSE);
4734 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004735
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004736 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004737 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004738 compl_col = col;
4739 if ((colnr_T)compl_col > curs_col)
4740 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004741
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004742 /* Setup variables for completion. Need to obtain "line" again,
4743 * it may have become invalid. */
4744 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004745 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004746 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4747 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004748#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004749 return FAIL;
4750 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004751 else if (ctrl_x_mode == CTRL_X_SPELL)
4752 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004753#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004754 if (spell_bad_len > 0)
4755 compl_col = curs_col - spell_bad_len;
4756 else
4757 compl_col = spell_word_start(startcol);
4758 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004759 {
4760 compl_length = 0;
4761 compl_col = curs_col;
4762 }
4763 else
4764 {
4765 spell_expand_check_cap(compl_col);
4766 compl_length = (int)curs_col - compl_col;
4767 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004768 /* Need to obtain "line" again, it may have become invalid. */
4769 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004770 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4771 if (compl_pattern == NULL)
4772#endif
4773 return FAIL;
4774 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004775 else
4776 {
4777 EMSG2(_(e_intern2), "ins_complete()");
4778 return FAIL;
4779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004781 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 {
4783 edit_submode_pre = (char_u *)_(" Adding");
4784 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4785 {
4786 /* Insert a new line, keep indentation but ignore 'comments' */
4787#ifdef FEAT_COMMENTS
4788 char_u *old = curbuf->b_p_com;
4789
4790 curbuf->b_p_com = (char_u *)"";
4791#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004792 compl_startpos.lnum = curwin->w_cursor.lnum;
4793 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 ins_eol('\r');
4795#ifdef FEAT_COMMENTS
4796 curbuf->b_p_com = old;
4797#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004798 compl_length = 0;
4799 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 }
4801 }
4802 else
4803 {
4804 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004805 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
4807
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004808 if (compl_cont_status & CONT_LOCAL)
4809 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 else
4811 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4812
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004813 /* Always add completion for the original text. */
4814 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004815 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4816 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004817 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004819 vim_free(compl_pattern);
4820 compl_pattern = NULL;
4821 vim_free(compl_orig_text);
4822 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 return FAIL;
4824 }
4825
4826 /* showmode might reset the internal line pointers, so it must
4827 * be called before line = ml_get(), or when this address is no
4828 * longer needed. -- Acevedo.
4829 */
4830 edit_submode_extra = (char_u *)_("-- Searching...");
4831 edit_submode_highl = HLF_COUNT;
4832 showmode();
4833 edit_submode_extra = NULL;
4834 out_flush();
4835 }
4836
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004837 compl_shown_match = compl_curr_match;
4838 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
4840 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004841 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004843 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004845 /* may undisplay the popup menu */
4846 ins_compl_upd_pum();
4847
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004848 if (n > 1) /* all matches have been found */
4849 compl_matches = n;
4850 compl_curr_match = compl_shown_match;
4851 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852
Bram Moolenaard68071d2006-05-02 22:08:30 +00004853 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4854 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 if (got_int && !global_busy)
4856 {
4857 (void)vgetc();
4858 got_int = FALSE;
4859 }
4860
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004861 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004862 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004864 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4865 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4867 edit_submode_highl = HLF_E;
4868 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4869 * because we couldn't expand anything at first place, but if we used
4870 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4871 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004872 if ( compl_length > 1
4873 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 || (ctrl_x_mode != 0
4875 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4876 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004877 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879
Bram Moolenaar572cb562005-08-05 21:35:02 +00004880 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004881 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004883 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884
4885 if (edit_submode_extra == NULL)
4886 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004887 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 {
4889 edit_submode_extra = (char_u *)_("Back at original");
4890 edit_submode_highl = HLF_W;
4891 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004892 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
4894 edit_submode_extra = (char_u *)_("Word from other line");
4895 edit_submode_highl = HLF_COUNT;
4896 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004897 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 {
4899 edit_submode_extra = (char_u *)_("The only match");
4900 edit_submode_highl = HLF_COUNT;
4901 }
4902 else
4903 {
4904 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004905 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004907 int number = 0;
4908 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004910 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 {
4912 /* search backwards for the first valid (!= -1) number.
4913 * This should normally succeed already at the first loop
4914 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004915 for (match = compl_curr_match->cp_prev; match != NULL
4916 && match != compl_first_match;
4917 match = match->cp_prev)
4918 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004920 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 break;
4922 }
4923 if (match != NULL)
4924 /* go up and assign all numbers which are not assigned
4925 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004926 for (match = match->cp_next;
4927 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004928 match = match->cp_next)
4929 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
4931 else /* BACKWARD */
4932 {
4933 /* search forwards (upwards) for the first valid (!= -1)
4934 * number. This should normally succeed already at the
4935 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004936 for (match = compl_curr_match->cp_next; match != NULL
4937 && match != compl_first_match;
4938 match = match->cp_next)
4939 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004941 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 break;
4943 }
4944 if (match != NULL)
4945 /* go down and assign all numbers which are not
4946 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004947 for (match = match->cp_prev; match
4948 && match->cp_number == -1;
4949 match = match->cp_prev)
4950 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
4952 }
4953
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004954 /* The match should always have a sequence number now, this is
4955 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004956 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
4958 /* Space for 10 text chars. + 2x10-digit no.s */
4959 static char_u match_ref[31];
4960
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004961 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004963 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004965 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004966 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004967 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 edit_submode_extra = match_ref;
4969 edit_submode_highl = HLF_R;
4970 if (dollar_vcol)
4971 curs_columns(FALSE);
4972 }
4973 }
4974 }
4975
4976 /* Show a message about what (completion) mode we're in. */
4977 showmode();
4978 if (edit_submode_extra != NULL)
4979 {
4980 if (!p_smd)
4981 msg_attr(edit_submode_extra,
4982 edit_submode_highl < HLF_COUNT
4983 ? hl_attr(edit_submode_highl) : 0);
4984 }
4985 else
4986 msg_clr_cmdline(); /* necessary for "noshowmode" */
4987
Bram Moolenaard68071d2006-05-02 22:08:30 +00004988 /* Show the popup menu, unless we got interrupted. */
4989 if (!compl_interrupted)
4990 {
4991 /* RedrawingDisabled may be set when invoked through complete(). */
4992 n = RedrawingDisabled;
4993 RedrawingDisabled = 0;
4994 ins_compl_show_pum();
4995 setcursor();
4996 RedrawingDisabled = n;
4997 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004998 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004999 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005000
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 return OK;
5002}
5003
5004/*
5005 * Looks in the first "len" chars. of "src" for search-metachars.
5006 * If dest is not NULL the chars. are copied there quoting (with
5007 * a backslash) the metachars, and dest would be NUL terminated.
5008 * Returns the length (needed) of dest
5009 */
5010 static int
5011quote_meta(dest, src, len)
5012 char_u *dest;
5013 char_u *src;
5014 int len;
5015{
5016 int m;
5017
5018 for (m = len; --len >= 0; src++)
5019 {
5020 switch (*src)
5021 {
5022 case '.':
5023 case '*':
5024 case '[':
5025 if (ctrl_x_mode == CTRL_X_DICTIONARY
5026 || ctrl_x_mode == CTRL_X_THESAURUS)
5027 break;
5028 case '~':
5029 if (!p_magic) /* quote these only if magic is set */
5030 break;
5031 case '\\':
5032 if (ctrl_x_mode == CTRL_X_DICTIONARY
5033 || ctrl_x_mode == CTRL_X_THESAURUS)
5034 break;
5035 case '^': /* currently it's not needed. */
5036 case '$':
5037 m++;
5038 if (dest != NULL)
5039 *dest++ = '\\';
5040 break;
5041 }
5042 if (dest != NULL)
5043 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005044# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 /* Copy remaining bytes of a multibyte character. */
5046 if (has_mbyte)
5047 {
5048 int i, mb_len;
5049
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005050 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 if (mb_len > 0 && len >= mb_len)
5052 for (i = 0; i < mb_len; ++i)
5053 {
5054 --len;
5055 ++src;
5056 if (dest != NULL)
5057 *dest++ = *src;
5058 }
5059 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
5062 if (dest != NULL)
5063 *dest = NUL;
5064
5065 return m;
5066}
5067#endif /* FEAT_INS_EXPAND */
5068
5069/*
5070 * Next character is interpreted literally.
5071 * A one, two or three digit decimal number is interpreted as its byte value.
5072 * If one or two digits are entered, the next character is given to vungetc().
5073 * For Unicode a character > 255 may be returned.
5074 */
5075 int
5076get_literal()
5077{
5078 int cc;
5079 int nc;
5080 int i;
5081 int hex = FALSE;
5082 int octal = FALSE;
5083#ifdef FEAT_MBYTE
5084 int unicode = 0;
5085#endif
5086
5087 if (got_int)
5088 return Ctrl_C;
5089
5090#ifdef FEAT_GUI
5091 /*
5092 * In GUI there is no point inserting the internal code for a special key.
5093 * It is more useful to insert the string "<KEY>" instead. This would
5094 * probably be useful in a text window too, but it would not be
5095 * vi-compatible (maybe there should be an option for it?) -- webb
5096 */
5097 if (gui.in_use)
5098 ++allow_keys;
5099#endif
5100#ifdef USE_ON_FLY_SCROLL
5101 dont_scroll = TRUE; /* disallow scrolling here */
5102#endif
5103 ++no_mapping; /* don't map the next key hits */
5104 cc = 0;
5105 i = 0;
5106 for (;;)
5107 {
5108 do
5109 nc = safe_vgetc();
5110 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
5111 || nc == K_HOR_SCROLLBAR);
5112#ifdef FEAT_CMDL_INFO
5113 if (!(State & CMDLINE)
5114# ifdef FEAT_MBYTE
5115 && MB_BYTE2LEN_CHECK(nc) == 1
5116# endif
5117 )
5118 add_to_showcmd(nc);
5119#endif
5120 if (nc == 'x' || nc == 'X')
5121 hex = TRUE;
5122 else if (nc == 'o' || nc == 'O')
5123 octal = TRUE;
5124#ifdef FEAT_MBYTE
5125 else if (nc == 'u' || nc == 'U')
5126 unicode = nc;
5127#endif
5128 else
5129 {
5130 if (hex
5131#ifdef FEAT_MBYTE
5132 || unicode != 0
5133#endif
5134 )
5135 {
5136 if (!vim_isxdigit(nc))
5137 break;
5138 cc = cc * 16 + hex2nr(nc);
5139 }
5140 else if (octal)
5141 {
5142 if (nc < '0' || nc > '7')
5143 break;
5144 cc = cc * 8 + nc - '0';
5145 }
5146 else
5147 {
5148 if (!VIM_ISDIGIT(nc))
5149 break;
5150 cc = cc * 10 + nc - '0';
5151 }
5152
5153 ++i;
5154 }
5155
5156 if (cc > 255
5157#ifdef FEAT_MBYTE
5158 && unicode == 0
5159#endif
5160 )
5161 cc = 255; /* limit range to 0-255 */
5162 nc = 0;
5163
5164 if (hex) /* hex: up to two chars */
5165 {
5166 if (i >= 2)
5167 break;
5168 }
5169#ifdef FEAT_MBYTE
5170 else if (unicode) /* Unicode: up to four or eight chars */
5171 {
5172 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5173 break;
5174 }
5175#endif
5176 else if (i >= 3) /* decimal or octal: up to three chars */
5177 break;
5178 }
5179 if (i == 0) /* no number entered */
5180 {
5181 if (nc == K_ZERO) /* NUL is stored as NL */
5182 {
5183 cc = '\n';
5184 nc = 0;
5185 }
5186 else
5187 {
5188 cc = nc;
5189 nc = 0;
5190 }
5191 }
5192
5193 if (cc == 0) /* NUL is stored as NL */
5194 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005195#ifdef FEAT_MBYTE
5196 if (enc_dbcs && (cc & 0xff) == 0)
5197 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5198 second byte will cause trouble! */
5199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200
5201 --no_mapping;
5202#ifdef FEAT_GUI
5203 if (gui.in_use)
5204 --allow_keys;
5205#endif
5206 if (nc)
5207 vungetc(nc);
5208 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5209 return cc;
5210}
5211
5212/*
5213 * Insert character, taking care of special keys and mod_mask
5214 */
5215 static void
5216insert_special(c, allow_modmask, ctrlv)
5217 int c;
5218 int allow_modmask;
5219 int ctrlv; /* c was typed after CTRL-V */
5220{
5221 char_u *p;
5222 int len;
5223
5224 /*
5225 * Special function key, translate into "<Key>". Up to the last '>' is
5226 * inserted with ins_str(), so as not to replace characters in replace
5227 * mode.
5228 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5229 * unless 'allow_modmask' is TRUE.
5230 */
5231#ifdef MACOS
5232 /* Command-key never produces a normal key */
5233 if (mod_mask & MOD_MASK_CMD)
5234 allow_modmask = TRUE;
5235#endif
5236 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5237 {
5238 p = get_special_key_name(c, mod_mask);
5239 len = (int)STRLEN(p);
5240 c = p[len - 1];
5241 if (len > 2)
5242 {
5243 if (stop_arrow() == FAIL)
5244 return;
5245 p[len - 1] = NUL;
5246 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005247 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 ctrlv = FALSE;
5249 }
5250 }
5251 if (stop_arrow() == OK)
5252 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5253}
5254
5255/*
5256 * Special characters in this context are those that need processing other
5257 * than the simple insertion that can be performed here. This includes ESC
5258 * which terminates the insert, and CR/NL which need special processing to
5259 * open up a new line. This routine tries to optimize insertions performed by
5260 * the "redo", "undo" or "put" commands, so it needs to know when it should
5261 * stop and defer processing to the "normal" mechanism.
5262 * '0' and '^' are special, because they can be followed by CTRL-D.
5263 */
5264#ifdef EBCDIC
5265# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5266#else
5267# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5268#endif
5269
5270#ifdef FEAT_MBYTE
5271# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5272#else
5273# define WHITECHAR(cc) vim_iswhite(cc)
5274#endif
5275
5276 void
5277insertchar(c, flags, second_indent)
5278 int c; /* character to insert or NUL */
5279 int flags; /* INSCHAR_FORMAT, etc. */
5280 int second_indent; /* indent for second line if >= 0 */
5281{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 int textwidth;
5283#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
5288 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5289 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
5291 /*
5292 * Try to break the line in two or more pieces when:
5293 * - Always do this if we have been called to do formatting only.
5294 * - Always do this when 'formatoptions' has the 'a' flag and the line
5295 * ends in white space.
5296 * - Otherwise:
5297 * - Don't do this if inserting a blank
5298 * - Don't do this if an existing character is being replaced, unless
5299 * we're in VREPLACE mode.
5300 * - Do this if the cursor is not on the line where insert started
5301 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5302 * before the insert.
5303 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5304 * before 'textwidth'
5305 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005306 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 && ((flags & INSCHAR_FORMAT)
5308 || (!vim_iswhite(c)
5309 && !((State & REPLACE_FLAG)
5310#ifdef FEAT_VREPLACE
5311 && !(State & VREPLACE_FLAG)
5312#endif
5313 && *ml_get_cursor() != NUL)
5314 && (curwin->w_cursor.lnum != Insstart.lnum
5315 || ((!has_format_option(FO_INS_LONG)
5316 || Insstart_textlen <= (colnr_T)textwidth)
5317 && (!fo_ins_blank
5318 || Insstart_blank_vcol <= (colnr_T)textwidth
5319 ))))))
5320 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005321 /* Format with 'formatexpr' when it's set. Use internal formatting
5322 * when 'formatexpr' isn't set or it returns non-zero. */
5323#if defined(FEAT_EVAL)
5324 if (*curbuf->b_p_fex == NUL
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005325 || fex_format(curwin->w_cursor.lnum, 1L, c) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005327 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005329
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 if (c == NUL) /* only formatting was wanted */
5331 return;
5332
5333#ifdef FEAT_COMMENTS
5334 /* Check whether this character should end a comment. */
5335 if (did_ai && (int)c == end_comment_pending)
5336 {
5337 char_u *line;
5338 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5339 int middle_len, end_len;
5340 int i;
5341
5342 /*
5343 * Need to remove existing (middle) comment leader and insert end
5344 * comment leader. First, check what comment leader we can find.
5345 */
5346 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5347 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5348 {
5349 /* Skip middle-comment string */
5350 while (*p && p[-1] != ':') /* find end of middle flags */
5351 ++p;
5352 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5353 /* Don't count trailing white space for middle_len */
5354 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5355 --middle_len;
5356
5357 /* Find the end-comment string */
5358 while (*p && p[-1] != ':') /* find end of end flags */
5359 ++p;
5360 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5361
5362 /* Skip white space before the cursor */
5363 i = curwin->w_cursor.col;
5364 while (--i >= 0 && vim_iswhite(line[i]))
5365 ;
5366 i++;
5367
5368 /* Skip to before the middle leader */
5369 i -= middle_len;
5370
5371 /* Check some expected things before we go on */
5372 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5373 {
5374 /* Backspace over all the stuff we want to replace */
5375 backspace_until_column(i);
5376
5377 /*
5378 * Insert the end-comment string, except for the last
5379 * character, which will get inserted as normal later.
5380 */
5381 ins_bytes_len(lead_end, end_len - 1);
5382 }
5383 }
5384 }
5385 end_comment_pending = NUL;
5386#endif
5387
5388 did_ai = FALSE;
5389#ifdef FEAT_SMARTINDENT
5390 did_si = FALSE;
5391 can_si = FALSE;
5392 can_si_back = FALSE;
5393#endif
5394
5395 /*
5396 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5397 * This speeds up normal text input considerably.
5398 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5399 * need to re-indent at a ':', or any other character (but not what
5400 * 'paste' is set)..
5401 */
5402#ifdef USE_ON_FLY_SCROLL
5403 dont_scroll = FALSE; /* allow scrolling here */
5404#endif
5405
5406 if ( !ISSPECIAL(c)
5407#ifdef FEAT_MBYTE
5408 && (!has_mbyte || (*mb_char2len)(c) == 1)
5409#endif
5410 && vpeekc() != NUL
5411 && !(State & REPLACE_FLAG)
5412#ifdef FEAT_CINDENT
5413 && !cindent_on()
5414#endif
5415#ifdef FEAT_RIGHTLEFT
5416 && !p_ri
5417#endif
5418 )
5419 {
5420#define INPUT_BUFLEN 100
5421 char_u buf[INPUT_BUFLEN + 1];
5422 int i;
5423 colnr_T virtcol = 0;
5424
5425 buf[0] = c;
5426 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005427 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 virtcol = get_nolist_virtcol();
5429 /*
5430 * Stop the string when:
5431 * - no more chars available
5432 * - finding a special character (command key)
5433 * - buffer is full
5434 * - running into the 'textwidth' boundary
5435 * - need to check for abbreviation: A non-word char after a word-char
5436 */
5437 while ( (c = vpeekc()) != NUL
5438 && !ISSPECIAL(c)
5439#ifdef FEAT_MBYTE
5440 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5441#endif
5442 && i < INPUT_BUFLEN
5443 && (textwidth == 0
5444 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5445 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5446 {
5447#ifdef FEAT_RIGHTLEFT
5448 c = vgetc();
5449 if (p_hkmap && KeyTyped)
5450 c = hkmap(c); /* Hebrew mode mapping */
5451# ifdef FEAT_FKMAP
5452 if (p_fkmap && KeyTyped)
5453 c = fkmap(c); /* Farsi mode mapping */
5454# endif
5455 buf[i++] = c;
5456#else
5457 buf[i++] = vgetc();
5458#endif
5459 }
5460
5461#ifdef FEAT_DIGRAPHS
5462 do_digraph(-1); /* clear digraphs */
5463 do_digraph(buf[i-1]); /* may be the start of a digraph */
5464#endif
5465 buf[i] = NUL;
5466 ins_str(buf);
5467 if (flags & INSCHAR_CTRLV)
5468 {
5469 redo_literal(*buf);
5470 i = 1;
5471 }
5472 else
5473 i = 0;
5474 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005475 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 }
5477 else
5478 {
5479#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005480 int cc;
5481
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5483 {
5484 char_u buf[MB_MAXBYTES + 1];
5485
5486 (*mb_char2bytes)(c, buf);
5487 buf[cc] = NUL;
5488 ins_char_bytes(buf, cc);
5489 AppendCharToRedobuff(c);
5490 }
5491 else
5492#endif
5493 {
5494 ins_char(c);
5495 if (flags & INSCHAR_CTRLV)
5496 redo_literal(c);
5497 else
5498 AppendCharToRedobuff(c);
5499 }
5500 }
5501}
5502
5503/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005504 * Format text at the current insert position.
5505 */
5506 static void
5507internal_format(textwidth, second_indent, flags, format_only)
5508 int textwidth;
5509 int second_indent;
5510 int flags;
5511 int format_only;
5512{
5513 int cc;
5514 int save_char = NUL;
5515 int haveto_redraw = FALSE;
5516 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5517#ifdef FEAT_MBYTE
5518 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5519#endif
5520 int fo_white_par = has_format_option(FO_WHITE_PAR);
5521 int first_line = TRUE;
5522#ifdef FEAT_COMMENTS
5523 colnr_T leader_len;
5524 int no_leader = FALSE;
5525 int do_comments = (flags & INSCHAR_DO_COM);
5526#endif
5527
5528 /*
5529 * When 'ai' is off we don't want a space under the cursor to be
5530 * deleted. Replace it with an 'x' temporarily.
5531 */
5532 if (!curbuf->b_p_ai)
5533 {
5534 cc = gchar_cursor();
5535 if (vim_iswhite(cc))
5536 {
5537 save_char = cc;
5538 pchar_cursor('x');
5539 }
5540 }
5541
5542 /*
5543 * Repeat breaking lines, until the current line is not too long.
5544 */
5545 while (!got_int)
5546 {
5547 int startcol; /* Cursor column at entry */
5548 int wantcol; /* column at textwidth border */
5549 int foundcol; /* column for start of spaces */
5550 int end_foundcol = 0; /* column for start of word */
5551 colnr_T len;
5552 colnr_T virtcol;
5553#ifdef FEAT_VREPLACE
5554 int orig_col = 0;
5555 char_u *saved_text = NULL;
5556#endif
5557 colnr_T col;
5558
5559 virtcol = get_nolist_virtcol();
5560 if (virtcol < (colnr_T)textwidth)
5561 break;
5562
5563#ifdef FEAT_COMMENTS
5564 if (no_leader)
5565 do_comments = FALSE;
5566 else if (!(flags & INSCHAR_FORMAT)
5567 && has_format_option(FO_WRAP_COMS))
5568 do_comments = TRUE;
5569
5570 /* Don't break until after the comment leader */
5571 if (do_comments)
5572 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5573 else
5574 leader_len = 0;
5575
5576 /* If the line doesn't start with a comment leader, then don't
5577 * start one in a following broken line. Avoids that a %word
5578 * moved to the start of the next line causes all following lines
5579 * to start with %. */
5580 if (leader_len == 0)
5581 no_leader = TRUE;
5582#endif
5583 if (!(flags & INSCHAR_FORMAT)
5584#ifdef FEAT_COMMENTS
5585 && leader_len == 0
5586#endif
5587 && !has_format_option(FO_WRAP))
5588
5589 {
5590 textwidth = 0;
5591 break;
5592 }
5593 if ((startcol = curwin->w_cursor.col) == 0)
5594 break;
5595
5596 /* find column of textwidth border */
5597 coladvance((colnr_T)textwidth);
5598 wantcol = curwin->w_cursor.col;
5599
5600 curwin->w_cursor.col = startcol - 1;
5601#ifdef FEAT_MBYTE
5602 /* Correct cursor for multi-byte character. */
5603 if (has_mbyte)
5604 mb_adjust_cursor();
5605#endif
5606 foundcol = 0;
5607
5608 /*
5609 * Find position to break at.
5610 * Stop at first entered white when 'formatoptions' has 'v'
5611 */
5612 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5613 || curwin->w_cursor.lnum != Insstart.lnum
5614 || curwin->w_cursor.col >= Insstart.col)
5615 {
5616 cc = gchar_cursor();
5617 if (WHITECHAR(cc))
5618 {
5619 /* remember position of blank just before text */
5620 end_foundcol = curwin->w_cursor.col;
5621
5622 /* find start of sequence of blanks */
5623 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5624 {
5625 dec_cursor();
5626 cc = gchar_cursor();
5627 }
5628 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5629 break; /* only spaces in front of text */
5630#ifdef FEAT_COMMENTS
5631 /* Don't break until after the comment leader */
5632 if (curwin->w_cursor.col < leader_len)
5633 break;
5634#endif
5635 if (has_format_option(FO_ONE_LETTER))
5636 {
5637 /* do not break after one-letter words */
5638 if (curwin->w_cursor.col == 0)
5639 break; /* one-letter word at begin */
5640
5641 col = curwin->w_cursor.col;
5642 dec_cursor();
5643 cc = gchar_cursor();
5644
5645 if (WHITECHAR(cc))
5646 continue; /* one-letter, continue */
5647 curwin->w_cursor.col = col;
5648 }
5649#ifdef FEAT_MBYTE
5650 if (has_mbyte)
5651 foundcol = curwin->w_cursor.col
5652 + (*mb_ptr2len)(ml_get_cursor());
5653 else
5654#endif
5655 foundcol = curwin->w_cursor.col + 1;
5656 if (curwin->w_cursor.col < (colnr_T)wantcol)
5657 break;
5658 }
5659#ifdef FEAT_MBYTE
5660 else if (cc >= 0x100 && fo_multibyte
5661 && curwin->w_cursor.col <= (colnr_T)wantcol)
5662 {
5663 /* Break after or before a multi-byte character. */
5664 foundcol = curwin->w_cursor.col;
5665 if (curwin->w_cursor.col < (colnr_T)wantcol)
5666 foundcol += (*mb_char2len)(cc);
5667 end_foundcol = foundcol;
5668 break;
5669 }
5670#endif
5671 if (curwin->w_cursor.col == 0)
5672 break;
5673 dec_cursor();
5674 }
5675
5676 if (foundcol == 0) /* no spaces, cannot break line */
5677 {
5678 curwin->w_cursor.col = startcol;
5679 break;
5680 }
5681
5682 /* Going to break the line, remove any "$" now. */
5683 undisplay_dollar();
5684
5685 /*
5686 * Offset between cursor position and line break is used by replace
5687 * stack functions. VREPLACE does not use this, and backspaces
5688 * over the text instead.
5689 */
5690#ifdef FEAT_VREPLACE
5691 if (State & VREPLACE_FLAG)
5692 orig_col = startcol; /* Will start backspacing from here */
5693 else
5694#endif
5695 replace_offset = startcol - end_foundcol - 1;
5696
5697 /*
5698 * adjust startcol for spaces that will be deleted and
5699 * characters that will remain on top line
5700 */
5701 curwin->w_cursor.col = foundcol;
5702 while (cc = gchar_cursor(), WHITECHAR(cc))
5703 inc_cursor();
5704 startcol -= curwin->w_cursor.col;
5705 if (startcol < 0)
5706 startcol = 0;
5707
5708#ifdef FEAT_VREPLACE
5709 if (State & VREPLACE_FLAG)
5710 {
5711 /*
5712 * In VREPLACE mode, we will backspace over the text to be
5713 * wrapped, so save a copy now to put on the next line.
5714 */
5715 saved_text = vim_strsave(ml_get_cursor());
5716 curwin->w_cursor.col = orig_col;
5717 if (saved_text == NULL)
5718 break; /* Can't do it, out of memory */
5719 saved_text[startcol] = NUL;
5720
5721 /* Backspace over characters that will move to the next line */
5722 if (!fo_white_par)
5723 backspace_until_column(foundcol);
5724 }
5725 else
5726#endif
5727 {
5728 /* put cursor after pos. to break line */
5729 if (!fo_white_par)
5730 curwin->w_cursor.col = foundcol;
5731 }
5732
5733 /*
5734 * Split the line just before the margin.
5735 * Only insert/delete lines, but don't really redraw the window.
5736 */
5737 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5738 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5739#ifdef FEAT_COMMENTS
5740 + (do_comments ? OPENLINE_DO_COM : 0)
5741#endif
5742 , old_indent);
5743 old_indent = 0;
5744
5745 replace_offset = 0;
5746 if (first_line)
5747 {
5748 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5749 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5750 if (second_indent >= 0)
5751 {
5752#ifdef FEAT_VREPLACE
5753 if (State & VREPLACE_FLAG)
5754 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5755 else
5756#endif
5757 (void)set_indent(second_indent, SIN_CHANGED);
5758 }
5759 first_line = FALSE;
5760 }
5761
5762#ifdef FEAT_VREPLACE
5763 if (State & VREPLACE_FLAG)
5764 {
5765 /*
5766 * In VREPLACE mode we have backspaced over the text to be
5767 * moved, now we re-insert it into the new line.
5768 */
5769 ins_bytes(saved_text);
5770 vim_free(saved_text);
5771 }
5772 else
5773#endif
5774 {
5775 /*
5776 * Check if cursor is not past the NUL off the line, cindent
5777 * may have added or removed indent.
5778 */
5779 curwin->w_cursor.col += startcol;
5780 len = (colnr_T)STRLEN(ml_get_curline());
5781 if (curwin->w_cursor.col > len)
5782 curwin->w_cursor.col = len;
5783 }
5784
5785 haveto_redraw = TRUE;
5786#ifdef FEAT_CINDENT
5787 can_cindent = TRUE;
5788#endif
5789 /* moved the cursor, don't autoindent or cindent now */
5790 did_ai = FALSE;
5791#ifdef FEAT_SMARTINDENT
5792 did_si = FALSE;
5793 can_si = FALSE;
5794 can_si_back = FALSE;
5795#endif
5796 line_breakcheck();
5797 }
5798
5799 if (save_char != NUL) /* put back space after cursor */
5800 pchar_cursor(save_char);
5801
5802 if (!format_only && haveto_redraw)
5803 {
5804 update_topline();
5805 redraw_curbuf_later(VALID);
5806 }
5807}
5808
5809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 * Called after inserting or deleting text: When 'formatoptions' includes the
5811 * 'a' flag format from the current line until the end of the paragraph.
5812 * Keep the cursor at the same position relative to the text.
5813 * The caller must have saved the cursor line for undo, following ones will be
5814 * saved here.
5815 */
5816 void
5817auto_format(trailblank, prev_line)
5818 int trailblank; /* when TRUE also format with trailing blank */
5819 int prev_line; /* may start in previous line */
5820{
5821 pos_T pos;
5822 colnr_T len;
5823 char_u *old;
5824 char_u *new, *pnew;
5825 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005826 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827
5828 if (!has_format_option(FO_AUTO))
5829 return;
5830
5831 pos = curwin->w_cursor;
5832 old = ml_get_curline();
5833
5834 /* may remove added space */
5835 check_auto_format(FALSE);
5836
5837 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5838 * user might insert normal text next. Also skip formatting when "1" is
5839 * in 'formatoptions' and there is a single character before the cursor.
5840 * Otherwise the line would be broken and when typing another non-white
5841 * next they are not joined back together. */
5842 wasatend = (pos.col == STRLEN(old));
5843 if (*old != NUL && !trailblank && wasatend)
5844 {
5845 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005846 cc = gchar_cursor();
5847 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5848 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005850 cc = gchar_cursor();
5851 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {
5853 curwin->w_cursor = pos;
5854 return;
5855 }
5856 curwin->w_cursor = pos;
5857 }
5858
5859#ifdef FEAT_COMMENTS
5860 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5861 * comments. */
5862 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5863 && get_leader_len(old, NULL, FALSE) == 0)
5864 return;
5865#endif
5866
5867 /*
5868 * May start formatting in a previous line, so that after "x" a word is
5869 * moved to the previous line if it fits there now. Only when this is not
5870 * the start of a paragraph.
5871 */
5872 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5873 {
5874 --curwin->w_cursor.lnum;
5875 if (u_save_cursor() == FAIL)
5876 return;
5877 }
5878
5879 /*
5880 * Do the formatting and restore the cursor position. "saved_cursor" will
5881 * be adjusted for the text formatting.
5882 */
5883 saved_cursor = pos;
5884 format_lines((linenr_T)-1);
5885 curwin->w_cursor = saved_cursor;
5886 saved_cursor.lnum = 0;
5887
5888 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5889 {
5890 /* "cannot happen" */
5891 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5892 coladvance((colnr_T)MAXCOL);
5893 }
5894 else
5895 check_cursor_col();
5896
5897 /* Insert mode: If the cursor is now after the end of the line while it
5898 * previously wasn't, the line was broken. Because of the rule above we
5899 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5900 * formatted. */
5901 if (!wasatend && has_format_option(FO_WHITE_PAR))
5902 {
5903 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005904 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 if (curwin->w_cursor.col == len)
5906 {
5907 pnew = vim_strnsave(new, len + 2);
5908 pnew[len] = ' ';
5909 pnew[len + 1] = NUL;
5910 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5911 /* remove the space later */
5912 did_add_space = TRUE;
5913 }
5914 else
5915 /* may remove added space */
5916 check_auto_format(FALSE);
5917 }
5918
5919 check_cursor();
5920}
5921
5922/*
5923 * When an extra space was added to continue a paragraph for auto-formatting,
5924 * delete it now. The space must be under the cursor, just after the insert
5925 * position.
5926 */
5927 static void
5928check_auto_format(end_insert)
5929 int end_insert; /* TRUE when ending Insert mode */
5930{
5931 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005932 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933
5934 if (did_add_space)
5935 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005936 cc = gchar_cursor();
5937 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 /* Somehow the space was removed already. */
5939 did_add_space = FALSE;
5940 else
5941 {
5942 if (!end_insert)
5943 {
5944 inc_cursor();
5945 c = gchar_cursor();
5946 dec_cursor();
5947 }
5948 if (c != NUL)
5949 {
5950 /* The space is no longer at the end of the line, delete it. */
5951 del_char(FALSE);
5952 did_add_space = FALSE;
5953 }
5954 }
5955 }
5956}
5957
5958/*
5959 * Find out textwidth to be used for formatting:
5960 * if 'textwidth' option is set, use it
5961 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5962 * if invalid value, use 0.
5963 * Set default to window width (maximum 79) for "gq" operator.
5964 */
5965 int
5966comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005967 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968{
5969 int textwidth;
5970
5971 textwidth = curbuf->b_p_tw;
5972 if (textwidth == 0 && curbuf->b_p_wm)
5973 {
5974 /* The width is the window width minus 'wrapmargin' minus all the
5975 * things that add to the margin. */
5976 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5977#ifdef FEAT_CMDWIN
5978 if (cmdwin_type != 0)
5979 textwidth -= 1;
5980#endif
5981#ifdef FEAT_FOLDING
5982 textwidth -= curwin->w_p_fdc;
5983#endif
5984#ifdef FEAT_SIGNS
5985 if (curwin->w_buffer->b_signlist != NULL
5986# ifdef FEAT_NETBEANS_INTG
5987 || usingNetbeans
5988# endif
5989 )
5990 textwidth -= 1;
5991#endif
5992 if (curwin->w_p_nu)
5993 textwidth -= 8;
5994 }
5995 if (textwidth < 0)
5996 textwidth = 0;
5997 if (ff && textwidth == 0)
5998 {
5999 textwidth = W_WIDTH(curwin) - 1;
6000 if (textwidth > 79)
6001 textwidth = 79;
6002 }
6003 return textwidth;
6004}
6005
6006/*
6007 * Put a character in the redo buffer, for when just after a CTRL-V.
6008 */
6009 static void
6010redo_literal(c)
6011 int c;
6012{
6013 char_u buf[10];
6014
6015 /* Only digits need special treatment. Translate them into a string of
6016 * three digits. */
6017 if (VIM_ISDIGIT(c))
6018 {
6019 sprintf((char *)buf, "%03d", c);
6020 AppendToRedobuff(buf);
6021 }
6022 else
6023 AppendCharToRedobuff(c);
6024}
6025
6026/*
6027 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006028 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 */
6030 static void
6031start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006032 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033{
6034 if (!arrow_used) /* something has been inserted */
6035 {
6036 AppendToRedobuff(ESC_STR);
6037 stop_insert(end_insert_pos, FALSE);
6038 arrow_used = TRUE; /* this means we stopped the current insert */
6039 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006040#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006041 check_spell_redraw();
6042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043}
6044
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006045#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006046/*
6047 * If we skipped highlighting word at cursor, do it now.
6048 * It may be skipped again, thus reset spell_redraw_lnum first.
6049 */
6050 static void
6051check_spell_redraw()
6052{
6053 if (spell_redraw_lnum != 0)
6054 {
6055 linenr_T lnum = spell_redraw_lnum;
6056
6057 spell_redraw_lnum = 0;
6058 redrawWinline(lnum, FALSE);
6059 }
6060}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006061
6062/*
6063 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6064 * spelled word, if there is one.
6065 */
6066 static void
6067spell_back_to_badword()
6068{
6069 pos_T tpos = curwin->w_cursor;
6070
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006071 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006072 if (curwin->w_cursor.col != tpos.col)
6073 start_arrow(&tpos);
6074}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006075#endif
6076
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077/*
6078 * stop_arrow() is called before a change is made in insert mode.
6079 * If an arrow key has been used, start a new insertion.
6080 * Returns FAIL if undo is impossible, shouldn't insert then.
6081 */
6082 int
6083stop_arrow()
6084{
6085 if (arrow_used)
6086 {
6087 if (u_save_cursor() == OK)
6088 {
6089 arrow_used = FALSE;
6090 ins_need_undo = FALSE;
6091 }
6092 Insstart = curwin->w_cursor; /* new insertion starts here */
6093 Insstart_textlen = linetabsize(ml_get_curline());
6094 ai_col = 0;
6095#ifdef FEAT_VREPLACE
6096 if (State & VREPLACE_FLAG)
6097 {
6098 orig_line_count = curbuf->b_ml.ml_line_count;
6099 vr_lines_changed = 1;
6100 }
6101#endif
6102 ResetRedobuff();
6103 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006104 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 }
6106 else if (ins_need_undo)
6107 {
6108 if (u_save_cursor() == OK)
6109 ins_need_undo = FALSE;
6110 }
6111
6112#ifdef FEAT_FOLDING
6113 /* Always open fold at the cursor line when inserting something. */
6114 foldOpenCursor();
6115#endif
6116
6117 return (arrow_used || ins_need_undo ? FAIL : OK);
6118}
6119
6120/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006121 * Do a few things to stop inserting.
6122 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6123 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 */
6125 static void
6126stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006127 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006128 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006130 int cc;
6131 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132
6133 stop_redo_ins();
6134 replace_flush(); /* abandon replace stack */
6135
6136 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006137 * Save the inserted text for later redo with ^@ and CTRL-A.
6138 * Don't do it when "restart_edit" was set and nothing was inserted,
6139 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006141 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006142 if (did_restart_edit == 0 || (ptr != NULL
6143 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006144 {
6145 vim_free(last_insert);
6146 last_insert = ptr;
6147 last_insert_skip = new_insert_skip;
6148 }
6149 else
6150 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006152 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 {
6154 /* Auto-format now. It may seem strange to do this when stopping an
6155 * insertion (or moving the cursor), but it's required when appending
6156 * a line and having it end in a space. But only do it when something
6157 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006158 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006160 pos_T tpos = curwin->w_cursor;
6161
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 /* When the cursor is at the end of the line after a space the
6163 * formatting will move it to the following word. Avoid that by
6164 * moving the cursor onto the space. */
6165 cc = 'x';
6166 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6167 {
6168 dec_cursor();
6169 cc = gchar_cursor();
6170 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006171 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 }
6173
6174 auto_format(TRUE, FALSE);
6175
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006176 if (vim_iswhite(cc))
6177 {
6178 if (gchar_cursor() != NUL)
6179 inc_cursor();
6180#ifdef FEAT_VIRTUALEDIT
6181 /* If the cursor is still at the same character, also keep
6182 * the "coladd". */
6183 if (gchar_cursor() == NUL
6184 && curwin->w_cursor.lnum == tpos.lnum
6185 && curwin->w_cursor.col == tpos.col)
6186 curwin->w_cursor.coladd = tpos.coladd;
6187#endif
6188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 }
6190
6191 /* If a space was inserted for auto-formatting, remove it now. */
6192 check_auto_format(TRUE);
6193
6194 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006195 * of the line, and put the cursor back.
6196 * Do this when ESC was used or moving the cursor up/down. */
6197 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006198 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006200 pos_T tpos = curwin->w_cursor;
6201
6202 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006203 for (;;)
6204 {
6205 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6206 --curwin->w_cursor.col;
6207 cc = gchar_cursor();
6208 if (!vim_iswhite(cc))
6209 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006211 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006212 if (curwin->w_cursor.lnum != tpos.lnum)
6213 curwin->w_cursor = tpos;
6214 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6216
6217#ifdef FEAT_VISUAL
6218 /* <C-S-Right> may have started Visual mode, adjust the position for
6219 * deleted characters. */
6220 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6221 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006222 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (VIsual.col > (colnr_T)cc)
6224 {
6225 VIsual.col = cc;
6226# ifdef FEAT_VIRTUALEDIT
6227 VIsual.coladd = 0;
6228# endif
6229 }
6230 }
6231#endif
6232 }
6233 }
6234 did_ai = FALSE;
6235#ifdef FEAT_SMARTINDENT
6236 did_si = FALSE;
6237 can_si = FALSE;
6238 can_si_back = FALSE;
6239#endif
6240
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006241 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6242 * now in a different buffer. */
6243 if (end_insert_pos != NULL)
6244 {
6245 curbuf->b_op_start = Insstart;
6246 curbuf->b_op_end = *end_insert_pos;
6247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248}
6249
6250/*
6251 * Set the last inserted text to a single character.
6252 * Used for the replace command.
6253 */
6254 void
6255set_last_insert(c)
6256 int c;
6257{
6258 char_u *s;
6259
6260 vim_free(last_insert);
6261#ifdef FEAT_MBYTE
6262 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6263#else
6264 last_insert = alloc(6);
6265#endif
6266 if (last_insert != NULL)
6267 {
6268 s = last_insert;
6269 /* Use the CTRL-V only when entering a special char */
6270 if (c < ' ' || c == DEL)
6271 *s++ = Ctrl_V;
6272 s = add_char2buf(c, s);
6273 *s++ = ESC;
6274 *s++ = NUL;
6275 last_insert_skip = 0;
6276 }
6277}
6278
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006279#if defined(EXITFREE) || defined(PROTO)
6280 void
6281free_last_insert()
6282{
6283 vim_free(last_insert);
6284 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006285 vim_free(compl_orig_text);
6286 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006287}
6288#endif
6289
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290/*
6291 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6292 * and CSI. Handle multi-byte characters.
6293 * Returns a pointer to after the added bytes.
6294 */
6295 char_u *
6296add_char2buf(c, s)
6297 int c;
6298 char_u *s;
6299{
6300#ifdef FEAT_MBYTE
6301 char_u temp[MB_MAXBYTES];
6302 int i;
6303 int len;
6304
6305 len = (*mb_char2bytes)(c, temp);
6306 for (i = 0; i < len; ++i)
6307 {
6308 c = temp[i];
6309#endif
6310 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6311 if (c == K_SPECIAL)
6312 {
6313 *s++ = K_SPECIAL;
6314 *s++ = KS_SPECIAL;
6315 *s++ = KE_FILLER;
6316 }
6317#ifdef FEAT_GUI
6318 else if (c == CSI)
6319 {
6320 *s++ = CSI;
6321 *s++ = KS_EXTRA;
6322 *s++ = (int)KE_CSI;
6323 }
6324#endif
6325 else
6326 *s++ = c;
6327#ifdef FEAT_MBYTE
6328 }
6329#endif
6330 return s;
6331}
6332
6333/*
6334 * move cursor to start of line
6335 * if flags & BL_WHITE move to first non-white
6336 * if flags & BL_SOL move to first non-white if startofline is set,
6337 * otherwise keep "curswant" column
6338 * if flags & BL_FIX don't leave the cursor on a NUL.
6339 */
6340 void
6341beginline(flags)
6342 int flags;
6343{
6344 if ((flags & BL_SOL) && !p_sol)
6345 coladvance(curwin->w_curswant);
6346 else
6347 {
6348 curwin->w_cursor.col = 0;
6349#ifdef FEAT_VIRTUALEDIT
6350 curwin->w_cursor.coladd = 0;
6351#endif
6352
6353 if (flags & (BL_WHITE | BL_SOL))
6354 {
6355 char_u *ptr;
6356
6357 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6358 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6359 ++curwin->w_cursor.col;
6360 }
6361 curwin->w_set_curswant = TRUE;
6362 }
6363}
6364
6365/*
6366 * oneright oneleft cursor_down cursor_up
6367 *
6368 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006369 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 * Return OK when successful, FAIL when we hit a line of file boundary.
6371 */
6372
6373 int
6374oneright()
6375{
6376 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378
6379#ifdef FEAT_VIRTUALEDIT
6380 if (virtual_active())
6381 {
6382 pos_T prevpos = curwin->w_cursor;
6383
6384 /* Adjust for multi-wide char (excluding TAB) */
6385 ptr = ml_get_cursor();
6386 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006387# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006389# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006391# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 ))
6393 ? ptr2cells(ptr) : 1));
6394 curwin->w_set_curswant = TRUE;
6395 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6396 return (prevpos.col != curwin->w_cursor.col
6397 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6398 }
6399#endif
6400
6401 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006402 if (*ptr == NUL)
6403 return FAIL; /* already at the very end */
6404
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006406 if (has_mbyte)
6407 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 else
6409#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006410 l = 1;
6411
6412 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6413 * contains "onemore". */
6414 if (ptr[l] == NUL
6415#ifdef FEAT_VIRTUALEDIT
6416 && (ve_flags & VE_ONEMORE) == 0
6417#endif
6418 )
6419 return FAIL;
6420 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421
6422 curwin->w_set_curswant = TRUE;
6423 return OK;
6424}
6425
6426 int
6427oneleft()
6428{
6429#ifdef FEAT_VIRTUALEDIT
6430 if (virtual_active())
6431 {
6432 int width;
6433 int v = getviscol();
6434
6435 if (v == 0)
6436 return FAIL;
6437
6438# ifdef FEAT_LINEBREAK
6439 /* We might get stuck on 'showbreak', skip over it. */
6440 width = 1;
6441 for (;;)
6442 {
6443 coladvance(v - width);
6444 /* getviscol() is slow, skip it when 'showbreak' is empty and
6445 * there are no multi-byte characters */
6446 if ((*p_sbr == NUL
6447# ifdef FEAT_MBYTE
6448 && !has_mbyte
6449# endif
6450 ) || getviscol() < v)
6451 break;
6452 ++width;
6453 }
6454# else
6455 coladvance(v - 1);
6456# endif
6457
6458 if (curwin->w_cursor.coladd == 1)
6459 {
6460 char_u *ptr;
6461
6462 /* Adjust for multi-wide char (not a TAB) */
6463 ptr = ml_get_cursor();
6464 if (*ptr != TAB && vim_isprintc(
6465# ifdef FEAT_MBYTE
6466 (*mb_ptr2char)(ptr)
6467# else
6468 *ptr
6469# endif
6470 ) && ptr2cells(ptr) > 1)
6471 curwin->w_cursor.coladd = 0;
6472 }
6473
6474 curwin->w_set_curswant = TRUE;
6475 return OK;
6476 }
6477#endif
6478
6479 if (curwin->w_cursor.col == 0)
6480 return FAIL;
6481
6482 curwin->w_set_curswant = TRUE;
6483 --curwin->w_cursor.col;
6484
6485#ifdef FEAT_MBYTE
6486 /* if the character on the left of the current cursor is a multi-byte
6487 * character, move to its first byte */
6488 if (has_mbyte)
6489 mb_adjust_cursor();
6490#endif
6491 return OK;
6492}
6493
6494 int
6495cursor_up(n, upd_topline)
6496 long n;
6497 int upd_topline; /* When TRUE: update topline */
6498{
6499 linenr_T lnum;
6500
6501 if (n > 0)
6502 {
6503 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006504 /* This fails if the cursor is already in the first line or the count
6505 * is larger than the line number and '-' is in 'cpoptions' */
6506 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 return FAIL;
6508 if (n >= lnum)
6509 lnum = 1;
6510 else
6511#ifdef FEAT_FOLDING
6512 if (hasAnyFolding(curwin))
6513 {
6514 /*
6515 * Count each sequence of folded lines as one logical line.
6516 */
6517 /* go to the the start of the current fold */
6518 (void)hasFolding(lnum, &lnum, NULL);
6519
6520 while (n--)
6521 {
6522 /* move up one line */
6523 --lnum;
6524 if (lnum <= 1)
6525 break;
6526 /* If we entered a fold, move to the beginning, unless in
6527 * Insert mode or when 'foldopen' contains "all": it will open
6528 * in a moment. */
6529 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6530 (void)hasFolding(lnum, &lnum, NULL);
6531 }
6532 if (lnum < 1)
6533 lnum = 1;
6534 }
6535 else
6536#endif
6537 lnum -= n;
6538 curwin->w_cursor.lnum = lnum;
6539 }
6540
6541 /* try to advance to the column we want to be at */
6542 coladvance(curwin->w_curswant);
6543
6544 if (upd_topline)
6545 update_topline(); /* make sure curwin->w_topline is valid */
6546
6547 return OK;
6548}
6549
6550/*
6551 * Cursor down a number of logical lines.
6552 */
6553 int
6554cursor_down(n, upd_topline)
6555 long n;
6556 int upd_topline; /* When TRUE: update topline */
6557{
6558 linenr_T lnum;
6559
6560 if (n > 0)
6561 {
6562 lnum = curwin->w_cursor.lnum;
6563#ifdef FEAT_FOLDING
6564 /* Move to last line of fold, will fail if it's the end-of-file. */
6565 (void)hasFolding(lnum, NULL, &lnum);
6566#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006567 /* This fails if the cursor is already in the last line or would move
6568 * beyound the last line and '-' is in 'cpoptions' */
6569 if (lnum >= curbuf->b_ml.ml_line_count
6570 || (lnum + n > curbuf->b_ml.ml_line_count
6571 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 return FAIL;
6573 if (lnum + n >= curbuf->b_ml.ml_line_count)
6574 lnum = curbuf->b_ml.ml_line_count;
6575 else
6576#ifdef FEAT_FOLDING
6577 if (hasAnyFolding(curwin))
6578 {
6579 linenr_T last;
6580
6581 /* count each sequence of folded lines as one logical line */
6582 while (n--)
6583 {
6584 if (hasFolding(lnum, NULL, &last))
6585 lnum = last + 1;
6586 else
6587 ++lnum;
6588 if (lnum >= curbuf->b_ml.ml_line_count)
6589 break;
6590 }
6591 if (lnum > curbuf->b_ml.ml_line_count)
6592 lnum = curbuf->b_ml.ml_line_count;
6593 }
6594 else
6595#endif
6596 lnum += n;
6597 curwin->w_cursor.lnum = lnum;
6598 }
6599
6600 /* try to advance to the column we want to be at */
6601 coladvance(curwin->w_curswant);
6602
6603 if (upd_topline)
6604 update_topline(); /* make sure curwin->w_topline is valid */
6605
6606 return OK;
6607}
6608
6609/*
6610 * Stuff the last inserted text in the read buffer.
6611 * Last_insert actually is a copy of the redo buffer, so we
6612 * first have to remove the command.
6613 */
6614 int
6615stuff_inserted(c, count, no_esc)
6616 int c; /* Command character to be inserted */
6617 long count; /* Repeat this many times */
6618 int no_esc; /* Don't add an ESC at the end */
6619{
6620 char_u *esc_ptr;
6621 char_u *ptr;
6622 char_u *last_ptr;
6623 char_u last = NUL;
6624
6625 ptr = get_last_insert();
6626 if (ptr == NULL)
6627 {
6628 EMSG(_(e_noinstext));
6629 return FAIL;
6630 }
6631
6632 /* may want to stuff the command character, to start Insert mode */
6633 if (c != NUL)
6634 stuffcharReadbuff(c);
6635 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6636 *esc_ptr = NUL; /* remove the ESC */
6637
6638 /* when the last char is either "0" or "^" it will be quoted if no ESC
6639 * comes after it OR if it will inserted more than once and "ptr"
6640 * starts with ^D. -- Acevedo
6641 */
6642 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6643 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6644 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6645 {
6646 last = *last_ptr;
6647 *last_ptr = NUL;
6648 }
6649
6650 do
6651 {
6652 stuffReadbuff(ptr);
6653 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6654 if (last)
6655 stuffReadbuff((char_u *)(last == '0'
6656 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6657 : IF_EB("\026^", CTRL_V_STR "^")));
6658 }
6659 while (--count > 0);
6660
6661 if (last)
6662 *last_ptr = last;
6663
6664 if (esc_ptr != NULL)
6665 *esc_ptr = ESC; /* put the ESC back */
6666
6667 /* may want to stuff a trailing ESC, to get out of Insert mode */
6668 if (!no_esc)
6669 stuffcharReadbuff(ESC);
6670
6671 return OK;
6672}
6673
6674 char_u *
6675get_last_insert()
6676{
6677 if (last_insert == NULL)
6678 return NULL;
6679 return last_insert + last_insert_skip;
6680}
6681
6682/*
6683 * Get last inserted string, and remove trailing <Esc>.
6684 * Returns pointer to allocated memory (must be freed) or NULL.
6685 */
6686 char_u *
6687get_last_insert_save()
6688{
6689 char_u *s;
6690 int len;
6691
6692 if (last_insert == NULL)
6693 return NULL;
6694 s = vim_strsave(last_insert + last_insert_skip);
6695 if (s != NULL)
6696 {
6697 len = (int)STRLEN(s);
6698 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6699 s[len - 1] = NUL;
6700 }
6701 return s;
6702}
6703
6704/*
6705 * Check the word in front of the cursor for an abbreviation.
6706 * Called when the non-id character "c" has been entered.
6707 * When an abbreviation is recognized it is removed from the text and
6708 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6709 */
6710 static int
6711echeck_abbr(c)
6712 int c;
6713{
6714 /* Don't check for abbreviation in paste mode, when disabled and just
6715 * after moving around with cursor keys. */
6716 if (p_paste || no_abbr || arrow_used)
6717 return FALSE;
6718
6719 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6720 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6721}
6722
6723/*
6724 * replace-stack functions
6725 *
6726 * When replacing characters, the replaced characters are remembered for each
6727 * new character. This is used to re-insert the old text when backspacing.
6728 *
6729 * There is a NUL headed list of characters for each character that is
6730 * currently in the file after the insertion point. When BS is used, one NUL
6731 * headed list is put back for the deleted character.
6732 *
6733 * For a newline, there are two NUL headed lists. One contains the characters
6734 * that the NL replaced. The extra one stores the characters after the cursor
6735 * that were deleted (always white space).
6736 *
6737 * Replace_offset is normally 0, in which case replace_push will add a new
6738 * character at the end of the stack. If replace_offset is not 0, that many
6739 * characters will be left on the stack above the newly inserted character.
6740 */
6741
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006742static char_u *replace_stack = NULL;
6743static long replace_stack_nr = 0; /* next entry in replace stack */
6744static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745
6746 void
6747replace_push(c)
6748 int c; /* character that is replaced (NUL is none) */
6749{
6750 char_u *p;
6751
6752 if (replace_stack_nr < replace_offset) /* nothing to do */
6753 return;
6754 if (replace_stack_len <= replace_stack_nr)
6755 {
6756 replace_stack_len += 50;
6757 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6758 if (p == NULL) /* out of memory */
6759 {
6760 replace_stack_len -= 50;
6761 return;
6762 }
6763 if (replace_stack != NULL)
6764 {
6765 mch_memmove(p, replace_stack,
6766 (size_t)(replace_stack_nr * sizeof(char_u)));
6767 vim_free(replace_stack);
6768 }
6769 replace_stack = p;
6770 }
6771 p = replace_stack + replace_stack_nr - replace_offset;
6772 if (replace_offset)
6773 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6774 *p = c;
6775 ++replace_stack_nr;
6776}
6777
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006778#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779/*
6780 * call replace_push(c) with replace_offset set to the first NUL.
6781 */
6782 static void
6783replace_push_off(c)
6784 int c;
6785{
6786 char_u *p;
6787
6788 p = replace_stack + replace_stack_nr;
6789 for (replace_offset = 1; replace_offset < replace_stack_nr;
6790 ++replace_offset)
6791 if (*--p == NUL)
6792 break;
6793 replace_push(c);
6794 replace_offset = 0;
6795}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
6798/*
6799 * Pop one item from the replace stack.
6800 * return -1 if stack empty
6801 * return replaced character or NUL otherwise
6802 */
6803 static int
6804replace_pop()
6805{
6806 if (replace_stack_nr == 0)
6807 return -1;
6808 return (int)replace_stack[--replace_stack_nr];
6809}
6810
6811/*
6812 * Join the top two items on the replace stack. This removes to "off"'th NUL
6813 * encountered.
6814 */
6815 static void
6816replace_join(off)
6817 int off; /* offset for which NUL to remove */
6818{
6819 int i;
6820
6821 for (i = replace_stack_nr; --i >= 0; )
6822 if (replace_stack[i] == NUL && off-- <= 0)
6823 {
6824 --replace_stack_nr;
6825 mch_memmove(replace_stack + i, replace_stack + i + 1,
6826 (size_t)(replace_stack_nr - i));
6827 return;
6828 }
6829}
6830
6831/*
6832 * Pop bytes from the replace stack until a NUL is found, and insert them
6833 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6834 */
6835 static void
6836replace_pop_ins()
6837{
6838 int cc;
6839 int oldState = State;
6840
6841 State = NORMAL; /* don't want REPLACE here */
6842 while ((cc = replace_pop()) > 0)
6843 {
6844#ifdef FEAT_MBYTE
6845 mb_replace_pop_ins(cc);
6846#else
6847 ins_char(cc);
6848#endif
6849 dec_cursor();
6850 }
6851 State = oldState;
6852}
6853
6854#ifdef FEAT_MBYTE
6855/*
6856 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6857 * indicates a multi-byte char, pop the other bytes too.
6858 */
6859 static void
6860mb_replace_pop_ins(cc)
6861 int cc;
6862{
6863 int n;
6864 char_u buf[MB_MAXBYTES];
6865 int i;
6866 int c;
6867
6868 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6869 {
6870 buf[0] = cc;
6871 for (i = 1; i < n; ++i)
6872 buf[i] = replace_pop();
6873 ins_bytes_len(buf, n);
6874 }
6875 else
6876 ins_char(cc);
6877
6878 if (enc_utf8)
6879 /* Handle composing chars. */
6880 for (;;)
6881 {
6882 c = replace_pop();
6883 if (c == -1) /* stack empty */
6884 break;
6885 if ((n = MB_BYTE2LEN(c)) == 1)
6886 {
6887 /* Not a multi-byte char, put it back. */
6888 replace_push(c);
6889 break;
6890 }
6891 else
6892 {
6893 buf[0] = c;
6894 for (i = 1; i < n; ++i)
6895 buf[i] = replace_pop();
6896 if (utf_iscomposing(utf_ptr2char(buf)))
6897 ins_bytes_len(buf, n);
6898 else
6899 {
6900 /* Not a composing char, put it back. */
6901 for (i = n - 1; i >= 0; --i)
6902 replace_push(buf[i]);
6903 break;
6904 }
6905 }
6906 }
6907}
6908#endif
6909
6910/*
6911 * make the replace stack empty
6912 * (called when exiting replace mode)
6913 */
6914 static void
6915replace_flush()
6916{
6917 vim_free(replace_stack);
6918 replace_stack = NULL;
6919 replace_stack_len = 0;
6920 replace_stack_nr = 0;
6921}
6922
6923/*
6924 * Handle doing a BS for one character.
6925 * cc < 0: replace stack empty, just move cursor
6926 * cc == 0: character was inserted, delete it
6927 * cc > 0: character was replaced, put cc (first byte of original char) back
6928 * and check for more characters to be put back
6929 */
6930 static void
6931replace_do_bs()
6932{
6933 int cc;
6934#ifdef FEAT_VREPLACE
6935 int orig_len = 0;
6936 int ins_len;
6937 int orig_vcols = 0;
6938 colnr_T start_vcol;
6939 char_u *p;
6940 int i;
6941 int vcol;
6942#endif
6943
6944 cc = replace_pop();
6945 if (cc > 0)
6946 {
6947#ifdef FEAT_VREPLACE
6948 if (State & VREPLACE_FLAG)
6949 {
6950 /* Get the number of screen cells used by the character we are
6951 * going to delete. */
6952 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6953 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6954 }
6955#endif
6956#ifdef FEAT_MBYTE
6957 if (has_mbyte)
6958 {
6959 del_char(FALSE);
6960# ifdef FEAT_VREPLACE
6961 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006962 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963# endif
6964 replace_push(cc);
6965 }
6966 else
6967#endif
6968 {
6969 pchar_cursor(cc);
6970#ifdef FEAT_VREPLACE
6971 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006972 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973#endif
6974 }
6975 replace_pop_ins();
6976
6977#ifdef FEAT_VREPLACE
6978 if (State & VREPLACE_FLAG)
6979 {
6980 /* Get the number of screen cells used by the inserted characters */
6981 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006982 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 vcol = start_vcol;
6984 for (i = 0; i < ins_len; ++i)
6985 {
6986 vcol += chartabsize(p + i, vcol);
6987#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006988 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989#endif
6990 }
6991 vcol -= start_vcol;
6992
6993 /* Delete spaces that were inserted after the cursor to keep the
6994 * text aligned. */
6995 curwin->w_cursor.col += ins_len;
6996 while (vcol > orig_vcols && gchar_cursor() == ' ')
6997 {
6998 del_char(FALSE);
6999 ++orig_vcols;
7000 }
7001 curwin->w_cursor.col -= ins_len;
7002 }
7003#endif
7004
7005 /* mark the buffer as changed and prepare for displaying */
7006 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7007 }
7008 else if (cc == 0)
7009 (void)del_char(FALSE);
7010}
7011
7012#ifdef FEAT_CINDENT
7013/*
7014 * Return TRUE if C-indenting is on.
7015 */
7016 static int
7017cindent_on()
7018{
7019 return (!p_paste && (curbuf->b_p_cin
7020# ifdef FEAT_EVAL
7021 || *curbuf->b_p_inde != NUL
7022# endif
7023 ));
7024}
7025#endif
7026
7027#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7028/*
7029 * Re-indent the current line, based on the current contents of it and the
7030 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7031 * confused what all the part that handles Control-T is doing that I'm not.
7032 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7033 */
7034
7035 void
7036fixthisline(get_the_indent)
7037 int (*get_the_indent) __ARGS((void));
7038{
7039 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
7040 if (linewhite(curwin->w_cursor.lnum))
7041 did_ai = TRUE; /* delete the indent if the line stays empty */
7042}
7043
7044 void
7045fix_indent()
7046{
7047 if (p_paste)
7048 return;
7049# ifdef FEAT_LISP
7050 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7051 fixthisline(get_lisp_indent);
7052# endif
7053# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7054 else
7055# endif
7056# ifdef FEAT_CINDENT
7057 if (cindent_on())
7058 do_c_expr_indent();
7059# endif
7060}
7061
7062#endif
7063
7064#ifdef FEAT_CINDENT
7065/*
7066 * return TRUE if 'cinkeys' contains the key "keytyped",
7067 * when == '*': Only if key is preceded with '*' (indent before insert)
7068 * when == '!': Only if key is prededed with '!' (don't insert)
7069 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7070 *
7071 * "keytyped" can have a few special values:
7072 * KEY_OPEN_FORW
7073 * KEY_OPEN_BACK
7074 * KEY_COMPLETE just finished completion.
7075 *
7076 * If line_is_empty is TRUE accept keys with '0' before them.
7077 */
7078 int
7079in_cinkeys(keytyped, when, line_is_empty)
7080 int keytyped;
7081 int when;
7082 int line_is_empty;
7083{
7084 char_u *look;
7085 int try_match;
7086 int try_match_word;
7087 char_u *p;
7088 char_u *line;
7089 int icase;
7090 int i;
7091
7092#ifdef FEAT_EVAL
7093 if (*curbuf->b_p_inde != NUL)
7094 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7095 else
7096#endif
7097 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7098 while (*look)
7099 {
7100 /*
7101 * Find out if we want to try a match with this key, depending on
7102 * 'when' and a '*' or '!' before the key.
7103 */
7104 switch (when)
7105 {
7106 case '*': try_match = (*look == '*'); break;
7107 case '!': try_match = (*look == '!'); break;
7108 default: try_match = (*look != '*'); break;
7109 }
7110 if (*look == '*' || *look == '!')
7111 ++look;
7112
7113 /*
7114 * If there is a '0', only accept a match if the line is empty.
7115 * But may still match when typing last char of a word.
7116 */
7117 if (*look == '0')
7118 {
7119 try_match_word = try_match;
7120 if (!line_is_empty)
7121 try_match = FALSE;
7122 ++look;
7123 }
7124 else
7125 try_match_word = FALSE;
7126
7127 /*
7128 * does it look like a control character?
7129 */
7130 if (*look == '^'
7131#ifdef EBCDIC
7132 && (Ctrl_chr(look[1]) != 0)
7133#else
7134 && look[1] >= '?' && look[1] <= '_'
7135#endif
7136 )
7137 {
7138 if (try_match && keytyped == Ctrl_chr(look[1]))
7139 return TRUE;
7140 look += 2;
7141 }
7142 /*
7143 * 'o' means "o" command, open forward.
7144 * 'O' means "O" command, open backward.
7145 */
7146 else if (*look == 'o')
7147 {
7148 if (try_match && keytyped == KEY_OPEN_FORW)
7149 return TRUE;
7150 ++look;
7151 }
7152 else if (*look == 'O')
7153 {
7154 if (try_match && keytyped == KEY_OPEN_BACK)
7155 return TRUE;
7156 ++look;
7157 }
7158
7159 /*
7160 * 'e' means to check for "else" at start of line and just before the
7161 * cursor.
7162 */
7163 else if (*look == 'e')
7164 {
7165 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7166 {
7167 p = ml_get_curline();
7168 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7169 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7170 return TRUE;
7171 }
7172 ++look;
7173 }
7174
7175 /*
7176 * ':' only causes an indent if it is at the end of a label or case
7177 * statement, or when it was before typing the ':' (to fix
7178 * class::method for C++).
7179 */
7180 else if (*look == ':')
7181 {
7182 if (try_match && keytyped == ':')
7183 {
7184 p = ml_get_curline();
7185 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7186 return TRUE;
7187 if (curwin->w_cursor.col > 2
7188 && p[curwin->w_cursor.col - 1] == ':'
7189 && p[curwin->w_cursor.col - 2] == ':')
7190 {
7191 p[curwin->w_cursor.col - 1] = ' ';
7192 i = (cin_iscase(p) || cin_isscopedecl(p)
7193 || cin_islabel(30));
7194 p = ml_get_curline();
7195 p[curwin->w_cursor.col - 1] = ':';
7196 if (i)
7197 return TRUE;
7198 }
7199 }
7200 ++look;
7201 }
7202
7203
7204 /*
7205 * Is it a key in <>, maybe?
7206 */
7207 else if (*look == '<')
7208 {
7209 if (try_match)
7210 {
7211 /*
7212 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7213 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7214 * >, *, : and ! keys if they really really want to.
7215 */
7216 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7217 && keytyped == look[1])
7218 return TRUE;
7219
7220 if (keytyped == get_special_key_code(look + 1))
7221 return TRUE;
7222 }
7223 while (*look && *look != '>')
7224 look++;
7225 while (*look == '>')
7226 look++;
7227 }
7228
7229 /*
7230 * Is it a word: "=word"?
7231 */
7232 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7233 {
7234 ++look;
7235 if (*look == '~')
7236 {
7237 icase = TRUE;
7238 ++look;
7239 }
7240 else
7241 icase = FALSE;
7242 p = vim_strchr(look, ',');
7243 if (p == NULL)
7244 p = look + STRLEN(look);
7245 if ((try_match || try_match_word)
7246 && curwin->w_cursor.col >= (colnr_T)(p - look))
7247 {
7248 int match = FALSE;
7249
7250#ifdef FEAT_INS_EXPAND
7251 if (keytyped == KEY_COMPLETE)
7252 {
7253 char_u *s;
7254
7255 /* Just completed a word, check if it starts with "look".
7256 * search back for the start of a word. */
7257 line = ml_get_curline();
7258# ifdef FEAT_MBYTE
7259 if (has_mbyte)
7260 {
7261 char_u *n;
7262
7263 for (s = line + curwin->w_cursor.col; s > line; s = n)
7264 {
7265 n = mb_prevptr(line, s);
7266 if (!vim_iswordp(n))
7267 break;
7268 }
7269 }
7270 else
7271# endif
7272 for (s = line + curwin->w_cursor.col; s > line; --s)
7273 if (!vim_iswordc(s[-1]))
7274 break;
7275 if (s + (p - look) <= line + curwin->w_cursor.col
7276 && (icase
7277 ? MB_STRNICMP(s, look, p - look)
7278 : STRNCMP(s, look, p - look)) == 0)
7279 match = TRUE;
7280 }
7281 else
7282#endif
7283 /* TODO: multi-byte */
7284 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7285 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7286 {
7287 line = ml_get_cursor();
7288 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7289 || !vim_iswordc(line[-(p - look) - 1]))
7290 && (icase
7291 ? MB_STRNICMP(line - (p - look), look, p - look)
7292 : STRNCMP(line - (p - look), look, p - look))
7293 == 0)
7294 match = TRUE;
7295 }
7296 if (match && try_match_word && !try_match)
7297 {
7298 /* "0=word": Check if there are only blanks before the
7299 * word. */
7300 line = ml_get_curline();
7301 if ((int)(skipwhite(line) - line) !=
7302 (int)(curwin->w_cursor.col - (p - look)))
7303 match = FALSE;
7304 }
7305 if (match)
7306 return TRUE;
7307 }
7308 look = p;
7309 }
7310
7311 /*
7312 * ok, it's a boring generic character.
7313 */
7314 else
7315 {
7316 if (try_match && *look == keytyped)
7317 return TRUE;
7318 ++look;
7319 }
7320
7321 /*
7322 * Skip over ", ".
7323 */
7324 look = skip_to_option_part(look);
7325 }
7326 return FALSE;
7327}
7328#endif /* FEAT_CINDENT */
7329
7330#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7331/*
7332 * Map Hebrew keyboard when in hkmap mode.
7333 */
7334 int
7335hkmap(c)
7336 int c;
7337{
7338 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7339 {
7340 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7341 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7342 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7343 static char_u map[26] =
7344 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7345 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7346 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7347 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7348 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7349 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7350 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7351 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7352 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7353
7354 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7355 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7356 /* '-1'='sofit' */
7357 else if (c == 'x')
7358 return 'X';
7359 else if (c == 'q')
7360 return '\''; /* {geresh}={'} */
7361 else if (c == 246)
7362 return ' '; /* \"o --> ' ' for a german keyboard */
7363 else if (c == 228)
7364 return ' '; /* \"a --> ' ' -- / -- */
7365 else if (c == 252)
7366 return ' '; /* \"u --> ' ' -- / -- */
7367#ifdef EBCDIC
7368 else if (islower(c))
7369#else
7370 /* NOTE: islower() does not do the right thing for us on Linux so we
7371 * do this the same was as 5.7 and previous, so it works correctly on
7372 * all systems. Specifically, the e.g. Delete and Arrow keys are
7373 * munged and won't work if e.g. searching for Hebrew text.
7374 */
7375 else if (c >= 'a' && c <= 'z')
7376#endif
7377 return (int)(map[CharOrdLow(c)] + p_aleph);
7378 else
7379 return c;
7380 }
7381 else
7382 {
7383 switch (c)
7384 {
7385 case '`': return ';';
7386 case '/': return '.';
7387 case '\'': return ',';
7388 case 'q': return '/';
7389 case 'w': return '\'';
7390
7391 /* Hebrew letters - set offset from 'a' */
7392 case ',': c = '{'; break;
7393 case '.': c = 'v'; break;
7394 case ';': c = 't'; break;
7395 default: {
7396 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7397
7398#ifdef EBCDIC
7399 /* see note about islower() above */
7400 if (!islower(c))
7401#else
7402 if (c < 'a' || c > 'z')
7403#endif
7404 return c;
7405 c = str[CharOrdLow(c)];
7406 break;
7407 }
7408 }
7409
7410 return (int)(CharOrdLow(c) + p_aleph);
7411 }
7412}
7413#endif
7414
7415 static void
7416ins_reg()
7417{
7418 int need_redraw = FALSE;
7419 int regname;
7420 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007421#ifdef FEAT_VISUAL
7422 int vis_active = VIsual_active;
7423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424
7425 /*
7426 * If we are going to wait for a character, show a '"'.
7427 */
7428 pc_status = PC_STATUS_UNSET;
7429 if (redrawing() && !char_avail())
7430 {
7431 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007432 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433
7434 edit_putchar('"', TRUE);
7435#ifdef FEAT_CMDL_INFO
7436 add_to_showcmd_c(Ctrl_R);
7437#endif
7438 }
7439
7440#ifdef USE_ON_FLY_SCROLL
7441 dont_scroll = TRUE; /* disallow scrolling here */
7442#endif
7443
7444 /*
7445 * Don't map the register name. This also prevents the mode message to be
7446 * deleted when ESC is hit.
7447 */
7448 ++no_mapping;
7449 regname = safe_vgetc();
7450#ifdef FEAT_LANGMAP
7451 LANGMAP_ADJUST(regname, TRUE);
7452#endif
7453 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7454 {
7455 /* Get a third key for literal register insertion */
7456 literally = regname;
7457#ifdef FEAT_CMDL_INFO
7458 add_to_showcmd_c(literally);
7459#endif
7460 regname = safe_vgetc();
7461#ifdef FEAT_LANGMAP
7462 LANGMAP_ADJUST(regname, TRUE);
7463#endif
7464 }
7465 --no_mapping;
7466
7467#ifdef FEAT_EVAL
7468 /*
7469 * Don't call u_sync() while getting the expression,
7470 * evaluating it or giving an error message for it!
7471 */
7472 ++no_u_sync;
7473 if (regname == '=')
7474 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007475# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007479# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 /* Restore the Input Method. */
7481 if (im_on)
7482 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007483# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007485 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7486 {
7487 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 else
7491 {
7492#endif
7493 if (literally == Ctrl_O || literally == Ctrl_P)
7494 {
7495 /* Append the command to the redo buffer. */
7496 AppendCharToRedobuff(Ctrl_R);
7497 AppendCharToRedobuff(literally);
7498 AppendCharToRedobuff(regname);
7499
7500 do_put(regname, BACKWARD, 1L,
7501 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7502 }
7503 else if (insert_reg(regname, literally) == FAIL)
7504 {
7505 vim_beep();
7506 need_redraw = TRUE; /* remove the '"' */
7507 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007508 else if (stop_insert_mode)
7509 /* When the '=' register was used and a function was invoked that
7510 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7511 * insert anything, need to remove the '"' */
7512 need_redraw = TRUE;
7513
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514#ifdef FEAT_EVAL
7515 }
7516 --no_u_sync;
7517#endif
7518#ifdef FEAT_CMDL_INFO
7519 clear_showcmd();
7520#endif
7521
7522 /* If the inserted register is empty, we need to remove the '"' */
7523 if (need_redraw || stuff_empty())
7524 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007525
7526#ifdef FEAT_VISUAL
7527 /* Disallow starting Visual mode here, would get a weird mode. */
7528 if (!vis_active && VIsual_active)
7529 end_visual_mode();
7530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531}
7532
7533/*
7534 * CTRL-G commands in Insert mode.
7535 */
7536 static void
7537ins_ctrl_g()
7538{
7539 int c;
7540
7541#ifdef FEAT_INS_EXPAND
7542 /* Right after CTRL-X the cursor will be after the ruler. */
7543 setcursor();
7544#endif
7545
7546 /*
7547 * Don't map the second key. This also prevents the mode message to be
7548 * deleted when ESC is hit.
7549 */
7550 ++no_mapping;
7551 c = safe_vgetc();
7552 --no_mapping;
7553 switch (c)
7554 {
7555 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7556 case K_UP:
7557 case Ctrl_K:
7558 case 'k': ins_up(TRUE);
7559 break;
7560
7561 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7562 case K_DOWN:
7563 case Ctrl_J:
7564 case 'j': ins_down(TRUE);
7565 break;
7566
7567 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007568 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007570
7571 /* Need to reset Insstart, esp. because a BS that joins
7572 * aline to the previous one must save for undo. */
7573 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 break;
7575
7576 /* Unknown CTRL-G command, reserved for future expansion. */
7577 default: vim_beep();
7578 }
7579}
7580
7581/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007582 * CTRL-^ in Insert mode.
7583 */
7584 static void
7585ins_ctrl_hat()
7586{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007587 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007588 {
7589 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7590 if (State & LANGMAP)
7591 {
7592 curbuf->b_p_iminsert = B_IMODE_NONE;
7593 State &= ~LANGMAP;
7594 }
7595 else
7596 {
7597 curbuf->b_p_iminsert = B_IMODE_LMAP;
7598 State |= LANGMAP;
7599#ifdef USE_IM_CONTROL
7600 im_set_active(FALSE);
7601#endif
7602 }
7603 }
7604#ifdef USE_IM_CONTROL
7605 else
7606 {
7607 /* There are no ":lmap" mappings, toggle IM */
7608 if (im_get_status())
7609 {
7610 curbuf->b_p_iminsert = B_IMODE_NONE;
7611 im_set_active(FALSE);
7612 }
7613 else
7614 {
7615 curbuf->b_p_iminsert = B_IMODE_IM;
7616 State &= ~LANGMAP;
7617 im_set_active(TRUE);
7618 }
7619 }
7620#endif
7621 set_iminsert_global();
7622 showmode();
7623#ifdef FEAT_GUI
7624 /* may show different cursor shape or color */
7625 if (gui.in_use)
7626 gui_update_cursor(TRUE, FALSE);
7627#endif
7628#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7629 /* Show/unshow value of 'keymap' in status lines. */
7630 status_redraw_curbuf();
7631#endif
7632}
7633
7634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 * Handle ESC in insert mode.
7636 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7637 * insert.
7638 */
7639 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007640ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 long *count;
7642 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007643 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644{
7645 int temp;
7646 static int disabled_redraw = FALSE;
7647
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007648#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007649 check_spell_redraw();
7650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651#if defined(FEAT_HANGULIN)
7652# if defined(ESC_CHG_TO_ENG_MODE)
7653 hangul_input_state_set(0);
7654# endif
7655 if (composing_hangul)
7656 {
7657 push_raw_key(composing_hangul_buffer, 2);
7658 composing_hangul = 0;
7659 }
7660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661
7662 temp = curwin->w_cursor.col;
7663 if (disabled_redraw)
7664 {
7665 --RedrawingDisabled;
7666 disabled_redraw = FALSE;
7667 }
7668 if (!arrow_used)
7669 {
7670 /*
7671 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007672 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7673 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 */
7675 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007676 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
7678 /*
7679 * Repeating insert may take a long time. Check for
7680 * interrupt now and then.
7681 */
7682 if (*count > 0)
7683 {
7684 line_breakcheck();
7685 if (got_int)
7686 *count = 0;
7687 }
7688
7689 if (--*count > 0) /* repeat what was typed */
7690 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007691 /* Vi repeats the insert without replacing characters. */
7692 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7693 State &= ~REPLACE_FLAG;
7694
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 (void)start_redo_ins();
7696 if (cmdchar == 'r' || cmdchar == 'v')
7697 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7698 ++RedrawingDisabled;
7699 disabled_redraw = TRUE;
7700 return FALSE; /* repeat the insert */
7701 }
7702 stop_insert(&curwin->w_cursor, TRUE);
7703 undisplay_dollar();
7704 }
7705
7706 /* When an autoindent was removed, curswant stays after the
7707 * indent */
7708 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7709 curwin->w_set_curswant = TRUE;
7710
7711 /* Remember the last Insert position in the '^ mark. */
7712 if (!cmdmod.keepjumps)
7713 curbuf->b_last_insert = curwin->w_cursor;
7714
7715 /*
7716 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007717 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007719 if (!nomove
7720 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721#ifdef FEAT_VIRTUALEDIT
7722 || curwin->w_cursor.coladd > 0
7723#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007724 )
7725 && (restart_edit == NUL
7726 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007728 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007730 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731#ifdef FEAT_RIGHTLEFT
7732 && !revins_on
7733#endif
7734 )
7735 {
7736#ifdef FEAT_VIRTUALEDIT
7737 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7738 {
7739 oneleft();
7740 if (restart_edit != NUL)
7741 ++curwin->w_cursor.coladd;
7742 }
7743 else
7744#endif
7745 {
7746 --curwin->w_cursor.col;
7747#ifdef FEAT_MBYTE
7748 /* Correct cursor for multi-byte character. */
7749 if (has_mbyte)
7750 mb_adjust_cursor();
7751#endif
7752 }
7753 }
7754
7755#ifdef USE_IM_CONTROL
7756 /* Disable IM to allow typing English directly for Normal mode commands.
7757 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7758 * well). */
7759 if (!(State & LANGMAP))
7760 im_save_status(&curbuf->b_p_iminsert);
7761 im_set_active(FALSE);
7762#endif
7763
7764 State = NORMAL;
7765 /* need to position cursor again (e.g. when on a TAB ) */
7766 changed_cline_bef_curs();
7767
7768#ifdef FEAT_MOUSE
7769 setmouse();
7770#endif
7771#ifdef CURSOR_SHAPE
7772 ui_cursor_shape(); /* may show different cursor shape */
7773#endif
7774
7775 /*
7776 * When recording or for CTRL-O, need to display the new mode.
7777 * Otherwise remove the mode message.
7778 */
7779 if (Recording || restart_edit != NUL)
7780 showmode();
7781 else if (p_smd)
7782 MSG("");
7783
7784 return TRUE; /* exit Insert mode */
7785}
7786
7787#ifdef FEAT_RIGHTLEFT
7788/*
7789 * Toggle language: hkmap and revins_on.
7790 * Move to end of reverse inserted text.
7791 */
7792 static void
7793ins_ctrl_()
7794{
7795 if (revins_on && revins_chars && revins_scol >= 0)
7796 {
7797 while (gchar_cursor() != NUL && revins_chars--)
7798 ++curwin->w_cursor.col;
7799 }
7800 p_ri = !p_ri;
7801 revins_on = (State == INSERT && p_ri);
7802 if (revins_on)
7803 {
7804 revins_scol = curwin->w_cursor.col;
7805 revins_legal++;
7806 revins_chars = 0;
7807 undisplay_dollar();
7808 }
7809 else
7810 revins_scol = -1;
7811#ifdef FEAT_FKMAP
7812 if (p_altkeymap)
7813 {
7814 /*
7815 * to be consistent also for redo command, using '.'
7816 * set arrow_used to true and stop it - causing to redo
7817 * characters entered in one mode (normal/reverse insert).
7818 */
7819 arrow_used = TRUE;
7820 (void)stop_arrow();
7821 p_fkmap = curwin->w_p_rl ^ p_ri;
7822 if (p_fkmap && p_ri)
7823 State = INSERT;
7824 }
7825 else
7826#endif
7827 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7828 showmode();
7829}
7830#endif
7831
7832#ifdef FEAT_VISUAL
7833/*
7834 * If 'keymodel' contains "startsel", may start selection.
7835 * Returns TRUE when a CTRL-O and other keys stuffed.
7836 */
7837 static int
7838ins_start_select(c)
7839 int c;
7840{
7841 if (km_startsel)
7842 switch (c)
7843 {
7844 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 case K_PAGEUP:
7847 case K_KPAGEUP:
7848 case K_PAGEDOWN:
7849 case K_KPAGEDOWN:
7850# ifdef MACOS
7851 case K_LEFT:
7852 case K_RIGHT:
7853 case K_UP:
7854 case K_DOWN:
7855 case K_END:
7856 case K_HOME:
7857# endif
7858 if (!(mod_mask & MOD_MASK_SHIFT))
7859 break;
7860 /* FALLTHROUGH */
7861 case K_S_LEFT:
7862 case K_S_RIGHT:
7863 case K_S_UP:
7864 case K_S_DOWN:
7865 case K_S_END:
7866 case K_S_HOME:
7867 /* Start selection right away, the cursor can move with
7868 * CTRL-O when beyond the end of the line. */
7869 start_selection();
7870
7871 /* Execute the key in (insert) Select mode. */
7872 stuffcharReadbuff(Ctrl_O);
7873 if (mod_mask)
7874 {
7875 char_u buf[4];
7876
7877 buf[0] = K_SPECIAL;
7878 buf[1] = KS_MODIFIER;
7879 buf[2] = mod_mask;
7880 buf[3] = NUL;
7881 stuffReadbuff(buf);
7882 }
7883 stuffcharReadbuff(c);
7884 return TRUE;
7885 }
7886 return FALSE;
7887}
7888#endif
7889
7890/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007891 * <Insert> key in Insert mode: toggle insert/remplace mode.
7892 */
7893 static void
7894ins_insert(replaceState)
7895 int replaceState;
7896{
7897#ifdef FEAT_FKMAP
7898 if (p_fkmap && p_ri)
7899 {
7900 beep_flush();
7901 EMSG(farsi_text_3); /* encoded in Farsi */
7902 return;
7903 }
7904#endif
7905
7906#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007907# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007908 set_vim_var_string(VV_INSERTMODE,
7909 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007910# ifdef FEAT_VREPLACE
7911 replaceState == VREPLACE ? "v" :
7912# endif
7913 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007914# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007915 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7916#endif
7917 if (State & REPLACE_FLAG)
7918 State = INSERT | (State & LANGMAP);
7919 else
7920 State = replaceState | (State & LANGMAP);
7921 AppendCharToRedobuff(K_INS);
7922 showmode();
7923#ifdef CURSOR_SHAPE
7924 ui_cursor_shape(); /* may show different cursor shape */
7925#endif
7926}
7927
7928/*
7929 * Pressed CTRL-O in Insert mode.
7930 */
7931 static void
7932ins_ctrl_o()
7933{
7934#ifdef FEAT_VREPLACE
7935 if (State & VREPLACE_FLAG)
7936 restart_edit = 'V';
7937 else
7938#endif
7939 if (State & REPLACE_FLAG)
7940 restart_edit = 'R';
7941 else
7942 restart_edit = 'I';
7943#ifdef FEAT_VIRTUALEDIT
7944 if (virtual_active())
7945 ins_at_eol = FALSE; /* cursor always keeps its column */
7946 else
7947#endif
7948 ins_at_eol = (gchar_cursor() == NUL);
7949}
7950
7951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 * If the cursor is on an indent, ^T/^D insert/delete one
7953 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7954 * Always round the indent to 'shiftwith', this is compatible
7955 * with vi. But vi only supports ^T and ^D after an
7956 * autoindent, we support it everywhere.
7957 */
7958 static void
7959ins_shift(c, lastc)
7960 int c;
7961 int lastc;
7962{
7963 if (stop_arrow() == FAIL)
7964 return;
7965 AppendCharToRedobuff(c);
7966
7967 /*
7968 * 0^D and ^^D: remove all indent.
7969 */
7970 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7971 {
7972 --curwin->w_cursor.col;
7973 (void)del_char(FALSE); /* delete the '^' or '0' */
7974 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7975 if (State & REPLACE_FLAG)
7976 replace_pop_ins();
7977 if (lastc == '^')
7978 old_indent = get_indent(); /* remember curr. indent */
7979 change_indent(INDENT_SET, 0, TRUE, 0);
7980 }
7981 else
7982 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7983
7984 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7985 did_ai = FALSE;
7986#ifdef FEAT_SMARTINDENT
7987 did_si = FALSE;
7988 can_si = FALSE;
7989 can_si_back = FALSE;
7990#endif
7991#ifdef FEAT_CINDENT
7992 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7993#endif
7994}
7995
7996 static void
7997ins_del()
7998{
7999 int temp;
8000
8001 if (stop_arrow() == FAIL)
8002 return;
8003 if (gchar_cursor() == NUL) /* delete newline */
8004 {
8005 temp = curwin->w_cursor.col;
8006 if (!can_bs(BS_EOL) /* only if "eol" included */
8007 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8008 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8009 || do_join(FALSE) == FAIL)
8010 vim_beep();
8011 else
8012 curwin->w_cursor.col = temp;
8013 }
8014 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8015 vim_beep();
8016 did_ai = FALSE;
8017#ifdef FEAT_SMARTINDENT
8018 did_si = FALSE;
8019 can_si = FALSE;
8020 can_si_back = FALSE;
8021#endif
8022 AppendCharToRedobuff(K_DEL);
8023}
8024
8025/*
8026 * Handle Backspace, delete-word and delete-line in Insert mode.
8027 * Return TRUE when backspace was actually used.
8028 */
8029 static int
8030ins_bs(c, mode, inserted_space_p)
8031 int c;
8032 int mode;
8033 int *inserted_space_p;
8034{
8035 linenr_T lnum;
8036 int cc;
8037 int temp = 0; /* init for GCC */
8038 colnr_T mincol;
8039 int did_backspace = FALSE;
8040 int in_indent;
8041 int oldState;
8042#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008043 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044#endif
8045
8046 /*
8047 * can't delete anything in an empty file
8048 * can't backup past first character in buffer
8049 * can't backup past starting point unless 'backspace' > 1
8050 * can backup to a previous line if 'backspace' == 0
8051 */
8052 if ( bufempty()
8053 || (
8054#ifdef FEAT_RIGHTLEFT
8055 !revins_on &&
8056#endif
8057 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8058 || (!can_bs(BS_START)
8059 && (arrow_used
8060 || (curwin->w_cursor.lnum == Insstart.lnum
8061 && curwin->w_cursor.col <= Insstart.col)))
8062 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8063 && curwin->w_cursor.col <= ai_col)
8064 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8065 {
8066 vim_beep();
8067 return FALSE;
8068 }
8069
8070 if (stop_arrow() == FAIL)
8071 return FALSE;
8072 in_indent = inindent(0);
8073#ifdef FEAT_CINDENT
8074 if (in_indent)
8075 can_cindent = FALSE;
8076#endif
8077#ifdef FEAT_COMMENTS
8078 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8079#endif
8080#ifdef FEAT_RIGHTLEFT
8081 if (revins_on) /* put cursor after last inserted char */
8082 inc_cursor();
8083#endif
8084
8085#ifdef FEAT_VIRTUALEDIT
8086 /* Virtualedit:
8087 * BACKSPACE_CHAR eats a virtual space
8088 * BACKSPACE_WORD eats all coladd
8089 * BACKSPACE_LINE eats all coladd and keeps going
8090 */
8091 if (curwin->w_cursor.coladd > 0)
8092 {
8093 if (mode == BACKSPACE_CHAR)
8094 {
8095 --curwin->w_cursor.coladd;
8096 return TRUE;
8097 }
8098 if (mode == BACKSPACE_WORD)
8099 {
8100 curwin->w_cursor.coladd = 0;
8101 return TRUE;
8102 }
8103 curwin->w_cursor.coladd = 0;
8104 }
8105#endif
8106
8107 /*
8108 * delete newline!
8109 */
8110 if (curwin->w_cursor.col == 0)
8111 {
8112 lnum = Insstart.lnum;
8113 if (curwin->w_cursor.lnum == Insstart.lnum
8114#ifdef FEAT_RIGHTLEFT
8115 || revins_on
8116#endif
8117 )
8118 {
8119 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8120 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8121 return FALSE;
8122 --Insstart.lnum;
8123 Insstart.col = MAXCOL;
8124 }
8125 /*
8126 * In replace mode:
8127 * cc < 0: NL was inserted, delete it
8128 * cc >= 0: NL was replaced, put original characters back
8129 */
8130 cc = -1;
8131 if (State & REPLACE_FLAG)
8132 cc = replace_pop(); /* returns -1 if NL was inserted */
8133 /*
8134 * In replace mode, in the line we started replacing, we only move the
8135 * cursor.
8136 */
8137 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8138 {
8139 dec_cursor();
8140 }
8141 else
8142 {
8143#ifdef FEAT_VREPLACE
8144 if (!(State & VREPLACE_FLAG)
8145 || curwin->w_cursor.lnum > orig_line_count)
8146#endif
8147 {
8148 temp = gchar_cursor(); /* remember current char */
8149 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008150
8151 /* When "aw" is in 'formatoptions' we must delete the space at
8152 * the end of the line, otherwise the line will be broken
8153 * again when auto-formatting. */
8154 if (has_format_option(FO_AUTO)
8155 && has_format_option(FO_WHITE_PAR))
8156 {
8157 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8158 TRUE);
8159 int len;
8160
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008161 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008162 if (len > 0 && ptr[len - 1] == ' ')
8163 ptr[len - 1] = NUL;
8164 }
8165
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 (void)do_join(FALSE);
8167 if (temp == NUL && gchar_cursor() != NUL)
8168 inc_cursor();
8169 }
8170#ifdef FEAT_VREPLACE
8171 else
8172 dec_cursor();
8173#endif
8174
8175 /*
8176 * In REPLACE mode we have to put back the text that was replaced
8177 * by the NL. On the replace stack is first a NUL-terminated
8178 * sequence of characters that were deleted and then the
8179 * characters that NL replaced.
8180 */
8181 if (State & REPLACE_FLAG)
8182 {
8183 /*
8184 * Do the next ins_char() in NORMAL state, to
8185 * prevent ins_char() from replacing characters and
8186 * avoiding showmatch().
8187 */
8188 oldState = State;
8189 State = NORMAL;
8190 /*
8191 * restore characters (blanks) deleted after cursor
8192 */
8193 while (cc > 0)
8194 {
8195 temp = curwin->w_cursor.col;
8196#ifdef FEAT_MBYTE
8197 mb_replace_pop_ins(cc);
8198#else
8199 ins_char(cc);
8200#endif
8201 curwin->w_cursor.col = temp;
8202 cc = replace_pop();
8203 }
8204 /* restore the characters that NL replaced */
8205 replace_pop_ins();
8206 State = oldState;
8207 }
8208 }
8209 did_ai = FALSE;
8210 }
8211 else
8212 {
8213 /*
8214 * Delete character(s) before the cursor.
8215 */
8216#ifdef FEAT_RIGHTLEFT
8217 if (revins_on) /* put cursor on last inserted char */
8218 dec_cursor();
8219#endif
8220 mincol = 0;
8221 /* keep indent */
8222 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
8223#ifdef FEAT_RIGHTLEFT
8224 && !revins_on
8225#endif
8226 )
8227 {
8228 temp = curwin->w_cursor.col;
8229 beginline(BL_WHITE);
8230 if (curwin->w_cursor.col < (colnr_T)temp)
8231 mincol = curwin->w_cursor.col;
8232 curwin->w_cursor.col = temp;
8233 }
8234
8235 /*
8236 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8237 */
8238 if ( mode == BACKSPACE_CHAR
8239 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008240 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 && (*(ml_get_cursor() - 1) == TAB
8242 || (*(ml_get_cursor() - 1) == ' '
8243 && (!*inserted_space_p
8244 || arrow_used))))))
8245 {
8246 int ts;
8247 colnr_T vcol;
8248 colnr_T want_vcol;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008249#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 int extra = 0;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252
8253 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008254 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 ts = curbuf->b_p_sw;
8256 else
8257 ts = curbuf->b_p_sts;
8258 /* Compute the virtual column where we want to be. Since
8259 * 'showbreak' may get in the way, need to get the last column of
8260 * the previous character. */
8261 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8262 dec_cursor();
8263 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8264 inc_cursor();
8265 want_vcol = (want_vcol / ts) * ts;
8266
8267 /* delete characters until we are at or before want_vcol */
8268 while (vcol > want_vcol
8269 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
8270 {
8271 dec_cursor();
8272 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8273 if (State & REPLACE_FLAG)
8274 {
8275 /* Don't delete characters before the insert point when in
8276 * Replace mode */
8277 if (curwin->w_cursor.lnum != Insstart.lnum
8278 || curwin->w_cursor.col >= Insstart.col)
8279 {
8280#if 0 /* what was this for? It causes problems when sw != ts. */
8281 if (State == REPLACE && (int)vcol < want_vcol)
8282 {
8283 (void)del_char(FALSE);
8284 extra = 2; /* don't pop too much */
8285 }
8286 else
8287#endif
8288 replace_do_bs();
8289 }
8290 }
8291 else
8292 (void)del_char(FALSE);
8293 }
8294
8295 /* insert extra spaces until we are at want_vcol */
8296 while (vcol < want_vcol)
8297 {
8298 /* Remember the first char we inserted */
8299 if (curwin->w_cursor.lnum == Insstart.lnum
8300 && curwin->w_cursor.col < Insstart.col)
8301 Insstart.col = curwin->w_cursor.col;
8302
8303#ifdef FEAT_VREPLACE
8304 if (State & VREPLACE_FLAG)
8305 ins_char(' ');
8306 else
8307#endif
8308 {
8309 ins_str((char_u *)" ");
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008310 if ((State & REPLACE_FLAG) /* && extra <= 1 */)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008312#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 if (extra)
8314 replace_push_off(NUL);
8315 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 replace_push(NUL);
8318 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008319#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 if (extra == 2)
8321 extra = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 }
8324 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8325 }
8326 }
8327
8328 /*
8329 * Delete upto starting point, start of line or previous word.
8330 */
8331 else do
8332 {
8333#ifdef FEAT_RIGHTLEFT
8334 if (!revins_on) /* put cursor on char to be deleted */
8335#endif
8336 dec_cursor();
8337
8338 /* start of word? */
8339 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8340 {
8341 mode = BACKSPACE_WORD_NOT_SPACE;
8342 temp = vim_iswordc(gchar_cursor());
8343 }
8344 /* end of word? */
8345 else if (mode == BACKSPACE_WORD_NOT_SPACE
8346 && (vim_isspace(cc = gchar_cursor())
8347 || vim_iswordc(cc) != temp))
8348 {
8349#ifdef FEAT_RIGHTLEFT
8350 if (!revins_on)
8351#endif
8352 inc_cursor();
8353#ifdef FEAT_RIGHTLEFT
8354 else if (State & REPLACE_FLAG)
8355 dec_cursor();
8356#endif
8357 break;
8358 }
8359 if (State & REPLACE_FLAG)
8360 replace_do_bs();
8361 else
8362 {
8363#ifdef FEAT_MBYTE
8364 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008365 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366#endif
8367 (void)del_char(FALSE);
8368#ifdef FEAT_MBYTE
8369 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008370 * If there are combining characters and 'delcombine' is set
8371 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 * character.
8373 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008374 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 inc_cursor();
8376#endif
8377#ifdef FEAT_RIGHTLEFT
8378 if (revins_chars)
8379 {
8380 revins_chars--;
8381 revins_legal++;
8382 }
8383 if (revins_on && gchar_cursor() == NUL)
8384 break;
8385#endif
8386 }
8387 /* Just a single backspace?: */
8388 if (mode == BACKSPACE_CHAR)
8389 break;
8390 } while (
8391#ifdef FEAT_RIGHTLEFT
8392 revins_on ||
8393#endif
8394 (curwin->w_cursor.col > mincol
8395 && (curwin->w_cursor.lnum != Insstart.lnum
8396 || curwin->w_cursor.col != Insstart.col)));
8397 did_backspace = TRUE;
8398 }
8399#ifdef FEAT_SMARTINDENT
8400 did_si = FALSE;
8401 can_si = FALSE;
8402 can_si_back = FALSE;
8403#endif
8404 if (curwin->w_cursor.col <= 1)
8405 did_ai = FALSE;
8406 /*
8407 * It's a little strange to put backspaces into the redo
8408 * buffer, but it makes auto-indent a lot easier to deal
8409 * with.
8410 */
8411 AppendCharToRedobuff(c);
8412
8413 /* If deleted before the insertion point, adjust it */
8414 if (curwin->w_cursor.lnum == Insstart.lnum
8415 && curwin->w_cursor.col < Insstart.col)
8416 Insstart.col = curwin->w_cursor.col;
8417
8418 /* vi behaviour: the cursor moves backward but the character that
8419 * was there remains visible
8420 * Vim behaviour: the cursor moves backward and the character that
8421 * was there is erased from the screen.
8422 * We can emulate the vi behaviour by pretending there is a dollar
8423 * displayed even when there isn't.
8424 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8425 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8426 dollar_vcol = curwin->w_virtcol;
8427
8428 return did_backspace;
8429}
8430
8431#ifdef FEAT_MOUSE
8432 static void
8433ins_mouse(c)
8434 int c;
8435{
8436 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008437 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438
8439# ifdef FEAT_GUI
8440 /* When GUI is active, also move/paste when 'mouse' is empty */
8441 if (!gui.in_use)
8442# endif
8443 if (!mouse_has(MOUSE_INSERT))
8444 return;
8445
8446 undisplay_dollar();
8447 tpos = curwin->w_cursor;
8448 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8449 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008450#ifdef FEAT_WINDOWS
8451 win_T *new_curwin = curwin;
8452
8453 if (curwin != old_curwin && win_valid(old_curwin))
8454 {
8455 /* Mouse took us to another window. We need to go back to the
8456 * previous one to stop insert there properly. */
8457 curwin = old_curwin;
8458 curbuf = curwin->w_buffer;
8459 }
8460#endif
8461 start_arrow(curwin == old_curwin ? &tpos : NULL);
8462#ifdef FEAT_WINDOWS
8463 if (curwin != new_curwin && win_valid(new_curwin))
8464 {
8465 curwin = new_curwin;
8466 curbuf = curwin->w_buffer;
8467 }
8468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469# ifdef FEAT_CINDENT
8470 can_cindent = TRUE;
8471# endif
8472 }
8473
8474#ifdef FEAT_WINDOWS
8475 /* redraw status lines (in case another window became active) */
8476 redraw_statuslines();
8477#endif
8478}
8479
8480 static void
8481ins_mousescroll(up)
8482 int up;
8483{
8484 pos_T tpos;
8485# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8486 win_T *old_curwin;
8487# endif
8488
8489 tpos = curwin->w_cursor;
8490
8491# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8492 old_curwin = curwin;
8493
8494 /* Currently the mouse coordinates are only known in the GUI. */
8495 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8496 {
8497 int row, col;
8498
8499 row = mouse_row;
8500 col = mouse_col;
8501
8502 /* find the window at the pointer coordinates */
8503 curwin = mouse_find_win(&row, &col);
8504 curbuf = curwin->w_buffer;
8505 }
8506 if (curwin == old_curwin)
8507# endif
8508 undisplay_dollar();
8509
8510 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8511 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8512 else
8513 scroll_redraw(up, 3L);
8514
8515# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8516 curwin->w_redr_status = TRUE;
8517
8518 curwin = old_curwin;
8519 curbuf = curwin->w_buffer;
8520# endif
8521
8522 if (!equalpos(curwin->w_cursor, tpos))
8523 {
8524 start_arrow(&tpos);
8525# ifdef FEAT_CINDENT
8526 can_cindent = TRUE;
8527# endif
8528 }
8529}
8530#endif
8531
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008532#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008533 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008534ins_tabline(c)
8535 int c;
8536{
8537 /* We will be leaving the current window, unless closing another tab. */
8538 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8539 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8540 {
8541 undisplay_dollar();
8542 start_arrow(&curwin->w_cursor);
8543# ifdef FEAT_CINDENT
8544 can_cindent = TRUE;
8545# endif
8546 }
8547
8548 if (c == K_TABLINE)
8549 goto_tabpage(current_tab);
8550 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008551 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008552 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008553 redraw_statuslines(); /* will redraw the tabline when needed */
8554 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008555}
8556#endif
8557
8558#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 void
8560ins_scroll()
8561{
8562 pos_T tpos;
8563
8564 undisplay_dollar();
8565 tpos = curwin->w_cursor;
8566 if (gui_do_scroll())
8567 {
8568 start_arrow(&tpos);
8569# ifdef FEAT_CINDENT
8570 can_cindent = TRUE;
8571# endif
8572 }
8573}
8574
8575 void
8576ins_horscroll()
8577{
8578 pos_T tpos;
8579
8580 undisplay_dollar();
8581 tpos = curwin->w_cursor;
8582 if (gui_do_horiz_scroll())
8583 {
8584 start_arrow(&tpos);
8585# ifdef FEAT_CINDENT
8586 can_cindent = TRUE;
8587# endif
8588 }
8589}
8590#endif
8591
8592 static void
8593ins_left()
8594{
8595 pos_T tpos;
8596
8597#ifdef FEAT_FOLDING
8598 if ((fdo_flags & FDO_HOR) && KeyTyped)
8599 foldOpenCursor();
8600#endif
8601 undisplay_dollar();
8602 tpos = curwin->w_cursor;
8603 if (oneleft() == OK)
8604 {
8605 start_arrow(&tpos);
8606#ifdef FEAT_RIGHTLEFT
8607 /* If exit reversed string, position is fixed */
8608 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8609 revins_legal++;
8610 revins_chars++;
8611#endif
8612 }
8613
8614 /*
8615 * if 'whichwrap' set for cursor in insert mode may go to
8616 * previous line
8617 */
8618 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8619 {
8620 start_arrow(&tpos);
8621 --(curwin->w_cursor.lnum);
8622 coladvance((colnr_T)MAXCOL);
8623 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8624 }
8625 else
8626 vim_beep();
8627}
8628
8629 static void
8630ins_home(c)
8631 int c;
8632{
8633 pos_T tpos;
8634
8635#ifdef FEAT_FOLDING
8636 if ((fdo_flags & FDO_HOR) && KeyTyped)
8637 foldOpenCursor();
8638#endif
8639 undisplay_dollar();
8640 tpos = curwin->w_cursor;
8641 if (c == K_C_HOME)
8642 curwin->w_cursor.lnum = 1;
8643 curwin->w_cursor.col = 0;
8644#ifdef FEAT_VIRTUALEDIT
8645 curwin->w_cursor.coladd = 0;
8646#endif
8647 curwin->w_curswant = 0;
8648 start_arrow(&tpos);
8649}
8650
8651 static void
8652ins_end(c)
8653 int c;
8654{
8655 pos_T tpos;
8656
8657#ifdef FEAT_FOLDING
8658 if ((fdo_flags & FDO_HOR) && KeyTyped)
8659 foldOpenCursor();
8660#endif
8661 undisplay_dollar();
8662 tpos = curwin->w_cursor;
8663 if (c == K_C_END)
8664 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8665 coladvance((colnr_T)MAXCOL);
8666 curwin->w_curswant = MAXCOL;
8667
8668 start_arrow(&tpos);
8669}
8670
8671 static void
8672ins_s_left()
8673{
8674#ifdef FEAT_FOLDING
8675 if ((fdo_flags & FDO_HOR) && KeyTyped)
8676 foldOpenCursor();
8677#endif
8678 undisplay_dollar();
8679 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8680 {
8681 start_arrow(&curwin->w_cursor);
8682 (void)bck_word(1L, FALSE, FALSE);
8683 curwin->w_set_curswant = TRUE;
8684 }
8685 else
8686 vim_beep();
8687}
8688
8689 static void
8690ins_right()
8691{
8692#ifdef FEAT_FOLDING
8693 if ((fdo_flags & FDO_HOR) && KeyTyped)
8694 foldOpenCursor();
8695#endif
8696 undisplay_dollar();
8697 if (gchar_cursor() != NUL || virtual_active()
8698 )
8699 {
8700 start_arrow(&curwin->w_cursor);
8701 curwin->w_set_curswant = TRUE;
8702#ifdef FEAT_VIRTUALEDIT
8703 if (virtual_active())
8704 oneright();
8705 else
8706#endif
8707 {
8708#ifdef FEAT_MBYTE
8709 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008710 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 else
8712#endif
8713 ++curwin->w_cursor.col;
8714 }
8715
8716#ifdef FEAT_RIGHTLEFT
8717 revins_legal++;
8718 if (revins_chars)
8719 revins_chars--;
8720#endif
8721 }
8722 /* if 'whichwrap' set for cursor in insert mode, may move the
8723 * cursor to the next line */
8724 else if (vim_strchr(p_ww, ']') != NULL
8725 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8726 {
8727 start_arrow(&curwin->w_cursor);
8728 curwin->w_set_curswant = TRUE;
8729 ++curwin->w_cursor.lnum;
8730 curwin->w_cursor.col = 0;
8731 }
8732 else
8733 vim_beep();
8734}
8735
8736 static void
8737ins_s_right()
8738{
8739#ifdef FEAT_FOLDING
8740 if ((fdo_flags & FDO_HOR) && KeyTyped)
8741 foldOpenCursor();
8742#endif
8743 undisplay_dollar();
8744 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8745 || gchar_cursor() != NUL)
8746 {
8747 start_arrow(&curwin->w_cursor);
8748 (void)fwd_word(1L, FALSE, 0);
8749 curwin->w_set_curswant = TRUE;
8750 }
8751 else
8752 vim_beep();
8753}
8754
8755 static void
8756ins_up(startcol)
8757 int startcol; /* when TRUE move to Insstart.col */
8758{
8759 pos_T tpos;
8760 linenr_T old_topline = curwin->w_topline;
8761#ifdef FEAT_DIFF
8762 int old_topfill = curwin->w_topfill;
8763#endif
8764
8765 undisplay_dollar();
8766 tpos = curwin->w_cursor;
8767 if (cursor_up(1L, TRUE) == OK)
8768 {
8769 if (startcol)
8770 coladvance(getvcol_nolist(&Insstart));
8771 if (old_topline != curwin->w_topline
8772#ifdef FEAT_DIFF
8773 || old_topfill != curwin->w_topfill
8774#endif
8775 )
8776 redraw_later(VALID);
8777 start_arrow(&tpos);
8778#ifdef FEAT_CINDENT
8779 can_cindent = TRUE;
8780#endif
8781 }
8782 else
8783 vim_beep();
8784}
8785
8786 static void
8787ins_pageup()
8788{
8789 pos_T tpos;
8790
8791 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008792
8793#ifdef FEAT_WINDOWS
8794 if (mod_mask & MOD_MASK_CTRL)
8795 {
8796 /* <C-PageUp>: tab page back */
8797 goto_tabpage(-1);
8798 return;
8799 }
8800#endif
8801
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 tpos = curwin->w_cursor;
8803 if (onepage(BACKWARD, 1L) == OK)
8804 {
8805 start_arrow(&tpos);
8806#ifdef FEAT_CINDENT
8807 can_cindent = TRUE;
8808#endif
8809 }
8810 else
8811 vim_beep();
8812}
8813
8814 static void
8815ins_down(startcol)
8816 int startcol; /* when TRUE move to Insstart.col */
8817{
8818 pos_T tpos;
8819 linenr_T old_topline = curwin->w_topline;
8820#ifdef FEAT_DIFF
8821 int old_topfill = curwin->w_topfill;
8822#endif
8823
8824 undisplay_dollar();
8825 tpos = curwin->w_cursor;
8826 if (cursor_down(1L, TRUE) == OK)
8827 {
8828 if (startcol)
8829 coladvance(getvcol_nolist(&Insstart));
8830 if (old_topline != curwin->w_topline
8831#ifdef FEAT_DIFF
8832 || old_topfill != curwin->w_topfill
8833#endif
8834 )
8835 redraw_later(VALID);
8836 start_arrow(&tpos);
8837#ifdef FEAT_CINDENT
8838 can_cindent = TRUE;
8839#endif
8840 }
8841 else
8842 vim_beep();
8843}
8844
8845 static void
8846ins_pagedown()
8847{
8848 pos_T tpos;
8849
8850 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008851
8852#ifdef FEAT_WINDOWS
8853 if (mod_mask & MOD_MASK_CTRL)
8854 {
8855 /* <C-PageDown>: tab page forward */
8856 goto_tabpage(0);
8857 return;
8858 }
8859#endif
8860
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 tpos = curwin->w_cursor;
8862 if (onepage(FORWARD, 1L) == OK)
8863 {
8864 start_arrow(&tpos);
8865#ifdef FEAT_CINDENT
8866 can_cindent = TRUE;
8867#endif
8868 }
8869 else
8870 vim_beep();
8871}
8872
8873#ifdef FEAT_DND
8874 static void
8875ins_drop()
8876{
8877 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8878}
8879#endif
8880
8881/*
8882 * Handle TAB in Insert or Replace mode.
8883 * Return TRUE when the TAB needs to be inserted like a normal character.
8884 */
8885 static int
8886ins_tab()
8887{
8888 int ind;
8889 int i;
8890 int temp;
8891
8892 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8893 Insstart_blank_vcol = get_nolist_virtcol();
8894 if (echeck_abbr(TAB + ABBR_OFF))
8895 return FALSE;
8896
8897 ind = inindent(0);
8898#ifdef FEAT_CINDENT
8899 if (ind)
8900 can_cindent = FALSE;
8901#endif
8902
8903 /*
8904 * When nothing special, insert TAB like a normal character
8905 */
8906 if (!curbuf->b_p_et
8907 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8908 && curbuf->b_p_sts == 0)
8909 return TRUE;
8910
8911 if (stop_arrow() == FAIL)
8912 return TRUE;
8913
8914 did_ai = FALSE;
8915#ifdef FEAT_SMARTINDENT
8916 did_si = FALSE;
8917 can_si = FALSE;
8918 can_si_back = FALSE;
8919#endif
8920 AppendToRedobuff((char_u *)"\t");
8921
8922 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8923 temp = (int)curbuf->b_p_sw;
8924 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8925 temp = (int)curbuf->b_p_sts;
8926 else /* otherwise use 'tabstop' */
8927 temp = (int)curbuf->b_p_ts;
8928 temp -= get_nolist_virtcol() % temp;
8929
8930 /*
8931 * Insert the first space with ins_char(). It will delete one char in
8932 * replace mode. Insert the rest with ins_str(); it will not delete any
8933 * chars. For VREPLACE mode, we use ins_char() for all characters.
8934 */
8935 ins_char(' ');
8936 while (--temp > 0)
8937 {
8938#ifdef FEAT_VREPLACE
8939 if (State & VREPLACE_FLAG)
8940 ins_char(' ');
8941 else
8942#endif
8943 {
8944 ins_str((char_u *)" ");
8945 if (State & REPLACE_FLAG) /* no char replaced */
8946 replace_push(NUL);
8947 }
8948 }
8949
8950 /*
8951 * When 'expandtab' not set: Replace spaces by TABs where possible.
8952 */
8953 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8954 {
8955 char_u *ptr;
8956#ifdef FEAT_VREPLACE
8957 char_u *saved_line = NULL; /* init for GCC */
8958 pos_T pos;
8959#endif
8960 pos_T fpos;
8961 pos_T *cursor;
8962 colnr_T want_vcol, vcol;
8963 int change_col = -1;
8964 int save_list = curwin->w_p_list;
8965
8966 /*
8967 * Get the current line. For VREPLACE mode, don't make real changes
8968 * yet, just work on a copy of the line.
8969 */
8970#ifdef FEAT_VREPLACE
8971 if (State & VREPLACE_FLAG)
8972 {
8973 pos = curwin->w_cursor;
8974 cursor = &pos;
8975 saved_line = vim_strsave(ml_get_curline());
8976 if (saved_line == NULL)
8977 return FALSE;
8978 ptr = saved_line + pos.col;
8979 }
8980 else
8981#endif
8982 {
8983 ptr = ml_get_cursor();
8984 cursor = &curwin->w_cursor;
8985 }
8986
8987 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8988 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8989 curwin->w_p_list = FALSE;
8990
8991 /* Find first white before the cursor */
8992 fpos = curwin->w_cursor;
8993 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8994 {
8995 --fpos.col;
8996 --ptr;
8997 }
8998
8999 /* In Replace mode, don't change characters before the insert point. */
9000 if ((State & REPLACE_FLAG)
9001 && fpos.lnum == Insstart.lnum
9002 && fpos.col < Insstart.col)
9003 {
9004 ptr += Insstart.col - fpos.col;
9005 fpos.col = Insstart.col;
9006 }
9007
9008 /* compute virtual column numbers of first white and cursor */
9009 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9010 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9011
9012 /* Use as many TABs as possible. Beware of 'showbreak' and
9013 * 'linebreak' adding extra virtual columns. */
9014 while (vim_iswhite(*ptr))
9015 {
9016 i = lbr_chartabsize((char_u *)"\t", vcol);
9017 if (vcol + i > want_vcol)
9018 break;
9019 if (*ptr != TAB)
9020 {
9021 *ptr = TAB;
9022 if (change_col < 0)
9023 {
9024 change_col = fpos.col; /* Column of first change */
9025 /* May have to adjust Insstart */
9026 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9027 Insstart.col = fpos.col;
9028 }
9029 }
9030 ++fpos.col;
9031 ++ptr;
9032 vcol += i;
9033 }
9034
9035 if (change_col >= 0)
9036 {
9037 int repl_off = 0;
9038
9039 /* Skip over the spaces we need. */
9040 while (vcol < want_vcol && *ptr == ' ')
9041 {
9042 vcol += lbr_chartabsize(ptr, vcol);
9043 ++ptr;
9044 ++repl_off;
9045 }
9046 if (vcol > want_vcol)
9047 {
9048 /* Must have a char with 'showbreak' just before it. */
9049 --ptr;
9050 --repl_off;
9051 }
9052 fpos.col += repl_off;
9053
9054 /* Delete following spaces. */
9055 i = cursor->col - fpos.col;
9056 if (i > 0)
9057 {
9058 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
9059 /* correct replace stack. */
9060 if ((State & REPLACE_FLAG)
9061#ifdef FEAT_VREPLACE
9062 && !(State & VREPLACE_FLAG)
9063#endif
9064 )
9065 for (temp = i; --temp >= 0; )
9066 replace_join(repl_off);
9067 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009068#ifdef FEAT_NETBEANS_INTG
9069 if (usingNetbeans)
9070 {
9071 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9072 (long)(i + 1));
9073 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9074 (char_u *)"\t", 1);
9075 }
9076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 cursor->col -= i;
9078
9079#ifdef FEAT_VREPLACE
9080 /*
9081 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9082 * backspacing over the changed spacing and then inserting the new
9083 * spacing.
9084 */
9085 if (State & VREPLACE_FLAG)
9086 {
9087 /* Backspace from real cursor to change_col */
9088 backspace_until_column(change_col);
9089
9090 /* Insert each char in saved_line from changed_col to
9091 * ptr-cursor */
9092 ins_bytes_len(saved_line + change_col,
9093 cursor->col - change_col);
9094 }
9095#endif
9096 }
9097
9098#ifdef FEAT_VREPLACE
9099 if (State & VREPLACE_FLAG)
9100 vim_free(saved_line);
9101#endif
9102 curwin->w_p_list = save_list;
9103 }
9104
9105 return FALSE;
9106}
9107
9108/*
9109 * Handle CR or NL in insert mode.
9110 * Return TRUE when out of memory or can't undo.
9111 */
9112 static int
9113ins_eol(c)
9114 int c;
9115{
9116 int i;
9117
9118 if (echeck_abbr(c + ABBR_OFF))
9119 return FALSE;
9120 if (stop_arrow() == FAIL)
9121 return TRUE;
9122 undisplay_dollar();
9123
9124 /*
9125 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9126 * character under the cursor. Only push a NUL on the replace stack,
9127 * nothing to put back when the NL is deleted.
9128 */
9129 if ((State & REPLACE_FLAG)
9130#ifdef FEAT_VREPLACE
9131 && !(State & VREPLACE_FLAG)
9132#endif
9133 )
9134 replace_push(NUL);
9135
9136 /*
9137 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9138 * replacing the next line, so we push all of the characters left on the
9139 * line onto the replace stack. This is not done here though, it is done
9140 * in open_line().
9141 */
9142
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009143#ifdef FEAT_VIRTUALEDIT
9144 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9145 * CTRL-O). */
9146 if (virtual_active() && curwin->w_cursor.coladd > 0)
9147 coladvance(getviscol());
9148#endif
9149
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150#ifdef FEAT_RIGHTLEFT
9151# ifdef FEAT_FKMAP
9152 if (p_altkeymap && p_fkmap)
9153 fkmap(NL);
9154# endif
9155 /* NL in reverse insert will always start in the end of
9156 * current line. */
9157 if (revins_on)
9158 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9159#endif
9160
9161 AppendToRedobuff(NL_STR);
9162 i = open_line(FORWARD,
9163#ifdef FEAT_COMMENTS
9164 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9165#endif
9166 0, old_indent);
9167 old_indent = 0;
9168#ifdef FEAT_CINDENT
9169 can_cindent = TRUE;
9170#endif
9171
9172 return (!i);
9173}
9174
9175#ifdef FEAT_DIGRAPHS
9176/*
9177 * Handle digraph in insert mode.
9178 * Returns character still to be inserted, or NUL when nothing remaining to be
9179 * done.
9180 */
9181 static int
9182ins_digraph()
9183{
9184 int c;
9185 int cc;
9186
9187 pc_status = PC_STATUS_UNSET;
9188 if (redrawing() && !char_avail())
9189 {
9190 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009191 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192
9193 edit_putchar('?', TRUE);
9194#ifdef FEAT_CMDL_INFO
9195 add_to_showcmd_c(Ctrl_K);
9196#endif
9197 }
9198
9199#ifdef USE_ON_FLY_SCROLL
9200 dont_scroll = TRUE; /* disallow scrolling here */
9201#endif
9202
9203 /* don't map the digraph chars. This also prevents the
9204 * mode message to be deleted when ESC is hit */
9205 ++no_mapping;
9206 ++allow_keys;
9207 c = safe_vgetc();
9208 --no_mapping;
9209 --allow_keys;
9210 if (IS_SPECIAL(c) || mod_mask) /* special key */
9211 {
9212#ifdef FEAT_CMDL_INFO
9213 clear_showcmd();
9214#endif
9215 insert_special(c, TRUE, FALSE);
9216 return NUL;
9217 }
9218 if (c != ESC)
9219 {
9220 if (redrawing() && !char_avail())
9221 {
9222 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009223 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224
9225 if (char2cells(c) == 1)
9226 {
9227 /* first remove the '?', otherwise it's restored when typing
9228 * an ESC next */
9229 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009230 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 edit_putchar(c, TRUE);
9232 }
9233#ifdef FEAT_CMDL_INFO
9234 add_to_showcmd_c(c);
9235#endif
9236 }
9237 ++no_mapping;
9238 ++allow_keys;
9239 cc = safe_vgetc();
9240 --no_mapping;
9241 --allow_keys;
9242 if (cc != ESC)
9243 {
9244 AppendToRedobuff((char_u *)CTRL_V_STR);
9245 c = getdigraph(c, cc, TRUE);
9246#ifdef FEAT_CMDL_INFO
9247 clear_showcmd();
9248#endif
9249 return c;
9250 }
9251 }
9252 edit_unputchar();
9253#ifdef FEAT_CMDL_INFO
9254 clear_showcmd();
9255#endif
9256 return NUL;
9257}
9258#endif
9259
9260/*
9261 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9262 * Returns the char to be inserted, or NUL if none found.
9263 */
9264 static int
9265ins_copychar(lnum)
9266 linenr_T lnum;
9267{
9268 int c;
9269 int temp;
9270 char_u *ptr, *prev_ptr;
9271
9272 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9273 {
9274 vim_beep();
9275 return NUL;
9276 }
9277
9278 /* try to advance to the cursor column */
9279 temp = 0;
9280 ptr = ml_get(lnum);
9281 prev_ptr = ptr;
9282 validate_virtcol();
9283 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9284 {
9285 prev_ptr = ptr;
9286 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9287 }
9288 if ((colnr_T)temp > curwin->w_virtcol)
9289 ptr = prev_ptr;
9290
9291#ifdef FEAT_MBYTE
9292 c = (*mb_ptr2char)(ptr);
9293#else
9294 c = *ptr;
9295#endif
9296 if (c == NUL)
9297 vim_beep();
9298 return c;
9299}
9300
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009301/*
9302 * CTRL-Y or CTRL-E typed in Insert mode.
9303 */
9304 static int
9305ins_ctrl_ey(tc)
9306 int tc;
9307{
9308 int c = tc;
9309
9310#ifdef FEAT_INS_EXPAND
9311 if (ctrl_x_mode == CTRL_X_SCROLL)
9312 {
9313 if (c == Ctrl_Y)
9314 scrolldown_clamp();
9315 else
9316 scrollup_clamp();
9317 redraw_later(VALID);
9318 }
9319 else
9320#endif
9321 {
9322 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9323 if (c != NUL)
9324 {
9325 long tw_save;
9326
9327 /* The character must be taken literally, insert like it
9328 * was typed after a CTRL-V, and pretend 'textwidth'
9329 * wasn't set. Digits, 'o' and 'x' are special after a
9330 * CTRL-V, don't use it for these. */
9331 if (c < 256 && !isalnum(c))
9332 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9333 tw_save = curbuf->b_p_tw;
9334 curbuf->b_p_tw = -1;
9335 insert_special(c, TRUE, FALSE);
9336 curbuf->b_p_tw = tw_save;
9337#ifdef FEAT_RIGHTLEFT
9338 revins_chars++;
9339 revins_legal++;
9340#endif
9341 c = Ctrl_V; /* pretend CTRL-V is last character */
9342 auto_format(FALSE, TRUE);
9343 }
9344 }
9345 return c;
9346}
9347
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348#ifdef FEAT_SMARTINDENT
9349/*
9350 * Try to do some very smart auto-indenting.
9351 * Used when inserting a "normal" character.
9352 */
9353 static void
9354ins_try_si(c)
9355 int c;
9356{
9357 pos_T *pos, old_pos;
9358 char_u *ptr;
9359 int i;
9360 int temp;
9361
9362 /*
9363 * do some very smart indenting when entering '{' or '}'
9364 */
9365 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9366 {
9367 /*
9368 * for '}' set indent equal to indent of line containing matching '{'
9369 */
9370 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9371 {
9372 old_pos = curwin->w_cursor;
9373 /*
9374 * If the matching '{' has a ')' immediately before it (ignoring
9375 * white-space), then line up with the start of the line
9376 * containing the matching '(' if there is one. This handles the
9377 * case where an "if (..\n..) {" statement continues over multiple
9378 * lines -- webb
9379 */
9380 ptr = ml_get(pos->lnum);
9381 i = pos->col;
9382 if (i > 0) /* skip blanks before '{' */
9383 while (--i > 0 && vim_iswhite(ptr[i]))
9384 ;
9385 curwin->w_cursor.lnum = pos->lnum;
9386 curwin->w_cursor.col = i;
9387 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9388 curwin->w_cursor = *pos;
9389 i = get_indent();
9390 curwin->w_cursor = old_pos;
9391#ifdef FEAT_VREPLACE
9392 if (State & VREPLACE_FLAG)
9393 change_indent(INDENT_SET, i, FALSE, NUL);
9394 else
9395#endif
9396 (void)set_indent(i, SIN_CHANGED);
9397 }
9398 else if (curwin->w_cursor.col > 0)
9399 {
9400 /*
9401 * when inserting '{' after "O" reduce indent, but not
9402 * more than indent of previous line
9403 */
9404 temp = TRUE;
9405 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9406 {
9407 old_pos = curwin->w_cursor;
9408 i = get_indent();
9409 while (curwin->w_cursor.lnum > 1)
9410 {
9411 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9412
9413 /* ignore empty lines and lines starting with '#'. */
9414 if (*ptr != '#' && *ptr != NUL)
9415 break;
9416 }
9417 if (get_indent() >= i)
9418 temp = FALSE;
9419 curwin->w_cursor = old_pos;
9420 }
9421 if (temp)
9422 shift_line(TRUE, FALSE, 1);
9423 }
9424 }
9425
9426 /*
9427 * set indent of '#' always to 0
9428 */
9429 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9430 {
9431 /* remember current indent for next line */
9432 old_indent = get_indent();
9433 (void)set_indent(0, SIN_CHANGED);
9434 }
9435
9436 /* Adjust ai_col, the char at this position can be deleted. */
9437 if (ai_col > curwin->w_cursor.col)
9438 ai_col = curwin->w_cursor.col;
9439}
9440#endif
9441
9442/*
9443 * Get the value that w_virtcol would have when 'list' is off.
9444 * Unless 'cpo' contains the 'L' flag.
9445 */
9446 static colnr_T
9447get_nolist_virtcol()
9448{
9449 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9450 return getvcol_nolist(&curwin->w_cursor);
9451 validate_virtcol();
9452 return curwin->w_virtcol;
9453}