blob: f3c61996f84cd11a9c263719cf1a9063d5d27be9 [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 Moolenaar217ad922005-03-20 22:37:15 +0000172#ifdef FEAT_SYN_HL
173static 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
1914 && !curwin->w_p_spell)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001915 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1916 {
1917 ctrl_x_mode = 0;
1918 edit_submode = NULL;
1919 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1920 : (char_u *)_("'thesaurus' option is empty"),
1921 hl_attr(HLF_E));
1922 if (emsg_silent == 0)
1923 {
1924 vim_beep();
1925 setcursor();
1926 out_flush();
1927 ui_delay(2000L, FALSE);
1928 }
1929 return FALSE;
1930 }
1931 return TRUE;
1932}
1933
1934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1936 * This depends on the current mode.
1937 */
1938 int
1939vim_is_ctrl_x_key(c)
1940 int c;
1941{
1942 /* Always allow ^R - let it's results then be checked */
1943 if (c == Ctrl_R)
1944 return TRUE;
1945
Bram Moolenaare3226be2005-12-18 22:10:00 +00001946 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001947 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001948 return TRUE;
1949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 switch (ctrl_x_mode)
1951 {
1952 case 0: /* Not in any CTRL-X mode */
1953 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1954 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001955 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1957 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1958 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001959 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1960 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 case CTRL_X_SCROLL:
1962 return (c == Ctrl_Y || c == Ctrl_E);
1963 case CTRL_X_WHOLE_LINE:
1964 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1965 case CTRL_X_FILES:
1966 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1967 case CTRL_X_DICTIONARY:
1968 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1969 case CTRL_X_THESAURUS:
1970 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1971 case CTRL_X_TAGS:
1972 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1973#ifdef FEAT_FIND_ID
1974 case CTRL_X_PATH_PATTERNS:
1975 return (c == Ctrl_P || c == Ctrl_N);
1976 case CTRL_X_PATH_DEFINES:
1977 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1978#endif
1979 case CTRL_X_CMDLINE:
1980 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1981 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001982#ifdef FEAT_COMPL_FUNC
1983 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001984 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001985 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001986 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001987#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001988 case CTRL_X_SPELL:
1989 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
1991 EMSG(_(e_internal));
1992 return FALSE;
1993}
1994
1995/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001996 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 * case of the originally typed text is used, and the case of the completed
1998 * text is infered, ie this tries to work out what case you probably wanted
1999 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002000 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 */
2002 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002003ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 char_u *str;
2005 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002006 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 char_u *fname;
2008 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002009 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010{
2011 int has_lower = FALSE;
2012 int was_letter = FALSE;
2013 int idx;
2014
2015 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2016 {
2017 /* Infer case of completed part -- webb */
2018 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002019 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002022 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002024 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
2026 has_lower = TRUE;
2027 if (isupper(IObuff[idx]))
2028 {
2029 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002030 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2032 break;
2033 }
2034 }
2035 }
2036
2037 /*
2038 * Rule 2: No lower case, 2nd consecutive letter converted to
2039 * upper case.
2040 */
2041 if (!has_lower)
2042 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002043 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002045 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 && islower(IObuff[idx]))
2047 {
2048 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002049 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2051 break;
2052 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002053 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 }
2055 }
2056
2057 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002058 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059
Bram Moolenaard289f132006-03-11 21:30:53 +00002060 return ins_compl_add(IObuff, len, icase, fname, NULL, NULL, NULL,
2061 dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 }
Bram Moolenaard289f132006-03-11 21:30:53 +00002063 return ins_compl_add(str, len, icase, fname, NULL, NULL, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064}
2065
2066/*
2067 * Add a match to the list of matches.
2068 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002069 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002070 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002072 int
Bram Moolenaard289f132006-03-11 21:30:53 +00002073ins_compl_add(str, len, icase, fname, kind, extra, info, cdir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 char_u *str;
2075 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002076 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 char_u *fname;
Bram Moolenaard289f132006-03-11 21:30:53 +00002078 char_u *kind; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002079 char_u *extra; /* extra text for popup menu or NULL */
Bram Moolenaard289f132006-03-11 21:30:53 +00002080 char_u *info; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002081 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002082 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002084 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002085 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
2087 ui_breakcheck();
2088 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002089 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if (len < 0)
2091 len = (int)STRLEN(str);
2092
2093 /*
2094 * If the same match is already present, don't add it.
2095 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002096 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002098 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 do
2100 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002101 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002102 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002103 && match->cp_str[len] == NUL)
2104 return NOTDONE;
2105 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002106 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
2108
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002109 /* Remove any popup menu before changing the list of matches. */
2110 ins_compl_del_pum();
2111
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 /*
2113 * Allocate a new match structure.
2114 * Copy the values to the new match structure.
2115 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002116 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002118 return FAIL;
2119 match->cp_number = -1;
2120 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002121 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002122 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 {
2124 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002125 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002127 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002130 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2132 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002133 if (fname != NULL
2134 && compl_curr_match
2135 && compl_curr_match->cp_fname != NULL
2136 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002137 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002138 else if (fname != NULL)
2139 {
2140 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002141 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002144 match->cp_fname = NULL;
2145 match->cp_flags = flags;
Bram Moolenaard289f132006-03-11 21:30:53 +00002146 if (kind != NULL)
2147 match->cp_kind = vim_strsave(kind);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002148 if (extra != NULL)
2149 match->cp_extra = vim_strsave(extra);
Bram Moolenaard289f132006-03-11 21:30:53 +00002150 if (info != NULL)
2151 match->cp_info = vim_strsave(info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152
2153 /*
2154 * Link the new match structure in the list of matches.
2155 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002156 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002157 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 else if (dir == FORWARD)
2159 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002160 match->cp_next = compl_curr_match->cp_next;
2161 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 }
2163 else /* BACKWARD */
2164 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002165 match->cp_next = compl_curr_match;
2166 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002168 if (match->cp_next)
2169 match->cp_next->cp_prev = match;
2170 if (match->cp_prev)
2171 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002173 compl_first_match = match;
2174 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002176 /*
2177 * Find the longest common string if still doing that.
2178 */
2179 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2180 ins_compl_longest_match(match);
2181
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 return OK;
2183}
2184
2185/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002186 * Return TRUE if "str[len]" matches with match->cp_str, considering
2187 * match->cp_icase.
2188 */
2189 static int
2190ins_compl_equal(match, str, len)
2191 compl_T *match;
2192 char_u *str;
2193 int len;
2194{
2195 if (match->cp_icase)
2196 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2197 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2198}
2199
2200/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002201 * Reduce the longest common string for match "match".
2202 */
2203 static void
2204ins_compl_longest_match(match)
2205 compl_T *match;
2206{
2207 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002208 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002209 int had_match;
2210
2211 if (compl_leader == NULL)
2212 /* First match, use it as a whole. */
2213 compl_leader = vim_strsave(match->cp_str);
2214 else
2215 {
2216 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002217 p = compl_leader;
2218 s = match->cp_str;
2219 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002220 {
2221#ifdef FEAT_MBYTE
2222 if (has_mbyte)
2223 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002224 c1 = mb_ptr2char(p);
2225 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002226 }
2227 else
2228#endif
2229 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002230 c1 = *p;
2231 c2 = *s;
2232 }
2233 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2234 : (c1 != c2))
2235 break;
2236#ifdef FEAT_MBYTE
2237 if (has_mbyte)
2238 {
2239 mb_ptr_adv(p);
2240 mb_ptr_adv(s);
2241 }
2242 else
2243#endif
2244 {
2245 ++p;
2246 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002247 }
2248 }
2249
2250 if (*p != NUL)
2251 {
2252 /* Leader was shortened, need to change the inserted text. */
2253 *p = NUL;
2254 had_match = (curwin->w_cursor.col > compl_col);
2255 ins_compl_delete();
2256 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2257 ins_redraw(FALSE);
2258
2259 /* When the match isn't there (to avoid matching itself) remove it
2260 * again after redrawing. */
2261 if (!had_match)
2262 ins_compl_delete();
2263 }
2264
2265 compl_used_match = FALSE;
2266 }
2267}
2268
2269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 * Add an array of matches to the list of matches.
2271 * Frees matches[].
2272 */
2273 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002274ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 int num_matches;
2276 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002277 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278{
2279 int i;
2280 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002281 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
Bram Moolenaar572cb562005-08-05 21:35:02 +00002283 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002284 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaard289f132006-03-11 21:30:53 +00002285 NULL, NULL, NULL, NULL, dir, 0)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002287 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 FreeWild(num_matches, matches);
2289}
2290
2291/* Make the completion list cyclic.
2292 * Return the number of matches (excluding the original).
2293 */
2294 static int
2295ins_compl_make_cyclic()
2296{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002297 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 int count = 0;
2299
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002300 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
2302 /*
2303 * Find the end of the list.
2304 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002305 match = compl_first_match;
2306 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002307 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002309 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 ++count;
2311 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002312 match->cp_next = compl_first_match;
2313 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
2315 return count;
2316}
2317
Bram Moolenaara94bc432006-03-10 21:42:59 +00002318/*
2319 * Start completion for the complete() function.
2320 * "startcol" is where the matched text starts (1 is first column).
2321 * "list" is the list of matches.
2322 */
2323 void
2324set_completion(startcol, list)
2325 int startcol;
2326 list_T *list;
2327{
2328 /* If already doing completions stop it. */
2329 if (ctrl_x_mode != 0)
2330 ins_compl_prep(' ');
2331 ins_compl_clear();
2332
2333 if (stop_arrow() == FAIL)
2334 return;
2335
2336 if (startcol > curwin->w_cursor.col)
2337 startcol = curwin->w_cursor.col;
2338 compl_col = startcol;
2339 compl_length = curwin->w_cursor.col - startcol;
2340 /* compl_pattern doesn't need to be set */
2341 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2342 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaard289f132006-03-11 21:30:53 +00002343 -1, FALSE, NULL, NULL, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002344 return;
2345
2346 /* Handle like dictionary completion. */
2347 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2348
2349 ins_compl_add_list(list);
2350 compl_matches = ins_compl_make_cyclic();
2351 compl_started = TRUE;
2352 compl_used_match = TRUE;
2353
2354 compl_curr_match = compl_first_match;
2355 ins_complete(Ctrl_N);
2356 out_flush();
2357}
2358
2359
Bram Moolenaar9372a112005-12-06 19:59:18 +00002360/* "compl_match_array" points the currently displayed list of entries in the
2361 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002362static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002363static int compl_match_arraysize;
2364
2365/*
2366 * Update the screen and when there is any scrolling remove the popup menu.
2367 */
2368 static void
2369ins_compl_upd_pum()
2370{
2371 int h;
2372
2373 if (compl_match_array != NULL)
2374 {
2375 h = curwin->w_cline_height;
2376 update_screen(0);
2377 if (h != curwin->w_cline_height)
2378 ins_compl_del_pum();
2379 }
2380}
2381
2382/*
2383 * Remove any popup menu.
2384 */
2385 static void
2386ins_compl_del_pum()
2387{
2388 if (compl_match_array != NULL)
2389 {
2390 pum_undisplay();
2391 vim_free(compl_match_array);
2392 compl_match_array = NULL;
2393 }
2394}
2395
2396/*
2397 * Return TRUE if the popup menu should be displayed.
2398 */
2399 static int
2400pum_wanted()
2401{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002402 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002403 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002404 return FALSE;
2405
2406 /* The display looks bad on a B&W display. */
2407 if (t_colors < 8
2408#ifdef FEAT_GUI
2409 && !gui.in_use
2410#endif
2411 )
2412 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002413 return TRUE;
2414}
2415
2416/*
2417 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002418 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002419 */
2420 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002421pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002422{
2423 compl_T *compl;
2424 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002425
2426 /* Don't display the popup menu if there are no matches or there is only
2427 * one (ignoring the original text). */
2428 compl = compl_first_match;
2429 i = 0;
2430 do
2431 {
2432 if (compl == NULL
2433 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2434 break;
2435 compl = compl->cp_next;
2436 } while (compl != compl_first_match);
2437
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002438 if (strstr((char *)p_cot, "menuone") != NULL)
2439 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002440 return (i >= 2);
2441}
2442
2443/*
2444 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002445 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002446 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002447 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002448ins_compl_show_pum()
2449{
2450 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002451 compl_T *shown_compl = NULL;
2452 int did_find_shown_match = FALSE;
2453 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002454 int i;
2455 int cur = -1;
2456 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002457 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002458
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002459 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002460 return;
2461
2462 /* Update the screen before drawing the popup menu over it. */
2463 update_screen(0);
2464
2465 if (compl_match_array == NULL)
2466 {
2467 /* Need to build the popup menu list. */
2468 compl_match_arraysize = 0;
2469 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002470 if (compl_leader != NULL)
2471 lead_len = STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002472 do
2473 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002474 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2475 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002476 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002477 ++compl_match_arraysize;
2478 compl = compl->cp_next;
2479 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002480 if (compl_match_arraysize == 0)
2481 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002482 compl_match_array = (pumitem_T *)alloc_clear(
2483 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002484 * compl_match_arraysize));
2485 if (compl_match_array != NULL)
2486 {
2487 i = 0;
2488 compl = compl_first_match;
2489 do
2490 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002491 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2492 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002493 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002494 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002495 if (!shown_match_ok)
2496 {
2497 if (compl == compl_shown_match || did_find_shown_match)
2498 {
2499 /* This item is the shown match or this is the
2500 * first displayed item after the shown match. */
2501 compl_shown_match = compl;
2502 did_find_shown_match = TRUE;
2503 shown_match_ok = TRUE;
2504 }
2505 else
2506 /* Remember this displayed match for when the
2507 * shown match is just below it. */
2508 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002509 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002510 }
2511 compl_match_array[i].pum_text = compl->cp_str;
Bram Moolenaard289f132006-03-11 21:30:53 +00002512 if (compl->cp_kind != NULL)
2513 compl_match_array[i].pum_kind = compl->cp_kind;
2514 if (compl->cp_info != NULL)
2515 compl_match_array[i].pum_info = compl->cp_info;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002516 if (compl->cp_extra != NULL)
2517 compl_match_array[i++].pum_extra = compl->cp_extra;
2518 else
2519 compl_match_array[i++].pum_extra = compl->cp_fname;
2520 }
2521
2522 if (compl == compl_shown_match)
2523 {
2524 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002525
2526 /* When the original text is the shown match don't set
2527 * compl_shown_match. */
2528 if (compl->cp_flags & ORIGINAL_TEXT)
2529 shown_match_ok = TRUE;
2530
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002531 if (!shown_match_ok && shown_compl != NULL)
2532 {
2533 /* The shown match isn't displayed, set it to the
2534 * previously displayed match. */
2535 compl_shown_match = shown_compl;
2536 shown_match_ok = TRUE;
2537 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002538 }
2539 compl = compl->cp_next;
2540 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002541
2542 if (!shown_match_ok) /* no displayed match at all */
2543 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002544 }
2545 }
2546 else
2547 {
2548 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002549 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002550 if (compl_match_array[i].pum_text == compl_shown_match->cp_str)
Bram Moolenaara6557602006-02-04 22:43:20 +00002551 break;
2552 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002553 }
2554
2555 if (compl_match_array != NULL)
2556 {
2557 /* Compute the screen column of the start of the completed text.
2558 * Use the cursor to get all wrapping and other settings right. */
2559 col = curwin->w_cursor.col;
2560 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002561 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002562 curwin->w_cursor.col = col;
2563 }
2564}
2565
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566#define DICT_FIRST (1) /* use just first element in "dict" */
2567#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002570 * Add any identifiers that match the given pattern in the list of dictionary
2571 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 */
2573 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002574ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2575 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002577 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002578 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002580 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 char_u *ptr;
2582 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 char_u **files;
2585 int count;
2586 int i;
2587 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002588 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
Bram Moolenaar0b238792006-03-02 22:49:12 +00002590 if (*dict == NUL)
2591 {
2592#ifdef FEAT_SYN_HL
2593 /* When 'dictionary' is empty and spell checking is enabled use
2594 * "spell". */
2595 if (!thesaurus && curwin->w_p_spell)
2596 dict = (char_u *)"spell";
2597 else
2598#endif
2599 return;
2600 }
2601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002603 if (buf == NULL)
2604 return;
2605
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 /* If 'infercase' is set, don't use 'smartcase' here */
2607 save_p_scs = p_scs;
2608 if (curbuf->b_p_inf)
2609 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002610
2611 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2612 * to only match at the start of a line. Otherwise just match the
2613 * pattern. */
2614 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2615 {
2616 i = STRLEN(pat) + 8;
2617 ptr = alloc(i);
2618 if (ptr == NULL)
2619 return;
2620 vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat);
2621 regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0);
2622 vim_free(ptr);
2623 }
2624 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002625 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002626 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002627 if (regmatch.regprog == NULL)
2628 goto theend;
2629 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002630
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2632 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002633 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 {
2635 /* copy one dictionary file name into buf */
2636 if (flags == DICT_EXACT)
2637 {
2638 count = 1;
2639 files = &dict;
2640 }
2641 else
2642 {
2643 /* Expand wildcards in the dictionary name, but do not allow
2644 * backticks (for security, the 'dict' option may have been set in
2645 * a modeline). */
2646 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaar0b238792006-03-02 22:49:12 +00002647 if (!thesaurus && STRCMP(buf, "spell") == 0)
2648 count = -1;
2649 else if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 || expand_wildcards(1, &buf, &count, &files,
2651 EW_FILE|EW_SILENT) != OK)
2652 count = 0;
2653 }
2654
Bram Moolenaar0b238792006-03-02 22:49:12 +00002655 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002657 /* Complete from active spelling. Skip "\<" in the pattern, we
2658 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002659 if (pat[0] == '\\' && pat[1] == '<')
2660 ptr = pat + 2;
2661 else
2662 ptr = pat;
2663 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002665 else
2666 {
2667 ins_compl_files(count, files, thesaurus, flags,
2668 &regmatch, buf, &dir);
2669 if (flags != DICT_EXACT)
2670 FreeWild(count, files);
2671 }
2672 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 break;
2674 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002675
2676theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 p_scs = save_p_scs;
2678 vim_free(regmatch.regprog);
2679 vim_free(buf);
2680}
2681
Bram Moolenaar0b238792006-03-02 22:49:12 +00002682 static void
2683ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2684 int count;
2685 char_u **files;
2686 int thesaurus;
2687 int flags;
2688 regmatch_T *regmatch;
2689 char_u *buf;
2690 int *dir;
2691{
2692 char_u *ptr;
2693 int i;
2694 FILE *fp;
2695 int add_r;
2696
2697 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2698 {
2699 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2700 if (flags != DICT_EXACT)
2701 {
2702 vim_snprintf((char *)IObuff, IOSIZE,
2703 _("Scanning dictionary: %s"), (char *)files[i]);
2704 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2705 }
2706
2707 if (fp != NULL)
2708 {
2709 /*
2710 * Read dictionary file line by line.
2711 * Check each line for a match.
2712 */
2713 while (!got_int && !compl_interrupted
2714 && !vim_fgets(buf, LSIZE, fp))
2715 {
2716 ptr = buf;
2717 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2718 {
2719 ptr = regmatch->startp[0];
2720 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2721 ptr = find_line_end(ptr);
2722 else
2723 ptr = find_word_end(ptr);
2724 add_r = ins_compl_add_infercase(regmatch->startp[0],
2725 (int)(ptr - regmatch->startp[0]),
Bram Moolenaard289f132006-03-11 21:30:53 +00002726 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002727 if (thesaurus)
2728 {
2729 char_u *wstart;
2730
2731 /*
2732 * Add the other matches on the line
2733 */
2734 while (!got_int)
2735 {
2736 /* Find start of the next word. Skip white
2737 * space and punctuation. */
2738 ptr = find_word_start(ptr);
2739 if (*ptr == NUL || *ptr == NL)
2740 break;
2741 wstart = ptr;
2742
2743 /* Find end of the word and add it. */
2744#ifdef FEAT_MBYTE
2745 if (has_mbyte)
2746 /* Japanese words may have characters in
2747 * different classes, only separate words
2748 * with single-byte non-word characters. */
2749 while (*ptr != NUL)
2750 {
2751 int l = (*mb_ptr2len)(ptr);
2752
2753 if (l < 2 && !vim_iswordc(*ptr))
2754 break;
2755 ptr += l;
2756 }
2757 else
2758#endif
2759 ptr = find_word_end(ptr);
2760 add_r = ins_compl_add_infercase(wstart,
2761 (int)(ptr - wstart),
2762 p_ic, files[i], *dir, 0);
2763 }
2764 }
2765 if (add_r == OK)
2766 /* if dir was BACKWARD then honor it just once */
2767 *dir = FORWARD;
2768 else if (add_r == FAIL)
2769 break;
2770 /* avoid expensive call to vim_regexec() when at end
2771 * of line */
2772 if (*ptr == '\n' || got_int)
2773 break;
2774 }
2775 line_breakcheck();
2776 ins_compl_check_keys(50);
2777 }
2778 fclose(fp);
2779 }
2780 }
2781}
2782
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783/*
2784 * Find the start of the next word.
2785 * Returns a pointer to the first char of the word. Also stops at a NUL.
2786 */
2787 char_u *
2788find_word_start(ptr)
2789 char_u *ptr;
2790{
2791#ifdef FEAT_MBYTE
2792 if (has_mbyte)
2793 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002794 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 else
2796#endif
2797 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2798 ++ptr;
2799 return ptr;
2800}
2801
2802/*
2803 * Find the end of the word. Assumes it starts inside a word.
2804 * Returns a pointer to just after the word.
2805 */
2806 char_u *
2807find_word_end(ptr)
2808 char_u *ptr;
2809{
2810#ifdef FEAT_MBYTE
2811 int start_class;
2812
2813 if (has_mbyte)
2814 {
2815 start_class = mb_get_class(ptr);
2816 if (start_class > 1)
2817 while (*ptr != NUL)
2818 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002819 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 if (mb_get_class(ptr) != start_class)
2821 break;
2822 }
2823 }
2824 else
2825#endif
2826 while (vim_iswordc(*ptr))
2827 ++ptr;
2828 return ptr;
2829}
2830
2831/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002832 * Find the end of the line, omitting CR and NL at the end.
2833 * Returns a pointer to just after the line.
2834 */
2835 static char_u *
2836find_line_end(ptr)
2837 char_u *ptr;
2838{
2839 char_u *s;
2840
2841 s = ptr + STRLEN(ptr);
2842 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2843 --s;
2844 return s;
2845}
2846
2847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 * Free the list of completions
2849 */
2850 static void
2851ins_compl_free()
2852{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002853 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002855 vim_free(compl_pattern);
2856 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002857 vim_free(compl_leader);
2858 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002860 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002862
2863 ins_compl_del_pum();
2864 pum_clear();
2865
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002866 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 do
2868 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002869 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002870 compl_curr_match = compl_curr_match->cp_next;
2871 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002873 if (match->cp_flags & FREE_FNAME)
2874 vim_free(match->cp_fname);
Bram Moolenaard289f132006-03-11 21:30:53 +00002875 vim_free(match->cp_kind);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002876 vim_free(match->cp_extra);
Bram Moolenaard289f132006-03-11 21:30:53 +00002877 vim_free(match->cp_info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002879 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2880 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881}
2882
2883 static void
2884ins_compl_clear()
2885{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002886 compl_cont_status = 0;
2887 compl_started = FALSE;
2888 compl_matches = 0;
2889 vim_free(compl_pattern);
2890 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002891 vim_free(compl_leader);
2892 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002894 vim_free(compl_orig_text);
2895 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896}
2897
2898/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002899 * Return TRUE when Insert completion is active.
2900 */
2901 int
2902ins_compl_active()
2903{
2904 return compl_started;
2905}
2906
2907/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002908 * Delete one character before the cursor and show the subset of the matches
2909 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002910 * Returns TRUE if the work is done and another char to be got from the user.
2911 */
2912 static int
2913ins_compl_bs()
2914{
2915 char_u *line;
2916 char_u *p;
2917
2918 if (curwin->w_cursor.col <= compl_col + compl_length)
2919 {
2920 /* Deleted more than what was used to find matches, need to look for
2921 * matches all over again. */
2922 ins_compl_free();
2923 compl_started = FALSE;
2924 compl_matches = 0;
2925 }
2926
2927 line = ml_get_curline();
2928 p = line + curwin->w_cursor.col;
2929 mb_ptr_back(line, p);
2930
2931 vim_free(compl_leader);
2932 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2933 if (compl_leader != NULL)
2934 {
2935 ins_compl_del_pum();
2936 ins_compl_delete();
2937 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2938
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002939 if (compl_started)
2940 ins_compl_set_original_text(compl_leader);
2941 else
Bram Moolenaara6557602006-02-04 22:43:20 +00002942 {
2943 /* Matches were cleared, need to search for them now. */
2944 if (ins_complete(Ctrl_N) == FAIL)
2945 compl_cont_status = 0;
2946 else
2947 {
2948 /* Remove the completed word again. */
2949 ins_compl_delete();
2950 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2951 }
2952 }
2953
2954 /* Show the popup menu with a different set of matches. */
2955 ins_compl_show_pum();
2956 compl_used_match = FALSE;
2957
2958 return TRUE;
2959 }
2960 return FALSE;
2961}
2962
2963/*
2964 * Append one character to the match leader. May reduce the number of
2965 * matches.
2966 */
2967 static void
2968ins_compl_addleader(c)
2969 int c;
2970{
2971#ifdef FEAT_MBYTE
2972 int cc;
2973
2974 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2975 {
2976 char_u buf[MB_MAXBYTES + 1];
2977
2978 (*mb_char2bytes)(c, buf);
2979 buf[cc] = NUL;
2980 ins_char_bytes(buf, cc);
2981 }
2982 else
2983#endif
2984 ins_char(c);
2985
2986 vim_free(compl_leader);
2987 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
2988 curwin->w_cursor.col - compl_col);
2989 if (compl_leader != NULL)
2990 {
2991 /* Show the popup menu with a different set of matches. */
2992 ins_compl_del_pum();
2993 ins_compl_show_pum();
2994 compl_used_match = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002995 ins_compl_set_original_text(compl_leader);
2996 }
2997}
2998
2999/*
3000 * Set the first match, the original text.
3001 */
3002 static void
3003ins_compl_set_original_text(str)
3004 char_u *str;
3005{
3006 char_u *p;
3007
3008 /* Replace the original text entry. */
3009 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3010 {
3011 p = vim_strsave(str);
3012 if (p != NULL)
3013 {
3014 vim_free(compl_first_match->cp_str);
3015 compl_first_match->cp_str = p;
3016 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003017 }
3018}
3019
3020/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003021 * Append one character to the match leader. May reduce the number of
3022 * matches.
3023 */
3024 static void
3025ins_compl_addfrommatch()
3026{
3027 char_u *p;
3028 int len = curwin->w_cursor.col - compl_col;
3029 int c;
3030
3031 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003032 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003033 return;
3034 p += len;
3035#ifdef FEAT_MBYTE
3036 c = mb_ptr2char(p);
3037#else
3038 c = *p;
3039#endif
3040 ins_compl_addleader(c);
3041}
3042
3043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003045 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003046 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003048 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049ins_compl_prep(c)
3050 int c;
3051{
3052 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 int temp;
3054 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003055 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056
3057 /* Forget any previous 'special' messages if this is actually
3058 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3059 */
3060 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3061 edit_submode_extra = NULL;
3062
3063 /* Ignore end of Select mode mapping */
3064 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003065 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003067 /* Set "compl_get_longest" when finding the first matches. */
3068 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3069 || (ctrl_x_mode == 0 && !compl_started))
3070 {
3071 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3072 compl_used_match = TRUE;
3073 }
3074
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3076 {
3077 /*
3078 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3079 * it will be yet. Now we decide.
3080 */
3081 switch (c)
3082 {
3083 case Ctrl_E:
3084 case Ctrl_Y:
3085 ctrl_x_mode = CTRL_X_SCROLL;
3086 if (!(State & REPLACE_FLAG))
3087 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3088 else
3089 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3090 edit_submode_pre = NULL;
3091 showmode();
3092 break;
3093 case Ctrl_L:
3094 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3095 break;
3096 case Ctrl_F:
3097 ctrl_x_mode = CTRL_X_FILES;
3098 break;
3099 case Ctrl_K:
3100 ctrl_x_mode = CTRL_X_DICTIONARY;
3101 break;
3102 case Ctrl_R:
3103 /* Simply allow ^R to happen without affecting ^X mode */
3104 break;
3105 case Ctrl_T:
3106 ctrl_x_mode = CTRL_X_THESAURUS;
3107 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003108#ifdef FEAT_COMPL_FUNC
3109 case Ctrl_U:
3110 ctrl_x_mode = CTRL_X_FUNCTION;
3111 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003112 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003113 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003114 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003115#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003116 case 's':
3117 case Ctrl_S:
3118 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003119#ifdef FEAT_SYN_HL
3120 spell_back_to_badword();
3121#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003122 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 case Ctrl_RSB:
3124 ctrl_x_mode = CTRL_X_TAGS;
3125 break;
3126#ifdef FEAT_FIND_ID
3127 case Ctrl_I:
3128 case K_S_TAB:
3129 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3130 break;
3131 case Ctrl_D:
3132 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3133 break;
3134#endif
3135 case Ctrl_V:
3136 case Ctrl_Q:
3137 ctrl_x_mode = CTRL_X_CMDLINE;
3138 break;
3139 case Ctrl_P:
3140 case Ctrl_N:
3141 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3142 * just started ^X mode, or there were enough ^X's to cancel
3143 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3144 * do normal expansion when interrupting a different mode (say
3145 * ^X^F^X^P or ^P^X^X^P, see below)
3146 * nothing changes if interrupting mode 0, (eg, the flag
3147 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003148 if (!(compl_cont_status & CONT_INTRPT))
3149 compl_cont_status |= CONT_LOCAL;
3150 else if (compl_cont_mode != 0)
3151 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 /* FALLTHROUGH */
3153 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003154 /* If we have typed at least 2 ^X's... for modes != 0, we set
3155 * compl_cont_status = 0 (eg, as if we had just started ^X
3156 * mode).
3157 * For mode 0, we set "compl_cont_mode" to an impossible
3158 * value, in both cases ^X^X can be used to restart the same
3159 * mode (avoiding ADDING mode).
3160 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3161 * 'complete' and local ^P expansions respectively.
3162 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3163 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 if (c == Ctrl_X)
3165 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003166 if (compl_cont_mode != 0)
3167 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003169 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 }
3171 ctrl_x_mode = 0;
3172 edit_submode = NULL;
3173 showmode();
3174 break;
3175 }
3176 }
3177 else if (ctrl_x_mode != 0)
3178 {
3179 /* We're already in CTRL-X mode, do we stay in it? */
3180 if (!vim_is_ctrl_x_key(c))
3181 {
3182 if (ctrl_x_mode == CTRL_X_SCROLL)
3183 ctrl_x_mode = 0;
3184 else
3185 ctrl_x_mode = CTRL_X_FINISHED;
3186 edit_submode = NULL;
3187 }
3188 showmode();
3189 }
3190
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003191 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 {
3193 /* Show error message from attempted keyword completion (probably
3194 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003195 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003197 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3198 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 || ctrl_x_mode == CTRL_X_FINISHED)
3200 {
3201 /* Get here when we have finished typing a sequence of ^N and
3202 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003203 * memory that was used, and make sure we can redo the insert. */
3204 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003206 char_u *p;
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 /*
3209 * If any of the original typed text has been changed,
3210 * eg when ignorecase is set, we must add back-spaces to
3211 * the redo buffer. We add as few as necessary to delete
3212 * just the part of the original text that has changed.
3213 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003214 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003215 p = compl_orig_text;
3216 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003218 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 ++ptr;
3220 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003221 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00003223 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 }
3225
3226#ifdef FEAT_CINDENT
3227 want_cindent = (can_cindent && cindent_on());
3228#endif
3229 /*
3230 * When completing whole lines: fix indent for 'cindent'.
3231 * Otherwise, break line if it's too long.
3232 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003233 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 {
3235#ifdef FEAT_CINDENT
3236 /* re-indent the current line */
3237 if (want_cindent)
3238 {
3239 do_c_expr_indent();
3240 want_cindent = FALSE; /* don't do it again */
3241 }
3242#endif
3243 }
3244 else
3245 {
3246 /* put the cursor on the last char, for 'tw' formatting */
3247 curwin->w_cursor.col--;
3248 if (stop_arrow() == OK)
3249 insertchar(NUL, 0, -1);
3250 curwin->w_cursor.col++;
3251 }
3252
3253 auto_format(FALSE, TRUE);
3254
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003255 /* if the popup menu is displayed hitting Enter means accepting
3256 * the selection without inserting anything. */
3257 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
3258 retval = TRUE;
3259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003261 compl_started = FALSE;
3262 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 msg_clr_cmdline(); /* necessary for "noshowmode" */
3264 ctrl_x_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 if (edit_submode != NULL)
3266 {
3267 edit_submode = NULL;
3268 showmode();
3269 }
3270
3271#ifdef FEAT_CINDENT
3272 /*
3273 * Indent now if a key was typed that is in 'cinkeys'.
3274 */
3275 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3276 do_c_expr_indent();
3277#endif
3278 }
3279 }
3280
3281 /* reset continue_* if we left expansion-mode, if we stay they'll be
3282 * (re)set properly in ins_complete() */
3283 if (!vim_is_ctrl_x_key(c))
3284 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003285 compl_cont_status = 0;
3286 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003288
3289 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290}
3291
3292/*
3293 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3294 * (depending on flag) starting from buf and looking for a non-scanned
3295 * buffer (other than curbuf). curbuf is special, if it is called with
3296 * buf=curbuf then it has to be the first call for a given flag/expansion.
3297 *
3298 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3299 */
3300 static buf_T *
3301ins_compl_next_buf(buf, flag)
3302 buf_T *buf;
3303 int flag;
3304{
3305#ifdef FEAT_WINDOWS
3306 static win_T *wp;
3307#endif
3308
3309 if (flag == 'w') /* just windows */
3310 {
3311#ifdef FEAT_WINDOWS
3312 if (buf == curbuf) /* first call for this flag/expansion */
3313 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003314 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 && wp->w_buffer->b_scanned)
3316 ;
3317 buf = wp->w_buffer;
3318#else
3319 buf = curbuf;
3320#endif
3321 }
3322 else
3323 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3324 * (unlisted buffers)
3325 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003326 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 && ((flag == 'U'
3328 ? buf->b_p_bl
3329 : (!buf->b_p_bl
3330 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003331 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 ;
3333 return buf;
3334}
3335
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003336#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003337static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003338
3339/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003340 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003341 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003342 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003343 static void
3344expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003345 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003346 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003347{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003348 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003349 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003350 char_u *funcname;
3351 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003352
Bram Moolenaare344bea2005-09-01 20:46:49 +00003353 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3354 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003355 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003356
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003357 /* Call 'completefunc' to obtain the list of matches. */
3358 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003359 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003360
Bram Moolenaare344bea2005-09-01 20:46:49 +00003361 pos = curwin->w_cursor;
3362 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3363 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003364 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003365 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003366
Bram Moolenaara94bc432006-03-10 21:42:59 +00003367 ins_compl_add_list(matchlist);
3368 list_unref(matchlist);
3369}
3370#endif /* FEAT_COMPL_FUNC */
3371
3372#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
3373/*
3374 * Add completions from a list.
3375 * Unreferences the list.
3376 */
3377 static void
3378ins_compl_add_list(list)
3379 list_T *list;
3380{
3381 listitem_T *li;
3382 int icase;
3383 char_u *p;
3384 char_u *x;
Bram Moolenaard289f132006-03-11 21:30:53 +00003385 char_u *k;
3386 char_u *info;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003387 int dir = compl_direction;
3388
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003389 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003390 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003391 {
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003392 icase = p_ic;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003393 if (li->li_tv.v_type == VAR_DICT && li->li_tv.vval.v_dict != NULL)
3394 {
3395 p = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"word", FALSE);
3396 x = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"menu", FALSE);
Bram Moolenaard289f132006-03-11 21:30:53 +00003397 k = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"kind", FALSE);
3398 info = get_dict_string(li->li_tv.vval.v_dict,
3399 (char_u *)"info", FALSE);
3400 if (get_dict_string(li->li_tv.vval.v_dict,
3401 (char_u *)"icase", FALSE) != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003402 icase = get_dict_number(li->li_tv.vval.v_dict,
3403 (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003404 }
3405 else
3406 {
3407 p = get_tv_string_chk(&li->li_tv);
3408 x = NULL;
Bram Moolenaard289f132006-03-11 21:30:53 +00003409 k = NULL;
3410 info = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003411 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003412 if (p != NULL && *p != NUL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003413 {
Bram Moolenaard289f132006-03-11 21:30:53 +00003414 if (ins_compl_add(p, -1, icase, NULL, k, x, info, dir, 0) == OK)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003415 /* if dir was BACKWARD then honor it just once */
3416 dir = FORWARD;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003417 }
Bram Moolenaar280f1262006-01-30 00:14:18 +00003418 else if (did_emsg)
3419 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003420 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003421}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003422#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003423
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003424/*
3425 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003426 * The search starts at position "ini" in curbuf and in the direction
3427 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003428 * When "compl_started" is FALSE start at that position, otherwise continue
3429 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003430 * This may return before finding all the matches.
3431 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 */
3433 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003434ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
3437 static pos_T first_match_pos;
3438 static pos_T last_match_pos;
3439 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003440 static int found_all = FALSE; /* Found all matches of a
3441 certain type. */
3442 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaar572cb562005-08-05 21:35:02 +00003444 pos_T *pos;
3445 char_u **matches;
3446 int save_p_scs;
3447 int save_p_ws;
3448 int save_p_ic;
3449 int i;
3450 int num_matches;
3451 int len;
3452 int found_new_match;
3453 int type = ctrl_x_mode;
3454 char_u *ptr;
3455 char_u *dict = NULL;
3456 int dict_f = 0;
3457 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003459 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
3461 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3462 ins_buf->b_scanned = 0;
3463 found_all = FALSE;
3464 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003465 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003466 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 last_match_pos = first_match_pos = *ini;
3468 }
3469
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003470 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003471 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3473 for (;;)
3474 {
3475 found_new_match = FAIL;
3476
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003477 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 * or if found_all says this entry is done. For ^X^L only use the
3479 * entries from 'complete' that look in loaded buffers. */
3480 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003481 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
3483 found_all = FALSE;
3484 while (*e_cpt == ',' || *e_cpt == ' ')
3485 e_cpt++;
3486 if (*e_cpt == '.' && !curbuf->b_scanned)
3487 {
3488 ins_buf = curbuf;
3489 first_match_pos = *ini;
3490 /* So that ^N can match word immediately after cursor */
3491 if (ctrl_x_mode == 0)
3492 dec(&first_match_pos);
3493 last_match_pos = first_match_pos;
3494 type = 0;
3495 }
3496 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3497 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3498 {
3499 /* Scan a buffer, but not the current one. */
3500 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3501 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003502 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 first_match_pos.col = last_match_pos.col = 0;
3504 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3505 last_match_pos.lnum = 0;
3506 type = 0;
3507 }
3508 else /* unloaded buffer, scan like dictionary */
3509 {
3510 found_all = TRUE;
3511 if (ins_buf->b_fname == NULL)
3512 continue;
3513 type = CTRL_X_DICTIONARY;
3514 dict = ins_buf->b_fname;
3515 dict_f = DICT_EXACT;
3516 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003517 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 ins_buf->b_fname == NULL
3519 ? buf_spname(ins_buf)
3520 : ins_buf->b_sfname == NULL
3521 ? (char *)ins_buf->b_fname
3522 : (char *)ins_buf->b_sfname);
3523 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3524 }
3525 else if (*e_cpt == NUL)
3526 break;
3527 else
3528 {
3529 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3530 type = -1;
3531 else if (*e_cpt == 'k' || *e_cpt == 's')
3532 {
3533 if (*e_cpt == 'k')
3534 type = CTRL_X_DICTIONARY;
3535 else
3536 type = CTRL_X_THESAURUS;
3537 if (*++e_cpt != ',' && *e_cpt != NUL)
3538 {
3539 dict = e_cpt;
3540 dict_f = DICT_FIRST;
3541 }
3542 }
3543#ifdef FEAT_FIND_ID
3544 else if (*e_cpt == 'i')
3545 type = CTRL_X_PATH_PATTERNS;
3546 else if (*e_cpt == 'd')
3547 type = CTRL_X_PATH_DEFINES;
3548#endif
3549 else if (*e_cpt == ']' || *e_cpt == 't')
3550 {
3551 type = CTRL_X_TAGS;
3552 sprintf((char*)IObuff, _("Scanning tags."));
3553 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3554 }
3555 else
3556 type = -1;
3557
3558 /* in any case e_cpt is advanced to the next entry */
3559 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3560
3561 found_all = TRUE;
3562 if (type == -1)
3563 continue;
3564 }
3565 }
3566
3567 switch (type)
3568 {
3569 case -1:
3570 break;
3571#ifdef FEAT_FIND_ID
3572 case CTRL_X_PATH_PATTERNS:
3573 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003574 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003575 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003577 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3579 (linenr_T)1, (linenr_T)MAXLNUM);
3580 break;
3581#endif
3582
3583 case CTRL_X_DICTIONARY:
3584 case CTRL_X_THESAURUS:
3585 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003586 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 : (type == CTRL_X_THESAURUS
3588 ? (*curbuf->b_p_tsr == NUL
3589 ? p_tsr
3590 : curbuf->b_p_tsr)
3591 : (*curbuf->b_p_dict == NUL
3592 ? p_dict
3593 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003594 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003595 dict != NULL ? dict_f
3596 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 dict = NULL;
3598 break;
3599
3600 case CTRL_X_TAGS:
3601 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3602 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003603 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
3605 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003606 * of matches is found when compl_pattern is empty */
3607 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3609 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3610 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3611 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003612 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 }
3614 p_ic = save_p_ic;
3615 break;
3616
3617 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003618 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3620 {
3621
3622 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003623 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003624 ins_compl_add_matches(num_matches, matches,
3625#ifdef CASE_INSENSITIVE_FILENAME
3626 TRUE
3627#else
3628 FALSE
3629#endif
3630 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 }
3632 break;
3633
3634 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003635 if (expand_cmdline(&compl_xp, compl_pattern,
3636 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003638 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 break;
3640
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003641#ifdef FEAT_COMPL_FUNC
3642 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003643 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003644 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003645 break;
3646#endif
3647
Bram Moolenaar488c6512005-08-11 20:09:58 +00003648 case CTRL_X_SPELL:
3649#ifdef FEAT_SYN_HL
3650 num_matches = expand_spelling(first_match_pos.lnum,
3651 first_match_pos.col, compl_pattern, &matches);
3652 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003653 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003654#endif
3655 break;
3656
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 default: /* normal ^P/^N and ^X^L */
3658 /*
3659 * If 'infercase' is set, don't use 'smartcase' here
3660 */
3661 save_p_scs = p_scs;
3662 if (ins_buf->b_p_inf)
3663 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 /* buffers other than curbuf are scanned from the beginning or the
3666 * end but never from the middle, thus setting nowrapscan in this
3667 * buffers is a good idea, on the other hand, we always set
3668 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3669 save_p_ws = p_ws;
3670 if (ins_buf != curbuf)
3671 p_ws = FALSE;
3672 else if (*e_cpt == '.')
3673 p_ws = TRUE;
3674 for (;;)
3675 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003676 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003678 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3679 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003681 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003683 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003685 found_new_match = searchit(NULL, ins_buf, pos,
3686 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003687 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003688 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003689 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003691 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003692 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 first_match_pos = *pos;
3694 last_match_pos = *pos;
3695 }
3696 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003697 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 found_new_match = FAIL;
3699 if (found_new_match == FAIL)
3700 {
3701 if (ins_buf == curbuf)
3702 found_all = TRUE;
3703 break;
3704 }
3705
3706 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003707 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 && ini->lnum == pos->lnum
3709 && ini->col == pos->col)
3710 continue;
3711 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3712 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3713 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003714 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
3716 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3717 continue;
3718 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3719 if (!p_paste)
3720 ptr = skipwhite(ptr);
3721 }
3722 len = (int)STRLEN(ptr);
3723 }
3724 else
3725 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003726 char_u *tmp_ptr = ptr;
3727
3728 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003730 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 /* Skip if already inside a word. */
3732 if (vim_iswordp(tmp_ptr))
3733 continue;
3734 /* Find start of next word. */
3735 tmp_ptr = find_word_start(tmp_ptr);
3736 }
3737 /* Find end of this word. */
3738 tmp_ptr = find_word_end(tmp_ptr);
3739 len = (int)(tmp_ptr - ptr);
3740
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003741 if ((compl_cont_status & CONT_ADDING)
3742 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
3744 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3745 {
3746 /* Try next line, if any. the new word will be
3747 * "join" as if the normal command "J" was used.
3748 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003749 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 * works -- Acevedo */
3751 STRNCPY(IObuff, ptr, len);
3752 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3753 tmp_ptr = ptr = skipwhite(ptr);
3754 /* Find start of next word. */
3755 tmp_ptr = find_word_start(tmp_ptr);
3756 /* Find end of next word. */
3757 tmp_ptr = find_word_end(tmp_ptr);
3758 if (tmp_ptr > ptr)
3759 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003760 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003762 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 IObuff[len++] = ' ';
3764 /* IObuf =~ "\k.* ", thus len >= 2 */
3765 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003766 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 || (vim_strchr(p_cpo, CPO_JOINSP)
3768 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003769 && (IObuff[len - 2] == '?'
3770 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 IObuff[len++] = ' ';
3772 }
3773 /* copy as much as posible of the new word */
3774 if (tmp_ptr - ptr >= IOSIZE - len)
3775 tmp_ptr = ptr + IOSIZE - len - 1;
3776 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3777 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003778 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780 IObuff[len] = NUL;
3781 ptr = IObuff;
3782 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003783 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 continue;
3785 }
3786 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003787 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003788 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003789 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 {
3791 found_new_match = OK;
3792 break;
3793 }
3794 }
3795 p_scs = save_p_scs;
3796 p_ws = save_p_ws;
3797 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003798
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003799 /* check if compl_curr_match has changed, (e.g. other type of
3800 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003801 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 found_new_match = OK;
3803
3804 /* break the loop for specialized modes (use 'complete' just for the
3805 * generic ctrl_x_mode == 0) or when we've found a new match */
3806 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003807 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003808 {
3809 if (got_int)
3810 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003811 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003812 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003813 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003814
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003815 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3816 || compl_interrupted)
3817 break;
3818 compl_started = TRUE;
3819 }
3820 else
3821 {
3822 /* Mark a buffer scanned when it has been scanned completely */
3823 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3824 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003826 compl_started = FALSE;
3827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003829 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
3831 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3832 && *e_cpt == NUL) /* Got to end of 'complete' */
3833 found_new_match = FAIL;
3834
3835 i = -1; /* total of matches, unknown */
3836 if (found_new_match == FAIL
3837 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3838 i = ins_compl_make_cyclic();
3839
3840 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003841 * just been made cyclic then we have to move compl_curr_match to the next
3842 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003843 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
3844 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003845 if (compl_curr_match == NULL)
3846 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 return i;
3848}
3849
3850/* Delete the old text being completed. */
3851 static void
3852ins_compl_delete()
3853{
3854 int i;
3855
3856 /*
3857 * In insert mode: Delete the typed part.
3858 * In replace mode: Put the old characters back, if any.
3859 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003860 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 backspace_until_column(i);
3862 changed_cline_bef_curs();
3863}
3864
3865/* Insert the new text being completed. */
3866 static void
3867ins_compl_insert()
3868{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003869 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003870 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3871 compl_used_match = FALSE;
3872 else
3873 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874}
3875
3876/*
3877 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003878 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3879 * get more completions. If it is FALSE, then we just do nothing when there
3880 * are no more completions in a given direction. The latter case is used when
3881 * we are still in the middle of finding completions, to allow browsing
3882 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 * Return the total number of matches, or -1 if still unknown -- webb.
3884 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003885 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3886 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 *
3888 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003889 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3890 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 */
3892 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003893ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003895 int count; /* repeat completion this many times; should
3896 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003897 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898{
3899 int num_matches = -1;
3900 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003901 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003902 compl_T *found_compl = NULL;
3903 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003905 if (compl_leader != NULL
3906 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003908 /* Set "compl_shown_match" to the actually shown match, it may differ
3909 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003910 while (!ins_compl_equal(compl_shown_match,
3911 compl_leader, STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003912 && compl_shown_match->cp_next != NULL
3913 && compl_shown_match->cp_next != compl_first_match)
3914 compl_shown_match = compl_shown_match->cp_next;
3915 }
3916
3917 if (allow_get_expansion && insert_match
3918 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 /* Delete old text to be replaced */
3920 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003921
Bram Moolenaare3226be2005-12-18 22:10:00 +00003922 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3923 * around. */
3924 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003926 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003928 if (compl_pending != 0)
3929 --compl_pending;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003930 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003931 found_end = (compl_first_match != NULL
3932 && (compl_shown_match->cp_next == compl_first_match
3933 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003934 }
3935 else if (compl_shows_dir == BACKWARD
3936 && compl_shown_match->cp_prev != NULL)
3937 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003938 if (compl_pending != 0)
3939 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00003940 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003941 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003942 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
3944 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003945 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003946 if (compl_shows_dir == BACKWARD)
3947 --compl_pending;
3948 else
3949 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00003950 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003951 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003952
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003953 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003954 if (compl_pending != 0 && compl_direction == compl_shows_dir)
Bram Moolenaara6557602006-02-04 22:43:20 +00003955 compl_shown_match = compl_curr_match;
3956 found_end = FALSE;
3957 }
3958 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
3959 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003960 && !ins_compl_equal(compl_shown_match,
3961 compl_leader, STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00003962 ++todo;
3963 else
3964 /* Remember a matching item. */
3965 found_compl = compl_shown_match;
3966
3967 /* Stop at the end of the list when we found a usable match. */
3968 if (found_end)
3969 {
3970 if (found_compl != NULL)
3971 {
3972 compl_shown_match = found_compl;
3973 break;
3974 }
3975 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003979 /* Insert the text of the new completion, or the compl_leader. */
3980 if (insert_match)
3981 {
3982 if (!compl_get_longest || compl_used_match)
3983 ins_compl_insert();
3984 else
3985 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3986 }
3987 else
3988 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989
3990 if (!allow_get_expansion)
3991 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003992 /* may undisplay the popup menu first */
3993 ins_compl_upd_pum();
3994
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003995 /* redraw to show the user what was inserted */
3996 update_screen(0);
3997
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003998 /* display the updated popup menu */
3999 ins_compl_show_pum();
4000
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 /* Delete old text to be replaced, since we're still searching and
4002 * don't want to match ourselves! */
4003 ins_compl_delete();
4004 }
4005
4006 /*
4007 * Show the file name for the match (if any)
4008 * Truncate the file name to avoid a wait for return.
4009 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004010 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
4012 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004013 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 if (i <= 0)
4015 i = 0;
4016 else
4017 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004018 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 msg(IObuff);
4020 redraw_cmdline = FALSE; /* don't overwrite! */
4021 }
4022
4023 return num_matches;
4024}
4025
4026/*
4027 * Call this while finding completions, to check whether the user has hit a key
4028 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004029 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004031 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 */
4033 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004034ins_compl_check_keys(frequency)
4035 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036{
4037 static int count = 0;
4038
4039 int c;
4040
4041 /* Don't check when reading keys from a script. That would break the test
4042 * scripts */
4043 if (using_script())
4044 return;
4045
4046 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004047 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 return;
4049 count = 0;
4050
4051 ++no_mapping;
4052 c = vpeekc_any();
4053 --no_mapping;
4054 if (c != NUL)
4055 {
4056 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4057 {
4058 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004059 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004060 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4061 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
4063 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004064 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004066 if (compl_pending != 0 && !got_int)
4067 (void)ins_compl_next(FALSE, compl_pending > 0
4068 ? compl_pending : -compl_pending, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004069}
4070
4071/*
4072 * Decide the direction of Insert mode complete from the key typed.
4073 * Returns BACKWARD or FORWARD.
4074 */
4075 static int
4076ins_compl_key2dir(c)
4077 int c;
4078{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004079 if (c == Ctrl_P || c == Ctrl_L
4080 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4081 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004082 return BACKWARD;
4083 return FORWARD;
4084}
4085
4086/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004087 * Return TRUE for keys that are used for completion only when the popup menu
4088 * is visible.
4089 */
4090 static int
4091ins_compl_pum_key(c)
4092 int c;
4093{
4094 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004095 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4096 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004097}
4098
4099/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004100 * Decide the number of completions to move forward.
4101 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4102 */
4103 static int
4104ins_compl_key2count(c)
4105 int c;
4106{
4107 int h;
4108
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004109 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004110 {
4111 h = pum_get_height();
4112 if (h > 3)
4113 h -= 2; /* keep some context */
4114 return h;
4115 }
4116 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117}
4118
4119/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004120 * Return TRUE if completion with "c" should insert the match, FALSE if only
4121 * to change the currently selected completion.
4122 */
4123 static int
4124ins_compl_use_match(c)
4125 int c;
4126{
4127 switch (c)
4128 {
4129 case K_UP:
4130 case K_DOWN:
4131 case K_PAGEDOWN:
4132 case K_KPAGEDOWN:
4133 case K_S_DOWN:
4134 case K_PAGEUP:
4135 case K_KPAGEUP:
4136 case K_S_UP:
4137 return FALSE;
4138 }
4139 return TRUE;
4140}
4141
4142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 * Do Insert mode completion.
4144 * Called when character "c" was typed, which has a meaning for completion.
4145 * Returns OK if completion was done, FAIL if something failed (out of mem).
4146 */
4147 static int
4148ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004149 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004151 char_u *line;
4152 int startcol = 0; /* column where searched text starts */
4153 colnr_T curs_col; /* cursor column */
4154 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
Bram Moolenaare3226be2005-12-18 22:10:00 +00004156 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004157 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 {
4159 /* First time we hit ^N or ^P (in a row, I mean) */
4160
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 did_ai = FALSE;
4162#ifdef FEAT_SMARTINDENT
4163 did_si = FALSE;
4164 can_si = FALSE;
4165 can_si_back = FALSE;
4166#endif
4167 if (stop_arrow() == FAIL)
4168 return FAIL;
4169
4170 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004171 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004172 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173
4174 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004175 * "compl_startpos" to the cursor as a pattern to add a new word
4176 * instead of expand the one before the cursor, in word-wise if
4177 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 * is not in the same line as the cursor then fix it (the line has
4179 * been split because it was longer than 'tw'). if SOL is set then
4180 * skip the previous pattern, a word at the beginning of the line has
4181 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004182 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4183 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
4185 /*
4186 * it is a continued search
4187 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004188 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4190 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4191 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004192 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004194 /* line (probably) wrapped, set compl_startpos to the
4195 * first non_blank in the line, if it is not a wordchar
4196 * include it to get a better pattern, but then we don't
4197 * want the "\\<" prefix, check it bellow */
4198 compl_col = (colnr_T)(skipwhite(line) - line);
4199 compl_startpos.col = compl_col;
4200 compl_startpos.lnum = curwin->w_cursor.lnum;
4201 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203 else
4204 {
4205 /* S_IPOS was set when we inserted a word that was at the
4206 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004207 * mode but first we need to redefine compl_startpos */
4208 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004210 compl_cont_status |= CONT_SOL;
4211 compl_startpos.col = (colnr_T)(skipwhite(
4212 line + compl_length
4213 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004215 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004217 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004218 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 * have enough space? just being paranoic */
4220#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004221 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004223 compl_cont_status &= ~CONT_SOL;
4224 compl_length = (IOSIZE - MIN_SPACE);
4225 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004227 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4228 if (compl_length < 1)
4229 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004232 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004234 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
4236 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004237 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004239 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004241 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004243 compl_cont_status = 0;
4244 compl_cont_status |= CONT_N_ADDS;
4245 compl_startpos = curwin->w_cursor;
4246 startcol = (int)curs_col;
4247 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
4249
4250 /* Work out completion pattern and original text -- webb */
4251 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4252 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004253 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4255 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004256 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004258 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004260 compl_col += ++startcol;
4261 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004264 compl_pattern = str_foldcase(line + compl_col,
4265 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004267 compl_pattern = vim_strnsave(line + compl_col,
4268 compl_length);
4269 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 return FAIL;
4271 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004272 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 {
4274 char_u *prefix = (char_u *)"\\<";
4275
4276 /* we need 3 extra chars, 1 for the NUL and
4277 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004278 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4279 compl_length) + 3);
4280 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004282 if (!vim_iswordp(line + compl_col)
4283 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 && (
4285#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004286 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004288 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289#endif
4290 )))
4291 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004292 STRCPY((char *)compl_pattern, prefix);
4293 (void)quote_meta(compl_pattern + STRLEN(prefix),
4294 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004296 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004298 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004300 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301#endif
4302 )
4303 {
4304 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004305 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4306 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004308 compl_col += curs_col;
4309 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 }
4311 else
4312 {
4313#ifdef FEAT_MBYTE
4314 /* Search the point of change class of multibyte character
4315 * or not a word single byte character backward. */
4316 if (has_mbyte)
4317 {
4318 int base_class;
4319 int head_off;
4320
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004321 startcol -= (*mb_head_off)(line, line + startcol);
4322 base_class = mb_get_class(line + startcol);
4323 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004325 head_off = (*mb_head_off)(line, line + startcol);
4326 if (base_class != mb_get_class(line + startcol
4327 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004329 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 }
4331 }
4332 else
4333#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004334 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004336 compl_col += ++startcol;
4337 compl_length = (int)curs_col - startcol;
4338 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 {
4340 /* Only match word with at least two chars -- webb
4341 * there's no need to call quote_meta,
4342 * alloc(7) is enough -- Acevedo
4343 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004344 compl_pattern = alloc(7);
4345 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004347 STRCPY((char *)compl_pattern, "\\<");
4348 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4349 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 }
4351 else
4352 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004353 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4354 compl_length) + 3);
4355 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004357 STRCPY((char *)compl_pattern, "\\<");
4358 (void)quote_meta(compl_pattern + 2, line + compl_col,
4359 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 }
4362 }
4363 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4364 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004365 compl_col = skipwhite(line) - line;
4366 compl_length = (int)curs_col - (int)compl_col;
4367 if (compl_length < 0) /* cursor in indent: empty pattern */
4368 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004370 compl_pattern = str_foldcase(line + compl_col, compl_length,
4371 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004373 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4374 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 return FAIL;
4376 }
4377 else if (ctrl_x_mode == CTRL_X_FILES)
4378 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004379 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004381 compl_col += ++startcol;
4382 compl_length = (int)curs_col - startcol;
4383 compl_pattern = addstar(line + compl_col, compl_length,
4384 EXPAND_FILES);
4385 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 return FAIL;
4387 }
4388 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4389 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004390 compl_pattern = vim_strnsave(line, curs_col);
4391 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004393 set_cmd_context(&compl_xp, compl_pattern,
4394 (int)STRLEN(compl_pattern), curs_col);
4395 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4396 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004398 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4399 compl_col = startcol;
4400 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004402 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004403 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004404#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004405 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004406 * Call user defined function 'completefunc' with "a:findstart"
4407 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004408 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004409 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004410 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004411 char_u *funcname;
4412 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004413
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004414 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004415 * string */
4416 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4417 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4418 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004419 {
4420 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4421 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004422 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004423 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004424
4425 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004426 args[1] = NULL;
4427 pos = curwin->w_cursor;
4428 col = call_func_retnr(funcname, 2, args, FALSE);
4429 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004430
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004431 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004432 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004433 compl_col = col;
4434 if ((colnr_T)compl_col > curs_col)
4435 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004436
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004437 /* Setup variables for completion. Need to obtain "line" again,
4438 * it may have become invalid. */
4439 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004440 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004441 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4442 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004443#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004444 return FAIL;
4445 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004446 else if (ctrl_x_mode == CTRL_X_SPELL)
4447 {
4448#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004449 if (spell_bad_len > 0)
4450 compl_col = curs_col - spell_bad_len;
4451 else
4452 compl_col = spell_word_start(startcol);
4453 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004454 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004455 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004456 compl_length = (int)curs_col - compl_col;
4457 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4458 if (compl_pattern == NULL)
4459#endif
4460 return FAIL;
4461 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004462 else
4463 {
4464 EMSG2(_(e_intern2), "ins_complete()");
4465 return FAIL;
4466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004468 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 {
4470 edit_submode_pre = (char_u *)_(" Adding");
4471 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4472 {
4473 /* Insert a new line, keep indentation but ignore 'comments' */
4474#ifdef FEAT_COMMENTS
4475 char_u *old = curbuf->b_p_com;
4476
4477 curbuf->b_p_com = (char_u *)"";
4478#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004479 compl_startpos.lnum = curwin->w_cursor.lnum;
4480 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 ins_eol('\r');
4482#ifdef FEAT_COMMENTS
4483 curbuf->b_p_com = old;
4484#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004485 compl_length = 0;
4486 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488 }
4489 else
4490 {
4491 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004492 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004495 if (compl_cont_status & CONT_LOCAL)
4496 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 else
4498 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4499
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004500 /* Always add completion for the original text. */
4501 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004502 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4503 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaard289f132006-03-11 21:30:53 +00004504 -1, FALSE, NULL, NULL, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004506 vim_free(compl_pattern);
4507 compl_pattern = NULL;
4508 vim_free(compl_orig_text);
4509 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 return FAIL;
4511 }
4512
4513 /* showmode might reset the internal line pointers, so it must
4514 * be called before line = ml_get(), or when this address is no
4515 * longer needed. -- Acevedo.
4516 */
4517 edit_submode_extra = (char_u *)_("-- Searching...");
4518 edit_submode_highl = HLF_COUNT;
4519 showmode();
4520 edit_submode_extra = NULL;
4521 out_flush();
4522 }
4523
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004524 compl_shown_match = compl_curr_match;
4525 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
4527 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004528 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004530 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004532 /* may undisplay the popup menu */
4533 ins_compl_upd_pum();
4534
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004535 if (n > 1) /* all matches have been found */
4536 compl_matches = n;
4537 compl_curr_match = compl_shown_match;
4538 compl_direction = compl_shows_dir;
4539 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540
4541 /* eat the ESC to avoid leaving insert mode */
4542 if (got_int && !global_busy)
4543 {
4544 (void)vgetc();
4545 got_int = FALSE;
4546 }
4547
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004548 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004549 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004551 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4552 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4554 edit_submode_highl = HLF_E;
4555 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4556 * because we couldn't expand anything at first place, but if we used
4557 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4558 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004559 if ( compl_length > 1
4560 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 || (ctrl_x_mode != 0
4562 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4563 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004564 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 }
4566
Bram Moolenaar572cb562005-08-05 21:35:02 +00004567 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004568 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004570 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571
4572 if (edit_submode_extra == NULL)
4573 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004574 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
4576 edit_submode_extra = (char_u *)_("Back at original");
4577 edit_submode_highl = HLF_W;
4578 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004579 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
4581 edit_submode_extra = (char_u *)_("Word from other line");
4582 edit_submode_highl = HLF_COUNT;
4583 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004584 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
4586 edit_submode_extra = (char_u *)_("The only match");
4587 edit_submode_highl = HLF_COUNT;
4588 }
4589 else
4590 {
4591 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004592 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004594 int number = 0;
4595 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004597 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
4599 /* search backwards for the first valid (!= -1) number.
4600 * This should normally succeed already at the first loop
4601 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004602 for (match = compl_curr_match->cp_prev; match != NULL
4603 && match != compl_first_match;
4604 match = match->cp_prev)
4605 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004607 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 break;
4609 }
4610 if (match != NULL)
4611 /* go up and assign all numbers which are not assigned
4612 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004613 for (match = match->cp_next;
4614 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004615 match = match->cp_next)
4616 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
4618 else /* BACKWARD */
4619 {
4620 /* search forwards (upwards) for the first valid (!= -1)
4621 * number. This should normally succeed already at the
4622 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004623 for (match = compl_curr_match->cp_next; match != NULL
4624 && match != compl_first_match;
4625 match = match->cp_next)
4626 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004628 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 break;
4630 }
4631 if (match != NULL)
4632 /* go down and assign all numbers which are not
4633 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004634 for (match = match->cp_prev; match
4635 && match->cp_number == -1;
4636 match = match->cp_prev)
4637 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 }
4639 }
4640
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004641 /* The match should always have a sequence number now, this is
4642 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004643 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 {
4645 /* Space for 10 text chars. + 2x10-digit no.s */
4646 static char_u match_ref[31];
4647
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004648 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004650 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004652 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004653 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004654 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 edit_submode_extra = match_ref;
4656 edit_submode_highl = HLF_R;
4657 if (dollar_vcol)
4658 curs_columns(FALSE);
4659 }
4660 }
4661 }
4662
4663 /* Show a message about what (completion) mode we're in. */
4664 showmode();
4665 if (edit_submode_extra != NULL)
4666 {
4667 if (!p_smd)
4668 msg_attr(edit_submode_extra,
4669 edit_submode_highl < HLF_COUNT
4670 ? hl_attr(edit_submode_highl) : 0);
4671 }
4672 else
4673 msg_clr_cmdline(); /* necessary for "noshowmode" */
4674
Bram Moolenaara94bc432006-03-10 21:42:59 +00004675 /* RedrawingDisabled may be set when invoked through complete(). */
4676 n = RedrawingDisabled;
4677 RedrawingDisabled = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004678 ins_compl_show_pum();
Bram Moolenaara94bc432006-03-10 21:42:59 +00004679 setcursor();
4680 RedrawingDisabled = n;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004681
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 return OK;
4683}
4684
4685/*
4686 * Looks in the first "len" chars. of "src" for search-metachars.
4687 * If dest is not NULL the chars. are copied there quoting (with
4688 * a backslash) the metachars, and dest would be NUL terminated.
4689 * Returns the length (needed) of dest
4690 */
4691 static int
4692quote_meta(dest, src, len)
4693 char_u *dest;
4694 char_u *src;
4695 int len;
4696{
4697 int m;
4698
4699 for (m = len; --len >= 0; src++)
4700 {
4701 switch (*src)
4702 {
4703 case '.':
4704 case '*':
4705 case '[':
4706 if (ctrl_x_mode == CTRL_X_DICTIONARY
4707 || ctrl_x_mode == CTRL_X_THESAURUS)
4708 break;
4709 case '~':
4710 if (!p_magic) /* quote these only if magic is set */
4711 break;
4712 case '\\':
4713 if (ctrl_x_mode == CTRL_X_DICTIONARY
4714 || ctrl_x_mode == CTRL_X_THESAURUS)
4715 break;
4716 case '^': /* currently it's not needed. */
4717 case '$':
4718 m++;
4719 if (dest != NULL)
4720 *dest++ = '\\';
4721 break;
4722 }
4723 if (dest != NULL)
4724 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004725# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 /* Copy remaining bytes of a multibyte character. */
4727 if (has_mbyte)
4728 {
4729 int i, mb_len;
4730
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004731 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 if (mb_len > 0 && len >= mb_len)
4733 for (i = 0; i < mb_len; ++i)
4734 {
4735 --len;
4736 ++src;
4737 if (dest != NULL)
4738 *dest++ = *src;
4739 }
4740 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004741# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
4743 if (dest != NULL)
4744 *dest = NUL;
4745
4746 return m;
4747}
4748#endif /* FEAT_INS_EXPAND */
4749
4750/*
4751 * Next character is interpreted literally.
4752 * A one, two or three digit decimal number is interpreted as its byte value.
4753 * If one or two digits are entered, the next character is given to vungetc().
4754 * For Unicode a character > 255 may be returned.
4755 */
4756 int
4757get_literal()
4758{
4759 int cc;
4760 int nc;
4761 int i;
4762 int hex = FALSE;
4763 int octal = FALSE;
4764#ifdef FEAT_MBYTE
4765 int unicode = 0;
4766#endif
4767
4768 if (got_int)
4769 return Ctrl_C;
4770
4771#ifdef FEAT_GUI
4772 /*
4773 * In GUI there is no point inserting the internal code for a special key.
4774 * It is more useful to insert the string "<KEY>" instead. This would
4775 * probably be useful in a text window too, but it would not be
4776 * vi-compatible (maybe there should be an option for it?) -- webb
4777 */
4778 if (gui.in_use)
4779 ++allow_keys;
4780#endif
4781#ifdef USE_ON_FLY_SCROLL
4782 dont_scroll = TRUE; /* disallow scrolling here */
4783#endif
4784 ++no_mapping; /* don't map the next key hits */
4785 cc = 0;
4786 i = 0;
4787 for (;;)
4788 {
4789 do
4790 nc = safe_vgetc();
4791 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4792 || nc == K_HOR_SCROLLBAR);
4793#ifdef FEAT_CMDL_INFO
4794 if (!(State & CMDLINE)
4795# ifdef FEAT_MBYTE
4796 && MB_BYTE2LEN_CHECK(nc) == 1
4797# endif
4798 )
4799 add_to_showcmd(nc);
4800#endif
4801 if (nc == 'x' || nc == 'X')
4802 hex = TRUE;
4803 else if (nc == 'o' || nc == 'O')
4804 octal = TRUE;
4805#ifdef FEAT_MBYTE
4806 else if (nc == 'u' || nc == 'U')
4807 unicode = nc;
4808#endif
4809 else
4810 {
4811 if (hex
4812#ifdef FEAT_MBYTE
4813 || unicode != 0
4814#endif
4815 )
4816 {
4817 if (!vim_isxdigit(nc))
4818 break;
4819 cc = cc * 16 + hex2nr(nc);
4820 }
4821 else if (octal)
4822 {
4823 if (nc < '0' || nc > '7')
4824 break;
4825 cc = cc * 8 + nc - '0';
4826 }
4827 else
4828 {
4829 if (!VIM_ISDIGIT(nc))
4830 break;
4831 cc = cc * 10 + nc - '0';
4832 }
4833
4834 ++i;
4835 }
4836
4837 if (cc > 255
4838#ifdef FEAT_MBYTE
4839 && unicode == 0
4840#endif
4841 )
4842 cc = 255; /* limit range to 0-255 */
4843 nc = 0;
4844
4845 if (hex) /* hex: up to two chars */
4846 {
4847 if (i >= 2)
4848 break;
4849 }
4850#ifdef FEAT_MBYTE
4851 else if (unicode) /* Unicode: up to four or eight chars */
4852 {
4853 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4854 break;
4855 }
4856#endif
4857 else if (i >= 3) /* decimal or octal: up to three chars */
4858 break;
4859 }
4860 if (i == 0) /* no number entered */
4861 {
4862 if (nc == K_ZERO) /* NUL is stored as NL */
4863 {
4864 cc = '\n';
4865 nc = 0;
4866 }
4867 else
4868 {
4869 cc = nc;
4870 nc = 0;
4871 }
4872 }
4873
4874 if (cc == 0) /* NUL is stored as NL */
4875 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004876#ifdef FEAT_MBYTE
4877 if (enc_dbcs && (cc & 0xff) == 0)
4878 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4879 second byte will cause trouble! */
4880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881
4882 --no_mapping;
4883#ifdef FEAT_GUI
4884 if (gui.in_use)
4885 --allow_keys;
4886#endif
4887 if (nc)
4888 vungetc(nc);
4889 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4890 return cc;
4891}
4892
4893/*
4894 * Insert character, taking care of special keys and mod_mask
4895 */
4896 static void
4897insert_special(c, allow_modmask, ctrlv)
4898 int c;
4899 int allow_modmask;
4900 int ctrlv; /* c was typed after CTRL-V */
4901{
4902 char_u *p;
4903 int len;
4904
4905 /*
4906 * Special function key, translate into "<Key>". Up to the last '>' is
4907 * inserted with ins_str(), so as not to replace characters in replace
4908 * mode.
4909 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4910 * unless 'allow_modmask' is TRUE.
4911 */
4912#ifdef MACOS
4913 /* Command-key never produces a normal key */
4914 if (mod_mask & MOD_MASK_CMD)
4915 allow_modmask = TRUE;
4916#endif
4917 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4918 {
4919 p = get_special_key_name(c, mod_mask);
4920 len = (int)STRLEN(p);
4921 c = p[len - 1];
4922 if (len > 2)
4923 {
4924 if (stop_arrow() == FAIL)
4925 return;
4926 p[len - 1] = NUL;
4927 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004928 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 ctrlv = FALSE;
4930 }
4931 }
4932 if (stop_arrow() == OK)
4933 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4934}
4935
4936/*
4937 * Special characters in this context are those that need processing other
4938 * than the simple insertion that can be performed here. This includes ESC
4939 * which terminates the insert, and CR/NL which need special processing to
4940 * open up a new line. This routine tries to optimize insertions performed by
4941 * the "redo", "undo" or "put" commands, so it needs to know when it should
4942 * stop and defer processing to the "normal" mechanism.
4943 * '0' and '^' are special, because they can be followed by CTRL-D.
4944 */
4945#ifdef EBCDIC
4946# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4947#else
4948# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4949#endif
4950
4951#ifdef FEAT_MBYTE
4952# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4953#else
4954# define WHITECHAR(cc) vim_iswhite(cc)
4955#endif
4956
4957 void
4958insertchar(c, flags, second_indent)
4959 int c; /* character to insert or NUL */
4960 int flags; /* INSCHAR_FORMAT, etc. */
4961 int second_indent; /* indent for second line if >= 0 */
4962{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 int textwidth;
4964#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
4969 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4970 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971
4972 /*
4973 * Try to break the line in two or more pieces when:
4974 * - Always do this if we have been called to do formatting only.
4975 * - Always do this when 'formatoptions' has the 'a' flag and the line
4976 * ends in white space.
4977 * - Otherwise:
4978 * - Don't do this if inserting a blank
4979 * - Don't do this if an existing character is being replaced, unless
4980 * we're in VREPLACE mode.
4981 * - Do this if the cursor is not on the line where insert started
4982 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4983 * before the insert.
4984 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4985 * before 'textwidth'
4986 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004987 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 && ((flags & INSCHAR_FORMAT)
4989 || (!vim_iswhite(c)
4990 && !((State & REPLACE_FLAG)
4991#ifdef FEAT_VREPLACE
4992 && !(State & VREPLACE_FLAG)
4993#endif
4994 && *ml_get_cursor() != NUL)
4995 && (curwin->w_cursor.lnum != Insstart.lnum
4996 || ((!has_format_option(FO_INS_LONG)
4997 || Insstart_textlen <= (colnr_T)textwidth)
4998 && (!fo_ins_blank
4999 || Insstart_blank_vcol <= (colnr_T)textwidth
5000 ))))))
5001 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005002 /* Format with 'formatexpr' when it's set. Use internal formatting
5003 * when 'formatexpr' isn't set or it returns non-zero. */
5004#if defined(FEAT_EVAL)
5005 if (*curbuf->b_p_fex == NUL
5006 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005008 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005010
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 if (c == NUL) /* only formatting was wanted */
5012 return;
5013
5014#ifdef FEAT_COMMENTS
5015 /* Check whether this character should end a comment. */
5016 if (did_ai && (int)c == end_comment_pending)
5017 {
5018 char_u *line;
5019 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5020 int middle_len, end_len;
5021 int i;
5022
5023 /*
5024 * Need to remove existing (middle) comment leader and insert end
5025 * comment leader. First, check what comment leader we can find.
5026 */
5027 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5028 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5029 {
5030 /* Skip middle-comment string */
5031 while (*p && p[-1] != ':') /* find end of middle flags */
5032 ++p;
5033 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5034 /* Don't count trailing white space for middle_len */
5035 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5036 --middle_len;
5037
5038 /* Find the end-comment string */
5039 while (*p && p[-1] != ':') /* find end of end flags */
5040 ++p;
5041 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5042
5043 /* Skip white space before the cursor */
5044 i = curwin->w_cursor.col;
5045 while (--i >= 0 && vim_iswhite(line[i]))
5046 ;
5047 i++;
5048
5049 /* Skip to before the middle leader */
5050 i -= middle_len;
5051
5052 /* Check some expected things before we go on */
5053 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5054 {
5055 /* Backspace over all the stuff we want to replace */
5056 backspace_until_column(i);
5057
5058 /*
5059 * Insert the end-comment string, except for the last
5060 * character, which will get inserted as normal later.
5061 */
5062 ins_bytes_len(lead_end, end_len - 1);
5063 }
5064 }
5065 }
5066 end_comment_pending = NUL;
5067#endif
5068
5069 did_ai = FALSE;
5070#ifdef FEAT_SMARTINDENT
5071 did_si = FALSE;
5072 can_si = FALSE;
5073 can_si_back = FALSE;
5074#endif
5075
5076 /*
5077 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5078 * This speeds up normal text input considerably.
5079 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5080 * need to re-indent at a ':', or any other character (but not what
5081 * 'paste' is set)..
5082 */
5083#ifdef USE_ON_FLY_SCROLL
5084 dont_scroll = FALSE; /* allow scrolling here */
5085#endif
5086
5087 if ( !ISSPECIAL(c)
5088#ifdef FEAT_MBYTE
5089 && (!has_mbyte || (*mb_char2len)(c) == 1)
5090#endif
5091 && vpeekc() != NUL
5092 && !(State & REPLACE_FLAG)
5093#ifdef FEAT_CINDENT
5094 && !cindent_on()
5095#endif
5096#ifdef FEAT_RIGHTLEFT
5097 && !p_ri
5098#endif
5099 )
5100 {
5101#define INPUT_BUFLEN 100
5102 char_u buf[INPUT_BUFLEN + 1];
5103 int i;
5104 colnr_T virtcol = 0;
5105
5106 buf[0] = c;
5107 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005108 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 virtcol = get_nolist_virtcol();
5110 /*
5111 * Stop the string when:
5112 * - no more chars available
5113 * - finding a special character (command key)
5114 * - buffer is full
5115 * - running into the 'textwidth' boundary
5116 * - need to check for abbreviation: A non-word char after a word-char
5117 */
5118 while ( (c = vpeekc()) != NUL
5119 && !ISSPECIAL(c)
5120#ifdef FEAT_MBYTE
5121 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5122#endif
5123 && i < INPUT_BUFLEN
5124 && (textwidth == 0
5125 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5126 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5127 {
5128#ifdef FEAT_RIGHTLEFT
5129 c = vgetc();
5130 if (p_hkmap && KeyTyped)
5131 c = hkmap(c); /* Hebrew mode mapping */
5132# ifdef FEAT_FKMAP
5133 if (p_fkmap && KeyTyped)
5134 c = fkmap(c); /* Farsi mode mapping */
5135# endif
5136 buf[i++] = c;
5137#else
5138 buf[i++] = vgetc();
5139#endif
5140 }
5141
5142#ifdef FEAT_DIGRAPHS
5143 do_digraph(-1); /* clear digraphs */
5144 do_digraph(buf[i-1]); /* may be the start of a digraph */
5145#endif
5146 buf[i] = NUL;
5147 ins_str(buf);
5148 if (flags & INSCHAR_CTRLV)
5149 {
5150 redo_literal(*buf);
5151 i = 1;
5152 }
5153 else
5154 i = 0;
5155 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005156 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 }
5158 else
5159 {
5160#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005161 int cc;
5162
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5164 {
5165 char_u buf[MB_MAXBYTES + 1];
5166
5167 (*mb_char2bytes)(c, buf);
5168 buf[cc] = NUL;
5169 ins_char_bytes(buf, cc);
5170 AppendCharToRedobuff(c);
5171 }
5172 else
5173#endif
5174 {
5175 ins_char(c);
5176 if (flags & INSCHAR_CTRLV)
5177 redo_literal(c);
5178 else
5179 AppendCharToRedobuff(c);
5180 }
5181 }
5182}
5183
5184/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005185 * Format text at the current insert position.
5186 */
5187 static void
5188internal_format(textwidth, second_indent, flags, format_only)
5189 int textwidth;
5190 int second_indent;
5191 int flags;
5192 int format_only;
5193{
5194 int cc;
5195 int save_char = NUL;
5196 int haveto_redraw = FALSE;
5197 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5198#ifdef FEAT_MBYTE
5199 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5200#endif
5201 int fo_white_par = has_format_option(FO_WHITE_PAR);
5202 int first_line = TRUE;
5203#ifdef FEAT_COMMENTS
5204 colnr_T leader_len;
5205 int no_leader = FALSE;
5206 int do_comments = (flags & INSCHAR_DO_COM);
5207#endif
5208
5209 /*
5210 * When 'ai' is off we don't want a space under the cursor to be
5211 * deleted. Replace it with an 'x' temporarily.
5212 */
5213 if (!curbuf->b_p_ai)
5214 {
5215 cc = gchar_cursor();
5216 if (vim_iswhite(cc))
5217 {
5218 save_char = cc;
5219 pchar_cursor('x');
5220 }
5221 }
5222
5223 /*
5224 * Repeat breaking lines, until the current line is not too long.
5225 */
5226 while (!got_int)
5227 {
5228 int startcol; /* Cursor column at entry */
5229 int wantcol; /* column at textwidth border */
5230 int foundcol; /* column for start of spaces */
5231 int end_foundcol = 0; /* column for start of word */
5232 colnr_T len;
5233 colnr_T virtcol;
5234#ifdef FEAT_VREPLACE
5235 int orig_col = 0;
5236 char_u *saved_text = NULL;
5237#endif
5238 colnr_T col;
5239
5240 virtcol = get_nolist_virtcol();
5241 if (virtcol < (colnr_T)textwidth)
5242 break;
5243
5244#ifdef FEAT_COMMENTS
5245 if (no_leader)
5246 do_comments = FALSE;
5247 else if (!(flags & INSCHAR_FORMAT)
5248 && has_format_option(FO_WRAP_COMS))
5249 do_comments = TRUE;
5250
5251 /* Don't break until after the comment leader */
5252 if (do_comments)
5253 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5254 else
5255 leader_len = 0;
5256
5257 /* If the line doesn't start with a comment leader, then don't
5258 * start one in a following broken line. Avoids that a %word
5259 * moved to the start of the next line causes all following lines
5260 * to start with %. */
5261 if (leader_len == 0)
5262 no_leader = TRUE;
5263#endif
5264 if (!(flags & INSCHAR_FORMAT)
5265#ifdef FEAT_COMMENTS
5266 && leader_len == 0
5267#endif
5268 && !has_format_option(FO_WRAP))
5269
5270 {
5271 textwidth = 0;
5272 break;
5273 }
5274 if ((startcol = curwin->w_cursor.col) == 0)
5275 break;
5276
5277 /* find column of textwidth border */
5278 coladvance((colnr_T)textwidth);
5279 wantcol = curwin->w_cursor.col;
5280
5281 curwin->w_cursor.col = startcol - 1;
5282#ifdef FEAT_MBYTE
5283 /* Correct cursor for multi-byte character. */
5284 if (has_mbyte)
5285 mb_adjust_cursor();
5286#endif
5287 foundcol = 0;
5288
5289 /*
5290 * Find position to break at.
5291 * Stop at first entered white when 'formatoptions' has 'v'
5292 */
5293 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5294 || curwin->w_cursor.lnum != Insstart.lnum
5295 || curwin->w_cursor.col >= Insstart.col)
5296 {
5297 cc = gchar_cursor();
5298 if (WHITECHAR(cc))
5299 {
5300 /* remember position of blank just before text */
5301 end_foundcol = curwin->w_cursor.col;
5302
5303 /* find start of sequence of blanks */
5304 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5305 {
5306 dec_cursor();
5307 cc = gchar_cursor();
5308 }
5309 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5310 break; /* only spaces in front of text */
5311#ifdef FEAT_COMMENTS
5312 /* Don't break until after the comment leader */
5313 if (curwin->w_cursor.col < leader_len)
5314 break;
5315#endif
5316 if (has_format_option(FO_ONE_LETTER))
5317 {
5318 /* do not break after one-letter words */
5319 if (curwin->w_cursor.col == 0)
5320 break; /* one-letter word at begin */
5321
5322 col = curwin->w_cursor.col;
5323 dec_cursor();
5324 cc = gchar_cursor();
5325
5326 if (WHITECHAR(cc))
5327 continue; /* one-letter, continue */
5328 curwin->w_cursor.col = col;
5329 }
5330#ifdef FEAT_MBYTE
5331 if (has_mbyte)
5332 foundcol = curwin->w_cursor.col
5333 + (*mb_ptr2len)(ml_get_cursor());
5334 else
5335#endif
5336 foundcol = curwin->w_cursor.col + 1;
5337 if (curwin->w_cursor.col < (colnr_T)wantcol)
5338 break;
5339 }
5340#ifdef FEAT_MBYTE
5341 else if (cc >= 0x100 && fo_multibyte
5342 && curwin->w_cursor.col <= (colnr_T)wantcol)
5343 {
5344 /* Break after or before a multi-byte character. */
5345 foundcol = curwin->w_cursor.col;
5346 if (curwin->w_cursor.col < (colnr_T)wantcol)
5347 foundcol += (*mb_char2len)(cc);
5348 end_foundcol = foundcol;
5349 break;
5350 }
5351#endif
5352 if (curwin->w_cursor.col == 0)
5353 break;
5354 dec_cursor();
5355 }
5356
5357 if (foundcol == 0) /* no spaces, cannot break line */
5358 {
5359 curwin->w_cursor.col = startcol;
5360 break;
5361 }
5362
5363 /* Going to break the line, remove any "$" now. */
5364 undisplay_dollar();
5365
5366 /*
5367 * Offset between cursor position and line break is used by replace
5368 * stack functions. VREPLACE does not use this, and backspaces
5369 * over the text instead.
5370 */
5371#ifdef FEAT_VREPLACE
5372 if (State & VREPLACE_FLAG)
5373 orig_col = startcol; /* Will start backspacing from here */
5374 else
5375#endif
5376 replace_offset = startcol - end_foundcol - 1;
5377
5378 /*
5379 * adjust startcol for spaces that will be deleted and
5380 * characters that will remain on top line
5381 */
5382 curwin->w_cursor.col = foundcol;
5383 while (cc = gchar_cursor(), WHITECHAR(cc))
5384 inc_cursor();
5385 startcol -= curwin->w_cursor.col;
5386 if (startcol < 0)
5387 startcol = 0;
5388
5389#ifdef FEAT_VREPLACE
5390 if (State & VREPLACE_FLAG)
5391 {
5392 /*
5393 * In VREPLACE mode, we will backspace over the text to be
5394 * wrapped, so save a copy now to put on the next line.
5395 */
5396 saved_text = vim_strsave(ml_get_cursor());
5397 curwin->w_cursor.col = orig_col;
5398 if (saved_text == NULL)
5399 break; /* Can't do it, out of memory */
5400 saved_text[startcol] = NUL;
5401
5402 /* Backspace over characters that will move to the next line */
5403 if (!fo_white_par)
5404 backspace_until_column(foundcol);
5405 }
5406 else
5407#endif
5408 {
5409 /* put cursor after pos. to break line */
5410 if (!fo_white_par)
5411 curwin->w_cursor.col = foundcol;
5412 }
5413
5414 /*
5415 * Split the line just before the margin.
5416 * Only insert/delete lines, but don't really redraw the window.
5417 */
5418 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5419 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5420#ifdef FEAT_COMMENTS
5421 + (do_comments ? OPENLINE_DO_COM : 0)
5422#endif
5423 , old_indent);
5424 old_indent = 0;
5425
5426 replace_offset = 0;
5427 if (first_line)
5428 {
5429 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5430 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5431 if (second_indent >= 0)
5432 {
5433#ifdef FEAT_VREPLACE
5434 if (State & VREPLACE_FLAG)
5435 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5436 else
5437#endif
5438 (void)set_indent(second_indent, SIN_CHANGED);
5439 }
5440 first_line = FALSE;
5441 }
5442
5443#ifdef FEAT_VREPLACE
5444 if (State & VREPLACE_FLAG)
5445 {
5446 /*
5447 * In VREPLACE mode we have backspaced over the text to be
5448 * moved, now we re-insert it into the new line.
5449 */
5450 ins_bytes(saved_text);
5451 vim_free(saved_text);
5452 }
5453 else
5454#endif
5455 {
5456 /*
5457 * Check if cursor is not past the NUL off the line, cindent
5458 * may have added or removed indent.
5459 */
5460 curwin->w_cursor.col += startcol;
5461 len = (colnr_T)STRLEN(ml_get_curline());
5462 if (curwin->w_cursor.col > len)
5463 curwin->w_cursor.col = len;
5464 }
5465
5466 haveto_redraw = TRUE;
5467#ifdef FEAT_CINDENT
5468 can_cindent = TRUE;
5469#endif
5470 /* moved the cursor, don't autoindent or cindent now */
5471 did_ai = FALSE;
5472#ifdef FEAT_SMARTINDENT
5473 did_si = FALSE;
5474 can_si = FALSE;
5475 can_si_back = FALSE;
5476#endif
5477 line_breakcheck();
5478 }
5479
5480 if (save_char != NUL) /* put back space after cursor */
5481 pchar_cursor(save_char);
5482
5483 if (!format_only && haveto_redraw)
5484 {
5485 update_topline();
5486 redraw_curbuf_later(VALID);
5487 }
5488}
5489
5490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 * Called after inserting or deleting text: When 'formatoptions' includes the
5492 * 'a' flag format from the current line until the end of the paragraph.
5493 * Keep the cursor at the same position relative to the text.
5494 * The caller must have saved the cursor line for undo, following ones will be
5495 * saved here.
5496 */
5497 void
5498auto_format(trailblank, prev_line)
5499 int trailblank; /* when TRUE also format with trailing blank */
5500 int prev_line; /* may start in previous line */
5501{
5502 pos_T pos;
5503 colnr_T len;
5504 char_u *old;
5505 char_u *new, *pnew;
5506 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005507 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
5509 if (!has_format_option(FO_AUTO))
5510 return;
5511
5512 pos = curwin->w_cursor;
5513 old = ml_get_curline();
5514
5515 /* may remove added space */
5516 check_auto_format(FALSE);
5517
5518 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5519 * user might insert normal text next. Also skip formatting when "1" is
5520 * in 'formatoptions' and there is a single character before the cursor.
5521 * Otherwise the line would be broken and when typing another non-white
5522 * next they are not joined back together. */
5523 wasatend = (pos.col == STRLEN(old));
5524 if (*old != NUL && !trailblank && wasatend)
5525 {
5526 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005527 cc = gchar_cursor();
5528 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5529 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005531 cc = gchar_cursor();
5532 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 {
5534 curwin->w_cursor = pos;
5535 return;
5536 }
5537 curwin->w_cursor = pos;
5538 }
5539
5540#ifdef FEAT_COMMENTS
5541 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5542 * comments. */
5543 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5544 && get_leader_len(old, NULL, FALSE) == 0)
5545 return;
5546#endif
5547
5548 /*
5549 * May start formatting in a previous line, so that after "x" a word is
5550 * moved to the previous line if it fits there now. Only when this is not
5551 * the start of a paragraph.
5552 */
5553 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5554 {
5555 --curwin->w_cursor.lnum;
5556 if (u_save_cursor() == FAIL)
5557 return;
5558 }
5559
5560 /*
5561 * Do the formatting and restore the cursor position. "saved_cursor" will
5562 * be adjusted for the text formatting.
5563 */
5564 saved_cursor = pos;
5565 format_lines((linenr_T)-1);
5566 curwin->w_cursor = saved_cursor;
5567 saved_cursor.lnum = 0;
5568
5569 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5570 {
5571 /* "cannot happen" */
5572 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5573 coladvance((colnr_T)MAXCOL);
5574 }
5575 else
5576 check_cursor_col();
5577
5578 /* Insert mode: If the cursor is now after the end of the line while it
5579 * previously wasn't, the line was broken. Because of the rule above we
5580 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5581 * formatted. */
5582 if (!wasatend && has_format_option(FO_WHITE_PAR))
5583 {
5584 new = ml_get_curline();
5585 len = STRLEN(new);
5586 if (curwin->w_cursor.col == len)
5587 {
5588 pnew = vim_strnsave(new, len + 2);
5589 pnew[len] = ' ';
5590 pnew[len + 1] = NUL;
5591 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5592 /* remove the space later */
5593 did_add_space = TRUE;
5594 }
5595 else
5596 /* may remove added space */
5597 check_auto_format(FALSE);
5598 }
5599
5600 check_cursor();
5601}
5602
5603/*
5604 * When an extra space was added to continue a paragraph for auto-formatting,
5605 * delete it now. The space must be under the cursor, just after the insert
5606 * position.
5607 */
5608 static void
5609check_auto_format(end_insert)
5610 int end_insert; /* TRUE when ending Insert mode */
5611{
5612 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005613 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614
5615 if (did_add_space)
5616 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005617 cc = gchar_cursor();
5618 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 /* Somehow the space was removed already. */
5620 did_add_space = FALSE;
5621 else
5622 {
5623 if (!end_insert)
5624 {
5625 inc_cursor();
5626 c = gchar_cursor();
5627 dec_cursor();
5628 }
5629 if (c != NUL)
5630 {
5631 /* The space is no longer at the end of the line, delete it. */
5632 del_char(FALSE);
5633 did_add_space = FALSE;
5634 }
5635 }
5636 }
5637}
5638
5639/*
5640 * Find out textwidth to be used for formatting:
5641 * if 'textwidth' option is set, use it
5642 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5643 * if invalid value, use 0.
5644 * Set default to window width (maximum 79) for "gq" operator.
5645 */
5646 int
5647comp_textwidth(ff)
5648 int ff; /* force formatting (for "Q" command) */
5649{
5650 int textwidth;
5651
5652 textwidth = curbuf->b_p_tw;
5653 if (textwidth == 0 && curbuf->b_p_wm)
5654 {
5655 /* The width is the window width minus 'wrapmargin' minus all the
5656 * things that add to the margin. */
5657 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5658#ifdef FEAT_CMDWIN
5659 if (cmdwin_type != 0)
5660 textwidth -= 1;
5661#endif
5662#ifdef FEAT_FOLDING
5663 textwidth -= curwin->w_p_fdc;
5664#endif
5665#ifdef FEAT_SIGNS
5666 if (curwin->w_buffer->b_signlist != NULL
5667# ifdef FEAT_NETBEANS_INTG
5668 || usingNetbeans
5669# endif
5670 )
5671 textwidth -= 1;
5672#endif
5673 if (curwin->w_p_nu)
5674 textwidth -= 8;
5675 }
5676 if (textwidth < 0)
5677 textwidth = 0;
5678 if (ff && textwidth == 0)
5679 {
5680 textwidth = W_WIDTH(curwin) - 1;
5681 if (textwidth > 79)
5682 textwidth = 79;
5683 }
5684 return textwidth;
5685}
5686
5687/*
5688 * Put a character in the redo buffer, for when just after a CTRL-V.
5689 */
5690 static void
5691redo_literal(c)
5692 int c;
5693{
5694 char_u buf[10];
5695
5696 /* Only digits need special treatment. Translate them into a string of
5697 * three digits. */
5698 if (VIM_ISDIGIT(c))
5699 {
5700 sprintf((char *)buf, "%03d", c);
5701 AppendToRedobuff(buf);
5702 }
5703 else
5704 AppendCharToRedobuff(c);
5705}
5706
5707/*
5708 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005709 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 */
5711 static void
5712start_arrow(end_insert_pos)
5713 pos_T *end_insert_pos;
5714{
5715 if (!arrow_used) /* something has been inserted */
5716 {
5717 AppendToRedobuff(ESC_STR);
5718 stop_insert(end_insert_pos, FALSE);
5719 arrow_used = TRUE; /* this means we stopped the current insert */
5720 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005721#ifdef FEAT_SYN_HL
5722 check_spell_redraw();
5723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724}
5725
Bram Moolenaar217ad922005-03-20 22:37:15 +00005726#ifdef FEAT_SYN_HL
5727/*
5728 * If we skipped highlighting word at cursor, do it now.
5729 * It may be skipped again, thus reset spell_redraw_lnum first.
5730 */
5731 static void
5732check_spell_redraw()
5733{
5734 if (spell_redraw_lnum != 0)
5735 {
5736 linenr_T lnum = spell_redraw_lnum;
5737
5738 spell_redraw_lnum = 0;
5739 redrawWinline(lnum, FALSE);
5740 }
5741}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005742
5743/*
5744 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5745 * spelled word, if there is one.
5746 */
5747 static void
5748spell_back_to_badword()
5749{
5750 pos_T tpos = curwin->w_cursor;
5751
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005752 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005753 if (curwin->w_cursor.col != tpos.col)
5754 start_arrow(&tpos);
5755}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005756#endif
5757
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758/*
5759 * stop_arrow() is called before a change is made in insert mode.
5760 * If an arrow key has been used, start a new insertion.
5761 * Returns FAIL if undo is impossible, shouldn't insert then.
5762 */
5763 int
5764stop_arrow()
5765{
5766 if (arrow_used)
5767 {
5768 if (u_save_cursor() == OK)
5769 {
5770 arrow_used = FALSE;
5771 ins_need_undo = FALSE;
5772 }
5773 Insstart = curwin->w_cursor; /* new insertion starts here */
5774 Insstart_textlen = linetabsize(ml_get_curline());
5775 ai_col = 0;
5776#ifdef FEAT_VREPLACE
5777 if (State & VREPLACE_FLAG)
5778 {
5779 orig_line_count = curbuf->b_ml.ml_line_count;
5780 vr_lines_changed = 1;
5781 }
5782#endif
5783 ResetRedobuff();
5784 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005785 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 }
5787 else if (ins_need_undo)
5788 {
5789 if (u_save_cursor() == OK)
5790 ins_need_undo = FALSE;
5791 }
5792
5793#ifdef FEAT_FOLDING
5794 /* Always open fold at the cursor line when inserting something. */
5795 foldOpenCursor();
5796#endif
5797
5798 return (arrow_used || ins_need_undo ? FAIL : OK);
5799}
5800
5801/*
5802 * do a few things to stop inserting
5803 */
5804 static void
5805stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005806 pos_T *end_insert_pos; /* where insert ended */
5807 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005809 int cc;
5810 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811
5812 stop_redo_ins();
5813 replace_flush(); /* abandon replace stack */
5814
5815 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005816 * Save the inserted text for later redo with ^@ and CTRL-A.
5817 * Don't do it when "restart_edit" was set and nothing was inserted,
5818 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005820 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005821 if (did_restart_edit == 0 || (ptr != NULL
5822 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005823 {
5824 vim_free(last_insert);
5825 last_insert = ptr;
5826 last_insert_skip = new_insert_skip;
5827 }
5828 else
5829 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830
5831 if (!arrow_used)
5832 {
5833 /* Auto-format now. It may seem strange to do this when stopping an
5834 * insertion (or moving the cursor), but it's required when appending
5835 * a line and having it end in a space. But only do it when something
5836 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005837 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005839 pos_T tpos = curwin->w_cursor;
5840
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 /* When the cursor is at the end of the line after a space the
5842 * formatting will move it to the following word. Avoid that by
5843 * moving the cursor onto the space. */
5844 cc = 'x';
5845 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5846 {
5847 dec_cursor();
5848 cc = gchar_cursor();
5849 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005850 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 }
5852
5853 auto_format(TRUE, FALSE);
5854
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005855 if (vim_iswhite(cc))
5856 {
5857 if (gchar_cursor() != NUL)
5858 inc_cursor();
5859#ifdef FEAT_VIRTUALEDIT
5860 /* If the cursor is still at the same character, also keep
5861 * the "coladd". */
5862 if (gchar_cursor() == NUL
5863 && curwin->w_cursor.lnum == tpos.lnum
5864 && curwin->w_cursor.col == tpos.col)
5865 curwin->w_cursor.coladd = tpos.coladd;
5866#endif
5867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 }
5869
5870 /* If a space was inserted for auto-formatting, remove it now. */
5871 check_auto_format(TRUE);
5872
5873 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005874 * of the line, and put the cursor back.
5875 * Do this when ESC was used or moving the cursor up/down. */
5876 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5877 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005879 pos_T tpos = curwin->w_cursor;
5880
5881 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5883 --curwin->w_cursor.col;
5884 while (cc = gchar_cursor(), vim_iswhite(cc))
5885 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005886 if (curwin->w_cursor.lnum != tpos.lnum)
5887 curwin->w_cursor = tpos;
5888 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5890
5891#ifdef FEAT_VISUAL
5892 /* <C-S-Right> may have started Visual mode, adjust the position for
5893 * deleted characters. */
5894 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5895 {
5896 cc = STRLEN(ml_get_curline());
5897 if (VIsual.col > (colnr_T)cc)
5898 {
5899 VIsual.col = cc;
5900# ifdef FEAT_VIRTUALEDIT
5901 VIsual.coladd = 0;
5902# endif
5903 }
5904 }
5905#endif
5906 }
5907 }
5908 did_ai = FALSE;
5909#ifdef FEAT_SMARTINDENT
5910 did_si = FALSE;
5911 can_si = FALSE;
5912 can_si_back = FALSE;
5913#endif
5914
5915 /* set '[ and '] to the inserted text */
5916 curbuf->b_op_start = Insstart;
5917 curbuf->b_op_end = *end_insert_pos;
5918}
5919
5920/*
5921 * Set the last inserted text to a single character.
5922 * Used for the replace command.
5923 */
5924 void
5925set_last_insert(c)
5926 int c;
5927{
5928 char_u *s;
5929
5930 vim_free(last_insert);
5931#ifdef FEAT_MBYTE
5932 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5933#else
5934 last_insert = alloc(6);
5935#endif
5936 if (last_insert != NULL)
5937 {
5938 s = last_insert;
5939 /* Use the CTRL-V only when entering a special char */
5940 if (c < ' ' || c == DEL)
5941 *s++ = Ctrl_V;
5942 s = add_char2buf(c, s);
5943 *s++ = ESC;
5944 *s++ = NUL;
5945 last_insert_skip = 0;
5946 }
5947}
5948
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005949#if defined(EXITFREE) || defined(PROTO)
5950 void
5951free_last_insert()
5952{
5953 vim_free(last_insert);
5954 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005955 vim_free(compl_orig_text);
5956 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005957}
5958#endif
5959
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960/*
5961 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5962 * and CSI. Handle multi-byte characters.
5963 * Returns a pointer to after the added bytes.
5964 */
5965 char_u *
5966add_char2buf(c, s)
5967 int c;
5968 char_u *s;
5969{
5970#ifdef FEAT_MBYTE
5971 char_u temp[MB_MAXBYTES];
5972 int i;
5973 int len;
5974
5975 len = (*mb_char2bytes)(c, temp);
5976 for (i = 0; i < len; ++i)
5977 {
5978 c = temp[i];
5979#endif
5980 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
5981 if (c == K_SPECIAL)
5982 {
5983 *s++ = K_SPECIAL;
5984 *s++ = KS_SPECIAL;
5985 *s++ = KE_FILLER;
5986 }
5987#ifdef FEAT_GUI
5988 else if (c == CSI)
5989 {
5990 *s++ = CSI;
5991 *s++ = KS_EXTRA;
5992 *s++ = (int)KE_CSI;
5993 }
5994#endif
5995 else
5996 *s++ = c;
5997#ifdef FEAT_MBYTE
5998 }
5999#endif
6000 return s;
6001}
6002
6003/*
6004 * move cursor to start of line
6005 * if flags & BL_WHITE move to first non-white
6006 * if flags & BL_SOL move to first non-white if startofline is set,
6007 * otherwise keep "curswant" column
6008 * if flags & BL_FIX don't leave the cursor on a NUL.
6009 */
6010 void
6011beginline(flags)
6012 int flags;
6013{
6014 if ((flags & BL_SOL) && !p_sol)
6015 coladvance(curwin->w_curswant);
6016 else
6017 {
6018 curwin->w_cursor.col = 0;
6019#ifdef FEAT_VIRTUALEDIT
6020 curwin->w_cursor.coladd = 0;
6021#endif
6022
6023 if (flags & (BL_WHITE | BL_SOL))
6024 {
6025 char_u *ptr;
6026
6027 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6028 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6029 ++curwin->w_cursor.col;
6030 }
6031 curwin->w_set_curswant = TRUE;
6032 }
6033}
6034
6035/*
6036 * oneright oneleft cursor_down cursor_up
6037 *
6038 * Move one char {right,left,down,up}.
6039 * Doesn't move onto the NUL past the end of the line.
6040 * Return OK when successful, FAIL when we hit a line of file boundary.
6041 */
6042
6043 int
6044oneright()
6045{
6046 char_u *ptr;
6047#ifdef FEAT_MBYTE
6048 int l;
6049#endif
6050
6051#ifdef FEAT_VIRTUALEDIT
6052 if (virtual_active())
6053 {
6054 pos_T prevpos = curwin->w_cursor;
6055
6056 /* Adjust for multi-wide char (excluding TAB) */
6057 ptr = ml_get_cursor();
6058 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
6059#ifdef FEAT_MBYTE
6060 (*mb_ptr2char)(ptr)
6061#else
6062 *ptr
6063#endif
6064 ))
6065 ? ptr2cells(ptr) : 1));
6066 curwin->w_set_curswant = TRUE;
6067 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6068 return (prevpos.col != curwin->w_cursor.col
6069 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6070 }
6071#endif
6072
6073 ptr = ml_get_cursor();
6074#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006075 if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 {
6077 /* The character under the cursor is a multi-byte character, move
6078 * several bytes right, but don't end up on the NUL. */
6079 if (ptr[l] == NUL)
6080 return FAIL;
6081 curwin->w_cursor.col += l;
6082 }
6083 else
6084#endif
6085 {
6086 if (*ptr++ == NUL || *ptr == NUL)
6087 return FAIL;
6088 ++curwin->w_cursor.col;
6089 }
6090
6091 curwin->w_set_curswant = TRUE;
6092 return OK;
6093}
6094
6095 int
6096oneleft()
6097{
6098#ifdef FEAT_VIRTUALEDIT
6099 if (virtual_active())
6100 {
6101 int width;
6102 int v = getviscol();
6103
6104 if (v == 0)
6105 return FAIL;
6106
6107# ifdef FEAT_LINEBREAK
6108 /* We might get stuck on 'showbreak', skip over it. */
6109 width = 1;
6110 for (;;)
6111 {
6112 coladvance(v - width);
6113 /* getviscol() is slow, skip it when 'showbreak' is empty and
6114 * there are no multi-byte characters */
6115 if ((*p_sbr == NUL
6116# ifdef FEAT_MBYTE
6117 && !has_mbyte
6118# endif
6119 ) || getviscol() < v)
6120 break;
6121 ++width;
6122 }
6123# else
6124 coladvance(v - 1);
6125# endif
6126
6127 if (curwin->w_cursor.coladd == 1)
6128 {
6129 char_u *ptr;
6130
6131 /* Adjust for multi-wide char (not a TAB) */
6132 ptr = ml_get_cursor();
6133 if (*ptr != TAB && vim_isprintc(
6134# ifdef FEAT_MBYTE
6135 (*mb_ptr2char)(ptr)
6136# else
6137 *ptr
6138# endif
6139 ) && ptr2cells(ptr) > 1)
6140 curwin->w_cursor.coladd = 0;
6141 }
6142
6143 curwin->w_set_curswant = TRUE;
6144 return OK;
6145 }
6146#endif
6147
6148 if (curwin->w_cursor.col == 0)
6149 return FAIL;
6150
6151 curwin->w_set_curswant = TRUE;
6152 --curwin->w_cursor.col;
6153
6154#ifdef FEAT_MBYTE
6155 /* if the character on the left of the current cursor is a multi-byte
6156 * character, move to its first byte */
6157 if (has_mbyte)
6158 mb_adjust_cursor();
6159#endif
6160 return OK;
6161}
6162
6163 int
6164cursor_up(n, upd_topline)
6165 long n;
6166 int upd_topline; /* When TRUE: update topline */
6167{
6168 linenr_T lnum;
6169
6170 if (n > 0)
6171 {
6172 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006173 /* This fails if the cursor is already in the first line or the count
6174 * is larger than the line number and '-' is in 'cpoptions' */
6175 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 return FAIL;
6177 if (n >= lnum)
6178 lnum = 1;
6179 else
6180#ifdef FEAT_FOLDING
6181 if (hasAnyFolding(curwin))
6182 {
6183 /*
6184 * Count each sequence of folded lines as one logical line.
6185 */
6186 /* go to the the start of the current fold */
6187 (void)hasFolding(lnum, &lnum, NULL);
6188
6189 while (n--)
6190 {
6191 /* move up one line */
6192 --lnum;
6193 if (lnum <= 1)
6194 break;
6195 /* If we entered a fold, move to the beginning, unless in
6196 * Insert mode or when 'foldopen' contains "all": it will open
6197 * in a moment. */
6198 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6199 (void)hasFolding(lnum, &lnum, NULL);
6200 }
6201 if (lnum < 1)
6202 lnum = 1;
6203 }
6204 else
6205#endif
6206 lnum -= n;
6207 curwin->w_cursor.lnum = lnum;
6208 }
6209
6210 /* try to advance to the column we want to be at */
6211 coladvance(curwin->w_curswant);
6212
6213 if (upd_topline)
6214 update_topline(); /* make sure curwin->w_topline is valid */
6215
6216 return OK;
6217}
6218
6219/*
6220 * Cursor down a number of logical lines.
6221 */
6222 int
6223cursor_down(n, upd_topline)
6224 long n;
6225 int upd_topline; /* When TRUE: update topline */
6226{
6227 linenr_T lnum;
6228
6229 if (n > 0)
6230 {
6231 lnum = curwin->w_cursor.lnum;
6232#ifdef FEAT_FOLDING
6233 /* Move to last line of fold, will fail if it's the end-of-file. */
6234 (void)hasFolding(lnum, NULL, &lnum);
6235#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006236 /* This fails if the cursor is already in the last line or would move
6237 * beyound the last line and '-' is in 'cpoptions' */
6238 if (lnum >= curbuf->b_ml.ml_line_count
6239 || (lnum + n > curbuf->b_ml.ml_line_count
6240 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 return FAIL;
6242 if (lnum + n >= curbuf->b_ml.ml_line_count)
6243 lnum = curbuf->b_ml.ml_line_count;
6244 else
6245#ifdef FEAT_FOLDING
6246 if (hasAnyFolding(curwin))
6247 {
6248 linenr_T last;
6249
6250 /* count each sequence of folded lines as one logical line */
6251 while (n--)
6252 {
6253 if (hasFolding(lnum, NULL, &last))
6254 lnum = last + 1;
6255 else
6256 ++lnum;
6257 if (lnum >= curbuf->b_ml.ml_line_count)
6258 break;
6259 }
6260 if (lnum > curbuf->b_ml.ml_line_count)
6261 lnum = curbuf->b_ml.ml_line_count;
6262 }
6263 else
6264#endif
6265 lnum += n;
6266 curwin->w_cursor.lnum = lnum;
6267 }
6268
6269 /* try to advance to the column we want to be at */
6270 coladvance(curwin->w_curswant);
6271
6272 if (upd_topline)
6273 update_topline(); /* make sure curwin->w_topline is valid */
6274
6275 return OK;
6276}
6277
6278/*
6279 * Stuff the last inserted text in the read buffer.
6280 * Last_insert actually is a copy of the redo buffer, so we
6281 * first have to remove the command.
6282 */
6283 int
6284stuff_inserted(c, count, no_esc)
6285 int c; /* Command character to be inserted */
6286 long count; /* Repeat this many times */
6287 int no_esc; /* Don't add an ESC at the end */
6288{
6289 char_u *esc_ptr;
6290 char_u *ptr;
6291 char_u *last_ptr;
6292 char_u last = NUL;
6293
6294 ptr = get_last_insert();
6295 if (ptr == NULL)
6296 {
6297 EMSG(_(e_noinstext));
6298 return FAIL;
6299 }
6300
6301 /* may want to stuff the command character, to start Insert mode */
6302 if (c != NUL)
6303 stuffcharReadbuff(c);
6304 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6305 *esc_ptr = NUL; /* remove the ESC */
6306
6307 /* when the last char is either "0" or "^" it will be quoted if no ESC
6308 * comes after it OR if it will inserted more than once and "ptr"
6309 * starts with ^D. -- Acevedo
6310 */
6311 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6312 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6313 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6314 {
6315 last = *last_ptr;
6316 *last_ptr = NUL;
6317 }
6318
6319 do
6320 {
6321 stuffReadbuff(ptr);
6322 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6323 if (last)
6324 stuffReadbuff((char_u *)(last == '0'
6325 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6326 : IF_EB("\026^", CTRL_V_STR "^")));
6327 }
6328 while (--count > 0);
6329
6330 if (last)
6331 *last_ptr = last;
6332
6333 if (esc_ptr != NULL)
6334 *esc_ptr = ESC; /* put the ESC back */
6335
6336 /* may want to stuff a trailing ESC, to get out of Insert mode */
6337 if (!no_esc)
6338 stuffcharReadbuff(ESC);
6339
6340 return OK;
6341}
6342
6343 char_u *
6344get_last_insert()
6345{
6346 if (last_insert == NULL)
6347 return NULL;
6348 return last_insert + last_insert_skip;
6349}
6350
6351/*
6352 * Get last inserted string, and remove trailing <Esc>.
6353 * Returns pointer to allocated memory (must be freed) or NULL.
6354 */
6355 char_u *
6356get_last_insert_save()
6357{
6358 char_u *s;
6359 int len;
6360
6361 if (last_insert == NULL)
6362 return NULL;
6363 s = vim_strsave(last_insert + last_insert_skip);
6364 if (s != NULL)
6365 {
6366 len = (int)STRLEN(s);
6367 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6368 s[len - 1] = NUL;
6369 }
6370 return s;
6371}
6372
6373/*
6374 * Check the word in front of the cursor for an abbreviation.
6375 * Called when the non-id character "c" has been entered.
6376 * When an abbreviation is recognized it is removed from the text and
6377 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6378 */
6379 static int
6380echeck_abbr(c)
6381 int c;
6382{
6383 /* Don't check for abbreviation in paste mode, when disabled and just
6384 * after moving around with cursor keys. */
6385 if (p_paste || no_abbr || arrow_used)
6386 return FALSE;
6387
6388 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6389 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6390}
6391
6392/*
6393 * replace-stack functions
6394 *
6395 * When replacing characters, the replaced characters are remembered for each
6396 * new character. This is used to re-insert the old text when backspacing.
6397 *
6398 * There is a NUL headed list of characters for each character that is
6399 * currently in the file after the insertion point. When BS is used, one NUL
6400 * headed list is put back for the deleted character.
6401 *
6402 * For a newline, there are two NUL headed lists. One contains the characters
6403 * that the NL replaced. The extra one stores the characters after the cursor
6404 * that were deleted (always white space).
6405 *
6406 * Replace_offset is normally 0, in which case replace_push will add a new
6407 * character at the end of the stack. If replace_offset is not 0, that many
6408 * characters will be left on the stack above the newly inserted character.
6409 */
6410
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006411static char_u *replace_stack = NULL;
6412static long replace_stack_nr = 0; /* next entry in replace stack */
6413static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414
6415 void
6416replace_push(c)
6417 int c; /* character that is replaced (NUL is none) */
6418{
6419 char_u *p;
6420
6421 if (replace_stack_nr < replace_offset) /* nothing to do */
6422 return;
6423 if (replace_stack_len <= replace_stack_nr)
6424 {
6425 replace_stack_len += 50;
6426 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6427 if (p == NULL) /* out of memory */
6428 {
6429 replace_stack_len -= 50;
6430 return;
6431 }
6432 if (replace_stack != NULL)
6433 {
6434 mch_memmove(p, replace_stack,
6435 (size_t)(replace_stack_nr * sizeof(char_u)));
6436 vim_free(replace_stack);
6437 }
6438 replace_stack = p;
6439 }
6440 p = replace_stack + replace_stack_nr - replace_offset;
6441 if (replace_offset)
6442 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6443 *p = c;
6444 ++replace_stack_nr;
6445}
6446
6447/*
6448 * call replace_push(c) with replace_offset set to the first NUL.
6449 */
6450 static void
6451replace_push_off(c)
6452 int c;
6453{
6454 char_u *p;
6455
6456 p = replace_stack + replace_stack_nr;
6457 for (replace_offset = 1; replace_offset < replace_stack_nr;
6458 ++replace_offset)
6459 if (*--p == NUL)
6460 break;
6461 replace_push(c);
6462 replace_offset = 0;
6463}
6464
6465/*
6466 * Pop one item from the replace stack.
6467 * return -1 if stack empty
6468 * return replaced character or NUL otherwise
6469 */
6470 static int
6471replace_pop()
6472{
6473 if (replace_stack_nr == 0)
6474 return -1;
6475 return (int)replace_stack[--replace_stack_nr];
6476}
6477
6478/*
6479 * Join the top two items on the replace stack. This removes to "off"'th NUL
6480 * encountered.
6481 */
6482 static void
6483replace_join(off)
6484 int off; /* offset for which NUL to remove */
6485{
6486 int i;
6487
6488 for (i = replace_stack_nr; --i >= 0; )
6489 if (replace_stack[i] == NUL && off-- <= 0)
6490 {
6491 --replace_stack_nr;
6492 mch_memmove(replace_stack + i, replace_stack + i + 1,
6493 (size_t)(replace_stack_nr - i));
6494 return;
6495 }
6496}
6497
6498/*
6499 * Pop bytes from the replace stack until a NUL is found, and insert them
6500 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6501 */
6502 static void
6503replace_pop_ins()
6504{
6505 int cc;
6506 int oldState = State;
6507
6508 State = NORMAL; /* don't want REPLACE here */
6509 while ((cc = replace_pop()) > 0)
6510 {
6511#ifdef FEAT_MBYTE
6512 mb_replace_pop_ins(cc);
6513#else
6514 ins_char(cc);
6515#endif
6516 dec_cursor();
6517 }
6518 State = oldState;
6519}
6520
6521#ifdef FEAT_MBYTE
6522/*
6523 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6524 * indicates a multi-byte char, pop the other bytes too.
6525 */
6526 static void
6527mb_replace_pop_ins(cc)
6528 int cc;
6529{
6530 int n;
6531 char_u buf[MB_MAXBYTES];
6532 int i;
6533 int c;
6534
6535 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6536 {
6537 buf[0] = cc;
6538 for (i = 1; i < n; ++i)
6539 buf[i] = replace_pop();
6540 ins_bytes_len(buf, n);
6541 }
6542 else
6543 ins_char(cc);
6544
6545 if (enc_utf8)
6546 /* Handle composing chars. */
6547 for (;;)
6548 {
6549 c = replace_pop();
6550 if (c == -1) /* stack empty */
6551 break;
6552 if ((n = MB_BYTE2LEN(c)) == 1)
6553 {
6554 /* Not a multi-byte char, put it back. */
6555 replace_push(c);
6556 break;
6557 }
6558 else
6559 {
6560 buf[0] = c;
6561 for (i = 1; i < n; ++i)
6562 buf[i] = replace_pop();
6563 if (utf_iscomposing(utf_ptr2char(buf)))
6564 ins_bytes_len(buf, n);
6565 else
6566 {
6567 /* Not a composing char, put it back. */
6568 for (i = n - 1; i >= 0; --i)
6569 replace_push(buf[i]);
6570 break;
6571 }
6572 }
6573 }
6574}
6575#endif
6576
6577/*
6578 * make the replace stack empty
6579 * (called when exiting replace mode)
6580 */
6581 static void
6582replace_flush()
6583{
6584 vim_free(replace_stack);
6585 replace_stack = NULL;
6586 replace_stack_len = 0;
6587 replace_stack_nr = 0;
6588}
6589
6590/*
6591 * Handle doing a BS for one character.
6592 * cc < 0: replace stack empty, just move cursor
6593 * cc == 0: character was inserted, delete it
6594 * cc > 0: character was replaced, put cc (first byte of original char) back
6595 * and check for more characters to be put back
6596 */
6597 static void
6598replace_do_bs()
6599{
6600 int cc;
6601#ifdef FEAT_VREPLACE
6602 int orig_len = 0;
6603 int ins_len;
6604 int orig_vcols = 0;
6605 colnr_T start_vcol;
6606 char_u *p;
6607 int i;
6608 int vcol;
6609#endif
6610
6611 cc = replace_pop();
6612 if (cc > 0)
6613 {
6614#ifdef FEAT_VREPLACE
6615 if (State & VREPLACE_FLAG)
6616 {
6617 /* Get the number of screen cells used by the character we are
6618 * going to delete. */
6619 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6620 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6621 }
6622#endif
6623#ifdef FEAT_MBYTE
6624 if (has_mbyte)
6625 {
6626 del_char(FALSE);
6627# ifdef FEAT_VREPLACE
6628 if (State & VREPLACE_FLAG)
6629 orig_len = STRLEN(ml_get_cursor());
6630# endif
6631 replace_push(cc);
6632 }
6633 else
6634#endif
6635 {
6636 pchar_cursor(cc);
6637#ifdef FEAT_VREPLACE
6638 if (State & VREPLACE_FLAG)
6639 orig_len = STRLEN(ml_get_cursor()) - 1;
6640#endif
6641 }
6642 replace_pop_ins();
6643
6644#ifdef FEAT_VREPLACE
6645 if (State & VREPLACE_FLAG)
6646 {
6647 /* Get the number of screen cells used by the inserted characters */
6648 p = ml_get_cursor();
6649 ins_len = STRLEN(p) - orig_len;
6650 vcol = start_vcol;
6651 for (i = 0; i < ins_len; ++i)
6652 {
6653 vcol += chartabsize(p + i, vcol);
6654#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006655 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656#endif
6657 }
6658 vcol -= start_vcol;
6659
6660 /* Delete spaces that were inserted after the cursor to keep the
6661 * text aligned. */
6662 curwin->w_cursor.col += ins_len;
6663 while (vcol > orig_vcols && gchar_cursor() == ' ')
6664 {
6665 del_char(FALSE);
6666 ++orig_vcols;
6667 }
6668 curwin->w_cursor.col -= ins_len;
6669 }
6670#endif
6671
6672 /* mark the buffer as changed and prepare for displaying */
6673 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6674 }
6675 else if (cc == 0)
6676 (void)del_char(FALSE);
6677}
6678
6679#ifdef FEAT_CINDENT
6680/*
6681 * Return TRUE if C-indenting is on.
6682 */
6683 static int
6684cindent_on()
6685{
6686 return (!p_paste && (curbuf->b_p_cin
6687# ifdef FEAT_EVAL
6688 || *curbuf->b_p_inde != NUL
6689# endif
6690 ));
6691}
6692#endif
6693
6694#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6695/*
6696 * Re-indent the current line, based on the current contents of it and the
6697 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6698 * confused what all the part that handles Control-T is doing that I'm not.
6699 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6700 */
6701
6702 void
6703fixthisline(get_the_indent)
6704 int (*get_the_indent) __ARGS((void));
6705{
6706 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6707 if (linewhite(curwin->w_cursor.lnum))
6708 did_ai = TRUE; /* delete the indent if the line stays empty */
6709}
6710
6711 void
6712fix_indent()
6713{
6714 if (p_paste)
6715 return;
6716# ifdef FEAT_LISP
6717 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6718 fixthisline(get_lisp_indent);
6719# endif
6720# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6721 else
6722# endif
6723# ifdef FEAT_CINDENT
6724 if (cindent_on())
6725 do_c_expr_indent();
6726# endif
6727}
6728
6729#endif
6730
6731#ifdef FEAT_CINDENT
6732/*
6733 * return TRUE if 'cinkeys' contains the key "keytyped",
6734 * when == '*': Only if key is preceded with '*' (indent before insert)
6735 * when == '!': Only if key is prededed with '!' (don't insert)
6736 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6737 *
6738 * "keytyped" can have a few special values:
6739 * KEY_OPEN_FORW
6740 * KEY_OPEN_BACK
6741 * KEY_COMPLETE just finished completion.
6742 *
6743 * If line_is_empty is TRUE accept keys with '0' before them.
6744 */
6745 int
6746in_cinkeys(keytyped, when, line_is_empty)
6747 int keytyped;
6748 int when;
6749 int line_is_empty;
6750{
6751 char_u *look;
6752 int try_match;
6753 int try_match_word;
6754 char_u *p;
6755 char_u *line;
6756 int icase;
6757 int i;
6758
6759#ifdef FEAT_EVAL
6760 if (*curbuf->b_p_inde != NUL)
6761 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6762 else
6763#endif
6764 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6765 while (*look)
6766 {
6767 /*
6768 * Find out if we want to try a match with this key, depending on
6769 * 'when' and a '*' or '!' before the key.
6770 */
6771 switch (when)
6772 {
6773 case '*': try_match = (*look == '*'); break;
6774 case '!': try_match = (*look == '!'); break;
6775 default: try_match = (*look != '*'); break;
6776 }
6777 if (*look == '*' || *look == '!')
6778 ++look;
6779
6780 /*
6781 * If there is a '0', only accept a match if the line is empty.
6782 * But may still match when typing last char of a word.
6783 */
6784 if (*look == '0')
6785 {
6786 try_match_word = try_match;
6787 if (!line_is_empty)
6788 try_match = FALSE;
6789 ++look;
6790 }
6791 else
6792 try_match_word = FALSE;
6793
6794 /*
6795 * does it look like a control character?
6796 */
6797 if (*look == '^'
6798#ifdef EBCDIC
6799 && (Ctrl_chr(look[1]) != 0)
6800#else
6801 && look[1] >= '?' && look[1] <= '_'
6802#endif
6803 )
6804 {
6805 if (try_match && keytyped == Ctrl_chr(look[1]))
6806 return TRUE;
6807 look += 2;
6808 }
6809 /*
6810 * 'o' means "o" command, open forward.
6811 * 'O' means "O" command, open backward.
6812 */
6813 else if (*look == 'o')
6814 {
6815 if (try_match && keytyped == KEY_OPEN_FORW)
6816 return TRUE;
6817 ++look;
6818 }
6819 else if (*look == 'O')
6820 {
6821 if (try_match && keytyped == KEY_OPEN_BACK)
6822 return TRUE;
6823 ++look;
6824 }
6825
6826 /*
6827 * 'e' means to check for "else" at start of line and just before the
6828 * cursor.
6829 */
6830 else if (*look == 'e')
6831 {
6832 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6833 {
6834 p = ml_get_curline();
6835 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6836 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6837 return TRUE;
6838 }
6839 ++look;
6840 }
6841
6842 /*
6843 * ':' only causes an indent if it is at the end of a label or case
6844 * statement, or when it was before typing the ':' (to fix
6845 * class::method for C++).
6846 */
6847 else if (*look == ':')
6848 {
6849 if (try_match && keytyped == ':')
6850 {
6851 p = ml_get_curline();
6852 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6853 return TRUE;
6854 if (curwin->w_cursor.col > 2
6855 && p[curwin->w_cursor.col - 1] == ':'
6856 && p[curwin->w_cursor.col - 2] == ':')
6857 {
6858 p[curwin->w_cursor.col - 1] = ' ';
6859 i = (cin_iscase(p) || cin_isscopedecl(p)
6860 || cin_islabel(30));
6861 p = ml_get_curline();
6862 p[curwin->w_cursor.col - 1] = ':';
6863 if (i)
6864 return TRUE;
6865 }
6866 }
6867 ++look;
6868 }
6869
6870
6871 /*
6872 * Is it a key in <>, maybe?
6873 */
6874 else if (*look == '<')
6875 {
6876 if (try_match)
6877 {
6878 /*
6879 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6880 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6881 * >, *, : and ! keys if they really really want to.
6882 */
6883 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6884 && keytyped == look[1])
6885 return TRUE;
6886
6887 if (keytyped == get_special_key_code(look + 1))
6888 return TRUE;
6889 }
6890 while (*look && *look != '>')
6891 look++;
6892 while (*look == '>')
6893 look++;
6894 }
6895
6896 /*
6897 * Is it a word: "=word"?
6898 */
6899 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6900 {
6901 ++look;
6902 if (*look == '~')
6903 {
6904 icase = TRUE;
6905 ++look;
6906 }
6907 else
6908 icase = FALSE;
6909 p = vim_strchr(look, ',');
6910 if (p == NULL)
6911 p = look + STRLEN(look);
6912 if ((try_match || try_match_word)
6913 && curwin->w_cursor.col >= (colnr_T)(p - look))
6914 {
6915 int match = FALSE;
6916
6917#ifdef FEAT_INS_EXPAND
6918 if (keytyped == KEY_COMPLETE)
6919 {
6920 char_u *s;
6921
6922 /* Just completed a word, check if it starts with "look".
6923 * search back for the start of a word. */
6924 line = ml_get_curline();
6925# ifdef FEAT_MBYTE
6926 if (has_mbyte)
6927 {
6928 char_u *n;
6929
6930 for (s = line + curwin->w_cursor.col; s > line; s = n)
6931 {
6932 n = mb_prevptr(line, s);
6933 if (!vim_iswordp(n))
6934 break;
6935 }
6936 }
6937 else
6938# endif
6939 for (s = line + curwin->w_cursor.col; s > line; --s)
6940 if (!vim_iswordc(s[-1]))
6941 break;
6942 if (s + (p - look) <= line + curwin->w_cursor.col
6943 && (icase
6944 ? MB_STRNICMP(s, look, p - look)
6945 : STRNCMP(s, look, p - look)) == 0)
6946 match = TRUE;
6947 }
6948 else
6949#endif
6950 /* TODO: multi-byte */
6951 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6952 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6953 {
6954 line = ml_get_cursor();
6955 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6956 || !vim_iswordc(line[-(p - look) - 1]))
6957 && (icase
6958 ? MB_STRNICMP(line - (p - look), look, p - look)
6959 : STRNCMP(line - (p - look), look, p - look))
6960 == 0)
6961 match = TRUE;
6962 }
6963 if (match && try_match_word && !try_match)
6964 {
6965 /* "0=word": Check if there are only blanks before the
6966 * word. */
6967 line = ml_get_curline();
6968 if ((int)(skipwhite(line) - line) !=
6969 (int)(curwin->w_cursor.col - (p - look)))
6970 match = FALSE;
6971 }
6972 if (match)
6973 return TRUE;
6974 }
6975 look = p;
6976 }
6977
6978 /*
6979 * ok, it's a boring generic character.
6980 */
6981 else
6982 {
6983 if (try_match && *look == keytyped)
6984 return TRUE;
6985 ++look;
6986 }
6987
6988 /*
6989 * Skip over ", ".
6990 */
6991 look = skip_to_option_part(look);
6992 }
6993 return FALSE;
6994}
6995#endif /* FEAT_CINDENT */
6996
6997#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
6998/*
6999 * Map Hebrew keyboard when in hkmap mode.
7000 */
7001 int
7002hkmap(c)
7003 int c;
7004{
7005 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7006 {
7007 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7008 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7009 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7010 static char_u map[26] =
7011 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7012 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7013 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7014 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7015 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7016 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7017 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7018 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7019 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7020
7021 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7022 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7023 /* '-1'='sofit' */
7024 else if (c == 'x')
7025 return 'X';
7026 else if (c == 'q')
7027 return '\''; /* {geresh}={'} */
7028 else if (c == 246)
7029 return ' '; /* \"o --> ' ' for a german keyboard */
7030 else if (c == 228)
7031 return ' '; /* \"a --> ' ' -- / -- */
7032 else if (c == 252)
7033 return ' '; /* \"u --> ' ' -- / -- */
7034#ifdef EBCDIC
7035 else if (islower(c))
7036#else
7037 /* NOTE: islower() does not do the right thing for us on Linux so we
7038 * do this the same was as 5.7 and previous, so it works correctly on
7039 * all systems. Specifically, the e.g. Delete and Arrow keys are
7040 * munged and won't work if e.g. searching for Hebrew text.
7041 */
7042 else if (c >= 'a' && c <= 'z')
7043#endif
7044 return (int)(map[CharOrdLow(c)] + p_aleph);
7045 else
7046 return c;
7047 }
7048 else
7049 {
7050 switch (c)
7051 {
7052 case '`': return ';';
7053 case '/': return '.';
7054 case '\'': return ',';
7055 case 'q': return '/';
7056 case 'w': return '\'';
7057
7058 /* Hebrew letters - set offset from 'a' */
7059 case ',': c = '{'; break;
7060 case '.': c = 'v'; break;
7061 case ';': c = 't'; break;
7062 default: {
7063 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7064
7065#ifdef EBCDIC
7066 /* see note about islower() above */
7067 if (!islower(c))
7068#else
7069 if (c < 'a' || c > 'z')
7070#endif
7071 return c;
7072 c = str[CharOrdLow(c)];
7073 break;
7074 }
7075 }
7076
7077 return (int)(CharOrdLow(c) + p_aleph);
7078 }
7079}
7080#endif
7081
7082 static void
7083ins_reg()
7084{
7085 int need_redraw = FALSE;
7086 int regname;
7087 int literally = 0;
7088
7089 /*
7090 * If we are going to wait for a character, show a '"'.
7091 */
7092 pc_status = PC_STATUS_UNSET;
7093 if (redrawing() && !char_avail())
7094 {
7095 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007096 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097
7098 edit_putchar('"', TRUE);
7099#ifdef FEAT_CMDL_INFO
7100 add_to_showcmd_c(Ctrl_R);
7101#endif
7102 }
7103
7104#ifdef USE_ON_FLY_SCROLL
7105 dont_scroll = TRUE; /* disallow scrolling here */
7106#endif
7107
7108 /*
7109 * Don't map the register name. This also prevents the mode message to be
7110 * deleted when ESC is hit.
7111 */
7112 ++no_mapping;
7113 regname = safe_vgetc();
7114#ifdef FEAT_LANGMAP
7115 LANGMAP_ADJUST(regname, TRUE);
7116#endif
7117 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7118 {
7119 /* Get a third key for literal register insertion */
7120 literally = regname;
7121#ifdef FEAT_CMDL_INFO
7122 add_to_showcmd_c(literally);
7123#endif
7124 regname = safe_vgetc();
7125#ifdef FEAT_LANGMAP
7126 LANGMAP_ADJUST(regname, TRUE);
7127#endif
7128 }
7129 --no_mapping;
7130
7131#ifdef FEAT_EVAL
7132 /*
7133 * Don't call u_sync() while getting the expression,
7134 * evaluating it or giving an error message for it!
7135 */
7136 ++no_u_sync;
7137 if (regname == '=')
7138 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007139# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007143# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 /* Restore the Input Method. */
7145 if (im_on)
7146 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007149 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7150 {
7151 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 else
7155 {
7156#endif
7157 if (literally == Ctrl_O || literally == Ctrl_P)
7158 {
7159 /* Append the command to the redo buffer. */
7160 AppendCharToRedobuff(Ctrl_R);
7161 AppendCharToRedobuff(literally);
7162 AppendCharToRedobuff(regname);
7163
7164 do_put(regname, BACKWARD, 1L,
7165 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7166 }
7167 else if (insert_reg(regname, literally) == FAIL)
7168 {
7169 vim_beep();
7170 need_redraw = TRUE; /* remove the '"' */
7171 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007172 else if (stop_insert_mode)
7173 /* When the '=' register was used and a function was invoked that
7174 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7175 * insert anything, need to remove the '"' */
7176 need_redraw = TRUE;
7177
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178#ifdef FEAT_EVAL
7179 }
7180 --no_u_sync;
7181#endif
7182#ifdef FEAT_CMDL_INFO
7183 clear_showcmd();
7184#endif
7185
7186 /* If the inserted register is empty, we need to remove the '"' */
7187 if (need_redraw || stuff_empty())
7188 edit_unputchar();
7189}
7190
7191/*
7192 * CTRL-G commands in Insert mode.
7193 */
7194 static void
7195ins_ctrl_g()
7196{
7197 int c;
7198
7199#ifdef FEAT_INS_EXPAND
7200 /* Right after CTRL-X the cursor will be after the ruler. */
7201 setcursor();
7202#endif
7203
7204 /*
7205 * Don't map the second key. This also prevents the mode message to be
7206 * deleted when ESC is hit.
7207 */
7208 ++no_mapping;
7209 c = safe_vgetc();
7210 --no_mapping;
7211 switch (c)
7212 {
7213 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7214 case K_UP:
7215 case Ctrl_K:
7216 case 'k': ins_up(TRUE);
7217 break;
7218
7219 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7220 case K_DOWN:
7221 case Ctrl_J:
7222 case 'j': ins_down(TRUE);
7223 break;
7224
7225 /* CTRL-G u: start new undoable edit */
7226 case 'u': u_sync();
7227 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007228
7229 /* Need to reset Insstart, esp. because a BS that joins
7230 * aline to the previous one must save for undo. */
7231 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 break;
7233
7234 /* Unknown CTRL-G command, reserved for future expansion. */
7235 default: vim_beep();
7236 }
7237}
7238
7239/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007240 * CTRL-^ in Insert mode.
7241 */
7242 static void
7243ins_ctrl_hat()
7244{
7245 if (map_to_exists_mode((char_u *)"", LANGMAP))
7246 {
7247 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7248 if (State & LANGMAP)
7249 {
7250 curbuf->b_p_iminsert = B_IMODE_NONE;
7251 State &= ~LANGMAP;
7252 }
7253 else
7254 {
7255 curbuf->b_p_iminsert = B_IMODE_LMAP;
7256 State |= LANGMAP;
7257#ifdef USE_IM_CONTROL
7258 im_set_active(FALSE);
7259#endif
7260 }
7261 }
7262#ifdef USE_IM_CONTROL
7263 else
7264 {
7265 /* There are no ":lmap" mappings, toggle IM */
7266 if (im_get_status())
7267 {
7268 curbuf->b_p_iminsert = B_IMODE_NONE;
7269 im_set_active(FALSE);
7270 }
7271 else
7272 {
7273 curbuf->b_p_iminsert = B_IMODE_IM;
7274 State &= ~LANGMAP;
7275 im_set_active(TRUE);
7276 }
7277 }
7278#endif
7279 set_iminsert_global();
7280 showmode();
7281#ifdef FEAT_GUI
7282 /* may show different cursor shape or color */
7283 if (gui.in_use)
7284 gui_update_cursor(TRUE, FALSE);
7285#endif
7286#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7287 /* Show/unshow value of 'keymap' in status lines. */
7288 status_redraw_curbuf();
7289#endif
7290}
7291
7292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 * Handle ESC in insert mode.
7294 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7295 * insert.
7296 */
7297 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007298ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 long *count;
7300 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007301 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
7303 int temp;
7304 static int disabled_redraw = FALSE;
7305
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007306#ifdef FEAT_SYN_HL
7307 check_spell_redraw();
7308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309#if defined(FEAT_HANGULIN)
7310# if defined(ESC_CHG_TO_ENG_MODE)
7311 hangul_input_state_set(0);
7312# endif
7313 if (composing_hangul)
7314 {
7315 push_raw_key(composing_hangul_buffer, 2);
7316 composing_hangul = 0;
7317 }
7318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
7320 temp = curwin->w_cursor.col;
7321 if (disabled_redraw)
7322 {
7323 --RedrawingDisabled;
7324 disabled_redraw = FALSE;
7325 }
7326 if (!arrow_used)
7327 {
7328 /*
7329 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007330 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7331 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 */
7333 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007334 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335
7336 /*
7337 * Repeating insert may take a long time. Check for
7338 * interrupt now and then.
7339 */
7340 if (*count > 0)
7341 {
7342 line_breakcheck();
7343 if (got_int)
7344 *count = 0;
7345 }
7346
7347 if (--*count > 0) /* repeat what was typed */
7348 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007349 /* Vi repeats the insert without replacing characters. */
7350 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7351 State &= ~REPLACE_FLAG;
7352
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 (void)start_redo_ins();
7354 if (cmdchar == 'r' || cmdchar == 'v')
7355 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7356 ++RedrawingDisabled;
7357 disabled_redraw = TRUE;
7358 return FALSE; /* repeat the insert */
7359 }
7360 stop_insert(&curwin->w_cursor, TRUE);
7361 undisplay_dollar();
7362 }
7363
7364 /* When an autoindent was removed, curswant stays after the
7365 * indent */
7366 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7367 curwin->w_set_curswant = TRUE;
7368
7369 /* Remember the last Insert position in the '^ mark. */
7370 if (!cmdmod.keepjumps)
7371 curbuf->b_last_insert = curwin->w_cursor;
7372
7373 /*
7374 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007375 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007377 if (!nomove
7378 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379#ifdef FEAT_VIRTUALEDIT
7380 || curwin->w_cursor.coladd > 0
7381#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007382 )
7383 && (restart_edit == NUL
7384 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007386 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007388 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389#ifdef FEAT_RIGHTLEFT
7390 && !revins_on
7391#endif
7392 )
7393 {
7394#ifdef FEAT_VIRTUALEDIT
7395 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7396 {
7397 oneleft();
7398 if (restart_edit != NUL)
7399 ++curwin->w_cursor.coladd;
7400 }
7401 else
7402#endif
7403 {
7404 --curwin->w_cursor.col;
7405#ifdef FEAT_MBYTE
7406 /* Correct cursor for multi-byte character. */
7407 if (has_mbyte)
7408 mb_adjust_cursor();
7409#endif
7410 }
7411 }
7412
7413#ifdef USE_IM_CONTROL
7414 /* Disable IM to allow typing English directly for Normal mode commands.
7415 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7416 * well). */
7417 if (!(State & LANGMAP))
7418 im_save_status(&curbuf->b_p_iminsert);
7419 im_set_active(FALSE);
7420#endif
7421
7422 State = NORMAL;
7423 /* need to position cursor again (e.g. when on a TAB ) */
7424 changed_cline_bef_curs();
7425
7426#ifdef FEAT_MOUSE
7427 setmouse();
7428#endif
7429#ifdef CURSOR_SHAPE
7430 ui_cursor_shape(); /* may show different cursor shape */
7431#endif
7432
7433 /*
7434 * When recording or for CTRL-O, need to display the new mode.
7435 * Otherwise remove the mode message.
7436 */
7437 if (Recording || restart_edit != NUL)
7438 showmode();
7439 else if (p_smd)
7440 MSG("");
7441
7442 return TRUE; /* exit Insert mode */
7443}
7444
7445#ifdef FEAT_RIGHTLEFT
7446/*
7447 * Toggle language: hkmap and revins_on.
7448 * Move to end of reverse inserted text.
7449 */
7450 static void
7451ins_ctrl_()
7452{
7453 if (revins_on && revins_chars && revins_scol >= 0)
7454 {
7455 while (gchar_cursor() != NUL && revins_chars--)
7456 ++curwin->w_cursor.col;
7457 }
7458 p_ri = !p_ri;
7459 revins_on = (State == INSERT && p_ri);
7460 if (revins_on)
7461 {
7462 revins_scol = curwin->w_cursor.col;
7463 revins_legal++;
7464 revins_chars = 0;
7465 undisplay_dollar();
7466 }
7467 else
7468 revins_scol = -1;
7469#ifdef FEAT_FKMAP
7470 if (p_altkeymap)
7471 {
7472 /*
7473 * to be consistent also for redo command, using '.'
7474 * set arrow_used to true and stop it - causing to redo
7475 * characters entered in one mode (normal/reverse insert).
7476 */
7477 arrow_used = TRUE;
7478 (void)stop_arrow();
7479 p_fkmap = curwin->w_p_rl ^ p_ri;
7480 if (p_fkmap && p_ri)
7481 State = INSERT;
7482 }
7483 else
7484#endif
7485 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7486 showmode();
7487}
7488#endif
7489
7490#ifdef FEAT_VISUAL
7491/*
7492 * If 'keymodel' contains "startsel", may start selection.
7493 * Returns TRUE when a CTRL-O and other keys stuffed.
7494 */
7495 static int
7496ins_start_select(c)
7497 int c;
7498{
7499 if (km_startsel)
7500 switch (c)
7501 {
7502 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 case K_PAGEUP:
7505 case K_KPAGEUP:
7506 case K_PAGEDOWN:
7507 case K_KPAGEDOWN:
7508# ifdef MACOS
7509 case K_LEFT:
7510 case K_RIGHT:
7511 case K_UP:
7512 case K_DOWN:
7513 case K_END:
7514 case K_HOME:
7515# endif
7516 if (!(mod_mask & MOD_MASK_SHIFT))
7517 break;
7518 /* FALLTHROUGH */
7519 case K_S_LEFT:
7520 case K_S_RIGHT:
7521 case K_S_UP:
7522 case K_S_DOWN:
7523 case K_S_END:
7524 case K_S_HOME:
7525 /* Start selection right away, the cursor can move with
7526 * CTRL-O when beyond the end of the line. */
7527 start_selection();
7528
7529 /* Execute the key in (insert) Select mode. */
7530 stuffcharReadbuff(Ctrl_O);
7531 if (mod_mask)
7532 {
7533 char_u buf[4];
7534
7535 buf[0] = K_SPECIAL;
7536 buf[1] = KS_MODIFIER;
7537 buf[2] = mod_mask;
7538 buf[3] = NUL;
7539 stuffReadbuff(buf);
7540 }
7541 stuffcharReadbuff(c);
7542 return TRUE;
7543 }
7544 return FALSE;
7545}
7546#endif
7547
7548/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007549 * <Insert> key in Insert mode: toggle insert/remplace mode.
7550 */
7551 static void
7552ins_insert(replaceState)
7553 int replaceState;
7554{
7555#ifdef FEAT_FKMAP
7556 if (p_fkmap && p_ri)
7557 {
7558 beep_flush();
7559 EMSG(farsi_text_3); /* encoded in Farsi */
7560 return;
7561 }
7562#endif
7563
7564#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007565# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007566 set_vim_var_string(VV_INSERTMODE,
7567 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007568# ifdef FEAT_VREPLACE
7569 replaceState == VREPLACE ? "v" :
7570# endif
7571 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007572# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007573 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7574#endif
7575 if (State & REPLACE_FLAG)
7576 State = INSERT | (State & LANGMAP);
7577 else
7578 State = replaceState | (State & LANGMAP);
7579 AppendCharToRedobuff(K_INS);
7580 showmode();
7581#ifdef CURSOR_SHAPE
7582 ui_cursor_shape(); /* may show different cursor shape */
7583#endif
7584}
7585
7586/*
7587 * Pressed CTRL-O in Insert mode.
7588 */
7589 static void
7590ins_ctrl_o()
7591{
7592#ifdef FEAT_VREPLACE
7593 if (State & VREPLACE_FLAG)
7594 restart_edit = 'V';
7595 else
7596#endif
7597 if (State & REPLACE_FLAG)
7598 restart_edit = 'R';
7599 else
7600 restart_edit = 'I';
7601#ifdef FEAT_VIRTUALEDIT
7602 if (virtual_active())
7603 ins_at_eol = FALSE; /* cursor always keeps its column */
7604 else
7605#endif
7606 ins_at_eol = (gchar_cursor() == NUL);
7607}
7608
7609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 * If the cursor is on an indent, ^T/^D insert/delete one
7611 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7612 * Always round the indent to 'shiftwith', this is compatible
7613 * with vi. But vi only supports ^T and ^D after an
7614 * autoindent, we support it everywhere.
7615 */
7616 static void
7617ins_shift(c, lastc)
7618 int c;
7619 int lastc;
7620{
7621 if (stop_arrow() == FAIL)
7622 return;
7623 AppendCharToRedobuff(c);
7624
7625 /*
7626 * 0^D and ^^D: remove all indent.
7627 */
7628 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7629 {
7630 --curwin->w_cursor.col;
7631 (void)del_char(FALSE); /* delete the '^' or '0' */
7632 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7633 if (State & REPLACE_FLAG)
7634 replace_pop_ins();
7635 if (lastc == '^')
7636 old_indent = get_indent(); /* remember curr. indent */
7637 change_indent(INDENT_SET, 0, TRUE, 0);
7638 }
7639 else
7640 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7641
7642 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7643 did_ai = FALSE;
7644#ifdef FEAT_SMARTINDENT
7645 did_si = FALSE;
7646 can_si = FALSE;
7647 can_si_back = FALSE;
7648#endif
7649#ifdef FEAT_CINDENT
7650 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7651#endif
7652}
7653
7654 static void
7655ins_del()
7656{
7657 int temp;
7658
7659 if (stop_arrow() == FAIL)
7660 return;
7661 if (gchar_cursor() == NUL) /* delete newline */
7662 {
7663 temp = curwin->w_cursor.col;
7664 if (!can_bs(BS_EOL) /* only if "eol" included */
7665 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7666 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7667 || do_join(FALSE) == FAIL)
7668 vim_beep();
7669 else
7670 curwin->w_cursor.col = temp;
7671 }
7672 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7673 vim_beep();
7674 did_ai = FALSE;
7675#ifdef FEAT_SMARTINDENT
7676 did_si = FALSE;
7677 can_si = FALSE;
7678 can_si_back = FALSE;
7679#endif
7680 AppendCharToRedobuff(K_DEL);
7681}
7682
7683/*
7684 * Handle Backspace, delete-word and delete-line in Insert mode.
7685 * Return TRUE when backspace was actually used.
7686 */
7687 static int
7688ins_bs(c, mode, inserted_space_p)
7689 int c;
7690 int mode;
7691 int *inserted_space_p;
7692{
7693 linenr_T lnum;
7694 int cc;
7695 int temp = 0; /* init for GCC */
7696 colnr_T mincol;
7697 int did_backspace = FALSE;
7698 int in_indent;
7699 int oldState;
7700#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007701 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702#endif
7703
7704 /*
7705 * can't delete anything in an empty file
7706 * can't backup past first character in buffer
7707 * can't backup past starting point unless 'backspace' > 1
7708 * can backup to a previous line if 'backspace' == 0
7709 */
7710 if ( bufempty()
7711 || (
7712#ifdef FEAT_RIGHTLEFT
7713 !revins_on &&
7714#endif
7715 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7716 || (!can_bs(BS_START)
7717 && (arrow_used
7718 || (curwin->w_cursor.lnum == Insstart.lnum
7719 && curwin->w_cursor.col <= Insstart.col)))
7720 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7721 && curwin->w_cursor.col <= ai_col)
7722 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7723 {
7724 vim_beep();
7725 return FALSE;
7726 }
7727
7728 if (stop_arrow() == FAIL)
7729 return FALSE;
7730 in_indent = inindent(0);
7731#ifdef FEAT_CINDENT
7732 if (in_indent)
7733 can_cindent = FALSE;
7734#endif
7735#ifdef FEAT_COMMENTS
7736 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7737#endif
7738#ifdef FEAT_RIGHTLEFT
7739 if (revins_on) /* put cursor after last inserted char */
7740 inc_cursor();
7741#endif
7742
7743#ifdef FEAT_VIRTUALEDIT
7744 /* Virtualedit:
7745 * BACKSPACE_CHAR eats a virtual space
7746 * BACKSPACE_WORD eats all coladd
7747 * BACKSPACE_LINE eats all coladd and keeps going
7748 */
7749 if (curwin->w_cursor.coladd > 0)
7750 {
7751 if (mode == BACKSPACE_CHAR)
7752 {
7753 --curwin->w_cursor.coladd;
7754 return TRUE;
7755 }
7756 if (mode == BACKSPACE_WORD)
7757 {
7758 curwin->w_cursor.coladd = 0;
7759 return TRUE;
7760 }
7761 curwin->w_cursor.coladd = 0;
7762 }
7763#endif
7764
7765 /*
7766 * delete newline!
7767 */
7768 if (curwin->w_cursor.col == 0)
7769 {
7770 lnum = Insstart.lnum;
7771 if (curwin->w_cursor.lnum == Insstart.lnum
7772#ifdef FEAT_RIGHTLEFT
7773 || revins_on
7774#endif
7775 )
7776 {
7777 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7778 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7779 return FALSE;
7780 --Insstart.lnum;
7781 Insstart.col = MAXCOL;
7782 }
7783 /*
7784 * In replace mode:
7785 * cc < 0: NL was inserted, delete it
7786 * cc >= 0: NL was replaced, put original characters back
7787 */
7788 cc = -1;
7789 if (State & REPLACE_FLAG)
7790 cc = replace_pop(); /* returns -1 if NL was inserted */
7791 /*
7792 * In replace mode, in the line we started replacing, we only move the
7793 * cursor.
7794 */
7795 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7796 {
7797 dec_cursor();
7798 }
7799 else
7800 {
7801#ifdef FEAT_VREPLACE
7802 if (!(State & VREPLACE_FLAG)
7803 || curwin->w_cursor.lnum > orig_line_count)
7804#endif
7805 {
7806 temp = gchar_cursor(); /* remember current char */
7807 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007808
7809 /* When "aw" is in 'formatoptions' we must delete the space at
7810 * the end of the line, otherwise the line will be broken
7811 * again when auto-formatting. */
7812 if (has_format_option(FO_AUTO)
7813 && has_format_option(FO_WHITE_PAR))
7814 {
7815 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7816 TRUE);
7817 int len;
7818
7819 len = STRLEN(ptr);
7820 if (len > 0 && ptr[len - 1] == ' ')
7821 ptr[len - 1] = NUL;
7822 }
7823
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 (void)do_join(FALSE);
7825 if (temp == NUL && gchar_cursor() != NUL)
7826 inc_cursor();
7827 }
7828#ifdef FEAT_VREPLACE
7829 else
7830 dec_cursor();
7831#endif
7832
7833 /*
7834 * In REPLACE mode we have to put back the text that was replaced
7835 * by the NL. On the replace stack is first a NUL-terminated
7836 * sequence of characters that were deleted and then the
7837 * characters that NL replaced.
7838 */
7839 if (State & REPLACE_FLAG)
7840 {
7841 /*
7842 * Do the next ins_char() in NORMAL state, to
7843 * prevent ins_char() from replacing characters and
7844 * avoiding showmatch().
7845 */
7846 oldState = State;
7847 State = NORMAL;
7848 /*
7849 * restore characters (blanks) deleted after cursor
7850 */
7851 while (cc > 0)
7852 {
7853 temp = curwin->w_cursor.col;
7854#ifdef FEAT_MBYTE
7855 mb_replace_pop_ins(cc);
7856#else
7857 ins_char(cc);
7858#endif
7859 curwin->w_cursor.col = temp;
7860 cc = replace_pop();
7861 }
7862 /* restore the characters that NL replaced */
7863 replace_pop_ins();
7864 State = oldState;
7865 }
7866 }
7867 did_ai = FALSE;
7868 }
7869 else
7870 {
7871 /*
7872 * Delete character(s) before the cursor.
7873 */
7874#ifdef FEAT_RIGHTLEFT
7875 if (revins_on) /* put cursor on last inserted char */
7876 dec_cursor();
7877#endif
7878 mincol = 0;
7879 /* keep indent */
7880 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7881#ifdef FEAT_RIGHTLEFT
7882 && !revins_on
7883#endif
7884 )
7885 {
7886 temp = curwin->w_cursor.col;
7887 beginline(BL_WHITE);
7888 if (curwin->w_cursor.col < (colnr_T)temp)
7889 mincol = curwin->w_cursor.col;
7890 curwin->w_cursor.col = temp;
7891 }
7892
7893 /*
7894 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7895 */
7896 if ( mode == BACKSPACE_CHAR
7897 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007898 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 && (*(ml_get_cursor() - 1) == TAB
7900 || (*(ml_get_cursor() - 1) == ' '
7901 && (!*inserted_space_p
7902 || arrow_used))))))
7903 {
7904 int ts;
7905 colnr_T vcol;
7906 colnr_T want_vcol;
7907 int extra = 0;
7908
7909 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007910 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 ts = curbuf->b_p_sw;
7912 else
7913 ts = curbuf->b_p_sts;
7914 /* Compute the virtual column where we want to be. Since
7915 * 'showbreak' may get in the way, need to get the last column of
7916 * the previous character. */
7917 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7918 dec_cursor();
7919 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7920 inc_cursor();
7921 want_vcol = (want_vcol / ts) * ts;
7922
7923 /* delete characters until we are at or before want_vcol */
7924 while (vcol > want_vcol
7925 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7926 {
7927 dec_cursor();
7928 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7929 if (State & REPLACE_FLAG)
7930 {
7931 /* Don't delete characters before the insert point when in
7932 * Replace mode */
7933 if (curwin->w_cursor.lnum != Insstart.lnum
7934 || curwin->w_cursor.col >= Insstart.col)
7935 {
7936#if 0 /* what was this for? It causes problems when sw != ts. */
7937 if (State == REPLACE && (int)vcol < want_vcol)
7938 {
7939 (void)del_char(FALSE);
7940 extra = 2; /* don't pop too much */
7941 }
7942 else
7943#endif
7944 replace_do_bs();
7945 }
7946 }
7947 else
7948 (void)del_char(FALSE);
7949 }
7950
7951 /* insert extra spaces until we are at want_vcol */
7952 while (vcol < want_vcol)
7953 {
7954 /* Remember the first char we inserted */
7955 if (curwin->w_cursor.lnum == Insstart.lnum
7956 && curwin->w_cursor.col < Insstart.col)
7957 Insstart.col = curwin->w_cursor.col;
7958
7959#ifdef FEAT_VREPLACE
7960 if (State & VREPLACE_FLAG)
7961 ins_char(' ');
7962 else
7963#endif
7964 {
7965 ins_str((char_u *)" ");
7966 if ((State & REPLACE_FLAG) && extra <= 1)
7967 {
7968 if (extra)
7969 replace_push_off(NUL);
7970 else
7971 replace_push(NUL);
7972 }
7973 if (extra == 2)
7974 extra = 1;
7975 }
7976 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7977 }
7978 }
7979
7980 /*
7981 * Delete upto starting point, start of line or previous word.
7982 */
7983 else do
7984 {
7985#ifdef FEAT_RIGHTLEFT
7986 if (!revins_on) /* put cursor on char to be deleted */
7987#endif
7988 dec_cursor();
7989
7990 /* start of word? */
7991 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
7992 {
7993 mode = BACKSPACE_WORD_NOT_SPACE;
7994 temp = vim_iswordc(gchar_cursor());
7995 }
7996 /* end of word? */
7997 else if (mode == BACKSPACE_WORD_NOT_SPACE
7998 && (vim_isspace(cc = gchar_cursor())
7999 || vim_iswordc(cc) != temp))
8000 {
8001#ifdef FEAT_RIGHTLEFT
8002 if (!revins_on)
8003#endif
8004 inc_cursor();
8005#ifdef FEAT_RIGHTLEFT
8006 else if (State & REPLACE_FLAG)
8007 dec_cursor();
8008#endif
8009 break;
8010 }
8011 if (State & REPLACE_FLAG)
8012 replace_do_bs();
8013 else
8014 {
8015#ifdef FEAT_MBYTE
8016 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008017 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018#endif
8019 (void)del_char(FALSE);
8020#ifdef FEAT_MBYTE
8021 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008022 * If there are combining characters and 'delcombine' is set
8023 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 * character.
8025 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008026 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 inc_cursor();
8028#endif
8029#ifdef FEAT_RIGHTLEFT
8030 if (revins_chars)
8031 {
8032 revins_chars--;
8033 revins_legal++;
8034 }
8035 if (revins_on && gchar_cursor() == NUL)
8036 break;
8037#endif
8038 }
8039 /* Just a single backspace?: */
8040 if (mode == BACKSPACE_CHAR)
8041 break;
8042 } while (
8043#ifdef FEAT_RIGHTLEFT
8044 revins_on ||
8045#endif
8046 (curwin->w_cursor.col > mincol
8047 && (curwin->w_cursor.lnum != Insstart.lnum
8048 || curwin->w_cursor.col != Insstart.col)));
8049 did_backspace = TRUE;
8050 }
8051#ifdef FEAT_SMARTINDENT
8052 did_si = FALSE;
8053 can_si = FALSE;
8054 can_si_back = FALSE;
8055#endif
8056 if (curwin->w_cursor.col <= 1)
8057 did_ai = FALSE;
8058 /*
8059 * It's a little strange to put backspaces into the redo
8060 * buffer, but it makes auto-indent a lot easier to deal
8061 * with.
8062 */
8063 AppendCharToRedobuff(c);
8064
8065 /* If deleted before the insertion point, adjust it */
8066 if (curwin->w_cursor.lnum == Insstart.lnum
8067 && curwin->w_cursor.col < Insstart.col)
8068 Insstart.col = curwin->w_cursor.col;
8069
8070 /* vi behaviour: the cursor moves backward but the character that
8071 * was there remains visible
8072 * Vim behaviour: the cursor moves backward and the character that
8073 * was there is erased from the screen.
8074 * We can emulate the vi behaviour by pretending there is a dollar
8075 * displayed even when there isn't.
8076 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8077 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8078 dollar_vcol = curwin->w_virtcol;
8079
8080 return did_backspace;
8081}
8082
8083#ifdef FEAT_MOUSE
8084 static void
8085ins_mouse(c)
8086 int c;
8087{
8088 pos_T tpos;
8089
8090# ifdef FEAT_GUI
8091 /* When GUI is active, also move/paste when 'mouse' is empty */
8092 if (!gui.in_use)
8093# endif
8094 if (!mouse_has(MOUSE_INSERT))
8095 return;
8096
8097 undisplay_dollar();
8098 tpos = curwin->w_cursor;
8099 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8100 {
8101 start_arrow(&tpos);
8102# ifdef FEAT_CINDENT
8103 can_cindent = TRUE;
8104# endif
8105 }
8106
8107#ifdef FEAT_WINDOWS
8108 /* redraw status lines (in case another window became active) */
8109 redraw_statuslines();
8110#endif
8111}
8112
8113 static void
8114ins_mousescroll(up)
8115 int up;
8116{
8117 pos_T tpos;
8118# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8119 win_T *old_curwin;
8120# endif
8121
8122 tpos = curwin->w_cursor;
8123
8124# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8125 old_curwin = curwin;
8126
8127 /* Currently the mouse coordinates are only known in the GUI. */
8128 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8129 {
8130 int row, col;
8131
8132 row = mouse_row;
8133 col = mouse_col;
8134
8135 /* find the window at the pointer coordinates */
8136 curwin = mouse_find_win(&row, &col);
8137 curbuf = curwin->w_buffer;
8138 }
8139 if (curwin == old_curwin)
8140# endif
8141 undisplay_dollar();
8142
8143 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8144 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8145 else
8146 scroll_redraw(up, 3L);
8147
8148# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8149 curwin->w_redr_status = TRUE;
8150
8151 curwin = old_curwin;
8152 curbuf = curwin->w_buffer;
8153# endif
8154
8155 if (!equalpos(curwin->w_cursor, tpos))
8156 {
8157 start_arrow(&tpos);
8158# ifdef FEAT_CINDENT
8159 can_cindent = TRUE;
8160# endif
8161 }
8162}
8163#endif
8164
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008165#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008166 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008167ins_tabline(c)
8168 int c;
8169{
8170 /* We will be leaving the current window, unless closing another tab. */
8171 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8172 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8173 {
8174 undisplay_dollar();
8175 start_arrow(&curwin->w_cursor);
8176# ifdef FEAT_CINDENT
8177 can_cindent = TRUE;
8178# endif
8179 }
8180
8181 if (c == K_TABLINE)
8182 goto_tabpage(current_tab);
8183 else
8184 handle_tabmenu();
8185
8186}
8187#endif
8188
8189#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 void
8191ins_scroll()
8192{
8193 pos_T tpos;
8194
8195 undisplay_dollar();
8196 tpos = curwin->w_cursor;
8197 if (gui_do_scroll())
8198 {
8199 start_arrow(&tpos);
8200# ifdef FEAT_CINDENT
8201 can_cindent = TRUE;
8202# endif
8203 }
8204}
8205
8206 void
8207ins_horscroll()
8208{
8209 pos_T tpos;
8210
8211 undisplay_dollar();
8212 tpos = curwin->w_cursor;
8213 if (gui_do_horiz_scroll())
8214 {
8215 start_arrow(&tpos);
8216# ifdef FEAT_CINDENT
8217 can_cindent = TRUE;
8218# endif
8219 }
8220}
8221#endif
8222
8223 static void
8224ins_left()
8225{
8226 pos_T tpos;
8227
8228#ifdef FEAT_FOLDING
8229 if ((fdo_flags & FDO_HOR) && KeyTyped)
8230 foldOpenCursor();
8231#endif
8232 undisplay_dollar();
8233 tpos = curwin->w_cursor;
8234 if (oneleft() == OK)
8235 {
8236 start_arrow(&tpos);
8237#ifdef FEAT_RIGHTLEFT
8238 /* If exit reversed string, position is fixed */
8239 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8240 revins_legal++;
8241 revins_chars++;
8242#endif
8243 }
8244
8245 /*
8246 * if 'whichwrap' set for cursor in insert mode may go to
8247 * previous line
8248 */
8249 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8250 {
8251 start_arrow(&tpos);
8252 --(curwin->w_cursor.lnum);
8253 coladvance((colnr_T)MAXCOL);
8254 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8255 }
8256 else
8257 vim_beep();
8258}
8259
8260 static void
8261ins_home(c)
8262 int c;
8263{
8264 pos_T tpos;
8265
8266#ifdef FEAT_FOLDING
8267 if ((fdo_flags & FDO_HOR) && KeyTyped)
8268 foldOpenCursor();
8269#endif
8270 undisplay_dollar();
8271 tpos = curwin->w_cursor;
8272 if (c == K_C_HOME)
8273 curwin->w_cursor.lnum = 1;
8274 curwin->w_cursor.col = 0;
8275#ifdef FEAT_VIRTUALEDIT
8276 curwin->w_cursor.coladd = 0;
8277#endif
8278 curwin->w_curswant = 0;
8279 start_arrow(&tpos);
8280}
8281
8282 static void
8283ins_end(c)
8284 int c;
8285{
8286 pos_T tpos;
8287
8288#ifdef FEAT_FOLDING
8289 if ((fdo_flags & FDO_HOR) && KeyTyped)
8290 foldOpenCursor();
8291#endif
8292 undisplay_dollar();
8293 tpos = curwin->w_cursor;
8294 if (c == K_C_END)
8295 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8296 coladvance((colnr_T)MAXCOL);
8297 curwin->w_curswant = MAXCOL;
8298
8299 start_arrow(&tpos);
8300}
8301
8302 static void
8303ins_s_left()
8304{
8305#ifdef FEAT_FOLDING
8306 if ((fdo_flags & FDO_HOR) && KeyTyped)
8307 foldOpenCursor();
8308#endif
8309 undisplay_dollar();
8310 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8311 {
8312 start_arrow(&curwin->w_cursor);
8313 (void)bck_word(1L, FALSE, FALSE);
8314 curwin->w_set_curswant = TRUE;
8315 }
8316 else
8317 vim_beep();
8318}
8319
8320 static void
8321ins_right()
8322{
8323#ifdef FEAT_FOLDING
8324 if ((fdo_flags & FDO_HOR) && KeyTyped)
8325 foldOpenCursor();
8326#endif
8327 undisplay_dollar();
8328 if (gchar_cursor() != NUL || virtual_active()
8329 )
8330 {
8331 start_arrow(&curwin->w_cursor);
8332 curwin->w_set_curswant = TRUE;
8333#ifdef FEAT_VIRTUALEDIT
8334 if (virtual_active())
8335 oneright();
8336 else
8337#endif
8338 {
8339#ifdef FEAT_MBYTE
8340 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008341 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 else
8343#endif
8344 ++curwin->w_cursor.col;
8345 }
8346
8347#ifdef FEAT_RIGHTLEFT
8348 revins_legal++;
8349 if (revins_chars)
8350 revins_chars--;
8351#endif
8352 }
8353 /* if 'whichwrap' set for cursor in insert mode, may move the
8354 * cursor to the next line */
8355 else if (vim_strchr(p_ww, ']') != NULL
8356 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8357 {
8358 start_arrow(&curwin->w_cursor);
8359 curwin->w_set_curswant = TRUE;
8360 ++curwin->w_cursor.lnum;
8361 curwin->w_cursor.col = 0;
8362 }
8363 else
8364 vim_beep();
8365}
8366
8367 static void
8368ins_s_right()
8369{
8370#ifdef FEAT_FOLDING
8371 if ((fdo_flags & FDO_HOR) && KeyTyped)
8372 foldOpenCursor();
8373#endif
8374 undisplay_dollar();
8375 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8376 || gchar_cursor() != NUL)
8377 {
8378 start_arrow(&curwin->w_cursor);
8379 (void)fwd_word(1L, FALSE, 0);
8380 curwin->w_set_curswant = TRUE;
8381 }
8382 else
8383 vim_beep();
8384}
8385
8386 static void
8387ins_up(startcol)
8388 int startcol; /* when TRUE move to Insstart.col */
8389{
8390 pos_T tpos;
8391 linenr_T old_topline = curwin->w_topline;
8392#ifdef FEAT_DIFF
8393 int old_topfill = curwin->w_topfill;
8394#endif
8395
8396 undisplay_dollar();
8397 tpos = curwin->w_cursor;
8398 if (cursor_up(1L, TRUE) == OK)
8399 {
8400 if (startcol)
8401 coladvance(getvcol_nolist(&Insstart));
8402 if (old_topline != curwin->w_topline
8403#ifdef FEAT_DIFF
8404 || old_topfill != curwin->w_topfill
8405#endif
8406 )
8407 redraw_later(VALID);
8408 start_arrow(&tpos);
8409#ifdef FEAT_CINDENT
8410 can_cindent = TRUE;
8411#endif
8412 }
8413 else
8414 vim_beep();
8415}
8416
8417 static void
8418ins_pageup()
8419{
8420 pos_T tpos;
8421
8422 undisplay_dollar();
8423 tpos = curwin->w_cursor;
8424 if (onepage(BACKWARD, 1L) == OK)
8425 {
8426 start_arrow(&tpos);
8427#ifdef FEAT_CINDENT
8428 can_cindent = TRUE;
8429#endif
8430 }
8431 else
8432 vim_beep();
8433}
8434
8435 static void
8436ins_down(startcol)
8437 int startcol; /* when TRUE move to Insstart.col */
8438{
8439 pos_T tpos;
8440 linenr_T old_topline = curwin->w_topline;
8441#ifdef FEAT_DIFF
8442 int old_topfill = curwin->w_topfill;
8443#endif
8444
8445 undisplay_dollar();
8446 tpos = curwin->w_cursor;
8447 if (cursor_down(1L, TRUE) == OK)
8448 {
8449 if (startcol)
8450 coladvance(getvcol_nolist(&Insstart));
8451 if (old_topline != curwin->w_topline
8452#ifdef FEAT_DIFF
8453 || old_topfill != curwin->w_topfill
8454#endif
8455 )
8456 redraw_later(VALID);
8457 start_arrow(&tpos);
8458#ifdef FEAT_CINDENT
8459 can_cindent = TRUE;
8460#endif
8461 }
8462 else
8463 vim_beep();
8464}
8465
8466 static void
8467ins_pagedown()
8468{
8469 pos_T tpos;
8470
8471 undisplay_dollar();
8472 tpos = curwin->w_cursor;
8473 if (onepage(FORWARD, 1L) == OK)
8474 {
8475 start_arrow(&tpos);
8476#ifdef FEAT_CINDENT
8477 can_cindent = TRUE;
8478#endif
8479 }
8480 else
8481 vim_beep();
8482}
8483
8484#ifdef FEAT_DND
8485 static void
8486ins_drop()
8487{
8488 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8489}
8490#endif
8491
8492/*
8493 * Handle TAB in Insert or Replace mode.
8494 * Return TRUE when the TAB needs to be inserted like a normal character.
8495 */
8496 static int
8497ins_tab()
8498{
8499 int ind;
8500 int i;
8501 int temp;
8502
8503 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8504 Insstart_blank_vcol = get_nolist_virtcol();
8505 if (echeck_abbr(TAB + ABBR_OFF))
8506 return FALSE;
8507
8508 ind = inindent(0);
8509#ifdef FEAT_CINDENT
8510 if (ind)
8511 can_cindent = FALSE;
8512#endif
8513
8514 /*
8515 * When nothing special, insert TAB like a normal character
8516 */
8517 if (!curbuf->b_p_et
8518 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8519 && curbuf->b_p_sts == 0)
8520 return TRUE;
8521
8522 if (stop_arrow() == FAIL)
8523 return TRUE;
8524
8525 did_ai = FALSE;
8526#ifdef FEAT_SMARTINDENT
8527 did_si = FALSE;
8528 can_si = FALSE;
8529 can_si_back = FALSE;
8530#endif
8531 AppendToRedobuff((char_u *)"\t");
8532
8533 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8534 temp = (int)curbuf->b_p_sw;
8535 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8536 temp = (int)curbuf->b_p_sts;
8537 else /* otherwise use 'tabstop' */
8538 temp = (int)curbuf->b_p_ts;
8539 temp -= get_nolist_virtcol() % temp;
8540
8541 /*
8542 * Insert the first space with ins_char(). It will delete one char in
8543 * replace mode. Insert the rest with ins_str(); it will not delete any
8544 * chars. For VREPLACE mode, we use ins_char() for all characters.
8545 */
8546 ins_char(' ');
8547 while (--temp > 0)
8548 {
8549#ifdef FEAT_VREPLACE
8550 if (State & VREPLACE_FLAG)
8551 ins_char(' ');
8552 else
8553#endif
8554 {
8555 ins_str((char_u *)" ");
8556 if (State & REPLACE_FLAG) /* no char replaced */
8557 replace_push(NUL);
8558 }
8559 }
8560
8561 /*
8562 * When 'expandtab' not set: Replace spaces by TABs where possible.
8563 */
8564 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8565 {
8566 char_u *ptr;
8567#ifdef FEAT_VREPLACE
8568 char_u *saved_line = NULL; /* init for GCC */
8569 pos_T pos;
8570#endif
8571 pos_T fpos;
8572 pos_T *cursor;
8573 colnr_T want_vcol, vcol;
8574 int change_col = -1;
8575 int save_list = curwin->w_p_list;
8576
8577 /*
8578 * Get the current line. For VREPLACE mode, don't make real changes
8579 * yet, just work on a copy of the line.
8580 */
8581#ifdef FEAT_VREPLACE
8582 if (State & VREPLACE_FLAG)
8583 {
8584 pos = curwin->w_cursor;
8585 cursor = &pos;
8586 saved_line = vim_strsave(ml_get_curline());
8587 if (saved_line == NULL)
8588 return FALSE;
8589 ptr = saved_line + pos.col;
8590 }
8591 else
8592#endif
8593 {
8594 ptr = ml_get_cursor();
8595 cursor = &curwin->w_cursor;
8596 }
8597
8598 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8599 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8600 curwin->w_p_list = FALSE;
8601
8602 /* Find first white before the cursor */
8603 fpos = curwin->w_cursor;
8604 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8605 {
8606 --fpos.col;
8607 --ptr;
8608 }
8609
8610 /* In Replace mode, don't change characters before the insert point. */
8611 if ((State & REPLACE_FLAG)
8612 && fpos.lnum == Insstart.lnum
8613 && fpos.col < Insstart.col)
8614 {
8615 ptr += Insstart.col - fpos.col;
8616 fpos.col = Insstart.col;
8617 }
8618
8619 /* compute virtual column numbers of first white and cursor */
8620 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8621 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8622
8623 /* Use as many TABs as possible. Beware of 'showbreak' and
8624 * 'linebreak' adding extra virtual columns. */
8625 while (vim_iswhite(*ptr))
8626 {
8627 i = lbr_chartabsize((char_u *)"\t", vcol);
8628 if (vcol + i > want_vcol)
8629 break;
8630 if (*ptr != TAB)
8631 {
8632 *ptr = TAB;
8633 if (change_col < 0)
8634 {
8635 change_col = fpos.col; /* Column of first change */
8636 /* May have to adjust Insstart */
8637 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8638 Insstart.col = fpos.col;
8639 }
8640 }
8641 ++fpos.col;
8642 ++ptr;
8643 vcol += i;
8644 }
8645
8646 if (change_col >= 0)
8647 {
8648 int repl_off = 0;
8649
8650 /* Skip over the spaces we need. */
8651 while (vcol < want_vcol && *ptr == ' ')
8652 {
8653 vcol += lbr_chartabsize(ptr, vcol);
8654 ++ptr;
8655 ++repl_off;
8656 }
8657 if (vcol > want_vcol)
8658 {
8659 /* Must have a char with 'showbreak' just before it. */
8660 --ptr;
8661 --repl_off;
8662 }
8663 fpos.col += repl_off;
8664
8665 /* Delete following spaces. */
8666 i = cursor->col - fpos.col;
8667 if (i > 0)
8668 {
8669 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8670 /* correct replace stack. */
8671 if ((State & REPLACE_FLAG)
8672#ifdef FEAT_VREPLACE
8673 && !(State & VREPLACE_FLAG)
8674#endif
8675 )
8676 for (temp = i; --temp >= 0; )
8677 replace_join(repl_off);
8678 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008679#ifdef FEAT_NETBEANS_INTG
8680 if (usingNetbeans)
8681 {
8682 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8683 (long)(i + 1));
8684 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8685 (char_u *)"\t", 1);
8686 }
8687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 cursor->col -= i;
8689
8690#ifdef FEAT_VREPLACE
8691 /*
8692 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8693 * backspacing over the changed spacing and then inserting the new
8694 * spacing.
8695 */
8696 if (State & VREPLACE_FLAG)
8697 {
8698 /* Backspace from real cursor to change_col */
8699 backspace_until_column(change_col);
8700
8701 /* Insert each char in saved_line from changed_col to
8702 * ptr-cursor */
8703 ins_bytes_len(saved_line + change_col,
8704 cursor->col - change_col);
8705 }
8706#endif
8707 }
8708
8709#ifdef FEAT_VREPLACE
8710 if (State & VREPLACE_FLAG)
8711 vim_free(saved_line);
8712#endif
8713 curwin->w_p_list = save_list;
8714 }
8715
8716 return FALSE;
8717}
8718
8719/*
8720 * Handle CR or NL in insert mode.
8721 * Return TRUE when out of memory or can't undo.
8722 */
8723 static int
8724ins_eol(c)
8725 int c;
8726{
8727 int i;
8728
8729 if (echeck_abbr(c + ABBR_OFF))
8730 return FALSE;
8731 if (stop_arrow() == FAIL)
8732 return TRUE;
8733 undisplay_dollar();
8734
8735 /*
8736 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8737 * character under the cursor. Only push a NUL on the replace stack,
8738 * nothing to put back when the NL is deleted.
8739 */
8740 if ((State & REPLACE_FLAG)
8741#ifdef FEAT_VREPLACE
8742 && !(State & VREPLACE_FLAG)
8743#endif
8744 )
8745 replace_push(NUL);
8746
8747 /*
8748 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8749 * replacing the next line, so we push all of the characters left on the
8750 * line onto the replace stack. This is not done here though, it is done
8751 * in open_line().
8752 */
8753
8754#ifdef FEAT_RIGHTLEFT
8755# ifdef FEAT_FKMAP
8756 if (p_altkeymap && p_fkmap)
8757 fkmap(NL);
8758# endif
8759 /* NL in reverse insert will always start in the end of
8760 * current line. */
8761 if (revins_on)
8762 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8763#endif
8764
8765 AppendToRedobuff(NL_STR);
8766 i = open_line(FORWARD,
8767#ifdef FEAT_COMMENTS
8768 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8769#endif
8770 0, old_indent);
8771 old_indent = 0;
8772#ifdef FEAT_CINDENT
8773 can_cindent = TRUE;
8774#endif
8775
8776 return (!i);
8777}
8778
8779#ifdef FEAT_DIGRAPHS
8780/*
8781 * Handle digraph in insert mode.
8782 * Returns character still to be inserted, or NUL when nothing remaining to be
8783 * done.
8784 */
8785 static int
8786ins_digraph()
8787{
8788 int c;
8789 int cc;
8790
8791 pc_status = PC_STATUS_UNSET;
8792 if (redrawing() && !char_avail())
8793 {
8794 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008795 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796
8797 edit_putchar('?', TRUE);
8798#ifdef FEAT_CMDL_INFO
8799 add_to_showcmd_c(Ctrl_K);
8800#endif
8801 }
8802
8803#ifdef USE_ON_FLY_SCROLL
8804 dont_scroll = TRUE; /* disallow scrolling here */
8805#endif
8806
8807 /* don't map the digraph chars. This also prevents the
8808 * mode message to be deleted when ESC is hit */
8809 ++no_mapping;
8810 ++allow_keys;
8811 c = safe_vgetc();
8812 --no_mapping;
8813 --allow_keys;
8814 if (IS_SPECIAL(c) || mod_mask) /* special key */
8815 {
8816#ifdef FEAT_CMDL_INFO
8817 clear_showcmd();
8818#endif
8819 insert_special(c, TRUE, FALSE);
8820 return NUL;
8821 }
8822 if (c != ESC)
8823 {
8824 if (redrawing() && !char_avail())
8825 {
8826 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008827 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828
8829 if (char2cells(c) == 1)
8830 {
8831 /* first remove the '?', otherwise it's restored when typing
8832 * an ESC next */
8833 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008834 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 edit_putchar(c, TRUE);
8836 }
8837#ifdef FEAT_CMDL_INFO
8838 add_to_showcmd_c(c);
8839#endif
8840 }
8841 ++no_mapping;
8842 ++allow_keys;
8843 cc = safe_vgetc();
8844 --no_mapping;
8845 --allow_keys;
8846 if (cc != ESC)
8847 {
8848 AppendToRedobuff((char_u *)CTRL_V_STR);
8849 c = getdigraph(c, cc, TRUE);
8850#ifdef FEAT_CMDL_INFO
8851 clear_showcmd();
8852#endif
8853 return c;
8854 }
8855 }
8856 edit_unputchar();
8857#ifdef FEAT_CMDL_INFO
8858 clear_showcmd();
8859#endif
8860 return NUL;
8861}
8862#endif
8863
8864/*
8865 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8866 * Returns the char to be inserted, or NUL if none found.
8867 */
8868 static int
8869ins_copychar(lnum)
8870 linenr_T lnum;
8871{
8872 int c;
8873 int temp;
8874 char_u *ptr, *prev_ptr;
8875
8876 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8877 {
8878 vim_beep();
8879 return NUL;
8880 }
8881
8882 /* try to advance to the cursor column */
8883 temp = 0;
8884 ptr = ml_get(lnum);
8885 prev_ptr = ptr;
8886 validate_virtcol();
8887 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8888 {
8889 prev_ptr = ptr;
8890 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8891 }
8892 if ((colnr_T)temp > curwin->w_virtcol)
8893 ptr = prev_ptr;
8894
8895#ifdef FEAT_MBYTE
8896 c = (*mb_ptr2char)(ptr);
8897#else
8898 c = *ptr;
8899#endif
8900 if (c == NUL)
8901 vim_beep();
8902 return c;
8903}
8904
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008905/*
8906 * CTRL-Y or CTRL-E typed in Insert mode.
8907 */
8908 static int
8909ins_ctrl_ey(tc)
8910 int tc;
8911{
8912 int c = tc;
8913
8914#ifdef FEAT_INS_EXPAND
8915 if (ctrl_x_mode == CTRL_X_SCROLL)
8916 {
8917 if (c == Ctrl_Y)
8918 scrolldown_clamp();
8919 else
8920 scrollup_clamp();
8921 redraw_later(VALID);
8922 }
8923 else
8924#endif
8925 {
8926 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8927 if (c != NUL)
8928 {
8929 long tw_save;
8930
8931 /* The character must be taken literally, insert like it
8932 * was typed after a CTRL-V, and pretend 'textwidth'
8933 * wasn't set. Digits, 'o' and 'x' are special after a
8934 * CTRL-V, don't use it for these. */
8935 if (c < 256 && !isalnum(c))
8936 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8937 tw_save = curbuf->b_p_tw;
8938 curbuf->b_p_tw = -1;
8939 insert_special(c, TRUE, FALSE);
8940 curbuf->b_p_tw = tw_save;
8941#ifdef FEAT_RIGHTLEFT
8942 revins_chars++;
8943 revins_legal++;
8944#endif
8945 c = Ctrl_V; /* pretend CTRL-V is last character */
8946 auto_format(FALSE, TRUE);
8947 }
8948 }
8949 return c;
8950}
8951
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952#ifdef FEAT_SMARTINDENT
8953/*
8954 * Try to do some very smart auto-indenting.
8955 * Used when inserting a "normal" character.
8956 */
8957 static void
8958ins_try_si(c)
8959 int c;
8960{
8961 pos_T *pos, old_pos;
8962 char_u *ptr;
8963 int i;
8964 int temp;
8965
8966 /*
8967 * do some very smart indenting when entering '{' or '}'
8968 */
8969 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8970 {
8971 /*
8972 * for '}' set indent equal to indent of line containing matching '{'
8973 */
8974 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
8975 {
8976 old_pos = curwin->w_cursor;
8977 /*
8978 * If the matching '{' has a ')' immediately before it (ignoring
8979 * white-space), then line up with the start of the line
8980 * containing the matching '(' if there is one. This handles the
8981 * case where an "if (..\n..) {" statement continues over multiple
8982 * lines -- webb
8983 */
8984 ptr = ml_get(pos->lnum);
8985 i = pos->col;
8986 if (i > 0) /* skip blanks before '{' */
8987 while (--i > 0 && vim_iswhite(ptr[i]))
8988 ;
8989 curwin->w_cursor.lnum = pos->lnum;
8990 curwin->w_cursor.col = i;
8991 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
8992 curwin->w_cursor = *pos;
8993 i = get_indent();
8994 curwin->w_cursor = old_pos;
8995#ifdef FEAT_VREPLACE
8996 if (State & VREPLACE_FLAG)
8997 change_indent(INDENT_SET, i, FALSE, NUL);
8998 else
8999#endif
9000 (void)set_indent(i, SIN_CHANGED);
9001 }
9002 else if (curwin->w_cursor.col > 0)
9003 {
9004 /*
9005 * when inserting '{' after "O" reduce indent, but not
9006 * more than indent of previous line
9007 */
9008 temp = TRUE;
9009 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9010 {
9011 old_pos = curwin->w_cursor;
9012 i = get_indent();
9013 while (curwin->w_cursor.lnum > 1)
9014 {
9015 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9016
9017 /* ignore empty lines and lines starting with '#'. */
9018 if (*ptr != '#' && *ptr != NUL)
9019 break;
9020 }
9021 if (get_indent() >= i)
9022 temp = FALSE;
9023 curwin->w_cursor = old_pos;
9024 }
9025 if (temp)
9026 shift_line(TRUE, FALSE, 1);
9027 }
9028 }
9029
9030 /*
9031 * set indent of '#' always to 0
9032 */
9033 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9034 {
9035 /* remember current indent for next line */
9036 old_indent = get_indent();
9037 (void)set_indent(0, SIN_CHANGED);
9038 }
9039
9040 /* Adjust ai_col, the char at this position can be deleted. */
9041 if (ai_col > curwin->w_cursor.col)
9042 ai_col = curwin->w_cursor.col;
9043}
9044#endif
9045
9046/*
9047 * Get the value that w_virtcol would have when 'list' is off.
9048 * Unless 'cpo' contains the 'L' flag.
9049 */
9050 static colnr_T
9051get_nolist_virtcol()
9052{
9053 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9054 return getvcol_nolist(&curwin->w_cursor);
9055 validate_virtcol();
9056 return curwin->w_virtcol;
9057}