blob: 9caca851b0ab042103e0e04134cf27eaaca4296f [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 Moolenaard7fd0c42006-08-22 17:55:55 +00002763 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002764 {
2765 ins_compl_files(count, files, thesaurus, flags,
2766 &regmatch, buf, &dir);
2767 if (flags != DICT_EXACT)
2768 FreeWild(count, files);
2769 }
2770 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 break;
2772 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002773
2774theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 p_scs = save_p_scs;
2776 vim_free(regmatch.regprog);
2777 vim_free(buf);
2778}
2779
Bram Moolenaar0b238792006-03-02 22:49:12 +00002780 static void
2781ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2782 int count;
2783 char_u **files;
2784 int thesaurus;
2785 int flags;
2786 regmatch_T *regmatch;
2787 char_u *buf;
2788 int *dir;
2789{
2790 char_u *ptr;
2791 int i;
2792 FILE *fp;
2793 int add_r;
2794
2795 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2796 {
2797 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2798 if (flags != DICT_EXACT)
2799 {
2800 vim_snprintf((char *)IObuff, IOSIZE,
2801 _("Scanning dictionary: %s"), (char *)files[i]);
2802 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2803 }
2804
2805 if (fp != NULL)
2806 {
2807 /*
2808 * Read dictionary file line by line.
2809 * Check each line for a match.
2810 */
2811 while (!got_int && !compl_interrupted
2812 && !vim_fgets(buf, LSIZE, fp))
2813 {
2814 ptr = buf;
2815 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2816 {
2817 ptr = regmatch->startp[0];
2818 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2819 ptr = find_line_end(ptr);
2820 else
2821 ptr = find_word_end(ptr);
2822 add_r = ins_compl_add_infercase(regmatch->startp[0],
2823 (int)(ptr - regmatch->startp[0]),
Bram Moolenaar91170f82006-05-05 21:15:17 +00002824 FALSE, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002825 if (thesaurus)
2826 {
2827 char_u *wstart;
2828
2829 /*
2830 * Add the other matches on the line
2831 */
2832 while (!got_int)
2833 {
2834 /* Find start of the next word. Skip white
2835 * space and punctuation. */
2836 ptr = find_word_start(ptr);
2837 if (*ptr == NUL || *ptr == NL)
2838 break;
2839 wstart = ptr;
2840
2841 /* Find end of the word and add it. */
2842#ifdef FEAT_MBYTE
2843 if (has_mbyte)
2844 /* Japanese words may have characters in
2845 * different classes, only separate words
2846 * with single-byte non-word characters. */
2847 while (*ptr != NUL)
2848 {
2849 int l = (*mb_ptr2len)(ptr);
2850
2851 if (l < 2 && !vim_iswordc(*ptr))
2852 break;
2853 ptr += l;
2854 }
2855 else
2856#endif
2857 ptr = find_word_end(ptr);
2858 add_r = ins_compl_add_infercase(wstart,
2859 (int)(ptr - wstart),
Bram Moolenaar91170f82006-05-05 21:15:17 +00002860 FALSE, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002861 }
2862 }
2863 if (add_r == OK)
2864 /* if dir was BACKWARD then honor it just once */
2865 *dir = FORWARD;
2866 else if (add_r == FAIL)
2867 break;
2868 /* avoid expensive call to vim_regexec() when at end
2869 * of line */
2870 if (*ptr == '\n' || got_int)
2871 break;
2872 }
2873 line_breakcheck();
2874 ins_compl_check_keys(50);
2875 }
2876 fclose(fp);
2877 }
2878 }
2879}
2880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881/*
2882 * Find the start of the next word.
2883 * Returns a pointer to the first char of the word. Also stops at a NUL.
2884 */
2885 char_u *
2886find_word_start(ptr)
2887 char_u *ptr;
2888{
2889#ifdef FEAT_MBYTE
2890 if (has_mbyte)
2891 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002892 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 else
2894#endif
2895 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2896 ++ptr;
2897 return ptr;
2898}
2899
2900/*
2901 * Find the end of the word. Assumes it starts inside a word.
2902 * Returns a pointer to just after the word.
2903 */
2904 char_u *
2905find_word_end(ptr)
2906 char_u *ptr;
2907{
2908#ifdef FEAT_MBYTE
2909 int start_class;
2910
2911 if (has_mbyte)
2912 {
2913 start_class = mb_get_class(ptr);
2914 if (start_class > 1)
2915 while (*ptr != NUL)
2916 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002917 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 if (mb_get_class(ptr) != start_class)
2919 break;
2920 }
2921 }
2922 else
2923#endif
2924 while (vim_iswordc(*ptr))
2925 ++ptr;
2926 return ptr;
2927}
2928
2929/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002930 * Find the end of the line, omitting CR and NL at the end.
2931 * Returns a pointer to just after the line.
2932 */
2933 static char_u *
2934find_line_end(ptr)
2935 char_u *ptr;
2936{
2937 char_u *s;
2938
2939 s = ptr + STRLEN(ptr);
2940 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2941 --s;
2942 return s;
2943}
2944
2945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 * Free the list of completions
2947 */
2948 static void
2949ins_compl_free()
2950{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002951 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002952 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002954 vim_free(compl_pattern);
2955 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002956 vim_free(compl_leader);
2957 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002959 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002961
2962 ins_compl_del_pum();
2963 pum_clear();
2964
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002965 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 do
2967 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002968 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002969 compl_curr_match = compl_curr_match->cp_next;
2970 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002972 if (match->cp_flags & FREE_FNAME)
2973 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00002974 for (i = 0; i < CPT_COUNT; ++i)
2975 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002977 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2978 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979}
2980
2981 static void
2982ins_compl_clear()
2983{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002984 compl_cont_status = 0;
2985 compl_started = FALSE;
2986 compl_matches = 0;
2987 vim_free(compl_pattern);
2988 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002989 vim_free(compl_leader);
2990 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002992 vim_free(compl_orig_text);
2993 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002994 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995}
2996
2997/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002998 * Return TRUE when Insert completion is active.
2999 */
3000 int
3001ins_compl_active()
3002{
3003 return compl_started;
3004}
3005
3006/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003007 * Delete one character before the cursor and show the subset of the matches
3008 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003009 * Returns the character to be used, NUL if the work is done and another char
3010 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003011 */
3012 static int
3013ins_compl_bs()
3014{
3015 char_u *line;
3016 char_u *p;
3017
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003018 line = ml_get_curline();
3019 p = line + curwin->w_cursor.col;
3020 mb_ptr_back(line, p);
3021
3022 /* Stop completion when the whole word was deleted. */
3023 if ((int)(p - line) - (int)compl_col <= 0)
3024 return K_BS;
3025
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003026 /* Deleted more than what was used to find matches or didn't finish
3027 * finding all matches: need to look for matches all over again. */
3028 if (curwin->w_cursor.col <= compl_col + compl_length
3029 || compl_was_interrupted)
3030 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003031
Bram Moolenaara6557602006-02-04 22:43:20 +00003032 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003033 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003034 if (compl_leader != NULL)
3035 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003036 ins_compl_new_leader();
3037 return NUL;
3038 }
3039 return K_BS;
3040}
Bram Moolenaara6557602006-02-04 22:43:20 +00003041
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003042/*
3043 * Called after changing "compl_leader".
3044 * Show the popup menu with a different set of matches.
3045 * May also search for matches again if the previous search was interrupted.
3046 */
3047 static void
3048ins_compl_new_leader()
3049{
3050 ins_compl_del_pum();
3051 ins_compl_delete();
3052 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3053 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003054
3055 if (compl_started)
3056 ins_compl_set_original_text(compl_leader);
3057 else
3058 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003059#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003060 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003061#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003062 /*
3063 * Matches were cleared, need to search for them now. First display
3064 * the changed text before the cursor. Set "compl_restarting" to
3065 * avoid that the first match is inserted.
3066 */
3067 update_screen(0);
3068#ifdef FEAT_GUI
3069 if (gui.in_use)
3070 {
3071 /* Show the cursor after the match, not after the redrawn text. */
3072 setcursor();
3073 out_flush();
3074 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003075 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003076#endif
3077 compl_restarting = TRUE;
3078 if (ins_complete(Ctrl_N) == FAIL)
3079 compl_cont_status = 0;
3080 compl_restarting = FALSE;
3081 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003082
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003083#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003084 if (!compl_used_match)
3085 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003086 /* Go to the original text, since none of the matches is inserted. */
3087 if (compl_first_match->cp_prev != NULL
3088 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3089 compl_shown_match = compl_first_match->cp_prev;
3090 else
3091 compl_shown_match = compl_first_match;
3092 compl_curr_match = compl_shown_match;
3093 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003094 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003095#endif
3096 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003097
3098 /* Show the popup menu with a different set of matches. */
3099 ins_compl_show_pum();
Bram Moolenaara6557602006-02-04 22:43:20 +00003100}
3101
3102/*
3103 * Append one character to the match leader. May reduce the number of
3104 * matches.
3105 */
3106 static void
3107ins_compl_addleader(c)
3108 int c;
3109{
3110#ifdef FEAT_MBYTE
3111 int cc;
3112
3113 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3114 {
3115 char_u buf[MB_MAXBYTES + 1];
3116
3117 (*mb_char2bytes)(c, buf);
3118 buf[cc] = NUL;
3119 ins_char_bytes(buf, cc);
3120 }
3121 else
3122#endif
3123 ins_char(c);
3124
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003125 /* If we didn't complete finding matches we must search again. */
3126 if (compl_was_interrupted)
3127 ins_compl_restart();
3128
Bram Moolenaara6557602006-02-04 22:43:20 +00003129 vim_free(compl_leader);
3130 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3131 curwin->w_cursor.col - compl_col);
3132 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003133 ins_compl_new_leader();
3134}
3135
3136/*
3137 * Setup for finding completions again without leaving CTRL-X mode. Used when
3138 * BS or a key was typed while still searching for matches.
3139 */
3140 static void
3141ins_compl_restart()
3142{
3143 ins_compl_free();
3144 compl_started = FALSE;
3145 compl_matches = 0;
3146 compl_cont_status = 0;
3147 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003148}
3149
3150/*
3151 * Set the first match, the original text.
3152 */
3153 static void
3154ins_compl_set_original_text(str)
3155 char_u *str;
3156{
3157 char_u *p;
3158
3159 /* Replace the original text entry. */
3160 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3161 {
3162 p = vim_strsave(str);
3163 if (p != NULL)
3164 {
3165 vim_free(compl_first_match->cp_str);
3166 compl_first_match->cp_str = p;
3167 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003168 }
3169}
3170
3171/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003172 * Append one character to the match leader. May reduce the number of
3173 * matches.
3174 */
3175 static void
3176ins_compl_addfrommatch()
3177{
3178 char_u *p;
3179 int len = curwin->w_cursor.col - compl_col;
3180 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003181 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003182
3183 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003184 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003185 {
3186 /* When still at the original match use the first entry that matches
3187 * the leader. */
3188 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3189 {
3190 p = NULL;
3191 for (cp = compl_shown_match->cp_next; cp != NULL
3192 && cp != compl_first_match; cp = cp->cp_next)
3193 {
3194 if (ins_compl_equal(cp, compl_leader,
3195 (int)STRLEN(compl_leader)))
3196 {
3197 p = cp->cp_str;
3198 break;
3199 }
3200 }
3201 if (p == NULL || (int)STRLEN(p) <= len)
3202 return;
3203 }
3204 else
3205 return;
3206 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003207 p += len;
3208#ifdef FEAT_MBYTE
3209 c = mb_ptr2char(p);
3210#else
3211 c = *p;
3212#endif
3213 ins_compl_addleader(c);
3214}
3215
3216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003218 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003219 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003221 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222ins_compl_prep(c)
3223 int c;
3224{
3225 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 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;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003381 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003382
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003384 * If any of the original typed text has been changed, eg when
3385 * ignorecase is set, we must add back-spaces to the redo
3386 * buffer. We add as few as necessary to delete just the part
3387 * of the original text that has changed.
3388 * When using the longest match, edited the match or used
3389 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003391 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3392 ptr = compl_curr_match->cp_str;
3393 else if (compl_leader != NULL)
3394 ptr = compl_leader;
3395 else
3396 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003397 if (compl_orig_text != NULL)
3398 {
3399 p = compl_orig_text;
3400 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3401 ++temp)
3402 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003403#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003404 if (temp > 0)
3405 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003406#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003407 for (p += temp; *p != NUL; mb_ptr_adv(p))
3408 AppendCharToRedobuff(K_BS);
3409 }
3410 if (ptr != NULL)
3411 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 }
3413
3414#ifdef FEAT_CINDENT
3415 want_cindent = (can_cindent && cindent_on());
3416#endif
3417 /*
3418 * When completing whole lines: fix indent for 'cindent'.
3419 * Otherwise, break line if it's too long.
3420 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003421 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 {
3423#ifdef FEAT_CINDENT
3424 /* re-indent the current line */
3425 if (want_cindent)
3426 {
3427 do_c_expr_indent();
3428 want_cindent = FALSE; /* don't do it again */
3429 }
3430#endif
3431 }
3432 else
3433 {
3434 /* put the cursor on the last char, for 'tw' formatting */
3435 curwin->w_cursor.col--;
3436 if (stop_arrow() == OK)
3437 insertchar(NUL, 0, -1);
3438 curwin->w_cursor.col++;
3439 }
3440
3441 auto_format(FALSE, TRUE);
3442
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003443 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003444 * the selection without inserting anything. When
3445 * compl_enter_selects is set the Enter key does the same. */
3446 if ((c == Ctrl_Y || (compl_enter_selects
3447 && (c == CAR || c == K_KENTER || c == NL)))
3448 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003449 retval = TRUE;
3450
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003451 /* CTRL-E means completion is Ended, go back to the typed text. */
3452 if (c == Ctrl_E)
3453 {
3454 ins_compl_delete();
3455 if (compl_leader != NULL)
3456 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3457 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003458 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003459 + curwin->w_cursor.col - compl_col);
3460 retval = TRUE;
3461 }
3462
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003464 compl_started = FALSE;
3465 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 msg_clr_cmdline(); /* necessary for "noshowmode" */
3467 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003468 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (edit_submode != NULL)
3470 {
3471 edit_submode = NULL;
3472 showmode();
3473 }
3474
3475#ifdef FEAT_CINDENT
3476 /*
3477 * Indent now if a key was typed that is in 'cinkeys'.
3478 */
3479 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3480 do_c_expr_indent();
3481#endif
3482 }
3483 }
3484
3485 /* reset continue_* if we left expansion-mode, if we stay they'll be
3486 * (re)set properly in ins_complete() */
3487 if (!vim_is_ctrl_x_key(c))
3488 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003489 compl_cont_status = 0;
3490 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003492
3493 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494}
3495
3496/*
3497 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3498 * (depending on flag) starting from buf and looking for a non-scanned
3499 * buffer (other than curbuf). curbuf is special, if it is called with
3500 * buf=curbuf then it has to be the first call for a given flag/expansion.
3501 *
3502 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3503 */
3504 static buf_T *
3505ins_compl_next_buf(buf, flag)
3506 buf_T *buf;
3507 int flag;
3508{
3509#ifdef FEAT_WINDOWS
3510 static win_T *wp;
3511#endif
3512
3513 if (flag == 'w') /* just windows */
3514 {
3515#ifdef FEAT_WINDOWS
3516 if (buf == curbuf) /* first call for this flag/expansion */
3517 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003518 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 && wp->w_buffer->b_scanned)
3520 ;
3521 buf = wp->w_buffer;
3522#else
3523 buf = curbuf;
3524#endif
3525 }
3526 else
3527 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3528 * (unlisted buffers)
3529 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003530 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 && ((flag == 'U'
3532 ? buf->b_p_bl
3533 : (!buf->b_p_bl
3534 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003535 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 ;
3537 return buf;
3538}
3539
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003540#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003541static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003542
3543/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003544 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003545 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003546 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003547 static void
3548expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003549 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003550 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003551{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003552 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003553 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003554 char_u *funcname;
3555 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003556
Bram Moolenaare344bea2005-09-01 20:46:49 +00003557 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3558 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003559 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003560
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003561 /* Call 'completefunc' to obtain the list of matches. */
3562 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003563 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003564
Bram Moolenaare344bea2005-09-01 20:46:49 +00003565 pos = curwin->w_cursor;
3566 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3567 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003568 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003569 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003570
Bram Moolenaara94bc432006-03-10 21:42:59 +00003571 ins_compl_add_list(matchlist);
3572 list_unref(matchlist);
3573}
3574#endif /* FEAT_COMPL_FUNC */
3575
Bram Moolenaar39f05632006-03-19 22:15:26 +00003576#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003577/*
3578 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003579 */
3580 static void
3581ins_compl_add_list(list)
3582 list_T *list;
3583{
3584 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003585 int dir = compl_direction;
3586
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003587 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003588 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003589 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003590 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3591 /* if dir was BACKWARD then honor it just once */
3592 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003593 else if (did_emsg)
3594 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003595 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003596}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003597
3598/*
3599 * Add a match to the list of matches from a typeval_T.
3600 * If the given string is already in the list of completions, then return
3601 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3602 * maybe because alloc() returns NULL, then FAIL is returned.
3603 */
3604 int
3605ins_compl_add_tv(tv, dir)
3606 typval_T *tv;
3607 int dir;
3608{
3609 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003610 int icase = FALSE;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003611 int dup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003612 char_u *(cptext[CPT_COUNT]);
3613
3614 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3615 {
3616 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3617 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3618 (char_u *)"abbr", FALSE);
3619 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3620 (char_u *)"menu", FALSE);
3621 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3622 (char_u *)"kind", FALSE);
3623 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3624 (char_u *)"info", FALSE);
3625 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3626 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003627 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
3628 dup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003629 }
3630 else
3631 {
3632 word = get_tv_string_chk(tv);
3633 vim_memset(cptext, 0, sizeof(cptext));
3634 }
3635 if (word == NULL || *word == NUL)
3636 return FAIL;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003637 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, dup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003638}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003639#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003640
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003641/*
3642 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003643 * The search starts at position "ini" in curbuf and in the direction
3644 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003645 * When "compl_started" is FALSE start at that position, otherwise continue
3646 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003647 * This may return before finding all the matches.
3648 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 */
3650 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003651ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
3654 static pos_T first_match_pos;
3655 static pos_T last_match_pos;
3656 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003657 static int found_all = FALSE; /* Found all matches of a
3658 certain type. */
3659 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
Bram Moolenaar572cb562005-08-05 21:35:02 +00003661 pos_T *pos;
3662 char_u **matches;
3663 int save_p_scs;
3664 int save_p_ws;
3665 int save_p_ic;
3666 int i;
3667 int num_matches;
3668 int len;
3669 int found_new_match;
3670 int type = ctrl_x_mode;
3671 char_u *ptr;
3672 char_u *dict = NULL;
3673 int dict_f = 0;
3674 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003676 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 {
3678 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3679 ins_buf->b_scanned = 0;
3680 found_all = FALSE;
3681 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003682 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003683 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 last_match_pos = first_match_pos = *ini;
3685 }
3686
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003687 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003688 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3690 for (;;)
3691 {
3692 found_new_match = FAIL;
3693
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003694 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 * or if found_all says this entry is done. For ^X^L only use the
3696 * entries from 'complete' that look in loaded buffers. */
3697 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003698 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
3700 found_all = FALSE;
3701 while (*e_cpt == ',' || *e_cpt == ' ')
3702 e_cpt++;
3703 if (*e_cpt == '.' && !curbuf->b_scanned)
3704 {
3705 ins_buf = curbuf;
3706 first_match_pos = *ini;
3707 /* So that ^N can match word immediately after cursor */
3708 if (ctrl_x_mode == 0)
3709 dec(&first_match_pos);
3710 last_match_pos = first_match_pos;
3711 type = 0;
3712 }
3713 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3714 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3715 {
3716 /* Scan a buffer, but not the current one. */
3717 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3718 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003719 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 first_match_pos.col = last_match_pos.col = 0;
3721 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3722 last_match_pos.lnum = 0;
3723 type = 0;
3724 }
3725 else /* unloaded buffer, scan like dictionary */
3726 {
3727 found_all = TRUE;
3728 if (ins_buf->b_fname == NULL)
3729 continue;
3730 type = CTRL_X_DICTIONARY;
3731 dict = ins_buf->b_fname;
3732 dict_f = DICT_EXACT;
3733 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003734 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 ins_buf->b_fname == NULL
3736 ? buf_spname(ins_buf)
3737 : ins_buf->b_sfname == NULL
3738 ? (char *)ins_buf->b_fname
3739 : (char *)ins_buf->b_sfname);
3740 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3741 }
3742 else if (*e_cpt == NUL)
3743 break;
3744 else
3745 {
3746 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3747 type = -1;
3748 else if (*e_cpt == 'k' || *e_cpt == 's')
3749 {
3750 if (*e_cpt == 'k')
3751 type = CTRL_X_DICTIONARY;
3752 else
3753 type = CTRL_X_THESAURUS;
3754 if (*++e_cpt != ',' && *e_cpt != NUL)
3755 {
3756 dict = e_cpt;
3757 dict_f = DICT_FIRST;
3758 }
3759 }
3760#ifdef FEAT_FIND_ID
3761 else if (*e_cpt == 'i')
3762 type = CTRL_X_PATH_PATTERNS;
3763 else if (*e_cpt == 'd')
3764 type = CTRL_X_PATH_DEFINES;
3765#endif
3766 else if (*e_cpt == ']' || *e_cpt == 't')
3767 {
3768 type = CTRL_X_TAGS;
3769 sprintf((char*)IObuff, _("Scanning tags."));
3770 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3771 }
3772 else
3773 type = -1;
3774
3775 /* in any case e_cpt is advanced to the next entry */
3776 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3777
3778 found_all = TRUE;
3779 if (type == -1)
3780 continue;
3781 }
3782 }
3783
3784 switch (type)
3785 {
3786 case -1:
3787 break;
3788#ifdef FEAT_FIND_ID
3789 case CTRL_X_PATH_PATTERNS:
3790 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003791 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003792 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003794 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3796 (linenr_T)1, (linenr_T)MAXLNUM);
3797 break;
3798#endif
3799
3800 case CTRL_X_DICTIONARY:
3801 case CTRL_X_THESAURUS:
3802 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003803 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 : (type == CTRL_X_THESAURUS
3805 ? (*curbuf->b_p_tsr == NUL
3806 ? p_tsr
3807 : curbuf->b_p_tsr)
3808 : (*curbuf->b_p_dict == NUL
3809 ? p_dict
3810 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003811 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003812 dict != NULL ? dict_f
3813 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 dict = NULL;
3815 break;
3816
3817 case CTRL_X_TAGS:
3818 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3819 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003820 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821
3822 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003823 * of matches is found when compl_pattern is empty */
3824 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3826 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3827 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3828 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00003829 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
3831 p_ic = save_p_ic;
3832 break;
3833
3834 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003835 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3837 {
3838
3839 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003840 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003841 ins_compl_add_matches(num_matches, matches,
3842#ifdef CASE_INSENSITIVE_FILENAME
3843 TRUE
3844#else
3845 FALSE
3846#endif
3847 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849 break;
3850
3851 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003852 if (expand_cmdline(&compl_xp, compl_pattern,
3853 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003855 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 break;
3857
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003858#ifdef FEAT_COMPL_FUNC
3859 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003860 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003861 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003862 break;
3863#endif
3864
Bram Moolenaar488c6512005-08-11 20:09:58 +00003865 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003866#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003867 num_matches = expand_spelling(first_match_pos.lnum,
3868 first_match_pos.col, compl_pattern, &matches);
3869 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003870 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003871#endif
3872 break;
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 default: /* normal ^P/^N and ^X^L */
3875 /*
3876 * If 'infercase' is set, don't use 'smartcase' here
3877 */
3878 save_p_scs = p_scs;
3879 if (ins_buf->b_p_inf)
3880 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003881
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 /* buffers other than curbuf are scanned from the beginning or the
3883 * end but never from the middle, thus setting nowrapscan in this
3884 * buffers is a good idea, on the other hand, we always set
3885 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3886 save_p_ws = p_ws;
3887 if (ins_buf != curbuf)
3888 p_ws = FALSE;
3889 else if (*e_cpt == '.')
3890 p_ws = TRUE;
3891 for (;;)
3892 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003893 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003895 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3896 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003898 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003900 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003902 found_new_match = searchit(NULL, ins_buf, pos,
3903 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003904 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003905 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003906 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003908 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003909 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 first_match_pos = *pos;
3911 last_match_pos = *pos;
3912 }
3913 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003914 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 found_new_match = FAIL;
3916 if (found_new_match == FAIL)
3917 {
3918 if (ins_buf == curbuf)
3919 found_all = TRUE;
3920 break;
3921 }
3922
3923 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003924 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 && ini->lnum == pos->lnum
3926 && ini->col == pos->col)
3927 continue;
3928 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3929 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3930 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003931 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 {
3933 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3934 continue;
3935 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3936 if (!p_paste)
3937 ptr = skipwhite(ptr);
3938 }
3939 len = (int)STRLEN(ptr);
3940 }
3941 else
3942 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003943 char_u *tmp_ptr = ptr;
3944
3945 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003947 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 /* Skip if already inside a word. */
3949 if (vim_iswordp(tmp_ptr))
3950 continue;
3951 /* Find start of next word. */
3952 tmp_ptr = find_word_start(tmp_ptr);
3953 }
3954 /* Find end of this word. */
3955 tmp_ptr = find_word_end(tmp_ptr);
3956 len = (int)(tmp_ptr - ptr);
3957
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003958 if ((compl_cont_status & CONT_ADDING)
3959 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 {
3961 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3962 {
3963 /* Try next line, if any. the new word will be
3964 * "join" as if the normal command "J" was used.
3965 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003966 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 * works -- Acevedo */
3968 STRNCPY(IObuff, ptr, len);
3969 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3970 tmp_ptr = ptr = skipwhite(ptr);
3971 /* Find start of next word. */
3972 tmp_ptr = find_word_start(tmp_ptr);
3973 /* Find end of next word. */
3974 tmp_ptr = find_word_end(tmp_ptr);
3975 if (tmp_ptr > ptr)
3976 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003977 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003979 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 IObuff[len++] = ' ';
3981 /* IObuf =~ "\k.* ", thus len >= 2 */
3982 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003983 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 || (vim_strchr(p_cpo, CPO_JOINSP)
3985 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003986 && (IObuff[len - 2] == '?'
3987 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 IObuff[len++] = ' ';
3989 }
3990 /* copy as much as posible of the new word */
3991 if (tmp_ptr - ptr >= IOSIZE - len)
3992 tmp_ptr = ptr + IOSIZE - len - 1;
3993 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3994 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003995 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
3997 IObuff[len] = NUL;
3998 ptr = IObuff;
3999 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004000 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 continue;
4002 }
4003 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004004 if (ins_compl_add_infercase(ptr, len, FALSE,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004005 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004006 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 {
4008 found_new_match = OK;
4009 break;
4010 }
4011 }
4012 p_scs = save_p_scs;
4013 p_ws = save_p_ws;
4014 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004015
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004016 /* check if compl_curr_match has changed, (e.g. other type of
4017 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004018 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 found_new_match = OK;
4020
4021 /* break the loop for specialized modes (use 'complete' just for the
4022 * generic ctrl_x_mode == 0) or when we've found a new match */
4023 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004024 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004025 {
4026 if (got_int)
4027 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004028 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004029 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004030 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004031
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004032 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4033 || compl_interrupted)
4034 break;
4035 compl_started = TRUE;
4036 }
4037 else
4038 {
4039 /* Mark a buffer scanned when it has been scanned completely */
4040 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4041 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004043 compl_started = FALSE;
4044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004046 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
4048 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4049 && *e_cpt == NUL) /* Got to end of 'complete' */
4050 found_new_match = FAIL;
4051
4052 i = -1; /* total of matches, unknown */
4053 if (found_new_match == FAIL
4054 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4055 i = ins_compl_make_cyclic();
4056
4057 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058 * just been made cyclic then we have to move compl_curr_match to the next
4059 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004060 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4061 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004062 if (compl_curr_match == NULL)
4063 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 return i;
4065}
4066
4067/* Delete the old text being completed. */
4068 static void
4069ins_compl_delete()
4070{
4071 int i;
4072
4073 /*
4074 * In insert mode: Delete the typed part.
4075 * In replace mode: Put the old characters back, if any.
4076 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004077 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 backspace_until_column(i);
4079 changed_cline_bef_curs();
4080}
4081
4082/* Insert the new text being completed. */
4083 static void
4084ins_compl_insert()
4085{
Bram Moolenaar572cb562005-08-05 21:35:02 +00004086 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004087 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4088 compl_used_match = FALSE;
4089 else
4090 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091}
4092
4093/*
4094 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004095 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4096 * get more completions. If it is FALSE, then we just do nothing when there
4097 * are no more completions in a given direction. The latter case is used when
4098 * we are still in the middle of finding completions, to allow browsing
4099 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 * Return the total number of matches, or -1 if still unknown -- webb.
4101 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004102 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4103 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 *
4105 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004106 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4107 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 */
4109 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004110ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004112 int count; /* repeat completion this many times; should
4113 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004114 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
4116 int num_matches = -1;
4117 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004118 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004119 compl_T *found_compl = NULL;
4120 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004121 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004123 if (compl_leader != NULL
4124 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004126 /* Set "compl_shown_match" to the actually shown match, it may differ
4127 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004128 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004129 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004130 && compl_shown_match->cp_next != NULL
4131 && compl_shown_match->cp_next != compl_first_match)
4132 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004133
4134 /* If we didn't find it searching forward, and compl_shows_dir is
4135 * backward, find the last match. */
4136 if (compl_shows_dir == BACKWARD
4137 && !ins_compl_equal(compl_shown_match,
4138 compl_leader, (int)STRLEN(compl_leader))
4139 && (compl_shown_match->cp_next == NULL
4140 || compl_shown_match->cp_next == compl_first_match))
4141 {
4142 while (!ins_compl_equal(compl_shown_match,
4143 compl_leader, (int)STRLEN(compl_leader))
4144 && compl_shown_match->cp_prev != NULL
4145 && compl_shown_match->cp_prev != compl_first_match)
4146 compl_shown_match = compl_shown_match->cp_prev;
4147 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004148 }
4149
4150 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004151 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 /* Delete old text to be replaced */
4153 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004154
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004155 /* When finding the longest common text we stick at the original text,
4156 * don't let CTRL-N or CTRL-P move to the first match. */
4157 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4158
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004159 /* When restarting the search don't insert the first match either. */
4160 if (compl_restarting)
4161 {
4162 advance = FALSE;
4163 compl_restarting = FALSE;
4164 }
4165
Bram Moolenaare3226be2005-12-18 22:10:00 +00004166 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4167 * around. */
4168 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004170 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004172 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004173 found_end = (compl_first_match != NULL
4174 && (compl_shown_match->cp_next == compl_first_match
4175 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004176 }
4177 else if (compl_shows_dir == BACKWARD
4178 && compl_shown_match->cp_prev != NULL)
4179 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004180 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004181 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004182 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
4184 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004185 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004186 if (!allow_get_expansion)
4187 {
4188 if (advance)
4189 {
4190 if (compl_shows_dir == BACKWARD)
4191 compl_pending -= todo + 1;
4192 else
4193 compl_pending += todo + 1;
4194 }
4195 return -1;
4196 }
4197
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004198 if (advance)
4199 {
4200 if (compl_shows_dir == BACKWARD)
4201 --compl_pending;
4202 else
4203 ++compl_pending;
4204 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004205
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004206 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004207 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004208
4209 /* handle any pending completions */
4210 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004211 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004212 {
4213 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4214 {
4215 compl_shown_match = compl_shown_match->cp_next;
4216 --compl_pending;
4217 }
4218 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4219 {
4220 compl_shown_match = compl_shown_match->cp_prev;
4221 ++compl_pending;
4222 }
4223 else
4224 break;
4225 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004226 found_end = FALSE;
4227 }
4228 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4229 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004230 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004231 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004232 ++todo;
4233 else
4234 /* Remember a matching item. */
4235 found_compl = compl_shown_match;
4236
4237 /* Stop at the end of the list when we found a usable match. */
4238 if (found_end)
4239 {
4240 if (found_compl != NULL)
4241 {
4242 compl_shown_match = found_compl;
4243 break;
4244 }
4245 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 }
4248
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004249 /* Insert the text of the new completion, or the compl_leader. */
4250 if (insert_match)
4251 {
4252 if (!compl_get_longest || compl_used_match)
4253 ins_compl_insert();
4254 else
4255 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4256 }
4257 else
4258 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
4260 if (!allow_get_expansion)
4261 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004262 /* may undisplay the popup menu first */
4263 ins_compl_upd_pum();
4264
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004265 /* redraw to show the user what was inserted */
4266 update_screen(0);
4267
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004268 /* display the updated popup menu */
4269 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004270#ifdef FEAT_GUI
4271 if (gui.in_use)
4272 {
4273 /* Show the cursor after the match, not after the redrawn text. */
4274 setcursor();
4275 out_flush();
4276 gui_update_cursor(FALSE, FALSE);
4277 }
4278#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004279
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 /* Delete old text to be replaced, since we're still searching and
4281 * don't want to match ourselves! */
4282 ins_compl_delete();
4283 }
4284
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004285 /* Enter will select a match when the match wasn't inserted and the popup
4286 * menu is visislbe. */
4287 compl_enter_selects = !insert_match && compl_match_array != NULL;
4288
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 /*
4290 * Show the file name for the match (if any)
4291 * Truncate the file name to avoid a wait for return.
4292 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004293 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 {
4295 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004296 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 if (i <= 0)
4298 i = 0;
4299 else
4300 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004301 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 msg(IObuff);
4303 redraw_cmdline = FALSE; /* don't overwrite! */
4304 }
4305
4306 return num_matches;
4307}
4308
4309/*
4310 * Call this while finding completions, to check whether the user has hit a key
4311 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004312 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004314 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 */
4316 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004317ins_compl_check_keys(frequency)
4318 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319{
4320 static int count = 0;
4321
4322 int c;
4323
4324 /* Don't check when reading keys from a script. That would break the test
4325 * scripts */
4326 if (using_script())
4327 return;
4328
4329 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004330 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 return;
4332 count = 0;
4333
Bram Moolenaara260a972006-06-23 19:36:29 +00004334 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4335 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 if (c != NUL)
4338 {
4339 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4340 {
4341 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004342 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004343 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4344 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004346 else
4347 {
4348 /* Need to get the character to have KeyTyped set. We'll put it
4349 * back with vungetc() below. */
4350 c = safe_vgetc();
4351
4352 /* Don't interrupt completion when the character wasn't typed,
4353 * e.g., when doing @q to replay keys. */
4354 if (c != Ctrl_R && KeyTyped)
4355 compl_interrupted = TRUE;
4356
4357 vungetc(c);
4358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004360 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004361 {
4362 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4363
4364 compl_pending = 0;
4365 (void)ins_compl_next(FALSE, todo, TRUE);
4366 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004367}
4368
4369/*
4370 * Decide the direction of Insert mode complete from the key typed.
4371 * Returns BACKWARD or FORWARD.
4372 */
4373 static int
4374ins_compl_key2dir(c)
4375 int c;
4376{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004377 if (c == Ctrl_P || c == Ctrl_L
4378 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4379 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004380 return BACKWARD;
4381 return FORWARD;
4382}
4383
4384/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004385 * Return TRUE for keys that are used for completion only when the popup menu
4386 * is visible.
4387 */
4388 static int
4389ins_compl_pum_key(c)
4390 int c;
4391{
4392 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004393 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4394 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004395}
4396
4397/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004398 * Decide the number of completions to move forward.
4399 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4400 */
4401 static int
4402ins_compl_key2count(c)
4403 int c;
4404{
4405 int h;
4406
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004407 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004408 {
4409 h = pum_get_height();
4410 if (h > 3)
4411 h -= 2; /* keep some context */
4412 return h;
4413 }
4414 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415}
4416
4417/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004418 * Return TRUE if completion with "c" should insert the match, FALSE if only
4419 * to change the currently selected completion.
4420 */
4421 static int
4422ins_compl_use_match(c)
4423 int c;
4424{
4425 switch (c)
4426 {
4427 case K_UP:
4428 case K_DOWN:
4429 case K_PAGEDOWN:
4430 case K_KPAGEDOWN:
4431 case K_S_DOWN:
4432 case K_PAGEUP:
4433 case K_KPAGEUP:
4434 case K_S_UP:
4435 return FALSE;
4436 }
4437 return TRUE;
4438}
4439
4440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 * Do Insert mode completion.
4442 * Called when character "c" was typed, which has a meaning for completion.
4443 * Returns OK if completion was done, FAIL if something failed (out of mem).
4444 */
4445 static int
4446ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004447 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004449 char_u *line;
4450 int startcol = 0; /* column where searched text starts */
4451 colnr_T curs_col; /* cursor column */
4452 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
Bram Moolenaare3226be2005-12-18 22:10:00 +00004454 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004455 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
4457 /* First time we hit ^N or ^P (in a row, I mean) */
4458
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 did_ai = FALSE;
4460#ifdef FEAT_SMARTINDENT
4461 did_si = FALSE;
4462 can_si = FALSE;
4463 can_si_back = FALSE;
4464#endif
4465 if (stop_arrow() == FAIL)
4466 return FAIL;
4467
4468 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004469 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004470 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
4472 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004473 * "compl_startpos" to the cursor as a pattern to add a new word
4474 * instead of expand the one before the cursor, in word-wise if
4475 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 * is not in the same line as the cursor then fix it (the line has
4477 * been split because it was longer than 'tw'). if SOL is set then
4478 * skip the previous pattern, a word at the beginning of the line has
4479 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004480 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4481 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 {
4483 /*
4484 * it is a continued search
4485 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004486 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4488 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4489 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004490 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004492 /* line (probably) wrapped, set compl_startpos to the
4493 * first non_blank in the line, if it is not a wordchar
4494 * include it to get a better pattern, but then we don't
4495 * want the "\\<" prefix, check it bellow */
4496 compl_col = (colnr_T)(skipwhite(line) - line);
4497 compl_startpos.col = compl_col;
4498 compl_startpos.lnum = curwin->w_cursor.lnum;
4499 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501 else
4502 {
4503 /* S_IPOS was set when we inserted a word that was at the
4504 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505 * mode but first we need to redefine compl_startpos */
4506 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004508 compl_cont_status |= CONT_SOL;
4509 compl_startpos.col = (colnr_T)(skipwhite(
4510 line + compl_length
4511 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004513 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004515 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004516 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 * have enough space? just being paranoic */
4518#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004519 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004521 compl_cont_status &= ~CONT_SOL;
4522 compl_length = (IOSIZE - MIN_SPACE);
4523 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004525 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4526 if (compl_length < 1)
4527 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 }
4529 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004530 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004532 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004535 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004537 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004539 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004541 compl_cont_status = 0;
4542 compl_cont_status |= CONT_N_ADDS;
4543 compl_startpos = curwin->w_cursor;
4544 startcol = (int)curs_col;
4545 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 }
4547
4548 /* Work out completion pattern and original text -- webb */
4549 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4550 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004551 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4553 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004554 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004556 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004558 compl_col += ++startcol;
4559 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
4561 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004562 compl_pattern = str_foldcase(line + compl_col,
4563 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004565 compl_pattern = vim_strnsave(line + compl_col,
4566 compl_length);
4567 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 return FAIL;
4569 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004570 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 {
4572 char_u *prefix = (char_u *)"\\<";
4573
4574 /* we need 3 extra chars, 1 for the NUL and
4575 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004576 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4577 compl_length) + 3);
4578 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004580 if (!vim_iswordp(line + compl_col)
4581 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 && (
4583#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004584 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004586 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587#endif
4588 )))
4589 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004590 STRCPY((char *)compl_pattern, prefix);
4591 (void)quote_meta(compl_pattern + STRLEN(prefix),
4592 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004594 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004596 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004598 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599#endif
4600 )
4601 {
4602 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004603 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4604 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004606 compl_col += curs_col;
4607 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609 else
4610 {
4611#ifdef FEAT_MBYTE
4612 /* Search the point of change class of multibyte character
4613 * or not a word single byte character backward. */
4614 if (has_mbyte)
4615 {
4616 int base_class;
4617 int head_off;
4618
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004619 startcol -= (*mb_head_off)(line, line + startcol);
4620 base_class = mb_get_class(line + startcol);
4621 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004623 head_off = (*mb_head_off)(line, line + startcol);
4624 if (base_class != mb_get_class(line + startcol
4625 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004627 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
4629 }
4630 else
4631#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004632 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004634 compl_col += ++startcol;
4635 compl_length = (int)curs_col - startcol;
4636 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
4638 /* Only match word with at least two chars -- webb
4639 * there's no need to call quote_meta,
4640 * alloc(7) is enough -- Acevedo
4641 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004642 compl_pattern = alloc(7);
4643 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004645 STRCPY((char *)compl_pattern, "\\<");
4646 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4647 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 }
4649 else
4650 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004651 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4652 compl_length) + 3);
4653 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004655 STRCPY((char *)compl_pattern, "\\<");
4656 (void)quote_meta(compl_pattern + 2, line + compl_col,
4657 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 }
4659 }
4660 }
4661 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4662 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004663 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004664 compl_length = (int)curs_col - (int)compl_col;
4665 if (compl_length < 0) /* cursor in indent: empty pattern */
4666 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004668 compl_pattern = str_foldcase(line + compl_col, compl_length,
4669 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004671 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4672 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 return FAIL;
4674 }
4675 else if (ctrl_x_mode == CTRL_X_FILES)
4676 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004677 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004679 compl_col += ++startcol;
4680 compl_length = (int)curs_col - startcol;
4681 compl_pattern = addstar(line + compl_col, compl_length,
4682 EXPAND_FILES);
4683 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 return FAIL;
4685 }
4686 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4687 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004688 compl_pattern = vim_strnsave(line, curs_col);
4689 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004691 set_cmd_context(&compl_xp, compl_pattern,
4692 (int)STRLEN(compl_pattern), curs_col);
4693 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4694 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004695 /* No completion possible, use an empty pattern to get a
4696 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004697 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004698 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004699 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4700 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004702 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004703 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004704#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004705 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004706 * Call user defined function 'completefunc' with "a:findstart"
4707 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004708 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004709 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004710 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004711 char_u *funcname;
4712 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004713
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004714 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004715 * string */
4716 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4717 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4718 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004719 {
4720 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4721 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004722 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004723 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004724
4725 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004726 args[1] = NULL;
4727 pos = curwin->w_cursor;
4728 col = call_func_retnr(funcname, 2, args, FALSE);
4729 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004730
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004731 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004732 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004733 compl_col = col;
4734 if ((colnr_T)compl_col > curs_col)
4735 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004736
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004737 /* Setup variables for completion. Need to obtain "line" again,
4738 * it may have become invalid. */
4739 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004740 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004741 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4742 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004743#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004744 return FAIL;
4745 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004746 else if (ctrl_x_mode == CTRL_X_SPELL)
4747 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004748#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004749 if (spell_bad_len > 0)
4750 compl_col = curs_col - spell_bad_len;
4751 else
4752 compl_col = spell_word_start(startcol);
4753 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004754 {
4755 compl_length = 0;
4756 compl_col = curs_col;
4757 }
4758 else
4759 {
4760 spell_expand_check_cap(compl_col);
4761 compl_length = (int)curs_col - compl_col;
4762 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004763 /* Need to obtain "line" again, it may have become invalid. */
4764 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004765 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4766 if (compl_pattern == NULL)
4767#endif
4768 return FAIL;
4769 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004770 else
4771 {
4772 EMSG2(_(e_intern2), "ins_complete()");
4773 return FAIL;
4774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004776 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
4778 edit_submode_pre = (char_u *)_(" Adding");
4779 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4780 {
4781 /* Insert a new line, keep indentation but ignore 'comments' */
4782#ifdef FEAT_COMMENTS
4783 char_u *old = curbuf->b_p_com;
4784
4785 curbuf->b_p_com = (char_u *)"";
4786#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004787 compl_startpos.lnum = curwin->w_cursor.lnum;
4788 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 ins_eol('\r');
4790#ifdef FEAT_COMMENTS
4791 curbuf->b_p_com = old;
4792#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004793 compl_length = 0;
4794 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
4796 }
4797 else
4798 {
4799 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004800 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
4802
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004803 if (compl_cont_status & CONT_LOCAL)
4804 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 else
4806 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4807
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004808 /* Always add completion for the original text. */
4809 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004810 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4811 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004812 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004814 vim_free(compl_pattern);
4815 compl_pattern = NULL;
4816 vim_free(compl_orig_text);
4817 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 return FAIL;
4819 }
4820
4821 /* showmode might reset the internal line pointers, so it must
4822 * be called before line = ml_get(), or when this address is no
4823 * longer needed. -- Acevedo.
4824 */
4825 edit_submode_extra = (char_u *)_("-- Searching...");
4826 edit_submode_highl = HLF_COUNT;
4827 showmode();
4828 edit_submode_extra = NULL;
4829 out_flush();
4830 }
4831
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004832 compl_shown_match = compl_curr_match;
4833 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
4835 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004836 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004838 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004840 /* may undisplay the popup menu */
4841 ins_compl_upd_pum();
4842
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004843 if (n > 1) /* all matches have been found */
4844 compl_matches = n;
4845 compl_curr_match = compl_shown_match;
4846 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
Bram Moolenaard68071d2006-05-02 22:08:30 +00004848 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4849 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if (got_int && !global_busy)
4851 {
4852 (void)vgetc();
4853 got_int = FALSE;
4854 }
4855
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004856 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004857 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004859 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4860 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4862 edit_submode_highl = HLF_E;
4863 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4864 * because we couldn't expand anything at first place, but if we used
4865 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4866 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004867 if ( compl_length > 1
4868 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 || (ctrl_x_mode != 0
4870 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4871 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004872 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874
Bram Moolenaar572cb562005-08-05 21:35:02 +00004875 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004876 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004878 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 if (edit_submode_extra == NULL)
4881 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004882 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 {
4884 edit_submode_extra = (char_u *)_("Back at original");
4885 edit_submode_highl = HLF_W;
4886 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004887 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 {
4889 edit_submode_extra = (char_u *)_("Word from other line");
4890 edit_submode_highl = HLF_COUNT;
4891 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004892 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
4894 edit_submode_extra = (char_u *)_("The only match");
4895 edit_submode_highl = HLF_COUNT;
4896 }
4897 else
4898 {
4899 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004900 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004902 int number = 0;
4903 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004905 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
4907 /* search backwards for the first valid (!= -1) number.
4908 * This should normally succeed already at the first loop
4909 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004910 for (match = compl_curr_match->cp_prev; match != NULL
4911 && match != compl_first_match;
4912 match = match->cp_prev)
4913 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004915 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 break;
4917 }
4918 if (match != NULL)
4919 /* go up and assign all numbers which are not assigned
4920 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004921 for (match = match->cp_next;
4922 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004923 match = match->cp_next)
4924 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
4926 else /* BACKWARD */
4927 {
4928 /* search forwards (upwards) for the first valid (!= -1)
4929 * number. This should normally succeed already at the
4930 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004931 for (match = compl_curr_match->cp_next; match != NULL
4932 && match != compl_first_match;
4933 match = match->cp_next)
4934 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004936 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 break;
4938 }
4939 if (match != NULL)
4940 /* go down and assign all numbers which are not
4941 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004942 for (match = match->cp_prev; match
4943 && match->cp_number == -1;
4944 match = match->cp_prev)
4945 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
4947 }
4948
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004949 /* The match should always have a sequence number now, this is
4950 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004951 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 {
4953 /* Space for 10 text chars. + 2x10-digit no.s */
4954 static char_u match_ref[31];
4955
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004956 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004958 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004960 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004961 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004962 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 edit_submode_extra = match_ref;
4964 edit_submode_highl = HLF_R;
4965 if (dollar_vcol)
4966 curs_columns(FALSE);
4967 }
4968 }
4969 }
4970
4971 /* Show a message about what (completion) mode we're in. */
4972 showmode();
4973 if (edit_submode_extra != NULL)
4974 {
4975 if (!p_smd)
4976 msg_attr(edit_submode_extra,
4977 edit_submode_highl < HLF_COUNT
4978 ? hl_attr(edit_submode_highl) : 0);
4979 }
4980 else
4981 msg_clr_cmdline(); /* necessary for "noshowmode" */
4982
Bram Moolenaard68071d2006-05-02 22:08:30 +00004983 /* Show the popup menu, unless we got interrupted. */
4984 if (!compl_interrupted)
4985 {
4986 /* RedrawingDisabled may be set when invoked through complete(). */
4987 n = RedrawingDisabled;
4988 RedrawingDisabled = 0;
4989 ins_compl_show_pum();
4990 setcursor();
4991 RedrawingDisabled = n;
4992 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004993 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004994 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004995
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 return OK;
4997}
4998
4999/*
5000 * Looks in the first "len" chars. of "src" for search-metachars.
5001 * If dest is not NULL the chars. are copied there quoting (with
5002 * a backslash) the metachars, and dest would be NUL terminated.
5003 * Returns the length (needed) of dest
5004 */
5005 static int
5006quote_meta(dest, src, len)
5007 char_u *dest;
5008 char_u *src;
5009 int len;
5010{
5011 int m;
5012
5013 for (m = len; --len >= 0; src++)
5014 {
5015 switch (*src)
5016 {
5017 case '.':
5018 case '*':
5019 case '[':
5020 if (ctrl_x_mode == CTRL_X_DICTIONARY
5021 || ctrl_x_mode == CTRL_X_THESAURUS)
5022 break;
5023 case '~':
5024 if (!p_magic) /* quote these only if magic is set */
5025 break;
5026 case '\\':
5027 if (ctrl_x_mode == CTRL_X_DICTIONARY
5028 || ctrl_x_mode == CTRL_X_THESAURUS)
5029 break;
5030 case '^': /* currently it's not needed. */
5031 case '$':
5032 m++;
5033 if (dest != NULL)
5034 *dest++ = '\\';
5035 break;
5036 }
5037 if (dest != NULL)
5038 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005039# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 /* Copy remaining bytes of a multibyte character. */
5041 if (has_mbyte)
5042 {
5043 int i, mb_len;
5044
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005045 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 if (mb_len > 0 && len >= mb_len)
5047 for (i = 0; i < mb_len; ++i)
5048 {
5049 --len;
5050 ++src;
5051 if (dest != NULL)
5052 *dest++ = *src;
5053 }
5054 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057 if (dest != NULL)
5058 *dest = NUL;
5059
5060 return m;
5061}
5062#endif /* FEAT_INS_EXPAND */
5063
5064/*
5065 * Next character is interpreted literally.
5066 * A one, two or three digit decimal number is interpreted as its byte value.
5067 * If one or two digits are entered, the next character is given to vungetc().
5068 * For Unicode a character > 255 may be returned.
5069 */
5070 int
5071get_literal()
5072{
5073 int cc;
5074 int nc;
5075 int i;
5076 int hex = FALSE;
5077 int octal = FALSE;
5078#ifdef FEAT_MBYTE
5079 int unicode = 0;
5080#endif
5081
5082 if (got_int)
5083 return Ctrl_C;
5084
5085#ifdef FEAT_GUI
5086 /*
5087 * In GUI there is no point inserting the internal code for a special key.
5088 * It is more useful to insert the string "<KEY>" instead. This would
5089 * probably be useful in a text window too, but it would not be
5090 * vi-compatible (maybe there should be an option for it?) -- webb
5091 */
5092 if (gui.in_use)
5093 ++allow_keys;
5094#endif
5095#ifdef USE_ON_FLY_SCROLL
5096 dont_scroll = TRUE; /* disallow scrolling here */
5097#endif
5098 ++no_mapping; /* don't map the next key hits */
5099 cc = 0;
5100 i = 0;
5101 for (;;)
5102 {
5103 do
5104 nc = safe_vgetc();
5105 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
5106 || nc == K_HOR_SCROLLBAR);
5107#ifdef FEAT_CMDL_INFO
5108 if (!(State & CMDLINE)
5109# ifdef FEAT_MBYTE
5110 && MB_BYTE2LEN_CHECK(nc) == 1
5111# endif
5112 )
5113 add_to_showcmd(nc);
5114#endif
5115 if (nc == 'x' || nc == 'X')
5116 hex = TRUE;
5117 else if (nc == 'o' || nc == 'O')
5118 octal = TRUE;
5119#ifdef FEAT_MBYTE
5120 else if (nc == 'u' || nc == 'U')
5121 unicode = nc;
5122#endif
5123 else
5124 {
5125 if (hex
5126#ifdef FEAT_MBYTE
5127 || unicode != 0
5128#endif
5129 )
5130 {
5131 if (!vim_isxdigit(nc))
5132 break;
5133 cc = cc * 16 + hex2nr(nc);
5134 }
5135 else if (octal)
5136 {
5137 if (nc < '0' || nc > '7')
5138 break;
5139 cc = cc * 8 + nc - '0';
5140 }
5141 else
5142 {
5143 if (!VIM_ISDIGIT(nc))
5144 break;
5145 cc = cc * 10 + nc - '0';
5146 }
5147
5148 ++i;
5149 }
5150
5151 if (cc > 255
5152#ifdef FEAT_MBYTE
5153 && unicode == 0
5154#endif
5155 )
5156 cc = 255; /* limit range to 0-255 */
5157 nc = 0;
5158
5159 if (hex) /* hex: up to two chars */
5160 {
5161 if (i >= 2)
5162 break;
5163 }
5164#ifdef FEAT_MBYTE
5165 else if (unicode) /* Unicode: up to four or eight chars */
5166 {
5167 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5168 break;
5169 }
5170#endif
5171 else if (i >= 3) /* decimal or octal: up to three chars */
5172 break;
5173 }
5174 if (i == 0) /* no number entered */
5175 {
5176 if (nc == K_ZERO) /* NUL is stored as NL */
5177 {
5178 cc = '\n';
5179 nc = 0;
5180 }
5181 else
5182 {
5183 cc = nc;
5184 nc = 0;
5185 }
5186 }
5187
5188 if (cc == 0) /* NUL is stored as NL */
5189 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005190#ifdef FEAT_MBYTE
5191 if (enc_dbcs && (cc & 0xff) == 0)
5192 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5193 second byte will cause trouble! */
5194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195
5196 --no_mapping;
5197#ifdef FEAT_GUI
5198 if (gui.in_use)
5199 --allow_keys;
5200#endif
5201 if (nc)
5202 vungetc(nc);
5203 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5204 return cc;
5205}
5206
5207/*
5208 * Insert character, taking care of special keys and mod_mask
5209 */
5210 static void
5211insert_special(c, allow_modmask, ctrlv)
5212 int c;
5213 int allow_modmask;
5214 int ctrlv; /* c was typed after CTRL-V */
5215{
5216 char_u *p;
5217 int len;
5218
5219 /*
5220 * Special function key, translate into "<Key>". Up to the last '>' is
5221 * inserted with ins_str(), so as not to replace characters in replace
5222 * mode.
5223 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5224 * unless 'allow_modmask' is TRUE.
5225 */
5226#ifdef MACOS
5227 /* Command-key never produces a normal key */
5228 if (mod_mask & MOD_MASK_CMD)
5229 allow_modmask = TRUE;
5230#endif
5231 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5232 {
5233 p = get_special_key_name(c, mod_mask);
5234 len = (int)STRLEN(p);
5235 c = p[len - 1];
5236 if (len > 2)
5237 {
5238 if (stop_arrow() == FAIL)
5239 return;
5240 p[len - 1] = NUL;
5241 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005242 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 ctrlv = FALSE;
5244 }
5245 }
5246 if (stop_arrow() == OK)
5247 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5248}
5249
5250/*
5251 * Special characters in this context are those that need processing other
5252 * than the simple insertion that can be performed here. This includes ESC
5253 * which terminates the insert, and CR/NL which need special processing to
5254 * open up a new line. This routine tries to optimize insertions performed by
5255 * the "redo", "undo" or "put" commands, so it needs to know when it should
5256 * stop and defer processing to the "normal" mechanism.
5257 * '0' and '^' are special, because they can be followed by CTRL-D.
5258 */
5259#ifdef EBCDIC
5260# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5261#else
5262# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5263#endif
5264
5265#ifdef FEAT_MBYTE
5266# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5267#else
5268# define WHITECHAR(cc) vim_iswhite(cc)
5269#endif
5270
5271 void
5272insertchar(c, flags, second_indent)
5273 int c; /* character to insert or NUL */
5274 int flags; /* INSCHAR_FORMAT, etc. */
5275 int second_indent; /* indent for second line if >= 0 */
5276{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 int textwidth;
5278#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
5283 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5284 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
5286 /*
5287 * Try to break the line in two or more pieces when:
5288 * - Always do this if we have been called to do formatting only.
5289 * - Always do this when 'formatoptions' has the 'a' flag and the line
5290 * ends in white space.
5291 * - Otherwise:
5292 * - Don't do this if inserting a blank
5293 * - Don't do this if an existing character is being replaced, unless
5294 * we're in VREPLACE mode.
5295 * - Do this if the cursor is not on the line where insert started
5296 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5297 * before the insert.
5298 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5299 * before 'textwidth'
5300 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005301 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 && ((flags & INSCHAR_FORMAT)
5303 || (!vim_iswhite(c)
5304 && !((State & REPLACE_FLAG)
5305#ifdef FEAT_VREPLACE
5306 && !(State & VREPLACE_FLAG)
5307#endif
5308 && *ml_get_cursor() != NUL)
5309 && (curwin->w_cursor.lnum != Insstart.lnum
5310 || ((!has_format_option(FO_INS_LONG)
5311 || Insstart_textlen <= (colnr_T)textwidth)
5312 && (!fo_ins_blank
5313 || Insstart_blank_vcol <= (colnr_T)textwidth
5314 ))))))
5315 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005316 /* Format with 'formatexpr' when it's set. Use internal formatting
5317 * when 'formatexpr' isn't set or it returns non-zero. */
5318#if defined(FEAT_EVAL)
5319 if (*curbuf->b_p_fex == NUL
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005320 || fex_format(curwin->w_cursor.lnum, 1L, c) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005322 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005324
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 if (c == NUL) /* only formatting was wanted */
5326 return;
5327
5328#ifdef FEAT_COMMENTS
5329 /* Check whether this character should end a comment. */
5330 if (did_ai && (int)c == end_comment_pending)
5331 {
5332 char_u *line;
5333 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5334 int middle_len, end_len;
5335 int i;
5336
5337 /*
5338 * Need to remove existing (middle) comment leader and insert end
5339 * comment leader. First, check what comment leader we can find.
5340 */
5341 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5342 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5343 {
5344 /* Skip middle-comment string */
5345 while (*p && p[-1] != ':') /* find end of middle flags */
5346 ++p;
5347 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5348 /* Don't count trailing white space for middle_len */
5349 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5350 --middle_len;
5351
5352 /* Find the end-comment string */
5353 while (*p && p[-1] != ':') /* find end of end flags */
5354 ++p;
5355 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5356
5357 /* Skip white space before the cursor */
5358 i = curwin->w_cursor.col;
5359 while (--i >= 0 && vim_iswhite(line[i]))
5360 ;
5361 i++;
5362
5363 /* Skip to before the middle leader */
5364 i -= middle_len;
5365
5366 /* Check some expected things before we go on */
5367 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5368 {
5369 /* Backspace over all the stuff we want to replace */
5370 backspace_until_column(i);
5371
5372 /*
5373 * Insert the end-comment string, except for the last
5374 * character, which will get inserted as normal later.
5375 */
5376 ins_bytes_len(lead_end, end_len - 1);
5377 }
5378 }
5379 }
5380 end_comment_pending = NUL;
5381#endif
5382
5383 did_ai = FALSE;
5384#ifdef FEAT_SMARTINDENT
5385 did_si = FALSE;
5386 can_si = FALSE;
5387 can_si_back = FALSE;
5388#endif
5389
5390 /*
5391 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5392 * This speeds up normal text input considerably.
5393 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5394 * need to re-indent at a ':', or any other character (but not what
5395 * 'paste' is set)..
5396 */
5397#ifdef USE_ON_FLY_SCROLL
5398 dont_scroll = FALSE; /* allow scrolling here */
5399#endif
5400
5401 if ( !ISSPECIAL(c)
5402#ifdef FEAT_MBYTE
5403 && (!has_mbyte || (*mb_char2len)(c) == 1)
5404#endif
5405 && vpeekc() != NUL
5406 && !(State & REPLACE_FLAG)
5407#ifdef FEAT_CINDENT
5408 && !cindent_on()
5409#endif
5410#ifdef FEAT_RIGHTLEFT
5411 && !p_ri
5412#endif
5413 )
5414 {
5415#define INPUT_BUFLEN 100
5416 char_u buf[INPUT_BUFLEN + 1];
5417 int i;
5418 colnr_T virtcol = 0;
5419
5420 buf[0] = c;
5421 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005422 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 virtcol = get_nolist_virtcol();
5424 /*
5425 * Stop the string when:
5426 * - no more chars available
5427 * - finding a special character (command key)
5428 * - buffer is full
5429 * - running into the 'textwidth' boundary
5430 * - need to check for abbreviation: A non-word char after a word-char
5431 */
5432 while ( (c = vpeekc()) != NUL
5433 && !ISSPECIAL(c)
5434#ifdef FEAT_MBYTE
5435 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5436#endif
5437 && i < INPUT_BUFLEN
5438 && (textwidth == 0
5439 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5440 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5441 {
5442#ifdef FEAT_RIGHTLEFT
5443 c = vgetc();
5444 if (p_hkmap && KeyTyped)
5445 c = hkmap(c); /* Hebrew mode mapping */
5446# ifdef FEAT_FKMAP
5447 if (p_fkmap && KeyTyped)
5448 c = fkmap(c); /* Farsi mode mapping */
5449# endif
5450 buf[i++] = c;
5451#else
5452 buf[i++] = vgetc();
5453#endif
5454 }
5455
5456#ifdef FEAT_DIGRAPHS
5457 do_digraph(-1); /* clear digraphs */
5458 do_digraph(buf[i-1]); /* may be the start of a digraph */
5459#endif
5460 buf[i] = NUL;
5461 ins_str(buf);
5462 if (flags & INSCHAR_CTRLV)
5463 {
5464 redo_literal(*buf);
5465 i = 1;
5466 }
5467 else
5468 i = 0;
5469 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005470 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
5472 else
5473 {
5474#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005475 int cc;
5476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5478 {
5479 char_u buf[MB_MAXBYTES + 1];
5480
5481 (*mb_char2bytes)(c, buf);
5482 buf[cc] = NUL;
5483 ins_char_bytes(buf, cc);
5484 AppendCharToRedobuff(c);
5485 }
5486 else
5487#endif
5488 {
5489 ins_char(c);
5490 if (flags & INSCHAR_CTRLV)
5491 redo_literal(c);
5492 else
5493 AppendCharToRedobuff(c);
5494 }
5495 }
5496}
5497
5498/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005499 * Format text at the current insert position.
5500 */
5501 static void
5502internal_format(textwidth, second_indent, flags, format_only)
5503 int textwidth;
5504 int second_indent;
5505 int flags;
5506 int format_only;
5507{
5508 int cc;
5509 int save_char = NUL;
5510 int haveto_redraw = FALSE;
5511 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5512#ifdef FEAT_MBYTE
5513 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5514#endif
5515 int fo_white_par = has_format_option(FO_WHITE_PAR);
5516 int first_line = TRUE;
5517#ifdef FEAT_COMMENTS
5518 colnr_T leader_len;
5519 int no_leader = FALSE;
5520 int do_comments = (flags & INSCHAR_DO_COM);
5521#endif
5522
5523 /*
5524 * When 'ai' is off we don't want a space under the cursor to be
5525 * deleted. Replace it with an 'x' temporarily.
5526 */
5527 if (!curbuf->b_p_ai)
5528 {
5529 cc = gchar_cursor();
5530 if (vim_iswhite(cc))
5531 {
5532 save_char = cc;
5533 pchar_cursor('x');
5534 }
5535 }
5536
5537 /*
5538 * Repeat breaking lines, until the current line is not too long.
5539 */
5540 while (!got_int)
5541 {
5542 int startcol; /* Cursor column at entry */
5543 int wantcol; /* column at textwidth border */
5544 int foundcol; /* column for start of spaces */
5545 int end_foundcol = 0; /* column for start of word */
5546 colnr_T len;
5547 colnr_T virtcol;
5548#ifdef FEAT_VREPLACE
5549 int orig_col = 0;
5550 char_u *saved_text = NULL;
5551#endif
5552 colnr_T col;
5553
5554 virtcol = get_nolist_virtcol();
5555 if (virtcol < (colnr_T)textwidth)
5556 break;
5557
5558#ifdef FEAT_COMMENTS
5559 if (no_leader)
5560 do_comments = FALSE;
5561 else if (!(flags & INSCHAR_FORMAT)
5562 && has_format_option(FO_WRAP_COMS))
5563 do_comments = TRUE;
5564
5565 /* Don't break until after the comment leader */
5566 if (do_comments)
5567 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5568 else
5569 leader_len = 0;
5570
5571 /* If the line doesn't start with a comment leader, then don't
5572 * start one in a following broken line. Avoids that a %word
5573 * moved to the start of the next line causes all following lines
5574 * to start with %. */
5575 if (leader_len == 0)
5576 no_leader = TRUE;
5577#endif
5578 if (!(flags & INSCHAR_FORMAT)
5579#ifdef FEAT_COMMENTS
5580 && leader_len == 0
5581#endif
5582 && !has_format_option(FO_WRAP))
5583
5584 {
5585 textwidth = 0;
5586 break;
5587 }
5588 if ((startcol = curwin->w_cursor.col) == 0)
5589 break;
5590
5591 /* find column of textwidth border */
5592 coladvance((colnr_T)textwidth);
5593 wantcol = curwin->w_cursor.col;
5594
5595 curwin->w_cursor.col = startcol - 1;
5596#ifdef FEAT_MBYTE
5597 /* Correct cursor for multi-byte character. */
5598 if (has_mbyte)
5599 mb_adjust_cursor();
5600#endif
5601 foundcol = 0;
5602
5603 /*
5604 * Find position to break at.
5605 * Stop at first entered white when 'formatoptions' has 'v'
5606 */
5607 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5608 || curwin->w_cursor.lnum != Insstart.lnum
5609 || curwin->w_cursor.col >= Insstart.col)
5610 {
5611 cc = gchar_cursor();
5612 if (WHITECHAR(cc))
5613 {
5614 /* remember position of blank just before text */
5615 end_foundcol = curwin->w_cursor.col;
5616
5617 /* find start of sequence of blanks */
5618 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5619 {
5620 dec_cursor();
5621 cc = gchar_cursor();
5622 }
5623 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5624 break; /* only spaces in front of text */
5625#ifdef FEAT_COMMENTS
5626 /* Don't break until after the comment leader */
5627 if (curwin->w_cursor.col < leader_len)
5628 break;
5629#endif
5630 if (has_format_option(FO_ONE_LETTER))
5631 {
5632 /* do not break after one-letter words */
5633 if (curwin->w_cursor.col == 0)
5634 break; /* one-letter word at begin */
5635
5636 col = curwin->w_cursor.col;
5637 dec_cursor();
5638 cc = gchar_cursor();
5639
5640 if (WHITECHAR(cc))
5641 continue; /* one-letter, continue */
5642 curwin->w_cursor.col = col;
5643 }
5644#ifdef FEAT_MBYTE
5645 if (has_mbyte)
5646 foundcol = curwin->w_cursor.col
5647 + (*mb_ptr2len)(ml_get_cursor());
5648 else
5649#endif
5650 foundcol = curwin->w_cursor.col + 1;
5651 if (curwin->w_cursor.col < (colnr_T)wantcol)
5652 break;
5653 }
5654#ifdef FEAT_MBYTE
5655 else if (cc >= 0x100 && fo_multibyte
5656 && curwin->w_cursor.col <= (colnr_T)wantcol)
5657 {
5658 /* Break after or before a multi-byte character. */
5659 foundcol = curwin->w_cursor.col;
5660 if (curwin->w_cursor.col < (colnr_T)wantcol)
5661 foundcol += (*mb_char2len)(cc);
5662 end_foundcol = foundcol;
5663 break;
5664 }
5665#endif
5666 if (curwin->w_cursor.col == 0)
5667 break;
5668 dec_cursor();
5669 }
5670
5671 if (foundcol == 0) /* no spaces, cannot break line */
5672 {
5673 curwin->w_cursor.col = startcol;
5674 break;
5675 }
5676
5677 /* Going to break the line, remove any "$" now. */
5678 undisplay_dollar();
5679
5680 /*
5681 * Offset between cursor position and line break is used by replace
5682 * stack functions. VREPLACE does not use this, and backspaces
5683 * over the text instead.
5684 */
5685#ifdef FEAT_VREPLACE
5686 if (State & VREPLACE_FLAG)
5687 orig_col = startcol; /* Will start backspacing from here */
5688 else
5689#endif
5690 replace_offset = startcol - end_foundcol - 1;
5691
5692 /*
5693 * adjust startcol for spaces that will be deleted and
5694 * characters that will remain on top line
5695 */
5696 curwin->w_cursor.col = foundcol;
5697 while (cc = gchar_cursor(), WHITECHAR(cc))
5698 inc_cursor();
5699 startcol -= curwin->w_cursor.col;
5700 if (startcol < 0)
5701 startcol = 0;
5702
5703#ifdef FEAT_VREPLACE
5704 if (State & VREPLACE_FLAG)
5705 {
5706 /*
5707 * In VREPLACE mode, we will backspace over the text to be
5708 * wrapped, so save a copy now to put on the next line.
5709 */
5710 saved_text = vim_strsave(ml_get_cursor());
5711 curwin->w_cursor.col = orig_col;
5712 if (saved_text == NULL)
5713 break; /* Can't do it, out of memory */
5714 saved_text[startcol] = NUL;
5715
5716 /* Backspace over characters that will move to the next line */
5717 if (!fo_white_par)
5718 backspace_until_column(foundcol);
5719 }
5720 else
5721#endif
5722 {
5723 /* put cursor after pos. to break line */
5724 if (!fo_white_par)
5725 curwin->w_cursor.col = foundcol;
5726 }
5727
5728 /*
5729 * Split the line just before the margin.
5730 * Only insert/delete lines, but don't really redraw the window.
5731 */
5732 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5733 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5734#ifdef FEAT_COMMENTS
5735 + (do_comments ? OPENLINE_DO_COM : 0)
5736#endif
5737 , old_indent);
5738 old_indent = 0;
5739
5740 replace_offset = 0;
5741 if (first_line)
5742 {
5743 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5744 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5745 if (second_indent >= 0)
5746 {
5747#ifdef FEAT_VREPLACE
5748 if (State & VREPLACE_FLAG)
5749 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5750 else
5751#endif
5752 (void)set_indent(second_indent, SIN_CHANGED);
5753 }
5754 first_line = FALSE;
5755 }
5756
5757#ifdef FEAT_VREPLACE
5758 if (State & VREPLACE_FLAG)
5759 {
5760 /*
5761 * In VREPLACE mode we have backspaced over the text to be
5762 * moved, now we re-insert it into the new line.
5763 */
5764 ins_bytes(saved_text);
5765 vim_free(saved_text);
5766 }
5767 else
5768#endif
5769 {
5770 /*
5771 * Check if cursor is not past the NUL off the line, cindent
5772 * may have added or removed indent.
5773 */
5774 curwin->w_cursor.col += startcol;
5775 len = (colnr_T)STRLEN(ml_get_curline());
5776 if (curwin->w_cursor.col > len)
5777 curwin->w_cursor.col = len;
5778 }
5779
5780 haveto_redraw = TRUE;
5781#ifdef FEAT_CINDENT
5782 can_cindent = TRUE;
5783#endif
5784 /* moved the cursor, don't autoindent or cindent now */
5785 did_ai = FALSE;
5786#ifdef FEAT_SMARTINDENT
5787 did_si = FALSE;
5788 can_si = FALSE;
5789 can_si_back = FALSE;
5790#endif
5791 line_breakcheck();
5792 }
5793
5794 if (save_char != NUL) /* put back space after cursor */
5795 pchar_cursor(save_char);
5796
5797 if (!format_only && haveto_redraw)
5798 {
5799 update_topline();
5800 redraw_curbuf_later(VALID);
5801 }
5802}
5803
5804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 * Called after inserting or deleting text: When 'formatoptions' includes the
5806 * 'a' flag format from the current line until the end of the paragraph.
5807 * Keep the cursor at the same position relative to the text.
5808 * The caller must have saved the cursor line for undo, following ones will be
5809 * saved here.
5810 */
5811 void
5812auto_format(trailblank, prev_line)
5813 int trailblank; /* when TRUE also format with trailing blank */
5814 int prev_line; /* may start in previous line */
5815{
5816 pos_T pos;
5817 colnr_T len;
5818 char_u *old;
5819 char_u *new, *pnew;
5820 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005821 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822
5823 if (!has_format_option(FO_AUTO))
5824 return;
5825
5826 pos = curwin->w_cursor;
5827 old = ml_get_curline();
5828
5829 /* may remove added space */
5830 check_auto_format(FALSE);
5831
5832 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5833 * user might insert normal text next. Also skip formatting when "1" is
5834 * in 'formatoptions' and there is a single character before the cursor.
5835 * Otherwise the line would be broken and when typing another non-white
5836 * next they are not joined back together. */
5837 wasatend = (pos.col == STRLEN(old));
5838 if (*old != NUL && !trailblank && wasatend)
5839 {
5840 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005841 cc = gchar_cursor();
5842 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5843 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005845 cc = gchar_cursor();
5846 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 {
5848 curwin->w_cursor = pos;
5849 return;
5850 }
5851 curwin->w_cursor = pos;
5852 }
5853
5854#ifdef FEAT_COMMENTS
5855 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5856 * comments. */
5857 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5858 && get_leader_len(old, NULL, FALSE) == 0)
5859 return;
5860#endif
5861
5862 /*
5863 * May start formatting in a previous line, so that after "x" a word is
5864 * moved to the previous line if it fits there now. Only when this is not
5865 * the start of a paragraph.
5866 */
5867 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5868 {
5869 --curwin->w_cursor.lnum;
5870 if (u_save_cursor() == FAIL)
5871 return;
5872 }
5873
5874 /*
5875 * Do the formatting and restore the cursor position. "saved_cursor" will
5876 * be adjusted for the text formatting.
5877 */
5878 saved_cursor = pos;
5879 format_lines((linenr_T)-1);
5880 curwin->w_cursor = saved_cursor;
5881 saved_cursor.lnum = 0;
5882
5883 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5884 {
5885 /* "cannot happen" */
5886 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5887 coladvance((colnr_T)MAXCOL);
5888 }
5889 else
5890 check_cursor_col();
5891
5892 /* Insert mode: If the cursor is now after the end of the line while it
5893 * previously wasn't, the line was broken. Because of the rule above we
5894 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5895 * formatted. */
5896 if (!wasatend && has_format_option(FO_WHITE_PAR))
5897 {
5898 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005899 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 if (curwin->w_cursor.col == len)
5901 {
5902 pnew = vim_strnsave(new, len + 2);
5903 pnew[len] = ' ';
5904 pnew[len + 1] = NUL;
5905 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5906 /* remove the space later */
5907 did_add_space = TRUE;
5908 }
5909 else
5910 /* may remove added space */
5911 check_auto_format(FALSE);
5912 }
5913
5914 check_cursor();
5915}
5916
5917/*
5918 * When an extra space was added to continue a paragraph for auto-formatting,
5919 * delete it now. The space must be under the cursor, just after the insert
5920 * position.
5921 */
5922 static void
5923check_auto_format(end_insert)
5924 int end_insert; /* TRUE when ending Insert mode */
5925{
5926 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005927 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928
5929 if (did_add_space)
5930 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005931 cc = gchar_cursor();
5932 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 /* Somehow the space was removed already. */
5934 did_add_space = FALSE;
5935 else
5936 {
5937 if (!end_insert)
5938 {
5939 inc_cursor();
5940 c = gchar_cursor();
5941 dec_cursor();
5942 }
5943 if (c != NUL)
5944 {
5945 /* The space is no longer at the end of the line, delete it. */
5946 del_char(FALSE);
5947 did_add_space = FALSE;
5948 }
5949 }
5950 }
5951}
5952
5953/*
5954 * Find out textwidth to be used for formatting:
5955 * if 'textwidth' option is set, use it
5956 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5957 * if invalid value, use 0.
5958 * Set default to window width (maximum 79) for "gq" operator.
5959 */
5960 int
5961comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005962 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963{
5964 int textwidth;
5965
5966 textwidth = curbuf->b_p_tw;
5967 if (textwidth == 0 && curbuf->b_p_wm)
5968 {
5969 /* The width is the window width minus 'wrapmargin' minus all the
5970 * things that add to the margin. */
5971 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5972#ifdef FEAT_CMDWIN
5973 if (cmdwin_type != 0)
5974 textwidth -= 1;
5975#endif
5976#ifdef FEAT_FOLDING
5977 textwidth -= curwin->w_p_fdc;
5978#endif
5979#ifdef FEAT_SIGNS
5980 if (curwin->w_buffer->b_signlist != NULL
5981# ifdef FEAT_NETBEANS_INTG
5982 || usingNetbeans
5983# endif
5984 )
5985 textwidth -= 1;
5986#endif
5987 if (curwin->w_p_nu)
5988 textwidth -= 8;
5989 }
5990 if (textwidth < 0)
5991 textwidth = 0;
5992 if (ff && textwidth == 0)
5993 {
5994 textwidth = W_WIDTH(curwin) - 1;
5995 if (textwidth > 79)
5996 textwidth = 79;
5997 }
5998 return textwidth;
5999}
6000
6001/*
6002 * Put a character in the redo buffer, for when just after a CTRL-V.
6003 */
6004 static void
6005redo_literal(c)
6006 int c;
6007{
6008 char_u buf[10];
6009
6010 /* Only digits need special treatment. Translate them into a string of
6011 * three digits. */
6012 if (VIM_ISDIGIT(c))
6013 {
6014 sprintf((char *)buf, "%03d", c);
6015 AppendToRedobuff(buf);
6016 }
6017 else
6018 AppendCharToRedobuff(c);
6019}
6020
6021/*
6022 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006023 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 */
6025 static void
6026start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006027 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028{
6029 if (!arrow_used) /* something has been inserted */
6030 {
6031 AppendToRedobuff(ESC_STR);
6032 stop_insert(end_insert_pos, FALSE);
6033 arrow_used = TRUE; /* this means we stopped the current insert */
6034 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006035#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006036 check_spell_redraw();
6037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038}
6039
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006040#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006041/*
6042 * If we skipped highlighting word at cursor, do it now.
6043 * It may be skipped again, thus reset spell_redraw_lnum first.
6044 */
6045 static void
6046check_spell_redraw()
6047{
6048 if (spell_redraw_lnum != 0)
6049 {
6050 linenr_T lnum = spell_redraw_lnum;
6051
6052 spell_redraw_lnum = 0;
6053 redrawWinline(lnum, FALSE);
6054 }
6055}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006056
6057/*
6058 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6059 * spelled word, if there is one.
6060 */
6061 static void
6062spell_back_to_badword()
6063{
6064 pos_T tpos = curwin->w_cursor;
6065
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006066 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006067 if (curwin->w_cursor.col != tpos.col)
6068 start_arrow(&tpos);
6069}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006070#endif
6071
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072/*
6073 * stop_arrow() is called before a change is made in insert mode.
6074 * If an arrow key has been used, start a new insertion.
6075 * Returns FAIL if undo is impossible, shouldn't insert then.
6076 */
6077 int
6078stop_arrow()
6079{
6080 if (arrow_used)
6081 {
6082 if (u_save_cursor() == OK)
6083 {
6084 arrow_used = FALSE;
6085 ins_need_undo = FALSE;
6086 }
6087 Insstart = curwin->w_cursor; /* new insertion starts here */
6088 Insstart_textlen = linetabsize(ml_get_curline());
6089 ai_col = 0;
6090#ifdef FEAT_VREPLACE
6091 if (State & VREPLACE_FLAG)
6092 {
6093 orig_line_count = curbuf->b_ml.ml_line_count;
6094 vr_lines_changed = 1;
6095 }
6096#endif
6097 ResetRedobuff();
6098 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006099 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 }
6101 else if (ins_need_undo)
6102 {
6103 if (u_save_cursor() == OK)
6104 ins_need_undo = FALSE;
6105 }
6106
6107#ifdef FEAT_FOLDING
6108 /* Always open fold at the cursor line when inserting something. */
6109 foldOpenCursor();
6110#endif
6111
6112 return (arrow_used || ins_need_undo ? FAIL : OK);
6113}
6114
6115/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006116 * Do a few things to stop inserting.
6117 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6118 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 */
6120 static void
6121stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006122 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006123 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006125 int cc;
6126 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127
6128 stop_redo_ins();
6129 replace_flush(); /* abandon replace stack */
6130
6131 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006132 * Save the inserted text for later redo with ^@ and CTRL-A.
6133 * Don't do it when "restart_edit" was set and nothing was inserted,
6134 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006136 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006137 if (did_restart_edit == 0 || (ptr != NULL
6138 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006139 {
6140 vim_free(last_insert);
6141 last_insert = ptr;
6142 last_insert_skip = new_insert_skip;
6143 }
6144 else
6145 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006147 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 {
6149 /* Auto-format now. It may seem strange to do this when stopping an
6150 * insertion (or moving the cursor), but it's required when appending
6151 * a line and having it end in a space. But only do it when something
6152 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006153 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006155 pos_T tpos = curwin->w_cursor;
6156
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 /* When the cursor is at the end of the line after a space the
6158 * formatting will move it to the following word. Avoid that by
6159 * moving the cursor onto the space. */
6160 cc = 'x';
6161 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6162 {
6163 dec_cursor();
6164 cc = gchar_cursor();
6165 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006166 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 }
6168
6169 auto_format(TRUE, FALSE);
6170
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006171 if (vim_iswhite(cc))
6172 {
6173 if (gchar_cursor() != NUL)
6174 inc_cursor();
6175#ifdef FEAT_VIRTUALEDIT
6176 /* If the cursor is still at the same character, also keep
6177 * the "coladd". */
6178 if (gchar_cursor() == NUL
6179 && curwin->w_cursor.lnum == tpos.lnum
6180 && curwin->w_cursor.col == tpos.col)
6181 curwin->w_cursor.coladd = tpos.coladd;
6182#endif
6183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 }
6185
6186 /* If a space was inserted for auto-formatting, remove it now. */
6187 check_auto_format(TRUE);
6188
6189 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006190 * of the line, and put the cursor back.
6191 * Do this when ESC was used or moving the cursor up/down. */
6192 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006193 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006195 pos_T tpos = curwin->w_cursor;
6196
6197 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006198 for (;;)
6199 {
6200 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6201 --curwin->w_cursor.col;
6202 cc = gchar_cursor();
6203 if (!vim_iswhite(cc))
6204 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006206 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006207 if (curwin->w_cursor.lnum != tpos.lnum)
6208 curwin->w_cursor = tpos;
6209 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6211
6212#ifdef FEAT_VISUAL
6213 /* <C-S-Right> may have started Visual mode, adjust the position for
6214 * deleted characters. */
6215 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6216 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006217 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 if (VIsual.col > (colnr_T)cc)
6219 {
6220 VIsual.col = cc;
6221# ifdef FEAT_VIRTUALEDIT
6222 VIsual.coladd = 0;
6223# endif
6224 }
6225 }
6226#endif
6227 }
6228 }
6229 did_ai = FALSE;
6230#ifdef FEAT_SMARTINDENT
6231 did_si = FALSE;
6232 can_si = FALSE;
6233 can_si_back = FALSE;
6234#endif
6235
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006236 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6237 * now in a different buffer. */
6238 if (end_insert_pos != NULL)
6239 {
6240 curbuf->b_op_start = Insstart;
6241 curbuf->b_op_end = *end_insert_pos;
6242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243}
6244
6245/*
6246 * Set the last inserted text to a single character.
6247 * Used for the replace command.
6248 */
6249 void
6250set_last_insert(c)
6251 int c;
6252{
6253 char_u *s;
6254
6255 vim_free(last_insert);
6256#ifdef FEAT_MBYTE
6257 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6258#else
6259 last_insert = alloc(6);
6260#endif
6261 if (last_insert != NULL)
6262 {
6263 s = last_insert;
6264 /* Use the CTRL-V only when entering a special char */
6265 if (c < ' ' || c == DEL)
6266 *s++ = Ctrl_V;
6267 s = add_char2buf(c, s);
6268 *s++ = ESC;
6269 *s++ = NUL;
6270 last_insert_skip = 0;
6271 }
6272}
6273
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006274#if defined(EXITFREE) || defined(PROTO)
6275 void
6276free_last_insert()
6277{
6278 vim_free(last_insert);
6279 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006280 vim_free(compl_orig_text);
6281 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006282}
6283#endif
6284
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285/*
6286 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6287 * and CSI. Handle multi-byte characters.
6288 * Returns a pointer to after the added bytes.
6289 */
6290 char_u *
6291add_char2buf(c, s)
6292 int c;
6293 char_u *s;
6294{
6295#ifdef FEAT_MBYTE
6296 char_u temp[MB_MAXBYTES];
6297 int i;
6298 int len;
6299
6300 len = (*mb_char2bytes)(c, temp);
6301 for (i = 0; i < len; ++i)
6302 {
6303 c = temp[i];
6304#endif
6305 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6306 if (c == K_SPECIAL)
6307 {
6308 *s++ = K_SPECIAL;
6309 *s++ = KS_SPECIAL;
6310 *s++ = KE_FILLER;
6311 }
6312#ifdef FEAT_GUI
6313 else if (c == CSI)
6314 {
6315 *s++ = CSI;
6316 *s++ = KS_EXTRA;
6317 *s++ = (int)KE_CSI;
6318 }
6319#endif
6320 else
6321 *s++ = c;
6322#ifdef FEAT_MBYTE
6323 }
6324#endif
6325 return s;
6326}
6327
6328/*
6329 * move cursor to start of line
6330 * if flags & BL_WHITE move to first non-white
6331 * if flags & BL_SOL move to first non-white if startofline is set,
6332 * otherwise keep "curswant" column
6333 * if flags & BL_FIX don't leave the cursor on a NUL.
6334 */
6335 void
6336beginline(flags)
6337 int flags;
6338{
6339 if ((flags & BL_SOL) && !p_sol)
6340 coladvance(curwin->w_curswant);
6341 else
6342 {
6343 curwin->w_cursor.col = 0;
6344#ifdef FEAT_VIRTUALEDIT
6345 curwin->w_cursor.coladd = 0;
6346#endif
6347
6348 if (flags & (BL_WHITE | BL_SOL))
6349 {
6350 char_u *ptr;
6351
6352 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6353 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6354 ++curwin->w_cursor.col;
6355 }
6356 curwin->w_set_curswant = TRUE;
6357 }
6358}
6359
6360/*
6361 * oneright oneleft cursor_down cursor_up
6362 *
6363 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006364 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 * Return OK when successful, FAIL when we hit a line of file boundary.
6366 */
6367
6368 int
6369oneright()
6370{
6371 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373
6374#ifdef FEAT_VIRTUALEDIT
6375 if (virtual_active())
6376 {
6377 pos_T prevpos = curwin->w_cursor;
6378
6379 /* Adjust for multi-wide char (excluding TAB) */
6380 ptr = ml_get_cursor();
6381 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006382# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006384# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006386# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 ))
6388 ? ptr2cells(ptr) : 1));
6389 curwin->w_set_curswant = TRUE;
6390 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6391 return (prevpos.col != curwin->w_cursor.col
6392 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6393 }
6394#endif
6395
6396 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006397 if (*ptr == NUL)
6398 return FAIL; /* already at the very end */
6399
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006401 if (has_mbyte)
6402 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 else
6404#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006405 l = 1;
6406
6407 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6408 * contains "onemore". */
6409 if (ptr[l] == NUL
6410#ifdef FEAT_VIRTUALEDIT
6411 && (ve_flags & VE_ONEMORE) == 0
6412#endif
6413 )
6414 return FAIL;
6415 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416
6417 curwin->w_set_curswant = TRUE;
6418 return OK;
6419}
6420
6421 int
6422oneleft()
6423{
6424#ifdef FEAT_VIRTUALEDIT
6425 if (virtual_active())
6426 {
6427 int width;
6428 int v = getviscol();
6429
6430 if (v == 0)
6431 return FAIL;
6432
6433# ifdef FEAT_LINEBREAK
6434 /* We might get stuck on 'showbreak', skip over it. */
6435 width = 1;
6436 for (;;)
6437 {
6438 coladvance(v - width);
6439 /* getviscol() is slow, skip it when 'showbreak' is empty and
6440 * there are no multi-byte characters */
6441 if ((*p_sbr == NUL
6442# ifdef FEAT_MBYTE
6443 && !has_mbyte
6444# endif
6445 ) || getviscol() < v)
6446 break;
6447 ++width;
6448 }
6449# else
6450 coladvance(v - 1);
6451# endif
6452
6453 if (curwin->w_cursor.coladd == 1)
6454 {
6455 char_u *ptr;
6456
6457 /* Adjust for multi-wide char (not a TAB) */
6458 ptr = ml_get_cursor();
6459 if (*ptr != TAB && vim_isprintc(
6460# ifdef FEAT_MBYTE
6461 (*mb_ptr2char)(ptr)
6462# else
6463 *ptr
6464# endif
6465 ) && ptr2cells(ptr) > 1)
6466 curwin->w_cursor.coladd = 0;
6467 }
6468
6469 curwin->w_set_curswant = TRUE;
6470 return OK;
6471 }
6472#endif
6473
6474 if (curwin->w_cursor.col == 0)
6475 return FAIL;
6476
6477 curwin->w_set_curswant = TRUE;
6478 --curwin->w_cursor.col;
6479
6480#ifdef FEAT_MBYTE
6481 /* if the character on the left of the current cursor is a multi-byte
6482 * character, move to its first byte */
6483 if (has_mbyte)
6484 mb_adjust_cursor();
6485#endif
6486 return OK;
6487}
6488
6489 int
6490cursor_up(n, upd_topline)
6491 long n;
6492 int upd_topline; /* When TRUE: update topline */
6493{
6494 linenr_T lnum;
6495
6496 if (n > 0)
6497 {
6498 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006499 /* This fails if the cursor is already in the first line or the count
6500 * is larger than the line number and '-' is in 'cpoptions' */
6501 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 return FAIL;
6503 if (n >= lnum)
6504 lnum = 1;
6505 else
6506#ifdef FEAT_FOLDING
6507 if (hasAnyFolding(curwin))
6508 {
6509 /*
6510 * Count each sequence of folded lines as one logical line.
6511 */
6512 /* go to the the start of the current fold */
6513 (void)hasFolding(lnum, &lnum, NULL);
6514
6515 while (n--)
6516 {
6517 /* move up one line */
6518 --lnum;
6519 if (lnum <= 1)
6520 break;
6521 /* If we entered a fold, move to the beginning, unless in
6522 * Insert mode or when 'foldopen' contains "all": it will open
6523 * in a moment. */
6524 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6525 (void)hasFolding(lnum, &lnum, NULL);
6526 }
6527 if (lnum < 1)
6528 lnum = 1;
6529 }
6530 else
6531#endif
6532 lnum -= n;
6533 curwin->w_cursor.lnum = lnum;
6534 }
6535
6536 /* try to advance to the column we want to be at */
6537 coladvance(curwin->w_curswant);
6538
6539 if (upd_topline)
6540 update_topline(); /* make sure curwin->w_topline is valid */
6541
6542 return OK;
6543}
6544
6545/*
6546 * Cursor down a number of logical lines.
6547 */
6548 int
6549cursor_down(n, upd_topline)
6550 long n;
6551 int upd_topline; /* When TRUE: update topline */
6552{
6553 linenr_T lnum;
6554
6555 if (n > 0)
6556 {
6557 lnum = curwin->w_cursor.lnum;
6558#ifdef FEAT_FOLDING
6559 /* Move to last line of fold, will fail if it's the end-of-file. */
6560 (void)hasFolding(lnum, NULL, &lnum);
6561#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006562 /* This fails if the cursor is already in the last line or would move
6563 * beyound the last line and '-' is in 'cpoptions' */
6564 if (lnum >= curbuf->b_ml.ml_line_count
6565 || (lnum + n > curbuf->b_ml.ml_line_count
6566 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 return FAIL;
6568 if (lnum + n >= curbuf->b_ml.ml_line_count)
6569 lnum = curbuf->b_ml.ml_line_count;
6570 else
6571#ifdef FEAT_FOLDING
6572 if (hasAnyFolding(curwin))
6573 {
6574 linenr_T last;
6575
6576 /* count each sequence of folded lines as one logical line */
6577 while (n--)
6578 {
6579 if (hasFolding(lnum, NULL, &last))
6580 lnum = last + 1;
6581 else
6582 ++lnum;
6583 if (lnum >= curbuf->b_ml.ml_line_count)
6584 break;
6585 }
6586 if (lnum > curbuf->b_ml.ml_line_count)
6587 lnum = curbuf->b_ml.ml_line_count;
6588 }
6589 else
6590#endif
6591 lnum += n;
6592 curwin->w_cursor.lnum = lnum;
6593 }
6594
6595 /* try to advance to the column we want to be at */
6596 coladvance(curwin->w_curswant);
6597
6598 if (upd_topline)
6599 update_topline(); /* make sure curwin->w_topline is valid */
6600
6601 return OK;
6602}
6603
6604/*
6605 * Stuff the last inserted text in the read buffer.
6606 * Last_insert actually is a copy of the redo buffer, so we
6607 * first have to remove the command.
6608 */
6609 int
6610stuff_inserted(c, count, no_esc)
6611 int c; /* Command character to be inserted */
6612 long count; /* Repeat this many times */
6613 int no_esc; /* Don't add an ESC at the end */
6614{
6615 char_u *esc_ptr;
6616 char_u *ptr;
6617 char_u *last_ptr;
6618 char_u last = NUL;
6619
6620 ptr = get_last_insert();
6621 if (ptr == NULL)
6622 {
6623 EMSG(_(e_noinstext));
6624 return FAIL;
6625 }
6626
6627 /* may want to stuff the command character, to start Insert mode */
6628 if (c != NUL)
6629 stuffcharReadbuff(c);
6630 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6631 *esc_ptr = NUL; /* remove the ESC */
6632
6633 /* when the last char is either "0" or "^" it will be quoted if no ESC
6634 * comes after it OR if it will inserted more than once and "ptr"
6635 * starts with ^D. -- Acevedo
6636 */
6637 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6638 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6639 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6640 {
6641 last = *last_ptr;
6642 *last_ptr = NUL;
6643 }
6644
6645 do
6646 {
6647 stuffReadbuff(ptr);
6648 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6649 if (last)
6650 stuffReadbuff((char_u *)(last == '0'
6651 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6652 : IF_EB("\026^", CTRL_V_STR "^")));
6653 }
6654 while (--count > 0);
6655
6656 if (last)
6657 *last_ptr = last;
6658
6659 if (esc_ptr != NULL)
6660 *esc_ptr = ESC; /* put the ESC back */
6661
6662 /* may want to stuff a trailing ESC, to get out of Insert mode */
6663 if (!no_esc)
6664 stuffcharReadbuff(ESC);
6665
6666 return OK;
6667}
6668
6669 char_u *
6670get_last_insert()
6671{
6672 if (last_insert == NULL)
6673 return NULL;
6674 return last_insert + last_insert_skip;
6675}
6676
6677/*
6678 * Get last inserted string, and remove trailing <Esc>.
6679 * Returns pointer to allocated memory (must be freed) or NULL.
6680 */
6681 char_u *
6682get_last_insert_save()
6683{
6684 char_u *s;
6685 int len;
6686
6687 if (last_insert == NULL)
6688 return NULL;
6689 s = vim_strsave(last_insert + last_insert_skip);
6690 if (s != NULL)
6691 {
6692 len = (int)STRLEN(s);
6693 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6694 s[len - 1] = NUL;
6695 }
6696 return s;
6697}
6698
6699/*
6700 * Check the word in front of the cursor for an abbreviation.
6701 * Called when the non-id character "c" has been entered.
6702 * When an abbreviation is recognized it is removed from the text and
6703 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6704 */
6705 static int
6706echeck_abbr(c)
6707 int c;
6708{
6709 /* Don't check for abbreviation in paste mode, when disabled and just
6710 * after moving around with cursor keys. */
6711 if (p_paste || no_abbr || arrow_used)
6712 return FALSE;
6713
6714 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6715 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6716}
6717
6718/*
6719 * replace-stack functions
6720 *
6721 * When replacing characters, the replaced characters are remembered for each
6722 * new character. This is used to re-insert the old text when backspacing.
6723 *
6724 * There is a NUL headed list of characters for each character that is
6725 * currently in the file after the insertion point. When BS is used, one NUL
6726 * headed list is put back for the deleted character.
6727 *
6728 * For a newline, there are two NUL headed lists. One contains the characters
6729 * that the NL replaced. The extra one stores the characters after the cursor
6730 * that were deleted (always white space).
6731 *
6732 * Replace_offset is normally 0, in which case replace_push will add a new
6733 * character at the end of the stack. If replace_offset is not 0, that many
6734 * characters will be left on the stack above the newly inserted character.
6735 */
6736
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006737static char_u *replace_stack = NULL;
6738static long replace_stack_nr = 0; /* next entry in replace stack */
6739static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740
6741 void
6742replace_push(c)
6743 int c; /* character that is replaced (NUL is none) */
6744{
6745 char_u *p;
6746
6747 if (replace_stack_nr < replace_offset) /* nothing to do */
6748 return;
6749 if (replace_stack_len <= replace_stack_nr)
6750 {
6751 replace_stack_len += 50;
6752 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6753 if (p == NULL) /* out of memory */
6754 {
6755 replace_stack_len -= 50;
6756 return;
6757 }
6758 if (replace_stack != NULL)
6759 {
6760 mch_memmove(p, replace_stack,
6761 (size_t)(replace_stack_nr * sizeof(char_u)));
6762 vim_free(replace_stack);
6763 }
6764 replace_stack = p;
6765 }
6766 p = replace_stack + replace_stack_nr - replace_offset;
6767 if (replace_offset)
6768 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6769 *p = c;
6770 ++replace_stack_nr;
6771}
6772
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006773#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774/*
6775 * call replace_push(c) with replace_offset set to the first NUL.
6776 */
6777 static void
6778replace_push_off(c)
6779 int c;
6780{
6781 char_u *p;
6782
6783 p = replace_stack + replace_stack_nr;
6784 for (replace_offset = 1; replace_offset < replace_stack_nr;
6785 ++replace_offset)
6786 if (*--p == NUL)
6787 break;
6788 replace_push(c);
6789 replace_offset = 0;
6790}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792
6793/*
6794 * Pop one item from the replace stack.
6795 * return -1 if stack empty
6796 * return replaced character or NUL otherwise
6797 */
6798 static int
6799replace_pop()
6800{
6801 if (replace_stack_nr == 0)
6802 return -1;
6803 return (int)replace_stack[--replace_stack_nr];
6804}
6805
6806/*
6807 * Join the top two items on the replace stack. This removes to "off"'th NUL
6808 * encountered.
6809 */
6810 static void
6811replace_join(off)
6812 int off; /* offset for which NUL to remove */
6813{
6814 int i;
6815
6816 for (i = replace_stack_nr; --i >= 0; )
6817 if (replace_stack[i] == NUL && off-- <= 0)
6818 {
6819 --replace_stack_nr;
6820 mch_memmove(replace_stack + i, replace_stack + i + 1,
6821 (size_t)(replace_stack_nr - i));
6822 return;
6823 }
6824}
6825
6826/*
6827 * Pop bytes from the replace stack until a NUL is found, and insert them
6828 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6829 */
6830 static void
6831replace_pop_ins()
6832{
6833 int cc;
6834 int oldState = State;
6835
6836 State = NORMAL; /* don't want REPLACE here */
6837 while ((cc = replace_pop()) > 0)
6838 {
6839#ifdef FEAT_MBYTE
6840 mb_replace_pop_ins(cc);
6841#else
6842 ins_char(cc);
6843#endif
6844 dec_cursor();
6845 }
6846 State = oldState;
6847}
6848
6849#ifdef FEAT_MBYTE
6850/*
6851 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6852 * indicates a multi-byte char, pop the other bytes too.
6853 */
6854 static void
6855mb_replace_pop_ins(cc)
6856 int cc;
6857{
6858 int n;
6859 char_u buf[MB_MAXBYTES];
6860 int i;
6861 int c;
6862
6863 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6864 {
6865 buf[0] = cc;
6866 for (i = 1; i < n; ++i)
6867 buf[i] = replace_pop();
6868 ins_bytes_len(buf, n);
6869 }
6870 else
6871 ins_char(cc);
6872
6873 if (enc_utf8)
6874 /* Handle composing chars. */
6875 for (;;)
6876 {
6877 c = replace_pop();
6878 if (c == -1) /* stack empty */
6879 break;
6880 if ((n = MB_BYTE2LEN(c)) == 1)
6881 {
6882 /* Not a multi-byte char, put it back. */
6883 replace_push(c);
6884 break;
6885 }
6886 else
6887 {
6888 buf[0] = c;
6889 for (i = 1; i < n; ++i)
6890 buf[i] = replace_pop();
6891 if (utf_iscomposing(utf_ptr2char(buf)))
6892 ins_bytes_len(buf, n);
6893 else
6894 {
6895 /* Not a composing char, put it back. */
6896 for (i = n - 1; i >= 0; --i)
6897 replace_push(buf[i]);
6898 break;
6899 }
6900 }
6901 }
6902}
6903#endif
6904
6905/*
6906 * make the replace stack empty
6907 * (called when exiting replace mode)
6908 */
6909 static void
6910replace_flush()
6911{
6912 vim_free(replace_stack);
6913 replace_stack = NULL;
6914 replace_stack_len = 0;
6915 replace_stack_nr = 0;
6916}
6917
6918/*
6919 * Handle doing a BS for one character.
6920 * cc < 0: replace stack empty, just move cursor
6921 * cc == 0: character was inserted, delete it
6922 * cc > 0: character was replaced, put cc (first byte of original char) back
6923 * and check for more characters to be put back
6924 */
6925 static void
6926replace_do_bs()
6927{
6928 int cc;
6929#ifdef FEAT_VREPLACE
6930 int orig_len = 0;
6931 int ins_len;
6932 int orig_vcols = 0;
6933 colnr_T start_vcol;
6934 char_u *p;
6935 int i;
6936 int vcol;
6937#endif
6938
6939 cc = replace_pop();
6940 if (cc > 0)
6941 {
6942#ifdef FEAT_VREPLACE
6943 if (State & VREPLACE_FLAG)
6944 {
6945 /* Get the number of screen cells used by the character we are
6946 * going to delete. */
6947 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6948 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6949 }
6950#endif
6951#ifdef FEAT_MBYTE
6952 if (has_mbyte)
6953 {
6954 del_char(FALSE);
6955# ifdef FEAT_VREPLACE
6956 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006957 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958# endif
6959 replace_push(cc);
6960 }
6961 else
6962#endif
6963 {
6964 pchar_cursor(cc);
6965#ifdef FEAT_VREPLACE
6966 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006967 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968#endif
6969 }
6970 replace_pop_ins();
6971
6972#ifdef FEAT_VREPLACE
6973 if (State & VREPLACE_FLAG)
6974 {
6975 /* Get the number of screen cells used by the inserted characters */
6976 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006977 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 vcol = start_vcol;
6979 for (i = 0; i < ins_len; ++i)
6980 {
6981 vcol += chartabsize(p + i, vcol);
6982#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006983 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984#endif
6985 }
6986 vcol -= start_vcol;
6987
6988 /* Delete spaces that were inserted after the cursor to keep the
6989 * text aligned. */
6990 curwin->w_cursor.col += ins_len;
6991 while (vcol > orig_vcols && gchar_cursor() == ' ')
6992 {
6993 del_char(FALSE);
6994 ++orig_vcols;
6995 }
6996 curwin->w_cursor.col -= ins_len;
6997 }
6998#endif
6999
7000 /* mark the buffer as changed and prepare for displaying */
7001 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7002 }
7003 else if (cc == 0)
7004 (void)del_char(FALSE);
7005}
7006
7007#ifdef FEAT_CINDENT
7008/*
7009 * Return TRUE if C-indenting is on.
7010 */
7011 static int
7012cindent_on()
7013{
7014 return (!p_paste && (curbuf->b_p_cin
7015# ifdef FEAT_EVAL
7016 || *curbuf->b_p_inde != NUL
7017# endif
7018 ));
7019}
7020#endif
7021
7022#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7023/*
7024 * Re-indent the current line, based on the current contents of it and the
7025 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7026 * confused what all the part that handles Control-T is doing that I'm not.
7027 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7028 */
7029
7030 void
7031fixthisline(get_the_indent)
7032 int (*get_the_indent) __ARGS((void));
7033{
7034 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
7035 if (linewhite(curwin->w_cursor.lnum))
7036 did_ai = TRUE; /* delete the indent if the line stays empty */
7037}
7038
7039 void
7040fix_indent()
7041{
7042 if (p_paste)
7043 return;
7044# ifdef FEAT_LISP
7045 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7046 fixthisline(get_lisp_indent);
7047# endif
7048# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7049 else
7050# endif
7051# ifdef FEAT_CINDENT
7052 if (cindent_on())
7053 do_c_expr_indent();
7054# endif
7055}
7056
7057#endif
7058
7059#ifdef FEAT_CINDENT
7060/*
7061 * return TRUE if 'cinkeys' contains the key "keytyped",
7062 * when == '*': Only if key is preceded with '*' (indent before insert)
7063 * when == '!': Only if key is prededed with '!' (don't insert)
7064 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7065 *
7066 * "keytyped" can have a few special values:
7067 * KEY_OPEN_FORW
7068 * KEY_OPEN_BACK
7069 * KEY_COMPLETE just finished completion.
7070 *
7071 * If line_is_empty is TRUE accept keys with '0' before them.
7072 */
7073 int
7074in_cinkeys(keytyped, when, line_is_empty)
7075 int keytyped;
7076 int when;
7077 int line_is_empty;
7078{
7079 char_u *look;
7080 int try_match;
7081 int try_match_word;
7082 char_u *p;
7083 char_u *line;
7084 int icase;
7085 int i;
7086
7087#ifdef FEAT_EVAL
7088 if (*curbuf->b_p_inde != NUL)
7089 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7090 else
7091#endif
7092 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7093 while (*look)
7094 {
7095 /*
7096 * Find out if we want to try a match with this key, depending on
7097 * 'when' and a '*' or '!' before the key.
7098 */
7099 switch (when)
7100 {
7101 case '*': try_match = (*look == '*'); break;
7102 case '!': try_match = (*look == '!'); break;
7103 default: try_match = (*look != '*'); break;
7104 }
7105 if (*look == '*' || *look == '!')
7106 ++look;
7107
7108 /*
7109 * If there is a '0', only accept a match if the line is empty.
7110 * But may still match when typing last char of a word.
7111 */
7112 if (*look == '0')
7113 {
7114 try_match_word = try_match;
7115 if (!line_is_empty)
7116 try_match = FALSE;
7117 ++look;
7118 }
7119 else
7120 try_match_word = FALSE;
7121
7122 /*
7123 * does it look like a control character?
7124 */
7125 if (*look == '^'
7126#ifdef EBCDIC
7127 && (Ctrl_chr(look[1]) != 0)
7128#else
7129 && look[1] >= '?' && look[1] <= '_'
7130#endif
7131 )
7132 {
7133 if (try_match && keytyped == Ctrl_chr(look[1]))
7134 return TRUE;
7135 look += 2;
7136 }
7137 /*
7138 * 'o' means "o" command, open forward.
7139 * 'O' means "O" command, open backward.
7140 */
7141 else if (*look == 'o')
7142 {
7143 if (try_match && keytyped == KEY_OPEN_FORW)
7144 return TRUE;
7145 ++look;
7146 }
7147 else if (*look == 'O')
7148 {
7149 if (try_match && keytyped == KEY_OPEN_BACK)
7150 return TRUE;
7151 ++look;
7152 }
7153
7154 /*
7155 * 'e' means to check for "else" at start of line and just before the
7156 * cursor.
7157 */
7158 else if (*look == 'e')
7159 {
7160 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7161 {
7162 p = ml_get_curline();
7163 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7164 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7165 return TRUE;
7166 }
7167 ++look;
7168 }
7169
7170 /*
7171 * ':' only causes an indent if it is at the end of a label or case
7172 * statement, or when it was before typing the ':' (to fix
7173 * class::method for C++).
7174 */
7175 else if (*look == ':')
7176 {
7177 if (try_match && keytyped == ':')
7178 {
7179 p = ml_get_curline();
7180 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7181 return TRUE;
7182 if (curwin->w_cursor.col > 2
7183 && p[curwin->w_cursor.col - 1] == ':'
7184 && p[curwin->w_cursor.col - 2] == ':')
7185 {
7186 p[curwin->w_cursor.col - 1] = ' ';
7187 i = (cin_iscase(p) || cin_isscopedecl(p)
7188 || cin_islabel(30));
7189 p = ml_get_curline();
7190 p[curwin->w_cursor.col - 1] = ':';
7191 if (i)
7192 return TRUE;
7193 }
7194 }
7195 ++look;
7196 }
7197
7198
7199 /*
7200 * Is it a key in <>, maybe?
7201 */
7202 else if (*look == '<')
7203 {
7204 if (try_match)
7205 {
7206 /*
7207 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7208 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7209 * >, *, : and ! keys if they really really want to.
7210 */
7211 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7212 && keytyped == look[1])
7213 return TRUE;
7214
7215 if (keytyped == get_special_key_code(look + 1))
7216 return TRUE;
7217 }
7218 while (*look && *look != '>')
7219 look++;
7220 while (*look == '>')
7221 look++;
7222 }
7223
7224 /*
7225 * Is it a word: "=word"?
7226 */
7227 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7228 {
7229 ++look;
7230 if (*look == '~')
7231 {
7232 icase = TRUE;
7233 ++look;
7234 }
7235 else
7236 icase = FALSE;
7237 p = vim_strchr(look, ',');
7238 if (p == NULL)
7239 p = look + STRLEN(look);
7240 if ((try_match || try_match_word)
7241 && curwin->w_cursor.col >= (colnr_T)(p - look))
7242 {
7243 int match = FALSE;
7244
7245#ifdef FEAT_INS_EXPAND
7246 if (keytyped == KEY_COMPLETE)
7247 {
7248 char_u *s;
7249
7250 /* Just completed a word, check if it starts with "look".
7251 * search back for the start of a word. */
7252 line = ml_get_curline();
7253# ifdef FEAT_MBYTE
7254 if (has_mbyte)
7255 {
7256 char_u *n;
7257
7258 for (s = line + curwin->w_cursor.col; s > line; s = n)
7259 {
7260 n = mb_prevptr(line, s);
7261 if (!vim_iswordp(n))
7262 break;
7263 }
7264 }
7265 else
7266# endif
7267 for (s = line + curwin->w_cursor.col; s > line; --s)
7268 if (!vim_iswordc(s[-1]))
7269 break;
7270 if (s + (p - look) <= line + curwin->w_cursor.col
7271 && (icase
7272 ? MB_STRNICMP(s, look, p - look)
7273 : STRNCMP(s, look, p - look)) == 0)
7274 match = TRUE;
7275 }
7276 else
7277#endif
7278 /* TODO: multi-byte */
7279 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7280 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7281 {
7282 line = ml_get_cursor();
7283 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7284 || !vim_iswordc(line[-(p - look) - 1]))
7285 && (icase
7286 ? MB_STRNICMP(line - (p - look), look, p - look)
7287 : STRNCMP(line - (p - look), look, p - look))
7288 == 0)
7289 match = TRUE;
7290 }
7291 if (match && try_match_word && !try_match)
7292 {
7293 /* "0=word": Check if there are only blanks before the
7294 * word. */
7295 line = ml_get_curline();
7296 if ((int)(skipwhite(line) - line) !=
7297 (int)(curwin->w_cursor.col - (p - look)))
7298 match = FALSE;
7299 }
7300 if (match)
7301 return TRUE;
7302 }
7303 look = p;
7304 }
7305
7306 /*
7307 * ok, it's a boring generic character.
7308 */
7309 else
7310 {
7311 if (try_match && *look == keytyped)
7312 return TRUE;
7313 ++look;
7314 }
7315
7316 /*
7317 * Skip over ", ".
7318 */
7319 look = skip_to_option_part(look);
7320 }
7321 return FALSE;
7322}
7323#endif /* FEAT_CINDENT */
7324
7325#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7326/*
7327 * Map Hebrew keyboard when in hkmap mode.
7328 */
7329 int
7330hkmap(c)
7331 int c;
7332{
7333 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7334 {
7335 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7336 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7337 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7338 static char_u map[26] =
7339 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7340 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7341 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7342 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7343 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7344 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7345 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7346 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7347 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7348
7349 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7350 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7351 /* '-1'='sofit' */
7352 else if (c == 'x')
7353 return 'X';
7354 else if (c == 'q')
7355 return '\''; /* {geresh}={'} */
7356 else if (c == 246)
7357 return ' '; /* \"o --> ' ' for a german keyboard */
7358 else if (c == 228)
7359 return ' '; /* \"a --> ' ' -- / -- */
7360 else if (c == 252)
7361 return ' '; /* \"u --> ' ' -- / -- */
7362#ifdef EBCDIC
7363 else if (islower(c))
7364#else
7365 /* NOTE: islower() does not do the right thing for us on Linux so we
7366 * do this the same was as 5.7 and previous, so it works correctly on
7367 * all systems. Specifically, the e.g. Delete and Arrow keys are
7368 * munged and won't work if e.g. searching for Hebrew text.
7369 */
7370 else if (c >= 'a' && c <= 'z')
7371#endif
7372 return (int)(map[CharOrdLow(c)] + p_aleph);
7373 else
7374 return c;
7375 }
7376 else
7377 {
7378 switch (c)
7379 {
7380 case '`': return ';';
7381 case '/': return '.';
7382 case '\'': return ',';
7383 case 'q': return '/';
7384 case 'w': return '\'';
7385
7386 /* Hebrew letters - set offset from 'a' */
7387 case ',': c = '{'; break;
7388 case '.': c = 'v'; break;
7389 case ';': c = 't'; break;
7390 default: {
7391 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7392
7393#ifdef EBCDIC
7394 /* see note about islower() above */
7395 if (!islower(c))
7396#else
7397 if (c < 'a' || c > 'z')
7398#endif
7399 return c;
7400 c = str[CharOrdLow(c)];
7401 break;
7402 }
7403 }
7404
7405 return (int)(CharOrdLow(c) + p_aleph);
7406 }
7407}
7408#endif
7409
7410 static void
7411ins_reg()
7412{
7413 int need_redraw = FALSE;
7414 int regname;
7415 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007416#ifdef FEAT_VISUAL
7417 int vis_active = VIsual_active;
7418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
7420 /*
7421 * If we are going to wait for a character, show a '"'.
7422 */
7423 pc_status = PC_STATUS_UNSET;
7424 if (redrawing() && !char_avail())
7425 {
7426 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007427 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428
7429 edit_putchar('"', TRUE);
7430#ifdef FEAT_CMDL_INFO
7431 add_to_showcmd_c(Ctrl_R);
7432#endif
7433 }
7434
7435#ifdef USE_ON_FLY_SCROLL
7436 dont_scroll = TRUE; /* disallow scrolling here */
7437#endif
7438
7439 /*
7440 * Don't map the register name. This also prevents the mode message to be
7441 * deleted when ESC is hit.
7442 */
7443 ++no_mapping;
7444 regname = safe_vgetc();
7445#ifdef FEAT_LANGMAP
7446 LANGMAP_ADJUST(regname, TRUE);
7447#endif
7448 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7449 {
7450 /* Get a third key for literal register insertion */
7451 literally = regname;
7452#ifdef FEAT_CMDL_INFO
7453 add_to_showcmd_c(literally);
7454#endif
7455 regname = safe_vgetc();
7456#ifdef FEAT_LANGMAP
7457 LANGMAP_ADJUST(regname, TRUE);
7458#endif
7459 }
7460 --no_mapping;
7461
7462#ifdef FEAT_EVAL
7463 /*
7464 * Don't call u_sync() while getting the expression,
7465 * evaluating it or giving an error message for it!
7466 */
7467 ++no_u_sync;
7468 if (regname == '=')
7469 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007470# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007474# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 /* Restore the Input Method. */
7476 if (im_on)
7477 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007478# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007480 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7481 {
7482 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 else
7486 {
7487#endif
7488 if (literally == Ctrl_O || literally == Ctrl_P)
7489 {
7490 /* Append the command to the redo buffer. */
7491 AppendCharToRedobuff(Ctrl_R);
7492 AppendCharToRedobuff(literally);
7493 AppendCharToRedobuff(regname);
7494
7495 do_put(regname, BACKWARD, 1L,
7496 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7497 }
7498 else if (insert_reg(regname, literally) == FAIL)
7499 {
7500 vim_beep();
7501 need_redraw = TRUE; /* remove the '"' */
7502 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007503 else if (stop_insert_mode)
7504 /* When the '=' register was used and a function was invoked that
7505 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7506 * insert anything, need to remove the '"' */
7507 need_redraw = TRUE;
7508
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509#ifdef FEAT_EVAL
7510 }
7511 --no_u_sync;
7512#endif
7513#ifdef FEAT_CMDL_INFO
7514 clear_showcmd();
7515#endif
7516
7517 /* If the inserted register is empty, we need to remove the '"' */
7518 if (need_redraw || stuff_empty())
7519 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007520
7521#ifdef FEAT_VISUAL
7522 /* Disallow starting Visual mode here, would get a weird mode. */
7523 if (!vis_active && VIsual_active)
7524 end_visual_mode();
7525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526}
7527
7528/*
7529 * CTRL-G commands in Insert mode.
7530 */
7531 static void
7532ins_ctrl_g()
7533{
7534 int c;
7535
7536#ifdef FEAT_INS_EXPAND
7537 /* Right after CTRL-X the cursor will be after the ruler. */
7538 setcursor();
7539#endif
7540
7541 /*
7542 * Don't map the second key. This also prevents the mode message to be
7543 * deleted when ESC is hit.
7544 */
7545 ++no_mapping;
7546 c = safe_vgetc();
7547 --no_mapping;
7548 switch (c)
7549 {
7550 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7551 case K_UP:
7552 case Ctrl_K:
7553 case 'k': ins_up(TRUE);
7554 break;
7555
7556 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7557 case K_DOWN:
7558 case Ctrl_J:
7559 case 'j': ins_down(TRUE);
7560 break;
7561
7562 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007563 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007565
7566 /* Need to reset Insstart, esp. because a BS that joins
7567 * aline to the previous one must save for undo. */
7568 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 break;
7570
7571 /* Unknown CTRL-G command, reserved for future expansion. */
7572 default: vim_beep();
7573 }
7574}
7575
7576/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007577 * CTRL-^ in Insert mode.
7578 */
7579 static void
7580ins_ctrl_hat()
7581{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007582 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007583 {
7584 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7585 if (State & LANGMAP)
7586 {
7587 curbuf->b_p_iminsert = B_IMODE_NONE;
7588 State &= ~LANGMAP;
7589 }
7590 else
7591 {
7592 curbuf->b_p_iminsert = B_IMODE_LMAP;
7593 State |= LANGMAP;
7594#ifdef USE_IM_CONTROL
7595 im_set_active(FALSE);
7596#endif
7597 }
7598 }
7599#ifdef USE_IM_CONTROL
7600 else
7601 {
7602 /* There are no ":lmap" mappings, toggle IM */
7603 if (im_get_status())
7604 {
7605 curbuf->b_p_iminsert = B_IMODE_NONE;
7606 im_set_active(FALSE);
7607 }
7608 else
7609 {
7610 curbuf->b_p_iminsert = B_IMODE_IM;
7611 State &= ~LANGMAP;
7612 im_set_active(TRUE);
7613 }
7614 }
7615#endif
7616 set_iminsert_global();
7617 showmode();
7618#ifdef FEAT_GUI
7619 /* may show different cursor shape or color */
7620 if (gui.in_use)
7621 gui_update_cursor(TRUE, FALSE);
7622#endif
7623#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7624 /* Show/unshow value of 'keymap' in status lines. */
7625 status_redraw_curbuf();
7626#endif
7627}
7628
7629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 * Handle ESC in insert mode.
7631 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7632 * insert.
7633 */
7634 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007635ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 long *count;
7637 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007638 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639{
7640 int temp;
7641 static int disabled_redraw = FALSE;
7642
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007643#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007644 check_spell_redraw();
7645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646#if defined(FEAT_HANGULIN)
7647# if defined(ESC_CHG_TO_ENG_MODE)
7648 hangul_input_state_set(0);
7649# endif
7650 if (composing_hangul)
7651 {
7652 push_raw_key(composing_hangul_buffer, 2);
7653 composing_hangul = 0;
7654 }
7655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656
7657 temp = curwin->w_cursor.col;
7658 if (disabled_redraw)
7659 {
7660 --RedrawingDisabled;
7661 disabled_redraw = FALSE;
7662 }
7663 if (!arrow_used)
7664 {
7665 /*
7666 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007667 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7668 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 */
7670 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007671 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672
7673 /*
7674 * Repeating insert may take a long time. Check for
7675 * interrupt now and then.
7676 */
7677 if (*count > 0)
7678 {
7679 line_breakcheck();
7680 if (got_int)
7681 *count = 0;
7682 }
7683
7684 if (--*count > 0) /* repeat what was typed */
7685 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007686 /* Vi repeats the insert without replacing characters. */
7687 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7688 State &= ~REPLACE_FLAG;
7689
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 (void)start_redo_ins();
7691 if (cmdchar == 'r' || cmdchar == 'v')
7692 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7693 ++RedrawingDisabled;
7694 disabled_redraw = TRUE;
7695 return FALSE; /* repeat the insert */
7696 }
7697 stop_insert(&curwin->w_cursor, TRUE);
7698 undisplay_dollar();
7699 }
7700
7701 /* When an autoindent was removed, curswant stays after the
7702 * indent */
7703 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7704 curwin->w_set_curswant = TRUE;
7705
7706 /* Remember the last Insert position in the '^ mark. */
7707 if (!cmdmod.keepjumps)
7708 curbuf->b_last_insert = curwin->w_cursor;
7709
7710 /*
7711 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007712 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007714 if (!nomove
7715 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716#ifdef FEAT_VIRTUALEDIT
7717 || curwin->w_cursor.coladd > 0
7718#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007719 )
7720 && (restart_edit == NUL
7721 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007723 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007725 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726#ifdef FEAT_RIGHTLEFT
7727 && !revins_on
7728#endif
7729 )
7730 {
7731#ifdef FEAT_VIRTUALEDIT
7732 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7733 {
7734 oneleft();
7735 if (restart_edit != NUL)
7736 ++curwin->w_cursor.coladd;
7737 }
7738 else
7739#endif
7740 {
7741 --curwin->w_cursor.col;
7742#ifdef FEAT_MBYTE
7743 /* Correct cursor for multi-byte character. */
7744 if (has_mbyte)
7745 mb_adjust_cursor();
7746#endif
7747 }
7748 }
7749
7750#ifdef USE_IM_CONTROL
7751 /* Disable IM to allow typing English directly for Normal mode commands.
7752 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7753 * well). */
7754 if (!(State & LANGMAP))
7755 im_save_status(&curbuf->b_p_iminsert);
7756 im_set_active(FALSE);
7757#endif
7758
7759 State = NORMAL;
7760 /* need to position cursor again (e.g. when on a TAB ) */
7761 changed_cline_bef_curs();
7762
7763#ifdef FEAT_MOUSE
7764 setmouse();
7765#endif
7766#ifdef CURSOR_SHAPE
7767 ui_cursor_shape(); /* may show different cursor shape */
7768#endif
7769
7770 /*
7771 * When recording or for CTRL-O, need to display the new mode.
7772 * Otherwise remove the mode message.
7773 */
7774 if (Recording || restart_edit != NUL)
7775 showmode();
7776 else if (p_smd)
7777 MSG("");
7778
7779 return TRUE; /* exit Insert mode */
7780}
7781
7782#ifdef FEAT_RIGHTLEFT
7783/*
7784 * Toggle language: hkmap and revins_on.
7785 * Move to end of reverse inserted text.
7786 */
7787 static void
7788ins_ctrl_()
7789{
7790 if (revins_on && revins_chars && revins_scol >= 0)
7791 {
7792 while (gchar_cursor() != NUL && revins_chars--)
7793 ++curwin->w_cursor.col;
7794 }
7795 p_ri = !p_ri;
7796 revins_on = (State == INSERT && p_ri);
7797 if (revins_on)
7798 {
7799 revins_scol = curwin->w_cursor.col;
7800 revins_legal++;
7801 revins_chars = 0;
7802 undisplay_dollar();
7803 }
7804 else
7805 revins_scol = -1;
7806#ifdef FEAT_FKMAP
7807 if (p_altkeymap)
7808 {
7809 /*
7810 * to be consistent also for redo command, using '.'
7811 * set arrow_used to true and stop it - causing to redo
7812 * characters entered in one mode (normal/reverse insert).
7813 */
7814 arrow_used = TRUE;
7815 (void)stop_arrow();
7816 p_fkmap = curwin->w_p_rl ^ p_ri;
7817 if (p_fkmap && p_ri)
7818 State = INSERT;
7819 }
7820 else
7821#endif
7822 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7823 showmode();
7824}
7825#endif
7826
7827#ifdef FEAT_VISUAL
7828/*
7829 * If 'keymodel' contains "startsel", may start selection.
7830 * Returns TRUE when a CTRL-O and other keys stuffed.
7831 */
7832 static int
7833ins_start_select(c)
7834 int c;
7835{
7836 if (km_startsel)
7837 switch (c)
7838 {
7839 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 case K_PAGEUP:
7842 case K_KPAGEUP:
7843 case K_PAGEDOWN:
7844 case K_KPAGEDOWN:
7845# ifdef MACOS
7846 case K_LEFT:
7847 case K_RIGHT:
7848 case K_UP:
7849 case K_DOWN:
7850 case K_END:
7851 case K_HOME:
7852# endif
7853 if (!(mod_mask & MOD_MASK_SHIFT))
7854 break;
7855 /* FALLTHROUGH */
7856 case K_S_LEFT:
7857 case K_S_RIGHT:
7858 case K_S_UP:
7859 case K_S_DOWN:
7860 case K_S_END:
7861 case K_S_HOME:
7862 /* Start selection right away, the cursor can move with
7863 * CTRL-O when beyond the end of the line. */
7864 start_selection();
7865
7866 /* Execute the key in (insert) Select mode. */
7867 stuffcharReadbuff(Ctrl_O);
7868 if (mod_mask)
7869 {
7870 char_u buf[4];
7871
7872 buf[0] = K_SPECIAL;
7873 buf[1] = KS_MODIFIER;
7874 buf[2] = mod_mask;
7875 buf[3] = NUL;
7876 stuffReadbuff(buf);
7877 }
7878 stuffcharReadbuff(c);
7879 return TRUE;
7880 }
7881 return FALSE;
7882}
7883#endif
7884
7885/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007886 * <Insert> key in Insert mode: toggle insert/remplace mode.
7887 */
7888 static void
7889ins_insert(replaceState)
7890 int replaceState;
7891{
7892#ifdef FEAT_FKMAP
7893 if (p_fkmap && p_ri)
7894 {
7895 beep_flush();
7896 EMSG(farsi_text_3); /* encoded in Farsi */
7897 return;
7898 }
7899#endif
7900
7901#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007902# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007903 set_vim_var_string(VV_INSERTMODE,
7904 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007905# ifdef FEAT_VREPLACE
7906 replaceState == VREPLACE ? "v" :
7907# endif
7908 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007909# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007910 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7911#endif
7912 if (State & REPLACE_FLAG)
7913 State = INSERT | (State & LANGMAP);
7914 else
7915 State = replaceState | (State & LANGMAP);
7916 AppendCharToRedobuff(K_INS);
7917 showmode();
7918#ifdef CURSOR_SHAPE
7919 ui_cursor_shape(); /* may show different cursor shape */
7920#endif
7921}
7922
7923/*
7924 * Pressed CTRL-O in Insert mode.
7925 */
7926 static void
7927ins_ctrl_o()
7928{
7929#ifdef FEAT_VREPLACE
7930 if (State & VREPLACE_FLAG)
7931 restart_edit = 'V';
7932 else
7933#endif
7934 if (State & REPLACE_FLAG)
7935 restart_edit = 'R';
7936 else
7937 restart_edit = 'I';
7938#ifdef FEAT_VIRTUALEDIT
7939 if (virtual_active())
7940 ins_at_eol = FALSE; /* cursor always keeps its column */
7941 else
7942#endif
7943 ins_at_eol = (gchar_cursor() == NUL);
7944}
7945
7946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 * If the cursor is on an indent, ^T/^D insert/delete one
7948 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7949 * Always round the indent to 'shiftwith', this is compatible
7950 * with vi. But vi only supports ^T and ^D after an
7951 * autoindent, we support it everywhere.
7952 */
7953 static void
7954ins_shift(c, lastc)
7955 int c;
7956 int lastc;
7957{
7958 if (stop_arrow() == FAIL)
7959 return;
7960 AppendCharToRedobuff(c);
7961
7962 /*
7963 * 0^D and ^^D: remove all indent.
7964 */
7965 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7966 {
7967 --curwin->w_cursor.col;
7968 (void)del_char(FALSE); /* delete the '^' or '0' */
7969 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7970 if (State & REPLACE_FLAG)
7971 replace_pop_ins();
7972 if (lastc == '^')
7973 old_indent = get_indent(); /* remember curr. indent */
7974 change_indent(INDENT_SET, 0, TRUE, 0);
7975 }
7976 else
7977 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7978
7979 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7980 did_ai = FALSE;
7981#ifdef FEAT_SMARTINDENT
7982 did_si = FALSE;
7983 can_si = FALSE;
7984 can_si_back = FALSE;
7985#endif
7986#ifdef FEAT_CINDENT
7987 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7988#endif
7989}
7990
7991 static void
7992ins_del()
7993{
7994 int temp;
7995
7996 if (stop_arrow() == FAIL)
7997 return;
7998 if (gchar_cursor() == NUL) /* delete newline */
7999 {
8000 temp = curwin->w_cursor.col;
8001 if (!can_bs(BS_EOL) /* only if "eol" included */
8002 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8003 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8004 || do_join(FALSE) == FAIL)
8005 vim_beep();
8006 else
8007 curwin->w_cursor.col = temp;
8008 }
8009 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8010 vim_beep();
8011 did_ai = FALSE;
8012#ifdef FEAT_SMARTINDENT
8013 did_si = FALSE;
8014 can_si = FALSE;
8015 can_si_back = FALSE;
8016#endif
8017 AppendCharToRedobuff(K_DEL);
8018}
8019
8020/*
8021 * Handle Backspace, delete-word and delete-line in Insert mode.
8022 * Return TRUE when backspace was actually used.
8023 */
8024 static int
8025ins_bs(c, mode, inserted_space_p)
8026 int c;
8027 int mode;
8028 int *inserted_space_p;
8029{
8030 linenr_T lnum;
8031 int cc;
8032 int temp = 0; /* init for GCC */
8033 colnr_T mincol;
8034 int did_backspace = FALSE;
8035 int in_indent;
8036 int oldState;
8037#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008038 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039#endif
8040
8041 /*
8042 * can't delete anything in an empty file
8043 * can't backup past first character in buffer
8044 * can't backup past starting point unless 'backspace' > 1
8045 * can backup to a previous line if 'backspace' == 0
8046 */
8047 if ( bufempty()
8048 || (
8049#ifdef FEAT_RIGHTLEFT
8050 !revins_on &&
8051#endif
8052 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8053 || (!can_bs(BS_START)
8054 && (arrow_used
8055 || (curwin->w_cursor.lnum == Insstart.lnum
8056 && curwin->w_cursor.col <= Insstart.col)))
8057 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8058 && curwin->w_cursor.col <= ai_col)
8059 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8060 {
8061 vim_beep();
8062 return FALSE;
8063 }
8064
8065 if (stop_arrow() == FAIL)
8066 return FALSE;
8067 in_indent = inindent(0);
8068#ifdef FEAT_CINDENT
8069 if (in_indent)
8070 can_cindent = FALSE;
8071#endif
8072#ifdef FEAT_COMMENTS
8073 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8074#endif
8075#ifdef FEAT_RIGHTLEFT
8076 if (revins_on) /* put cursor after last inserted char */
8077 inc_cursor();
8078#endif
8079
8080#ifdef FEAT_VIRTUALEDIT
8081 /* Virtualedit:
8082 * BACKSPACE_CHAR eats a virtual space
8083 * BACKSPACE_WORD eats all coladd
8084 * BACKSPACE_LINE eats all coladd and keeps going
8085 */
8086 if (curwin->w_cursor.coladd > 0)
8087 {
8088 if (mode == BACKSPACE_CHAR)
8089 {
8090 --curwin->w_cursor.coladd;
8091 return TRUE;
8092 }
8093 if (mode == BACKSPACE_WORD)
8094 {
8095 curwin->w_cursor.coladd = 0;
8096 return TRUE;
8097 }
8098 curwin->w_cursor.coladd = 0;
8099 }
8100#endif
8101
8102 /*
8103 * delete newline!
8104 */
8105 if (curwin->w_cursor.col == 0)
8106 {
8107 lnum = Insstart.lnum;
8108 if (curwin->w_cursor.lnum == Insstart.lnum
8109#ifdef FEAT_RIGHTLEFT
8110 || revins_on
8111#endif
8112 )
8113 {
8114 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8115 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8116 return FALSE;
8117 --Insstart.lnum;
8118 Insstart.col = MAXCOL;
8119 }
8120 /*
8121 * In replace mode:
8122 * cc < 0: NL was inserted, delete it
8123 * cc >= 0: NL was replaced, put original characters back
8124 */
8125 cc = -1;
8126 if (State & REPLACE_FLAG)
8127 cc = replace_pop(); /* returns -1 if NL was inserted */
8128 /*
8129 * In replace mode, in the line we started replacing, we only move the
8130 * cursor.
8131 */
8132 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8133 {
8134 dec_cursor();
8135 }
8136 else
8137 {
8138#ifdef FEAT_VREPLACE
8139 if (!(State & VREPLACE_FLAG)
8140 || curwin->w_cursor.lnum > orig_line_count)
8141#endif
8142 {
8143 temp = gchar_cursor(); /* remember current char */
8144 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008145
8146 /* When "aw" is in 'formatoptions' we must delete the space at
8147 * the end of the line, otherwise the line will be broken
8148 * again when auto-formatting. */
8149 if (has_format_option(FO_AUTO)
8150 && has_format_option(FO_WHITE_PAR))
8151 {
8152 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8153 TRUE);
8154 int len;
8155
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008156 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008157 if (len > 0 && ptr[len - 1] == ' ')
8158 ptr[len - 1] = NUL;
8159 }
8160
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 (void)do_join(FALSE);
8162 if (temp == NUL && gchar_cursor() != NUL)
8163 inc_cursor();
8164 }
8165#ifdef FEAT_VREPLACE
8166 else
8167 dec_cursor();
8168#endif
8169
8170 /*
8171 * In REPLACE mode we have to put back the text that was replaced
8172 * by the NL. On the replace stack is first a NUL-terminated
8173 * sequence of characters that were deleted and then the
8174 * characters that NL replaced.
8175 */
8176 if (State & REPLACE_FLAG)
8177 {
8178 /*
8179 * Do the next ins_char() in NORMAL state, to
8180 * prevent ins_char() from replacing characters and
8181 * avoiding showmatch().
8182 */
8183 oldState = State;
8184 State = NORMAL;
8185 /*
8186 * restore characters (blanks) deleted after cursor
8187 */
8188 while (cc > 0)
8189 {
8190 temp = curwin->w_cursor.col;
8191#ifdef FEAT_MBYTE
8192 mb_replace_pop_ins(cc);
8193#else
8194 ins_char(cc);
8195#endif
8196 curwin->w_cursor.col = temp;
8197 cc = replace_pop();
8198 }
8199 /* restore the characters that NL replaced */
8200 replace_pop_ins();
8201 State = oldState;
8202 }
8203 }
8204 did_ai = FALSE;
8205 }
8206 else
8207 {
8208 /*
8209 * Delete character(s) before the cursor.
8210 */
8211#ifdef FEAT_RIGHTLEFT
8212 if (revins_on) /* put cursor on last inserted char */
8213 dec_cursor();
8214#endif
8215 mincol = 0;
8216 /* keep indent */
8217 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
8218#ifdef FEAT_RIGHTLEFT
8219 && !revins_on
8220#endif
8221 )
8222 {
8223 temp = curwin->w_cursor.col;
8224 beginline(BL_WHITE);
8225 if (curwin->w_cursor.col < (colnr_T)temp)
8226 mincol = curwin->w_cursor.col;
8227 curwin->w_cursor.col = temp;
8228 }
8229
8230 /*
8231 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8232 */
8233 if ( mode == BACKSPACE_CHAR
8234 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008235 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 && (*(ml_get_cursor() - 1) == TAB
8237 || (*(ml_get_cursor() - 1) == ' '
8238 && (!*inserted_space_p
8239 || arrow_used))))))
8240 {
8241 int ts;
8242 colnr_T vcol;
8243 colnr_T want_vcol;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008244#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 int extra = 0;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247
8248 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008249 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 ts = curbuf->b_p_sw;
8251 else
8252 ts = curbuf->b_p_sts;
8253 /* Compute the virtual column where we want to be. Since
8254 * 'showbreak' may get in the way, need to get the last column of
8255 * the previous character. */
8256 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8257 dec_cursor();
8258 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8259 inc_cursor();
8260 want_vcol = (want_vcol / ts) * ts;
8261
8262 /* delete characters until we are at or before want_vcol */
8263 while (vcol > want_vcol
8264 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
8265 {
8266 dec_cursor();
8267 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8268 if (State & REPLACE_FLAG)
8269 {
8270 /* Don't delete characters before the insert point when in
8271 * Replace mode */
8272 if (curwin->w_cursor.lnum != Insstart.lnum
8273 || curwin->w_cursor.col >= Insstart.col)
8274 {
8275#if 0 /* what was this for? It causes problems when sw != ts. */
8276 if (State == REPLACE && (int)vcol < want_vcol)
8277 {
8278 (void)del_char(FALSE);
8279 extra = 2; /* don't pop too much */
8280 }
8281 else
8282#endif
8283 replace_do_bs();
8284 }
8285 }
8286 else
8287 (void)del_char(FALSE);
8288 }
8289
8290 /* insert extra spaces until we are at want_vcol */
8291 while (vcol < want_vcol)
8292 {
8293 /* Remember the first char we inserted */
8294 if (curwin->w_cursor.lnum == Insstart.lnum
8295 && curwin->w_cursor.col < Insstart.col)
8296 Insstart.col = curwin->w_cursor.col;
8297
8298#ifdef FEAT_VREPLACE
8299 if (State & VREPLACE_FLAG)
8300 ins_char(' ');
8301 else
8302#endif
8303 {
8304 ins_str((char_u *)" ");
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008305 if ((State & REPLACE_FLAG) /* && extra <= 1 */)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008307#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 if (extra)
8309 replace_push_off(NUL);
8310 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 replace_push(NUL);
8313 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008314#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 if (extra == 2)
8316 extra = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 }
8319 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8320 }
8321 }
8322
8323 /*
8324 * Delete upto starting point, start of line or previous word.
8325 */
8326 else do
8327 {
8328#ifdef FEAT_RIGHTLEFT
8329 if (!revins_on) /* put cursor on char to be deleted */
8330#endif
8331 dec_cursor();
8332
8333 /* start of word? */
8334 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8335 {
8336 mode = BACKSPACE_WORD_NOT_SPACE;
8337 temp = vim_iswordc(gchar_cursor());
8338 }
8339 /* end of word? */
8340 else if (mode == BACKSPACE_WORD_NOT_SPACE
8341 && (vim_isspace(cc = gchar_cursor())
8342 || vim_iswordc(cc) != temp))
8343 {
8344#ifdef FEAT_RIGHTLEFT
8345 if (!revins_on)
8346#endif
8347 inc_cursor();
8348#ifdef FEAT_RIGHTLEFT
8349 else if (State & REPLACE_FLAG)
8350 dec_cursor();
8351#endif
8352 break;
8353 }
8354 if (State & REPLACE_FLAG)
8355 replace_do_bs();
8356 else
8357 {
8358#ifdef FEAT_MBYTE
8359 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008360 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361#endif
8362 (void)del_char(FALSE);
8363#ifdef FEAT_MBYTE
8364 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008365 * If there are combining characters and 'delcombine' is set
8366 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 * character.
8368 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008369 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 inc_cursor();
8371#endif
8372#ifdef FEAT_RIGHTLEFT
8373 if (revins_chars)
8374 {
8375 revins_chars--;
8376 revins_legal++;
8377 }
8378 if (revins_on && gchar_cursor() == NUL)
8379 break;
8380#endif
8381 }
8382 /* Just a single backspace?: */
8383 if (mode == BACKSPACE_CHAR)
8384 break;
8385 } while (
8386#ifdef FEAT_RIGHTLEFT
8387 revins_on ||
8388#endif
8389 (curwin->w_cursor.col > mincol
8390 && (curwin->w_cursor.lnum != Insstart.lnum
8391 || curwin->w_cursor.col != Insstart.col)));
8392 did_backspace = TRUE;
8393 }
8394#ifdef FEAT_SMARTINDENT
8395 did_si = FALSE;
8396 can_si = FALSE;
8397 can_si_back = FALSE;
8398#endif
8399 if (curwin->w_cursor.col <= 1)
8400 did_ai = FALSE;
8401 /*
8402 * It's a little strange to put backspaces into the redo
8403 * buffer, but it makes auto-indent a lot easier to deal
8404 * with.
8405 */
8406 AppendCharToRedobuff(c);
8407
8408 /* If deleted before the insertion point, adjust it */
8409 if (curwin->w_cursor.lnum == Insstart.lnum
8410 && curwin->w_cursor.col < Insstart.col)
8411 Insstart.col = curwin->w_cursor.col;
8412
8413 /* vi behaviour: the cursor moves backward but the character that
8414 * was there remains visible
8415 * Vim behaviour: the cursor moves backward and the character that
8416 * was there is erased from the screen.
8417 * We can emulate the vi behaviour by pretending there is a dollar
8418 * displayed even when there isn't.
8419 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8420 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8421 dollar_vcol = curwin->w_virtcol;
8422
8423 return did_backspace;
8424}
8425
8426#ifdef FEAT_MOUSE
8427 static void
8428ins_mouse(c)
8429 int c;
8430{
8431 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008432 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433
8434# ifdef FEAT_GUI
8435 /* When GUI is active, also move/paste when 'mouse' is empty */
8436 if (!gui.in_use)
8437# endif
8438 if (!mouse_has(MOUSE_INSERT))
8439 return;
8440
8441 undisplay_dollar();
8442 tpos = curwin->w_cursor;
8443 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8444 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008445#ifdef FEAT_WINDOWS
8446 win_T *new_curwin = curwin;
8447
8448 if (curwin != old_curwin && win_valid(old_curwin))
8449 {
8450 /* Mouse took us to another window. We need to go back to the
8451 * previous one to stop insert there properly. */
8452 curwin = old_curwin;
8453 curbuf = curwin->w_buffer;
8454 }
8455#endif
8456 start_arrow(curwin == old_curwin ? &tpos : NULL);
8457#ifdef FEAT_WINDOWS
8458 if (curwin != new_curwin && win_valid(new_curwin))
8459 {
8460 curwin = new_curwin;
8461 curbuf = curwin->w_buffer;
8462 }
8463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464# ifdef FEAT_CINDENT
8465 can_cindent = TRUE;
8466# endif
8467 }
8468
8469#ifdef FEAT_WINDOWS
8470 /* redraw status lines (in case another window became active) */
8471 redraw_statuslines();
8472#endif
8473}
8474
8475 static void
8476ins_mousescroll(up)
8477 int up;
8478{
8479 pos_T tpos;
8480# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8481 win_T *old_curwin;
8482# endif
8483
8484 tpos = curwin->w_cursor;
8485
8486# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8487 old_curwin = curwin;
8488
8489 /* Currently the mouse coordinates are only known in the GUI. */
8490 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8491 {
8492 int row, col;
8493
8494 row = mouse_row;
8495 col = mouse_col;
8496
8497 /* find the window at the pointer coordinates */
8498 curwin = mouse_find_win(&row, &col);
8499 curbuf = curwin->w_buffer;
8500 }
8501 if (curwin == old_curwin)
8502# endif
8503 undisplay_dollar();
8504
8505 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8506 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8507 else
8508 scroll_redraw(up, 3L);
8509
8510# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8511 curwin->w_redr_status = TRUE;
8512
8513 curwin = old_curwin;
8514 curbuf = curwin->w_buffer;
8515# endif
8516
8517 if (!equalpos(curwin->w_cursor, tpos))
8518 {
8519 start_arrow(&tpos);
8520# ifdef FEAT_CINDENT
8521 can_cindent = TRUE;
8522# endif
8523 }
8524}
8525#endif
8526
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008527#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008528 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008529ins_tabline(c)
8530 int c;
8531{
8532 /* We will be leaving the current window, unless closing another tab. */
8533 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8534 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8535 {
8536 undisplay_dollar();
8537 start_arrow(&curwin->w_cursor);
8538# ifdef FEAT_CINDENT
8539 can_cindent = TRUE;
8540# endif
8541 }
8542
8543 if (c == K_TABLINE)
8544 goto_tabpage(current_tab);
8545 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008546 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008547 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008548 redraw_statuslines(); /* will redraw the tabline when needed */
8549 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008550}
8551#endif
8552
8553#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 void
8555ins_scroll()
8556{
8557 pos_T tpos;
8558
8559 undisplay_dollar();
8560 tpos = curwin->w_cursor;
8561 if (gui_do_scroll())
8562 {
8563 start_arrow(&tpos);
8564# ifdef FEAT_CINDENT
8565 can_cindent = TRUE;
8566# endif
8567 }
8568}
8569
8570 void
8571ins_horscroll()
8572{
8573 pos_T tpos;
8574
8575 undisplay_dollar();
8576 tpos = curwin->w_cursor;
8577 if (gui_do_horiz_scroll())
8578 {
8579 start_arrow(&tpos);
8580# ifdef FEAT_CINDENT
8581 can_cindent = TRUE;
8582# endif
8583 }
8584}
8585#endif
8586
8587 static void
8588ins_left()
8589{
8590 pos_T tpos;
8591
8592#ifdef FEAT_FOLDING
8593 if ((fdo_flags & FDO_HOR) && KeyTyped)
8594 foldOpenCursor();
8595#endif
8596 undisplay_dollar();
8597 tpos = curwin->w_cursor;
8598 if (oneleft() == OK)
8599 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00008600#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8601 /* Only call start_arrow() when not busy with preediting, it will
8602 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8603 if (!im_is_preediting())
8604#endif
8605 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606#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}