blob: e8396d1398403312eeadc421948ab252bfc9c3d3 [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 Moolenaar488c6512005-08-11 20:09:58 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^P^S^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 Moolenaar488c6512005-08-11 20:09:58 +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 Moolenaard289f132006-03-11 21:30:53 +000072 char_u *cp_kind; /* kind menu text (allocated, can be NULL) */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000073 char_u *cp_extra; /* extra menu text (allocated, can be NULL) */
74 char_u *cp_info; /* verbose info (can be NULL) */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000075 char_u *cp_fname; /* file containing the match, allocated when
76 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000077 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
78 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000079};
80
Bram Moolenaar572cb562005-08-05 21:35:02 +000081#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#define FREE_FNAME (2)
83
84/*
85 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000086 * "compl_first_match" points to the start of the list.
87 * "compl_curr_match" points to the currently selected entry.
88 * "compl_shown_match" is different from compl_curr_match during
89 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000090 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000091static compl_T *compl_first_match = NULL;
92static compl_T *compl_curr_match = NULL;
93static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
Bram Moolenaara6557602006-02-04 22:43:20 +000095/* When "compl_leader" is not NULL only matches that start with this string
96 * are used. */
97static char_u *compl_leader = NULL;
98
Bram Moolenaarc7453f52006-02-10 23:20:28 +000099static int compl_get_longest = FALSE; /* put longest common string
100 in compl_leader */
101
Bram Moolenaara6557602006-02-04 22:43:20 +0000102static int compl_used_match; /* Selected one of the matches. When
103 FALSE the match was edited or using
104 the longest common string. */
105
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000106/* When the first completion is done "compl_started" is set. When it's
107 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000108static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000109
Bram Moolenaar572cb562005-08-05 21:35:02 +0000110static int compl_matches = 0;
111static char_u *compl_pattern = NULL;
112static int compl_direction = FORWARD;
113static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000114static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000115static pos_T compl_startpos;
116static colnr_T compl_col = 0; /* column where the text starts
117 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000118static char_u *compl_orig_text = NULL; /* text as it was before
119 * completion started */
120static int compl_cont_mode = 0;
121static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000123static void ins_ctrl_x __ARGS((void));
124static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000125static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000126static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000127static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000129static void ins_compl_upd_pum __ARGS((void));
130static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000131static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000132static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000133static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000134static 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 +0000135static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136static void ins_compl_free __ARGS((void));
137static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000138static int ins_compl_bs __ARGS((void));
139static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000140static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000141static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000142static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000144#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
145static void ins_compl_add_list __ARGS((list_T *list));
146#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000147static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148static void ins_compl_delete __ARGS((void));
149static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000150static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000151static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000152static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000153static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000154static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static int ins_complete __ARGS((int c));
156static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
157#endif /* FEAT_INS_EXPAND */
158
159#define BACKSPACE_CHAR 1
160#define BACKSPACE_WORD 2
161#define BACKSPACE_WORD_NOT_SPACE 3
162#define BACKSPACE_LINE 4
163
Bram Moolenaar754b5602006-02-09 23:53:20 +0000164static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165static void ins_ctrl_v __ARGS((void));
166static void undisplay_dollar __ARGS((void));
167static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000168static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169static void check_auto_format __ARGS((int));
170static void redo_literal __ARGS((int c));
171static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000172#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000173static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000174static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000175static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
178static int echeck_abbr __ARGS((int));
179static void replace_push_off __ARGS((int c));
180static int replace_pop __ARGS((void));
181static void replace_join __ARGS((int off));
182static void replace_pop_ins __ARGS((void));
183#ifdef FEAT_MBYTE
184static void mb_replace_pop_ins __ARGS((int cc));
185#endif
186static void replace_flush __ARGS((void));
187static void replace_do_bs __ARGS((void));
188#ifdef FEAT_CINDENT
189static int cindent_on __ARGS((void));
190#endif
191static void ins_reg __ARGS((void));
192static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000193static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000194static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#ifdef FEAT_RIGHTLEFT
196static void ins_ctrl_ __ARGS((void));
197#endif
198#ifdef FEAT_VISUAL
199static int ins_start_select __ARGS((int c));
200#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000201static void ins_insert __ARGS((int replaceState));
202static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203static void ins_shift __ARGS((int c, int lastc));
204static void ins_del __ARGS((void));
205static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
206#ifdef FEAT_MOUSE
207static void ins_mouse __ARGS((int c));
208static void ins_mousescroll __ARGS((int up));
209#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000210#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
211static void ins_tabline __ARGS((int c));
212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213static void ins_left __ARGS((void));
214static void ins_home __ARGS((int c));
215static void ins_end __ARGS((int c));
216static void ins_s_left __ARGS((void));
217static void ins_right __ARGS((void));
218static void ins_s_right __ARGS((void));
219static void ins_up __ARGS((int startcol));
220static void ins_pageup __ARGS((void));
221static void ins_down __ARGS((int startcol));
222static void ins_pagedown __ARGS((void));
223#ifdef FEAT_DND
224static void ins_drop __ARGS((void));
225#endif
226static int ins_tab __ARGS((void));
227static int ins_eol __ARGS((int c));
228#ifdef FEAT_DIGRAPHS
229static int ins_digraph __ARGS((void));
230#endif
231static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000232static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#ifdef FEAT_SMARTINDENT
234static void ins_try_si __ARGS((int c));
235#endif
236static colnr_T get_nolist_virtcol __ARGS((void));
237
238static colnr_T Insstart_textlen; /* length of line when insert started */
239static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
240
241static char_u *last_insert = NULL; /* the text of the previous insert,
242 K_SPECIAL and CSI are escaped */
243static int last_insert_skip; /* nr of chars in front of previous insert */
244static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000245static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247#ifdef FEAT_CINDENT
248static int can_cindent; /* may do cindenting on this line */
249#endif
250
251static int old_indent = 0; /* for ^^D command in insert mode */
252
253#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000254static int revins_on; /* reverse insert mode on */
255static int revins_chars; /* how much to skip after edit */
256static int revins_legal; /* was the last char 'legal'? */
257static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#endif
259
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260static int ins_need_undo; /* call u_save() before inserting a
261 char. Set when edit() is called.
262 after that arrow_used is used. */
263
264static int did_add_space = FALSE; /* auto_format() added an extra space
265 under the cursor */
266
267/*
268 * edit(): Start inserting text.
269 *
270 * "cmdchar" can be:
271 * 'i' normal insert command
272 * 'a' normal append command
273 * 'R' replace command
274 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
275 * but still only one <CR> is inserted. The <Esc> is not used for redo.
276 * 'g' "gI" command.
277 * 'V' "gR" command for Virtual Replace mode.
278 * 'v' "gr" command for single character Virtual Replace mode.
279 *
280 * This function is not called recursively. For CTRL-O commands, it returns
281 * and lets the caller handle the Normal-mode command.
282 *
283 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
284 */
285 int
286edit(cmdchar, startln, count)
287 int cmdchar;
288 int startln; /* if set, insert at start of line */
289 long count;
290{
291 int c = 0;
292 char_u *ptr;
293 int lastc;
294 colnr_T mincol;
295 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 int i;
297 int did_backspace = TRUE; /* previous char was backspace */
298#ifdef FEAT_CINDENT
299 int line_is_white = FALSE; /* line is empty before insert */
300#endif
301 linenr_T old_topline = 0; /* topline before insertion */
302#ifdef FEAT_DIFF
303 int old_topfill = -1;
304#endif
305 int inserted_space = FALSE; /* just inserted a space */
306 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000307 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000309 /* Remember whether editing was restarted after CTRL-O. */
310 did_restart_edit = restart_edit;
311
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 /* sleep before redrawing, needed for "CTRL-O :" that results in an
313 * error message */
314 check_for_delay(TRUE);
315
316#ifdef HAVE_SANDBOX
317 /* Don't allow inserting in the sandbox. */
318 if (sandbox != 0)
319 {
320 EMSG(_(e_sandbox));
321 return FALSE;
322 }
323#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000324 /* Don't allow changes in the buffer while editing the cmdline. The
325 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000326 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000327 {
328 EMSG(_(e_secure));
329 return FALSE;
330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331
332#ifdef FEAT_INS_EXPAND
333 ins_compl_clear(); /* clear stuff for CTRL-X mode */
334#endif
335
Bram Moolenaar843ee412004-06-30 16:16:41 +0000336#ifdef FEAT_AUTOCMD
337 /*
338 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
339 */
340 if (cmdchar != 'r' && cmdchar != 'v')
341 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000342# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000343 if (cmdchar == 'R')
344 ptr = (char_u *)"r";
345 else if (cmdchar == 'V')
346 ptr = (char_u *)"v";
347 else
348 ptr = (char_u *)"i";
349 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000350# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000351 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
352 }
353#endif
354
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355#ifdef FEAT_MOUSE
356 /*
357 * When doing a paste with the middle mouse button, Insstart is set to
358 * where the paste started.
359 */
360 if (where_paste_started.lnum != 0)
361 Insstart = where_paste_started;
362 else
363#endif
364 {
365 Insstart = curwin->w_cursor;
366 if (startln)
367 Insstart.col = 0;
368 }
369 Insstart_textlen = linetabsize(ml_get_curline());
370 Insstart_blank_vcol = MAXCOL;
371 if (!did_ai)
372 ai_col = 0;
373
374 if (cmdchar != NUL && restart_edit == 0)
375 {
376 ResetRedobuff();
377 AppendNumberToRedobuff(count);
378#ifdef FEAT_VREPLACE
379 if (cmdchar == 'V' || cmdchar == 'v')
380 {
381 /* "gR" or "gr" command */
382 AppendCharToRedobuff('g');
383 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
384 }
385 else
386#endif
387 {
388 AppendCharToRedobuff(cmdchar);
389 if (cmdchar == 'g') /* "gI" command */
390 AppendCharToRedobuff('I');
391 else if (cmdchar == 'r') /* "r<CR>" command */
392 count = 1; /* insert only one <CR> */
393 }
394 }
395
396 if (cmdchar == 'R')
397 {
398#ifdef FEAT_FKMAP
399 if (p_fkmap && p_ri)
400 {
401 beep_flush();
402 EMSG(farsi_text_3); /* encoded in Farsi */
403 State = INSERT;
404 }
405 else
406#endif
407 State = REPLACE;
408 }
409#ifdef FEAT_VREPLACE
410 else if (cmdchar == 'V' || cmdchar == 'v')
411 {
412 State = VREPLACE;
413 replaceState = VREPLACE;
414 orig_line_count = curbuf->b_ml.ml_line_count;
415 vr_lines_changed = 1;
416 }
417#endif
418 else
419 State = INSERT;
420
421 stop_insert_mode = FALSE;
422
423 /*
424 * Need to recompute the cursor position, it might move when the cursor is
425 * on a TAB or special character.
426 */
427 curs_columns(TRUE);
428
429 /*
430 * Enable langmap or IME, indicated by 'iminsert'.
431 * Note that IME may enabled/disabled without us noticing here, thus the
432 * 'iminsert' value may not reflect what is actually used. It is updated
433 * when hitting <Esc>.
434 */
435 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
436 State |= LANGMAP;
437#ifdef USE_IM_CONTROL
438 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
439#endif
440
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441#ifdef FEAT_MOUSE
442 setmouse();
443#endif
444#ifdef FEAT_CMDL_INFO
445 clear_showcmd();
446#endif
447#ifdef FEAT_RIGHTLEFT
448 /* there is no reverse replace mode */
449 revins_on = (State == INSERT && p_ri);
450 if (revins_on)
451 undisplay_dollar();
452 revins_chars = 0;
453 revins_legal = 0;
454 revins_scol = -1;
455#endif
456
457 /*
458 * Handle restarting Insert mode.
459 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
460 * restart_edit non-zero, and something in the stuff buffer.
461 */
462 if (restart_edit != 0 && stuff_empty())
463 {
464#ifdef FEAT_MOUSE
465 /*
466 * After a paste we consider text typed to be part of the insert for
467 * the pasted text. You can backspace over the pasted text too.
468 */
469 if (where_paste_started.lnum)
470 arrow_used = FALSE;
471 else
472#endif
473 arrow_used = TRUE;
474 restart_edit = 0;
475
476 /*
477 * If the cursor was after the end-of-line before the CTRL-O and it is
478 * now at the end-of-line, put it after the end-of-line (this is not
479 * correct in very rare cases).
480 * Also do this if curswant is greater than the current virtual
481 * column. Eg after "^O$" or "^O80|".
482 */
483 validate_virtcol();
484 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000485 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 || curwin->w_curswant > curwin->w_virtcol)
487 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
488 {
489 if (ptr[1] == NUL)
490 ++curwin->w_cursor.col;
491#ifdef FEAT_MBYTE
492 else if (has_mbyte)
493 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000494 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 if (ptr[i] == NUL)
496 curwin->w_cursor.col += i;
497 }
498#endif
499 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000500 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 }
502 else
503 arrow_used = FALSE;
504
505 /* we are in insert mode now, don't need to start it anymore */
506 need_start_insertmode = FALSE;
507
508 /* Need to save the line for undo before inserting the first char. */
509 ins_need_undo = TRUE;
510
511#ifdef FEAT_MOUSE
512 where_paste_started.lnum = 0;
513#endif
514#ifdef FEAT_CINDENT
515 can_cindent = TRUE;
516#endif
517#ifdef FEAT_FOLDING
518 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
519 * restarting. */
520 if (!p_im && did_restart_edit == 0)
521 foldOpenCursor();
522#endif
523
524 /*
525 * If 'showmode' is set, show the current (insert/replace/..) mode.
526 * A warning message for changing a readonly file is given here, before
527 * actually changing anything. It's put after the mode, if any.
528 */
529 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000530 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 i = showmode();
532
533 if (!p_im && did_restart_edit == 0)
534 change_warning(i + 1);
535
536#ifdef CURSOR_SHAPE
537 ui_cursor_shape(); /* may show different cursor shape */
538#endif
539#ifdef FEAT_DIGRAPHS
540 do_digraph(-1); /* clear digraphs */
541#endif
542
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000543 /*
544 * Get the current length of the redo buffer, those characters have to be
545 * skipped if we want to get to the inserted characters.
546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 ptr = get_inserted();
548 if (ptr == NULL)
549 new_insert_skip = 0;
550 else
551 {
552 new_insert_skip = (int)STRLEN(ptr);
553 vim_free(ptr);
554 }
555
556 old_indent = 0;
557
558 /*
559 * Main loop in Insert mode: repeat until Insert mode is left.
560 */
561 for (;;)
562 {
563#ifdef FEAT_RIGHTLEFT
564 if (!revins_legal)
565 revins_scol = -1; /* reset on illegal motions */
566 else
567 revins_legal = 0;
568#endif
569 if (arrow_used) /* don't repeat insert when arrow key used */
570 count = 0;
571
572 if (stop_insert_mode)
573 {
574 /* ":stopinsert" used or 'insertmode' reset */
575 count = 0;
576 goto doESCkey;
577 }
578
579 /* set curwin->w_curswant for next K_DOWN or K_UP */
580 if (!arrow_used)
581 curwin->w_set_curswant = TRUE;
582
583 /* If there is no typeahead may check for timestamps (e.g., for when a
584 * menu invoked a shell command). */
585 if (stuff_empty())
586 {
587 did_check_timestamps = FALSE;
588 if (need_check_timestamps)
589 check_timestamps(FALSE);
590 }
591
592 /*
593 * When emsg() was called msg_scroll will have been set.
594 */
595 msg_scroll = FALSE;
596
597#ifdef FEAT_GUI
598 /* When 'mousefocus' is set a mouse movement may have taken us to
599 * another window. "need_mouse_correct" may then be set because of an
600 * autocommand. */
601 if (need_mouse_correct)
602 gui_mouse_correct();
603#endif
604
605#ifdef FEAT_FOLDING
606 /* Open fold at the cursor line, according to 'foldopen'. */
607 if (fdo_flags & FDO_INSERT)
608 foldOpenCursor();
609 /* Close folds where the cursor isn't, according to 'foldclose' */
610 if (!char_avail())
611 foldCheckClose();
612#endif
613
614 /*
615 * If we inserted a character at the last position of the last line in
616 * the window, scroll the window one line up. This avoids an extra
617 * redraw.
618 * This is detected when the cursor column is smaller after inserting
619 * something.
620 * Don't do this when the topline changed already, it has
621 * already been adjusted (by insertchar() calling open_line())).
622 */
623 if (curbuf->b_mod_set
624 && curwin->w_p_wrap
625 && !did_backspace
626 && curwin->w_topline == old_topline
627#ifdef FEAT_DIFF
628 && curwin->w_topfill == old_topfill
629#endif
630 )
631 {
632 mincol = curwin->w_wcol;
633 validate_cursor_col();
634
635 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
636 && curwin->w_wrow == W_WINROW(curwin)
637 + curwin->w_height - 1 - p_so
638 && (curwin->w_cursor.lnum != curwin->w_topline
639#ifdef FEAT_DIFF
640 || curwin->w_topfill > 0
641#endif
642 ))
643 {
644#ifdef FEAT_DIFF
645 if (curwin->w_topfill > 0)
646 --curwin->w_topfill;
647 else
648#endif
649#ifdef FEAT_FOLDING
650 if (hasFolding(curwin->w_topline, NULL, &old_topline))
651 set_topline(curwin, old_topline + 1);
652 else
653#endif
654 set_topline(curwin, curwin->w_topline + 1);
655 }
656 }
657
658 /* May need to adjust w_topline to show the cursor. */
659 update_topline();
660
661 did_backspace = FALSE;
662
663 validate_cursor(); /* may set must_redraw */
664
665 /*
666 * Redraw the display when no characters are waiting.
667 * Also shows mode, ruler and positions cursor.
668 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000669 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
671#ifdef FEAT_SCROLLBIND
672 if (curwin->w_p_scb)
673 do_check_scrollbind(TRUE);
674#endif
675
676 update_curswant();
677 old_topline = curwin->w_topline;
678#ifdef FEAT_DIFF
679 old_topfill = curwin->w_topfill;
680#endif
681
682#ifdef USE_ON_FLY_SCROLL
683 dont_scroll = FALSE; /* allow scrolling here */
684#endif
685
686 /*
687 * Get a character for Insert mode.
688 */
689 lastc = c; /* remember previous char for CTRL-D */
690 c = safe_vgetc();
691
692#ifdef FEAT_RIGHTLEFT
693 if (p_hkmap && KeyTyped)
694 c = hkmap(c); /* Hebrew mode mapping */
695#endif
696#ifdef FEAT_FKMAP
697 if (p_fkmap && KeyTyped)
698 c = fkmap(c); /* Farsi mode mapping */
699#endif
700
701#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000702 /*
703 * Special handling of keys while the popup menu is visible or wanted
704 * and the cursor is still in the completed word.
705 */
706 if (compl_started && pum_wanted() && curwin->w_cursor.col >= compl_col)
Bram Moolenaara6557602006-02-04 22:43:20 +0000707 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000708 /* BS: Delete one character from "compl_leader". */
709 if ((c == K_BS || c == Ctrl_H)
710 && curwin->w_cursor.col > compl_col && ins_compl_bs())
Bram Moolenaara6557602006-02-04 22:43:20 +0000711 continue;
712
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000713 /* When no match was selected or it was edited. */
714 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000715 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000716 /* CTRL-L: Add one character from the current match to
717 * "compl_leader". */
718 if (c == Ctrl_L)
719 {
720 ins_compl_addfrommatch();
721 continue;
722 }
723
Bram Moolenaardf1bdc92006-02-23 21:32:16 +0000724 /* A printable, non-white character: Add to "compl_leader". */
725 if (vim_isprintc(c) && !vim_iswhite(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000726 {
727 ins_compl_addleader(c);
728 continue;
729 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000730
731 /* Pressing Enter selects the current match. */
732 if (c == CAR || c == K_KENTER || c == NL)
733 {
734 ins_compl_delete();
735 ins_compl_insert();
736 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000737 }
738 }
739
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
741 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000742 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000743 if (c != K_IGNORE && ins_compl_prep(c))
744 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745#endif
746
Bram Moolenaar488c6512005-08-11 20:09:58 +0000747 /* CTRL-\ CTRL-N goes to Normal mode,
748 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
749 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 if (c == Ctrl_BSL)
751 {
752 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000753 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 ++no_mapping;
755 ++allow_keys;
756 c = safe_vgetc();
757 --no_mapping;
758 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000759 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000761 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 vungetc(c);
763 c = Ctrl_BSL;
764 }
765 else if (c == Ctrl_G && p_im)
766 continue;
767 else
768 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000769 if (c == Ctrl_O)
770 {
771 ins_ctrl_o();
772 ins_at_eol = FALSE; /* cursor keeps its column */
773 nomove = TRUE;
774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 count = 0;
776 goto doESCkey;
777 }
778 }
779
780#ifdef FEAT_DIGRAPHS
781 c = do_digraph(c);
782#endif
783
784#ifdef FEAT_INS_EXPAND
785 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
786 goto docomplete;
787#endif
788 if (c == Ctrl_V || c == Ctrl_Q)
789 {
790 ins_ctrl_v();
791 c = Ctrl_V; /* pretend CTRL-V is last typed character */
792 continue;
793 }
794
795#ifdef FEAT_CINDENT
796 if (cindent_on()
797# ifdef FEAT_INS_EXPAND
798 && ctrl_x_mode == 0
799# endif
800 )
801 {
802 /* A key name preceded by a bang means this key is not to be
803 * inserted. Skip ahead to the re-indenting below.
804 * A key name preceded by a star means that indenting has to be
805 * done before inserting the key. */
806 line_is_white = inindent(0);
807 if (in_cinkeys(c, '!', line_is_white))
808 goto force_cindent;
809 if (can_cindent && in_cinkeys(c, '*', line_is_white)
810 && stop_arrow() == OK)
811 do_c_expr_indent();
812 }
813#endif
814
815#ifdef FEAT_RIGHTLEFT
816 if (curwin->w_p_rl)
817 switch (c)
818 {
819 case K_LEFT: c = K_RIGHT; break;
820 case K_S_LEFT: c = K_S_RIGHT; break;
821 case K_C_LEFT: c = K_C_RIGHT; break;
822 case K_RIGHT: c = K_LEFT; break;
823 case K_S_RIGHT: c = K_S_LEFT; break;
824 case K_C_RIGHT: c = K_C_LEFT; break;
825 }
826#endif
827
828#ifdef FEAT_VISUAL
829 /*
830 * If 'keymodel' contains "startsel", may start selection. If it
831 * does, a CTRL-O and c will be stuffed, we need to get these
832 * characters.
833 */
834 if (ins_start_select(c))
835 continue;
836#endif
837
838 /*
839 * The big switch to handle a character in insert mode.
840 */
841 switch (c)
842 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000843 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 if (echeck_abbr(ESC + ABBR_OFF))
845 break;
846 /*FALLTHROUGH*/
847
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000848 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849#ifdef FEAT_CMDWIN
850 if (c == Ctrl_C && cmdwin_type != 0)
851 {
852 /* Close the cmdline window. */
853 cmdwin_result = K_IGNORE;
854 got_int = FALSE; /* don't stop executing autocommands et al. */
855 goto doESCkey;
856 }
857#endif
858
859#ifdef UNIX
860do_intr:
861#endif
862 /* when 'insertmode' set, and not halfway a mapping, don't leave
863 * Insert mode */
864 if (goto_im())
865 {
866 if (got_int)
867 {
868 (void)vgetc(); /* flush all buffers */
869 got_int = FALSE;
870 }
871 else
872 vim_beep();
873 break;
874 }
875doESCkey:
876 /*
877 * This is the ONLY return from edit()!
878 */
879 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
880 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000881 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 o_lnum = curwin->w_cursor.lnum;
883
Bram Moolenaar488c6512005-08-11 20:09:58 +0000884 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000885 {
886#ifdef FEAT_AUTOCMD
887 if (cmdchar != 'r' && cmdchar != 'v')
888 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
889 FALSE, curbuf);
890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 continue;
894
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000895 case Ctrl_Z: /* suspend when 'insertmode' set */
896 if (!p_im)
897 goto normalchar; /* insert CTRL-Z as normal char */
898 stuffReadbuff((char_u *)":st\r");
899 c = Ctrl_O;
900 /*FALLTHROUGH*/
901
902 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000903#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000904 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000905 goto docomplete;
906#endif
907 if (echeck_abbr(Ctrl_O + ABBR_OFF))
908 break;
909 ins_ctrl_o();
910 count = 0;
911 goto doESCkey;
912
Bram Moolenaar572cb562005-08-05 21:35:02 +0000913 case K_INS: /* toggle insert/replace mode */
914 case K_KINS:
915 ins_insert(replaceState);
916 break;
917
918 case K_SELECT: /* end of Select mode mapping - ignore */
919 break;
920
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000921#ifdef FEAT_SNIFF
922 case K_SNIFF: /* Sniff command received */
923 stuffcharReadbuff(K_SNIFF);
924 goto doESCkey;
925#endif
926
927 case K_HELP: /* Help key works like <ESC> <Help> */
928 case K_F1:
929 case K_XF1:
930 stuffcharReadbuff(K_HELP);
931 if (p_im)
932 need_start_insertmode = TRUE;
933 goto doESCkey;
934
935#ifdef FEAT_NETBEANS_INTG
936 case K_F21: /* NetBeans command */
937 ++no_mapping; /* don't map the next key hits */
938 i = safe_vgetc();
939 --no_mapping;
940 netbeans_keycommand(i);
941 break;
942#endif
943
944 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 case NUL:
946 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000947 /* For ^@ the trailing ESC will end the insert, unless there is an
948 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
950 && c != Ctrl_A && !p_im)
951 goto doESCkey; /* quit insert mode */
952 inserted_space = FALSE;
953 break;
954
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000955 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 ins_reg();
957 auto_format(FALSE, TRUE);
958 inserted_space = FALSE;
959 break;
960
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000961 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 ins_ctrl_g();
963 break;
964
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000965 case Ctrl_HAT: /* switch input mode and/or langmap */
966 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 break;
968
969#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000970 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 if (!p_ari)
972 goto normalchar;
973 ins_ctrl_();
974 break;
975#endif
976
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000977 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
979 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
980 goto docomplete;
981#endif
982 /* FALLTHROUGH */
983
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000984 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985# ifdef FEAT_INS_EXPAND
986 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
987 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000988 if (has_compl_option(FALSE))
989 goto docomplete;
990 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 }
992# endif
993 ins_shift(c, lastc);
994 auto_format(FALSE, TRUE);
995 inserted_space = FALSE;
996 break;
997
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000998 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 case K_KDEL:
1000 ins_del();
1001 auto_format(FALSE, TRUE);
1002 break;
1003
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001004 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 case Ctrl_H:
1006 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1007 auto_format(FALSE, TRUE);
1008 break;
1009
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001010 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1012 auto_format(FALSE, TRUE);
1013 break;
1014
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001015 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001016# ifdef FEAT_COMPL_FUNC
1017 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001018 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001019 goto docomplete;
1020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1022 auto_format(FALSE, TRUE);
1023 inserted_space = FALSE;
1024 break;
1025
1026#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001027 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 case K_LEFTMOUSE_NM:
1029 case K_LEFTDRAG:
1030 case K_LEFTRELEASE:
1031 case K_LEFTRELEASE_NM:
1032 case K_MIDDLEMOUSE:
1033 case K_MIDDLEDRAG:
1034 case K_MIDDLERELEASE:
1035 case K_RIGHTMOUSE:
1036 case K_RIGHTDRAG:
1037 case K_RIGHTRELEASE:
1038 case K_X1MOUSE:
1039 case K_X1DRAG:
1040 case K_X1RELEASE:
1041 case K_X2MOUSE:
1042 case K_X2DRAG:
1043 case K_X2RELEASE:
1044 ins_mouse(c);
1045 break;
1046
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001047 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 ins_mousescroll(FALSE);
1049 break;
1050
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001051 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 ins_mousescroll(TRUE);
1053 break;
1054#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001055#ifdef FEAT_GUI_TABLINE
1056 case K_TABLINE:
1057 case K_TABMENU:
1058 ins_tabline(c);
1059 break;
1060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001062 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 break;
1064
Bram Moolenaar754b5602006-02-09 23:53:20 +00001065#ifdef FEAT_AUTOCMD
1066 case K_CURSORHOLD: /* Didn't type something for a while. */
1067 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1068 did_cursorhold = TRUE;
1069 break;
1070#endif
1071
Bram Moolenaar4770d092006-01-12 23:22:24 +00001072#ifdef FEAT_GUI_W32
1073 /* On Win32 ignore <M-F4>, we get it when closing the window was
1074 * cancelled. */
1075 case K_F4:
1076 if (mod_mask != MOD_MASK_ALT)
1077 goto normalchar;
1078 break;
1079#endif
1080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081#ifdef FEAT_GUI
1082 case K_VER_SCROLLBAR:
1083 ins_scroll();
1084 break;
1085
1086 case K_HOR_SCROLLBAR:
1087 ins_horscroll();
1088 break;
1089#endif
1090
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001091 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 case K_S_HOME:
1094 case K_C_HOME:
1095 ins_home(c);
1096 break;
1097
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001098 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 case K_S_END:
1101 case K_C_END:
1102 ins_end(c);
1103 break;
1104
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001105 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001106 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1107 ins_s_left();
1108 else
1109 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 break;
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 case K_C_LEFT:
1114 ins_s_left();
1115 break;
1116
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001117 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001118 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1119 ins_s_right();
1120 else
1121 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 break;
1123
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001124 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 case K_C_RIGHT:
1126 ins_s_right();
1127 break;
1128
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001129 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001130#ifdef FEAT_INS_EXPAND
1131 if (pum_visible())
1132 goto docomplete;
1133#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001134 if (mod_mask & MOD_MASK_SHIFT)
1135 ins_pageup();
1136 else
1137 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 break;
1139
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001140 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 case K_PAGEUP:
1142 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001143#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001144 if (pum_visible())
1145 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 ins_pageup();
1148 break;
1149
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001150 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001151#ifdef FEAT_INS_EXPAND
1152 if (pum_visible())
1153 goto docomplete;
1154#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001155 if (mod_mask & MOD_MASK_SHIFT)
1156 ins_pagedown();
1157 else
1158 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 break;
1160
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001161 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 case K_PAGEDOWN:
1163 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001164#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001165 if (pum_visible())
1166 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 ins_pagedown();
1169 break;
1170
1171#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001172 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 ins_drop();
1174 break;
1175#endif
1176
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001177 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 c = TAB;
1179 /* FALLTHROUGH */
1180
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001181 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1183 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1184 goto docomplete;
1185#endif
1186 inserted_space = FALSE;
1187 if (ins_tab())
1188 goto normalchar; /* insert TAB as a normal char */
1189 auto_format(FALSE, TRUE);
1190 break;
1191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001192 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 c = CAR;
1194 /* FALLTHROUGH */
1195 case CAR:
1196 case NL:
1197#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1198 /* In a quickfix window a <CR> jumps to the error under the
1199 * cursor. */
1200 if (bt_quickfix(curbuf) && c == CAR)
1201 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001202 if (curwin->w_llist_ref == NULL) /* quickfix window */
1203 do_cmdline_cmd((char_u *)".cc");
1204 else /* location list window */
1205 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 break;
1207 }
1208#endif
1209#ifdef FEAT_CMDWIN
1210 if (cmdwin_type != 0)
1211 {
1212 /* Execute the command in the cmdline window. */
1213 cmdwin_result = CAR;
1214 goto doESCkey;
1215 }
1216#endif
1217 if (ins_eol(c) && !p_im)
1218 goto doESCkey; /* out of memory */
1219 auto_format(FALSE, FALSE);
1220 inserted_space = FALSE;
1221 break;
1222
1223#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001224 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225# ifdef FEAT_INS_EXPAND
1226 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1227 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001228 if (has_compl_option(TRUE))
1229 goto docomplete;
1230 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 }
1232# endif
1233# ifdef FEAT_DIGRAPHS
1234 c = ins_digraph();
1235 if (c == NUL)
1236 break;
1237# endif
1238 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
1241#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001242 case Ctrl_X: /* Enter CTRL-X mode */
1243 ins_ctrl_x();
1244 break;
1245
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001246 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (ctrl_x_mode != CTRL_X_TAGS)
1248 goto normalchar;
1249 goto docomplete;
1250
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001251 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 if (ctrl_x_mode != CTRL_X_FILES)
1253 goto normalchar;
1254 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001255
1256 case 's': /* Spelling completion after ^X */
1257 case Ctrl_S:
1258 if (ctrl_x_mode != CTRL_X_SPELL)
1259 goto normalchar;
1260 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261#endif
1262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264#ifdef FEAT_INS_EXPAND
1265 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1266#endif
1267 {
1268 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1269 if (p_im)
1270 {
1271 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1272 break;
1273 goto doESCkey;
1274 }
1275 goto normalchar;
1276 }
1277#ifdef FEAT_INS_EXPAND
1278 /* FALLTHROUGH */
1279
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001280 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 case Ctrl_N:
1282 /* if 'complete' is empty then plain ^P is no longer special,
1283 * but it is under other ^X modes */
1284 if (*curbuf->b_p_cpt == NUL
1285 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001286 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 goto normalchar;
1288
1289docomplete:
1290 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001291 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 break;
1293#endif /* FEAT_INS_EXPAND */
1294
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001295 case Ctrl_Y: /* copy from previous line or scroll down */
1296 case Ctrl_E: /* copy from next line or scroll up */
1297 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 break;
1299
1300 default:
1301#ifdef UNIX
1302 if (c == intr_char) /* special interrupt char */
1303 goto do_intr;
1304#endif
1305
1306 /*
1307 * Insert a nomal character.
1308 */
1309normalchar:
1310#ifdef FEAT_SMARTINDENT
1311 /* Try to perform smart-indenting. */
1312 ins_try_si(c);
1313#endif
1314
1315 if (c == ' ')
1316 {
1317 inserted_space = TRUE;
1318#ifdef FEAT_CINDENT
1319 if (inindent(0))
1320 can_cindent = FALSE;
1321#endif
1322 if (Insstart_blank_vcol == MAXCOL
1323 && curwin->w_cursor.lnum == Insstart.lnum)
1324 Insstart_blank_vcol = get_nolist_virtcol();
1325 }
1326
1327 if (vim_iswordc(c) || !echeck_abbr(
1328#ifdef FEAT_MBYTE
1329 /* Add ABBR_OFF for characters above 0x100, this is
1330 * what check_abbr() expects. */
1331 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1332#endif
1333 c))
1334 {
1335 insert_special(c, FALSE, FALSE);
1336#ifdef FEAT_RIGHTLEFT
1337 revins_legal++;
1338 revins_chars++;
1339#endif
1340 }
1341
1342 auto_format(FALSE, TRUE);
1343
1344#ifdef FEAT_FOLDING
1345 /* When inserting a character the cursor line must never be in a
1346 * closed fold. */
1347 foldOpenCursor();
1348#endif
1349 break;
1350 } /* end of switch (c) */
1351
1352 /* If the cursor was moved we didn't just insert a space */
1353 if (arrow_used)
1354 inserted_space = FALSE;
1355
1356#ifdef FEAT_CINDENT
1357 if (can_cindent && cindent_on()
1358# ifdef FEAT_INS_EXPAND
1359 && ctrl_x_mode == 0
1360# endif
1361 )
1362 {
1363force_cindent:
1364 /*
1365 * Indent now if a key was typed that is in 'cinkeys'.
1366 */
1367 if (in_cinkeys(c, ' ', line_is_white))
1368 {
1369 if (stop_arrow() == OK)
1370 /* re-indent the current line */
1371 do_c_expr_indent();
1372 }
1373 }
1374#endif /* FEAT_CINDENT */
1375
1376 } /* for (;;) */
1377 /* NOTREACHED */
1378}
1379
1380/*
1381 * Redraw for Insert mode.
1382 * This is postponed until getting the next character to make '$' in the 'cpo'
1383 * option work correctly.
1384 * Only redraw when there are no characters available. This speeds up
1385 * inserting sequences of characters (e.g., for CTRL-R).
1386 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001387/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001389ins_redraw(ready)
1390 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 if (!char_avail())
1393 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001394#ifdef FEAT_AUTOCMD
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001395 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001396 if (ready && has_cursormovedI()
1397 && !equalpos(last_cursormoved, curwin->w_cursor))
1398 {
1399 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1400 last_cursormoved = curwin->w_cursor;
1401 }
1402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 if (must_redraw)
1404 update_screen(0);
1405 else if (clear_cmdline || redraw_cmdline)
1406 showmode(); /* clear cmdline and show mode */
1407 showruler(FALSE);
1408 setcursor();
1409 emsg_on_display = FALSE; /* may remove error message now */
1410 }
1411}
1412
1413/*
1414 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1415 */
1416 static void
1417ins_ctrl_v()
1418{
1419 int c;
1420
1421 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001422 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 if (redrawing() && !char_avail())
1425 edit_putchar('^', TRUE);
1426 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1427
1428#ifdef FEAT_CMDL_INFO
1429 add_to_showcmd_c(Ctrl_V);
1430#endif
1431
1432 c = get_literal();
1433#ifdef FEAT_CMDL_INFO
1434 clear_showcmd();
1435#endif
1436 insert_special(c, FALSE, TRUE);
1437#ifdef FEAT_RIGHTLEFT
1438 revins_chars++;
1439 revins_legal++;
1440#endif
1441}
1442
1443/*
1444 * Put a character directly onto the screen. It's not stored in a buffer.
1445 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1446 */
1447static int pc_status;
1448#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1449#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1450#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1451#define PC_STATUS_SET 3 /* pc_bytes was filled */
1452#ifdef FEAT_MBYTE
1453static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1454#else
1455static char_u pc_bytes[2]; /* saved bytes */
1456#endif
1457static int pc_attr;
1458static int pc_row;
1459static int pc_col;
1460
1461 void
1462edit_putchar(c, highlight)
1463 int c;
1464 int highlight;
1465{
1466 int attr;
1467
1468 if (ScreenLines != NULL)
1469 {
1470 update_topline(); /* just in case w_topline isn't valid */
1471 validate_cursor();
1472 if (highlight)
1473 attr = hl_attr(HLF_8);
1474 else
1475 attr = 0;
1476 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1477 pc_col = W_WINCOL(curwin);
1478#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1479 pc_status = PC_STATUS_UNSET;
1480#endif
1481#ifdef FEAT_RIGHTLEFT
1482 if (curwin->w_p_rl)
1483 {
1484 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1485# ifdef FEAT_MBYTE
1486 if (has_mbyte)
1487 {
1488 int fix_col = mb_fix_col(pc_col, pc_row);
1489
1490 if (fix_col != pc_col)
1491 {
1492 screen_putchar(' ', pc_row, fix_col, attr);
1493 --curwin->w_wcol;
1494 pc_status = PC_STATUS_RIGHT;
1495 }
1496 }
1497# endif
1498 }
1499 else
1500#endif
1501 {
1502 pc_col += curwin->w_wcol;
1503#ifdef FEAT_MBYTE
1504 if (mb_lefthalve(pc_row, pc_col))
1505 pc_status = PC_STATUS_LEFT;
1506#endif
1507 }
1508
1509 /* save the character to be able to put it back */
1510#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1511 if (pc_status == PC_STATUS_UNSET)
1512#endif
1513 {
1514 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1515 pc_status = PC_STATUS_SET;
1516 }
1517 screen_putchar(c, pc_row, pc_col, attr);
1518 }
1519}
1520
1521/*
1522 * Undo the previous edit_putchar().
1523 */
1524 void
1525edit_unputchar()
1526{
1527 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1528 {
1529#if defined(FEAT_MBYTE)
1530 if (pc_status == PC_STATUS_RIGHT)
1531 ++curwin->w_wcol;
1532 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1533 redrawWinline(curwin->w_cursor.lnum, FALSE);
1534 else
1535#endif
1536 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1537 }
1538}
1539
1540/*
1541 * Called when p_dollar is set: display a '$' at the end of the changed text
1542 * Only works when cursor is in the line that changes.
1543 */
1544 void
1545display_dollar(col)
1546 colnr_T col;
1547{
1548 colnr_T save_col;
1549
1550 if (!redrawing())
1551 return;
1552
1553 cursor_off();
1554 save_col = curwin->w_cursor.col;
1555 curwin->w_cursor.col = col;
1556#ifdef FEAT_MBYTE
1557 if (has_mbyte)
1558 {
1559 char_u *p;
1560
1561 /* If on the last byte of a multi-byte move to the first byte. */
1562 p = ml_get_curline();
1563 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1564 }
1565#endif
1566 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1567 if (curwin->w_wcol < W_WIDTH(curwin))
1568 {
1569 edit_putchar('$', FALSE);
1570 dollar_vcol = curwin->w_virtcol;
1571 }
1572 curwin->w_cursor.col = save_col;
1573}
1574
1575/*
1576 * Call this function before moving the cursor from the normal insert position
1577 * in insert mode.
1578 */
1579 static void
1580undisplay_dollar()
1581{
1582 if (dollar_vcol)
1583 {
1584 dollar_vcol = 0;
1585 redrawWinline(curwin->w_cursor.lnum, FALSE);
1586 }
1587}
1588
1589/*
1590 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1591 * Keep the cursor on the same character.
1592 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1593 * type == INDENT_DEC decrease indent (for CTRL-D)
1594 * type == INDENT_SET set indent to "amount"
1595 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1596 */
1597 void
1598change_indent(type, amount, round, replaced)
1599 int type;
1600 int amount;
1601 int round;
1602 int replaced; /* replaced character, put on replace stack */
1603{
1604 int vcol;
1605 int last_vcol;
1606 int insstart_less; /* reduction for Insstart.col */
1607 int new_cursor_col;
1608 int i;
1609 char_u *ptr;
1610 int save_p_list;
1611 int start_col;
1612 colnr_T vc;
1613#ifdef FEAT_VREPLACE
1614 colnr_T orig_col = 0; /* init for GCC */
1615 char_u *new_line, *orig_line = NULL; /* init for GCC */
1616
1617 /* VREPLACE mode needs to know what the line was like before changing */
1618 if (State & VREPLACE_FLAG)
1619 {
1620 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1621 orig_col = curwin->w_cursor.col;
1622 }
1623#endif
1624
1625 /* for the following tricks we don't want list mode */
1626 save_p_list = curwin->w_p_list;
1627 curwin->w_p_list = FALSE;
1628 vc = getvcol_nolist(&curwin->w_cursor);
1629 vcol = vc;
1630
1631 /*
1632 * For Replace mode we need to fix the replace stack later, which is only
1633 * possible when the cursor is in the indent. Remember the number of
1634 * characters before the cursor if it's possible.
1635 */
1636 start_col = curwin->w_cursor.col;
1637
1638 /* determine offset from first non-blank */
1639 new_cursor_col = curwin->w_cursor.col;
1640 beginline(BL_WHITE);
1641 new_cursor_col -= curwin->w_cursor.col;
1642
1643 insstart_less = curwin->w_cursor.col;
1644
1645 /*
1646 * If the cursor is in the indent, compute how many screen columns the
1647 * cursor is to the left of the first non-blank.
1648 */
1649 if (new_cursor_col < 0)
1650 vcol = get_indent() - vcol;
1651
1652 if (new_cursor_col > 0) /* can't fix replace stack */
1653 start_col = -1;
1654
1655 /*
1656 * Set the new indent. The cursor will be put on the first non-blank.
1657 */
1658 if (type == INDENT_SET)
1659 (void)set_indent(amount, SIN_CHANGED);
1660 else
1661 {
1662#ifdef FEAT_VREPLACE
1663 int save_State = State;
1664
1665 /* Avoid being called recursively. */
1666 if (State & VREPLACE_FLAG)
1667 State = INSERT;
1668#endif
1669 shift_line(type == INDENT_DEC, round, 1);
1670#ifdef FEAT_VREPLACE
1671 State = save_State;
1672#endif
1673 }
1674 insstart_less -= curwin->w_cursor.col;
1675
1676 /*
1677 * Try to put cursor on same character.
1678 * If the cursor is at or after the first non-blank in the line,
1679 * compute the cursor column relative to the column of the first
1680 * non-blank character.
1681 * If we are not in insert mode, leave the cursor on the first non-blank.
1682 * If the cursor is before the first non-blank, position it relative
1683 * to the first non-blank, counted in screen columns.
1684 */
1685 if (new_cursor_col >= 0)
1686 {
1687 /*
1688 * When changing the indent while the cursor is touching it, reset
1689 * Insstart_col to 0.
1690 */
1691 if (new_cursor_col == 0)
1692 insstart_less = MAXCOL;
1693 new_cursor_col += curwin->w_cursor.col;
1694 }
1695 else if (!(State & INSERT))
1696 new_cursor_col = curwin->w_cursor.col;
1697 else
1698 {
1699 /*
1700 * Compute the screen column where the cursor should be.
1701 */
1702 vcol = get_indent() - vcol;
1703 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1704
1705 /*
1706 * Advance the cursor until we reach the right screen column.
1707 */
1708 vcol = last_vcol = 0;
1709 new_cursor_col = -1;
1710 ptr = ml_get_curline();
1711 while (vcol <= (int)curwin->w_virtcol)
1712 {
1713 last_vcol = vcol;
1714#ifdef FEAT_MBYTE
1715 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001716 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 else
1718#endif
1719 ++new_cursor_col;
1720 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1721 }
1722 vcol = last_vcol;
1723
1724 /*
1725 * May need to insert spaces to be able to position the cursor on
1726 * the right screen column.
1727 */
1728 if (vcol != (int)curwin->w_virtcol)
1729 {
1730 curwin->w_cursor.col = new_cursor_col;
1731 i = (int)curwin->w_virtcol - vcol;
1732 ptr = alloc(i + 1);
1733 if (ptr != NULL)
1734 {
1735 new_cursor_col += i;
1736 ptr[i] = NUL;
1737 while (--i >= 0)
1738 ptr[i] = ' ';
1739 ins_str(ptr);
1740 vim_free(ptr);
1741 }
1742 }
1743
1744 /*
1745 * When changing the indent while the cursor is in it, reset
1746 * Insstart_col to 0.
1747 */
1748 insstart_less = MAXCOL;
1749 }
1750
1751 curwin->w_p_list = save_p_list;
1752
1753 if (new_cursor_col <= 0)
1754 curwin->w_cursor.col = 0;
1755 else
1756 curwin->w_cursor.col = new_cursor_col;
1757 curwin->w_set_curswant = TRUE;
1758 changed_cline_bef_curs();
1759
1760 /*
1761 * May have to adjust the start of the insert.
1762 */
1763 if (State & INSERT)
1764 {
1765 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1766 {
1767 if ((int)Insstart.col <= insstart_less)
1768 Insstart.col = 0;
1769 else
1770 Insstart.col -= insstart_less;
1771 }
1772 if ((int)ai_col <= insstart_less)
1773 ai_col = 0;
1774 else
1775 ai_col -= insstart_less;
1776 }
1777
1778 /*
1779 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1780 * If the number of characters before the cursor decreased, need to pop a
1781 * few characters from the replace stack.
1782 * If the number of characters before the cursor increased, need to push a
1783 * few NULs onto the replace stack.
1784 */
1785 if (REPLACE_NORMAL(State) && start_col >= 0)
1786 {
1787 while (start_col > (int)curwin->w_cursor.col)
1788 {
1789 replace_join(0); /* remove a NUL from the replace stack */
1790 --start_col;
1791 }
1792 while (start_col < (int)curwin->w_cursor.col || replaced)
1793 {
1794 replace_push(NUL);
1795 if (replaced)
1796 {
1797 replace_push(replaced);
1798 replaced = NUL;
1799 }
1800 ++start_col;
1801 }
1802 }
1803
1804#ifdef FEAT_VREPLACE
1805 /*
1806 * For VREPLACE mode, we also have to fix the replace stack. In this case
1807 * it is always possible because we backspace over the whole line and then
1808 * put it back again the way we wanted it.
1809 */
1810 if (State & VREPLACE_FLAG)
1811 {
1812 /* If orig_line didn't allocate, just return. At least we did the job,
1813 * even if you can't backspace. */
1814 if (orig_line == NULL)
1815 return;
1816
1817 /* Save new line */
1818 new_line = vim_strsave(ml_get_curline());
1819 if (new_line == NULL)
1820 return;
1821
1822 /* We only put back the new line up to the cursor */
1823 new_line[curwin->w_cursor.col] = NUL;
1824
1825 /* Put back original line */
1826 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1827 curwin->w_cursor.col = orig_col;
1828
1829 /* Backspace from cursor to start of line */
1830 backspace_until_column(0);
1831
1832 /* Insert new stuff into line again */
1833 ins_bytes(new_line);
1834
1835 vim_free(new_line);
1836 }
1837#endif
1838}
1839
1840/*
1841 * Truncate the space at the end of a line. This is to be used only in an
1842 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1843 * modes.
1844 */
1845 void
1846truncate_spaces(line)
1847 char_u *line;
1848{
1849 int i;
1850
1851 /* find start of trailing white space */
1852 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1853 {
1854 if (State & REPLACE_FLAG)
1855 replace_join(0); /* remove a NUL from the replace stack */
1856 }
1857 line[i + 1] = NUL;
1858}
1859
1860#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1861 || defined(FEAT_COMMENTS) || defined(PROTO)
1862/*
1863 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1864 * modes correctly. May also be used when not in insert mode at all.
1865 */
1866 void
1867backspace_until_column(col)
1868 int col;
1869{
1870 while ((int)curwin->w_cursor.col > col)
1871 {
1872 curwin->w_cursor.col--;
1873 if (State & REPLACE_FLAG)
1874 replace_do_bs();
1875 else
1876 (void)del_char(FALSE);
1877 }
1878}
1879#endif
1880
1881#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1882/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001883 * CTRL-X pressed in Insert mode.
1884 */
1885 static void
1886ins_ctrl_x()
1887{
1888 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1889 * CTRL-V works like CTRL-N */
1890 if (ctrl_x_mode != CTRL_X_CMDLINE)
1891 {
1892 /* if the next ^X<> won't ADD nothing, then reset
1893 * compl_cont_status */
1894 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001895 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001896 else
1897 compl_cont_status = 0;
1898 /* We're not sure which CTRL-X mode it will be yet */
1899 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1900 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1901 edit_submode_pre = NULL;
1902 showmode();
1903 }
1904}
1905
1906/*
1907 * Return TRUE if the 'dict' or 'tsr' option can be used.
1908 */
1909 static int
1910has_compl_option(dict_opt)
1911 int dict_opt;
1912{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001913 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00001914# ifdef FEAT_SPELL
1915 && !curwin->w_p_spell
1916# endif
1917 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001918 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1919 {
1920 ctrl_x_mode = 0;
1921 edit_submode = NULL;
1922 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1923 : (char_u *)_("'thesaurus' option is empty"),
1924 hl_attr(HLF_E));
1925 if (emsg_silent == 0)
1926 {
1927 vim_beep();
1928 setcursor();
1929 out_flush();
1930 ui_delay(2000L, FALSE);
1931 }
1932 return FALSE;
1933 }
1934 return TRUE;
1935}
1936
1937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1939 * This depends on the current mode.
1940 */
1941 int
1942vim_is_ctrl_x_key(c)
1943 int c;
1944{
1945 /* Always allow ^R - let it's results then be checked */
1946 if (c == Ctrl_R)
1947 return TRUE;
1948
Bram Moolenaare3226be2005-12-18 22:10:00 +00001949 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001950 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001951 return TRUE;
1952
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 switch (ctrl_x_mode)
1954 {
1955 case 0: /* Not in any CTRL-X mode */
1956 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1957 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001958 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1960 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1961 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001962 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1963 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 case CTRL_X_SCROLL:
1965 return (c == Ctrl_Y || c == Ctrl_E);
1966 case CTRL_X_WHOLE_LINE:
1967 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1968 case CTRL_X_FILES:
1969 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1970 case CTRL_X_DICTIONARY:
1971 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1972 case CTRL_X_THESAURUS:
1973 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1974 case CTRL_X_TAGS:
1975 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1976#ifdef FEAT_FIND_ID
1977 case CTRL_X_PATH_PATTERNS:
1978 return (c == Ctrl_P || c == Ctrl_N);
1979 case CTRL_X_PATH_DEFINES:
1980 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1981#endif
1982 case CTRL_X_CMDLINE:
1983 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1984 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001985#ifdef FEAT_COMPL_FUNC
1986 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001987 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001988 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001989 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001990#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001991 case CTRL_X_SPELL:
1992 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 }
1994 EMSG(_(e_internal));
1995 return FALSE;
1996}
1997
1998/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001999 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 * case of the originally typed text is used, and the case of the completed
2001 * text is infered, ie this tries to work out what case you probably wanted
2002 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002003 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 */
2005 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002006ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 char_u *str;
2008 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002009 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 char_u *fname;
2011 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002012 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013{
2014 int has_lower = FALSE;
2015 int was_letter = FALSE;
2016 int idx;
2017
2018 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2019 {
2020 /* Infer case of completed part -- webb */
2021 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002022 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023
2024 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002025 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002027 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 {
2029 has_lower = TRUE;
2030 if (isupper(IObuff[idx]))
2031 {
2032 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002033 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2035 break;
2036 }
2037 }
2038 }
2039
2040 /*
2041 * Rule 2: No lower case, 2nd consecutive letter converted to
2042 * upper case.
2043 */
2044 if (!has_lower)
2045 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002046 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002048 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 && islower(IObuff[idx]))
2050 {
2051 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002052 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2054 break;
2055 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002056 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
2058 }
2059
2060 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002061 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062
Bram Moolenaard289f132006-03-11 21:30:53 +00002063 return ins_compl_add(IObuff, len, icase, fname, NULL, NULL, NULL,
2064 dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 }
Bram Moolenaard289f132006-03-11 21:30:53 +00002066 return ins_compl_add(str, len, icase, fname, NULL, NULL, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067}
2068
2069/*
2070 * Add a match to the list of matches.
2071 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002072 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002073 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002075 int
Bram Moolenaard289f132006-03-11 21:30:53 +00002076ins_compl_add(str, len, icase, fname, kind, extra, info, cdir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 char_u *str;
2078 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002079 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 char_u *fname;
Bram Moolenaard289f132006-03-11 21:30:53 +00002081 char_u *kind; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002082 char_u *extra; /* extra text for popup menu or NULL */
Bram Moolenaard289f132006-03-11 21:30:53 +00002083 char_u *info; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002084 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002085 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002087 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002088 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
2090 ui_breakcheck();
2091 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002092 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 if (len < 0)
2094 len = (int)STRLEN(str);
2095
2096 /*
2097 * If the same match is already present, don't add it.
2098 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002099 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002101 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 do
2103 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002104 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002105 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002106 && match->cp_str[len] == NUL)
2107 return NOTDONE;
2108 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002109 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
2111
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002112 /* Remove any popup menu before changing the list of matches. */
2113 ins_compl_del_pum();
2114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 /*
2116 * Allocate a new match structure.
2117 * Copy the values to the new match structure.
2118 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002119 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002121 return FAIL;
2122 match->cp_number = -1;
2123 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002124 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002125 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
2127 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002128 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002130 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002131
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002133 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2135 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002136 if (fname != NULL
2137 && compl_curr_match
2138 && compl_curr_match->cp_fname != NULL
2139 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002140 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002141 else if (fname != NULL)
2142 {
2143 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002144 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002147 match->cp_fname = NULL;
2148 match->cp_flags = flags;
Bram Moolenaar9d47f172006-03-15 23:03:01 +00002149 if (kind != NULL && *kind != NUL)
Bram Moolenaard289f132006-03-11 21:30:53 +00002150 match->cp_kind = vim_strsave(kind);
Bram Moolenaar9d47f172006-03-15 23:03:01 +00002151 if (extra != NULL && *extra != NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002152 match->cp_extra = vim_strsave(extra);
Bram Moolenaar9d47f172006-03-15 23:03:01 +00002153 if (info != NULL && *info != NUL)
Bram Moolenaard289f132006-03-11 21:30:53 +00002154 match->cp_info = vim_strsave(info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156 /*
2157 * Link the new match structure in the list of matches.
2158 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002159 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002160 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 else if (dir == FORWARD)
2162 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002163 match->cp_next = compl_curr_match->cp_next;
2164 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166 else /* BACKWARD */
2167 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002168 match->cp_next = compl_curr_match;
2169 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002171 if (match->cp_next)
2172 match->cp_next->cp_prev = match;
2173 if (match->cp_prev)
2174 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002176 compl_first_match = match;
2177 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002179 /*
2180 * Find the longest common string if still doing that.
2181 */
2182 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2183 ins_compl_longest_match(match);
2184
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 return OK;
2186}
2187
2188/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002189 * Return TRUE if "str[len]" matches with match->cp_str, considering
2190 * match->cp_icase.
2191 */
2192 static int
2193ins_compl_equal(match, str, len)
2194 compl_T *match;
2195 char_u *str;
2196 int len;
2197{
2198 if (match->cp_icase)
2199 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2200 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2201}
2202
2203/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002204 * Reduce the longest common string for match "match".
2205 */
2206 static void
2207ins_compl_longest_match(match)
2208 compl_T *match;
2209{
2210 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002211 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002212 int had_match;
2213
2214 if (compl_leader == NULL)
2215 /* First match, use it as a whole. */
2216 compl_leader = vim_strsave(match->cp_str);
2217 else
2218 {
2219 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002220 p = compl_leader;
2221 s = match->cp_str;
2222 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002223 {
2224#ifdef FEAT_MBYTE
2225 if (has_mbyte)
2226 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002227 c1 = mb_ptr2char(p);
2228 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002229 }
2230 else
2231#endif
2232 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002233 c1 = *p;
2234 c2 = *s;
2235 }
2236 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2237 : (c1 != c2))
2238 break;
2239#ifdef FEAT_MBYTE
2240 if (has_mbyte)
2241 {
2242 mb_ptr_adv(p);
2243 mb_ptr_adv(s);
2244 }
2245 else
2246#endif
2247 {
2248 ++p;
2249 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002250 }
2251 }
2252
2253 if (*p != NUL)
2254 {
2255 /* Leader was shortened, need to change the inserted text. */
2256 *p = NUL;
2257 had_match = (curwin->w_cursor.col > compl_col);
2258 ins_compl_delete();
2259 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2260 ins_redraw(FALSE);
2261
2262 /* When the match isn't there (to avoid matching itself) remove it
2263 * again after redrawing. */
2264 if (!had_match)
2265 ins_compl_delete();
2266 }
2267
2268 compl_used_match = FALSE;
2269 }
2270}
2271
2272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 * Add an array of matches to the list of matches.
2274 * Frees matches[].
2275 */
2276 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002277ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 int num_matches;
2279 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002280 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281{
2282 int i;
2283 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002284 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285
Bram Moolenaar572cb562005-08-05 21:35:02 +00002286 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002287 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaard289f132006-03-11 21:30:53 +00002288 NULL, NULL, NULL, NULL, dir, 0)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002290 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 FreeWild(num_matches, matches);
2292}
2293
2294/* Make the completion list cyclic.
2295 * Return the number of matches (excluding the original).
2296 */
2297 static int
2298ins_compl_make_cyclic()
2299{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002300 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 int count = 0;
2302
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002303 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
2305 /*
2306 * Find the end of the list.
2307 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002308 match = compl_first_match;
2309 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002310 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002312 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 ++count;
2314 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002315 match->cp_next = compl_first_match;
2316 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
2318 return count;
2319}
2320
Bram Moolenaara94bc432006-03-10 21:42:59 +00002321/*
2322 * Start completion for the complete() function.
2323 * "startcol" is where the matched text starts (1 is first column).
2324 * "list" is the list of matches.
2325 */
2326 void
2327set_completion(startcol, list)
2328 int startcol;
2329 list_T *list;
2330{
2331 /* If already doing completions stop it. */
2332 if (ctrl_x_mode != 0)
2333 ins_compl_prep(' ');
2334 ins_compl_clear();
2335
2336 if (stop_arrow() == FAIL)
2337 return;
2338
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002339 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002340 startcol = curwin->w_cursor.col;
2341 compl_col = startcol;
2342 compl_length = curwin->w_cursor.col - startcol;
2343 /* compl_pattern doesn't need to be set */
2344 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2345 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaard289f132006-03-11 21:30:53 +00002346 -1, FALSE, NULL, NULL, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002347 return;
2348
2349 /* Handle like dictionary completion. */
2350 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2351
2352 ins_compl_add_list(list);
2353 compl_matches = ins_compl_make_cyclic();
2354 compl_started = TRUE;
2355 compl_used_match = TRUE;
2356
2357 compl_curr_match = compl_first_match;
2358 ins_complete(Ctrl_N);
2359 out_flush();
2360}
2361
2362
Bram Moolenaar9372a112005-12-06 19:59:18 +00002363/* "compl_match_array" points the currently displayed list of entries in the
2364 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002365static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002366static int compl_match_arraysize;
2367
2368/*
2369 * Update the screen and when there is any scrolling remove the popup menu.
2370 */
2371 static void
2372ins_compl_upd_pum()
2373{
2374 int h;
2375
2376 if (compl_match_array != NULL)
2377 {
2378 h = curwin->w_cline_height;
2379 update_screen(0);
2380 if (h != curwin->w_cline_height)
2381 ins_compl_del_pum();
2382 }
2383}
2384
2385/*
2386 * Remove any popup menu.
2387 */
2388 static void
2389ins_compl_del_pum()
2390{
2391 if (compl_match_array != NULL)
2392 {
2393 pum_undisplay();
2394 vim_free(compl_match_array);
2395 compl_match_array = NULL;
2396 }
2397}
2398
2399/*
2400 * Return TRUE if the popup menu should be displayed.
2401 */
2402 static int
2403pum_wanted()
2404{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002405 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002406 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002407 return FALSE;
2408
2409 /* The display looks bad on a B&W display. */
2410 if (t_colors < 8
2411#ifdef FEAT_GUI
2412 && !gui.in_use
2413#endif
2414 )
2415 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002416 return TRUE;
2417}
2418
2419/*
2420 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002421 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002422 */
2423 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002424pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002425{
2426 compl_T *compl;
2427 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002428
2429 /* Don't display the popup menu if there are no matches or there is only
2430 * one (ignoring the original text). */
2431 compl = compl_first_match;
2432 i = 0;
2433 do
2434 {
2435 if (compl == NULL
2436 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2437 break;
2438 compl = compl->cp_next;
2439 } while (compl != compl_first_match);
2440
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002441 if (strstr((char *)p_cot, "menuone") != NULL)
2442 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002443 return (i >= 2);
2444}
2445
2446/*
2447 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002448 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002449 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002450 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002451ins_compl_show_pum()
2452{
2453 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002454 compl_T *shown_compl = NULL;
2455 int did_find_shown_match = FALSE;
2456 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002457 int i;
2458 int cur = -1;
2459 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002460 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002461
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002462 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002463 return;
2464
2465 /* Update the screen before drawing the popup menu over it. */
2466 update_screen(0);
2467
2468 if (compl_match_array == NULL)
2469 {
2470 /* Need to build the popup menu list. */
2471 compl_match_arraysize = 0;
2472 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002473 if (compl_leader != NULL)
2474 lead_len = STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002475 do
2476 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002477 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2478 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002479 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002480 ++compl_match_arraysize;
2481 compl = compl->cp_next;
2482 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002483 if (compl_match_arraysize == 0)
2484 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002485 compl_match_array = (pumitem_T *)alloc_clear(
2486 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002487 * compl_match_arraysize));
2488 if (compl_match_array != NULL)
2489 {
2490 i = 0;
2491 compl = compl_first_match;
2492 do
2493 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002494 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2495 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002496 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002497 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002498 if (!shown_match_ok)
2499 {
2500 if (compl == compl_shown_match || did_find_shown_match)
2501 {
2502 /* This item is the shown match or this is the
2503 * first displayed item after the shown match. */
2504 compl_shown_match = compl;
2505 did_find_shown_match = TRUE;
2506 shown_match_ok = TRUE;
2507 }
2508 else
2509 /* Remember this displayed match for when the
2510 * shown match is just below it. */
2511 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002512 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002513 }
2514 compl_match_array[i].pum_text = compl->cp_str;
Bram Moolenaard289f132006-03-11 21:30:53 +00002515 if (compl->cp_kind != NULL)
2516 compl_match_array[i].pum_kind = compl->cp_kind;
2517 if (compl->cp_info != NULL)
2518 compl_match_array[i].pum_info = compl->cp_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002519 if (compl->cp_extra != NULL)
2520 compl_match_array[i++].pum_extra = compl->cp_extra;
2521 else
2522 compl_match_array[i++].pum_extra = compl->cp_fname;
2523 }
2524
2525 if (compl == compl_shown_match)
2526 {
2527 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002528
2529 /* When the original text is the shown match don't set
2530 * compl_shown_match. */
2531 if (compl->cp_flags & ORIGINAL_TEXT)
2532 shown_match_ok = TRUE;
2533
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002534 if (!shown_match_ok && shown_compl != NULL)
2535 {
2536 /* The shown match isn't displayed, set it to the
2537 * previously displayed match. */
2538 compl_shown_match = shown_compl;
2539 shown_match_ok = TRUE;
2540 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002541 }
2542 compl = compl->cp_next;
2543 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002544
2545 if (!shown_match_ok) /* no displayed match at all */
2546 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002547 }
2548 }
2549 else
2550 {
2551 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002552 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002553 if (compl_match_array[i].pum_text == compl_shown_match->cp_str)
Bram Moolenaara6557602006-02-04 22:43:20 +00002554 break;
2555 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002556 }
2557
2558 if (compl_match_array != NULL)
2559 {
2560 /* Compute the screen column of the start of the completed text.
2561 * Use the cursor to get all wrapping and other settings right. */
2562 col = curwin->w_cursor.col;
2563 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002564 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002565 curwin->w_cursor.col = col;
2566 }
2567}
2568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569#define DICT_FIRST (1) /* use just first element in "dict" */
2570#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002571
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002573 * Add any identifiers that match the given pattern in the list of dictionary
2574 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 */
2576 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002577ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2578 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002580 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002581 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002583 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 char_u *ptr;
2585 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 char_u **files;
2588 int count;
2589 int i;
2590 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002591 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
Bram Moolenaar0b238792006-03-02 22:49:12 +00002593 if (*dict == NUL)
2594 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002595#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002596 /* When 'dictionary' is empty and spell checking is enabled use
2597 * "spell". */
2598 if (!thesaurus && curwin->w_p_spell)
2599 dict = (char_u *)"spell";
2600 else
2601#endif
2602 return;
2603 }
2604
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002606 if (buf == NULL)
2607 return;
2608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 /* If 'infercase' is set, don't use 'smartcase' here */
2610 save_p_scs = p_scs;
2611 if (curbuf->b_p_inf)
2612 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002613
2614 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2615 * to only match at the start of a line. Otherwise just match the
2616 * pattern. */
2617 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2618 {
2619 i = STRLEN(pat) + 8;
2620 ptr = alloc(i);
2621 if (ptr == NULL)
2622 return;
2623 vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat);
2624 regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0);
2625 vim_free(ptr);
2626 }
2627 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002628 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002629 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002630 if (regmatch.regprog == NULL)
2631 goto theend;
2632 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002633
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2635 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002636 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 {
2638 /* copy one dictionary file name into buf */
2639 if (flags == DICT_EXACT)
2640 {
2641 count = 1;
2642 files = &dict;
2643 }
2644 else
2645 {
2646 /* Expand wildcards in the dictionary name, but do not allow
2647 * backticks (for security, the 'dict' option may have been set in
2648 * a modeline). */
2649 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002650# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002651 if (!thesaurus && STRCMP(buf, "spell") == 0)
2652 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002653 else
2654# endif
2655 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 || expand_wildcards(1, &buf, &count, &files,
2657 EW_FILE|EW_SILENT) != OK)
2658 count = 0;
2659 }
2660
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002661# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002662 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002664 /* Complete from active spelling. Skip "\<" in the pattern, we
2665 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002666 if (pat[0] == '\\' && pat[1] == '<')
2667 ptr = pat + 2;
2668 else
2669 ptr = pat;
2670 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002672 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002673# endif
Bram Moolenaar0b238792006-03-02 22:49:12 +00002674 {
2675 ins_compl_files(count, files, thesaurus, flags,
2676 &regmatch, buf, &dir);
2677 if (flags != DICT_EXACT)
2678 FreeWild(count, files);
2679 }
2680 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 break;
2682 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002683
2684theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 p_scs = save_p_scs;
2686 vim_free(regmatch.regprog);
2687 vim_free(buf);
2688}
2689
Bram Moolenaar0b238792006-03-02 22:49:12 +00002690 static void
2691ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2692 int count;
2693 char_u **files;
2694 int thesaurus;
2695 int flags;
2696 regmatch_T *regmatch;
2697 char_u *buf;
2698 int *dir;
2699{
2700 char_u *ptr;
2701 int i;
2702 FILE *fp;
2703 int add_r;
2704
2705 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2706 {
2707 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2708 if (flags != DICT_EXACT)
2709 {
2710 vim_snprintf((char *)IObuff, IOSIZE,
2711 _("Scanning dictionary: %s"), (char *)files[i]);
2712 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2713 }
2714
2715 if (fp != NULL)
2716 {
2717 /*
2718 * Read dictionary file line by line.
2719 * Check each line for a match.
2720 */
2721 while (!got_int && !compl_interrupted
2722 && !vim_fgets(buf, LSIZE, fp))
2723 {
2724 ptr = buf;
2725 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2726 {
2727 ptr = regmatch->startp[0];
2728 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2729 ptr = find_line_end(ptr);
2730 else
2731 ptr = find_word_end(ptr);
2732 add_r = ins_compl_add_infercase(regmatch->startp[0],
2733 (int)(ptr - regmatch->startp[0]),
Bram Moolenaard289f132006-03-11 21:30:53 +00002734 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002735 if (thesaurus)
2736 {
2737 char_u *wstart;
2738
2739 /*
2740 * Add the other matches on the line
2741 */
2742 while (!got_int)
2743 {
2744 /* Find start of the next word. Skip white
2745 * space and punctuation. */
2746 ptr = find_word_start(ptr);
2747 if (*ptr == NUL || *ptr == NL)
2748 break;
2749 wstart = ptr;
2750
2751 /* Find end of the word and add it. */
2752#ifdef FEAT_MBYTE
2753 if (has_mbyte)
2754 /* Japanese words may have characters in
2755 * different classes, only separate words
2756 * with single-byte non-word characters. */
2757 while (*ptr != NUL)
2758 {
2759 int l = (*mb_ptr2len)(ptr);
2760
2761 if (l < 2 && !vim_iswordc(*ptr))
2762 break;
2763 ptr += l;
2764 }
2765 else
2766#endif
2767 ptr = find_word_end(ptr);
2768 add_r = ins_compl_add_infercase(wstart,
2769 (int)(ptr - wstart),
2770 p_ic, files[i], *dir, 0);
2771 }
2772 }
2773 if (add_r == OK)
2774 /* if dir was BACKWARD then honor it just once */
2775 *dir = FORWARD;
2776 else if (add_r == FAIL)
2777 break;
2778 /* avoid expensive call to vim_regexec() when at end
2779 * of line */
2780 if (*ptr == '\n' || got_int)
2781 break;
2782 }
2783 line_breakcheck();
2784 ins_compl_check_keys(50);
2785 }
2786 fclose(fp);
2787 }
2788 }
2789}
2790
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791/*
2792 * Find the start of the next word.
2793 * Returns a pointer to the first char of the word. Also stops at a NUL.
2794 */
2795 char_u *
2796find_word_start(ptr)
2797 char_u *ptr;
2798{
2799#ifdef FEAT_MBYTE
2800 if (has_mbyte)
2801 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002802 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 else
2804#endif
2805 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2806 ++ptr;
2807 return ptr;
2808}
2809
2810/*
2811 * Find the end of the word. Assumes it starts inside a word.
2812 * Returns a pointer to just after the word.
2813 */
2814 char_u *
2815find_word_end(ptr)
2816 char_u *ptr;
2817{
2818#ifdef FEAT_MBYTE
2819 int start_class;
2820
2821 if (has_mbyte)
2822 {
2823 start_class = mb_get_class(ptr);
2824 if (start_class > 1)
2825 while (*ptr != NUL)
2826 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002827 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 if (mb_get_class(ptr) != start_class)
2829 break;
2830 }
2831 }
2832 else
2833#endif
2834 while (vim_iswordc(*ptr))
2835 ++ptr;
2836 return ptr;
2837}
2838
2839/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002840 * Find the end of the line, omitting CR and NL at the end.
2841 * Returns a pointer to just after the line.
2842 */
2843 static char_u *
2844find_line_end(ptr)
2845 char_u *ptr;
2846{
2847 char_u *s;
2848
2849 s = ptr + STRLEN(ptr);
2850 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2851 --s;
2852 return s;
2853}
2854
2855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 * Free the list of completions
2857 */
2858 static void
2859ins_compl_free()
2860{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002861 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002863 vim_free(compl_pattern);
2864 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002865 vim_free(compl_leader);
2866 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002868 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002870
2871 ins_compl_del_pum();
2872 pum_clear();
2873
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002874 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 do
2876 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002877 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002878 compl_curr_match = compl_curr_match->cp_next;
2879 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002881 if (match->cp_flags & FREE_FNAME)
2882 vim_free(match->cp_fname);
Bram Moolenaard289f132006-03-11 21:30:53 +00002883 vim_free(match->cp_kind);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002884 vim_free(match->cp_extra);
Bram Moolenaard289f132006-03-11 21:30:53 +00002885 vim_free(match->cp_info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002887 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2888 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889}
2890
2891 static void
2892ins_compl_clear()
2893{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002894 compl_cont_status = 0;
2895 compl_started = FALSE;
2896 compl_matches = 0;
2897 vim_free(compl_pattern);
2898 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002899 vim_free(compl_leader);
2900 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002902 vim_free(compl_orig_text);
2903 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904}
2905
2906/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002907 * Return TRUE when Insert completion is active.
2908 */
2909 int
2910ins_compl_active()
2911{
2912 return compl_started;
2913}
2914
2915/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002916 * Delete one character before the cursor and show the subset of the matches
2917 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002918 * Returns TRUE if the work is done and another char to be got from the user.
2919 */
2920 static int
2921ins_compl_bs()
2922{
2923 char_u *line;
2924 char_u *p;
2925
2926 if (curwin->w_cursor.col <= compl_col + compl_length)
2927 {
2928 /* Deleted more than what was used to find matches, need to look for
2929 * matches all over again. */
2930 ins_compl_free();
2931 compl_started = FALSE;
2932 compl_matches = 0;
2933 }
2934
2935 line = ml_get_curline();
2936 p = line + curwin->w_cursor.col;
2937 mb_ptr_back(line, p);
2938
2939 vim_free(compl_leader);
2940 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2941 if (compl_leader != NULL)
2942 {
2943 ins_compl_del_pum();
2944 ins_compl_delete();
2945 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2946
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002947 if (compl_started)
2948 ins_compl_set_original_text(compl_leader);
2949 else
Bram Moolenaara6557602006-02-04 22:43:20 +00002950 {
2951 /* Matches were cleared, need to search for them now. */
2952 if (ins_complete(Ctrl_N) == FAIL)
2953 compl_cont_status = 0;
2954 else
2955 {
2956 /* Remove the completed word again. */
2957 ins_compl_delete();
2958 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2959 }
2960 }
2961
2962 /* Show the popup menu with a different set of matches. */
2963 ins_compl_show_pum();
2964 compl_used_match = FALSE;
2965
2966 return TRUE;
2967 }
2968 return FALSE;
2969}
2970
2971/*
2972 * Append one character to the match leader. May reduce the number of
2973 * matches.
2974 */
2975 static void
2976ins_compl_addleader(c)
2977 int c;
2978{
2979#ifdef FEAT_MBYTE
2980 int cc;
2981
2982 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2983 {
2984 char_u buf[MB_MAXBYTES + 1];
2985
2986 (*mb_char2bytes)(c, buf);
2987 buf[cc] = NUL;
2988 ins_char_bytes(buf, cc);
2989 }
2990 else
2991#endif
2992 ins_char(c);
2993
2994 vim_free(compl_leader);
2995 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
2996 curwin->w_cursor.col - compl_col);
2997 if (compl_leader != NULL)
2998 {
2999 /* Show the popup menu with a different set of matches. */
3000 ins_compl_del_pum();
3001 ins_compl_show_pum();
3002 compl_used_match = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003003 ins_compl_set_original_text(compl_leader);
3004 }
3005}
3006
3007/*
3008 * Set the first match, the original text.
3009 */
3010 static void
3011ins_compl_set_original_text(str)
3012 char_u *str;
3013{
3014 char_u *p;
3015
3016 /* Replace the original text entry. */
3017 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3018 {
3019 p = vim_strsave(str);
3020 if (p != NULL)
3021 {
3022 vim_free(compl_first_match->cp_str);
3023 compl_first_match->cp_str = p;
3024 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003025 }
3026}
3027
3028/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003029 * Append one character to the match leader. May reduce the number of
3030 * matches.
3031 */
3032 static void
3033ins_compl_addfrommatch()
3034{
3035 char_u *p;
3036 int len = curwin->w_cursor.col - compl_col;
3037 int c;
3038
3039 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003040 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003041 return;
3042 p += len;
3043#ifdef FEAT_MBYTE
3044 c = mb_ptr2char(p);
3045#else
3046 c = *p;
3047#endif
3048 ins_compl_addleader(c);
3049}
3050
3051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003053 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003054 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003056 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057ins_compl_prep(c)
3058 int c;
3059{
3060 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 int temp;
3062 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003063 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
3065 /* Forget any previous 'special' messages if this is actually
3066 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3067 */
3068 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3069 edit_submode_extra = NULL;
3070
3071 /* Ignore end of Select mode mapping */
3072 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003073 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003075 /* Set "compl_get_longest" when finding the first matches. */
3076 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3077 || (ctrl_x_mode == 0 && !compl_started))
3078 {
3079 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3080 compl_used_match = TRUE;
3081 }
3082
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3084 {
3085 /*
3086 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3087 * it will be yet. Now we decide.
3088 */
3089 switch (c)
3090 {
3091 case Ctrl_E:
3092 case Ctrl_Y:
3093 ctrl_x_mode = CTRL_X_SCROLL;
3094 if (!(State & REPLACE_FLAG))
3095 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3096 else
3097 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3098 edit_submode_pre = NULL;
3099 showmode();
3100 break;
3101 case Ctrl_L:
3102 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3103 break;
3104 case Ctrl_F:
3105 ctrl_x_mode = CTRL_X_FILES;
3106 break;
3107 case Ctrl_K:
3108 ctrl_x_mode = CTRL_X_DICTIONARY;
3109 break;
3110 case Ctrl_R:
3111 /* Simply allow ^R to happen without affecting ^X mode */
3112 break;
3113 case Ctrl_T:
3114 ctrl_x_mode = CTRL_X_THESAURUS;
3115 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003116#ifdef FEAT_COMPL_FUNC
3117 case Ctrl_U:
3118 ctrl_x_mode = CTRL_X_FUNCTION;
3119 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003120 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003121 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003122 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003123#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003124 case 's':
3125 case Ctrl_S:
3126 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003127#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003128 spell_back_to_badword();
3129#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003130 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 case Ctrl_RSB:
3132 ctrl_x_mode = CTRL_X_TAGS;
3133 break;
3134#ifdef FEAT_FIND_ID
3135 case Ctrl_I:
3136 case K_S_TAB:
3137 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3138 break;
3139 case Ctrl_D:
3140 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3141 break;
3142#endif
3143 case Ctrl_V:
3144 case Ctrl_Q:
3145 ctrl_x_mode = CTRL_X_CMDLINE;
3146 break;
3147 case Ctrl_P:
3148 case Ctrl_N:
3149 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3150 * just started ^X mode, or there were enough ^X's to cancel
3151 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3152 * do normal expansion when interrupting a different mode (say
3153 * ^X^F^X^P or ^P^X^X^P, see below)
3154 * nothing changes if interrupting mode 0, (eg, the flag
3155 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003156 if (!(compl_cont_status & CONT_INTRPT))
3157 compl_cont_status |= CONT_LOCAL;
3158 else if (compl_cont_mode != 0)
3159 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 /* FALLTHROUGH */
3161 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003162 /* If we have typed at least 2 ^X's... for modes != 0, we set
3163 * compl_cont_status = 0 (eg, as if we had just started ^X
3164 * mode).
3165 * For mode 0, we set "compl_cont_mode" to an impossible
3166 * value, in both cases ^X^X can be used to restart the same
3167 * mode (avoiding ADDING mode).
3168 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3169 * 'complete' and local ^P expansions respectively.
3170 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3171 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (c == Ctrl_X)
3173 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003174 if (compl_cont_mode != 0)
3175 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003177 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 }
3179 ctrl_x_mode = 0;
3180 edit_submode = NULL;
3181 showmode();
3182 break;
3183 }
3184 }
3185 else if (ctrl_x_mode != 0)
3186 {
3187 /* We're already in CTRL-X mode, do we stay in it? */
3188 if (!vim_is_ctrl_x_key(c))
3189 {
3190 if (ctrl_x_mode == CTRL_X_SCROLL)
3191 ctrl_x_mode = 0;
3192 else
3193 ctrl_x_mode = CTRL_X_FINISHED;
3194 edit_submode = NULL;
3195 }
3196 showmode();
3197 }
3198
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003199 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 {
3201 /* Show error message from attempted keyword completion (probably
3202 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003203 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003205 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3206 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 || ctrl_x_mode == CTRL_X_FINISHED)
3208 {
3209 /* Get here when we have finished typing a sequence of ^N and
3210 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003211 * memory that was used, and make sure we can redo the insert. */
3212 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003214 char_u *p;
3215
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 /*
3217 * If any of the original typed text has been changed,
3218 * eg when ignorecase is set, we must add back-spaces to
3219 * the redo buffer. We add as few as necessary to delete
3220 * just the part of the original text that has changed.
3221 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003222 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003223 p = compl_orig_text;
3224 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003226 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 ++ptr;
3228 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003229 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00003231 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 }
3233
3234#ifdef FEAT_CINDENT
3235 want_cindent = (can_cindent && cindent_on());
3236#endif
3237 /*
3238 * When completing whole lines: fix indent for 'cindent'.
3239 * Otherwise, break line if it's too long.
3240 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003241 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 {
3243#ifdef FEAT_CINDENT
3244 /* re-indent the current line */
3245 if (want_cindent)
3246 {
3247 do_c_expr_indent();
3248 want_cindent = FALSE; /* don't do it again */
3249 }
3250#endif
3251 }
3252 else
3253 {
3254 /* put the cursor on the last char, for 'tw' formatting */
3255 curwin->w_cursor.col--;
3256 if (stop_arrow() == OK)
3257 insertchar(NUL, 0, -1);
3258 curwin->w_cursor.col++;
3259 }
3260
3261 auto_format(FALSE, TRUE);
3262
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003263 /* if the popup menu is displayed hitting Enter means accepting
3264 * the selection without inserting anything. */
3265 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
3266 retval = TRUE;
3267
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003269 compl_started = FALSE;
3270 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 msg_clr_cmdline(); /* necessary for "noshowmode" */
3272 ctrl_x_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 if (edit_submode != NULL)
3274 {
3275 edit_submode = NULL;
3276 showmode();
3277 }
3278
3279#ifdef FEAT_CINDENT
3280 /*
3281 * Indent now if a key was typed that is in 'cinkeys'.
3282 */
3283 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3284 do_c_expr_indent();
3285#endif
3286 }
3287 }
3288
3289 /* reset continue_* if we left expansion-mode, if we stay they'll be
3290 * (re)set properly in ins_complete() */
3291 if (!vim_is_ctrl_x_key(c))
3292 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003293 compl_cont_status = 0;
3294 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003296
3297 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298}
3299
3300/*
3301 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3302 * (depending on flag) starting from buf and looking for a non-scanned
3303 * buffer (other than curbuf). curbuf is special, if it is called with
3304 * buf=curbuf then it has to be the first call for a given flag/expansion.
3305 *
3306 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3307 */
3308 static buf_T *
3309ins_compl_next_buf(buf, flag)
3310 buf_T *buf;
3311 int flag;
3312{
3313#ifdef FEAT_WINDOWS
3314 static win_T *wp;
3315#endif
3316
3317 if (flag == 'w') /* just windows */
3318 {
3319#ifdef FEAT_WINDOWS
3320 if (buf == curbuf) /* first call for this flag/expansion */
3321 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003322 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 && wp->w_buffer->b_scanned)
3324 ;
3325 buf = wp->w_buffer;
3326#else
3327 buf = curbuf;
3328#endif
3329 }
3330 else
3331 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3332 * (unlisted buffers)
3333 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003334 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 && ((flag == 'U'
3336 ? buf->b_p_bl
3337 : (!buf->b_p_bl
3338 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003339 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 ;
3341 return buf;
3342}
3343
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003344#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003345static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003346
3347/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003348 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003349 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003350 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003351 static void
3352expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003353 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003354 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003355{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003356 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003357 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003358 char_u *funcname;
3359 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003360
Bram Moolenaare344bea2005-09-01 20:46:49 +00003361 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3362 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003363 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003364
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003365 /* Call 'completefunc' to obtain the list of matches. */
3366 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003367 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003368
Bram Moolenaare344bea2005-09-01 20:46:49 +00003369 pos = curwin->w_cursor;
3370 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3371 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003372 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003373 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003374
Bram Moolenaara94bc432006-03-10 21:42:59 +00003375 ins_compl_add_list(matchlist);
3376 list_unref(matchlist);
3377}
3378#endif /* FEAT_COMPL_FUNC */
3379
3380#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
3381/*
3382 * Add completions from a list.
3383 * Unreferences the list.
3384 */
3385 static void
3386ins_compl_add_list(list)
3387 list_T *list;
3388{
3389 listitem_T *li;
3390 int icase;
3391 char_u *p;
3392 char_u *x;
Bram Moolenaard289f132006-03-11 21:30:53 +00003393 char_u *k;
3394 char_u *info;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003395 int dir = compl_direction;
3396
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003397 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003398 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003399 {
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003400 icase = p_ic;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003401 if (li->li_tv.v_type == VAR_DICT && li->li_tv.vval.v_dict != NULL)
3402 {
3403 p = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"word", FALSE);
3404 x = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"menu", FALSE);
Bram Moolenaard289f132006-03-11 21:30:53 +00003405 k = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"kind", FALSE);
3406 info = get_dict_string(li->li_tv.vval.v_dict,
3407 (char_u *)"info", FALSE);
3408 if (get_dict_string(li->li_tv.vval.v_dict,
3409 (char_u *)"icase", FALSE) != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003410 icase = get_dict_number(li->li_tv.vval.v_dict,
3411 (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003412 }
3413 else
3414 {
3415 p = get_tv_string_chk(&li->li_tv);
3416 x = NULL;
Bram Moolenaard289f132006-03-11 21:30:53 +00003417 k = NULL;
3418 info = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003419 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003420 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003421 {
Bram Moolenaard289f132006-03-11 21:30:53 +00003422 if (ins_compl_add(p, -1, icase, NULL, k, x, info, dir, 0) == OK)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003423 /* if dir was BACKWARD then honor it just once */
3424 dir = FORWARD;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003425 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003426 else if (did_emsg)
3427 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003428 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003429}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003430#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003431
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003432/*
3433 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003434 * The search starts at position "ini" in curbuf and in the direction
3435 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003436 * When "compl_started" is FALSE start at that position, otherwise continue
3437 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003438 * This may return before finding all the matches.
3439 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 */
3441 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003442ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
3445 static pos_T first_match_pos;
3446 static pos_T last_match_pos;
3447 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003448 static int found_all = FALSE; /* Found all matches of a
3449 certain type. */
3450 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
Bram Moolenaar572cb562005-08-05 21:35:02 +00003452 pos_T *pos;
3453 char_u **matches;
3454 int save_p_scs;
3455 int save_p_ws;
3456 int save_p_ic;
3457 int i;
3458 int num_matches;
3459 int len;
3460 int found_new_match;
3461 int type = ctrl_x_mode;
3462 char_u *ptr;
3463 char_u *dict = NULL;
3464 int dict_f = 0;
3465 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003467 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 {
3469 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3470 ins_buf->b_scanned = 0;
3471 found_all = FALSE;
3472 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003473 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003474 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 last_match_pos = first_match_pos = *ini;
3476 }
3477
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003478 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003479 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3481 for (;;)
3482 {
3483 found_new_match = FAIL;
3484
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003485 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 * or if found_all says this entry is done. For ^X^L only use the
3487 * entries from 'complete' that look in loaded buffers. */
3488 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003489 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 {
3491 found_all = FALSE;
3492 while (*e_cpt == ',' || *e_cpt == ' ')
3493 e_cpt++;
3494 if (*e_cpt == '.' && !curbuf->b_scanned)
3495 {
3496 ins_buf = curbuf;
3497 first_match_pos = *ini;
3498 /* So that ^N can match word immediately after cursor */
3499 if (ctrl_x_mode == 0)
3500 dec(&first_match_pos);
3501 last_match_pos = first_match_pos;
3502 type = 0;
3503 }
3504 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3505 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3506 {
3507 /* Scan a buffer, but not the current one. */
3508 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3509 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003510 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 first_match_pos.col = last_match_pos.col = 0;
3512 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3513 last_match_pos.lnum = 0;
3514 type = 0;
3515 }
3516 else /* unloaded buffer, scan like dictionary */
3517 {
3518 found_all = TRUE;
3519 if (ins_buf->b_fname == NULL)
3520 continue;
3521 type = CTRL_X_DICTIONARY;
3522 dict = ins_buf->b_fname;
3523 dict_f = DICT_EXACT;
3524 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003525 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 ins_buf->b_fname == NULL
3527 ? buf_spname(ins_buf)
3528 : ins_buf->b_sfname == NULL
3529 ? (char *)ins_buf->b_fname
3530 : (char *)ins_buf->b_sfname);
3531 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3532 }
3533 else if (*e_cpt == NUL)
3534 break;
3535 else
3536 {
3537 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3538 type = -1;
3539 else if (*e_cpt == 'k' || *e_cpt == 's')
3540 {
3541 if (*e_cpt == 'k')
3542 type = CTRL_X_DICTIONARY;
3543 else
3544 type = CTRL_X_THESAURUS;
3545 if (*++e_cpt != ',' && *e_cpt != NUL)
3546 {
3547 dict = e_cpt;
3548 dict_f = DICT_FIRST;
3549 }
3550 }
3551#ifdef FEAT_FIND_ID
3552 else if (*e_cpt == 'i')
3553 type = CTRL_X_PATH_PATTERNS;
3554 else if (*e_cpt == 'd')
3555 type = CTRL_X_PATH_DEFINES;
3556#endif
3557 else if (*e_cpt == ']' || *e_cpt == 't')
3558 {
3559 type = CTRL_X_TAGS;
3560 sprintf((char*)IObuff, _("Scanning tags."));
3561 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3562 }
3563 else
3564 type = -1;
3565
3566 /* in any case e_cpt is advanced to the next entry */
3567 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3568
3569 found_all = TRUE;
3570 if (type == -1)
3571 continue;
3572 }
3573 }
3574
3575 switch (type)
3576 {
3577 case -1:
3578 break;
3579#ifdef FEAT_FIND_ID
3580 case CTRL_X_PATH_PATTERNS:
3581 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003582 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003583 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003585 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3587 (linenr_T)1, (linenr_T)MAXLNUM);
3588 break;
3589#endif
3590
3591 case CTRL_X_DICTIONARY:
3592 case CTRL_X_THESAURUS:
3593 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003594 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 : (type == CTRL_X_THESAURUS
3596 ? (*curbuf->b_p_tsr == NUL
3597 ? p_tsr
3598 : curbuf->b_p_tsr)
3599 : (*curbuf->b_p_dict == NUL
3600 ? p_dict
3601 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003602 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003603 dict != NULL ? dict_f
3604 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 dict = NULL;
3606 break;
3607
3608 case CTRL_X_TAGS:
3609 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3610 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003611 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
3613 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003614 * of matches is found when compl_pattern is empty */
3615 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3617 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3618 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3619 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003620 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622 p_ic = save_p_ic;
3623 break;
3624
3625 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003626 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3628 {
3629
3630 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003631 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003632 ins_compl_add_matches(num_matches, matches,
3633#ifdef CASE_INSENSITIVE_FILENAME
3634 TRUE
3635#else
3636 FALSE
3637#endif
3638 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
3640 break;
3641
3642 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003643 if (expand_cmdline(&compl_xp, compl_pattern,
3644 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003646 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 break;
3648
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003649#ifdef FEAT_COMPL_FUNC
3650 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003651 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003652 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003653 break;
3654#endif
3655
Bram Moolenaar488c6512005-08-11 20:09:58 +00003656 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003657#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003658 num_matches = expand_spelling(first_match_pos.lnum,
3659 first_match_pos.col, compl_pattern, &matches);
3660 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003661 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003662#endif
3663 break;
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 default: /* normal ^P/^N and ^X^L */
3666 /*
3667 * If 'infercase' is set, don't use 'smartcase' here
3668 */
3669 save_p_scs = p_scs;
3670 if (ins_buf->b_p_inf)
3671 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003672
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 /* buffers other than curbuf are scanned from the beginning or the
3674 * end but never from the middle, thus setting nowrapscan in this
3675 * buffers is a good idea, on the other hand, we always set
3676 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3677 save_p_ws = p_ws;
3678 if (ins_buf != curbuf)
3679 p_ws = FALSE;
3680 else if (*e_cpt == '.')
3681 p_ws = TRUE;
3682 for (;;)
3683 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003684 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003686 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3687 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003689 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003691 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003693 found_new_match = searchit(NULL, ins_buf, pos,
3694 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003695 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003696 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003697 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003699 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003700 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 first_match_pos = *pos;
3702 last_match_pos = *pos;
3703 }
3704 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003705 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 found_new_match = FAIL;
3707 if (found_new_match == FAIL)
3708 {
3709 if (ins_buf == curbuf)
3710 found_all = TRUE;
3711 break;
3712 }
3713
3714 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003715 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 && ini->lnum == pos->lnum
3717 && ini->col == pos->col)
3718 continue;
3719 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3720 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3721 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003722 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
3724 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3725 continue;
3726 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3727 if (!p_paste)
3728 ptr = skipwhite(ptr);
3729 }
3730 len = (int)STRLEN(ptr);
3731 }
3732 else
3733 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003734 char_u *tmp_ptr = ptr;
3735
3736 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003738 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 /* Skip if already inside a word. */
3740 if (vim_iswordp(tmp_ptr))
3741 continue;
3742 /* Find start of next word. */
3743 tmp_ptr = find_word_start(tmp_ptr);
3744 }
3745 /* Find end of this word. */
3746 tmp_ptr = find_word_end(tmp_ptr);
3747 len = (int)(tmp_ptr - ptr);
3748
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003749 if ((compl_cont_status & CONT_ADDING)
3750 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 {
3752 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3753 {
3754 /* Try next line, if any. the new word will be
3755 * "join" as if the normal command "J" was used.
3756 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003757 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 * works -- Acevedo */
3759 STRNCPY(IObuff, ptr, len);
3760 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3761 tmp_ptr = ptr = skipwhite(ptr);
3762 /* Find start of next word. */
3763 tmp_ptr = find_word_start(tmp_ptr);
3764 /* Find end of next word. */
3765 tmp_ptr = find_word_end(tmp_ptr);
3766 if (tmp_ptr > ptr)
3767 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003768 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003770 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 IObuff[len++] = ' ';
3772 /* IObuf =~ "\k.* ", thus len >= 2 */
3773 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003774 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 || (vim_strchr(p_cpo, CPO_JOINSP)
3776 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003777 && (IObuff[len - 2] == '?'
3778 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 IObuff[len++] = ' ';
3780 }
3781 /* copy as much as posible of the new word */
3782 if (tmp_ptr - ptr >= IOSIZE - len)
3783 tmp_ptr = ptr + IOSIZE - len - 1;
3784 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3785 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003786 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 }
3788 IObuff[len] = NUL;
3789 ptr = IObuff;
3790 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003791 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 continue;
3793 }
3794 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003795 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003796 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003797 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 {
3799 found_new_match = OK;
3800 break;
3801 }
3802 }
3803 p_scs = save_p_scs;
3804 p_ws = save_p_ws;
3805 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003806
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003807 /* check if compl_curr_match has changed, (e.g. other type of
3808 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003809 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 found_new_match = OK;
3811
3812 /* break the loop for specialized modes (use 'complete' just for the
3813 * generic ctrl_x_mode == 0) or when we've found a new match */
3814 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003815 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003816 {
3817 if (got_int)
3818 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003819 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003820 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003821 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003822
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003823 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3824 || compl_interrupted)
3825 break;
3826 compl_started = TRUE;
3827 }
3828 else
3829 {
3830 /* Mark a buffer scanned when it has been scanned completely */
3831 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3832 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003834 compl_started = FALSE;
3835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003837 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838
3839 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3840 && *e_cpt == NUL) /* Got to end of 'complete' */
3841 found_new_match = FAIL;
3842
3843 i = -1; /* total of matches, unknown */
3844 if (found_new_match == FAIL
3845 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3846 i = ins_compl_make_cyclic();
3847
3848 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003849 * just been made cyclic then we have to move compl_curr_match to the next
3850 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003851 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
3852 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003853 if (compl_curr_match == NULL)
3854 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 return i;
3856}
3857
3858/* Delete the old text being completed. */
3859 static void
3860ins_compl_delete()
3861{
3862 int i;
3863
3864 /*
3865 * In insert mode: Delete the typed part.
3866 * In replace mode: Put the old characters back, if any.
3867 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003868 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 backspace_until_column(i);
3870 changed_cline_bef_curs();
3871}
3872
3873/* Insert the new text being completed. */
3874 static void
3875ins_compl_insert()
3876{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003877 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003878 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3879 compl_used_match = FALSE;
3880 else
3881 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882}
3883
3884/*
3885 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003886 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3887 * get more completions. If it is FALSE, then we just do nothing when there
3888 * are no more completions in a given direction. The latter case is used when
3889 * we are still in the middle of finding completions, to allow browsing
3890 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 * Return the total number of matches, or -1 if still unknown -- webb.
3892 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003893 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3894 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 *
3896 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003897 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3898 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 */
3900 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003901ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003903 int count; /* repeat completion this many times; should
3904 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003905 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906{
3907 int num_matches = -1;
3908 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003909 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003910 compl_T *found_compl = NULL;
3911 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003913 if (compl_leader != NULL
3914 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003916 /* Set "compl_shown_match" to the actually shown match, it may differ
3917 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003918 while (!ins_compl_equal(compl_shown_match,
3919 compl_leader, STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003920 && compl_shown_match->cp_next != NULL
3921 && compl_shown_match->cp_next != compl_first_match)
3922 compl_shown_match = compl_shown_match->cp_next;
3923 }
3924
3925 if (allow_get_expansion && insert_match
3926 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 /* Delete old text to be replaced */
3928 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003929
Bram Moolenaare3226be2005-12-18 22:10:00 +00003930 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3931 * around. */
3932 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003934 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003936 if (compl_pending != 0)
3937 --compl_pending;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003938 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003939 found_end = (compl_first_match != NULL
3940 && (compl_shown_match->cp_next == compl_first_match
3941 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003942 }
3943 else if (compl_shows_dir == BACKWARD
3944 && compl_shown_match->cp_prev != NULL)
3945 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003946 if (compl_pending != 0)
3947 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00003948 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003949 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003950 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
3952 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003953 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003954 if (compl_shows_dir == BACKWARD)
3955 --compl_pending;
3956 else
3957 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00003958 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003959 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003960
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003961 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003962 if (compl_pending != 0 && compl_direction == compl_shows_dir)
Bram Moolenaara6557602006-02-04 22:43:20 +00003963 compl_shown_match = compl_curr_match;
3964 found_end = FALSE;
3965 }
3966 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
3967 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003968 && !ins_compl_equal(compl_shown_match,
3969 compl_leader, STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00003970 ++todo;
3971 else
3972 /* Remember a matching item. */
3973 found_compl = compl_shown_match;
3974
3975 /* Stop at the end of the list when we found a usable match. */
3976 if (found_end)
3977 {
3978 if (found_compl != NULL)
3979 {
3980 compl_shown_match = found_compl;
3981 break;
3982 }
3983 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 }
3986
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003987 /* Insert the text of the new completion, or the compl_leader. */
3988 if (insert_match)
3989 {
3990 if (!compl_get_longest || compl_used_match)
3991 ins_compl_insert();
3992 else
3993 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3994 }
3995 else
3996 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997
3998 if (!allow_get_expansion)
3999 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004000 /* may undisplay the popup menu first */
4001 ins_compl_upd_pum();
4002
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004003 /* redraw to show the user what was inserted */
4004 update_screen(0);
4005
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004006 /* display the updated popup menu */
4007 ins_compl_show_pum();
4008
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 /* Delete old text to be replaced, since we're still searching and
4010 * don't want to match ourselves! */
4011 ins_compl_delete();
4012 }
4013
4014 /*
4015 * Show the file name for the match (if any)
4016 * Truncate the file name to avoid a wait for return.
4017 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004018 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
4020 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004021 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 if (i <= 0)
4023 i = 0;
4024 else
4025 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004026 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 msg(IObuff);
4028 redraw_cmdline = FALSE; /* don't overwrite! */
4029 }
4030
4031 return num_matches;
4032}
4033
4034/*
4035 * Call this while finding completions, to check whether the user has hit a key
4036 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004037 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004039 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 */
4041 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004042ins_compl_check_keys(frequency)
4043 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044{
4045 static int count = 0;
4046
4047 int c;
4048
4049 /* Don't check when reading keys from a script. That would break the test
4050 * scripts */
4051 if (using_script())
4052 return;
4053
4054 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004055 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 return;
4057 count = 0;
4058
4059 ++no_mapping;
4060 c = vpeekc_any();
4061 --no_mapping;
4062 if (c != NUL)
4063 {
4064 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4065 {
4066 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004067 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004068 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4069 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
4071 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004072 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004074 if (compl_pending != 0 && !got_int)
4075 (void)ins_compl_next(FALSE, compl_pending > 0
4076 ? compl_pending : -compl_pending, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004077}
4078
4079/*
4080 * Decide the direction of Insert mode complete from the key typed.
4081 * Returns BACKWARD or FORWARD.
4082 */
4083 static int
4084ins_compl_key2dir(c)
4085 int c;
4086{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004087 if (c == Ctrl_P || c == Ctrl_L
4088 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4089 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004090 return BACKWARD;
4091 return FORWARD;
4092}
4093
4094/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004095 * Return TRUE for keys that are used for completion only when the popup menu
4096 * is visible.
4097 */
4098 static int
4099ins_compl_pum_key(c)
4100 int c;
4101{
4102 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004103 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4104 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004105}
4106
4107/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004108 * Decide the number of completions to move forward.
4109 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4110 */
4111 static int
4112ins_compl_key2count(c)
4113 int c;
4114{
4115 int h;
4116
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004117 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004118 {
4119 h = pum_get_height();
4120 if (h > 3)
4121 h -= 2; /* keep some context */
4122 return h;
4123 }
4124 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125}
4126
4127/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004128 * Return TRUE if completion with "c" should insert the match, FALSE if only
4129 * to change the currently selected completion.
4130 */
4131 static int
4132ins_compl_use_match(c)
4133 int c;
4134{
4135 switch (c)
4136 {
4137 case K_UP:
4138 case K_DOWN:
4139 case K_PAGEDOWN:
4140 case K_KPAGEDOWN:
4141 case K_S_DOWN:
4142 case K_PAGEUP:
4143 case K_KPAGEUP:
4144 case K_S_UP:
4145 return FALSE;
4146 }
4147 return TRUE;
4148}
4149
4150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 * Do Insert mode completion.
4152 * Called when character "c" was typed, which has a meaning for completion.
4153 * Returns OK if completion was done, FAIL if something failed (out of mem).
4154 */
4155 static int
4156ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004157 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004159 char_u *line;
4160 int startcol = 0; /* column where searched text starts */
4161 colnr_T curs_col; /* cursor column */
4162 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
Bram Moolenaare3226be2005-12-18 22:10:00 +00004164 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004165 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 {
4167 /* First time we hit ^N or ^P (in a row, I mean) */
4168
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 did_ai = FALSE;
4170#ifdef FEAT_SMARTINDENT
4171 did_si = FALSE;
4172 can_si = FALSE;
4173 can_si_back = FALSE;
4174#endif
4175 if (stop_arrow() == FAIL)
4176 return FAIL;
4177
4178 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004179 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004180 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
4182 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004183 * "compl_startpos" to the cursor as a pattern to add a new word
4184 * instead of expand the one before the cursor, in word-wise if
4185 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 * is not in the same line as the cursor then fix it (the line has
4187 * been split because it was longer than 'tw'). if SOL is set then
4188 * skip the previous pattern, a word at the beginning of the line has
4189 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004190 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4191 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 {
4193 /*
4194 * it is a continued search
4195 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004196 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4198 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4199 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004200 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004202 /* line (probably) wrapped, set compl_startpos to the
4203 * first non_blank in the line, if it is not a wordchar
4204 * include it to get a better pattern, but then we don't
4205 * want the "\\<" prefix, check it bellow */
4206 compl_col = (colnr_T)(skipwhite(line) - line);
4207 compl_startpos.col = compl_col;
4208 compl_startpos.lnum = curwin->w_cursor.lnum;
4209 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 }
4211 else
4212 {
4213 /* S_IPOS was set when we inserted a word that was at the
4214 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004215 * mode but first we need to redefine compl_startpos */
4216 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004218 compl_cont_status |= CONT_SOL;
4219 compl_startpos.col = (colnr_T)(skipwhite(
4220 line + compl_length
4221 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004223 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004225 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004226 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 * have enough space? just being paranoic */
4228#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004229 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004231 compl_cont_status &= ~CONT_SOL;
4232 compl_length = (IOSIZE - MIN_SPACE);
4233 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004235 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4236 if (compl_length < 1)
4237 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 }
4239 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004240 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004242 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 }
4244 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004245 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004247 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004249 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251 compl_cont_status = 0;
4252 compl_cont_status |= CONT_N_ADDS;
4253 compl_startpos = curwin->w_cursor;
4254 startcol = (int)curs_col;
4255 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
4257
4258 /* Work out completion pattern and original text -- webb */
4259 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4260 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004261 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4263 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004264 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004266 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004268 compl_col += ++startcol;
4269 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004272 compl_pattern = str_foldcase(line + compl_col,
4273 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004275 compl_pattern = vim_strnsave(line + compl_col,
4276 compl_length);
4277 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 return FAIL;
4279 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004280 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 {
4282 char_u *prefix = (char_u *)"\\<";
4283
4284 /* we need 3 extra chars, 1 for the NUL and
4285 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004286 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4287 compl_length) + 3);
4288 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004290 if (!vim_iswordp(line + compl_col)
4291 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 && (
4293#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004294 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004296 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297#endif
4298 )))
4299 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004300 STRCPY((char *)compl_pattern, prefix);
4301 (void)quote_meta(compl_pattern + STRLEN(prefix),
4302 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004304 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004306 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004308 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309#endif
4310 )
4311 {
4312 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004313 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4314 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004316 compl_col += curs_col;
4317 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 }
4319 else
4320 {
4321#ifdef FEAT_MBYTE
4322 /* Search the point of change class of multibyte character
4323 * or not a word single byte character backward. */
4324 if (has_mbyte)
4325 {
4326 int base_class;
4327 int head_off;
4328
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004329 startcol -= (*mb_head_off)(line, line + startcol);
4330 base_class = mb_get_class(line + startcol);
4331 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004333 head_off = (*mb_head_off)(line, line + startcol);
4334 if (base_class != mb_get_class(line + startcol
4335 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004337 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339 }
4340 else
4341#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004342 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004344 compl_col += ++startcol;
4345 compl_length = (int)curs_col - startcol;
4346 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 {
4348 /* Only match word with at least two chars -- webb
4349 * there's no need to call quote_meta,
4350 * alloc(7) is enough -- Acevedo
4351 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004352 compl_pattern = alloc(7);
4353 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004355 STRCPY((char *)compl_pattern, "\\<");
4356 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4357 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359 else
4360 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004361 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4362 compl_length) + 3);
4363 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004365 STRCPY((char *)compl_pattern, "\\<");
4366 (void)quote_meta(compl_pattern + 2, line + compl_col,
4367 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 }
4370 }
4371 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4372 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004373 compl_col = skipwhite(line) - line;
4374 compl_length = (int)curs_col - (int)compl_col;
4375 if (compl_length < 0) /* cursor in indent: empty pattern */
4376 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004378 compl_pattern = str_foldcase(line + compl_col, compl_length,
4379 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004381 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4382 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 return FAIL;
4384 }
4385 else if (ctrl_x_mode == CTRL_X_FILES)
4386 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004387 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004389 compl_col += ++startcol;
4390 compl_length = (int)curs_col - startcol;
4391 compl_pattern = addstar(line + compl_col, compl_length,
4392 EXPAND_FILES);
4393 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 return FAIL;
4395 }
4396 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4397 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004398 compl_pattern = vim_strnsave(line, curs_col);
4399 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004401 set_cmd_context(&compl_xp, compl_pattern,
4402 (int)STRLEN(compl_pattern), curs_col);
4403 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4404 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004406 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4407 compl_col = startcol;
4408 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004410 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004411 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004412#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004413 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004414 * Call user defined function 'completefunc' with "a:findstart"
4415 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004416 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004417 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004418 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004419 char_u *funcname;
4420 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004421
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004422 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004423 * string */
4424 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4425 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4426 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004427 {
4428 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4429 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004430 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004431 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004432
4433 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004434 args[1] = NULL;
4435 pos = curwin->w_cursor;
4436 col = call_func_retnr(funcname, 2, args, FALSE);
4437 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004438
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004439 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004440 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004441 compl_col = col;
4442 if ((colnr_T)compl_col > curs_col)
4443 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004444
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004445 /* Setup variables for completion. Need to obtain "line" again,
4446 * it may have become invalid. */
4447 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004448 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004449 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4450 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004451#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004452 return FAIL;
4453 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004454 else if (ctrl_x_mode == CTRL_X_SPELL)
4455 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004456#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004457 if (spell_bad_len > 0)
4458 compl_col = curs_col - spell_bad_len;
4459 else
4460 compl_col = spell_word_start(startcol);
4461 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004462 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004463 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004464 compl_length = (int)curs_col - compl_col;
4465 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4466 if (compl_pattern == NULL)
4467#endif
4468 return FAIL;
4469 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004470 else
4471 {
4472 EMSG2(_(e_intern2), "ins_complete()");
4473 return FAIL;
4474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004476 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 {
4478 edit_submode_pre = (char_u *)_(" Adding");
4479 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4480 {
4481 /* Insert a new line, keep indentation but ignore 'comments' */
4482#ifdef FEAT_COMMENTS
4483 char_u *old = curbuf->b_p_com;
4484
4485 curbuf->b_p_com = (char_u *)"";
4486#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004487 compl_startpos.lnum = curwin->w_cursor.lnum;
4488 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 ins_eol('\r');
4490#ifdef FEAT_COMMENTS
4491 curbuf->b_p_com = old;
4492#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004493 compl_length = 0;
4494 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 }
4496 }
4497 else
4498 {
4499 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004500 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
4502
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004503 if (compl_cont_status & CONT_LOCAL)
4504 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 else
4506 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4507
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004508 /* Always add completion for the original text. */
4509 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004510 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4511 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaard289f132006-03-11 21:30:53 +00004512 -1, FALSE, NULL, NULL, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004514 vim_free(compl_pattern);
4515 compl_pattern = NULL;
4516 vim_free(compl_orig_text);
4517 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 return FAIL;
4519 }
4520
4521 /* showmode might reset the internal line pointers, so it must
4522 * be called before line = ml_get(), or when this address is no
4523 * longer needed. -- Acevedo.
4524 */
4525 edit_submode_extra = (char_u *)_("-- Searching...");
4526 edit_submode_highl = HLF_COUNT;
4527 showmode();
4528 edit_submode_extra = NULL;
4529 out_flush();
4530 }
4531
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004532 compl_shown_match = compl_curr_match;
4533 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
4535 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004536 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004538 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004540 /* may undisplay the popup menu */
4541 ins_compl_upd_pum();
4542
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004543 if (n > 1) /* all matches have been found */
4544 compl_matches = n;
4545 compl_curr_match = compl_shown_match;
4546 compl_direction = compl_shows_dir;
4547 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
4549 /* eat the ESC to avoid leaving insert mode */
4550 if (got_int && !global_busy)
4551 {
4552 (void)vgetc();
4553 got_int = FALSE;
4554 }
4555
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004556 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004557 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004559 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4560 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4562 edit_submode_highl = HLF_E;
4563 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4564 * because we couldn't expand anything at first place, but if we used
4565 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4566 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004567 if ( compl_length > 1
4568 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 || (ctrl_x_mode != 0
4570 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4571 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004572 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 }
4574
Bram Moolenaar572cb562005-08-05 21:35:02 +00004575 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004576 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004578 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
4580 if (edit_submode_extra == NULL)
4581 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004582 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
4584 edit_submode_extra = (char_u *)_("Back at original");
4585 edit_submode_highl = HLF_W;
4586 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004587 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 {
4589 edit_submode_extra = (char_u *)_("Word from other line");
4590 edit_submode_highl = HLF_COUNT;
4591 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004592 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 {
4594 edit_submode_extra = (char_u *)_("The only match");
4595 edit_submode_highl = HLF_COUNT;
4596 }
4597 else
4598 {
4599 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004600 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004602 int number = 0;
4603 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004605 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {
4607 /* search backwards for the first valid (!= -1) number.
4608 * This should normally succeed already at the first loop
4609 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004610 for (match = compl_curr_match->cp_prev; match != NULL
4611 && match != compl_first_match;
4612 match = match->cp_prev)
4613 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004615 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 break;
4617 }
4618 if (match != NULL)
4619 /* go up and assign all numbers which are not assigned
4620 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004621 for (match = match->cp_next;
4622 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004623 match = match->cp_next)
4624 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
4626 else /* BACKWARD */
4627 {
4628 /* search forwards (upwards) for the first valid (!= -1)
4629 * number. This should normally succeed already at the
4630 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004631 for (match = compl_curr_match->cp_next; match != NULL
4632 && match != compl_first_match;
4633 match = match->cp_next)
4634 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004636 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 break;
4638 }
4639 if (match != NULL)
4640 /* go down and assign all numbers which are not
4641 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004642 for (match = match->cp_prev; match
4643 && match->cp_number == -1;
4644 match = match->cp_prev)
4645 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
4647 }
4648
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004649 /* The match should always have a sequence number now, this is
4650 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004651 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 {
4653 /* Space for 10 text chars. + 2x10-digit no.s */
4654 static char_u match_ref[31];
4655
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004656 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004658 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004660 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004661 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004662 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 edit_submode_extra = match_ref;
4664 edit_submode_highl = HLF_R;
4665 if (dollar_vcol)
4666 curs_columns(FALSE);
4667 }
4668 }
4669 }
4670
4671 /* Show a message about what (completion) mode we're in. */
4672 showmode();
4673 if (edit_submode_extra != NULL)
4674 {
4675 if (!p_smd)
4676 msg_attr(edit_submode_extra,
4677 edit_submode_highl < HLF_COUNT
4678 ? hl_attr(edit_submode_highl) : 0);
4679 }
4680 else
4681 msg_clr_cmdline(); /* necessary for "noshowmode" */
4682
Bram Moolenaara94bc432006-03-10 21:42:59 +00004683 /* RedrawingDisabled may be set when invoked through complete(). */
4684 n = RedrawingDisabled;
4685 RedrawingDisabled = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004686 ins_compl_show_pum();
Bram Moolenaara94bc432006-03-10 21:42:59 +00004687 setcursor();
4688 RedrawingDisabled = n;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004689
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 return OK;
4691}
4692
4693/*
4694 * Looks in the first "len" chars. of "src" for search-metachars.
4695 * If dest is not NULL the chars. are copied there quoting (with
4696 * a backslash) the metachars, and dest would be NUL terminated.
4697 * Returns the length (needed) of dest
4698 */
4699 static int
4700quote_meta(dest, src, len)
4701 char_u *dest;
4702 char_u *src;
4703 int len;
4704{
4705 int m;
4706
4707 for (m = len; --len >= 0; src++)
4708 {
4709 switch (*src)
4710 {
4711 case '.':
4712 case '*':
4713 case '[':
4714 if (ctrl_x_mode == CTRL_X_DICTIONARY
4715 || ctrl_x_mode == CTRL_X_THESAURUS)
4716 break;
4717 case '~':
4718 if (!p_magic) /* quote these only if magic is set */
4719 break;
4720 case '\\':
4721 if (ctrl_x_mode == CTRL_X_DICTIONARY
4722 || ctrl_x_mode == CTRL_X_THESAURUS)
4723 break;
4724 case '^': /* currently it's not needed. */
4725 case '$':
4726 m++;
4727 if (dest != NULL)
4728 *dest++ = '\\';
4729 break;
4730 }
4731 if (dest != NULL)
4732 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004733# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 /* Copy remaining bytes of a multibyte character. */
4735 if (has_mbyte)
4736 {
4737 int i, mb_len;
4738
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004739 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 if (mb_len > 0 && len >= mb_len)
4741 for (i = 0; i < mb_len; ++i)
4742 {
4743 --len;
4744 ++src;
4745 if (dest != NULL)
4746 *dest++ = *src;
4747 }
4748 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004749# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 }
4751 if (dest != NULL)
4752 *dest = NUL;
4753
4754 return m;
4755}
4756#endif /* FEAT_INS_EXPAND */
4757
4758/*
4759 * Next character is interpreted literally.
4760 * A one, two or three digit decimal number is interpreted as its byte value.
4761 * If one or two digits are entered, the next character is given to vungetc().
4762 * For Unicode a character > 255 may be returned.
4763 */
4764 int
4765get_literal()
4766{
4767 int cc;
4768 int nc;
4769 int i;
4770 int hex = FALSE;
4771 int octal = FALSE;
4772#ifdef FEAT_MBYTE
4773 int unicode = 0;
4774#endif
4775
4776 if (got_int)
4777 return Ctrl_C;
4778
4779#ifdef FEAT_GUI
4780 /*
4781 * In GUI there is no point inserting the internal code for a special key.
4782 * It is more useful to insert the string "<KEY>" instead. This would
4783 * probably be useful in a text window too, but it would not be
4784 * vi-compatible (maybe there should be an option for it?) -- webb
4785 */
4786 if (gui.in_use)
4787 ++allow_keys;
4788#endif
4789#ifdef USE_ON_FLY_SCROLL
4790 dont_scroll = TRUE; /* disallow scrolling here */
4791#endif
4792 ++no_mapping; /* don't map the next key hits */
4793 cc = 0;
4794 i = 0;
4795 for (;;)
4796 {
4797 do
4798 nc = safe_vgetc();
4799 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4800 || nc == K_HOR_SCROLLBAR);
4801#ifdef FEAT_CMDL_INFO
4802 if (!(State & CMDLINE)
4803# ifdef FEAT_MBYTE
4804 && MB_BYTE2LEN_CHECK(nc) == 1
4805# endif
4806 )
4807 add_to_showcmd(nc);
4808#endif
4809 if (nc == 'x' || nc == 'X')
4810 hex = TRUE;
4811 else if (nc == 'o' || nc == 'O')
4812 octal = TRUE;
4813#ifdef FEAT_MBYTE
4814 else if (nc == 'u' || nc == 'U')
4815 unicode = nc;
4816#endif
4817 else
4818 {
4819 if (hex
4820#ifdef FEAT_MBYTE
4821 || unicode != 0
4822#endif
4823 )
4824 {
4825 if (!vim_isxdigit(nc))
4826 break;
4827 cc = cc * 16 + hex2nr(nc);
4828 }
4829 else if (octal)
4830 {
4831 if (nc < '0' || nc > '7')
4832 break;
4833 cc = cc * 8 + nc - '0';
4834 }
4835 else
4836 {
4837 if (!VIM_ISDIGIT(nc))
4838 break;
4839 cc = cc * 10 + nc - '0';
4840 }
4841
4842 ++i;
4843 }
4844
4845 if (cc > 255
4846#ifdef FEAT_MBYTE
4847 && unicode == 0
4848#endif
4849 )
4850 cc = 255; /* limit range to 0-255 */
4851 nc = 0;
4852
4853 if (hex) /* hex: up to two chars */
4854 {
4855 if (i >= 2)
4856 break;
4857 }
4858#ifdef FEAT_MBYTE
4859 else if (unicode) /* Unicode: up to four or eight chars */
4860 {
4861 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4862 break;
4863 }
4864#endif
4865 else if (i >= 3) /* decimal or octal: up to three chars */
4866 break;
4867 }
4868 if (i == 0) /* no number entered */
4869 {
4870 if (nc == K_ZERO) /* NUL is stored as NL */
4871 {
4872 cc = '\n';
4873 nc = 0;
4874 }
4875 else
4876 {
4877 cc = nc;
4878 nc = 0;
4879 }
4880 }
4881
4882 if (cc == 0) /* NUL is stored as NL */
4883 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004884#ifdef FEAT_MBYTE
4885 if (enc_dbcs && (cc & 0xff) == 0)
4886 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4887 second byte will cause trouble! */
4888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
4890 --no_mapping;
4891#ifdef FEAT_GUI
4892 if (gui.in_use)
4893 --allow_keys;
4894#endif
4895 if (nc)
4896 vungetc(nc);
4897 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4898 return cc;
4899}
4900
4901/*
4902 * Insert character, taking care of special keys and mod_mask
4903 */
4904 static void
4905insert_special(c, allow_modmask, ctrlv)
4906 int c;
4907 int allow_modmask;
4908 int ctrlv; /* c was typed after CTRL-V */
4909{
4910 char_u *p;
4911 int len;
4912
4913 /*
4914 * Special function key, translate into "<Key>". Up to the last '>' is
4915 * inserted with ins_str(), so as not to replace characters in replace
4916 * mode.
4917 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4918 * unless 'allow_modmask' is TRUE.
4919 */
4920#ifdef MACOS
4921 /* Command-key never produces a normal key */
4922 if (mod_mask & MOD_MASK_CMD)
4923 allow_modmask = TRUE;
4924#endif
4925 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4926 {
4927 p = get_special_key_name(c, mod_mask);
4928 len = (int)STRLEN(p);
4929 c = p[len - 1];
4930 if (len > 2)
4931 {
4932 if (stop_arrow() == FAIL)
4933 return;
4934 p[len - 1] = NUL;
4935 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004936 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 ctrlv = FALSE;
4938 }
4939 }
4940 if (stop_arrow() == OK)
4941 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4942}
4943
4944/*
4945 * Special characters in this context are those that need processing other
4946 * than the simple insertion that can be performed here. This includes ESC
4947 * which terminates the insert, and CR/NL which need special processing to
4948 * open up a new line. This routine tries to optimize insertions performed by
4949 * the "redo", "undo" or "put" commands, so it needs to know when it should
4950 * stop and defer processing to the "normal" mechanism.
4951 * '0' and '^' are special, because they can be followed by CTRL-D.
4952 */
4953#ifdef EBCDIC
4954# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4955#else
4956# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4957#endif
4958
4959#ifdef FEAT_MBYTE
4960# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4961#else
4962# define WHITECHAR(cc) vim_iswhite(cc)
4963#endif
4964
4965 void
4966insertchar(c, flags, second_indent)
4967 int c; /* character to insert or NUL */
4968 int flags; /* INSCHAR_FORMAT, etc. */
4969 int second_indent; /* indent for second line if >= 0 */
4970{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 int textwidth;
4972#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976
4977 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4978 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979
4980 /*
4981 * Try to break the line in two or more pieces when:
4982 * - Always do this if we have been called to do formatting only.
4983 * - Always do this when 'formatoptions' has the 'a' flag and the line
4984 * ends in white space.
4985 * - Otherwise:
4986 * - Don't do this if inserting a blank
4987 * - Don't do this if an existing character is being replaced, unless
4988 * we're in VREPLACE mode.
4989 * - Do this if the cursor is not on the line where insert started
4990 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4991 * before the insert.
4992 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4993 * before 'textwidth'
4994 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004995 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 && ((flags & INSCHAR_FORMAT)
4997 || (!vim_iswhite(c)
4998 && !((State & REPLACE_FLAG)
4999#ifdef FEAT_VREPLACE
5000 && !(State & VREPLACE_FLAG)
5001#endif
5002 && *ml_get_cursor() != NUL)
5003 && (curwin->w_cursor.lnum != Insstart.lnum
5004 || ((!has_format_option(FO_INS_LONG)
5005 || Insstart_textlen <= (colnr_T)textwidth)
5006 && (!fo_ins_blank
5007 || Insstart_blank_vcol <= (colnr_T)textwidth
5008 ))))))
5009 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005010 /* Format with 'formatexpr' when it's set. Use internal formatting
5011 * when 'formatexpr' isn't set or it returns non-zero. */
5012#if defined(FEAT_EVAL)
5013 if (*curbuf->b_p_fex == NUL
5014 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005016 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005018
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (c == NUL) /* only formatting was wanted */
5020 return;
5021
5022#ifdef FEAT_COMMENTS
5023 /* Check whether this character should end a comment. */
5024 if (did_ai && (int)c == end_comment_pending)
5025 {
5026 char_u *line;
5027 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5028 int middle_len, end_len;
5029 int i;
5030
5031 /*
5032 * Need to remove existing (middle) comment leader and insert end
5033 * comment leader. First, check what comment leader we can find.
5034 */
5035 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5036 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5037 {
5038 /* Skip middle-comment string */
5039 while (*p && p[-1] != ':') /* find end of middle flags */
5040 ++p;
5041 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5042 /* Don't count trailing white space for middle_len */
5043 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5044 --middle_len;
5045
5046 /* Find the end-comment string */
5047 while (*p && p[-1] != ':') /* find end of end flags */
5048 ++p;
5049 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5050
5051 /* Skip white space before the cursor */
5052 i = curwin->w_cursor.col;
5053 while (--i >= 0 && vim_iswhite(line[i]))
5054 ;
5055 i++;
5056
5057 /* Skip to before the middle leader */
5058 i -= middle_len;
5059
5060 /* Check some expected things before we go on */
5061 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5062 {
5063 /* Backspace over all the stuff we want to replace */
5064 backspace_until_column(i);
5065
5066 /*
5067 * Insert the end-comment string, except for the last
5068 * character, which will get inserted as normal later.
5069 */
5070 ins_bytes_len(lead_end, end_len - 1);
5071 }
5072 }
5073 }
5074 end_comment_pending = NUL;
5075#endif
5076
5077 did_ai = FALSE;
5078#ifdef FEAT_SMARTINDENT
5079 did_si = FALSE;
5080 can_si = FALSE;
5081 can_si_back = FALSE;
5082#endif
5083
5084 /*
5085 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5086 * This speeds up normal text input considerably.
5087 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5088 * need to re-indent at a ':', or any other character (but not what
5089 * 'paste' is set)..
5090 */
5091#ifdef USE_ON_FLY_SCROLL
5092 dont_scroll = FALSE; /* allow scrolling here */
5093#endif
5094
5095 if ( !ISSPECIAL(c)
5096#ifdef FEAT_MBYTE
5097 && (!has_mbyte || (*mb_char2len)(c) == 1)
5098#endif
5099 && vpeekc() != NUL
5100 && !(State & REPLACE_FLAG)
5101#ifdef FEAT_CINDENT
5102 && !cindent_on()
5103#endif
5104#ifdef FEAT_RIGHTLEFT
5105 && !p_ri
5106#endif
5107 )
5108 {
5109#define INPUT_BUFLEN 100
5110 char_u buf[INPUT_BUFLEN + 1];
5111 int i;
5112 colnr_T virtcol = 0;
5113
5114 buf[0] = c;
5115 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005116 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 virtcol = get_nolist_virtcol();
5118 /*
5119 * Stop the string when:
5120 * - no more chars available
5121 * - finding a special character (command key)
5122 * - buffer is full
5123 * - running into the 'textwidth' boundary
5124 * - need to check for abbreviation: A non-word char after a word-char
5125 */
5126 while ( (c = vpeekc()) != NUL
5127 && !ISSPECIAL(c)
5128#ifdef FEAT_MBYTE
5129 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5130#endif
5131 && i < INPUT_BUFLEN
5132 && (textwidth == 0
5133 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5134 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5135 {
5136#ifdef FEAT_RIGHTLEFT
5137 c = vgetc();
5138 if (p_hkmap && KeyTyped)
5139 c = hkmap(c); /* Hebrew mode mapping */
5140# ifdef FEAT_FKMAP
5141 if (p_fkmap && KeyTyped)
5142 c = fkmap(c); /* Farsi mode mapping */
5143# endif
5144 buf[i++] = c;
5145#else
5146 buf[i++] = vgetc();
5147#endif
5148 }
5149
5150#ifdef FEAT_DIGRAPHS
5151 do_digraph(-1); /* clear digraphs */
5152 do_digraph(buf[i-1]); /* may be the start of a digraph */
5153#endif
5154 buf[i] = NUL;
5155 ins_str(buf);
5156 if (flags & INSCHAR_CTRLV)
5157 {
5158 redo_literal(*buf);
5159 i = 1;
5160 }
5161 else
5162 i = 0;
5163 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005164 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
5166 else
5167 {
5168#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005169 int cc;
5170
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5172 {
5173 char_u buf[MB_MAXBYTES + 1];
5174
5175 (*mb_char2bytes)(c, buf);
5176 buf[cc] = NUL;
5177 ins_char_bytes(buf, cc);
5178 AppendCharToRedobuff(c);
5179 }
5180 else
5181#endif
5182 {
5183 ins_char(c);
5184 if (flags & INSCHAR_CTRLV)
5185 redo_literal(c);
5186 else
5187 AppendCharToRedobuff(c);
5188 }
5189 }
5190}
5191
5192/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005193 * Format text at the current insert position.
5194 */
5195 static void
5196internal_format(textwidth, second_indent, flags, format_only)
5197 int textwidth;
5198 int second_indent;
5199 int flags;
5200 int format_only;
5201{
5202 int cc;
5203 int save_char = NUL;
5204 int haveto_redraw = FALSE;
5205 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5206#ifdef FEAT_MBYTE
5207 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5208#endif
5209 int fo_white_par = has_format_option(FO_WHITE_PAR);
5210 int first_line = TRUE;
5211#ifdef FEAT_COMMENTS
5212 colnr_T leader_len;
5213 int no_leader = FALSE;
5214 int do_comments = (flags & INSCHAR_DO_COM);
5215#endif
5216
5217 /*
5218 * When 'ai' is off we don't want a space under the cursor to be
5219 * deleted. Replace it with an 'x' temporarily.
5220 */
5221 if (!curbuf->b_p_ai)
5222 {
5223 cc = gchar_cursor();
5224 if (vim_iswhite(cc))
5225 {
5226 save_char = cc;
5227 pchar_cursor('x');
5228 }
5229 }
5230
5231 /*
5232 * Repeat breaking lines, until the current line is not too long.
5233 */
5234 while (!got_int)
5235 {
5236 int startcol; /* Cursor column at entry */
5237 int wantcol; /* column at textwidth border */
5238 int foundcol; /* column for start of spaces */
5239 int end_foundcol = 0; /* column for start of word */
5240 colnr_T len;
5241 colnr_T virtcol;
5242#ifdef FEAT_VREPLACE
5243 int orig_col = 0;
5244 char_u *saved_text = NULL;
5245#endif
5246 colnr_T col;
5247
5248 virtcol = get_nolist_virtcol();
5249 if (virtcol < (colnr_T)textwidth)
5250 break;
5251
5252#ifdef FEAT_COMMENTS
5253 if (no_leader)
5254 do_comments = FALSE;
5255 else if (!(flags & INSCHAR_FORMAT)
5256 && has_format_option(FO_WRAP_COMS))
5257 do_comments = TRUE;
5258
5259 /* Don't break until after the comment leader */
5260 if (do_comments)
5261 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5262 else
5263 leader_len = 0;
5264
5265 /* If the line doesn't start with a comment leader, then don't
5266 * start one in a following broken line. Avoids that a %word
5267 * moved to the start of the next line causes all following lines
5268 * to start with %. */
5269 if (leader_len == 0)
5270 no_leader = TRUE;
5271#endif
5272 if (!(flags & INSCHAR_FORMAT)
5273#ifdef FEAT_COMMENTS
5274 && leader_len == 0
5275#endif
5276 && !has_format_option(FO_WRAP))
5277
5278 {
5279 textwidth = 0;
5280 break;
5281 }
5282 if ((startcol = curwin->w_cursor.col) == 0)
5283 break;
5284
5285 /* find column of textwidth border */
5286 coladvance((colnr_T)textwidth);
5287 wantcol = curwin->w_cursor.col;
5288
5289 curwin->w_cursor.col = startcol - 1;
5290#ifdef FEAT_MBYTE
5291 /* Correct cursor for multi-byte character. */
5292 if (has_mbyte)
5293 mb_adjust_cursor();
5294#endif
5295 foundcol = 0;
5296
5297 /*
5298 * Find position to break at.
5299 * Stop at first entered white when 'formatoptions' has 'v'
5300 */
5301 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5302 || curwin->w_cursor.lnum != Insstart.lnum
5303 || curwin->w_cursor.col >= Insstart.col)
5304 {
5305 cc = gchar_cursor();
5306 if (WHITECHAR(cc))
5307 {
5308 /* remember position of blank just before text */
5309 end_foundcol = curwin->w_cursor.col;
5310
5311 /* find start of sequence of blanks */
5312 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5313 {
5314 dec_cursor();
5315 cc = gchar_cursor();
5316 }
5317 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5318 break; /* only spaces in front of text */
5319#ifdef FEAT_COMMENTS
5320 /* Don't break until after the comment leader */
5321 if (curwin->w_cursor.col < leader_len)
5322 break;
5323#endif
5324 if (has_format_option(FO_ONE_LETTER))
5325 {
5326 /* do not break after one-letter words */
5327 if (curwin->w_cursor.col == 0)
5328 break; /* one-letter word at begin */
5329
5330 col = curwin->w_cursor.col;
5331 dec_cursor();
5332 cc = gchar_cursor();
5333
5334 if (WHITECHAR(cc))
5335 continue; /* one-letter, continue */
5336 curwin->w_cursor.col = col;
5337 }
5338#ifdef FEAT_MBYTE
5339 if (has_mbyte)
5340 foundcol = curwin->w_cursor.col
5341 + (*mb_ptr2len)(ml_get_cursor());
5342 else
5343#endif
5344 foundcol = curwin->w_cursor.col + 1;
5345 if (curwin->w_cursor.col < (colnr_T)wantcol)
5346 break;
5347 }
5348#ifdef FEAT_MBYTE
5349 else if (cc >= 0x100 && fo_multibyte
5350 && curwin->w_cursor.col <= (colnr_T)wantcol)
5351 {
5352 /* Break after or before a multi-byte character. */
5353 foundcol = curwin->w_cursor.col;
5354 if (curwin->w_cursor.col < (colnr_T)wantcol)
5355 foundcol += (*mb_char2len)(cc);
5356 end_foundcol = foundcol;
5357 break;
5358 }
5359#endif
5360 if (curwin->w_cursor.col == 0)
5361 break;
5362 dec_cursor();
5363 }
5364
5365 if (foundcol == 0) /* no spaces, cannot break line */
5366 {
5367 curwin->w_cursor.col = startcol;
5368 break;
5369 }
5370
5371 /* Going to break the line, remove any "$" now. */
5372 undisplay_dollar();
5373
5374 /*
5375 * Offset between cursor position and line break is used by replace
5376 * stack functions. VREPLACE does not use this, and backspaces
5377 * over the text instead.
5378 */
5379#ifdef FEAT_VREPLACE
5380 if (State & VREPLACE_FLAG)
5381 orig_col = startcol; /* Will start backspacing from here */
5382 else
5383#endif
5384 replace_offset = startcol - end_foundcol - 1;
5385
5386 /*
5387 * adjust startcol for spaces that will be deleted and
5388 * characters that will remain on top line
5389 */
5390 curwin->w_cursor.col = foundcol;
5391 while (cc = gchar_cursor(), WHITECHAR(cc))
5392 inc_cursor();
5393 startcol -= curwin->w_cursor.col;
5394 if (startcol < 0)
5395 startcol = 0;
5396
5397#ifdef FEAT_VREPLACE
5398 if (State & VREPLACE_FLAG)
5399 {
5400 /*
5401 * In VREPLACE mode, we will backspace over the text to be
5402 * wrapped, so save a copy now to put on the next line.
5403 */
5404 saved_text = vim_strsave(ml_get_cursor());
5405 curwin->w_cursor.col = orig_col;
5406 if (saved_text == NULL)
5407 break; /* Can't do it, out of memory */
5408 saved_text[startcol] = NUL;
5409
5410 /* Backspace over characters that will move to the next line */
5411 if (!fo_white_par)
5412 backspace_until_column(foundcol);
5413 }
5414 else
5415#endif
5416 {
5417 /* put cursor after pos. to break line */
5418 if (!fo_white_par)
5419 curwin->w_cursor.col = foundcol;
5420 }
5421
5422 /*
5423 * Split the line just before the margin.
5424 * Only insert/delete lines, but don't really redraw the window.
5425 */
5426 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5427 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5428#ifdef FEAT_COMMENTS
5429 + (do_comments ? OPENLINE_DO_COM : 0)
5430#endif
5431 , old_indent);
5432 old_indent = 0;
5433
5434 replace_offset = 0;
5435 if (first_line)
5436 {
5437 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5438 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5439 if (second_indent >= 0)
5440 {
5441#ifdef FEAT_VREPLACE
5442 if (State & VREPLACE_FLAG)
5443 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5444 else
5445#endif
5446 (void)set_indent(second_indent, SIN_CHANGED);
5447 }
5448 first_line = FALSE;
5449 }
5450
5451#ifdef FEAT_VREPLACE
5452 if (State & VREPLACE_FLAG)
5453 {
5454 /*
5455 * In VREPLACE mode we have backspaced over the text to be
5456 * moved, now we re-insert it into the new line.
5457 */
5458 ins_bytes(saved_text);
5459 vim_free(saved_text);
5460 }
5461 else
5462#endif
5463 {
5464 /*
5465 * Check if cursor is not past the NUL off the line, cindent
5466 * may have added or removed indent.
5467 */
5468 curwin->w_cursor.col += startcol;
5469 len = (colnr_T)STRLEN(ml_get_curline());
5470 if (curwin->w_cursor.col > len)
5471 curwin->w_cursor.col = len;
5472 }
5473
5474 haveto_redraw = TRUE;
5475#ifdef FEAT_CINDENT
5476 can_cindent = TRUE;
5477#endif
5478 /* moved the cursor, don't autoindent or cindent now */
5479 did_ai = FALSE;
5480#ifdef FEAT_SMARTINDENT
5481 did_si = FALSE;
5482 can_si = FALSE;
5483 can_si_back = FALSE;
5484#endif
5485 line_breakcheck();
5486 }
5487
5488 if (save_char != NUL) /* put back space after cursor */
5489 pchar_cursor(save_char);
5490
5491 if (!format_only && haveto_redraw)
5492 {
5493 update_topline();
5494 redraw_curbuf_later(VALID);
5495 }
5496}
5497
5498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 * Called after inserting or deleting text: When 'formatoptions' includes the
5500 * 'a' flag format from the current line until the end of the paragraph.
5501 * Keep the cursor at the same position relative to the text.
5502 * The caller must have saved the cursor line for undo, following ones will be
5503 * saved here.
5504 */
5505 void
5506auto_format(trailblank, prev_line)
5507 int trailblank; /* when TRUE also format with trailing blank */
5508 int prev_line; /* may start in previous line */
5509{
5510 pos_T pos;
5511 colnr_T len;
5512 char_u *old;
5513 char_u *new, *pnew;
5514 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005515 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
5517 if (!has_format_option(FO_AUTO))
5518 return;
5519
5520 pos = curwin->w_cursor;
5521 old = ml_get_curline();
5522
5523 /* may remove added space */
5524 check_auto_format(FALSE);
5525
5526 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5527 * user might insert normal text next. Also skip formatting when "1" is
5528 * in 'formatoptions' and there is a single character before the cursor.
5529 * Otherwise the line would be broken and when typing another non-white
5530 * next they are not joined back together. */
5531 wasatend = (pos.col == STRLEN(old));
5532 if (*old != NUL && !trailblank && wasatend)
5533 {
5534 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005535 cc = gchar_cursor();
5536 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5537 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005539 cc = gchar_cursor();
5540 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 {
5542 curwin->w_cursor = pos;
5543 return;
5544 }
5545 curwin->w_cursor = pos;
5546 }
5547
5548#ifdef FEAT_COMMENTS
5549 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5550 * comments. */
5551 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5552 && get_leader_len(old, NULL, FALSE) == 0)
5553 return;
5554#endif
5555
5556 /*
5557 * May start formatting in a previous line, so that after "x" a word is
5558 * moved to the previous line if it fits there now. Only when this is not
5559 * the start of a paragraph.
5560 */
5561 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5562 {
5563 --curwin->w_cursor.lnum;
5564 if (u_save_cursor() == FAIL)
5565 return;
5566 }
5567
5568 /*
5569 * Do the formatting and restore the cursor position. "saved_cursor" will
5570 * be adjusted for the text formatting.
5571 */
5572 saved_cursor = pos;
5573 format_lines((linenr_T)-1);
5574 curwin->w_cursor = saved_cursor;
5575 saved_cursor.lnum = 0;
5576
5577 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5578 {
5579 /* "cannot happen" */
5580 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5581 coladvance((colnr_T)MAXCOL);
5582 }
5583 else
5584 check_cursor_col();
5585
5586 /* Insert mode: If the cursor is now after the end of the line while it
5587 * previously wasn't, the line was broken. Because of the rule above we
5588 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5589 * formatted. */
5590 if (!wasatend && has_format_option(FO_WHITE_PAR))
5591 {
5592 new = ml_get_curline();
5593 len = STRLEN(new);
5594 if (curwin->w_cursor.col == len)
5595 {
5596 pnew = vim_strnsave(new, len + 2);
5597 pnew[len] = ' ';
5598 pnew[len + 1] = NUL;
5599 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5600 /* remove the space later */
5601 did_add_space = TRUE;
5602 }
5603 else
5604 /* may remove added space */
5605 check_auto_format(FALSE);
5606 }
5607
5608 check_cursor();
5609}
5610
5611/*
5612 * When an extra space was added to continue a paragraph for auto-formatting,
5613 * delete it now. The space must be under the cursor, just after the insert
5614 * position.
5615 */
5616 static void
5617check_auto_format(end_insert)
5618 int end_insert; /* TRUE when ending Insert mode */
5619{
5620 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005621 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622
5623 if (did_add_space)
5624 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005625 cc = gchar_cursor();
5626 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 /* Somehow the space was removed already. */
5628 did_add_space = FALSE;
5629 else
5630 {
5631 if (!end_insert)
5632 {
5633 inc_cursor();
5634 c = gchar_cursor();
5635 dec_cursor();
5636 }
5637 if (c != NUL)
5638 {
5639 /* The space is no longer at the end of the line, delete it. */
5640 del_char(FALSE);
5641 did_add_space = FALSE;
5642 }
5643 }
5644 }
5645}
5646
5647/*
5648 * Find out textwidth to be used for formatting:
5649 * if 'textwidth' option is set, use it
5650 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5651 * if invalid value, use 0.
5652 * Set default to window width (maximum 79) for "gq" operator.
5653 */
5654 int
5655comp_textwidth(ff)
5656 int ff; /* force formatting (for "Q" command) */
5657{
5658 int textwidth;
5659
5660 textwidth = curbuf->b_p_tw;
5661 if (textwidth == 0 && curbuf->b_p_wm)
5662 {
5663 /* The width is the window width minus 'wrapmargin' minus all the
5664 * things that add to the margin. */
5665 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5666#ifdef FEAT_CMDWIN
5667 if (cmdwin_type != 0)
5668 textwidth -= 1;
5669#endif
5670#ifdef FEAT_FOLDING
5671 textwidth -= curwin->w_p_fdc;
5672#endif
5673#ifdef FEAT_SIGNS
5674 if (curwin->w_buffer->b_signlist != NULL
5675# ifdef FEAT_NETBEANS_INTG
5676 || usingNetbeans
5677# endif
5678 )
5679 textwidth -= 1;
5680#endif
5681 if (curwin->w_p_nu)
5682 textwidth -= 8;
5683 }
5684 if (textwidth < 0)
5685 textwidth = 0;
5686 if (ff && textwidth == 0)
5687 {
5688 textwidth = W_WIDTH(curwin) - 1;
5689 if (textwidth > 79)
5690 textwidth = 79;
5691 }
5692 return textwidth;
5693}
5694
5695/*
5696 * Put a character in the redo buffer, for when just after a CTRL-V.
5697 */
5698 static void
5699redo_literal(c)
5700 int c;
5701{
5702 char_u buf[10];
5703
5704 /* Only digits need special treatment. Translate them into a string of
5705 * three digits. */
5706 if (VIM_ISDIGIT(c))
5707 {
5708 sprintf((char *)buf, "%03d", c);
5709 AppendToRedobuff(buf);
5710 }
5711 else
5712 AppendCharToRedobuff(c);
5713}
5714
5715/*
5716 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005717 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 */
5719 static void
5720start_arrow(end_insert_pos)
5721 pos_T *end_insert_pos;
5722{
5723 if (!arrow_used) /* something has been inserted */
5724 {
5725 AppendToRedobuff(ESC_STR);
5726 stop_insert(end_insert_pos, FALSE);
5727 arrow_used = TRUE; /* this means we stopped the current insert */
5728 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005729#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005730 check_spell_redraw();
5731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732}
5733
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005734#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005735/*
5736 * If we skipped highlighting word at cursor, do it now.
5737 * It may be skipped again, thus reset spell_redraw_lnum first.
5738 */
5739 static void
5740check_spell_redraw()
5741{
5742 if (spell_redraw_lnum != 0)
5743 {
5744 linenr_T lnum = spell_redraw_lnum;
5745
5746 spell_redraw_lnum = 0;
5747 redrawWinline(lnum, FALSE);
5748 }
5749}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005750
5751/*
5752 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5753 * spelled word, if there is one.
5754 */
5755 static void
5756spell_back_to_badword()
5757{
5758 pos_T tpos = curwin->w_cursor;
5759
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005760 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005761 if (curwin->w_cursor.col != tpos.col)
5762 start_arrow(&tpos);
5763}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005764#endif
5765
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766/*
5767 * stop_arrow() is called before a change is made in insert mode.
5768 * If an arrow key has been used, start a new insertion.
5769 * Returns FAIL if undo is impossible, shouldn't insert then.
5770 */
5771 int
5772stop_arrow()
5773{
5774 if (arrow_used)
5775 {
5776 if (u_save_cursor() == OK)
5777 {
5778 arrow_used = FALSE;
5779 ins_need_undo = FALSE;
5780 }
5781 Insstart = curwin->w_cursor; /* new insertion starts here */
5782 Insstart_textlen = linetabsize(ml_get_curline());
5783 ai_col = 0;
5784#ifdef FEAT_VREPLACE
5785 if (State & VREPLACE_FLAG)
5786 {
5787 orig_line_count = curbuf->b_ml.ml_line_count;
5788 vr_lines_changed = 1;
5789 }
5790#endif
5791 ResetRedobuff();
5792 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005793 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 }
5795 else if (ins_need_undo)
5796 {
5797 if (u_save_cursor() == OK)
5798 ins_need_undo = FALSE;
5799 }
5800
5801#ifdef FEAT_FOLDING
5802 /* Always open fold at the cursor line when inserting something. */
5803 foldOpenCursor();
5804#endif
5805
5806 return (arrow_used || ins_need_undo ? FAIL : OK);
5807}
5808
5809/*
5810 * do a few things to stop inserting
5811 */
5812 static void
5813stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005814 pos_T *end_insert_pos; /* where insert ended */
5815 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005817 int cc;
5818 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819
5820 stop_redo_ins();
5821 replace_flush(); /* abandon replace stack */
5822
5823 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005824 * Save the inserted text for later redo with ^@ and CTRL-A.
5825 * Don't do it when "restart_edit" was set and nothing was inserted,
5826 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005828 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005829 if (did_restart_edit == 0 || (ptr != NULL
5830 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005831 {
5832 vim_free(last_insert);
5833 last_insert = ptr;
5834 last_insert_skip = new_insert_skip;
5835 }
5836 else
5837 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
5839 if (!arrow_used)
5840 {
5841 /* Auto-format now. It may seem strange to do this when stopping an
5842 * insertion (or moving the cursor), but it's required when appending
5843 * a line and having it end in a space. But only do it when something
5844 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005845 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005847 pos_T tpos = curwin->w_cursor;
5848
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 /* When the cursor is at the end of the line after a space the
5850 * formatting will move it to the following word. Avoid that by
5851 * moving the cursor onto the space. */
5852 cc = 'x';
5853 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5854 {
5855 dec_cursor();
5856 cc = gchar_cursor();
5857 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005858 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 }
5860
5861 auto_format(TRUE, FALSE);
5862
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005863 if (vim_iswhite(cc))
5864 {
5865 if (gchar_cursor() != NUL)
5866 inc_cursor();
5867#ifdef FEAT_VIRTUALEDIT
5868 /* If the cursor is still at the same character, also keep
5869 * the "coladd". */
5870 if (gchar_cursor() == NUL
5871 && curwin->w_cursor.lnum == tpos.lnum
5872 && curwin->w_cursor.col == tpos.col)
5873 curwin->w_cursor.coladd = tpos.coladd;
5874#endif
5875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 }
5877
5878 /* If a space was inserted for auto-formatting, remove it now. */
5879 check_auto_format(TRUE);
5880
5881 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005882 * of the line, and put the cursor back.
5883 * Do this when ESC was used or moving the cursor up/down. */
5884 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5885 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005887 pos_T tpos = curwin->w_cursor;
5888
5889 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5891 --curwin->w_cursor.col;
5892 while (cc = gchar_cursor(), vim_iswhite(cc))
5893 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005894 if (curwin->w_cursor.lnum != tpos.lnum)
5895 curwin->w_cursor = tpos;
5896 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5898
5899#ifdef FEAT_VISUAL
5900 /* <C-S-Right> may have started Visual mode, adjust the position for
5901 * deleted characters. */
5902 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5903 {
5904 cc = STRLEN(ml_get_curline());
5905 if (VIsual.col > (colnr_T)cc)
5906 {
5907 VIsual.col = cc;
5908# ifdef FEAT_VIRTUALEDIT
5909 VIsual.coladd = 0;
5910# endif
5911 }
5912 }
5913#endif
5914 }
5915 }
5916 did_ai = FALSE;
5917#ifdef FEAT_SMARTINDENT
5918 did_si = FALSE;
5919 can_si = FALSE;
5920 can_si_back = FALSE;
5921#endif
5922
5923 /* set '[ and '] to the inserted text */
5924 curbuf->b_op_start = Insstart;
5925 curbuf->b_op_end = *end_insert_pos;
5926}
5927
5928/*
5929 * Set the last inserted text to a single character.
5930 * Used for the replace command.
5931 */
5932 void
5933set_last_insert(c)
5934 int c;
5935{
5936 char_u *s;
5937
5938 vim_free(last_insert);
5939#ifdef FEAT_MBYTE
5940 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5941#else
5942 last_insert = alloc(6);
5943#endif
5944 if (last_insert != NULL)
5945 {
5946 s = last_insert;
5947 /* Use the CTRL-V only when entering a special char */
5948 if (c < ' ' || c == DEL)
5949 *s++ = Ctrl_V;
5950 s = add_char2buf(c, s);
5951 *s++ = ESC;
5952 *s++ = NUL;
5953 last_insert_skip = 0;
5954 }
5955}
5956
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005957#if defined(EXITFREE) || defined(PROTO)
5958 void
5959free_last_insert()
5960{
5961 vim_free(last_insert);
5962 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005963 vim_free(compl_orig_text);
5964 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005965}
5966#endif
5967
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968/*
5969 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5970 * and CSI. Handle multi-byte characters.
5971 * Returns a pointer to after the added bytes.
5972 */
5973 char_u *
5974add_char2buf(c, s)
5975 int c;
5976 char_u *s;
5977{
5978#ifdef FEAT_MBYTE
5979 char_u temp[MB_MAXBYTES];
5980 int i;
5981 int len;
5982
5983 len = (*mb_char2bytes)(c, temp);
5984 for (i = 0; i < len; ++i)
5985 {
5986 c = temp[i];
5987#endif
5988 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5989 if (c == K_SPECIAL)
5990 {
5991 *s++ = K_SPECIAL;
5992 *s++ = KS_SPECIAL;
5993 *s++ = KE_FILLER;
5994 }
5995#ifdef FEAT_GUI
5996 else if (c == CSI)
5997 {
5998 *s++ = CSI;
5999 *s++ = KS_EXTRA;
6000 *s++ = (int)KE_CSI;
6001 }
6002#endif
6003 else
6004 *s++ = c;
6005#ifdef FEAT_MBYTE
6006 }
6007#endif
6008 return s;
6009}
6010
6011/*
6012 * move cursor to start of line
6013 * if flags & BL_WHITE move to first non-white
6014 * if flags & BL_SOL move to first non-white if startofline is set,
6015 * otherwise keep "curswant" column
6016 * if flags & BL_FIX don't leave the cursor on a NUL.
6017 */
6018 void
6019beginline(flags)
6020 int flags;
6021{
6022 if ((flags & BL_SOL) && !p_sol)
6023 coladvance(curwin->w_curswant);
6024 else
6025 {
6026 curwin->w_cursor.col = 0;
6027#ifdef FEAT_VIRTUALEDIT
6028 curwin->w_cursor.coladd = 0;
6029#endif
6030
6031 if (flags & (BL_WHITE | BL_SOL))
6032 {
6033 char_u *ptr;
6034
6035 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6036 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6037 ++curwin->w_cursor.col;
6038 }
6039 curwin->w_set_curswant = TRUE;
6040 }
6041}
6042
6043/*
6044 * oneright oneleft cursor_down cursor_up
6045 *
6046 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006047 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 * Return OK when successful, FAIL when we hit a line of file boundary.
6049 */
6050
6051 int
6052oneright()
6053{
6054 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056
6057#ifdef FEAT_VIRTUALEDIT
6058 if (virtual_active())
6059 {
6060 pos_T prevpos = curwin->w_cursor;
6061
6062 /* Adjust for multi-wide char (excluding TAB) */
6063 ptr = ml_get_cursor();
6064 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006065# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006067# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 ))
6071 ? ptr2cells(ptr) : 1));
6072 curwin->w_set_curswant = TRUE;
6073 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6074 return (prevpos.col != curwin->w_cursor.col
6075 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6076 }
6077#endif
6078
6079 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006080 if (*ptr == NUL)
6081 return FAIL; /* already at the very end */
6082
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006084 if (has_mbyte)
6085 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 else
6087#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006088 l = 1;
6089
6090 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6091 * contains "onemore". */
6092 if (ptr[l] == NUL
6093#ifdef FEAT_VIRTUALEDIT
6094 && (ve_flags & VE_ONEMORE) == 0
6095#endif
6096 )
6097 return FAIL;
6098 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099
6100 curwin->w_set_curswant = TRUE;
6101 return OK;
6102}
6103
6104 int
6105oneleft()
6106{
6107#ifdef FEAT_VIRTUALEDIT
6108 if (virtual_active())
6109 {
6110 int width;
6111 int v = getviscol();
6112
6113 if (v == 0)
6114 return FAIL;
6115
6116# ifdef FEAT_LINEBREAK
6117 /* We might get stuck on 'showbreak', skip over it. */
6118 width = 1;
6119 for (;;)
6120 {
6121 coladvance(v - width);
6122 /* getviscol() is slow, skip it when 'showbreak' is empty and
6123 * there are no multi-byte characters */
6124 if ((*p_sbr == NUL
6125# ifdef FEAT_MBYTE
6126 && !has_mbyte
6127# endif
6128 ) || getviscol() < v)
6129 break;
6130 ++width;
6131 }
6132# else
6133 coladvance(v - 1);
6134# endif
6135
6136 if (curwin->w_cursor.coladd == 1)
6137 {
6138 char_u *ptr;
6139
6140 /* Adjust for multi-wide char (not a TAB) */
6141 ptr = ml_get_cursor();
6142 if (*ptr != TAB && vim_isprintc(
6143# ifdef FEAT_MBYTE
6144 (*mb_ptr2char)(ptr)
6145# else
6146 *ptr
6147# endif
6148 ) && ptr2cells(ptr) > 1)
6149 curwin->w_cursor.coladd = 0;
6150 }
6151
6152 curwin->w_set_curswant = TRUE;
6153 return OK;
6154 }
6155#endif
6156
6157 if (curwin->w_cursor.col == 0)
6158 return FAIL;
6159
6160 curwin->w_set_curswant = TRUE;
6161 --curwin->w_cursor.col;
6162
6163#ifdef FEAT_MBYTE
6164 /* if the character on the left of the current cursor is a multi-byte
6165 * character, move to its first byte */
6166 if (has_mbyte)
6167 mb_adjust_cursor();
6168#endif
6169 return OK;
6170}
6171
6172 int
6173cursor_up(n, upd_topline)
6174 long n;
6175 int upd_topline; /* When TRUE: update topline */
6176{
6177 linenr_T lnum;
6178
6179 if (n > 0)
6180 {
6181 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006182 /* This fails if the cursor is already in the first line or the count
6183 * is larger than the line number and '-' is in 'cpoptions' */
6184 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 return FAIL;
6186 if (n >= lnum)
6187 lnum = 1;
6188 else
6189#ifdef FEAT_FOLDING
6190 if (hasAnyFolding(curwin))
6191 {
6192 /*
6193 * Count each sequence of folded lines as one logical line.
6194 */
6195 /* go to the the start of the current fold */
6196 (void)hasFolding(lnum, &lnum, NULL);
6197
6198 while (n--)
6199 {
6200 /* move up one line */
6201 --lnum;
6202 if (lnum <= 1)
6203 break;
6204 /* If we entered a fold, move to the beginning, unless in
6205 * Insert mode or when 'foldopen' contains "all": it will open
6206 * in a moment. */
6207 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6208 (void)hasFolding(lnum, &lnum, NULL);
6209 }
6210 if (lnum < 1)
6211 lnum = 1;
6212 }
6213 else
6214#endif
6215 lnum -= n;
6216 curwin->w_cursor.lnum = lnum;
6217 }
6218
6219 /* try to advance to the column we want to be at */
6220 coladvance(curwin->w_curswant);
6221
6222 if (upd_topline)
6223 update_topline(); /* make sure curwin->w_topline is valid */
6224
6225 return OK;
6226}
6227
6228/*
6229 * Cursor down a number of logical lines.
6230 */
6231 int
6232cursor_down(n, upd_topline)
6233 long n;
6234 int upd_topline; /* When TRUE: update topline */
6235{
6236 linenr_T lnum;
6237
6238 if (n > 0)
6239 {
6240 lnum = curwin->w_cursor.lnum;
6241#ifdef FEAT_FOLDING
6242 /* Move to last line of fold, will fail if it's the end-of-file. */
6243 (void)hasFolding(lnum, NULL, &lnum);
6244#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006245 /* This fails if the cursor is already in the last line or would move
6246 * beyound the last line and '-' is in 'cpoptions' */
6247 if (lnum >= curbuf->b_ml.ml_line_count
6248 || (lnum + n > curbuf->b_ml.ml_line_count
6249 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 return FAIL;
6251 if (lnum + n >= curbuf->b_ml.ml_line_count)
6252 lnum = curbuf->b_ml.ml_line_count;
6253 else
6254#ifdef FEAT_FOLDING
6255 if (hasAnyFolding(curwin))
6256 {
6257 linenr_T last;
6258
6259 /* count each sequence of folded lines as one logical line */
6260 while (n--)
6261 {
6262 if (hasFolding(lnum, NULL, &last))
6263 lnum = last + 1;
6264 else
6265 ++lnum;
6266 if (lnum >= curbuf->b_ml.ml_line_count)
6267 break;
6268 }
6269 if (lnum > curbuf->b_ml.ml_line_count)
6270 lnum = curbuf->b_ml.ml_line_count;
6271 }
6272 else
6273#endif
6274 lnum += n;
6275 curwin->w_cursor.lnum = lnum;
6276 }
6277
6278 /* try to advance to the column we want to be at */
6279 coladvance(curwin->w_curswant);
6280
6281 if (upd_topline)
6282 update_topline(); /* make sure curwin->w_topline is valid */
6283
6284 return OK;
6285}
6286
6287/*
6288 * Stuff the last inserted text in the read buffer.
6289 * Last_insert actually is a copy of the redo buffer, so we
6290 * first have to remove the command.
6291 */
6292 int
6293stuff_inserted(c, count, no_esc)
6294 int c; /* Command character to be inserted */
6295 long count; /* Repeat this many times */
6296 int no_esc; /* Don't add an ESC at the end */
6297{
6298 char_u *esc_ptr;
6299 char_u *ptr;
6300 char_u *last_ptr;
6301 char_u last = NUL;
6302
6303 ptr = get_last_insert();
6304 if (ptr == NULL)
6305 {
6306 EMSG(_(e_noinstext));
6307 return FAIL;
6308 }
6309
6310 /* may want to stuff the command character, to start Insert mode */
6311 if (c != NUL)
6312 stuffcharReadbuff(c);
6313 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6314 *esc_ptr = NUL; /* remove the ESC */
6315
6316 /* when the last char is either "0" or "^" it will be quoted if no ESC
6317 * comes after it OR if it will inserted more than once and "ptr"
6318 * starts with ^D. -- Acevedo
6319 */
6320 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6321 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6322 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6323 {
6324 last = *last_ptr;
6325 *last_ptr = NUL;
6326 }
6327
6328 do
6329 {
6330 stuffReadbuff(ptr);
6331 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6332 if (last)
6333 stuffReadbuff((char_u *)(last == '0'
6334 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6335 : IF_EB("\026^", CTRL_V_STR "^")));
6336 }
6337 while (--count > 0);
6338
6339 if (last)
6340 *last_ptr = last;
6341
6342 if (esc_ptr != NULL)
6343 *esc_ptr = ESC; /* put the ESC back */
6344
6345 /* may want to stuff a trailing ESC, to get out of Insert mode */
6346 if (!no_esc)
6347 stuffcharReadbuff(ESC);
6348
6349 return OK;
6350}
6351
6352 char_u *
6353get_last_insert()
6354{
6355 if (last_insert == NULL)
6356 return NULL;
6357 return last_insert + last_insert_skip;
6358}
6359
6360/*
6361 * Get last inserted string, and remove trailing <Esc>.
6362 * Returns pointer to allocated memory (must be freed) or NULL.
6363 */
6364 char_u *
6365get_last_insert_save()
6366{
6367 char_u *s;
6368 int len;
6369
6370 if (last_insert == NULL)
6371 return NULL;
6372 s = vim_strsave(last_insert + last_insert_skip);
6373 if (s != NULL)
6374 {
6375 len = (int)STRLEN(s);
6376 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6377 s[len - 1] = NUL;
6378 }
6379 return s;
6380}
6381
6382/*
6383 * Check the word in front of the cursor for an abbreviation.
6384 * Called when the non-id character "c" has been entered.
6385 * When an abbreviation is recognized it is removed from the text and
6386 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6387 */
6388 static int
6389echeck_abbr(c)
6390 int c;
6391{
6392 /* Don't check for abbreviation in paste mode, when disabled and just
6393 * after moving around with cursor keys. */
6394 if (p_paste || no_abbr || arrow_used)
6395 return FALSE;
6396
6397 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6398 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6399}
6400
6401/*
6402 * replace-stack functions
6403 *
6404 * When replacing characters, the replaced characters are remembered for each
6405 * new character. This is used to re-insert the old text when backspacing.
6406 *
6407 * There is a NUL headed list of characters for each character that is
6408 * currently in the file after the insertion point. When BS is used, one NUL
6409 * headed list is put back for the deleted character.
6410 *
6411 * For a newline, there are two NUL headed lists. One contains the characters
6412 * that the NL replaced. The extra one stores the characters after the cursor
6413 * that were deleted (always white space).
6414 *
6415 * Replace_offset is normally 0, in which case replace_push will add a new
6416 * character at the end of the stack. If replace_offset is not 0, that many
6417 * characters will be left on the stack above the newly inserted character.
6418 */
6419
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006420static char_u *replace_stack = NULL;
6421static long replace_stack_nr = 0; /* next entry in replace stack */
6422static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423
6424 void
6425replace_push(c)
6426 int c; /* character that is replaced (NUL is none) */
6427{
6428 char_u *p;
6429
6430 if (replace_stack_nr < replace_offset) /* nothing to do */
6431 return;
6432 if (replace_stack_len <= replace_stack_nr)
6433 {
6434 replace_stack_len += 50;
6435 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6436 if (p == NULL) /* out of memory */
6437 {
6438 replace_stack_len -= 50;
6439 return;
6440 }
6441 if (replace_stack != NULL)
6442 {
6443 mch_memmove(p, replace_stack,
6444 (size_t)(replace_stack_nr * sizeof(char_u)));
6445 vim_free(replace_stack);
6446 }
6447 replace_stack = p;
6448 }
6449 p = replace_stack + replace_stack_nr - replace_offset;
6450 if (replace_offset)
6451 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6452 *p = c;
6453 ++replace_stack_nr;
6454}
6455
6456/*
6457 * call replace_push(c) with replace_offset set to the first NUL.
6458 */
6459 static void
6460replace_push_off(c)
6461 int c;
6462{
6463 char_u *p;
6464
6465 p = replace_stack + replace_stack_nr;
6466 for (replace_offset = 1; replace_offset < replace_stack_nr;
6467 ++replace_offset)
6468 if (*--p == NUL)
6469 break;
6470 replace_push(c);
6471 replace_offset = 0;
6472}
6473
6474/*
6475 * Pop one item from the replace stack.
6476 * return -1 if stack empty
6477 * return replaced character or NUL otherwise
6478 */
6479 static int
6480replace_pop()
6481{
6482 if (replace_stack_nr == 0)
6483 return -1;
6484 return (int)replace_stack[--replace_stack_nr];
6485}
6486
6487/*
6488 * Join the top two items on the replace stack. This removes to "off"'th NUL
6489 * encountered.
6490 */
6491 static void
6492replace_join(off)
6493 int off; /* offset for which NUL to remove */
6494{
6495 int i;
6496
6497 for (i = replace_stack_nr; --i >= 0; )
6498 if (replace_stack[i] == NUL && off-- <= 0)
6499 {
6500 --replace_stack_nr;
6501 mch_memmove(replace_stack + i, replace_stack + i + 1,
6502 (size_t)(replace_stack_nr - i));
6503 return;
6504 }
6505}
6506
6507/*
6508 * Pop bytes from the replace stack until a NUL is found, and insert them
6509 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6510 */
6511 static void
6512replace_pop_ins()
6513{
6514 int cc;
6515 int oldState = State;
6516
6517 State = NORMAL; /* don't want REPLACE here */
6518 while ((cc = replace_pop()) > 0)
6519 {
6520#ifdef FEAT_MBYTE
6521 mb_replace_pop_ins(cc);
6522#else
6523 ins_char(cc);
6524#endif
6525 dec_cursor();
6526 }
6527 State = oldState;
6528}
6529
6530#ifdef FEAT_MBYTE
6531/*
6532 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6533 * indicates a multi-byte char, pop the other bytes too.
6534 */
6535 static void
6536mb_replace_pop_ins(cc)
6537 int cc;
6538{
6539 int n;
6540 char_u buf[MB_MAXBYTES];
6541 int i;
6542 int c;
6543
6544 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6545 {
6546 buf[0] = cc;
6547 for (i = 1; i < n; ++i)
6548 buf[i] = replace_pop();
6549 ins_bytes_len(buf, n);
6550 }
6551 else
6552 ins_char(cc);
6553
6554 if (enc_utf8)
6555 /* Handle composing chars. */
6556 for (;;)
6557 {
6558 c = replace_pop();
6559 if (c == -1) /* stack empty */
6560 break;
6561 if ((n = MB_BYTE2LEN(c)) == 1)
6562 {
6563 /* Not a multi-byte char, put it back. */
6564 replace_push(c);
6565 break;
6566 }
6567 else
6568 {
6569 buf[0] = c;
6570 for (i = 1; i < n; ++i)
6571 buf[i] = replace_pop();
6572 if (utf_iscomposing(utf_ptr2char(buf)))
6573 ins_bytes_len(buf, n);
6574 else
6575 {
6576 /* Not a composing char, put it back. */
6577 for (i = n - 1; i >= 0; --i)
6578 replace_push(buf[i]);
6579 break;
6580 }
6581 }
6582 }
6583}
6584#endif
6585
6586/*
6587 * make the replace stack empty
6588 * (called when exiting replace mode)
6589 */
6590 static void
6591replace_flush()
6592{
6593 vim_free(replace_stack);
6594 replace_stack = NULL;
6595 replace_stack_len = 0;
6596 replace_stack_nr = 0;
6597}
6598
6599/*
6600 * Handle doing a BS for one character.
6601 * cc < 0: replace stack empty, just move cursor
6602 * cc == 0: character was inserted, delete it
6603 * cc > 0: character was replaced, put cc (first byte of original char) back
6604 * and check for more characters to be put back
6605 */
6606 static void
6607replace_do_bs()
6608{
6609 int cc;
6610#ifdef FEAT_VREPLACE
6611 int orig_len = 0;
6612 int ins_len;
6613 int orig_vcols = 0;
6614 colnr_T start_vcol;
6615 char_u *p;
6616 int i;
6617 int vcol;
6618#endif
6619
6620 cc = replace_pop();
6621 if (cc > 0)
6622 {
6623#ifdef FEAT_VREPLACE
6624 if (State & VREPLACE_FLAG)
6625 {
6626 /* Get the number of screen cells used by the character we are
6627 * going to delete. */
6628 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6629 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6630 }
6631#endif
6632#ifdef FEAT_MBYTE
6633 if (has_mbyte)
6634 {
6635 del_char(FALSE);
6636# ifdef FEAT_VREPLACE
6637 if (State & VREPLACE_FLAG)
6638 orig_len = STRLEN(ml_get_cursor());
6639# endif
6640 replace_push(cc);
6641 }
6642 else
6643#endif
6644 {
6645 pchar_cursor(cc);
6646#ifdef FEAT_VREPLACE
6647 if (State & VREPLACE_FLAG)
6648 orig_len = STRLEN(ml_get_cursor()) - 1;
6649#endif
6650 }
6651 replace_pop_ins();
6652
6653#ifdef FEAT_VREPLACE
6654 if (State & VREPLACE_FLAG)
6655 {
6656 /* Get the number of screen cells used by the inserted characters */
6657 p = ml_get_cursor();
6658 ins_len = STRLEN(p) - orig_len;
6659 vcol = start_vcol;
6660 for (i = 0; i < ins_len; ++i)
6661 {
6662 vcol += chartabsize(p + i, vcol);
6663#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006664 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665#endif
6666 }
6667 vcol -= start_vcol;
6668
6669 /* Delete spaces that were inserted after the cursor to keep the
6670 * text aligned. */
6671 curwin->w_cursor.col += ins_len;
6672 while (vcol > orig_vcols && gchar_cursor() == ' ')
6673 {
6674 del_char(FALSE);
6675 ++orig_vcols;
6676 }
6677 curwin->w_cursor.col -= ins_len;
6678 }
6679#endif
6680
6681 /* mark the buffer as changed and prepare for displaying */
6682 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6683 }
6684 else if (cc == 0)
6685 (void)del_char(FALSE);
6686}
6687
6688#ifdef FEAT_CINDENT
6689/*
6690 * Return TRUE if C-indenting is on.
6691 */
6692 static int
6693cindent_on()
6694{
6695 return (!p_paste && (curbuf->b_p_cin
6696# ifdef FEAT_EVAL
6697 || *curbuf->b_p_inde != NUL
6698# endif
6699 ));
6700}
6701#endif
6702
6703#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6704/*
6705 * Re-indent the current line, based on the current contents of it and the
6706 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6707 * confused what all the part that handles Control-T is doing that I'm not.
6708 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6709 */
6710
6711 void
6712fixthisline(get_the_indent)
6713 int (*get_the_indent) __ARGS((void));
6714{
6715 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6716 if (linewhite(curwin->w_cursor.lnum))
6717 did_ai = TRUE; /* delete the indent if the line stays empty */
6718}
6719
6720 void
6721fix_indent()
6722{
6723 if (p_paste)
6724 return;
6725# ifdef FEAT_LISP
6726 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6727 fixthisline(get_lisp_indent);
6728# endif
6729# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6730 else
6731# endif
6732# ifdef FEAT_CINDENT
6733 if (cindent_on())
6734 do_c_expr_indent();
6735# endif
6736}
6737
6738#endif
6739
6740#ifdef FEAT_CINDENT
6741/*
6742 * return TRUE if 'cinkeys' contains the key "keytyped",
6743 * when == '*': Only if key is preceded with '*' (indent before insert)
6744 * when == '!': Only if key is prededed with '!' (don't insert)
6745 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6746 *
6747 * "keytyped" can have a few special values:
6748 * KEY_OPEN_FORW
6749 * KEY_OPEN_BACK
6750 * KEY_COMPLETE just finished completion.
6751 *
6752 * If line_is_empty is TRUE accept keys with '0' before them.
6753 */
6754 int
6755in_cinkeys(keytyped, when, line_is_empty)
6756 int keytyped;
6757 int when;
6758 int line_is_empty;
6759{
6760 char_u *look;
6761 int try_match;
6762 int try_match_word;
6763 char_u *p;
6764 char_u *line;
6765 int icase;
6766 int i;
6767
6768#ifdef FEAT_EVAL
6769 if (*curbuf->b_p_inde != NUL)
6770 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6771 else
6772#endif
6773 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6774 while (*look)
6775 {
6776 /*
6777 * Find out if we want to try a match with this key, depending on
6778 * 'when' and a '*' or '!' before the key.
6779 */
6780 switch (when)
6781 {
6782 case '*': try_match = (*look == '*'); break;
6783 case '!': try_match = (*look == '!'); break;
6784 default: try_match = (*look != '*'); break;
6785 }
6786 if (*look == '*' || *look == '!')
6787 ++look;
6788
6789 /*
6790 * If there is a '0', only accept a match if the line is empty.
6791 * But may still match when typing last char of a word.
6792 */
6793 if (*look == '0')
6794 {
6795 try_match_word = try_match;
6796 if (!line_is_empty)
6797 try_match = FALSE;
6798 ++look;
6799 }
6800 else
6801 try_match_word = FALSE;
6802
6803 /*
6804 * does it look like a control character?
6805 */
6806 if (*look == '^'
6807#ifdef EBCDIC
6808 && (Ctrl_chr(look[1]) != 0)
6809#else
6810 && look[1] >= '?' && look[1] <= '_'
6811#endif
6812 )
6813 {
6814 if (try_match && keytyped == Ctrl_chr(look[1]))
6815 return TRUE;
6816 look += 2;
6817 }
6818 /*
6819 * 'o' means "o" command, open forward.
6820 * 'O' means "O" command, open backward.
6821 */
6822 else if (*look == 'o')
6823 {
6824 if (try_match && keytyped == KEY_OPEN_FORW)
6825 return TRUE;
6826 ++look;
6827 }
6828 else if (*look == 'O')
6829 {
6830 if (try_match && keytyped == KEY_OPEN_BACK)
6831 return TRUE;
6832 ++look;
6833 }
6834
6835 /*
6836 * 'e' means to check for "else" at start of line and just before the
6837 * cursor.
6838 */
6839 else if (*look == 'e')
6840 {
6841 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6842 {
6843 p = ml_get_curline();
6844 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6845 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6846 return TRUE;
6847 }
6848 ++look;
6849 }
6850
6851 /*
6852 * ':' only causes an indent if it is at the end of a label or case
6853 * statement, or when it was before typing the ':' (to fix
6854 * class::method for C++).
6855 */
6856 else if (*look == ':')
6857 {
6858 if (try_match && keytyped == ':')
6859 {
6860 p = ml_get_curline();
6861 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6862 return TRUE;
6863 if (curwin->w_cursor.col > 2
6864 && p[curwin->w_cursor.col - 1] == ':'
6865 && p[curwin->w_cursor.col - 2] == ':')
6866 {
6867 p[curwin->w_cursor.col - 1] = ' ';
6868 i = (cin_iscase(p) || cin_isscopedecl(p)
6869 || cin_islabel(30));
6870 p = ml_get_curline();
6871 p[curwin->w_cursor.col - 1] = ':';
6872 if (i)
6873 return TRUE;
6874 }
6875 }
6876 ++look;
6877 }
6878
6879
6880 /*
6881 * Is it a key in <>, maybe?
6882 */
6883 else if (*look == '<')
6884 {
6885 if (try_match)
6886 {
6887 /*
6888 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6889 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6890 * >, *, : and ! keys if they really really want to.
6891 */
6892 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6893 && keytyped == look[1])
6894 return TRUE;
6895
6896 if (keytyped == get_special_key_code(look + 1))
6897 return TRUE;
6898 }
6899 while (*look && *look != '>')
6900 look++;
6901 while (*look == '>')
6902 look++;
6903 }
6904
6905 /*
6906 * Is it a word: "=word"?
6907 */
6908 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6909 {
6910 ++look;
6911 if (*look == '~')
6912 {
6913 icase = TRUE;
6914 ++look;
6915 }
6916 else
6917 icase = FALSE;
6918 p = vim_strchr(look, ',');
6919 if (p == NULL)
6920 p = look + STRLEN(look);
6921 if ((try_match || try_match_word)
6922 && curwin->w_cursor.col >= (colnr_T)(p - look))
6923 {
6924 int match = FALSE;
6925
6926#ifdef FEAT_INS_EXPAND
6927 if (keytyped == KEY_COMPLETE)
6928 {
6929 char_u *s;
6930
6931 /* Just completed a word, check if it starts with "look".
6932 * search back for the start of a word. */
6933 line = ml_get_curline();
6934# ifdef FEAT_MBYTE
6935 if (has_mbyte)
6936 {
6937 char_u *n;
6938
6939 for (s = line + curwin->w_cursor.col; s > line; s = n)
6940 {
6941 n = mb_prevptr(line, s);
6942 if (!vim_iswordp(n))
6943 break;
6944 }
6945 }
6946 else
6947# endif
6948 for (s = line + curwin->w_cursor.col; s > line; --s)
6949 if (!vim_iswordc(s[-1]))
6950 break;
6951 if (s + (p - look) <= line + curwin->w_cursor.col
6952 && (icase
6953 ? MB_STRNICMP(s, look, p - look)
6954 : STRNCMP(s, look, p - look)) == 0)
6955 match = TRUE;
6956 }
6957 else
6958#endif
6959 /* TODO: multi-byte */
6960 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6961 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6962 {
6963 line = ml_get_cursor();
6964 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6965 || !vim_iswordc(line[-(p - look) - 1]))
6966 && (icase
6967 ? MB_STRNICMP(line - (p - look), look, p - look)
6968 : STRNCMP(line - (p - look), look, p - look))
6969 == 0)
6970 match = TRUE;
6971 }
6972 if (match && try_match_word && !try_match)
6973 {
6974 /* "0=word": Check if there are only blanks before the
6975 * word. */
6976 line = ml_get_curline();
6977 if ((int)(skipwhite(line) - line) !=
6978 (int)(curwin->w_cursor.col - (p - look)))
6979 match = FALSE;
6980 }
6981 if (match)
6982 return TRUE;
6983 }
6984 look = p;
6985 }
6986
6987 /*
6988 * ok, it's a boring generic character.
6989 */
6990 else
6991 {
6992 if (try_match && *look == keytyped)
6993 return TRUE;
6994 ++look;
6995 }
6996
6997 /*
6998 * Skip over ", ".
6999 */
7000 look = skip_to_option_part(look);
7001 }
7002 return FALSE;
7003}
7004#endif /* FEAT_CINDENT */
7005
7006#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7007/*
7008 * Map Hebrew keyboard when in hkmap mode.
7009 */
7010 int
7011hkmap(c)
7012 int c;
7013{
7014 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7015 {
7016 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7017 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7018 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7019 static char_u map[26] =
7020 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7021 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7022 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7023 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7024 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7025 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7026 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7027 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7028 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7029
7030 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7031 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7032 /* '-1'='sofit' */
7033 else if (c == 'x')
7034 return 'X';
7035 else if (c == 'q')
7036 return '\''; /* {geresh}={'} */
7037 else if (c == 246)
7038 return ' '; /* \"o --> ' ' for a german keyboard */
7039 else if (c == 228)
7040 return ' '; /* \"a --> ' ' -- / -- */
7041 else if (c == 252)
7042 return ' '; /* \"u --> ' ' -- / -- */
7043#ifdef EBCDIC
7044 else if (islower(c))
7045#else
7046 /* NOTE: islower() does not do the right thing for us on Linux so we
7047 * do this the same was as 5.7 and previous, so it works correctly on
7048 * all systems. Specifically, the e.g. Delete and Arrow keys are
7049 * munged and won't work if e.g. searching for Hebrew text.
7050 */
7051 else if (c >= 'a' && c <= 'z')
7052#endif
7053 return (int)(map[CharOrdLow(c)] + p_aleph);
7054 else
7055 return c;
7056 }
7057 else
7058 {
7059 switch (c)
7060 {
7061 case '`': return ';';
7062 case '/': return '.';
7063 case '\'': return ',';
7064 case 'q': return '/';
7065 case 'w': return '\'';
7066
7067 /* Hebrew letters - set offset from 'a' */
7068 case ',': c = '{'; break;
7069 case '.': c = 'v'; break;
7070 case ';': c = 't'; break;
7071 default: {
7072 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7073
7074#ifdef EBCDIC
7075 /* see note about islower() above */
7076 if (!islower(c))
7077#else
7078 if (c < 'a' || c > 'z')
7079#endif
7080 return c;
7081 c = str[CharOrdLow(c)];
7082 break;
7083 }
7084 }
7085
7086 return (int)(CharOrdLow(c) + p_aleph);
7087 }
7088}
7089#endif
7090
7091 static void
7092ins_reg()
7093{
7094 int need_redraw = FALSE;
7095 int regname;
7096 int literally = 0;
7097
7098 /*
7099 * If we are going to wait for a character, show a '"'.
7100 */
7101 pc_status = PC_STATUS_UNSET;
7102 if (redrawing() && !char_avail())
7103 {
7104 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007105 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
7107 edit_putchar('"', TRUE);
7108#ifdef FEAT_CMDL_INFO
7109 add_to_showcmd_c(Ctrl_R);
7110#endif
7111 }
7112
7113#ifdef USE_ON_FLY_SCROLL
7114 dont_scroll = TRUE; /* disallow scrolling here */
7115#endif
7116
7117 /*
7118 * Don't map the register name. This also prevents the mode message to be
7119 * deleted when ESC is hit.
7120 */
7121 ++no_mapping;
7122 regname = safe_vgetc();
7123#ifdef FEAT_LANGMAP
7124 LANGMAP_ADJUST(regname, TRUE);
7125#endif
7126 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7127 {
7128 /* Get a third key for literal register insertion */
7129 literally = regname;
7130#ifdef FEAT_CMDL_INFO
7131 add_to_showcmd_c(literally);
7132#endif
7133 regname = safe_vgetc();
7134#ifdef FEAT_LANGMAP
7135 LANGMAP_ADJUST(regname, TRUE);
7136#endif
7137 }
7138 --no_mapping;
7139
7140#ifdef FEAT_EVAL
7141 /*
7142 * Don't call u_sync() while getting the expression,
7143 * evaluating it or giving an error message for it!
7144 */
7145 ++no_u_sync;
7146 if (regname == '=')
7147 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007148# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007150# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007152# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 /* Restore the Input Method. */
7154 if (im_on)
7155 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007156# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007158 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7159 {
7160 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 else
7164 {
7165#endif
7166 if (literally == Ctrl_O || literally == Ctrl_P)
7167 {
7168 /* Append the command to the redo buffer. */
7169 AppendCharToRedobuff(Ctrl_R);
7170 AppendCharToRedobuff(literally);
7171 AppendCharToRedobuff(regname);
7172
7173 do_put(regname, BACKWARD, 1L,
7174 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7175 }
7176 else if (insert_reg(regname, literally) == FAIL)
7177 {
7178 vim_beep();
7179 need_redraw = TRUE; /* remove the '"' */
7180 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007181 else if (stop_insert_mode)
7182 /* When the '=' register was used and a function was invoked that
7183 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7184 * insert anything, need to remove the '"' */
7185 need_redraw = TRUE;
7186
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187#ifdef FEAT_EVAL
7188 }
7189 --no_u_sync;
7190#endif
7191#ifdef FEAT_CMDL_INFO
7192 clear_showcmd();
7193#endif
7194
7195 /* If the inserted register is empty, we need to remove the '"' */
7196 if (need_redraw || stuff_empty())
7197 edit_unputchar();
7198}
7199
7200/*
7201 * CTRL-G commands in Insert mode.
7202 */
7203 static void
7204ins_ctrl_g()
7205{
7206 int c;
7207
7208#ifdef FEAT_INS_EXPAND
7209 /* Right after CTRL-X the cursor will be after the ruler. */
7210 setcursor();
7211#endif
7212
7213 /*
7214 * Don't map the second key. This also prevents the mode message to be
7215 * deleted when ESC is hit.
7216 */
7217 ++no_mapping;
7218 c = safe_vgetc();
7219 --no_mapping;
7220 switch (c)
7221 {
7222 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7223 case K_UP:
7224 case Ctrl_K:
7225 case 'k': ins_up(TRUE);
7226 break;
7227
7228 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7229 case K_DOWN:
7230 case Ctrl_J:
7231 case 'j': ins_down(TRUE);
7232 break;
7233
7234 /* CTRL-G u: start new undoable edit */
7235 case 'u': u_sync();
7236 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007237
7238 /* Need to reset Insstart, esp. because a BS that joins
7239 * aline to the previous one must save for undo. */
7240 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 break;
7242
7243 /* Unknown CTRL-G command, reserved for future expansion. */
7244 default: vim_beep();
7245 }
7246}
7247
7248/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007249 * CTRL-^ in Insert mode.
7250 */
7251 static void
7252ins_ctrl_hat()
7253{
7254 if (map_to_exists_mode((char_u *)"", LANGMAP))
7255 {
7256 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7257 if (State & LANGMAP)
7258 {
7259 curbuf->b_p_iminsert = B_IMODE_NONE;
7260 State &= ~LANGMAP;
7261 }
7262 else
7263 {
7264 curbuf->b_p_iminsert = B_IMODE_LMAP;
7265 State |= LANGMAP;
7266#ifdef USE_IM_CONTROL
7267 im_set_active(FALSE);
7268#endif
7269 }
7270 }
7271#ifdef USE_IM_CONTROL
7272 else
7273 {
7274 /* There are no ":lmap" mappings, toggle IM */
7275 if (im_get_status())
7276 {
7277 curbuf->b_p_iminsert = B_IMODE_NONE;
7278 im_set_active(FALSE);
7279 }
7280 else
7281 {
7282 curbuf->b_p_iminsert = B_IMODE_IM;
7283 State &= ~LANGMAP;
7284 im_set_active(TRUE);
7285 }
7286 }
7287#endif
7288 set_iminsert_global();
7289 showmode();
7290#ifdef FEAT_GUI
7291 /* may show different cursor shape or color */
7292 if (gui.in_use)
7293 gui_update_cursor(TRUE, FALSE);
7294#endif
7295#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7296 /* Show/unshow value of 'keymap' in status lines. */
7297 status_redraw_curbuf();
7298#endif
7299}
7300
7301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 * Handle ESC in insert mode.
7303 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7304 * insert.
7305 */
7306 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007307ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 long *count;
7309 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007310 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311{
7312 int temp;
7313 static int disabled_redraw = FALSE;
7314
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007315#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007316 check_spell_redraw();
7317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318#if defined(FEAT_HANGULIN)
7319# if defined(ESC_CHG_TO_ENG_MODE)
7320 hangul_input_state_set(0);
7321# endif
7322 if (composing_hangul)
7323 {
7324 push_raw_key(composing_hangul_buffer, 2);
7325 composing_hangul = 0;
7326 }
7327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328
7329 temp = curwin->w_cursor.col;
7330 if (disabled_redraw)
7331 {
7332 --RedrawingDisabled;
7333 disabled_redraw = FALSE;
7334 }
7335 if (!arrow_used)
7336 {
7337 /*
7338 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007339 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7340 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 */
7342 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007343 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344
7345 /*
7346 * Repeating insert may take a long time. Check for
7347 * interrupt now and then.
7348 */
7349 if (*count > 0)
7350 {
7351 line_breakcheck();
7352 if (got_int)
7353 *count = 0;
7354 }
7355
7356 if (--*count > 0) /* repeat what was typed */
7357 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007358 /* Vi repeats the insert without replacing characters. */
7359 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7360 State &= ~REPLACE_FLAG;
7361
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 (void)start_redo_ins();
7363 if (cmdchar == 'r' || cmdchar == 'v')
7364 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7365 ++RedrawingDisabled;
7366 disabled_redraw = TRUE;
7367 return FALSE; /* repeat the insert */
7368 }
7369 stop_insert(&curwin->w_cursor, TRUE);
7370 undisplay_dollar();
7371 }
7372
7373 /* When an autoindent was removed, curswant stays after the
7374 * indent */
7375 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7376 curwin->w_set_curswant = TRUE;
7377
7378 /* Remember the last Insert position in the '^ mark. */
7379 if (!cmdmod.keepjumps)
7380 curbuf->b_last_insert = curwin->w_cursor;
7381
7382 /*
7383 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007384 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007386 if (!nomove
7387 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388#ifdef FEAT_VIRTUALEDIT
7389 || curwin->w_cursor.coladd > 0
7390#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007391 )
7392 && (restart_edit == NUL
7393 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007395 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007397 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398#ifdef FEAT_RIGHTLEFT
7399 && !revins_on
7400#endif
7401 )
7402 {
7403#ifdef FEAT_VIRTUALEDIT
7404 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7405 {
7406 oneleft();
7407 if (restart_edit != NUL)
7408 ++curwin->w_cursor.coladd;
7409 }
7410 else
7411#endif
7412 {
7413 --curwin->w_cursor.col;
7414#ifdef FEAT_MBYTE
7415 /* Correct cursor for multi-byte character. */
7416 if (has_mbyte)
7417 mb_adjust_cursor();
7418#endif
7419 }
7420 }
7421
7422#ifdef USE_IM_CONTROL
7423 /* Disable IM to allow typing English directly for Normal mode commands.
7424 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7425 * well). */
7426 if (!(State & LANGMAP))
7427 im_save_status(&curbuf->b_p_iminsert);
7428 im_set_active(FALSE);
7429#endif
7430
7431 State = NORMAL;
7432 /* need to position cursor again (e.g. when on a TAB ) */
7433 changed_cline_bef_curs();
7434
7435#ifdef FEAT_MOUSE
7436 setmouse();
7437#endif
7438#ifdef CURSOR_SHAPE
7439 ui_cursor_shape(); /* may show different cursor shape */
7440#endif
7441
7442 /*
7443 * When recording or for CTRL-O, need to display the new mode.
7444 * Otherwise remove the mode message.
7445 */
7446 if (Recording || restart_edit != NUL)
7447 showmode();
7448 else if (p_smd)
7449 MSG("");
7450
7451 return TRUE; /* exit Insert mode */
7452}
7453
7454#ifdef FEAT_RIGHTLEFT
7455/*
7456 * Toggle language: hkmap and revins_on.
7457 * Move to end of reverse inserted text.
7458 */
7459 static void
7460ins_ctrl_()
7461{
7462 if (revins_on && revins_chars && revins_scol >= 0)
7463 {
7464 while (gchar_cursor() != NUL && revins_chars--)
7465 ++curwin->w_cursor.col;
7466 }
7467 p_ri = !p_ri;
7468 revins_on = (State == INSERT && p_ri);
7469 if (revins_on)
7470 {
7471 revins_scol = curwin->w_cursor.col;
7472 revins_legal++;
7473 revins_chars = 0;
7474 undisplay_dollar();
7475 }
7476 else
7477 revins_scol = -1;
7478#ifdef FEAT_FKMAP
7479 if (p_altkeymap)
7480 {
7481 /*
7482 * to be consistent also for redo command, using '.'
7483 * set arrow_used to true and stop it - causing to redo
7484 * characters entered in one mode (normal/reverse insert).
7485 */
7486 arrow_used = TRUE;
7487 (void)stop_arrow();
7488 p_fkmap = curwin->w_p_rl ^ p_ri;
7489 if (p_fkmap && p_ri)
7490 State = INSERT;
7491 }
7492 else
7493#endif
7494 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7495 showmode();
7496}
7497#endif
7498
7499#ifdef FEAT_VISUAL
7500/*
7501 * If 'keymodel' contains "startsel", may start selection.
7502 * Returns TRUE when a CTRL-O and other keys stuffed.
7503 */
7504 static int
7505ins_start_select(c)
7506 int c;
7507{
7508 if (km_startsel)
7509 switch (c)
7510 {
7511 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 case K_PAGEUP:
7514 case K_KPAGEUP:
7515 case K_PAGEDOWN:
7516 case K_KPAGEDOWN:
7517# ifdef MACOS
7518 case K_LEFT:
7519 case K_RIGHT:
7520 case K_UP:
7521 case K_DOWN:
7522 case K_END:
7523 case K_HOME:
7524# endif
7525 if (!(mod_mask & MOD_MASK_SHIFT))
7526 break;
7527 /* FALLTHROUGH */
7528 case K_S_LEFT:
7529 case K_S_RIGHT:
7530 case K_S_UP:
7531 case K_S_DOWN:
7532 case K_S_END:
7533 case K_S_HOME:
7534 /* Start selection right away, the cursor can move with
7535 * CTRL-O when beyond the end of the line. */
7536 start_selection();
7537
7538 /* Execute the key in (insert) Select mode. */
7539 stuffcharReadbuff(Ctrl_O);
7540 if (mod_mask)
7541 {
7542 char_u buf[4];
7543
7544 buf[0] = K_SPECIAL;
7545 buf[1] = KS_MODIFIER;
7546 buf[2] = mod_mask;
7547 buf[3] = NUL;
7548 stuffReadbuff(buf);
7549 }
7550 stuffcharReadbuff(c);
7551 return TRUE;
7552 }
7553 return FALSE;
7554}
7555#endif
7556
7557/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007558 * <Insert> key in Insert mode: toggle insert/remplace mode.
7559 */
7560 static void
7561ins_insert(replaceState)
7562 int replaceState;
7563{
7564#ifdef FEAT_FKMAP
7565 if (p_fkmap && p_ri)
7566 {
7567 beep_flush();
7568 EMSG(farsi_text_3); /* encoded in Farsi */
7569 return;
7570 }
7571#endif
7572
7573#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007574# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007575 set_vim_var_string(VV_INSERTMODE,
7576 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007577# ifdef FEAT_VREPLACE
7578 replaceState == VREPLACE ? "v" :
7579# endif
7580 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007581# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007582 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7583#endif
7584 if (State & REPLACE_FLAG)
7585 State = INSERT | (State & LANGMAP);
7586 else
7587 State = replaceState | (State & LANGMAP);
7588 AppendCharToRedobuff(K_INS);
7589 showmode();
7590#ifdef CURSOR_SHAPE
7591 ui_cursor_shape(); /* may show different cursor shape */
7592#endif
7593}
7594
7595/*
7596 * Pressed CTRL-O in Insert mode.
7597 */
7598 static void
7599ins_ctrl_o()
7600{
7601#ifdef FEAT_VREPLACE
7602 if (State & VREPLACE_FLAG)
7603 restart_edit = 'V';
7604 else
7605#endif
7606 if (State & REPLACE_FLAG)
7607 restart_edit = 'R';
7608 else
7609 restart_edit = 'I';
7610#ifdef FEAT_VIRTUALEDIT
7611 if (virtual_active())
7612 ins_at_eol = FALSE; /* cursor always keeps its column */
7613 else
7614#endif
7615 ins_at_eol = (gchar_cursor() == NUL);
7616}
7617
7618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 * If the cursor is on an indent, ^T/^D insert/delete one
7620 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7621 * Always round the indent to 'shiftwith', this is compatible
7622 * with vi. But vi only supports ^T and ^D after an
7623 * autoindent, we support it everywhere.
7624 */
7625 static void
7626ins_shift(c, lastc)
7627 int c;
7628 int lastc;
7629{
7630 if (stop_arrow() == FAIL)
7631 return;
7632 AppendCharToRedobuff(c);
7633
7634 /*
7635 * 0^D and ^^D: remove all indent.
7636 */
7637 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7638 {
7639 --curwin->w_cursor.col;
7640 (void)del_char(FALSE); /* delete the '^' or '0' */
7641 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7642 if (State & REPLACE_FLAG)
7643 replace_pop_ins();
7644 if (lastc == '^')
7645 old_indent = get_indent(); /* remember curr. indent */
7646 change_indent(INDENT_SET, 0, TRUE, 0);
7647 }
7648 else
7649 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7650
7651 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7652 did_ai = FALSE;
7653#ifdef FEAT_SMARTINDENT
7654 did_si = FALSE;
7655 can_si = FALSE;
7656 can_si_back = FALSE;
7657#endif
7658#ifdef FEAT_CINDENT
7659 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7660#endif
7661}
7662
7663 static void
7664ins_del()
7665{
7666 int temp;
7667
7668 if (stop_arrow() == FAIL)
7669 return;
7670 if (gchar_cursor() == NUL) /* delete newline */
7671 {
7672 temp = curwin->w_cursor.col;
7673 if (!can_bs(BS_EOL) /* only if "eol" included */
7674 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7675 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7676 || do_join(FALSE) == FAIL)
7677 vim_beep();
7678 else
7679 curwin->w_cursor.col = temp;
7680 }
7681 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7682 vim_beep();
7683 did_ai = FALSE;
7684#ifdef FEAT_SMARTINDENT
7685 did_si = FALSE;
7686 can_si = FALSE;
7687 can_si_back = FALSE;
7688#endif
7689 AppendCharToRedobuff(K_DEL);
7690}
7691
7692/*
7693 * Handle Backspace, delete-word and delete-line in Insert mode.
7694 * Return TRUE when backspace was actually used.
7695 */
7696 static int
7697ins_bs(c, mode, inserted_space_p)
7698 int c;
7699 int mode;
7700 int *inserted_space_p;
7701{
7702 linenr_T lnum;
7703 int cc;
7704 int temp = 0; /* init for GCC */
7705 colnr_T mincol;
7706 int did_backspace = FALSE;
7707 int in_indent;
7708 int oldState;
7709#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007710 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711#endif
7712
7713 /*
7714 * can't delete anything in an empty file
7715 * can't backup past first character in buffer
7716 * can't backup past starting point unless 'backspace' > 1
7717 * can backup to a previous line if 'backspace' == 0
7718 */
7719 if ( bufempty()
7720 || (
7721#ifdef FEAT_RIGHTLEFT
7722 !revins_on &&
7723#endif
7724 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7725 || (!can_bs(BS_START)
7726 && (arrow_used
7727 || (curwin->w_cursor.lnum == Insstart.lnum
7728 && curwin->w_cursor.col <= Insstart.col)))
7729 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7730 && curwin->w_cursor.col <= ai_col)
7731 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7732 {
7733 vim_beep();
7734 return FALSE;
7735 }
7736
7737 if (stop_arrow() == FAIL)
7738 return FALSE;
7739 in_indent = inindent(0);
7740#ifdef FEAT_CINDENT
7741 if (in_indent)
7742 can_cindent = FALSE;
7743#endif
7744#ifdef FEAT_COMMENTS
7745 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7746#endif
7747#ifdef FEAT_RIGHTLEFT
7748 if (revins_on) /* put cursor after last inserted char */
7749 inc_cursor();
7750#endif
7751
7752#ifdef FEAT_VIRTUALEDIT
7753 /* Virtualedit:
7754 * BACKSPACE_CHAR eats a virtual space
7755 * BACKSPACE_WORD eats all coladd
7756 * BACKSPACE_LINE eats all coladd and keeps going
7757 */
7758 if (curwin->w_cursor.coladd > 0)
7759 {
7760 if (mode == BACKSPACE_CHAR)
7761 {
7762 --curwin->w_cursor.coladd;
7763 return TRUE;
7764 }
7765 if (mode == BACKSPACE_WORD)
7766 {
7767 curwin->w_cursor.coladd = 0;
7768 return TRUE;
7769 }
7770 curwin->w_cursor.coladd = 0;
7771 }
7772#endif
7773
7774 /*
7775 * delete newline!
7776 */
7777 if (curwin->w_cursor.col == 0)
7778 {
7779 lnum = Insstart.lnum;
7780 if (curwin->w_cursor.lnum == Insstart.lnum
7781#ifdef FEAT_RIGHTLEFT
7782 || revins_on
7783#endif
7784 )
7785 {
7786 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7787 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7788 return FALSE;
7789 --Insstart.lnum;
7790 Insstart.col = MAXCOL;
7791 }
7792 /*
7793 * In replace mode:
7794 * cc < 0: NL was inserted, delete it
7795 * cc >= 0: NL was replaced, put original characters back
7796 */
7797 cc = -1;
7798 if (State & REPLACE_FLAG)
7799 cc = replace_pop(); /* returns -1 if NL was inserted */
7800 /*
7801 * In replace mode, in the line we started replacing, we only move the
7802 * cursor.
7803 */
7804 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7805 {
7806 dec_cursor();
7807 }
7808 else
7809 {
7810#ifdef FEAT_VREPLACE
7811 if (!(State & VREPLACE_FLAG)
7812 || curwin->w_cursor.lnum > orig_line_count)
7813#endif
7814 {
7815 temp = gchar_cursor(); /* remember current char */
7816 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007817
7818 /* When "aw" is in 'formatoptions' we must delete the space at
7819 * the end of the line, otherwise the line will be broken
7820 * again when auto-formatting. */
7821 if (has_format_option(FO_AUTO)
7822 && has_format_option(FO_WHITE_PAR))
7823 {
7824 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7825 TRUE);
7826 int len;
7827
7828 len = STRLEN(ptr);
7829 if (len > 0 && ptr[len - 1] == ' ')
7830 ptr[len - 1] = NUL;
7831 }
7832
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 (void)do_join(FALSE);
7834 if (temp == NUL && gchar_cursor() != NUL)
7835 inc_cursor();
7836 }
7837#ifdef FEAT_VREPLACE
7838 else
7839 dec_cursor();
7840#endif
7841
7842 /*
7843 * In REPLACE mode we have to put back the text that was replaced
7844 * by the NL. On the replace stack is first a NUL-terminated
7845 * sequence of characters that were deleted and then the
7846 * characters that NL replaced.
7847 */
7848 if (State & REPLACE_FLAG)
7849 {
7850 /*
7851 * Do the next ins_char() in NORMAL state, to
7852 * prevent ins_char() from replacing characters and
7853 * avoiding showmatch().
7854 */
7855 oldState = State;
7856 State = NORMAL;
7857 /*
7858 * restore characters (blanks) deleted after cursor
7859 */
7860 while (cc > 0)
7861 {
7862 temp = curwin->w_cursor.col;
7863#ifdef FEAT_MBYTE
7864 mb_replace_pop_ins(cc);
7865#else
7866 ins_char(cc);
7867#endif
7868 curwin->w_cursor.col = temp;
7869 cc = replace_pop();
7870 }
7871 /* restore the characters that NL replaced */
7872 replace_pop_ins();
7873 State = oldState;
7874 }
7875 }
7876 did_ai = FALSE;
7877 }
7878 else
7879 {
7880 /*
7881 * Delete character(s) before the cursor.
7882 */
7883#ifdef FEAT_RIGHTLEFT
7884 if (revins_on) /* put cursor on last inserted char */
7885 dec_cursor();
7886#endif
7887 mincol = 0;
7888 /* keep indent */
7889 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7890#ifdef FEAT_RIGHTLEFT
7891 && !revins_on
7892#endif
7893 )
7894 {
7895 temp = curwin->w_cursor.col;
7896 beginline(BL_WHITE);
7897 if (curwin->w_cursor.col < (colnr_T)temp)
7898 mincol = curwin->w_cursor.col;
7899 curwin->w_cursor.col = temp;
7900 }
7901
7902 /*
7903 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7904 */
7905 if ( mode == BACKSPACE_CHAR
7906 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007907 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 && (*(ml_get_cursor() - 1) == TAB
7909 || (*(ml_get_cursor() - 1) == ' '
7910 && (!*inserted_space_p
7911 || arrow_used))))))
7912 {
7913 int ts;
7914 colnr_T vcol;
7915 colnr_T want_vcol;
7916 int extra = 0;
7917
7918 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007919 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 ts = curbuf->b_p_sw;
7921 else
7922 ts = curbuf->b_p_sts;
7923 /* Compute the virtual column where we want to be. Since
7924 * 'showbreak' may get in the way, need to get the last column of
7925 * the previous character. */
7926 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7927 dec_cursor();
7928 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7929 inc_cursor();
7930 want_vcol = (want_vcol / ts) * ts;
7931
7932 /* delete characters until we are at or before want_vcol */
7933 while (vcol > want_vcol
7934 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7935 {
7936 dec_cursor();
7937 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7938 if (State & REPLACE_FLAG)
7939 {
7940 /* Don't delete characters before the insert point when in
7941 * Replace mode */
7942 if (curwin->w_cursor.lnum != Insstart.lnum
7943 || curwin->w_cursor.col >= Insstart.col)
7944 {
7945#if 0 /* what was this for? It causes problems when sw != ts. */
7946 if (State == REPLACE && (int)vcol < want_vcol)
7947 {
7948 (void)del_char(FALSE);
7949 extra = 2; /* don't pop too much */
7950 }
7951 else
7952#endif
7953 replace_do_bs();
7954 }
7955 }
7956 else
7957 (void)del_char(FALSE);
7958 }
7959
7960 /* insert extra spaces until we are at want_vcol */
7961 while (vcol < want_vcol)
7962 {
7963 /* Remember the first char we inserted */
7964 if (curwin->w_cursor.lnum == Insstart.lnum
7965 && curwin->w_cursor.col < Insstart.col)
7966 Insstart.col = curwin->w_cursor.col;
7967
7968#ifdef FEAT_VREPLACE
7969 if (State & VREPLACE_FLAG)
7970 ins_char(' ');
7971 else
7972#endif
7973 {
7974 ins_str((char_u *)" ");
7975 if ((State & REPLACE_FLAG) && extra <= 1)
7976 {
7977 if (extra)
7978 replace_push_off(NUL);
7979 else
7980 replace_push(NUL);
7981 }
7982 if (extra == 2)
7983 extra = 1;
7984 }
7985 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7986 }
7987 }
7988
7989 /*
7990 * Delete upto starting point, start of line or previous word.
7991 */
7992 else do
7993 {
7994#ifdef FEAT_RIGHTLEFT
7995 if (!revins_on) /* put cursor on char to be deleted */
7996#endif
7997 dec_cursor();
7998
7999 /* start of word? */
8000 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8001 {
8002 mode = BACKSPACE_WORD_NOT_SPACE;
8003 temp = vim_iswordc(gchar_cursor());
8004 }
8005 /* end of word? */
8006 else if (mode == BACKSPACE_WORD_NOT_SPACE
8007 && (vim_isspace(cc = gchar_cursor())
8008 || vim_iswordc(cc) != temp))
8009 {
8010#ifdef FEAT_RIGHTLEFT
8011 if (!revins_on)
8012#endif
8013 inc_cursor();
8014#ifdef FEAT_RIGHTLEFT
8015 else if (State & REPLACE_FLAG)
8016 dec_cursor();
8017#endif
8018 break;
8019 }
8020 if (State & REPLACE_FLAG)
8021 replace_do_bs();
8022 else
8023 {
8024#ifdef FEAT_MBYTE
8025 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008026 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027#endif
8028 (void)del_char(FALSE);
8029#ifdef FEAT_MBYTE
8030 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008031 * If there are combining characters and 'delcombine' is set
8032 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 * character.
8034 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008035 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 inc_cursor();
8037#endif
8038#ifdef FEAT_RIGHTLEFT
8039 if (revins_chars)
8040 {
8041 revins_chars--;
8042 revins_legal++;
8043 }
8044 if (revins_on && gchar_cursor() == NUL)
8045 break;
8046#endif
8047 }
8048 /* Just a single backspace?: */
8049 if (mode == BACKSPACE_CHAR)
8050 break;
8051 } while (
8052#ifdef FEAT_RIGHTLEFT
8053 revins_on ||
8054#endif
8055 (curwin->w_cursor.col > mincol
8056 && (curwin->w_cursor.lnum != Insstart.lnum
8057 || curwin->w_cursor.col != Insstart.col)));
8058 did_backspace = TRUE;
8059 }
8060#ifdef FEAT_SMARTINDENT
8061 did_si = FALSE;
8062 can_si = FALSE;
8063 can_si_back = FALSE;
8064#endif
8065 if (curwin->w_cursor.col <= 1)
8066 did_ai = FALSE;
8067 /*
8068 * It's a little strange to put backspaces into the redo
8069 * buffer, but it makes auto-indent a lot easier to deal
8070 * with.
8071 */
8072 AppendCharToRedobuff(c);
8073
8074 /* If deleted before the insertion point, adjust it */
8075 if (curwin->w_cursor.lnum == Insstart.lnum
8076 && curwin->w_cursor.col < Insstart.col)
8077 Insstart.col = curwin->w_cursor.col;
8078
8079 /* vi behaviour: the cursor moves backward but the character that
8080 * was there remains visible
8081 * Vim behaviour: the cursor moves backward and the character that
8082 * was there is erased from the screen.
8083 * We can emulate the vi behaviour by pretending there is a dollar
8084 * displayed even when there isn't.
8085 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8086 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8087 dollar_vcol = curwin->w_virtcol;
8088
8089 return did_backspace;
8090}
8091
8092#ifdef FEAT_MOUSE
8093 static void
8094ins_mouse(c)
8095 int c;
8096{
8097 pos_T tpos;
8098
8099# ifdef FEAT_GUI
8100 /* When GUI is active, also move/paste when 'mouse' is empty */
8101 if (!gui.in_use)
8102# endif
8103 if (!mouse_has(MOUSE_INSERT))
8104 return;
8105
8106 undisplay_dollar();
8107 tpos = curwin->w_cursor;
8108 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8109 {
8110 start_arrow(&tpos);
8111# ifdef FEAT_CINDENT
8112 can_cindent = TRUE;
8113# endif
8114 }
8115
8116#ifdef FEAT_WINDOWS
8117 /* redraw status lines (in case another window became active) */
8118 redraw_statuslines();
8119#endif
8120}
8121
8122 static void
8123ins_mousescroll(up)
8124 int up;
8125{
8126 pos_T tpos;
8127# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8128 win_T *old_curwin;
8129# endif
8130
8131 tpos = curwin->w_cursor;
8132
8133# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8134 old_curwin = curwin;
8135
8136 /* Currently the mouse coordinates are only known in the GUI. */
8137 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8138 {
8139 int row, col;
8140
8141 row = mouse_row;
8142 col = mouse_col;
8143
8144 /* find the window at the pointer coordinates */
8145 curwin = mouse_find_win(&row, &col);
8146 curbuf = curwin->w_buffer;
8147 }
8148 if (curwin == old_curwin)
8149# endif
8150 undisplay_dollar();
8151
8152 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8153 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8154 else
8155 scroll_redraw(up, 3L);
8156
8157# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8158 curwin->w_redr_status = TRUE;
8159
8160 curwin = old_curwin;
8161 curbuf = curwin->w_buffer;
8162# endif
8163
8164 if (!equalpos(curwin->w_cursor, tpos))
8165 {
8166 start_arrow(&tpos);
8167# ifdef FEAT_CINDENT
8168 can_cindent = TRUE;
8169# endif
8170 }
8171}
8172#endif
8173
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008174#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008175 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008176ins_tabline(c)
8177 int c;
8178{
8179 /* We will be leaving the current window, unless closing another tab. */
8180 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8181 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8182 {
8183 undisplay_dollar();
8184 start_arrow(&curwin->w_cursor);
8185# ifdef FEAT_CINDENT
8186 can_cindent = TRUE;
8187# endif
8188 }
8189
8190 if (c == K_TABLINE)
8191 goto_tabpage(current_tab);
8192 else
8193 handle_tabmenu();
8194
8195}
8196#endif
8197
8198#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 void
8200ins_scroll()
8201{
8202 pos_T tpos;
8203
8204 undisplay_dollar();
8205 tpos = curwin->w_cursor;
8206 if (gui_do_scroll())
8207 {
8208 start_arrow(&tpos);
8209# ifdef FEAT_CINDENT
8210 can_cindent = TRUE;
8211# endif
8212 }
8213}
8214
8215 void
8216ins_horscroll()
8217{
8218 pos_T tpos;
8219
8220 undisplay_dollar();
8221 tpos = curwin->w_cursor;
8222 if (gui_do_horiz_scroll())
8223 {
8224 start_arrow(&tpos);
8225# ifdef FEAT_CINDENT
8226 can_cindent = TRUE;
8227# endif
8228 }
8229}
8230#endif
8231
8232 static void
8233ins_left()
8234{
8235 pos_T tpos;
8236
8237#ifdef FEAT_FOLDING
8238 if ((fdo_flags & FDO_HOR) && KeyTyped)
8239 foldOpenCursor();
8240#endif
8241 undisplay_dollar();
8242 tpos = curwin->w_cursor;
8243 if (oneleft() == OK)
8244 {
8245 start_arrow(&tpos);
8246#ifdef FEAT_RIGHTLEFT
8247 /* If exit reversed string, position is fixed */
8248 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8249 revins_legal++;
8250 revins_chars++;
8251#endif
8252 }
8253
8254 /*
8255 * if 'whichwrap' set for cursor in insert mode may go to
8256 * previous line
8257 */
8258 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8259 {
8260 start_arrow(&tpos);
8261 --(curwin->w_cursor.lnum);
8262 coladvance((colnr_T)MAXCOL);
8263 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8264 }
8265 else
8266 vim_beep();
8267}
8268
8269 static void
8270ins_home(c)
8271 int c;
8272{
8273 pos_T tpos;
8274
8275#ifdef FEAT_FOLDING
8276 if ((fdo_flags & FDO_HOR) && KeyTyped)
8277 foldOpenCursor();
8278#endif
8279 undisplay_dollar();
8280 tpos = curwin->w_cursor;
8281 if (c == K_C_HOME)
8282 curwin->w_cursor.lnum = 1;
8283 curwin->w_cursor.col = 0;
8284#ifdef FEAT_VIRTUALEDIT
8285 curwin->w_cursor.coladd = 0;
8286#endif
8287 curwin->w_curswant = 0;
8288 start_arrow(&tpos);
8289}
8290
8291 static void
8292ins_end(c)
8293 int c;
8294{
8295 pos_T tpos;
8296
8297#ifdef FEAT_FOLDING
8298 if ((fdo_flags & FDO_HOR) && KeyTyped)
8299 foldOpenCursor();
8300#endif
8301 undisplay_dollar();
8302 tpos = curwin->w_cursor;
8303 if (c == K_C_END)
8304 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8305 coladvance((colnr_T)MAXCOL);
8306 curwin->w_curswant = MAXCOL;
8307
8308 start_arrow(&tpos);
8309}
8310
8311 static void
8312ins_s_left()
8313{
8314#ifdef FEAT_FOLDING
8315 if ((fdo_flags & FDO_HOR) && KeyTyped)
8316 foldOpenCursor();
8317#endif
8318 undisplay_dollar();
8319 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8320 {
8321 start_arrow(&curwin->w_cursor);
8322 (void)bck_word(1L, FALSE, FALSE);
8323 curwin->w_set_curswant = TRUE;
8324 }
8325 else
8326 vim_beep();
8327}
8328
8329 static void
8330ins_right()
8331{
8332#ifdef FEAT_FOLDING
8333 if ((fdo_flags & FDO_HOR) && KeyTyped)
8334 foldOpenCursor();
8335#endif
8336 undisplay_dollar();
8337 if (gchar_cursor() != NUL || virtual_active()
8338 )
8339 {
8340 start_arrow(&curwin->w_cursor);
8341 curwin->w_set_curswant = TRUE;
8342#ifdef FEAT_VIRTUALEDIT
8343 if (virtual_active())
8344 oneright();
8345 else
8346#endif
8347 {
8348#ifdef FEAT_MBYTE
8349 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008350 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 else
8352#endif
8353 ++curwin->w_cursor.col;
8354 }
8355
8356#ifdef FEAT_RIGHTLEFT
8357 revins_legal++;
8358 if (revins_chars)
8359 revins_chars--;
8360#endif
8361 }
8362 /* if 'whichwrap' set for cursor in insert mode, may move the
8363 * cursor to the next line */
8364 else if (vim_strchr(p_ww, ']') != NULL
8365 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8366 {
8367 start_arrow(&curwin->w_cursor);
8368 curwin->w_set_curswant = TRUE;
8369 ++curwin->w_cursor.lnum;
8370 curwin->w_cursor.col = 0;
8371 }
8372 else
8373 vim_beep();
8374}
8375
8376 static void
8377ins_s_right()
8378{
8379#ifdef FEAT_FOLDING
8380 if ((fdo_flags & FDO_HOR) && KeyTyped)
8381 foldOpenCursor();
8382#endif
8383 undisplay_dollar();
8384 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8385 || gchar_cursor() != NUL)
8386 {
8387 start_arrow(&curwin->w_cursor);
8388 (void)fwd_word(1L, FALSE, 0);
8389 curwin->w_set_curswant = TRUE;
8390 }
8391 else
8392 vim_beep();
8393}
8394
8395 static void
8396ins_up(startcol)
8397 int startcol; /* when TRUE move to Insstart.col */
8398{
8399 pos_T tpos;
8400 linenr_T old_topline = curwin->w_topline;
8401#ifdef FEAT_DIFF
8402 int old_topfill = curwin->w_topfill;
8403#endif
8404
8405 undisplay_dollar();
8406 tpos = curwin->w_cursor;
8407 if (cursor_up(1L, TRUE) == OK)
8408 {
8409 if (startcol)
8410 coladvance(getvcol_nolist(&Insstart));
8411 if (old_topline != curwin->w_topline
8412#ifdef FEAT_DIFF
8413 || old_topfill != curwin->w_topfill
8414#endif
8415 )
8416 redraw_later(VALID);
8417 start_arrow(&tpos);
8418#ifdef FEAT_CINDENT
8419 can_cindent = TRUE;
8420#endif
8421 }
8422 else
8423 vim_beep();
8424}
8425
8426 static void
8427ins_pageup()
8428{
8429 pos_T tpos;
8430
8431 undisplay_dollar();
8432 tpos = curwin->w_cursor;
8433 if (onepage(BACKWARD, 1L) == OK)
8434 {
8435 start_arrow(&tpos);
8436#ifdef FEAT_CINDENT
8437 can_cindent = TRUE;
8438#endif
8439 }
8440 else
8441 vim_beep();
8442}
8443
8444 static void
8445ins_down(startcol)
8446 int startcol; /* when TRUE move to Insstart.col */
8447{
8448 pos_T tpos;
8449 linenr_T old_topline = curwin->w_topline;
8450#ifdef FEAT_DIFF
8451 int old_topfill = curwin->w_topfill;
8452#endif
8453
8454 undisplay_dollar();
8455 tpos = curwin->w_cursor;
8456 if (cursor_down(1L, TRUE) == OK)
8457 {
8458 if (startcol)
8459 coladvance(getvcol_nolist(&Insstart));
8460 if (old_topline != curwin->w_topline
8461#ifdef FEAT_DIFF
8462 || old_topfill != curwin->w_topfill
8463#endif
8464 )
8465 redraw_later(VALID);
8466 start_arrow(&tpos);
8467#ifdef FEAT_CINDENT
8468 can_cindent = TRUE;
8469#endif
8470 }
8471 else
8472 vim_beep();
8473}
8474
8475 static void
8476ins_pagedown()
8477{
8478 pos_T tpos;
8479
8480 undisplay_dollar();
8481 tpos = curwin->w_cursor;
8482 if (onepage(FORWARD, 1L) == OK)
8483 {
8484 start_arrow(&tpos);
8485#ifdef FEAT_CINDENT
8486 can_cindent = TRUE;
8487#endif
8488 }
8489 else
8490 vim_beep();
8491}
8492
8493#ifdef FEAT_DND
8494 static void
8495ins_drop()
8496{
8497 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8498}
8499#endif
8500
8501/*
8502 * Handle TAB in Insert or Replace mode.
8503 * Return TRUE when the TAB needs to be inserted like a normal character.
8504 */
8505 static int
8506ins_tab()
8507{
8508 int ind;
8509 int i;
8510 int temp;
8511
8512 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8513 Insstart_blank_vcol = get_nolist_virtcol();
8514 if (echeck_abbr(TAB + ABBR_OFF))
8515 return FALSE;
8516
8517 ind = inindent(0);
8518#ifdef FEAT_CINDENT
8519 if (ind)
8520 can_cindent = FALSE;
8521#endif
8522
8523 /*
8524 * When nothing special, insert TAB like a normal character
8525 */
8526 if (!curbuf->b_p_et
8527 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8528 && curbuf->b_p_sts == 0)
8529 return TRUE;
8530
8531 if (stop_arrow() == FAIL)
8532 return TRUE;
8533
8534 did_ai = FALSE;
8535#ifdef FEAT_SMARTINDENT
8536 did_si = FALSE;
8537 can_si = FALSE;
8538 can_si_back = FALSE;
8539#endif
8540 AppendToRedobuff((char_u *)"\t");
8541
8542 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8543 temp = (int)curbuf->b_p_sw;
8544 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8545 temp = (int)curbuf->b_p_sts;
8546 else /* otherwise use 'tabstop' */
8547 temp = (int)curbuf->b_p_ts;
8548 temp -= get_nolist_virtcol() % temp;
8549
8550 /*
8551 * Insert the first space with ins_char(). It will delete one char in
8552 * replace mode. Insert the rest with ins_str(); it will not delete any
8553 * chars. For VREPLACE mode, we use ins_char() for all characters.
8554 */
8555 ins_char(' ');
8556 while (--temp > 0)
8557 {
8558#ifdef FEAT_VREPLACE
8559 if (State & VREPLACE_FLAG)
8560 ins_char(' ');
8561 else
8562#endif
8563 {
8564 ins_str((char_u *)" ");
8565 if (State & REPLACE_FLAG) /* no char replaced */
8566 replace_push(NUL);
8567 }
8568 }
8569
8570 /*
8571 * When 'expandtab' not set: Replace spaces by TABs where possible.
8572 */
8573 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8574 {
8575 char_u *ptr;
8576#ifdef FEAT_VREPLACE
8577 char_u *saved_line = NULL; /* init for GCC */
8578 pos_T pos;
8579#endif
8580 pos_T fpos;
8581 pos_T *cursor;
8582 colnr_T want_vcol, vcol;
8583 int change_col = -1;
8584 int save_list = curwin->w_p_list;
8585
8586 /*
8587 * Get the current line. For VREPLACE mode, don't make real changes
8588 * yet, just work on a copy of the line.
8589 */
8590#ifdef FEAT_VREPLACE
8591 if (State & VREPLACE_FLAG)
8592 {
8593 pos = curwin->w_cursor;
8594 cursor = &pos;
8595 saved_line = vim_strsave(ml_get_curline());
8596 if (saved_line == NULL)
8597 return FALSE;
8598 ptr = saved_line + pos.col;
8599 }
8600 else
8601#endif
8602 {
8603 ptr = ml_get_cursor();
8604 cursor = &curwin->w_cursor;
8605 }
8606
8607 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8608 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8609 curwin->w_p_list = FALSE;
8610
8611 /* Find first white before the cursor */
8612 fpos = curwin->w_cursor;
8613 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8614 {
8615 --fpos.col;
8616 --ptr;
8617 }
8618
8619 /* In Replace mode, don't change characters before the insert point. */
8620 if ((State & REPLACE_FLAG)
8621 && fpos.lnum == Insstart.lnum
8622 && fpos.col < Insstart.col)
8623 {
8624 ptr += Insstart.col - fpos.col;
8625 fpos.col = Insstart.col;
8626 }
8627
8628 /* compute virtual column numbers of first white and cursor */
8629 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8630 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8631
8632 /* Use as many TABs as possible. Beware of 'showbreak' and
8633 * 'linebreak' adding extra virtual columns. */
8634 while (vim_iswhite(*ptr))
8635 {
8636 i = lbr_chartabsize((char_u *)"\t", vcol);
8637 if (vcol + i > want_vcol)
8638 break;
8639 if (*ptr != TAB)
8640 {
8641 *ptr = TAB;
8642 if (change_col < 0)
8643 {
8644 change_col = fpos.col; /* Column of first change */
8645 /* May have to adjust Insstart */
8646 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8647 Insstart.col = fpos.col;
8648 }
8649 }
8650 ++fpos.col;
8651 ++ptr;
8652 vcol += i;
8653 }
8654
8655 if (change_col >= 0)
8656 {
8657 int repl_off = 0;
8658
8659 /* Skip over the spaces we need. */
8660 while (vcol < want_vcol && *ptr == ' ')
8661 {
8662 vcol += lbr_chartabsize(ptr, vcol);
8663 ++ptr;
8664 ++repl_off;
8665 }
8666 if (vcol > want_vcol)
8667 {
8668 /* Must have a char with 'showbreak' just before it. */
8669 --ptr;
8670 --repl_off;
8671 }
8672 fpos.col += repl_off;
8673
8674 /* Delete following spaces. */
8675 i = cursor->col - fpos.col;
8676 if (i > 0)
8677 {
8678 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8679 /* correct replace stack. */
8680 if ((State & REPLACE_FLAG)
8681#ifdef FEAT_VREPLACE
8682 && !(State & VREPLACE_FLAG)
8683#endif
8684 )
8685 for (temp = i; --temp >= 0; )
8686 replace_join(repl_off);
8687 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008688#ifdef FEAT_NETBEANS_INTG
8689 if (usingNetbeans)
8690 {
8691 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8692 (long)(i + 1));
8693 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8694 (char_u *)"\t", 1);
8695 }
8696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 cursor->col -= i;
8698
8699#ifdef FEAT_VREPLACE
8700 /*
8701 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8702 * backspacing over the changed spacing and then inserting the new
8703 * spacing.
8704 */
8705 if (State & VREPLACE_FLAG)
8706 {
8707 /* Backspace from real cursor to change_col */
8708 backspace_until_column(change_col);
8709
8710 /* Insert each char in saved_line from changed_col to
8711 * ptr-cursor */
8712 ins_bytes_len(saved_line + change_col,
8713 cursor->col - change_col);
8714 }
8715#endif
8716 }
8717
8718#ifdef FEAT_VREPLACE
8719 if (State & VREPLACE_FLAG)
8720 vim_free(saved_line);
8721#endif
8722 curwin->w_p_list = save_list;
8723 }
8724
8725 return FALSE;
8726}
8727
8728/*
8729 * Handle CR or NL in insert mode.
8730 * Return TRUE when out of memory or can't undo.
8731 */
8732 static int
8733ins_eol(c)
8734 int c;
8735{
8736 int i;
8737
8738 if (echeck_abbr(c + ABBR_OFF))
8739 return FALSE;
8740 if (stop_arrow() == FAIL)
8741 return TRUE;
8742 undisplay_dollar();
8743
8744 /*
8745 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8746 * character under the cursor. Only push a NUL on the replace stack,
8747 * nothing to put back when the NL is deleted.
8748 */
8749 if ((State & REPLACE_FLAG)
8750#ifdef FEAT_VREPLACE
8751 && !(State & VREPLACE_FLAG)
8752#endif
8753 )
8754 replace_push(NUL);
8755
8756 /*
8757 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8758 * replacing the next line, so we push all of the characters left on the
8759 * line onto the replace stack. This is not done here though, it is done
8760 * in open_line().
8761 */
8762
8763#ifdef FEAT_RIGHTLEFT
8764# ifdef FEAT_FKMAP
8765 if (p_altkeymap && p_fkmap)
8766 fkmap(NL);
8767# endif
8768 /* NL in reverse insert will always start in the end of
8769 * current line. */
8770 if (revins_on)
8771 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8772#endif
8773
8774 AppendToRedobuff(NL_STR);
8775 i = open_line(FORWARD,
8776#ifdef FEAT_COMMENTS
8777 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8778#endif
8779 0, old_indent);
8780 old_indent = 0;
8781#ifdef FEAT_CINDENT
8782 can_cindent = TRUE;
8783#endif
8784
8785 return (!i);
8786}
8787
8788#ifdef FEAT_DIGRAPHS
8789/*
8790 * Handle digraph in insert mode.
8791 * Returns character still to be inserted, or NUL when nothing remaining to be
8792 * done.
8793 */
8794 static int
8795ins_digraph()
8796{
8797 int c;
8798 int cc;
8799
8800 pc_status = PC_STATUS_UNSET;
8801 if (redrawing() && !char_avail())
8802 {
8803 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008804 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805
8806 edit_putchar('?', TRUE);
8807#ifdef FEAT_CMDL_INFO
8808 add_to_showcmd_c(Ctrl_K);
8809#endif
8810 }
8811
8812#ifdef USE_ON_FLY_SCROLL
8813 dont_scroll = TRUE; /* disallow scrolling here */
8814#endif
8815
8816 /* don't map the digraph chars. This also prevents the
8817 * mode message to be deleted when ESC is hit */
8818 ++no_mapping;
8819 ++allow_keys;
8820 c = safe_vgetc();
8821 --no_mapping;
8822 --allow_keys;
8823 if (IS_SPECIAL(c) || mod_mask) /* special key */
8824 {
8825#ifdef FEAT_CMDL_INFO
8826 clear_showcmd();
8827#endif
8828 insert_special(c, TRUE, FALSE);
8829 return NUL;
8830 }
8831 if (c != ESC)
8832 {
8833 if (redrawing() && !char_avail())
8834 {
8835 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008836 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837
8838 if (char2cells(c) == 1)
8839 {
8840 /* first remove the '?', otherwise it's restored when typing
8841 * an ESC next */
8842 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008843 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 edit_putchar(c, TRUE);
8845 }
8846#ifdef FEAT_CMDL_INFO
8847 add_to_showcmd_c(c);
8848#endif
8849 }
8850 ++no_mapping;
8851 ++allow_keys;
8852 cc = safe_vgetc();
8853 --no_mapping;
8854 --allow_keys;
8855 if (cc != ESC)
8856 {
8857 AppendToRedobuff((char_u *)CTRL_V_STR);
8858 c = getdigraph(c, cc, TRUE);
8859#ifdef FEAT_CMDL_INFO
8860 clear_showcmd();
8861#endif
8862 return c;
8863 }
8864 }
8865 edit_unputchar();
8866#ifdef FEAT_CMDL_INFO
8867 clear_showcmd();
8868#endif
8869 return NUL;
8870}
8871#endif
8872
8873/*
8874 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8875 * Returns the char to be inserted, or NUL if none found.
8876 */
8877 static int
8878ins_copychar(lnum)
8879 linenr_T lnum;
8880{
8881 int c;
8882 int temp;
8883 char_u *ptr, *prev_ptr;
8884
8885 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8886 {
8887 vim_beep();
8888 return NUL;
8889 }
8890
8891 /* try to advance to the cursor column */
8892 temp = 0;
8893 ptr = ml_get(lnum);
8894 prev_ptr = ptr;
8895 validate_virtcol();
8896 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8897 {
8898 prev_ptr = ptr;
8899 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8900 }
8901 if ((colnr_T)temp > curwin->w_virtcol)
8902 ptr = prev_ptr;
8903
8904#ifdef FEAT_MBYTE
8905 c = (*mb_ptr2char)(ptr);
8906#else
8907 c = *ptr;
8908#endif
8909 if (c == NUL)
8910 vim_beep();
8911 return c;
8912}
8913
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008914/*
8915 * CTRL-Y or CTRL-E typed in Insert mode.
8916 */
8917 static int
8918ins_ctrl_ey(tc)
8919 int tc;
8920{
8921 int c = tc;
8922
8923#ifdef FEAT_INS_EXPAND
8924 if (ctrl_x_mode == CTRL_X_SCROLL)
8925 {
8926 if (c == Ctrl_Y)
8927 scrolldown_clamp();
8928 else
8929 scrollup_clamp();
8930 redraw_later(VALID);
8931 }
8932 else
8933#endif
8934 {
8935 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8936 if (c != NUL)
8937 {
8938 long tw_save;
8939
8940 /* The character must be taken literally, insert like it
8941 * was typed after a CTRL-V, and pretend 'textwidth'
8942 * wasn't set. Digits, 'o' and 'x' are special after a
8943 * CTRL-V, don't use it for these. */
8944 if (c < 256 && !isalnum(c))
8945 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8946 tw_save = curbuf->b_p_tw;
8947 curbuf->b_p_tw = -1;
8948 insert_special(c, TRUE, FALSE);
8949 curbuf->b_p_tw = tw_save;
8950#ifdef FEAT_RIGHTLEFT
8951 revins_chars++;
8952 revins_legal++;
8953#endif
8954 c = Ctrl_V; /* pretend CTRL-V is last character */
8955 auto_format(FALSE, TRUE);
8956 }
8957 }
8958 return c;
8959}
8960
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961#ifdef FEAT_SMARTINDENT
8962/*
8963 * Try to do some very smart auto-indenting.
8964 * Used when inserting a "normal" character.
8965 */
8966 static void
8967ins_try_si(c)
8968 int c;
8969{
8970 pos_T *pos, old_pos;
8971 char_u *ptr;
8972 int i;
8973 int temp;
8974
8975 /*
8976 * do some very smart indenting when entering '{' or '}'
8977 */
8978 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8979 {
8980 /*
8981 * for '}' set indent equal to indent of line containing matching '{'
8982 */
8983 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8984 {
8985 old_pos = curwin->w_cursor;
8986 /*
8987 * If the matching '{' has a ')' immediately before it (ignoring
8988 * white-space), then line up with the start of the line
8989 * containing the matching '(' if there is one. This handles the
8990 * case where an "if (..\n..) {" statement continues over multiple
8991 * lines -- webb
8992 */
8993 ptr = ml_get(pos->lnum);
8994 i = pos->col;
8995 if (i > 0) /* skip blanks before '{' */
8996 while (--i > 0 && vim_iswhite(ptr[i]))
8997 ;
8998 curwin->w_cursor.lnum = pos->lnum;
8999 curwin->w_cursor.col = i;
9000 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9001 curwin->w_cursor = *pos;
9002 i = get_indent();
9003 curwin->w_cursor = old_pos;
9004#ifdef FEAT_VREPLACE
9005 if (State & VREPLACE_FLAG)
9006 change_indent(INDENT_SET, i, FALSE, NUL);
9007 else
9008#endif
9009 (void)set_indent(i, SIN_CHANGED);
9010 }
9011 else if (curwin->w_cursor.col > 0)
9012 {
9013 /*
9014 * when inserting '{' after "O" reduce indent, but not
9015 * more than indent of previous line
9016 */
9017 temp = TRUE;
9018 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9019 {
9020 old_pos = curwin->w_cursor;
9021 i = get_indent();
9022 while (curwin->w_cursor.lnum > 1)
9023 {
9024 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9025
9026 /* ignore empty lines and lines starting with '#'. */
9027 if (*ptr != '#' && *ptr != NUL)
9028 break;
9029 }
9030 if (get_indent() >= i)
9031 temp = FALSE;
9032 curwin->w_cursor = old_pos;
9033 }
9034 if (temp)
9035 shift_line(TRUE, FALSE, 1);
9036 }
9037 }
9038
9039 /*
9040 * set indent of '#' always to 0
9041 */
9042 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9043 {
9044 /* remember current indent for next line */
9045 old_indent = get_indent();
9046 (void)set_indent(0, SIN_CHANGED);
9047 }
9048
9049 /* Adjust ai_col, the char at this position can be deleted. */
9050 if (ai_col > curwin->w_cursor.col)
9051 ai_col = curwin->w_cursor.col;
9052}
9053#endif
9054
9055/*
9056 * Get the value that w_virtcol would have when 'list' is off.
9057 * Unless 'cpo' contains the 'L' flag.
9058 */
9059 static colnr_T
9060get_nolist_virtcol()
9061{
9062 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9063 return getvcol_nolist(&curwin->w_cursor);
9064 validate_virtcol();
9065 return curwin->w_virtcol;
9066}